Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/server/services/import/import-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ type ImportFailures = {
data: any;
};

type ImportSuccess = {
/** Result entity. */
result: Object;
/** Data imported. */
data: any;
};

class IdMapper {
private mapping: {
[slug in SchemaUID]?: Map<string | number, string | number>;
Expand Down Expand Up @@ -174,7 +181,7 @@ const importContentTypeSlug = async (
fileIdToDbId,
componentsDataStore,
}: { slug: SchemaUID; user: User; idField?: string; importStage: ImportStage; fileIdToDbId: IdMapper; componentsDataStore: Partial<Record<SchemaUID, SlugEntries>> },
): Promise<{ failures: ImportFailures[] }> => {
): Promise<{ failures: ImportFailures[]; success: ImportSuccess[] }> => {
let fileEntries = toPairs(slugEntries);

// Sort localized data with default locale first.
Expand All @@ -196,9 +203,11 @@ const importContentTypeSlug = async (
await sortDataByLocale();

const failures: ImportFailures[] = [];
const success: ImportSuccess[] = [];
for (let [fileId, fileEntry] of fileEntries) {
try {
await updateOrCreate(user, slug, fileId, fileEntry, idField, { importStage, fileIdToDbId, componentsDataStore });
const result = await updateOrCreate(user, slug, fileId, fileEntry, idField, { importStage, fileIdToDbId, componentsDataStore });
success.push({ result, data: fileEntry });
} catch (err: any) {
strapi.log.error(err);
failures.push({ error: err, data: fileEntry });
Expand All @@ -207,6 +216,7 @@ const importContentTypeSlug = async (

return {
failures,
success,
};
};

Expand Down Expand Up @@ -247,6 +257,8 @@ const updateOrCreate = async (
if (dbEntry) {
fileIdToDbId.setMapping(slug, fileId, dbEntry.id);
}

return dbEntry;
};

function linkMediaAttributes(schema: Schema, fileEntry: FileEntry, { fileIdToDbId }: { fileIdToDbId: IdMapper }) {
Expand Down Expand Up @@ -397,7 +409,7 @@ const updateOrCreateCollectionTypeEntry = async (
if (!schema.pluginOptions?.i18n?.localized) {
let dbEntry: Entry = await strapi.db.query(slug).findOne({ where });

if (!dbEntry) {
if (!dbEntry || isEmpty(where)) {
return strapi.entityService.create(slug, { data: fileEntry });
} else {
return strapi.entityService.update(slug, dbEntry.id, { data: omit(fileEntry, ['id']) });
Expand Down