@stackbit/cms-core 0.0.18-alpha.0 → 0.0.19-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/content-source-interface.d.ts +70 -26
- package/dist/content-source-interface.d.ts.map +1 -1
- package/dist/content-source-interface.js +1 -1
- package/dist/content-source-interface.js.map +1 -1
- package/dist/content-store-types.d.ts +31 -8
- package/dist/content-store-types.d.ts.map +1 -1
- package/dist/content-store.d.ts +4 -4
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +415 -317
- package/dist/content-store.js.map +1 -1
- package/package.json +2 -2
- package/src/content-source-interface.ts +107 -31
- package/src/content-store-types.ts +32 -8
- package/src/content-store.ts +497 -378
package/dist/content-store.js
CHANGED
|
@@ -173,19 +173,23 @@ class ContentStore {
|
|
|
173
173
|
// TODO: load presets externally from config, and create additional map
|
|
174
174
|
// that maps presetIds by model name instead of storing that map inside every model
|
|
175
175
|
this.presets = config.presets;
|
|
176
|
-
const
|
|
177
|
-
const
|
|
178
|
-
const
|
|
179
|
-
|
|
176
|
+
const csiDocuments = await contentSourceInstance.getDocuments({ modelMap });
|
|
177
|
+
const csiAssets = await contentSourceInstance.getAssets();
|
|
178
|
+
const csiDocumentMap = lodash_1.default.keyBy(csiDocuments, 'id');
|
|
179
|
+
const csiAssetMap = lodash_1.default.keyBy(csiAssets, 'id');
|
|
180
|
+
const contentStoreDocuments = mapCSIDocumentsToStoreDocuments({
|
|
181
|
+
csiDocuments,
|
|
180
182
|
contentSourceInstance,
|
|
181
183
|
modelMap,
|
|
182
184
|
defaultLocaleCode
|
|
183
185
|
});
|
|
184
|
-
const contentStoreAssets =
|
|
185
|
-
|
|
186
|
+
const contentStoreAssets = mapCSIAssetsToStoreAssets({
|
|
187
|
+
csiAssets,
|
|
186
188
|
contentSourceInstance,
|
|
187
189
|
defaultLocaleCode
|
|
188
190
|
});
|
|
191
|
+
const documentMap = lodash_1.default.keyBy(contentStoreDocuments, 'srcObjectId');
|
|
192
|
+
const assetMap = lodash_1.default.keyBy(contentStoreAssets, 'srcObjectId');
|
|
189
193
|
this.logger.debug('loaded content source data', {
|
|
190
194
|
contentSourceId,
|
|
191
195
|
locales,
|
|
@@ -203,16 +207,26 @@ class ContentStore {
|
|
|
203
207
|
defaultLocaleCode: defaultLocaleCode,
|
|
204
208
|
models: models,
|
|
205
209
|
modelMap: modelMap,
|
|
210
|
+
csiDocuments: csiDocuments,
|
|
211
|
+
csiDocumentMap: csiDocumentMap,
|
|
206
212
|
documents: contentStoreDocuments,
|
|
207
|
-
documentMap:
|
|
213
|
+
documentMap: documentMap,
|
|
214
|
+
csiAssets: csiAssets,
|
|
215
|
+
csiAssetMap: csiAssetMap,
|
|
208
216
|
assets: contentStoreAssets,
|
|
209
|
-
assetMap:
|
|
217
|
+
assetMap: assetMap
|
|
210
218
|
};
|
|
211
219
|
contentSourceInstance.startWatchingContentUpdates({
|
|
212
220
|
getModelMap: () => {
|
|
213
221
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
214
222
|
return contentSourceData.modelMap;
|
|
215
223
|
},
|
|
224
|
+
getDocument({ documentId }) {
|
|
225
|
+
return csiDocumentMap[documentId];
|
|
226
|
+
},
|
|
227
|
+
getAsset({ assetId }) {
|
|
228
|
+
return csiAssetMap[assetId];
|
|
229
|
+
},
|
|
216
230
|
onContentChange: (contentChangeEvent) => {
|
|
217
231
|
this.logger.debug('content source called onContentChange', { contentSourceId });
|
|
218
232
|
const result = this.onContentChange(contentSourceId, contentChangeEvent);
|
|
@@ -244,11 +258,17 @@ class ContentStore {
|
|
|
244
258
|
deletedAssets: []
|
|
245
259
|
};
|
|
246
260
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
261
|
+
// update contentSourceData with deleted documents
|
|
247
262
|
contentChangeEvent.deletedDocumentIds.forEach((docId) => {
|
|
263
|
+
// delete document from documents map
|
|
248
264
|
delete contentSourceData.documentMap[docId];
|
|
265
|
+
delete contentSourceData.csiDocumentMap[docId];
|
|
266
|
+
// delete document from document array
|
|
249
267
|
const index = contentSourceData.documents.findIndex((document) => document.srcObjectId === docId);
|
|
250
268
|
if (index !== -1) {
|
|
269
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
251
270
|
contentSourceData.documents.splice(index, 1);
|
|
271
|
+
contentSourceData.csiDocuments.splice(index, 1);
|
|
252
272
|
}
|
|
253
273
|
result.deletedDocuments.push({
|
|
254
274
|
srcType: contentSourceData.type,
|
|
@@ -256,11 +276,17 @@ class ContentStore {
|
|
|
256
276
|
srcObjectId: docId
|
|
257
277
|
});
|
|
258
278
|
});
|
|
279
|
+
// update contentSourceData with deleted assets
|
|
259
280
|
contentChangeEvent.deletedAssetIds.forEach((assetId) => {
|
|
281
|
+
// delete document from asset map
|
|
260
282
|
delete contentSourceData.assetMap[assetId];
|
|
283
|
+
delete contentSourceData.csiAssetMap[assetId];
|
|
284
|
+
// delete document from asset array
|
|
261
285
|
const index = contentSourceData.assets.findIndex((asset) => asset.srcObjectId === assetId);
|
|
262
286
|
if (index !== -1) {
|
|
287
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
263
288
|
contentSourceData.assets.splice(index, 1);
|
|
289
|
+
contentSourceData.csiAssets.splice(index, 1);
|
|
264
290
|
}
|
|
265
291
|
result.deletedAssets.push({
|
|
266
292
|
srcType: contentSourceData.type,
|
|
@@ -268,26 +294,36 @@ class ContentStore {
|
|
|
268
294
|
srcObjectId: assetId
|
|
269
295
|
});
|
|
270
296
|
});
|
|
271
|
-
|
|
272
|
-
|
|
297
|
+
// map csi documents and assets to content store documents and assets
|
|
298
|
+
const documents = mapCSIDocumentsToStoreDocuments({
|
|
299
|
+
csiDocuments: contentChangeEvent.documents,
|
|
273
300
|
contentSourceInstance: contentSourceData.instance,
|
|
274
301
|
modelMap: contentSourceData.modelMap,
|
|
275
302
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
276
303
|
});
|
|
277
|
-
const assets =
|
|
278
|
-
|
|
304
|
+
const assets = mapCSIAssetsToStoreAssets({
|
|
305
|
+
csiAssets: contentChangeEvent.assets,
|
|
279
306
|
contentSourceInstance: contentSourceData.instance,
|
|
280
307
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
281
308
|
});
|
|
309
|
+
// update contentSourceData with new or updated documents and assets
|
|
310
|
+
Object.assign(contentSourceData.csiDocumentMap, lodash_1.default.keyBy(contentChangeEvent.documents, 'id'));
|
|
311
|
+
Object.assign(contentSourceData.csiAssets, lodash_1.default.keyBy(contentChangeEvent.assets, 'id'));
|
|
282
312
|
Object.assign(contentSourceData.documentMap, lodash_1.default.keyBy(documents, 'srcObjectId'));
|
|
283
313
|
Object.assign(contentSourceData.assetMap, lodash_1.default.keyBy(assets, 'srcObjectId'));
|
|
284
|
-
for (
|
|
285
|
-
|
|
286
|
-
|
|
314
|
+
for (let idx = 0; idx < documents.length; idx++) {
|
|
315
|
+
// the indexes of mapped documents and documents from changeEvent are the same
|
|
316
|
+
const document = documents[idx];
|
|
317
|
+
const csiDocument = contentChangeEvent.documents[idx];
|
|
318
|
+
const dataIndex = contentSourceData.documents.findIndex((existingDoc) => existingDoc.srcObjectId === document.srcObjectId);
|
|
319
|
+
if (dataIndex === -1) {
|
|
287
320
|
contentSourceData.documents.push(document);
|
|
321
|
+
contentSourceData.csiDocuments.push(csiDocument);
|
|
288
322
|
}
|
|
289
323
|
else {
|
|
290
|
-
|
|
324
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
325
|
+
contentSourceData.documents.splice(dataIndex, 1, document);
|
|
326
|
+
contentSourceData.csiDocuments.splice(dataIndex, 1, csiDocument);
|
|
291
327
|
}
|
|
292
328
|
result.updatedDocuments.push({
|
|
293
329
|
srcType: contentSourceData.type,
|
|
@@ -295,13 +331,19 @@ class ContentStore {
|
|
|
295
331
|
srcObjectId: document.srcObjectId
|
|
296
332
|
});
|
|
297
333
|
}
|
|
298
|
-
for (
|
|
334
|
+
for (let idx = 0; idx < assets.length; idx++) {
|
|
335
|
+
// the indexes of mapped assets and assets from changeEvent are the same
|
|
336
|
+
const asset = assets[idx];
|
|
337
|
+
const csiAsset = contentChangeEvent.assets[idx];
|
|
299
338
|
const index = contentSourceData.assets.findIndex((existingAsset) => existingAsset.srcObjectId === asset.srcObjectId);
|
|
300
339
|
if (index === -1) {
|
|
301
340
|
contentSourceData.assets.push(asset);
|
|
341
|
+
contentSourceData.csiAssets.push(csiAsset);
|
|
302
342
|
}
|
|
303
343
|
else {
|
|
344
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
304
345
|
contentSourceData.assets.splice(index, 1, asset);
|
|
346
|
+
contentSourceData.csiAssets.splice(index, 1, csiAsset);
|
|
305
347
|
}
|
|
306
348
|
result.updatedAssets.push({
|
|
307
349
|
srcType: contentSourceData.type,
|
|
@@ -441,7 +483,8 @@ class ContentStore {
|
|
|
441
483
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
442
484
|
// get the document that is being updated
|
|
443
485
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
444
|
-
|
|
486
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
487
|
+
if (!document || !csiDocument) {
|
|
445
488
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
446
489
|
}
|
|
447
490
|
// get the document model
|
|
@@ -485,7 +528,7 @@ class ContentStore {
|
|
|
485
528
|
refId: result.srcDocumentId
|
|
486
529
|
};
|
|
487
530
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
488
|
-
|
|
531
|
+
document: csiDocument,
|
|
489
532
|
modelMap: modelMap,
|
|
490
533
|
userContext: userContext,
|
|
491
534
|
operations: [
|
|
@@ -515,7 +558,8 @@ class ContentStore {
|
|
|
515
558
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
516
559
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
517
560
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
518
|
-
|
|
561
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
562
|
+
if (!document || !csiDocument) {
|
|
519
563
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
520
564
|
}
|
|
521
565
|
// get the document model
|
|
@@ -551,7 +595,7 @@ class ContentStore {
|
|
|
551
595
|
refId: result.id
|
|
552
596
|
};
|
|
553
597
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
554
|
-
|
|
598
|
+
document: csiDocument,
|
|
555
599
|
modelMap: modelMap,
|
|
556
600
|
userContext: userContext,
|
|
557
601
|
operations: [
|
|
@@ -606,19 +650,20 @@ class ContentStore {
|
|
|
606
650
|
this.logger.debug('updateDocument');
|
|
607
651
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
608
652
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
609
|
-
const modelMap = contentSourceData.modelMap;
|
|
610
653
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
654
|
+
const document = contentSourceData.documentMap[srcDocumentId];
|
|
655
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
656
|
+
if (!document || !csiDocument) {
|
|
657
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
658
|
+
}
|
|
659
|
+
const modelMap = contentSourceData.modelMap;
|
|
660
|
+
const documentModelName = document.srcModelName;
|
|
661
|
+
const model = modelMap[documentModelName];
|
|
662
|
+
if (!model) {
|
|
663
|
+
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
664
|
+
}
|
|
611
665
|
const operations = await (0, utils_1.mapPromise)(updateOperations, async (updateOperation) => {
|
|
612
666
|
var _a;
|
|
613
|
-
const document = contentSourceData.documentMap[srcDocumentId];
|
|
614
|
-
if (!document) {
|
|
615
|
-
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
616
|
-
}
|
|
617
|
-
const documentModelName = document.srcModelName;
|
|
618
|
-
const model = modelMap[documentModelName];
|
|
619
|
-
if (!model) {
|
|
620
|
-
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
621
|
-
}
|
|
622
667
|
const locale = (_a = updateOperation.locale) !== null && _a !== void 0 ? _a : contentSourceData.defaultLocaleCode;
|
|
623
668
|
const modelField = getModelFieldForFieldAtPath(document, model, updateOperation.fieldPath, modelMap, locale);
|
|
624
669
|
switch (updateOperation.opType) {
|
|
@@ -660,8 +705,8 @@ class ContentStore {
|
|
|
660
705
|
return { ...updateOperation, modelField };
|
|
661
706
|
}
|
|
662
707
|
});
|
|
663
|
-
const
|
|
664
|
-
|
|
708
|
+
const updatedDocumentResult = await contentSourceData.instance.updateDocument({
|
|
709
|
+
document: csiDocument,
|
|
665
710
|
modelMap,
|
|
666
711
|
userContext,
|
|
667
712
|
operations
|
|
@@ -670,7 +715,7 @@ class ContentStore {
|
|
|
670
715
|
// instead wait for contentSource to call onContentChange(contentChangeEvent)
|
|
671
716
|
// and use data from contentChangeEvent to update the cache
|
|
672
717
|
// contentSourceData.documentMap = Object.assign(contentSourceData.documentMap, { [document.srcObjectId]: document });
|
|
673
|
-
return { srcDocumentId:
|
|
718
|
+
return { srcDocumentId: updatedDocumentResult.id };
|
|
674
719
|
}
|
|
675
720
|
async duplicateDocument({ srcType, srcProjectId, srcDocumentId, object, user }) {
|
|
676
721
|
this.logger.debug('duplicateDocument');
|
|
@@ -726,8 +771,8 @@ class ContentStore {
|
|
|
726
771
|
});
|
|
727
772
|
sourceAssets.push(sourceAsset);
|
|
728
773
|
}
|
|
729
|
-
const storeAssets =
|
|
730
|
-
|
|
774
|
+
const storeAssets = mapCSIAssetsToStoreAssets({
|
|
775
|
+
csiAssets: sourceAssets,
|
|
731
776
|
contentSourceInstance: contentSourceData.instance,
|
|
732
777
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
733
778
|
});
|
|
@@ -738,7 +783,11 @@ class ContentStore {
|
|
|
738
783
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
739
784
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
740
785
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
741
|
-
|
|
786
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
787
|
+
if (!csiDocument) {
|
|
788
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
789
|
+
}
|
|
790
|
+
await contentSourceData.instance.deleteDocument({ document: csiDocument, userContext });
|
|
742
791
|
// do not update cache in contentSourceData.documents and documentMap,
|
|
743
792
|
// instead wait for contentSource to call onContentChange(contentChangeEvent)
|
|
744
793
|
// and use data from contentChangeEvent to update the cache
|
|
@@ -748,21 +797,12 @@ class ContentStore {
|
|
|
748
797
|
this.logger.debug('validateDocuments');
|
|
749
798
|
const objectsBySourceId = lodash_1.default.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
750
799
|
let errors = [];
|
|
751
|
-
for (const [contentSourceId,
|
|
800
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
752
801
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
753
802
|
locale = locale !== null && locale !== void 0 ? locale : contentSourceData.defaultLocaleCode;
|
|
754
|
-
const
|
|
755
|
-
const assetIds = [];
|
|
756
|
-
for (const object of objects) {
|
|
757
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
758
|
-
documentIds.push(object.srcObjectId);
|
|
759
|
-
}
|
|
760
|
-
else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
761
|
-
assetIds.push(object.srcObjectId);
|
|
762
|
-
}
|
|
763
|
-
}
|
|
803
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
764
804
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
765
|
-
const validationResult = await contentSourceData.instance.validateDocuments({
|
|
805
|
+
const validationResult = await contentSourceData.instance.validateDocuments({ documents, assets, locale, userContext });
|
|
766
806
|
errors = errors.concat(validationResult.errors.map((validationError) => ({
|
|
767
807
|
message: validationError.message,
|
|
768
808
|
srcType: contentSourceData.type,
|
|
@@ -792,20 +832,11 @@ class ContentStore {
|
|
|
792
832
|
async publishDocuments({ objects, user }) {
|
|
793
833
|
this.logger.debug('publishDocuments');
|
|
794
834
|
const objectsBySourceId = lodash_1.default.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
795
|
-
for (const [contentSourceId,
|
|
835
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
796
836
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
797
837
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
798
|
-
const
|
|
799
|
-
|
|
800
|
-
for (const object of objects) {
|
|
801
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
802
|
-
documentIds.push(object.srcObjectId);
|
|
803
|
-
}
|
|
804
|
-
else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
805
|
-
assetIds.push(object.srcObjectId);
|
|
806
|
-
}
|
|
807
|
-
}
|
|
808
|
-
await contentSourceData.instance.publishDocuments({ documentIds, assetIds, userContext });
|
|
838
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
839
|
+
await contentSourceData.instance.publishDocuments({ documents, assets, userContext });
|
|
809
840
|
}
|
|
810
841
|
}
|
|
811
842
|
getContentSourceDataByIdOrThrow(contentSourceId) {
|
|
@@ -825,91 +856,92 @@ function getUserContextForSrcType(srcType, user) {
|
|
|
825
856
|
var _a;
|
|
826
857
|
return (_a = user === null || user === void 0 ? void 0 : user.connections) === null || _a === void 0 ? void 0 : _a.find((connection) => connection.type === srcType);
|
|
827
858
|
}
|
|
828
|
-
function
|
|
859
|
+
function mapCSIAssetsToStoreAssets({ csiAssets, contentSourceInstance, defaultLocaleCode }) {
|
|
829
860
|
const extra = {
|
|
830
861
|
srcType: contentSourceInstance.getContentSourceType(),
|
|
831
862
|
srcProjectId: contentSourceInstance.getProjectId(),
|
|
832
863
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
833
864
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
834
865
|
};
|
|
835
|
-
return
|
|
866
|
+
return csiAssets.map((csiAsset) => sourceAssetToStoreAsset({ csiAsset, defaultLocaleCode, extra }));
|
|
836
867
|
}
|
|
837
|
-
function sourceAssetToStoreAsset({
|
|
868
|
+
function sourceAssetToStoreAsset({ csiAsset, defaultLocaleCode, extra }) {
|
|
838
869
|
return {
|
|
839
870
|
type: 'asset',
|
|
840
871
|
...extra,
|
|
841
|
-
srcObjectId:
|
|
842
|
-
srcObjectUrl:
|
|
843
|
-
srcObjectLabel: getObjectLabel(
|
|
872
|
+
srcObjectId: csiAsset.id,
|
|
873
|
+
srcObjectUrl: csiAsset.manageUrl,
|
|
874
|
+
srcObjectLabel: getObjectLabel(csiAsset.fields, common_schema_1.IMAGE_MODEL, defaultLocaleCode),
|
|
844
875
|
srcModelName: common_schema_1.IMAGE_MODEL.name,
|
|
845
876
|
srcModelLabel: common_schema_1.IMAGE_MODEL.label,
|
|
846
|
-
isChanged:
|
|
847
|
-
status:
|
|
848
|
-
createdAt:
|
|
849
|
-
createdBy:
|
|
850
|
-
updatedAt:
|
|
851
|
-
updatedBy:
|
|
877
|
+
isChanged: csiAsset.status === 'added' || csiAsset.status === 'modified',
|
|
878
|
+
status: csiAsset.status,
|
|
879
|
+
createdAt: csiAsset.createdAt,
|
|
880
|
+
createdBy: csiAsset.createdBy,
|
|
881
|
+
updatedAt: csiAsset.updatedAt,
|
|
882
|
+
updatedBy: csiAsset.updatedBy,
|
|
852
883
|
fields: {
|
|
853
884
|
title: {
|
|
854
885
|
label: 'Title',
|
|
855
|
-
...
|
|
886
|
+
...csiAsset.fields.title
|
|
856
887
|
},
|
|
857
888
|
file: {
|
|
858
889
|
label: 'File',
|
|
859
|
-
...
|
|
890
|
+
...csiAsset.fields.file
|
|
860
891
|
}
|
|
861
892
|
}
|
|
862
893
|
};
|
|
863
894
|
}
|
|
864
|
-
function
|
|
895
|
+
function mapCSIDocumentsToStoreDocuments({ csiDocuments, contentSourceInstance, modelMap, defaultLocaleCode }) {
|
|
865
896
|
const extra = {
|
|
866
897
|
srcType: contentSourceInstance.getContentSourceType(),
|
|
867
898
|
srcProjectId: contentSourceInstance.getProjectId(),
|
|
868
899
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
869
900
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
870
901
|
};
|
|
871
|
-
return
|
|
902
|
+
return csiDocuments.map((csiDocument) => mapCSIDocumentToStoreDocument({ csiDocument, model: modelMap[csiDocument.modelName], modelMap, defaultLocaleCode, extra }));
|
|
872
903
|
}
|
|
873
|
-
function
|
|
904
|
+
function mapCSIDocumentToStoreDocument({ csiDocument, model, modelMap, defaultLocaleCode, extra }) {
|
|
874
905
|
var _a, _b;
|
|
875
906
|
return {
|
|
876
907
|
type: 'document',
|
|
877
908
|
...extra,
|
|
878
|
-
srcObjectId:
|
|
879
|
-
srcObjectUrl:
|
|
880
|
-
srcObjectLabel: getObjectLabel(
|
|
881
|
-
srcModelLabel: (_a = model.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(
|
|
882
|
-
srcModelName:
|
|
883
|
-
isChanged:
|
|
884
|
-
status:
|
|
885
|
-
createdAt:
|
|
886
|
-
createdBy:
|
|
887
|
-
updatedAt:
|
|
888
|
-
updatedBy:
|
|
889
|
-
fields:
|
|
890
|
-
|
|
909
|
+
srcObjectId: csiDocument.id,
|
|
910
|
+
srcObjectUrl: csiDocument.manageUrl,
|
|
911
|
+
srcObjectLabel: getObjectLabel(csiDocument.fields, model, defaultLocaleCode),
|
|
912
|
+
srcModelLabel: (_a = model.label) !== null && _a !== void 0 ? _a : lodash_1.default.startCase(csiDocument.modelName),
|
|
913
|
+
srcModelName: csiDocument.modelName,
|
|
914
|
+
isChanged: csiDocument.status === 'added' || csiDocument.status === 'modified',
|
|
915
|
+
status: csiDocument.status,
|
|
916
|
+
createdAt: csiDocument.createdAt,
|
|
917
|
+
createdBy: csiDocument.createdBy,
|
|
918
|
+
updatedAt: csiDocument.updatedAt,
|
|
919
|
+
updatedBy: csiDocument.updatedBy,
|
|
920
|
+
fields: mapCSIFieldsToStoreFields({
|
|
921
|
+
csiDocumentFields: csiDocument.fields,
|
|
891
922
|
modelFields: (_b = model.fields) !== null && _b !== void 0 ? _b : [],
|
|
892
|
-
|
|
893
|
-
|
|
923
|
+
context: {
|
|
924
|
+
modelMap,
|
|
925
|
+
defaultLocaleCode
|
|
926
|
+
}
|
|
894
927
|
})
|
|
895
928
|
};
|
|
896
929
|
}
|
|
897
|
-
function
|
|
930
|
+
function mapCSIFieldsToStoreFields({ csiDocumentFields, modelFields, context }) {
|
|
898
931
|
return modelFields.reduce((result, modelField) => {
|
|
899
|
-
const
|
|
900
|
-
const docField =
|
|
901
|
-
|
|
932
|
+
const csiDocumentField = csiDocumentFields[modelField.name];
|
|
933
|
+
const docField = mapCSIFieldToStoreField({
|
|
934
|
+
csiDocumentField,
|
|
902
935
|
modelField,
|
|
903
|
-
|
|
904
|
-
defaultLocaleCode
|
|
936
|
+
context
|
|
905
937
|
});
|
|
906
938
|
docField.label = modelField.label;
|
|
907
939
|
result[modelField.name] = docField;
|
|
908
940
|
return result;
|
|
909
941
|
}, {});
|
|
910
942
|
}
|
|
911
|
-
function
|
|
912
|
-
if (!
|
|
943
|
+
function mapCSIFieldToStoreField({ csiDocumentField, modelField, context }) {
|
|
944
|
+
if (!csiDocumentField) {
|
|
913
945
|
const isUnset = ['object', 'model', 'reference', 'richText', 'markdown', 'image', 'file', 'json'].includes(modelField.type);
|
|
914
946
|
return {
|
|
915
947
|
type: modelField.type,
|
|
@@ -920,135 +952,129 @@ function mapSourceFieldToStoreField({ documentField, modelField, modelMap, defau
|
|
|
920
952
|
// TODO: check if need to add "options" to "enum" and subtype/min/max to "number"
|
|
921
953
|
switch (modelField.type) {
|
|
922
954
|
case 'object':
|
|
923
|
-
return mapObjectField(
|
|
955
|
+
return mapObjectField(csiDocumentField, modelField, context);
|
|
924
956
|
case 'model':
|
|
925
|
-
return mapModelField(
|
|
957
|
+
return mapModelField(csiDocumentField, modelField, context);
|
|
926
958
|
case 'list':
|
|
927
|
-
return mapListField(
|
|
959
|
+
return mapListField(csiDocumentField, modelField, context);
|
|
928
960
|
case 'richText':
|
|
929
|
-
return mapRichTextField(
|
|
961
|
+
return mapRichTextField(csiDocumentField);
|
|
930
962
|
case 'markdown':
|
|
931
|
-
return mapMarkdownField(
|
|
963
|
+
return mapMarkdownField(csiDocumentField);
|
|
932
964
|
default:
|
|
933
|
-
return
|
|
965
|
+
return csiDocumentField;
|
|
934
966
|
}
|
|
935
967
|
}
|
|
936
|
-
function mapObjectField(
|
|
968
|
+
function mapObjectField(csiDocumentField, modelField, context) {
|
|
937
969
|
var _a, _b, _c;
|
|
938
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
970
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
939
971
|
return {
|
|
940
|
-
type:
|
|
941
|
-
srcObjectLabel: getObjectLabel((_a =
|
|
942
|
-
fields:
|
|
943
|
-
|
|
972
|
+
type: csiDocumentField.type,
|
|
973
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, modelField !== null && modelField !== void 0 ? modelField : [], context.defaultLocaleCode),
|
|
974
|
+
fields: mapCSIFieldsToStoreFields({
|
|
975
|
+
csiDocumentFields: (_b = csiDocumentField.fields) !== null && _b !== void 0 ? _b : {},
|
|
944
976
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
945
|
-
|
|
946
|
-
defaultLocaleCode
|
|
977
|
+
context
|
|
947
978
|
})
|
|
948
979
|
};
|
|
949
980
|
}
|
|
950
981
|
return {
|
|
951
|
-
type:
|
|
982
|
+
type: csiDocumentField.type,
|
|
952
983
|
localized: true,
|
|
953
|
-
locales: lodash_1.default.mapValues(
|
|
984
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
954
985
|
var _a, _b, _c;
|
|
955
986
|
return {
|
|
956
987
|
locale: locale.locale,
|
|
957
988
|
srcObjectLabel: getObjectLabel((_a = locale.fields) !== null && _a !== void 0 ? _a : {}, modelField, locale.locale),
|
|
958
|
-
fields:
|
|
959
|
-
|
|
989
|
+
fields: mapCSIFieldsToStoreFields({
|
|
990
|
+
csiDocumentFields: (_b = locale.fields) !== null && _b !== void 0 ? _b : {},
|
|
960
991
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
961
|
-
|
|
962
|
-
defaultLocaleCode
|
|
992
|
+
context
|
|
963
993
|
})
|
|
964
994
|
};
|
|
965
995
|
})
|
|
966
996
|
};
|
|
967
997
|
}
|
|
968
|
-
function mapModelField(
|
|
998
|
+
function mapModelField(csiDocumentField, modelField, context) {
|
|
969
999
|
var _a, _b, _c, _d;
|
|
970
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
971
|
-
const model = modelMap[
|
|
1000
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1001
|
+
const model = context.modelMap[csiDocumentField.modelName];
|
|
972
1002
|
return {
|
|
973
|
-
type:
|
|
974
|
-
srcObjectLabel: getObjectLabel((_a =
|
|
975
|
-
srcModelName:
|
|
1003
|
+
type: csiDocumentField.type,
|
|
1004
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, model, context.defaultLocaleCode),
|
|
1005
|
+
srcModelName: csiDocumentField.modelName,
|
|
976
1006
|
srcModelLabel: (_b = model.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(model.name),
|
|
977
|
-
fields:
|
|
978
|
-
|
|
1007
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1008
|
+
csiDocumentFields: (_c = csiDocumentField.fields) !== null && _c !== void 0 ? _c : {},
|
|
979
1009
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
980
|
-
|
|
981
|
-
defaultLocaleCode
|
|
1010
|
+
context
|
|
982
1011
|
})
|
|
983
1012
|
};
|
|
984
1013
|
}
|
|
985
1014
|
return {
|
|
986
|
-
type:
|
|
1015
|
+
type: csiDocumentField.type,
|
|
987
1016
|
localized: true,
|
|
988
|
-
locales: lodash_1.default.mapValues(
|
|
1017
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
989
1018
|
var _a, _b, _c, _d;
|
|
990
|
-
const model = modelMap[locale.modelName];
|
|
1019
|
+
const model = context.modelMap[locale.modelName];
|
|
991
1020
|
return {
|
|
992
1021
|
locale: locale.locale,
|
|
993
1022
|
srcObjectLabel: getObjectLabel((_a = locale.fields) !== null && _a !== void 0 ? _a : {}, model, locale.locale),
|
|
994
1023
|
srcModelName: locale.modelName,
|
|
995
1024
|
srcModelLabel: (_b = model.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(model.name),
|
|
996
|
-
fields:
|
|
997
|
-
|
|
1025
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1026
|
+
csiDocumentFields: (_c = locale.fields) !== null && _c !== void 0 ? _c : {},
|
|
998
1027
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
999
|
-
|
|
1000
|
-
defaultLocaleCode
|
|
1028
|
+
context
|
|
1001
1029
|
})
|
|
1002
1030
|
};
|
|
1003
1031
|
})
|
|
1004
1032
|
};
|
|
1005
1033
|
}
|
|
1006
|
-
function mapListField(
|
|
1007
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1034
|
+
function mapListField(csiDocumentField, modelField, context) {
|
|
1035
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1008
1036
|
return {
|
|
1009
|
-
type:
|
|
1010
|
-
items:
|
|
1037
|
+
type: csiDocumentField.type,
|
|
1038
|
+
items: csiDocumentField.items.map((item) => {
|
|
1011
1039
|
var _a;
|
|
1012
|
-
return
|
|
1013
|
-
|
|
1040
|
+
return mapCSIFieldToStoreField({
|
|
1041
|
+
csiDocumentField: item,
|
|
1014
1042
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1015
|
-
|
|
1016
|
-
defaultLocaleCode
|
|
1043
|
+
context
|
|
1017
1044
|
});
|
|
1018
1045
|
})
|
|
1019
1046
|
};
|
|
1020
1047
|
}
|
|
1021
1048
|
return {
|
|
1022
|
-
type:
|
|
1049
|
+
type: csiDocumentField.type,
|
|
1023
1050
|
localized: true,
|
|
1024
|
-
locales: lodash_1.default.mapValues(
|
|
1051
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1025
1052
|
var _a;
|
|
1026
1053
|
return {
|
|
1027
1054
|
locale: locale.locale,
|
|
1028
1055
|
items: ((_a = locale.items) !== null && _a !== void 0 ? _a : []).map((item) => {
|
|
1029
1056
|
var _a;
|
|
1030
|
-
return
|
|
1031
|
-
|
|
1057
|
+
return mapCSIFieldToStoreField({
|
|
1058
|
+
csiDocumentField: item,
|
|
1032
1059
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1033
|
-
|
|
1034
|
-
defaultLocaleCode
|
|
1060
|
+
context
|
|
1035
1061
|
});
|
|
1036
1062
|
})
|
|
1037
1063
|
};
|
|
1038
1064
|
})
|
|
1039
1065
|
};
|
|
1040
1066
|
}
|
|
1041
|
-
function mapRichTextField(
|
|
1042
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1067
|
+
function mapRichTextField(csiDocumentField) {
|
|
1068
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1043
1069
|
return {
|
|
1044
|
-
...
|
|
1070
|
+
...csiDocumentField,
|
|
1045
1071
|
multiElement: true
|
|
1046
1072
|
};
|
|
1047
1073
|
}
|
|
1048
1074
|
return {
|
|
1049
|
-
type:
|
|
1075
|
+
type: csiDocumentField.type,
|
|
1050
1076
|
localized: true,
|
|
1051
|
-
locales: lodash_1.default.mapValues(
|
|
1077
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1052
1078
|
return {
|
|
1053
1079
|
...locale,
|
|
1054
1080
|
multiElement: true
|
|
@@ -1056,18 +1082,18 @@ function mapRichTextField(documentField) {
|
|
|
1056
1082
|
})
|
|
1057
1083
|
};
|
|
1058
1084
|
}
|
|
1059
|
-
function mapMarkdownField(
|
|
1060
|
-
if (!(0, content_source_interface_1.isLocalizedField)(
|
|
1085
|
+
function mapMarkdownField(csiDocumentField) {
|
|
1086
|
+
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1061
1087
|
return {
|
|
1062
1088
|
type: 'markdown',
|
|
1063
|
-
value:
|
|
1089
|
+
value: csiDocumentField.value,
|
|
1064
1090
|
multiElement: true
|
|
1065
1091
|
};
|
|
1066
1092
|
}
|
|
1067
1093
|
return {
|
|
1068
1094
|
type: 'markdown',
|
|
1069
1095
|
localized: true,
|
|
1070
|
-
locales: lodash_1.default.mapValues(
|
|
1096
|
+
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1071
1097
|
return {
|
|
1072
1098
|
...locale,
|
|
1073
1099
|
multiElement: true
|
|
@@ -1076,7 +1102,7 @@ function mapMarkdownField(documentField) {
|
|
|
1076
1102
|
};
|
|
1077
1103
|
}
|
|
1078
1104
|
function mapStoreFieldsToSourceFields({ documentFields, modelFields, modelMap }) {
|
|
1079
|
-
// TODO:
|
|
1105
|
+
// TODO: implement
|
|
1080
1106
|
throw new Error(`duplicateDocument not implemented yet`);
|
|
1081
1107
|
}
|
|
1082
1108
|
function getContentSourceIdForContentSource(contentSource) {
|
|
@@ -1153,6 +1179,7 @@ function toLocalizedAPIField(docField, locale, isListItem = false) {
|
|
|
1153
1179
|
if (docFieldLocalized.type === 'object' || docFieldLocalized.type === 'model') {
|
|
1154
1180
|
return {
|
|
1155
1181
|
...docFieldLocalized,
|
|
1182
|
+
type: 'object',
|
|
1156
1183
|
...commonProps,
|
|
1157
1184
|
...(docFieldLocalized.isUnset
|
|
1158
1185
|
? null
|
|
@@ -1291,192 +1318,200 @@ async function createDocumentRecursively({ object, model, modelMap, locale, user
|
|
|
1291
1318
|
};
|
|
1292
1319
|
}
|
|
1293
1320
|
async function createNestedObjectRecursively({ object, modelFields, fieldPath, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1321
|
+
object = object !== null && object !== void 0 ? object : {};
|
|
1322
|
+
const result = {
|
|
1323
|
+
fields: {},
|
|
1324
|
+
newRefDocuments: []
|
|
1325
|
+
};
|
|
1326
|
+
const objectFieldNames = Object.keys(object);
|
|
1327
|
+
for (const modelField of modelFields) {
|
|
1328
|
+
const fieldName = modelField.name;
|
|
1329
|
+
let value;
|
|
1330
|
+
if (fieldName in object) {
|
|
1331
|
+
value = object[fieldName];
|
|
1332
|
+
lodash_1.default.pull(objectFieldNames, fieldName);
|
|
1333
|
+
}
|
|
1334
|
+
else if (modelField.const) {
|
|
1335
|
+
value = modelField.const;
|
|
1336
|
+
}
|
|
1337
|
+
else if (!lodash_1.default.isNil(modelField.default)) {
|
|
1338
|
+
value = modelField.default;
|
|
1339
|
+
}
|
|
1340
|
+
if (!lodash_1.default.isNil(value)) {
|
|
1341
|
+
const fieldResult = await createNestedField({
|
|
1342
|
+
value,
|
|
1343
|
+
modelField,
|
|
1344
|
+
fieldPath: fieldPath.concat(fieldName),
|
|
1301
1345
|
modelMap,
|
|
1302
1346
|
locale,
|
|
1303
1347
|
userContext,
|
|
1304
1348
|
contentSourceInstance
|
|
1305
1349
|
});
|
|
1350
|
+
result.fields[fieldName] = fieldResult.field;
|
|
1351
|
+
result.newRefDocuments = result.newRefDocuments.concat(fieldResult.newRefDocuments);
|
|
1352
|
+
}
|
|
1353
|
+
}
|
|
1354
|
+
if (objectFieldNames.length > 0) {
|
|
1355
|
+
throw new Error(`no model fields found when creating a document with fields: '${objectFieldNames.join(', ')}'`);
|
|
1356
|
+
}
|
|
1357
|
+
return result;
|
|
1358
|
+
}
|
|
1359
|
+
async function createNestedField({ value, modelField, fieldPath, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1360
|
+
var _a;
|
|
1361
|
+
if (modelField.type === 'object') {
|
|
1362
|
+
const result = await createNestedObjectRecursively({
|
|
1363
|
+
object: value,
|
|
1364
|
+
modelFields: modelField.fields,
|
|
1365
|
+
fieldPath,
|
|
1366
|
+
modelMap,
|
|
1367
|
+
locale,
|
|
1368
|
+
userContext,
|
|
1369
|
+
contentSourceInstance
|
|
1370
|
+
});
|
|
1371
|
+
return {
|
|
1372
|
+
field: {
|
|
1373
|
+
type: 'object',
|
|
1374
|
+
fields: result.fields
|
|
1375
|
+
},
|
|
1376
|
+
newRefDocuments: result.newRefDocuments
|
|
1377
|
+
};
|
|
1378
|
+
}
|
|
1379
|
+
else if (modelField.type === 'model') {
|
|
1380
|
+
let { $$type, ...rest } = value;
|
|
1381
|
+
const modelNames = modelField.models;
|
|
1382
|
+
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1383
|
+
// the 'type' property in default values
|
|
1384
|
+
if (!$$type && 'type' in rest) {
|
|
1385
|
+
$$type = rest.type;
|
|
1386
|
+
rest = lodash_1.default.omit(rest, 'type');
|
|
1387
|
+
}
|
|
1388
|
+
const modelName = $$type !== null && $$type !== void 0 ? $$type : (modelNames.length === 1 ? modelNames[0] : null);
|
|
1389
|
+
if (!modelName) {
|
|
1390
|
+
throw new Error(`no $$type was specified for nested model`);
|
|
1391
|
+
}
|
|
1392
|
+
const model = modelMap[modelName];
|
|
1393
|
+
if (!model) {
|
|
1394
|
+
throw new Error(`no model with name '${modelName}' was found`);
|
|
1395
|
+
}
|
|
1396
|
+
const result = await createNestedObjectRecursively({
|
|
1397
|
+
object: rest,
|
|
1398
|
+
modelFields: (_a = model.fields) !== null && _a !== void 0 ? _a : [],
|
|
1399
|
+
fieldPath,
|
|
1400
|
+
modelMap,
|
|
1401
|
+
locale,
|
|
1402
|
+
userContext,
|
|
1403
|
+
contentSourceInstance
|
|
1404
|
+
});
|
|
1405
|
+
return {
|
|
1406
|
+
field: {
|
|
1407
|
+
type: 'model',
|
|
1408
|
+
modelName: modelName,
|
|
1409
|
+
fields: result.fields
|
|
1410
|
+
},
|
|
1411
|
+
newRefDocuments: result.newRefDocuments
|
|
1412
|
+
};
|
|
1413
|
+
}
|
|
1414
|
+
else if (modelField.type === 'image') {
|
|
1415
|
+
let refId;
|
|
1416
|
+
if (lodash_1.default.isPlainObject(value)) {
|
|
1417
|
+
refId = value.$$ref;
|
|
1418
|
+
}
|
|
1419
|
+
else {
|
|
1420
|
+
refId = value;
|
|
1421
|
+
}
|
|
1422
|
+
if (!refId) {
|
|
1423
|
+
throw new Error(`reference field must specify a value`);
|
|
1424
|
+
}
|
|
1425
|
+
return {
|
|
1426
|
+
field: {
|
|
1427
|
+
type: 'reference',
|
|
1428
|
+
refType: 'asset',
|
|
1429
|
+
refId: refId
|
|
1430
|
+
},
|
|
1431
|
+
newRefDocuments: []
|
|
1432
|
+
};
|
|
1433
|
+
}
|
|
1434
|
+
else if (modelField.type === 'reference') {
|
|
1435
|
+
let { $$ref: refId = null, $$type: modelName = null, ...rest } = lodash_1.default.isPlainObject(value) ? value : { $$ref: value };
|
|
1436
|
+
if (refId) {
|
|
1306
1437
|
return {
|
|
1307
1438
|
field: {
|
|
1308
|
-
type: '
|
|
1309
|
-
|
|
1439
|
+
type: 'reference',
|
|
1440
|
+
refType: 'document',
|
|
1441
|
+
refId: refId
|
|
1310
1442
|
},
|
|
1311
|
-
newRefDocuments:
|
|
1443
|
+
newRefDocuments: []
|
|
1312
1444
|
};
|
|
1313
1445
|
}
|
|
1314
|
-
else
|
|
1315
|
-
let { $$type, ...rest } = value;
|
|
1446
|
+
else {
|
|
1316
1447
|
const modelNames = modelField.models;
|
|
1317
|
-
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1318
|
-
// the 'type' property in default values
|
|
1319
|
-
if (!$$type && 'type' in rest) {
|
|
1320
|
-
$$type = rest.type;
|
|
1321
|
-
rest = lodash_1.default.omit(rest, 'type');
|
|
1322
|
-
}
|
|
1323
|
-
const modelName = $$type !== null && $$type !== void 0 ? $$type : (modelNames.length === 1 ? modelNames[0] : null);
|
|
1324
1448
|
if (!modelName) {
|
|
1325
|
-
|
|
1449
|
+
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1450
|
+
// the 'type' property in default values
|
|
1451
|
+
if ('type' in rest) {
|
|
1452
|
+
modelName = rest.type;
|
|
1453
|
+
rest = lodash_1.default.omit(rest, 'type');
|
|
1454
|
+
}
|
|
1455
|
+
else if (modelNames.length === 1) {
|
|
1456
|
+
modelName = modelNames[0];
|
|
1457
|
+
}
|
|
1326
1458
|
}
|
|
1327
1459
|
const model = modelMap[modelName];
|
|
1328
1460
|
if (!model) {
|
|
1329
1461
|
throw new Error(`no model with name '${modelName}' was found`);
|
|
1330
1462
|
}
|
|
1331
|
-
const
|
|
1463
|
+
const { document, newRefDocuments } = await createDocumentRecursively({
|
|
1332
1464
|
object: rest,
|
|
1333
|
-
|
|
1334
|
-
fieldPath,
|
|
1465
|
+
model: model,
|
|
1335
1466
|
modelMap,
|
|
1336
1467
|
locale,
|
|
1337
1468
|
userContext,
|
|
1338
1469
|
contentSourceInstance
|
|
1339
1470
|
});
|
|
1340
|
-
return {
|
|
1341
|
-
field: {
|
|
1342
|
-
type: 'model',
|
|
1343
|
-
modelName: modelName,
|
|
1344
|
-
fields: result.fields
|
|
1345
|
-
},
|
|
1346
|
-
newRefDocuments: result.newRefDocuments
|
|
1347
|
-
};
|
|
1348
|
-
}
|
|
1349
|
-
else if (modelField.type === 'image') {
|
|
1350
|
-
let refId;
|
|
1351
|
-
if (lodash_1.default.isPlainObject(value)) {
|
|
1352
|
-
refId = value.$$ref;
|
|
1353
|
-
}
|
|
1354
|
-
else {
|
|
1355
|
-
refId = value;
|
|
1356
|
-
}
|
|
1357
|
-
if (!refId) {
|
|
1358
|
-
throw new Error(`reference field must specify a value`);
|
|
1359
|
-
}
|
|
1360
1471
|
return {
|
|
1361
1472
|
field: {
|
|
1362
1473
|
type: 'reference',
|
|
1363
|
-
refType: '
|
|
1364
|
-
refId:
|
|
1474
|
+
refType: 'document',
|
|
1475
|
+
refId: document.id
|
|
1365
1476
|
},
|
|
1366
|
-
newRefDocuments: []
|
|
1477
|
+
newRefDocuments: [document, ...newRefDocuments]
|
|
1367
1478
|
};
|
|
1368
1479
|
}
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
field: {
|
|
1374
|
-
type: 'reference',
|
|
1375
|
-
refType: 'document',
|
|
1376
|
-
refId: refId
|
|
1377
|
-
},
|
|
1378
|
-
newRefDocuments: []
|
|
1379
|
-
};
|
|
1380
|
-
}
|
|
1381
|
-
else {
|
|
1382
|
-
const modelNames = modelField.models;
|
|
1383
|
-
if (!modelName) {
|
|
1384
|
-
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1385
|
-
// the 'type' property in default values
|
|
1386
|
-
if ('type' in rest) {
|
|
1387
|
-
modelName = rest.type;
|
|
1388
|
-
rest = lodash_1.default.omit(rest, 'type');
|
|
1389
|
-
}
|
|
1390
|
-
else if (modelNames.length === 1) {
|
|
1391
|
-
modelName = modelNames[0];
|
|
1392
|
-
}
|
|
1393
|
-
}
|
|
1394
|
-
const model = modelMap[modelName];
|
|
1395
|
-
if (!model) {
|
|
1396
|
-
throw new Error(`no model with name '${modelName}' was found`);
|
|
1397
|
-
}
|
|
1398
|
-
const { document, newRefDocuments } = await createDocumentRecursively({
|
|
1399
|
-
object: rest,
|
|
1400
|
-
model: model,
|
|
1401
|
-
modelMap,
|
|
1402
|
-
locale,
|
|
1403
|
-
userContext,
|
|
1404
|
-
contentSourceInstance
|
|
1405
|
-
});
|
|
1406
|
-
return {
|
|
1407
|
-
field: {
|
|
1408
|
-
type: 'reference',
|
|
1409
|
-
refType: 'document',
|
|
1410
|
-
refId: document.id
|
|
1411
|
-
},
|
|
1412
|
-
newRefDocuments: [document, ...newRefDocuments]
|
|
1413
|
-
};
|
|
1414
|
-
}
|
|
1480
|
+
}
|
|
1481
|
+
else if (modelField.type === 'list') {
|
|
1482
|
+
if (!Array.isArray(value)) {
|
|
1483
|
+
throw new Error(`value for list field must be array`);
|
|
1415
1484
|
}
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
}
|
|
1420
|
-
const itemsField = modelField.items;
|
|
1421
|
-
if (!itemsField) {
|
|
1422
|
-
throw new Error(`list field does not define items`);
|
|
1423
|
-
}
|
|
1424
|
-
const arrayResult = await (0, utils_1.mapPromise)(value, async (item, index) => {
|
|
1425
|
-
return createNestedField({
|
|
1426
|
-
value: item,
|
|
1427
|
-
modelField: itemsField,
|
|
1428
|
-
fieldPath: fieldPath.concat(index)
|
|
1429
|
-
});
|
|
1430
|
-
});
|
|
1431
|
-
return {
|
|
1432
|
-
field: {
|
|
1433
|
-
type: 'list',
|
|
1434
|
-
items: arrayResult.map((result) => result.field)
|
|
1435
|
-
},
|
|
1436
|
-
newRefDocuments: arrayResult.reduce((result, { newRefDocuments }) => result.concat(newRefDocuments), [])
|
|
1437
|
-
};
|
|
1485
|
+
const itemsField = modelField.items;
|
|
1486
|
+
if (!itemsField) {
|
|
1487
|
+
throw new Error(`list field does not define items`);
|
|
1438
1488
|
}
|
|
1489
|
+
const arrayResult = await (0, utils_1.mapPromise)(value, async (item, index) => {
|
|
1490
|
+
return createNestedField({
|
|
1491
|
+
value: item,
|
|
1492
|
+
modelField: itemsField,
|
|
1493
|
+
fieldPath: fieldPath.concat(index),
|
|
1494
|
+
modelMap,
|
|
1495
|
+
locale,
|
|
1496
|
+
userContext,
|
|
1497
|
+
contentSourceInstance
|
|
1498
|
+
});
|
|
1499
|
+
});
|
|
1439
1500
|
return {
|
|
1440
1501
|
field: {
|
|
1441
|
-
type:
|
|
1442
|
-
|
|
1502
|
+
type: 'list',
|
|
1503
|
+
items: arrayResult.map((result) => result.field)
|
|
1443
1504
|
},
|
|
1444
|
-
newRefDocuments: []
|
|
1505
|
+
newRefDocuments: arrayResult.reduce((result, { newRefDocuments }) => result.concat(newRefDocuments), [])
|
|
1445
1506
|
};
|
|
1446
|
-
}
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1507
|
+
}
|
|
1508
|
+
return {
|
|
1509
|
+
field: {
|
|
1510
|
+
type: modelField.type,
|
|
1511
|
+
value: value
|
|
1512
|
+
},
|
|
1450
1513
|
newRefDocuments: []
|
|
1451
1514
|
};
|
|
1452
|
-
const objectFieldNames = Object.keys(object);
|
|
1453
|
-
for (const modelField of modelFields) {
|
|
1454
|
-
const fieldName = modelField.name;
|
|
1455
|
-
let value;
|
|
1456
|
-
if (fieldName in object) {
|
|
1457
|
-
value = object[fieldName];
|
|
1458
|
-
lodash_1.default.pull(objectFieldNames, fieldName);
|
|
1459
|
-
}
|
|
1460
|
-
else if (modelField.const) {
|
|
1461
|
-
value = modelField.const;
|
|
1462
|
-
}
|
|
1463
|
-
else if (!lodash_1.default.isNil(modelField.default)) {
|
|
1464
|
-
value = modelField.default;
|
|
1465
|
-
}
|
|
1466
|
-
if (!lodash_1.default.isNil(value)) {
|
|
1467
|
-
const fieldResult = await createNestedField({
|
|
1468
|
-
value,
|
|
1469
|
-
modelField,
|
|
1470
|
-
fieldPath: fieldPath.concat(fieldName)
|
|
1471
|
-
});
|
|
1472
|
-
result.fields[fieldName] = fieldResult.field;
|
|
1473
|
-
result.newRefDocuments = result.newRefDocuments.concat(fieldResult.newRefDocuments);
|
|
1474
|
-
}
|
|
1475
|
-
}
|
|
1476
|
-
if (objectFieldNames.length > 0) {
|
|
1477
|
-
throw new Error(`no model fields found when creating a document with fields: '${objectFieldNames.join(', ')}'`);
|
|
1478
|
-
}
|
|
1479
|
-
return result;
|
|
1480
1515
|
}
|
|
1481
1516
|
function getModelFieldForFieldAtPath(document, model, fieldPath, modelMap, locale) {
|
|
1482
1517
|
if (lodash_1.default.isEmpty(fieldPath)) {
|
|
@@ -1576,12 +1611,13 @@ function getModelFieldForFieldAtPath(document, model, fieldPath, modelMap, local
|
|
|
1576
1611
|
return getField(childDocField, childModelField, childFieldPath);
|
|
1577
1612
|
}
|
|
1578
1613
|
async function convertOperationField({ operationField, fieldPath, modelField, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1579
|
-
|
|
1614
|
+
// for insert operations, the modelField will be of the list, so get the modelField of the list items
|
|
1615
|
+
const modelFieldOrListItems = modelField.type === 'list' ? modelField.items : modelField;
|
|
1580
1616
|
switch (operationField.type) {
|
|
1581
|
-
case 'object':
|
|
1582
|
-
result = await createNestedObjectRecursively({
|
|
1617
|
+
case 'object': {
|
|
1618
|
+
const result = await createNestedObjectRecursively({
|
|
1583
1619
|
object: operationField.object,
|
|
1584
|
-
modelFields:
|
|
1620
|
+
modelFields: modelFieldOrListItems.fields,
|
|
1585
1621
|
fieldPath: fieldPath,
|
|
1586
1622
|
modelMap,
|
|
1587
1623
|
locale,
|
|
@@ -1592,12 +1628,13 @@ async function convertOperationField({ operationField, fieldPath, modelField, mo
|
|
|
1592
1628
|
type: operationField.type,
|
|
1593
1629
|
fields: result.fields
|
|
1594
1630
|
};
|
|
1595
|
-
|
|
1631
|
+
}
|
|
1632
|
+
case 'model': {
|
|
1596
1633
|
const model = modelMap[operationField.modelName];
|
|
1597
1634
|
if (!model) {
|
|
1598
1635
|
throw new Error(`error updating document, could not find document model: '${operationField.modelName}'`);
|
|
1599
1636
|
}
|
|
1600
|
-
result = await createNestedObjectRecursively({
|
|
1637
|
+
const result = await createNestedObjectRecursively({
|
|
1601
1638
|
object: operationField.object,
|
|
1602
1639
|
modelFields: model.fields,
|
|
1603
1640
|
fieldPath,
|
|
@@ -1611,6 +1648,51 @@ async function convertOperationField({ operationField, fieldPath, modelField, mo
|
|
|
1611
1648
|
modelName: operationField.modelName,
|
|
1612
1649
|
fields: result.fields
|
|
1613
1650
|
};
|
|
1651
|
+
}
|
|
1652
|
+
case 'list': {
|
|
1653
|
+
if (modelField.type !== 'list') {
|
|
1654
|
+
throw new Error(`'the operation field type '${operationField.type}' does not match the model field type '${modelField.type}'`);
|
|
1655
|
+
}
|
|
1656
|
+
const result = await (0, utils_1.mapPromise)(operationField.items, async (item, index) => {
|
|
1657
|
+
const result = await createNestedField({
|
|
1658
|
+
value: item,
|
|
1659
|
+
modelField: modelField.items,
|
|
1660
|
+
fieldPath,
|
|
1661
|
+
modelMap,
|
|
1662
|
+
locale,
|
|
1663
|
+
userContext,
|
|
1664
|
+
contentSourceInstance
|
|
1665
|
+
});
|
|
1666
|
+
return result.field;
|
|
1667
|
+
});
|
|
1668
|
+
return {
|
|
1669
|
+
type: operationField.type,
|
|
1670
|
+
items: result
|
|
1671
|
+
};
|
|
1672
|
+
}
|
|
1673
|
+
case 'string':
|
|
1674
|
+
if (typeof operationField.value !== 'string') {
|
|
1675
|
+
return {
|
|
1676
|
+
type: operationField.type,
|
|
1677
|
+
value: ''
|
|
1678
|
+
};
|
|
1679
|
+
}
|
|
1680
|
+
return operationField;
|
|
1681
|
+
case 'enum':
|
|
1682
|
+
if (typeof operationField.value !== 'string') {
|
|
1683
|
+
if (modelFieldOrListItems.type !== 'enum') {
|
|
1684
|
+
throw new Error(`'the operation field type 'enum' does not match the model field type '${modelFieldOrListItems.type}'`);
|
|
1685
|
+
}
|
|
1686
|
+
const option = modelFieldOrListItems.options[0];
|
|
1687
|
+
const optionValue = typeof option === 'object' ? option.value : option;
|
|
1688
|
+
return {
|
|
1689
|
+
type: operationField.type,
|
|
1690
|
+
value: optionValue
|
|
1691
|
+
};
|
|
1692
|
+
}
|
|
1693
|
+
return operationField;
|
|
1694
|
+
case 'image':
|
|
1695
|
+
return operationField;
|
|
1614
1696
|
default:
|
|
1615
1697
|
return operationField;
|
|
1616
1698
|
}
|
|
@@ -1640,4 +1722,20 @@ function isStackbitConfigFile(filePath) {
|
|
|
1640
1722
|
const isMainStackbitConfigFile = pathObject.name === 'stackbit' && ['yaml', 'yml'].includes(pathObject.ext.substring(1));
|
|
1641
1723
|
return isMainStackbitConfigFile || isInDotStackbitFolder;
|
|
1642
1724
|
}
|
|
1725
|
+
function getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, objects) {
|
|
1726
|
+
const documents = [];
|
|
1727
|
+
const assets = [];
|
|
1728
|
+
for (const object of objects) {
|
|
1729
|
+
if (object.srcObjectId in contentSourceData.csiDocumentMap) {
|
|
1730
|
+
documents.push(contentSourceData.csiDocumentMap[object.srcObjectId]);
|
|
1731
|
+
}
|
|
1732
|
+
else if (object.srcObjectId in contentSourceData.csiAssetMap) {
|
|
1733
|
+
assets.push(contentSourceData.csiAssetMap[object.srcObjectId]);
|
|
1734
|
+
}
|
|
1735
|
+
}
|
|
1736
|
+
return {
|
|
1737
|
+
documents,
|
|
1738
|
+
assets
|
|
1739
|
+
};
|
|
1740
|
+
}
|
|
1643
1741
|
//# sourceMappingURL=content-store.js.map
|