@stackbit/cms-core 0.0.19-alpha.0 → 0.0.19-alpha.1
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 +25 -1
- package/dist/content-source-interface.d.ts.map +1 -1
- package/dist/content-store-types.d.ts +6 -2
- package/dist/content-store-types.d.ts.map +1 -1
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +239 -186
- package/dist/content-store.js.map +1 -1
- package/package.json +2 -2
- package/src/content-source-interface.ts +36 -5
- package/src/content-store-types.ts +13 -3
- package/src/content-store.ts +265 -217
package/dist/content-store.js
CHANGED
|
@@ -920,26 +920,27 @@ function mapCSIDocumentToStoreDocument({ csiDocument, model, modelMap, defaultLo
|
|
|
920
920
|
fields: mapCSIFieldsToStoreFields({
|
|
921
921
|
csiDocumentFields: csiDocument.fields,
|
|
922
922
|
modelFields: (_b = model.fields) !== null && _b !== void 0 ? _b : [],
|
|
923
|
-
|
|
924
|
-
|
|
923
|
+
context: {
|
|
924
|
+
modelMap,
|
|
925
|
+
defaultLocaleCode
|
|
926
|
+
}
|
|
925
927
|
})
|
|
926
928
|
};
|
|
927
929
|
}
|
|
928
|
-
function mapCSIFieldsToStoreFields({ csiDocumentFields, modelFields,
|
|
930
|
+
function mapCSIFieldsToStoreFields({ csiDocumentFields, modelFields, context }) {
|
|
929
931
|
return modelFields.reduce((result, modelField) => {
|
|
930
932
|
const csiDocumentField = csiDocumentFields[modelField.name];
|
|
931
|
-
const docField =
|
|
933
|
+
const docField = mapCSIFieldToStoreField({
|
|
932
934
|
csiDocumentField,
|
|
933
935
|
modelField,
|
|
934
|
-
|
|
935
|
-
defaultLocaleCode
|
|
936
|
+
context
|
|
936
937
|
});
|
|
937
938
|
docField.label = modelField.label;
|
|
938
939
|
result[modelField.name] = docField;
|
|
939
940
|
return result;
|
|
940
941
|
}, {});
|
|
941
942
|
}
|
|
942
|
-
function
|
|
943
|
+
function mapCSIFieldToStoreField({ csiDocumentField, modelField, context }) {
|
|
943
944
|
if (!csiDocumentField) {
|
|
944
945
|
const isUnset = ['object', 'model', 'reference', 'richText', 'markdown', 'image', 'file', 'json'].includes(modelField.type);
|
|
945
946
|
return {
|
|
@@ -951,11 +952,11 @@ function mapSourceFieldToStoreField({ csiDocumentField, modelField, modelMap, de
|
|
|
951
952
|
// TODO: check if need to add "options" to "enum" and subtype/min/max to "number"
|
|
952
953
|
switch (modelField.type) {
|
|
953
954
|
case 'object':
|
|
954
|
-
return mapObjectField(csiDocumentField, modelField,
|
|
955
|
+
return mapObjectField(csiDocumentField, modelField, context);
|
|
955
956
|
case 'model':
|
|
956
|
-
return mapModelField(csiDocumentField, modelField,
|
|
957
|
+
return mapModelField(csiDocumentField, modelField, context);
|
|
957
958
|
case 'list':
|
|
958
|
-
return mapListField(csiDocumentField, modelField,
|
|
959
|
+
return mapListField(csiDocumentField, modelField, context);
|
|
959
960
|
case 'richText':
|
|
960
961
|
return mapRichTextField(csiDocumentField);
|
|
961
962
|
case 'markdown':
|
|
@@ -964,17 +965,16 @@ function mapSourceFieldToStoreField({ csiDocumentField, modelField, modelMap, de
|
|
|
964
965
|
return csiDocumentField;
|
|
965
966
|
}
|
|
966
967
|
}
|
|
967
|
-
function mapObjectField(csiDocumentField, modelField,
|
|
968
|
+
function mapObjectField(csiDocumentField, modelField, context) {
|
|
968
969
|
var _a, _b, _c;
|
|
969
970
|
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
970
971
|
return {
|
|
971
972
|
type: csiDocumentField.type,
|
|
972
|
-
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, modelField !== null && modelField !== void 0 ? modelField : [], defaultLocaleCode),
|
|
973
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, modelField !== null && modelField !== void 0 ? modelField : [], context.defaultLocaleCode),
|
|
973
974
|
fields: mapCSIFieldsToStoreFields({
|
|
974
975
|
csiDocumentFields: (_b = csiDocumentField.fields) !== null && _b !== void 0 ? _b : {},
|
|
975
976
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
976
|
-
|
|
977
|
-
defaultLocaleCode
|
|
977
|
+
context
|
|
978
978
|
})
|
|
979
979
|
};
|
|
980
980
|
}
|
|
@@ -989,27 +989,25 @@ function mapObjectField(csiDocumentField, modelField, modelMap, defaultLocaleCod
|
|
|
989
989
|
fields: mapCSIFieldsToStoreFields({
|
|
990
990
|
csiDocumentFields: (_b = locale.fields) !== null && _b !== void 0 ? _b : {},
|
|
991
991
|
modelFields: (_c = modelField.fields) !== null && _c !== void 0 ? _c : [],
|
|
992
|
-
|
|
993
|
-
defaultLocaleCode
|
|
992
|
+
context
|
|
994
993
|
})
|
|
995
994
|
};
|
|
996
995
|
})
|
|
997
996
|
};
|
|
998
997
|
}
|
|
999
|
-
function mapModelField(csiDocumentField, modelField,
|
|
998
|
+
function mapModelField(csiDocumentField, modelField, context) {
|
|
1000
999
|
var _a, _b, _c, _d;
|
|
1001
1000
|
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1002
|
-
const model = modelMap[csiDocumentField.modelName];
|
|
1001
|
+
const model = context.modelMap[csiDocumentField.modelName];
|
|
1003
1002
|
return {
|
|
1004
1003
|
type: csiDocumentField.type,
|
|
1005
|
-
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, model, defaultLocaleCode),
|
|
1004
|
+
srcObjectLabel: getObjectLabel((_a = csiDocumentField.fields) !== null && _a !== void 0 ? _a : {}, model, context.defaultLocaleCode),
|
|
1006
1005
|
srcModelName: csiDocumentField.modelName,
|
|
1007
1006
|
srcModelLabel: (_b = model.label) !== null && _b !== void 0 ? _b : lodash_1.default.startCase(model.name),
|
|
1008
1007
|
fields: mapCSIFieldsToStoreFields({
|
|
1009
1008
|
csiDocumentFields: (_c = csiDocumentField.fields) !== null && _c !== void 0 ? _c : {},
|
|
1010
1009
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
1011
|
-
|
|
1012
|
-
defaultLocaleCode
|
|
1010
|
+
context
|
|
1013
1011
|
})
|
|
1014
1012
|
};
|
|
1015
1013
|
}
|
|
@@ -1018,7 +1016,7 @@ function mapModelField(csiDocumentField, modelField, modelMap, defaultLocaleCode
|
|
|
1018
1016
|
localized: true,
|
|
1019
1017
|
locales: lodash_1.default.mapValues(csiDocumentField.locales, (locale) => {
|
|
1020
1018
|
var _a, _b, _c, _d;
|
|
1021
|
-
const model = modelMap[locale.modelName];
|
|
1019
|
+
const model = context.modelMap[locale.modelName];
|
|
1022
1020
|
return {
|
|
1023
1021
|
locale: locale.locale,
|
|
1024
1022
|
srcObjectLabel: getObjectLabel((_a = locale.fields) !== null && _a !== void 0 ? _a : {}, model, locale.locale),
|
|
@@ -1027,24 +1025,22 @@ function mapModelField(csiDocumentField, modelField, modelMap, defaultLocaleCode
|
|
|
1027
1025
|
fields: mapCSIFieldsToStoreFields({
|
|
1028
1026
|
csiDocumentFields: (_c = locale.fields) !== null && _c !== void 0 ? _c : {},
|
|
1029
1027
|
modelFields: (_d = model.fields) !== null && _d !== void 0 ? _d : [],
|
|
1030
|
-
|
|
1031
|
-
defaultLocaleCode
|
|
1028
|
+
context
|
|
1032
1029
|
})
|
|
1033
1030
|
};
|
|
1034
1031
|
})
|
|
1035
1032
|
};
|
|
1036
1033
|
}
|
|
1037
|
-
function mapListField(csiDocumentField, modelField,
|
|
1034
|
+
function mapListField(csiDocumentField, modelField, context) {
|
|
1038
1035
|
if (!(0, content_source_interface_1.isLocalizedField)(csiDocumentField)) {
|
|
1039
1036
|
return {
|
|
1040
1037
|
type: csiDocumentField.type,
|
|
1041
1038
|
items: csiDocumentField.items.map((item) => {
|
|
1042
1039
|
var _a;
|
|
1043
|
-
return
|
|
1040
|
+
return mapCSIFieldToStoreField({
|
|
1044
1041
|
csiDocumentField: item,
|
|
1045
1042
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1046
|
-
|
|
1047
|
-
defaultLocaleCode
|
|
1043
|
+
context
|
|
1048
1044
|
});
|
|
1049
1045
|
})
|
|
1050
1046
|
};
|
|
@@ -1058,11 +1054,10 @@ function mapListField(csiDocumentField, modelField, modelMap, defaultLocaleCode)
|
|
|
1058
1054
|
locale: locale.locale,
|
|
1059
1055
|
items: ((_a = locale.items) !== null && _a !== void 0 ? _a : []).map((item) => {
|
|
1060
1056
|
var _a;
|
|
1061
|
-
return
|
|
1057
|
+
return mapCSIFieldToStoreField({
|
|
1062
1058
|
csiDocumentField: item,
|
|
1063
1059
|
modelField: (_a = modelField.items) !== null && _a !== void 0 ? _a : { type: 'string' },
|
|
1064
|
-
|
|
1065
|
-
defaultLocaleCode
|
|
1060
|
+
context
|
|
1066
1061
|
});
|
|
1067
1062
|
})
|
|
1068
1063
|
};
|
|
@@ -1322,192 +1317,200 @@ async function createDocumentRecursively({ object, model, modelMap, locale, user
|
|
|
1322
1317
|
};
|
|
1323
1318
|
}
|
|
1324
1319
|
async function createNestedObjectRecursively({ object, modelFields, fieldPath, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1320
|
+
object = object !== null && object !== void 0 ? object : {};
|
|
1321
|
+
const result = {
|
|
1322
|
+
fields: {},
|
|
1323
|
+
newRefDocuments: []
|
|
1324
|
+
};
|
|
1325
|
+
const objectFieldNames = Object.keys(object);
|
|
1326
|
+
for (const modelField of modelFields) {
|
|
1327
|
+
const fieldName = modelField.name;
|
|
1328
|
+
let value;
|
|
1329
|
+
if (fieldName in object) {
|
|
1330
|
+
value = object[fieldName];
|
|
1331
|
+
lodash_1.default.pull(objectFieldNames, fieldName);
|
|
1332
|
+
}
|
|
1333
|
+
else if (modelField.const) {
|
|
1334
|
+
value = modelField.const;
|
|
1335
|
+
}
|
|
1336
|
+
else if (!lodash_1.default.isNil(modelField.default)) {
|
|
1337
|
+
value = modelField.default;
|
|
1338
|
+
}
|
|
1339
|
+
if (!lodash_1.default.isNil(value)) {
|
|
1340
|
+
const fieldResult = await createNestedField({
|
|
1341
|
+
value,
|
|
1342
|
+
modelField,
|
|
1343
|
+
fieldPath: fieldPath.concat(fieldName),
|
|
1332
1344
|
modelMap,
|
|
1333
1345
|
locale,
|
|
1334
1346
|
userContext,
|
|
1335
1347
|
contentSourceInstance
|
|
1336
1348
|
});
|
|
1349
|
+
result.fields[fieldName] = fieldResult.field;
|
|
1350
|
+
result.newRefDocuments = result.newRefDocuments.concat(fieldResult.newRefDocuments);
|
|
1351
|
+
}
|
|
1352
|
+
}
|
|
1353
|
+
if (objectFieldNames.length > 0) {
|
|
1354
|
+
throw new Error(`no model fields found when creating a document with fields: '${objectFieldNames.join(', ')}'`);
|
|
1355
|
+
}
|
|
1356
|
+
return result;
|
|
1357
|
+
}
|
|
1358
|
+
async function createNestedField({ value, modelField, fieldPath, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1359
|
+
var _a;
|
|
1360
|
+
if (modelField.type === 'object') {
|
|
1361
|
+
const result = await createNestedObjectRecursively({
|
|
1362
|
+
object: value,
|
|
1363
|
+
modelFields: modelField.fields,
|
|
1364
|
+
fieldPath,
|
|
1365
|
+
modelMap,
|
|
1366
|
+
locale,
|
|
1367
|
+
userContext,
|
|
1368
|
+
contentSourceInstance
|
|
1369
|
+
});
|
|
1370
|
+
return {
|
|
1371
|
+
field: {
|
|
1372
|
+
type: 'object',
|
|
1373
|
+
fields: result.fields
|
|
1374
|
+
},
|
|
1375
|
+
newRefDocuments: result.newRefDocuments
|
|
1376
|
+
};
|
|
1377
|
+
}
|
|
1378
|
+
else if (modelField.type === 'model') {
|
|
1379
|
+
let { $$type, ...rest } = value;
|
|
1380
|
+
const modelNames = modelField.models;
|
|
1381
|
+
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1382
|
+
// the 'type' property in default values
|
|
1383
|
+
if (!$$type && 'type' in rest) {
|
|
1384
|
+
$$type = rest.type;
|
|
1385
|
+
rest = lodash_1.default.omit(rest, 'type');
|
|
1386
|
+
}
|
|
1387
|
+
const modelName = $$type !== null && $$type !== void 0 ? $$type : (modelNames.length === 1 ? modelNames[0] : null);
|
|
1388
|
+
if (!modelName) {
|
|
1389
|
+
throw new Error(`no $$type was specified for nested model`);
|
|
1390
|
+
}
|
|
1391
|
+
const model = modelMap[modelName];
|
|
1392
|
+
if (!model) {
|
|
1393
|
+
throw new Error(`no model with name '${modelName}' was found`);
|
|
1394
|
+
}
|
|
1395
|
+
const result = await createNestedObjectRecursively({
|
|
1396
|
+
object: rest,
|
|
1397
|
+
modelFields: (_a = model.fields) !== null && _a !== void 0 ? _a : [],
|
|
1398
|
+
fieldPath,
|
|
1399
|
+
modelMap,
|
|
1400
|
+
locale,
|
|
1401
|
+
userContext,
|
|
1402
|
+
contentSourceInstance
|
|
1403
|
+
});
|
|
1404
|
+
return {
|
|
1405
|
+
field: {
|
|
1406
|
+
type: 'model',
|
|
1407
|
+
modelName: modelName,
|
|
1408
|
+
fields: result.fields
|
|
1409
|
+
},
|
|
1410
|
+
newRefDocuments: result.newRefDocuments
|
|
1411
|
+
};
|
|
1412
|
+
}
|
|
1413
|
+
else if (modelField.type === 'image') {
|
|
1414
|
+
let refId;
|
|
1415
|
+
if (lodash_1.default.isPlainObject(value)) {
|
|
1416
|
+
refId = value.$$ref;
|
|
1417
|
+
}
|
|
1418
|
+
else {
|
|
1419
|
+
refId = value;
|
|
1420
|
+
}
|
|
1421
|
+
if (!refId) {
|
|
1422
|
+
throw new Error(`reference field must specify a value`);
|
|
1423
|
+
}
|
|
1424
|
+
return {
|
|
1425
|
+
field: {
|
|
1426
|
+
type: 'reference',
|
|
1427
|
+
refType: 'asset',
|
|
1428
|
+
refId: refId
|
|
1429
|
+
},
|
|
1430
|
+
newRefDocuments: []
|
|
1431
|
+
};
|
|
1432
|
+
}
|
|
1433
|
+
else if (modelField.type === 'reference') {
|
|
1434
|
+
let { $$ref: refId = null, $$type: modelName = null, ...rest } = lodash_1.default.isPlainObject(value) ? value : { $$ref: value };
|
|
1435
|
+
if (refId) {
|
|
1337
1436
|
return {
|
|
1338
1437
|
field: {
|
|
1339
|
-
type: '
|
|
1340
|
-
|
|
1438
|
+
type: 'reference',
|
|
1439
|
+
refType: 'document',
|
|
1440
|
+
refId: refId
|
|
1341
1441
|
},
|
|
1342
|
-
newRefDocuments:
|
|
1442
|
+
newRefDocuments: []
|
|
1343
1443
|
};
|
|
1344
1444
|
}
|
|
1345
|
-
else
|
|
1346
|
-
let { $$type, ...rest } = value;
|
|
1445
|
+
else {
|
|
1347
1446
|
const modelNames = modelField.models;
|
|
1348
|
-
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1349
|
-
// the 'type' property in default values
|
|
1350
|
-
if (!$$type && 'type' in rest) {
|
|
1351
|
-
$$type = rest.type;
|
|
1352
|
-
rest = lodash_1.default.omit(rest, 'type');
|
|
1353
|
-
}
|
|
1354
|
-
const modelName = $$type !== null && $$type !== void 0 ? $$type : (modelNames.length === 1 ? modelNames[0] : null);
|
|
1355
1447
|
if (!modelName) {
|
|
1356
|
-
|
|
1448
|
+
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1449
|
+
// the 'type' property in default values
|
|
1450
|
+
if ('type' in rest) {
|
|
1451
|
+
modelName = rest.type;
|
|
1452
|
+
rest = lodash_1.default.omit(rest, 'type');
|
|
1453
|
+
}
|
|
1454
|
+
else if (modelNames.length === 1) {
|
|
1455
|
+
modelName = modelNames[0];
|
|
1456
|
+
}
|
|
1357
1457
|
}
|
|
1358
1458
|
const model = modelMap[modelName];
|
|
1359
1459
|
if (!model) {
|
|
1360
1460
|
throw new Error(`no model with name '${modelName}' was found`);
|
|
1361
1461
|
}
|
|
1362
|
-
const
|
|
1462
|
+
const { document, newRefDocuments } = await createDocumentRecursively({
|
|
1363
1463
|
object: rest,
|
|
1364
|
-
|
|
1365
|
-
fieldPath,
|
|
1464
|
+
model: model,
|
|
1366
1465
|
modelMap,
|
|
1367
1466
|
locale,
|
|
1368
1467
|
userContext,
|
|
1369
1468
|
contentSourceInstance
|
|
1370
1469
|
});
|
|
1371
|
-
return {
|
|
1372
|
-
field: {
|
|
1373
|
-
type: 'model',
|
|
1374
|
-
modelName: modelName,
|
|
1375
|
-
fields: result.fields
|
|
1376
|
-
},
|
|
1377
|
-
newRefDocuments: result.newRefDocuments
|
|
1378
|
-
};
|
|
1379
|
-
}
|
|
1380
|
-
else if (modelField.type === 'image') {
|
|
1381
|
-
let refId;
|
|
1382
|
-
if (lodash_1.default.isPlainObject(value)) {
|
|
1383
|
-
refId = value.$$ref;
|
|
1384
|
-
}
|
|
1385
|
-
else {
|
|
1386
|
-
refId = value;
|
|
1387
|
-
}
|
|
1388
|
-
if (!refId) {
|
|
1389
|
-
throw new Error(`reference field must specify a value`);
|
|
1390
|
-
}
|
|
1391
1470
|
return {
|
|
1392
1471
|
field: {
|
|
1393
1472
|
type: 'reference',
|
|
1394
|
-
refType: '
|
|
1395
|
-
refId:
|
|
1473
|
+
refType: 'document',
|
|
1474
|
+
refId: document.id
|
|
1396
1475
|
},
|
|
1397
|
-
newRefDocuments: []
|
|
1476
|
+
newRefDocuments: [document, ...newRefDocuments]
|
|
1398
1477
|
};
|
|
1399
1478
|
}
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
field: {
|
|
1405
|
-
type: 'reference',
|
|
1406
|
-
refType: 'document',
|
|
1407
|
-
refId: refId
|
|
1408
|
-
},
|
|
1409
|
-
newRefDocuments: []
|
|
1410
|
-
};
|
|
1411
|
-
}
|
|
1412
|
-
else {
|
|
1413
|
-
const modelNames = modelField.models;
|
|
1414
|
-
if (!modelName) {
|
|
1415
|
-
// for backward compatibility check if the object has 'type' instead of '$$type' because older projects use
|
|
1416
|
-
// the 'type' property in default values
|
|
1417
|
-
if ('type' in rest) {
|
|
1418
|
-
modelName = rest.type;
|
|
1419
|
-
rest = lodash_1.default.omit(rest, 'type');
|
|
1420
|
-
}
|
|
1421
|
-
else if (modelNames.length === 1) {
|
|
1422
|
-
modelName = modelNames[0];
|
|
1423
|
-
}
|
|
1424
|
-
}
|
|
1425
|
-
const model = modelMap[modelName];
|
|
1426
|
-
if (!model) {
|
|
1427
|
-
throw new Error(`no model with name '${modelName}' was found`);
|
|
1428
|
-
}
|
|
1429
|
-
const { document, newRefDocuments } = await createDocumentRecursively({
|
|
1430
|
-
object: rest,
|
|
1431
|
-
model: model,
|
|
1432
|
-
modelMap,
|
|
1433
|
-
locale,
|
|
1434
|
-
userContext,
|
|
1435
|
-
contentSourceInstance
|
|
1436
|
-
});
|
|
1437
|
-
return {
|
|
1438
|
-
field: {
|
|
1439
|
-
type: 'reference',
|
|
1440
|
-
refType: 'document',
|
|
1441
|
-
refId: document.id
|
|
1442
|
-
},
|
|
1443
|
-
newRefDocuments: [document, ...newRefDocuments]
|
|
1444
|
-
};
|
|
1445
|
-
}
|
|
1479
|
+
}
|
|
1480
|
+
else if (modelField.type === 'list') {
|
|
1481
|
+
if (!Array.isArray(value)) {
|
|
1482
|
+
throw new Error(`value for list field must be array`);
|
|
1446
1483
|
}
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
}
|
|
1451
|
-
const itemsField = modelField.items;
|
|
1452
|
-
if (!itemsField) {
|
|
1453
|
-
throw new Error(`list field does not define items`);
|
|
1454
|
-
}
|
|
1455
|
-
const arrayResult = await (0, utils_1.mapPromise)(value, async (item, index) => {
|
|
1456
|
-
return createNestedField({
|
|
1457
|
-
value: item,
|
|
1458
|
-
modelField: itemsField,
|
|
1459
|
-
fieldPath: fieldPath.concat(index)
|
|
1460
|
-
});
|
|
1461
|
-
});
|
|
1462
|
-
return {
|
|
1463
|
-
field: {
|
|
1464
|
-
type: 'list',
|
|
1465
|
-
items: arrayResult.map((result) => result.field)
|
|
1466
|
-
},
|
|
1467
|
-
newRefDocuments: arrayResult.reduce((result, { newRefDocuments }) => result.concat(newRefDocuments), [])
|
|
1468
|
-
};
|
|
1484
|
+
const itemsField = modelField.items;
|
|
1485
|
+
if (!itemsField) {
|
|
1486
|
+
throw new Error(`list field does not define items`);
|
|
1469
1487
|
}
|
|
1488
|
+
const arrayResult = await (0, utils_1.mapPromise)(value, async (item, index) => {
|
|
1489
|
+
return createNestedField({
|
|
1490
|
+
value: item,
|
|
1491
|
+
modelField: itemsField,
|
|
1492
|
+
fieldPath: fieldPath.concat(index),
|
|
1493
|
+
modelMap,
|
|
1494
|
+
locale,
|
|
1495
|
+
userContext,
|
|
1496
|
+
contentSourceInstance
|
|
1497
|
+
});
|
|
1498
|
+
});
|
|
1470
1499
|
return {
|
|
1471
1500
|
field: {
|
|
1472
|
-
type:
|
|
1473
|
-
|
|
1501
|
+
type: 'list',
|
|
1502
|
+
items: arrayResult.map((result) => result.field)
|
|
1474
1503
|
},
|
|
1475
|
-
newRefDocuments: []
|
|
1504
|
+
newRefDocuments: arrayResult.reduce((result, { newRefDocuments }) => result.concat(newRefDocuments), [])
|
|
1476
1505
|
};
|
|
1477
|
-
}
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1506
|
+
}
|
|
1507
|
+
return {
|
|
1508
|
+
field: {
|
|
1509
|
+
type: modelField.type,
|
|
1510
|
+
value: value
|
|
1511
|
+
},
|
|
1481
1512
|
newRefDocuments: []
|
|
1482
1513
|
};
|
|
1483
|
-
const objectFieldNames = Object.keys(object);
|
|
1484
|
-
for (const modelField of modelFields) {
|
|
1485
|
-
const fieldName = modelField.name;
|
|
1486
|
-
let value;
|
|
1487
|
-
if (fieldName in object) {
|
|
1488
|
-
value = object[fieldName];
|
|
1489
|
-
lodash_1.default.pull(objectFieldNames, fieldName);
|
|
1490
|
-
}
|
|
1491
|
-
else if (modelField.const) {
|
|
1492
|
-
value = modelField.const;
|
|
1493
|
-
}
|
|
1494
|
-
else if (!lodash_1.default.isNil(modelField.default)) {
|
|
1495
|
-
value = modelField.default;
|
|
1496
|
-
}
|
|
1497
|
-
if (!lodash_1.default.isNil(value)) {
|
|
1498
|
-
const fieldResult = await createNestedField({
|
|
1499
|
-
value,
|
|
1500
|
-
modelField,
|
|
1501
|
-
fieldPath: fieldPath.concat(fieldName)
|
|
1502
|
-
});
|
|
1503
|
-
result.fields[fieldName] = fieldResult.field;
|
|
1504
|
-
result.newRefDocuments = result.newRefDocuments.concat(fieldResult.newRefDocuments);
|
|
1505
|
-
}
|
|
1506
|
-
}
|
|
1507
|
-
if (objectFieldNames.length > 0) {
|
|
1508
|
-
throw new Error(`no model fields found when creating a document with fields: '${objectFieldNames.join(', ')}'`);
|
|
1509
|
-
}
|
|
1510
|
-
return result;
|
|
1511
1514
|
}
|
|
1512
1515
|
function getModelFieldForFieldAtPath(document, model, fieldPath, modelMap, locale) {
|
|
1513
1516
|
if (lodash_1.default.isEmpty(fieldPath)) {
|
|
@@ -1607,12 +1610,13 @@ function getModelFieldForFieldAtPath(document, model, fieldPath, modelMap, local
|
|
|
1607
1610
|
return getField(childDocField, childModelField, childFieldPath);
|
|
1608
1611
|
}
|
|
1609
1612
|
async function convertOperationField({ operationField, fieldPath, modelField, modelMap, locale, userContext, contentSourceInstance }) {
|
|
1610
|
-
|
|
1613
|
+
// for insert operations, the modelField will be of the list, so get the modelField of the list items
|
|
1614
|
+
const modelFieldOrListItems = modelField.type === 'list' ? modelField.items : modelField;
|
|
1611
1615
|
switch (operationField.type) {
|
|
1612
|
-
case 'object':
|
|
1613
|
-
result = await createNestedObjectRecursively({
|
|
1616
|
+
case 'object': {
|
|
1617
|
+
const result = await createNestedObjectRecursively({
|
|
1614
1618
|
object: operationField.object,
|
|
1615
|
-
modelFields:
|
|
1619
|
+
modelFields: modelFieldOrListItems.fields,
|
|
1616
1620
|
fieldPath: fieldPath,
|
|
1617
1621
|
modelMap,
|
|
1618
1622
|
locale,
|
|
@@ -1623,12 +1627,13 @@ async function convertOperationField({ operationField, fieldPath, modelField, mo
|
|
|
1623
1627
|
type: operationField.type,
|
|
1624
1628
|
fields: result.fields
|
|
1625
1629
|
};
|
|
1626
|
-
|
|
1630
|
+
}
|
|
1631
|
+
case 'model': {
|
|
1627
1632
|
const model = modelMap[operationField.modelName];
|
|
1628
1633
|
if (!model) {
|
|
1629
1634
|
throw new Error(`error updating document, could not find document model: '${operationField.modelName}'`);
|
|
1630
1635
|
}
|
|
1631
|
-
result = await createNestedObjectRecursively({
|
|
1636
|
+
const result = await createNestedObjectRecursively({
|
|
1632
1637
|
object: operationField.object,
|
|
1633
1638
|
modelFields: model.fields,
|
|
1634
1639
|
fieldPath,
|
|
@@ -1642,6 +1647,54 @@ async function convertOperationField({ operationField, fieldPath, modelField, mo
|
|
|
1642
1647
|
modelName: operationField.modelName,
|
|
1643
1648
|
fields: result.fields
|
|
1644
1649
|
};
|
|
1650
|
+
}
|
|
1651
|
+
case 'list': {
|
|
1652
|
+
if (modelField.type !== 'list') {
|
|
1653
|
+
throw new Error(`'the operation field type '${operationField.type}' does not match the model field type '${modelField.type}'`);
|
|
1654
|
+
}
|
|
1655
|
+
const result = await (0, utils_1.mapPromise)(operationField.items, async (item, index) => {
|
|
1656
|
+
const result = await createNestedField({
|
|
1657
|
+
value: item,
|
|
1658
|
+
modelField: modelField.items,
|
|
1659
|
+
fieldPath,
|
|
1660
|
+
modelMap,
|
|
1661
|
+
locale,
|
|
1662
|
+
userContext,
|
|
1663
|
+
contentSourceInstance
|
|
1664
|
+
});
|
|
1665
|
+
return result.field;
|
|
1666
|
+
});
|
|
1667
|
+
return {
|
|
1668
|
+
type: operationField.type,
|
|
1669
|
+
items: result
|
|
1670
|
+
};
|
|
1671
|
+
}
|
|
1672
|
+
case 'string':
|
|
1673
|
+
if (typeof operationField.value !== 'string') {
|
|
1674
|
+
return {
|
|
1675
|
+
type: operationField.type,
|
|
1676
|
+
value: ''
|
|
1677
|
+
};
|
|
1678
|
+
}
|
|
1679
|
+
return operationField;
|
|
1680
|
+
case 'enum':
|
|
1681
|
+
if (typeof operationField.value !== 'string') {
|
|
1682
|
+
if (modelFieldOrListItems.type !== 'enum') {
|
|
1683
|
+
throw new Error(`'the operation field type 'enum' does not match the model field type '${modelFieldOrListItems.type}'`);
|
|
1684
|
+
}
|
|
1685
|
+
const option = modelFieldOrListItems.options[0];
|
|
1686
|
+
const optionValue = typeof option === 'object' ? option.value : option;
|
|
1687
|
+
return {
|
|
1688
|
+
type: operationField.type,
|
|
1689
|
+
value: optionValue
|
|
1690
|
+
};
|
|
1691
|
+
}
|
|
1692
|
+
return operationField;
|
|
1693
|
+
case 'image':
|
|
1694
|
+
if (typeof operationField.value !== 'string') {
|
|
1695
|
+
throw new Error('image value was not specified');
|
|
1696
|
+
}
|
|
1697
|
+
return operationField;
|
|
1645
1698
|
default:
|
|
1646
1699
|
return operationField;
|
|
1647
1700
|
}
|