Skip to content

Commit

Permalink
update migration scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanKiral committed Jul 11, 2024
1 parent 39d44cf commit 714cee0
Show file tree
Hide file tree
Showing 18 changed files with 499 additions and 343 deletions.
35 changes: 0 additions & 35 deletions Migrations/07_sample_migration_publish.js

This file was deleted.

5 changes: 5 additions & 0 deletions exampleParams.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"environmentId": "",
"apiKey": "",
"migrationsPath": "./Migrations"
}
52 changes: 52 additions & 0 deletions src/01_collections.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MigrationModule } from "@kontent-ai/data-ops";
import { CollectionModels } from "@kontent-ai/management-sdk";

import { collectionHealthTechExtId } from "./constants/externalIds.js";

const migration: MigrationModule = {
order: 1,
run: async client => {
const setCollectionsData: CollectionModels.ISetCollectionData[] = [
{
op: "addInto",
value: {
name: "healthtech",
codename: "healthtech",
externalId: collectionHealthTechExtId,
},
},
{
op: "replace",
reference: { codename: "default" },
property_name: "name",
value: "common",
},
];

await client
.setCollections()
.withData(setCollectionsData)
.toPromise();
},
rollback: async client => {
const setCollectionsData: CollectionModels.ISetCollectionData[] = [
{
op: "remove",
reference: { codename: "healthtech" },
} as unknown as CollectionModels.ISetCollectionData,
{
op: "replace",
reference: { codename: "default" },
property_name: "name",
value: "default",
},
];

await client
.setCollections()
.withData(setCollectionsData)
.toPromise();
},
};

export default migration;
44 changes: 0 additions & 44 deletions src/01_sample_init_createBlogType.ts

This file was deleted.

54 changes: 0 additions & 54 deletions src/02_sample_init_createBlogItem.ts

This file was deleted.

15 changes: 15 additions & 0 deletions src/02_webSpotlight.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { MigrationModule } from "@kontent-ai/data-ops";

const migration: MigrationModule = {
order: 2,
run: async client => {
client as never;
// TODO:
},
rollback: async client => {
client as never;
// TODO:
},
};

export default migration;
38 changes: 0 additions & 38 deletions src/03_sample_migration_createAuthorType.ts

This file was deleted.

42 changes: 42 additions & 0 deletions src/03_taxonomies.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { MigrationModule } from "@kontent-ai/data-ops";
import { TaxonomyModels } from "@kontent-ai/management-sdk";

import { taxonomyArticleTypeExtId } from "./constants/externalIds.js";

const migration: MigrationModule = {
order: 3,
run: async client => {
const taxonomyData: TaxonomyModels.IAddTaxonomyRequestModel = {
name: "Article Type",
codename: "article_type",
external_id: taxonomyArticleTypeExtId.taxonomyGroup,
terms: [
{
name: "Blog Post",
codename: "blog_post",
external_id: taxonomyArticleTypeExtId.terms.blogPost,
terms: [],
},
{
name: "news",
codename: "news",
external_id: taxonomyArticleTypeExtId.terms.news,
terms: [],
},
],
};

await client
.addTaxonomy()
.withData(taxonomyData)
.toPromise();
},
rollback: async client => {
await client
.deleteTaxonomy()
.byTaxonomyExternalId(taxonomyArticleTypeExtId.taxonomyGroup)
.toPromise();
},
};

export default migration;
86 changes: 86 additions & 0 deletions src/04_assets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { MigrationModule } from "@kontent-ai/data-ops";
import { AssetFolderModels, ManagementClient, SharedContracts } from "@kontent-ai/management-sdk";
import * as fsPromises from "fs/promises";
import path from "path";

import { assetFoldersImagesExtId, assetKontentExternalId } from "./constants/externalIds.js";

const migration: MigrationModule = {
order: 4,
run: async client => {
const assetFoldersData: AssetFolderModels.IAddAssetFoldersData = {
folders: [
{ name: "Images", external_id: assetFoldersImagesExtId, folders: [] },
],
};

await client
.addAssetFolders()
.withData(assetFoldersData)
.toPromise();

await addAsset(client, {
fileName: "kontent-ai.png",
externalId: assetKontentExternalId,
contentType: "image/png",
collection: { codename: "default" },
});
},
rollback: async client => {
await client
.modifyAssetFolders()
.withData([{
op: "remove",
reference: { external_id: assetFoldersImagesExtId },
}] as unknown as AssetFolderModels.IModifyAssetFolderData[])
.toPromise();

await client
.deleteAsset()
.byAssetExternalId(assetKontentExternalId)
.toPromise();
},
};

type AddAssetParams = Readonly<{
fileName: string;
externalId: string;
contentType: string;
collection: SharedContracts.IReferenceObjectContract;
folderExternalId?: string;
descriptions?: Readonly<Record<string, string>>;
}>;

const addAsset = async (client: ManagementClient, params: AddAssetParams) => {
const binaryData = await fsPromises.readFile(path.resolve(process.cwd(), `./src/assets/${params.fileName}`));

const fileRef = await client
.uploadBinaryFile()
.withData({
filename: params.fileName,
binaryData,
contentType: params.contentType,
contentLength: binaryData.length,
})
.toPromise()
.then(res => res.data);

return client
.addAsset()
.withData(() => ({
external_id: params.externalId,
folder: params.folderExternalId ? { external_id: params.folderExternalId } : undefined,
collection: { reference: params.collection },
file_reference: fileRef,
descriptions: params.descriptions
? Object.entries(params.descriptions).map(([codename, description]) => ({
language: { codename },
description,
}))
: undefined,
}))
.toPromise()
.then(res => res.data);
};

export default migration;
Loading

0 comments on commit 714cee0

Please sign in to comment.