@strapi/data-transfer
Version:
Data transfer capabilities for Strapi
1 lines • 4.3 kB
Source Map (JSON)
{"version":3,"file":"entities.mjs","sources":["../../../../../../src/strapi/providers/local-destination/strategies/restore/entities.ts"],"sourcesContent":["import { Writable } from 'stream';\nimport type { Core, UID } from '@strapi/types';\n\nimport { ProviderTransferError } from '../../../../../errors/providers';\nimport type { IEntity, Transaction } from '../../../../../types';\nimport * as queries from '../../../../queries';\nimport { collectComponentIdMappings } from '../../../../../utils/components';\n\ninterface IEntitiesRestoreStreamOptions {\n strapi: Core.Strapi;\n updateMappingTable<TSchemaUID extends UID.Schema>(\n type: TSchemaUID,\n oldID: number,\n newID: number\n ): void;\n transaction?: Transaction;\n}\n\nexport const createEntitiesWriteStream = (options: IEntitiesRestoreStreamOptions) => {\n const { strapi, updateMappingTable, transaction } = options;\n const query = queries.entity.createEntityQuery(strapi);\n\n return new Writable({\n objectMode: true,\n\n async write(entity: IEntity, _encoding, callback) {\n await transaction?.attach(async () => {\n const { type, id, data } = entity;\n const { create, getDeepPopulateComponentLikeQuery } = query(type);\n const contentType = strapi.getModel(type);\n\n try {\n const created = await create({\n data,\n populate: getDeepPopulateComponentLikeQuery(contentType, { select: 'id' }),\n select: 'id',\n });\n\n updateMappingTable(type, id, created.id);\n\n // Register the ID of every component instance that was re-created\n // with the entity — including the ones whose ID did not change — so\n // that the links stage can resolve component-side references and\n // tell transferred components apart from missing/orphaned ones\n const componentMappings = collectComponentIdMappings({\n data,\n created,\n schema: contentType,\n strapi,\n });\n\n for (const { uid, oldID, newID } of componentMappings) {\n updateMappingTable(uid, oldID, newID);\n }\n } catch (e) {\n if (e instanceof Error) {\n return callback(e);\n }\n\n return callback(new ProviderTransferError(`Failed to create \"${type}\" (${id})`));\n }\n\n return callback(null);\n });\n },\n });\n};\n"],"names":["createEntitiesWriteStream","options","strapi","updateMappingTable","transaction","query","queries","Writable","objectMode","write","entity","_encoding","callback","attach","type","id","data","create","getDeepPopulateComponentLikeQuery","contentType","getModel","created","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