fix(extract): handle singleton collections in getCollectionItems (#144)#146
Conversation
|
This writes the output into an array in the json which may impact the loading function too |
…Items A singleton returns a single object; spreading it crashes (directus-labs#144). Return the object as-is so the file stays an object — loadSingletons reads it back as an object, so wrapping it in an array silently drops the singleton's data on apply. getDataFromCollection normalises to an array for the relation-stripping passes but writes the original shape.
|
You're completely right. I went and checked on a real instance and it's actually a bit worse than the crash: with the array wrap, loadSingletons reads the file back as an object, so the array comes through as updateSingleton(collection, {0: {…}}) and the singleton's data just quietly gets dropped on apply, no error at all. So I've switched it to write the singleton as an object instead, and normalise to an array only for the relation-stripping pass. On a test instance that gives a clean extract (the singleton as an object, array collections untouched) and the apply round-trips fine. Does that approach look right to you, or would you rather handle singletons a different way, like detecting them and using readSingleton? |
59e5542 to
a586e98
Compare
Fixes #144.
extract(with content) crashes when the source instance has singleton collections, becausegetCollectionItemsassumesreadItems()always returns an array:For a singleton, the SDK resolves to the single item object, so
...responsethrowsSpread syntax requires ...iterable[Symbol.iterator] to be a functionandresponse.lengthisundefined. The extract aborts at the first singleton (alphabetically) and writes no content for it or any collection after it.Fix
Guard the non-array response — wrap a singleton object in an array:
Verification
tsc -bbuilds clean.extractwith content completes on an instance that has singleton collections (reproduced with Agency OS'sglobals,os_settings,pages_blog,pages_projects). The crash was independently confirmed by another reporter in extract crashes on singleton collections (getCollectionItems spreads a non-array response) #144 on a different Node version.Single-file change; non-singleton collections keep the existing array path unchanged.