-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmigrate-from-external-system.ts
133 lines (126 loc) · 4.25 KB
/
migrate-from-external-system.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import { Buffer as BufferProxy } from 'buffer';
import {
elementsBuilder,
FileBinaryData,
importAsync,
MigrationAsset,
MigrationElementModels,
MigrationItem,
MigrationItemSystem,
storeAsync
} from '../lib/index.js';
/**
* Optionally (but highly recommended) you may define a migration model
* representing the environment you are trying to migrate into.
*/
type LanguageCodenames = 'default' | 'en';
type CollectionCodenames = 'default' | 'global';
type WorkflowCodenames = 'default' | 'custom';
type WorkflowStepCodenames = 'published' | 'archived' | 'draft';
type ContentTypeCodenames = 'article' | 'author';
type ContentTypeCodename<Codename extends ContentTypeCodenames> = Codename;
type ArticleItem = MigrationItem<
{
readonly title: MigrationElementModels.TextElement;
readonly rating: MigrationElementModels.NumberElement;
readonly related_pages: MigrationElementModels.LinkedItemsElement;
readonly teaser_image: MigrationElementModels.AssetElement;
readonly text: MigrationElementModels.RichTextElement;
readonly slug: MigrationElementModels.UrlSlugElement;
},
MigrationItemSystem<ContentTypeCodename<'article'>, LanguageCodenames, CollectionCodenames, WorkflowCodenames>,
WorkflowStepCodenames
>;
/**
* Typically you query your external system to create the migration items & assets
* */
const migrationItem: ArticleItem = {
system: {
name: 'My article',
// codename is primary identifier - also used for validating whether asset exists in target env
codename: 'myArticle',
collection: {
// collection codename must match the collection in your target K.ai environment
codename: 'default'
},
language: {
// language codename must match the language in your target K.ai environment
codename: 'en'
},
type: {
// type codename must match the content type codename in your target K.ai environment
codename: 'article'
},
workflow: {
codename: 'default'
}
},
versions: [
{
workflow_step: {
codename: 'published'
},
elements: {
title: elementsBuilder.textElement({ value: 'Title of the article' }),
text: elementsBuilder.richTextElement({ value: '<p>Content of the article</p>', components: [] }),
slug: elementsBuilder.urlSlugElement({ value: 'my-article' }),
rating: elementsBuilder.numberElement({ value: 5 }),
related_pages: elementsBuilder.linkedItemsElement({
value: [
{
codename: 'pageA'
},
{
codename: 'pageB'
}
]
}),
// assets are referenced by their codename
teaser_image: elementsBuilder.assetElement({ value: [{ codename: 'article_teaser' }] })
}
}
]
};
const migrationAsset: MigrationAsset = {
// codename of the asset - Used for validating whether asset exists in target env
codename: 'article_teaser',
// filename will be used in K.ai asset as a filename
filename: 'article_teaser.jpg',
// title will be used in K.ai asset as a title
title: 'Article teaser',
// binary data of the asset you want to upload
binary_data: stringToBinaryData('data'),
// collection assignment
collection: {
codename: 'teasers'
},
// description of asset in project languages
descriptions: [
{
description: 'Teaser of the article',
language: {
codename: 'en_uk'
}
}
]
};
// stores data on FS for later use
await storeAsync({
data: {
items: [migrationItem],
assets: [migrationAsset]
},
filename: 'data.zip'
});
// and import to Kontent.ai
await importAsync({
data: {
items: [migrationItem],
assets: [migrationAsset]
},
apiKey: '<apiKey>',
environmentId: '<envId>'
});
function stringToBinaryData(input: string): FileBinaryData {
return BufferProxy.from(input);
}