Last active
August 23, 2022 11:36
-
-
Save cachrisman/1aa838f3642f52b88ccb73e5b0e4bd80 to your computer and use it in GitHub Desktop.
Query Contentful Space Environment for Content Type usage by number of entries.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async () => { | |
const contentful = require('contentful-management') | |
const parallel = require("async-parallel"); | |
const client = contentful.createClient({ | |
accessToken: '<CMA_TOKEN>' | |
}) | |
const space_id = '<SPACE_ID>' | |
const environment_id = '<ENV_ID>' | |
const space = await client.getSpace(space_id) | |
const environment = await space.getEnvironment(environment_id) | |
const getContentTypes = async () => { | |
const content_types = await environment.getContentTypes({limit:1000}) | |
return content_types.items | |
} | |
const calculateUsage = async (content_type) => { | |
try { | |
const response = await environment.getEntries({ | |
limit: 1000, | |
content_type: content_type.sys.id, | |
order: 'sys.createdAt' | |
}) | |
const entries = response.items | |
let createdAt = null, | |
updatedAt = null | |
if (!!entries.length) { | |
createdAt = entries[0].sys.createdAt | |
updatedAt = entries.sort((a,b) => new Date(a.sys.updatedAt) - new Date(a.sys.updatedAt))[0].sys.updatedAt | |
} | |
return { | |
name: content_type.sys.id, | |
quantity: response.total, | |
most_recently_created: createdAt, | |
most_recently_updated: updatedAt | |
} | |
} catch (error) { | |
console.log(error) | |
return {} | |
} | |
} | |
const content_types = await getContentTypes() | |
parallel.setConcurrency(3); | |
const usage = await parallel.map(content_types, calculateUsage) | |
usage.sort((a,b) => new Date(b.most_recently_created) - new Date(a.most_recently_created)) | |
console.log("\n================================================= Usage =================================================\n") | |
console.table(usage) | |
})() |
@sahithibol I've made a separate repo with instructions that is more complete: https://github.com/cachrisman/content-type-usage-query
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@cachrisman We have not found any command here to run this script can u share the steps to run this script here please.