@strapi/data-transfer
Version:
Data transfer capabilities for Strapi
53 lines (50 loc) • 2.33 kB
JavaScript
import { Writable } from 'stream';
import { ProviderTransferError } from '../../../../../errors/providers.mjs';
import { createEntityQuery } from '../../../../queries/entity.mjs';
import 'lodash/fp';
import { collectComponentIdMappings } from '../../../../../utils/components.mjs';
const createEntitiesWriteStream = (options)=>{
const { strapi, updateMappingTable, transaction } = options;
const query = createEntityQuery(strapi);
return new 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 = 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 ProviderTransferError(`Failed to create "${type}" (${id})`));
}
return callback(null);
});
}
});
};
export { createEntitiesWriteStream };
//# sourceMappingURL=entities.mjs.map