Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Commit

Permalink
feat: adds ability to export only selected data types (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Aug 6, 2020
1 parent 233b8be commit fd38c54
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 14 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,17 @@ Install package globally:
|-----------------|---------------------------------------------------------------------------------------------------------------------|
| **projectId** | Id of Kentico Kontent project **(required)** |
| **apiKey** | Content management Api key **(required)** |
| **action** | Action. Possible values are: 'restore' & 'backup' & 'clean' **(required)** |
| zipFilename | Name of zip used for export / restoring data. E.g. 'kontent-backup'. |
| **action** | Action. Possible values are: `restore` & `backup` & `clean` **(required)** |
| zipFilename | Name of zip used for export / restoring data. (e.g. 'kontent-backup'). |
| enableLog | Indicates if default logging is enabled (useful to indicate progress)
| force | If enabled, project will we exported / restored even if there are data inconsistencies. Enabled by default. |
| baseUrl | Custom base URL for Management API calls. |
| exportFilter | Can be used to export only selected data types. Expects CSV of types. For example `contentType,language` will cause backup manager to export only content types & language data. List of data types can be found below. |

### Data types

* Start a line with a star
* Profit!

### Execution

Expand Down
1 change: 1 addition & 0 deletions src/core/core.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface ICliFileConfig {
enableLog: boolean;
force: boolean;
baseUrl?: string;
exportFilter?: ItemType[]
}

export type CliAction = 'backup' | 'restore' | 'clean';
Expand Down
3 changes: 2 additions & 1 deletion src/export/export.models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import {
ProjectContracts,
} from '@kentico/kontent-management';

import { IProcessedItem, IPackageMetadata } from '../core';
import { IProcessedItem, IPackageMetadata, ItemType } from '../core';

export interface IExportConfig {
projectId: string;
apiKey: string;
baseUrl?: string;
onExport?: (item: IProcessedItem) => void;
exportFilter?: ItemType[];
}

export interface IExportData {
Expand Down
28 changes: 20 additions & 8 deletions src/export/export.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,27 @@ export class ExportService {
const contentTypes = await this.exportContentTypesAsync();
const projectValidation = await this.exportProjectValidationAsync();

const exportItems = {
asset: this.config.exportFilter?.includes('asset') ?? true,
assetFolder: this.config.exportFilter?.includes('assetFolder') ?? true,
binaryFile: this.config.exportFilter?.includes('binaryFile') ?? true,
contentItem: this.config.exportFilter?.includes('contentItem') ?? true,
contentType: this.config.exportFilter?.includes('contentType') ?? true,
contentTypeSnippet: this.config.exportFilter?.includes('contentTypeSnippet') ?? true,
language: this.config.exportFilter?.includes('language') ?? true,
languageVariant: this.config.exportFilter?.includes('languageVariant') ?? true,
taxonomy: this.config.exportFilter?.includes('taxonomy') ?? true,
};

const data: IExportData = {
contentTypes,
contentTypeSnippets: await this.exportContentTypeSnippetsAsync(),
taxonomies: await this.exportTaxonomiesAsync(),
contentItems: await this.exportContentItemsAsync(),
languageVariants: await this.exportLanguageVariantsAsync(contentTypes.map(m => m.id)),
assets: await this.exportAssetsAsync(),
languages: await this.exportLanguagesAsync(),
assetFolders: await this.exportAssetFoldersAsync(),
contentTypes: exportItems.contentType ? contentTypes : [],
contentTypeSnippets: exportItems.contentTypeSnippet ? await this.exportContentTypeSnippetsAsync() : [],
taxonomies: exportItems.taxonomy ? await this.exportTaxonomiesAsync() : [],
contentItems: exportItems.contentItem ? await this.exportContentItemsAsync() : [],
languageVariants: exportItems.languageVariant ? await this.exportLanguageVariantsAsync(contentTypes.map(m => m.id)) : [],
assets: exportItems.asset ? await this.exportAssetsAsync() : [],
languages: exportItems.language ? await this.exportLanguagesAsync() : [],
assetFolders: exportItems.assetFolder ? await this.exportAssetFoldersAsync() : [],
};

return {
Expand Down
11 changes: 9 additions & 2 deletions src/node-js/cli/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as fs from 'fs';
import yargs = require('yargs');

import { CleanService } from '../../clean';
import { ICliFileConfig, getFilenameWithoutExtension, CliAction } from '../../core';
import { ICliFileConfig, getFilenameWithoutExtension, CliAction, ItemType } from '../../core';
import { ExportService } from '../../export';
import { IImportSource, ImportService } from '../../import';
import { ZipService } from '../../zip';
Expand All @@ -18,6 +18,7 @@ const backupAsync = async (config: ICliFileConfig) => {
apiKey: config.apiKey,
projectId: config.projectId,
baseUrl: config.baseUrl,
exportFilter: config.exportFilter,
onExport: (item) => {
if (config.enableLog) {
console.log(`Exported: ${item.title} | ${item.type}`);
Expand Down Expand Up @@ -197,6 +198,11 @@ const getConfig = async () => {
const projectId: string | undefined = argv.projectId as string | undefined;
const baseUrl: string | undefined = argv.baseUrl as string | undefined;
const zipFilename: string | undefined = (argv.zipFilename as string | undefined) ?? getDefaultBackupFilename();
const exportFilter: string | undefined = argv.exportFilter as string | undefined;

const exportFilterMapped: ItemType[] | undefined = exportFilter ? exportFilter.split(',').map(m => m.trim()).map(m => {
return m as ItemType;
}) : undefined;

if (!action) {
throw Error(`No action was provided`);
Expand All @@ -218,7 +224,8 @@ const getConfig = async () => {
force,
projectId,
zipFilename,
baseUrl
baseUrl,
exportFilter: exportFilterMapped
};

return config;
Expand Down
3 changes: 2 additions & 1 deletion src/node-js/cli/sample-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"action": "backup",
"enableLog": true,
"force": false,
"baseUrl": null
"baseUrl": null,
"exportFilter": null
}

0 comments on commit fd38c54

Please sign in to comment.