UNPKG

@strapi/data-transfer

Version:

Data transfer capabilities for Strapi

55 lines (51 loc) 2.36 kB
'use strict'; var stream = require('stream'); var providers = require('../../../../../errors/providers.js'); var entity = require('../../../../queries/entity.js'); require('lodash/fp'); var components = require('../../../../../utils/components.js'); const createEntitiesWriteStream = (options)=>{ const { strapi, updateMappingTable, transaction } = options; const query = entity.createEntityQuery(strapi); return new stream.Writable({ objectMode: true, async write (entity, _encoding, callback) { await transaction?.attach(async ()=>{ const { type, id, data } = entity; const { create, getDeepPopulateComponentLikeQuery } = query(type); const contentType = strapi.getModel(type); try { const created = await create({ data, populate: getDeepPopulateComponentLikeQuery(contentType, { select: 'id' }), select: 'id' }); updateMappingTable(type, id, created.id); // Register the ID of every component instance that was re-created // with the entity — including the ones whose ID did not change — so // that the links stage can resolve component-side references and // tell transferred components apart from missing/orphaned ones const componentMappings = components.collectComponentIdMappings({ data, created, schema: contentType, strapi }); for (const { uid, oldID, newID } of componentMappings){ updateMappingTable(uid, oldID, newID); } } catch (e) { if (e instanceof Error) { return callback(e); } return callback(new providers.ProviderTransferError(`Failed to create "${type}" (${id})`)); } return callback(null); }); } }); }; exports.createEntitiesWriteStream = createEntitiesWriteStream; //# sourceMappingURL=entities.js.map