diff --git a/README.md b/README.md index f14ca3f..dfb0f64 100644 --- a/README.md +++ b/README.md @@ -126,11 +126,36 @@ const run = async () => { // called when any content is imported console.log(`Imported: ${item.title} | ${item.type}`); }, - fixLanguages: true, + canImport: { + asset: (item) => { + if(item.title.startsWith('_corporate')) { + // asset will be imported only if the title starts with "_corporate" + return true; + } + // otherwise asset will NOT be imported + return false; + }, + contentType: (item) => { + if (item.codename === 'article') { + // content type will be imported only with its codename is 'article' + return true; + } + // all other types will be excluded from import + return false; + }, + assetFolder: item => true, // all folders will be imported + contentItem: item => true, // all content items will be imported + contentTypeSnippet: item => true, // all content type snippets will be imported + language: item => true, // all languages will be imported + languageVariant: item => true, // all language variants will be imported + taxonomy: item => true,// all taxonomies will be imported + }, + enablePublish: true, // when enables, previously published language variants will be published after restore (does not affect unpublished variants) projectId: 'targetProjectId', apiKey: 'targetProjectId', - enableLog: true, - workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // workflow id that items are assigned + enableLog: true, // shows progress of immport in console + fixLanguages: true, // backup manager will attempt to create missing languages & map existing languages + workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned }); // read export data from zip diff --git a/lib/import/import.models.ts b/lib/import/import.models.ts index 28c0d56..275954e 100644 --- a/lib/import/import.models.ts +++ b/lib/import/import.models.ts @@ -23,7 +23,7 @@ export interface IImportConfig { onUnsupportedBinaryFile?: (binaryFile: IBinaryFile) => void; onImport?: (item: IProcessedItem) => void; fixLanguages: boolean; - process?: { + canImport?: { taxonomy?: (item: TaxonomyContracts.ITaxonomyContract) => boolean | Promise; contentTypeSnippet?: ( item: ContentTypeSnippetContracts.IContentTypeSnippetContract diff --git a/lib/import/import.service.ts b/lib/import/import.service.ts index c9d53b3..dfa002f 100644 --- a/lib/import/import.service.ts +++ b/lib/import/import.service.ts @@ -216,54 +216,54 @@ export class ImportService { } private removeSkippedItemsFromImport(source: IImportSource): void { - if (this.config.process && this.config.process.asset) { + if (this.config.canImport && this.config.canImport.asset) { for (const item of source.importData.assets) { - const shouldImport = this.config.process.asset(item); + const shouldImport = this.config.canImport.asset(item); if (!shouldImport) { source.importData.assets = source.importData.assets.filter((m) => m.id !== item.id); } } } - if (this.config.process && this.config.process.language) { + if (this.config.canImport && this.config.canImport.language) { for (const item of source.importData.languages) { - const shouldImport = this.config.process.language(item); + const shouldImport = this.config.canImport.language(item); if (!shouldImport) { source.importData.languages = source.importData.languages.filter((m) => m.id !== item.id); } } } - if (this.config.process && this.config.process.assetFolder) { + if (this.config.canImport && this.config.canImport.assetFolder) { for (const item of source.assetFolders) { - const shouldImport = this.config.process.assetFolder(item); + const shouldImport = this.config.canImport.assetFolder(item); if (!shouldImport) { source.assetFolders = source.assetFolders.filter((m) => m.id !== item.id); } } } - if (this.config.process && this.config.process.contentType) { + if (this.config.canImport && this.config.canImport.contentType) { for (const item of source.importData.contentTypes) { - const shouldImport = this.config.process.contentType(item); + const shouldImport = this.config.canImport.contentType(item); if (!shouldImport) { source.importData.contentTypes = source.importData.contentTypes.filter((m) => m.id !== item.id); } } } - if (this.config.process && this.config.process.contentItem) { + if (this.config.canImport && this.config.canImport.contentItem) { for (const item of source.importData.contentItems) { - const shouldImport = this.config.process.contentItem(item); + const shouldImport = this.config.canImport.contentItem(item); if (!shouldImport) { source.importData.contentItems = source.importData.contentItems.filter((m) => m.id !== item.id); } } } - if (this.config.process && this.config.process.contentTypeSnippet) { + if (this.config.canImport && this.config.canImport.contentTypeSnippet) { for (const item of source.importData.contentTypeSnippets) { - const shouldImport = this.config.process.contentTypeSnippet(item); + const shouldImport = this.config.canImport.contentTypeSnippet(item); if (!shouldImport) { source.importData.contentTypeSnippets = source.importData.contentTypeSnippets.filter( (m) => m.id !== item.id @@ -272,9 +272,9 @@ export class ImportService { } } - if (this.config.process && this.config.process.languageVariant) { + if (this.config.canImport && this.config.canImport.languageVariant) { for (const item of source.importData.languageVariants) { - const shouldImport = this.config.process.languageVariant(item); + const shouldImport = this.config.canImport.languageVariant(item); if (!shouldImport) { source.importData.languageVariants = source.importData.languageVariants.filter( (m) => m.item.id !== item.item.id && m.language.id !== item.language.id @@ -283,9 +283,9 @@ export class ImportService { } } - if (this.config.process && this.config.process.taxonomy) { + if (this.config.canImport && this.config.canImport.taxonomy) { for (const item of source.importData.taxonomies) { - const shouldImport = this.config.process.taxonomy(item); + const shouldImport = this.config.canImport.taxonomy(item); if (!shouldImport) { source.importData.taxonomies = source.importData.taxonomies.filter((m) => m.id !== item.id); } diff --git a/lib/node/cli/app.ts b/lib/node/cli/app.ts index 8de5c87..d716802 100644 --- a/lib/node/cli/app.ts +++ b/lib/node/cli/app.ts @@ -136,7 +136,7 @@ const restoreAsync = async (config: ICliFileConfig) => { apiKey: config.apiKey, enableLog: config.enableLog, workflowIdForImportedItems: undefined, - process: { + canImport: { contentItem: (item) => { return true; } diff --git a/lib/samples/restore-sample.ts b/lib/samples/restore-sample.ts index 7d64795..ed2b04b 100644 --- a/lib/samples/restore-sample.ts +++ b/lib/samples/restore-sample.ts @@ -17,11 +17,37 @@ const run = async () => { // called when any content is imported console.log(`Imported: ${item.title} | ${item.type}`); }, - enablePublish: true, + // be careful when filtering data to import because you might break data consistency. + // for example, it might not be possible to import language variant without first importing content item and so on. + canImport: { + asset: (item) => { + if(item.title.startsWith('_corporate')) { + // asset will be imported only if the title starts with "_corporate" + return true; + } + // otherwise asset will NOT be imported + return false; + }, + contentType: (item) => { + if (item.codename === 'article') { + // content type will be imported only with its codename is 'article' + return true; + } + // all other types will be excluded from import + return false; + }, + assetFolder: item => true, // all folders will be imported + contentItem: item => true, // all content items will be imported + contentTypeSnippet: item => true, // all content type snippets will be imported + language: item => true, // all languages will be imported + languageVariant: item => true, // all language variants will be imported + taxonomy: item => true,// all taxonomies will be imported + }, + enablePublish: true, // when enables, previously published language variants will be published after restore (does not affect unpublished variants) projectId: 'targetProjectId', apiKey: 'targetProjectId', - enableLog: true, - fixLanguages: true, + enableLog: true, // shows progress of immport in console + fixLanguages: true, // backup manager will attempt to create missing languages & map existing languages workflowIdForImportedItems: '00000000-0000-0000-0000-000000000000' // id that items are assigned });