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