diff --git a/src/lib/extract/extract-content.ts b/src/lib/extract/extract-content.ts index 3765996..a70f5f0 100644 --- a/src/lib/extract/extract-content.ts +++ b/src/lib/extract/extract-content.ts @@ -1,4 +1,4 @@ -import {readCollections, readItems, readRelations} from '@directus/sdk' +import {readCollection, readCollections, readItems, readRelations, readSingleton} from '@directus/sdk' import {Errors, ux} from '@oclif/core' import {DIRECTUS_PINK} from '../constants.js' @@ -42,10 +42,18 @@ async function getCollections(relations: RelationInfo[], plan?: TemplatePlan) { .filter((collection) => !brokenJunctions.has(collection)) } -async function getCollectionItems(collection: string): Promise[]> { +async function getCollectionItems(collection: string): Promise | Record[]> { const items: Record[] = [] let page = 1 + // Check if collection is a singleton + const collectionMeta = await api.client.request(readCollection(collection)) + + if (collectionMeta.meta.singleton) { + const response = await api.client.request(readSingleton(collection)) + return response + } + while (true) { // eslint-disable-next-line no-await-in-loop const response = (await api.client.request(readItems(collection as never, {limit: PAGE_SIZE, page}))) as Record< @@ -139,12 +147,14 @@ async function getDataFromCollection( const response = await getCollectionItems(collection) const excludedRelations = getExcludedRelationFields(collection, relations, plan) + const items = Array.isArray(response) ? response : [response] + if (plan?.relationStrategy === 'empty') { - emptyExcludedRelations(response, excludedRelations) + emptyExcludedRelations(items, excludedRelations) } const warnings = - plan?.relationStrategy === 'preserve' ? getBrokenRelationWarnings(collection, response, excludedRelations) : [] + plan?.relationStrategy === 'preserve' ? getBrokenRelationWarnings(collection, items, excludedRelations) : [] await writeToFile(`${collection}`, response, `${dir}/content/`) return warnings