UNPKG

@strapi/data-transfer

Version:

Data transfer capabilities for Strapi

1 lines 4.8 kB
{"version":3,"file":"entities.mjs","sources":["../../../../src/strapi/providers/local-source/entities.ts"],"sourcesContent":["import { Readable, Transform } from 'stream';\nimport type { Core, Struct } from '@strapi/types';\n\nimport * as shared from '../../queries';\nimport { IEntity } from '../../../types';\n\n/**\n * Generate and consume content-types streams in order to stream each entity individually\n */\nexport const createEntitiesStream = (\n strapi: Core.Strapi,\n options: { onWarning?: (message: string) => void } = {}\n): Readable => {\n const contentTypes: Struct.ContentTypeSchema[] = Object.values(strapi.contentTypes);\n\n async function* contentTypeStreamGenerator() {\n for (const contentType of contentTypes) {\n const query = shared.entity.createEntityQuery(strapi).call(null, contentType.uid);\n\n const stream: Readable = strapi.db\n // Create a query builder instance (default type is 'select')\n .queryBuilder(contentType.uid)\n // Fetch all columns\n .select('*')\n // Apply the populate\n .populate(query.deepPopulateComponentLikeQuery)\n // Get a readable stream\n .stream();\n\n yield { contentType, stream };\n }\n }\n\n return Readable.from(\n (async function* entitiesGenerator(): AsyncGenerator<{\n entity: IEntity;\n contentType: Struct.ContentTypeSchema;\n }> {\n for await (const { stream, contentType } of contentTypeStreamGenerator()) {\n try {\n for await (const entity of stream) {\n yield { entity, contentType };\n }\n } catch (error) {\n // Surface the failure instead of silently dropping the remaining\n // entities of this content type: every entity skipped here leaves\n // dangling links pointing at rows that were never transferred\n const message = error instanceof Error ? error.message : String(error);\n\n options.onWarning?.(\n `Failed to read all entities of type \"${contentType.uid}\" from the source, the remaining entities of this type were skipped: ${message}`\n );\n } finally {\n stream.destroy();\n }\n }\n })()\n );\n};\n\n/**\n * Create an entity transform stream which convert the output of\n * the multi-content-types stream to the transfer entity format\n */\nexport const createEntitiesTransformStream = (): Transform => {\n return new Transform({\n objectMode: true,\n transform(data, _encoding, callback) {\n const { entity, contentType } = data;\n const { id, ...attributes } = entity;\n\n callback(null, {\n type: contentType.uid,\n id,\n data: attributes,\n });\n },\n });\n};\n"],"names":["createEntitiesStream","strapi","options","contentTypes","Object","values","contentTypeStreamGenerator","contentType","query","shared","call","uid","stream","db","queryBuilder","select","populate","deepPopulateComponentLikeQuery","Readable","from","entitiesGenerator","entity","error","message","Error","String","onWarning","destroy","createEntitiesTransformStream","Transform","objectMode","transform","data","_encoding","callback","id","attributes","type"],"mappings":";;;;AAMA;;AAEC,IACM,MAAMA,oBAAAA,GAAuB,CAClCC,MAAAA,EACAC,OAAAA,GAAqD,EAAE,GAAA;AAEvD,IAAA,MAAMC,YAAAA,GAA2CC,MAAAA,CAAOC,MAAM,CAACJ,OAAOE,YAAY,CAAA;IAElF,gBAAgBG,0BAAAA,GAAAA;QACd,KAAK,MAAMC,eAAeJ,YAAAA,CAAc;YACtC,MAAMK,KAAAA,GAAQC,iBAA+B,CAACR,MAAAA,CAAAA,CAAQS,IAAI,CAAC,IAAA,EAAMH,WAAAA,CAAYI,GAAG,CAAA;AAEhF,YAAA,MAAMC,MAAAA,GAAmBX,MAAAA,CAAOY,EAC9B;AACCC,aAAAA,YAAY,CAACP,WAAAA,CAAYI,GAAG,CAC7B;aACCI,MAAM,CAAC,IACR;AACCC,aAAAA,QAAQ,CAACR,KAAAA,CAAMS,8BAA8B,CAC9C;aACCL,MAAM,EAAA;YAET,MAAM;AAAEL,gBAAAA,WAAAA;AAAaK,gBAAAA;AAAO,aAAA;AAC9B,QAAA;AACF,IAAA;AAEA,IAAA,OAAOM,QAAAA,CAASC,IAAI,CACjB,gBAAgBC,iBAAAA,GAAAA;AAIf,QAAA,WAAW,MAAM,EAAER,MAAM,EAAEL,WAAW,EAAE,IAAID,0BAAAA,EAAAA,CAA8B;YACxE,IAAI;gBACF,WAAW,MAAMe,UAAUT,MAAAA,CAAQ;oBACjC,MAAM;AAAES,wBAAAA,MAAAA;AAAQd,wBAAAA;AAAY,qBAAA;AAC9B,gBAAA;AACF,YAAA,CAAA,CAAE,OAAOe,KAAAA,EAAO;;;;AAId,gBAAA,MAAMC,UAAUD,KAAAA,YAAiBE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