@stackbit/cms-core 0.1.20-cross-references.0 → 0.1.20

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.
Files changed (42) hide show
  1. package/dist/content-store-types.d.ts +8 -34
  2. package/dist/content-store-types.d.ts.map +1 -1
  3. package/dist/content-store-utils.d.ts +1 -3
  4. package/dist/content-store-utils.d.ts.map +1 -1
  5. package/dist/content-store-utils.js +1 -8
  6. package/dist/content-store-utils.js.map +1 -1
  7. package/dist/content-store.d.ts +3 -8
  8. package/dist/content-store.d.ts.map +1 -1
  9. package/dist/content-store.js +45 -71
  10. package/dist/content-store.js.map +1 -1
  11. package/dist/utils/create-update-csi-docs.d.ts +10 -11
  12. package/dist/utils/create-update-csi-docs.d.ts.map +1 -1
  13. package/dist/utils/create-update-csi-docs.js +15 -94
  14. package/dist/utils/create-update-csi-docs.js.map +1 -1
  15. package/dist/utils/csi-to-store-docs-converter.js +1 -69
  16. package/dist/utils/csi-to-store-docs-converter.js.map +1 -1
  17. package/dist/utils/duplicate-document.js +0 -3
  18. package/dist/utils/duplicate-document.js.map +1 -1
  19. package/dist/utils/model-utils.d.ts +4 -4
  20. package/dist/utils/model-utils.d.ts.map +1 -1
  21. package/dist/utils/model-utils.js +2 -67
  22. package/dist/utils/model-utils.js.map +1 -1
  23. package/dist/utils/schema-utils.d.ts +87 -0
  24. package/dist/utils/schema-utils.d.ts.map +1 -0
  25. package/dist/utils/schema-utils.js +195 -0
  26. package/dist/utils/schema-utils.js.map +1 -0
  27. package/dist/utils/store-to-api-docs-converter.d.ts.map +1 -1
  28. package/dist/utils/store-to-api-docs-converter.js +1 -38
  29. package/dist/utils/store-to-api-docs-converter.js.map +1 -1
  30. package/dist/utils/store-to-csi-docs-converter.js +0 -30
  31. package/dist/utils/store-to-csi-docs-converter.js.map +1 -1
  32. package/package.json +4 -4
  33. package/src/content-store-types.ts +3 -41
  34. package/src/content-store-utils.ts +1 -9
  35. package/src/content-store.ts +49 -78
  36. package/src/utils/create-update-csi-docs.ts +19 -110
  37. package/src/utils/csi-to-store-docs-converter.ts +17 -91
  38. package/src/utils/duplicate-document.ts +0 -3
  39. package/src/utils/model-utils.ts +7 -95
  40. package/src/utils/schema-utils.js +212 -0
  41. package/src/utils/store-to-api-docs-converter.ts +1 -38
  42. package/src/utils/store-to-csi-docs-converter.ts +0 -30
@@ -32,8 +32,7 @@ import {
32
32
  getModelFieldForFieldAtPath,
33
33
  getUserContextForSrcType,
34
34
  groupDocumentsByContentSource,
35
- groupModelsByContentSource,
36
- updateOperationValueFieldWithCrossReference
35
+ groupModelsByContentSource
37
36
  } from './content-store-utils';
38
37
  import {
39
38
  getSiteMapEntriesFromStackbitConfig,
@@ -201,7 +200,7 @@ export class ContentStore {
201
200
  this.contentUpdatesWatchTimer.stopTimer();
202
201
  }
203
202
 
204
- async onFilesChange(updatedFiles: string[]): Promise<{ schemaChanged?: boolean; contentChanges: ContentStoreTypes.ContentChangeResult }> {
203
+ async onFilesChange(updatedFiles: string[]): Promise<void> {
205
204
  this.logger.debug('onFilesChange');
206
205
 
207
206
  let schemaChanged = false;
@@ -249,20 +248,15 @@ export class ContentStore {
249
248
  }
250
249
  }
251
250
 
252
- let contentChanges: ContentStoreTypes.ContentChangeResult = {
253
- updatedDocuments: [],
254
- updatedAssets: [],
255
- deletedDocuments: [],
256
- deletedAssets: []
257
- };
258
-
259
251
  // If the schema was changed, there is no need to accumulate or notify about content changes.
260
252
  // The processData will update the store with the latest data. And once the Studio receives
261
253
  // the schemaChanged notification it will reload all the models and the documents with their latest state.
262
254
  if (schemaChanged) {
263
255
  await this.loadContentSourcesAndProcessData({ init: false, contentSourceIds: contentSourceIdsWithChangedSchema });
256
+
257
+ this.onSchemaChangeCallback();
264
258
  } else {
265
- contentChanges = contentChangeEvents.reduce((contentChanges, { contentSourceId, contentChangeEvent }) => {
259
+ const contentChanges = contentChangeEvents.reduce((contentChanges: ContentStoreTypes.ContentChangeResult, { contentSourceId, contentChangeEvent }) => {
266
260
  const contentChangeResult = this.onContentChange(contentSourceId, contentChangeEvent);
267
261
  return {
268
262
  updatedDocuments: contentChanges.updatedDocuments.concat(contentChangeResult.updatedDocuments),
@@ -270,7 +264,12 @@ export class ContentStore {
270
264
  deletedDocuments: contentChanges.deletedDocuments.concat(contentChangeResult.deletedDocuments),
271
265
  deletedAssets: contentChanges.deletedAssets.concat(contentChangeResult.deletedAssets)
272
266
  };
273
- }, contentChanges);
267
+ }, {
268
+ updatedDocuments: [],
269
+ updatedAssets: [],
270
+ deletedDocuments: [],
271
+ deletedAssets: []
272
+ });
274
273
 
275
274
  this.siteMapEntryGroups = await updateSiteMapEntriesWithContentChanges({
276
275
  siteMapEntryGroups: this.siteMapEntryGroups,
@@ -278,14 +277,9 @@ export class ContentStore {
278
277
  stackbitConfig: this.stackbitConfig,
279
278
  contentSourceDataById: this.contentSourceDataById
280
279
  });
281
- }
282
280
 
283
- // TODO: maybe instead of returning object with results
284
- // replace with this.onSchemaChangeCallback() and this.onContentChangeCallback(contentChanges) for consistency of data flow?
285
- return {
286
- schemaChanged: schemaChanged,
287
- contentChanges: contentChanges
288
- };
281
+ this.onContentChangeCallback(contentChanges);
282
+ }
289
283
  }
290
284
 
291
285
  private async loadYamlModels({ stackbitConfig }: { stackbitConfig: Config }): Promise<Model[]> {
@@ -1180,8 +1174,6 @@ export class ContentStore {
1180
1174
  srcDocumentId,
1181
1175
  fieldPath,
1182
1176
  modelName,
1183
- refSrcType,
1184
- refProjectId,
1185
1177
  object,
1186
1178
  index,
1187
1179
  locale,
@@ -1192,14 +1184,12 @@ export class ContentStore {
1192
1184
  srcDocumentId: string;
1193
1185
  fieldPath: (string | number)[];
1194
1186
  modelName?: string;
1195
- refSrcType?: string;
1196
- refProjectId?: string;
1197
1187
  object?: Record<string, any>;
1198
1188
  index?: number;
1199
1189
  locale?: string;
1200
1190
  user?: ContentStoreTypes.User;
1201
- }): Promise<{ srcDocumentId: string; createdDocumentId: string }> {
1202
- this.logger.debug('createAndLinkDocument', { srcType, srcProjectId, srcDocumentId, fieldPath, modelName, refSrcType, refProjectId, index, locale });
1191
+ }): Promise<{ srcDocumentId: string, createdDocumentId: string }> {
1192
+ this.logger.debug('createAndLinkDocument', { srcType, srcProjectId, srcDocumentId, fieldPath, modelName, index, locale });
1203
1193
 
1204
1194
  const contentSourceId = getContentSourceId(srcType, srcProjectId);
1205
1195
  const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
@@ -1213,51 +1203,36 @@ export class ContentStore {
1213
1203
 
1214
1204
  // get the document model
1215
1205
  const documentModelName = document.srcModelName;
1216
- const modelMap = contentSourceData.modelMap;
1217
- const model = modelMap[documentModelName];
1218
1206
  const csiModelMap = contentSourceData.csiModelMap;
1219
1207
  const csiModel = csiModelMap[documentModelName];
1220
- if (!model || !csiModel) {
1208
+ if (!csiModel) {
1221
1209
  throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
1222
1210
  }
1223
1211
 
1224
1212
  // get the 'reference' model field in the updated document that will be used to link the new document
1225
1213
  locale = locale ?? contentSourceData.defaultLocaleCode;
1226
- const modelField = getModelFieldForFieldAtPath(document, model, fieldPath, modelMap, locale);
1227
1214
  const csiModelField = getModelFieldForFieldAtPath(document, csiModel, fieldPath, csiModelMap, locale);
1228
- if (!modelField || !csiModelField) {
1215
+ if (!csiModelField) {
1229
1216
  throw Error(`the "fieldPath" points to non existing model field: ${fieldPath.join('.')}`);
1230
1217
  }
1231
- const fieldProps = modelField.type === 'list' ? modelField.items! : modelField;
1232
- const csiFieldProps = csiModelField.type === 'list' ? csiModelField.items! : csiModelField;
1233
- if (fieldProps.type !== 'reference' && fieldProps.type !== 'cross-reference') {
1218
+ const fieldProps = csiModelField.type === 'list' ? csiModelField.items! : csiModelField;
1219
+ if (fieldProps.type !== 'reference') {
1234
1220
  throw Error(`error in "createAndLinkDocument", this operation can only be used on reference field: ${fieldPath.join('.')}`);
1235
1221
  }
1236
1222
 
1237
1223
  // get the model name for the new document
1238
1224
  if (!modelName && fieldProps.models.length === 1) {
1239
- if (fieldProps.type === 'reference') {
1240
- modelName = fieldProps.models[0];
1241
- } else if (fieldProps.type === 'cross-reference') {
1242
- modelName = fieldProps.models[0]!.modelName;
1243
- }
1225
+ modelName = fieldProps.models[0];
1244
1226
  }
1245
1227
  if (!modelName) {
1246
1228
  throw Error(`error in "createAndLinkDocument", missing "modelName": ${fieldPath.join('.')}`);
1247
1229
  }
1248
1230
 
1249
- if (fieldProps.type === 'reference') {
1250
- refSrcType = srcType;
1251
- refProjectId = srcProjectId;
1252
- } else if (!refSrcType || !refProjectId) {
1253
- throw Error(`the "refSrcType" and "refProjectId" must be specified when linking a cross-reference field: ${fieldPath.join('.')}`);
1254
- }
1255
-
1256
1231
  // create the new document
1257
1232
  const result = await this.createDocument({
1258
1233
  object: object,
1259
- srcProjectId: refProjectId,
1260
- srcType: refSrcType,
1234
+ srcProjectId: srcProjectId,
1235
+ srcType: srcType,
1261
1236
  modelName: modelName,
1262
1237
  locale: locale,
1263
1238
  user: user
@@ -1265,29 +1240,17 @@ export class ContentStore {
1265
1240
 
1266
1241
  // update the document by linking the field to the created document
1267
1242
  const userContext = getUserContextForSrcType(srcType, user);
1268
- let field: CSITypes.UpdateOperationField;
1269
- if (fieldProps.type === 'reference') {
1270
- field = {
1271
- type: 'reference',
1272
- refType: 'document',
1273
- refId: result.srcDocumentId
1274
- } as CSITypes.UpdateOperationReferenceField;
1275
- } else {
1276
- if (!['string', 'text', 'json'].includes(csiFieldProps.type)) {
1277
- throw new Error(`The 'cross-reference' field can be only applied on string, text and json fields: ${fieldPath.join('.')}`);
1278
- }
1279
- field = updateOperationValueFieldWithCrossReference(csiFieldProps.type as 'string' | 'text' | 'json', {
1280
- refId: result.srcDocumentId,
1281
- refSrcType: refSrcType,
1282
- refProjectId: refProjectId
1283
- });
1284
- }
1243
+ const field = {
1244
+ type: 'reference',
1245
+ refType: 'document',
1246
+ refId: result.srcDocumentId
1247
+ } as const;
1285
1248
  const updatedDocument = await contentSourceData.instance.updateDocument({
1286
1249
  document: csiDocument,
1287
1250
  modelMap: csiModelMap,
1288
1251
  userContext: userContext,
1289
1252
  operations: [
1290
- modelField.type === 'list'
1253
+ csiModelField.type === 'list'
1291
1254
  ? {
1292
1255
  opType: 'insert',
1293
1256
  fieldPath: fieldPath,
@@ -1463,7 +1426,7 @@ export class ContentStore {
1463
1426
  object,
1464
1427
  locale,
1465
1428
  defaultLocaleDocumentId,
1466
- user
1429
+ user,
1467
1430
  }: {
1468
1431
  srcType: string;
1469
1432
  srcProjectId: string;
@@ -1477,17 +1440,22 @@ export class ContentStore {
1477
1440
 
1478
1441
  const contentSourceId = getContentSourceId(srcType, srcProjectId);
1479
1442
  const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
1443
+ const modelMap = contentSourceData.modelMap;
1444
+ const csiModelMap = contentSourceData.csiModelMap;
1445
+ const userContext = getUserContextForSrcType(srcType, user);
1480
1446
  const resolvedLocale = locale ?? contentSourceData.defaultLocaleCode;
1481
1447
 
1482
1448
  const result = await createDocumentRecursively({
1483
1449
  object,
1484
1450
  modelName,
1485
- contentSourceId,
1486
- contentSourceDataById: this.contentSourceDataById,
1451
+ modelMap,
1452
+ csiModelMap,
1487
1453
  createDocument: getCreateDocumentThunk({
1488
1454
  locale: resolvedLocale,
1455
+ csiModelMap,
1456
+ userContext,
1489
1457
  defaultLocaleDocumentId,
1490
- user
1458
+ contentSourceInstance: contentSourceData.instance
1491
1459
  })
1492
1460
  });
1493
1461
 
@@ -1550,11 +1518,11 @@ export class ContentStore {
1550
1518
  csiModelField,
1551
1519
  modelMap,
1552
1520
  csiModelMap,
1553
- contentSourceId,
1554
- contentSourceDataById: this.contentSourceDataById,
1555
1521
  createDocument: getCreateDocumentThunk({
1556
1522
  locale: updateOperation.locale,
1557
- user
1523
+ csiModelMap,
1524
+ userContext,
1525
+ contentSourceInstance: contentSourceData.instance
1558
1526
  })
1559
1527
  });
1560
1528
  return {
@@ -1578,11 +1546,11 @@ export class ContentStore {
1578
1546
  csiModelField: csiModelField.items,
1579
1547
  modelMap,
1580
1548
  csiModelMap,
1581
- contentSourceId,
1582
- contentSourceDataById: this.contentSourceDataById,
1583
1549
  createDocument: getCreateDocumentThunk({
1584
1550
  locale: updateOperation.locale,
1585
- user
1551
+ csiModelMap,
1552
+ userContext,
1553
+ contentSourceInstance: contentSourceData.instance
1586
1554
  })
1587
1555
  });
1588
1556
  return {
@@ -1649,6 +1617,7 @@ export class ContentStore {
1649
1617
  throw new Error(`no model with name '${document.srcModelName}' was found`);
1650
1618
  }
1651
1619
 
1620
+ const userContext = getUserContextForSrcType(srcType, user);
1652
1621
  const resolvedLocale = locale ?? contentSourceData.defaultLocaleCode;
1653
1622
 
1654
1623
  const extendedObject = mergeObjectWithDocument({
@@ -1664,11 +1633,13 @@ export class ContentStore {
1664
1633
  const result = await createDocumentRecursively({
1665
1634
  object: extendedObject,
1666
1635
  modelName: model.name,
1667
- contentSourceId,
1668
- contentSourceDataById: this.contentSourceDataById,
1636
+ modelMap,
1637
+ csiModelMap,
1669
1638
  createDocument: getCreateDocumentThunk({
1670
1639
  locale: resolvedLocale,
1671
- user
1640
+ csiModelMap,
1641
+ userContext,
1642
+ contentSourceInstance: contentSourceData.instance
1672
1643
  })
1673
1644
  });
1674
1645
 
@@ -7,41 +7,41 @@ import { fieldPathToString, mapPromise } from '@stackbit/utils';
7
7
  import * as CSITypes from '@stackbit/types';
8
8
 
9
9
  import * as ContentStoreTypes from '../content-store-types';
10
- import { getContentSourceId, getUserContextForSrcType, updateOperationValueFieldWithCrossReference } from '../content-store-utils';
11
10
 
12
11
  export type CreateDocumentCallback = ({
13
12
  updateOperationFields,
14
- contentSourceData,
15
13
  modelName
16
14
  }: {
17
15
  updateOperationFields: Record<string, CSITypes.UpdateOperationField>;
18
- contentSourceData: ContentStoreTypes.ContentSourceData;
19
16
  modelName: string;
20
17
  }) => Promise<CSITypes.Document>;
21
18
 
22
19
  export function getCreateDocumentThunk({
20
+ csiModelMap,
23
21
  locale,
24
22
  defaultLocaleDocumentId,
25
- user
23
+ userContext,
24
+ contentSourceInstance
26
25
  }: {
26
+ csiModelMap: Record<string, CSIModel>;
27
27
  locale?: string;
28
28
  defaultLocaleDocumentId?: string;
29
- user?: ContentStoreTypes.User;
29
+ userContext: unknown;
30
+ contentSourceInstance: CSITypes.ContentSourceInterface;
30
31
  }): CreateDocumentCallback {
31
- return async ({ updateOperationFields, modelName, contentSourceData }) => {
32
+ return async ({ updateOperationFields, modelName }) => {
32
33
  // When passing model and modelMap to contentSourceInstance, we have to pass
33
34
  // the original models (i.e., csiModel and csiModelMap) that we've received
34
35
  // from that contentSourceInstance. We can't pass internal models as they
35
36
  // might
36
- const csiModel = contentSourceData.csiModelMap[modelName];
37
+ const csiModel = csiModelMap[modelName];
37
38
  if (!csiModel) {
38
39
  throw new Error(`no model with name '${modelName}' was found`);
39
40
  }
40
- const userContext = getUserContextForSrcType(contentSourceData.srcType, user);
41
- return await contentSourceData.instance.createDocument({
41
+ return await contentSourceInstance.createDocument({
42
42
  updateOperationFields: updateOperationFields,
43
43
  model: csiModel,
44
- modelMap: contentSourceData.csiModelMap,
44
+ modelMap: csiModelMap,
45
45
  locale,
46
46
  defaultLocaleDocumentId,
47
47
  userContext
@@ -90,24 +90,18 @@ export function getCreateDocumentThunk({
90
90
  export async function createDocumentRecursively({
91
91
  object,
92
92
  modelName,
93
- contentSourceId,
94
- contentSourceDataById,
93
+ modelMap,
94
+ csiModelMap,
95
95
  createDocument
96
96
  }: {
97
97
  object?: Record<string, any>;
98
98
  modelName: string;
99
- contentSourceId: string;
100
- contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
99
+ modelMap: Record<string, SDKModel>;
100
+ csiModelMap: Record<string, CSIModel>;
101
101
  createDocument: CreateDocumentCallback;
102
102
  }): Promise<{ document: CSITypes.Document; newRefDocuments: CSITypes.Document[] }> {
103
- const contentSourceData = contentSourceDataById[contentSourceId];
104
- if (!contentSourceData) {
105
- throw new Error(`no content source data for id '${contentSourceId}'`);
106
- }
107
- const modelMap = contentSourceData.modelMap;
108
- const csiModelMap = contentSourceData.csiModelMap;
109
103
  const model = modelMap[modelName];
110
- const csiModel = contentSourceData.csiModelMap[modelName];
104
+ const csiModel = csiModelMap[modelName];
111
105
  if (!model || !csiModel) {
112
106
  throw new Error(`no model with name '${modelName}' was found`);
113
107
  }
@@ -127,14 +121,11 @@ export async function createDocumentRecursively({
127
121
  fieldPath: [modelName],
128
122
  modelMap,
129
123
  csiModelMap,
130
- contentSourceId,
131
- contentSourceDataById,
132
124
  createDocument
133
125
  });
134
126
 
135
127
  const document = await createDocument({
136
128
  updateOperationFields: nestedResult.fields,
137
- contentSourceData: contentSourceData,
138
129
  modelName: modelName
139
130
  });
140
131
  return {
@@ -161,8 +152,6 @@ async function createObjectRecursively({
161
152
  fieldPath,
162
153
  modelMap,
163
154
  csiModelMap,
164
- contentSourceId,
165
- contentSourceDataById,
166
155
  createDocument
167
156
  }: {
168
157
  object?: Record<string, any>;
@@ -171,8 +160,6 @@ async function createObjectRecursively({
171
160
  fieldPath: (string | number)[];
172
161
  modelMap: Record<string, SDKModel>;
173
162
  csiModelMap: Record<string, CSIModel>;
174
- contentSourceId: string;
175
- contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
176
163
  createDocument: CreateDocumentCallback;
177
164
  }): Promise<{
178
165
  fields: Record<string, CSITypes.UpdateOperationField>;
@@ -219,8 +206,6 @@ async function createObjectRecursively({
219
206
  fieldPath: fieldPath.concat(fieldName),
220
207
  modelMap,
221
208
  csiModelMap,
222
- contentSourceId,
223
- contentSourceDataById,
224
209
  createDocument
225
210
  });
226
211
  result.fields[fieldName] = fieldResult.field;
@@ -241,8 +226,6 @@ async function createUpdateOperationFieldRecursively({
241
226
  fieldPath,
242
227
  modelMap,
243
228
  csiModelMap,
244
- contentSourceId,
245
- contentSourceDataById,
246
229
  createDocument
247
230
  }: {
248
231
  value: any;
@@ -251,8 +234,6 @@ async function createUpdateOperationFieldRecursively({
251
234
  fieldPath: (string | number)[];
252
235
  modelMap: Record<string, SDKModel>;
253
236
  csiModelMap: Record<string, CSIModel>;
254
- contentSourceId: string;
255
- contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
256
237
  createDocument: CreateDocumentCallback;
257
238
  }): Promise<{ field: CSITypes.UpdateOperationField; newRefDocuments: CSITypes.Document[] }> {
258
239
  if (csiModelField.type === 'object') {
@@ -266,8 +247,6 @@ async function createUpdateOperationFieldRecursively({
266
247
  fieldPath,
267
248
  modelMap,
268
249
  csiModelMap,
269
- contentSourceId,
270
- contentSourceDataById,
271
250
  createDocument
272
251
  });
273
252
  return {
@@ -305,8 +284,6 @@ async function createUpdateOperationFieldRecursively({
305
284
  fieldPath,
306
285
  modelMap,
307
286
  csiModelMap,
308
- contentSourceId,
309
- contentSourceDataById,
310
287
  createDocument
311
288
  });
312
289
  return {
@@ -322,7 +299,7 @@ async function createUpdateOperationFieldRecursively({
322
299
  // TODO: if modelField.source is cloudinary, the new document field
323
300
  // should be of the 'image' type with 'title' and 'url' properties
324
301
  if (_.isPlainObject(value)) {
325
- refId = value.$$ref;
302
+ refId = value.$$ref ?? value.url;
326
303
  } else {
327
304
  refId = value;
328
305
  }
@@ -363,14 +340,11 @@ async function createUpdateOperationFieldRecursively({
363
340
  modelName = modelNames[0];
364
341
  }
365
342
  }
366
- if (!modelName) {
367
- throw new Error('reference field type must have $$type or $$ref properties when creating new documents');
368
- }
369
343
  const { document, newRefDocuments } = await createDocumentRecursively({
370
344
  object: rest,
371
345
  modelName,
372
- contentSourceId,
373
- contentSourceDataById,
346
+ modelMap,
347
+ csiModelMap,
374
348
  createDocument
375
349
  });
376
350
  return {
@@ -382,39 +356,6 @@ async function createUpdateOperationFieldRecursively({
382
356
  newRefDocuments: [document, ...newRefDocuments]
383
357
  };
384
358
  }
385
- } else if (['string', 'text', 'json'].includes(csiModelField.type) && modelField.type === 'cross-reference') {
386
- const fieldType = csiModelField.type as 'string' | 'text' | 'json';
387
- let { $$ref: refId = null, $$type: modelName = null, $$refSrcType: refSrcType = null, $$refProjectId: refProjectId = null, ...rest } = value;
388
- let refObject;
389
- const newRefDocuments: CSITypes.Document[] = [];
390
- if (refId && refSrcType && refProjectId) {
391
- refObject = { refId, refSrcType, refProjectId };
392
- } else {
393
- if (!modelName || !refSrcType || !refProjectId) {
394
- const models = modelField.models;
395
- if (models && models.length === 1) {
396
- modelName = models[0]!.modelName;
397
- refSrcType = models[0]!.srcType;
398
- refProjectId = models[0]!.srcProjectId;
399
- }
400
- }
401
- if (!modelName || !refSrcType || !refProjectId) {
402
- throw new Error('reference field type must have $$type or $$ref properties when creating new documents');
403
- }
404
- const { document, newRefDocuments } = await createDocumentRecursively({
405
- object: rest,
406
- modelName,
407
- contentSourceId: getContentSourceId(refSrcType, refProjectId),
408
- contentSourceDataById,
409
- createDocument
410
- });
411
- newRefDocuments.push(document, ...newRefDocuments);
412
- refObject = { refId: document.id, refSrcType, refProjectId };
413
- }
414
- return {
415
- field: updateOperationValueFieldWithCrossReference(fieldType, refObject),
416
- newRefDocuments
417
- };
418
359
  } else if (csiModelField.type === 'list') {
419
360
  if (modelField.type !== 'list') {
420
361
  throw new Error(`field type mismatch between external and internal models at field path ${fieldPathToString(fieldPath)}`);
@@ -437,8 +378,6 @@ async function createUpdateOperationFieldRecursively({
437
378
  fieldPath: fieldPath.concat(index),
438
379
  modelMap,
439
380
  csiModelMap,
440
- contentSourceId,
441
- contentSourceDataById,
442
381
  createDocument
443
382
  });
444
383
  if (result.field.type === 'list') {
@@ -486,8 +425,6 @@ export async function convertOperationField({
486
425
  csiModelField,
487
426
  modelMap,
488
427
  csiModelMap,
489
- contentSourceId,
490
- contentSourceDataById,
491
428
  createDocument
492
429
  }: {
493
430
  operationField: ContentStoreTypes.UpdateOperationField;
@@ -496,8 +433,6 @@ export async function convertOperationField({
496
433
  csiModelField: FieldSpecificProps;
497
434
  modelMap: Record<string, SDKModel>;
498
435
  csiModelMap: Record<string, SDKModel>;
499
- contentSourceId: string;
500
- contentSourceDataById: Record<string, ContentStoreTypes.ContentSourceData>;
501
436
  createDocument: CreateDocumentCallback;
502
437
  }): Promise<CSITypes.UpdateOperationField> {
503
438
  switch (operationField.type) {
@@ -512,8 +447,6 @@ export async function convertOperationField({
512
447
  fieldPath,
513
448
  modelMap,
514
449
  csiModelMap,
515
- contentSourceId,
516
- contentSourceDataById,
517
450
  createDocument
518
451
  });
519
452
  return {
@@ -534,8 +467,6 @@ export async function convertOperationField({
534
467
  fieldPath,
535
468
  modelMap,
536
469
  csiModelMap,
537
- contentSourceId,
538
- contentSourceDataById,
539
470
  createDocument
540
471
  });
541
472
  return {
@@ -545,23 +476,7 @@ export async function convertOperationField({
545
476
  };
546
477
  }
547
478
  case 'reference':
548
- // ContentStore and CSI 'reference' operation field have the same format
549
479
  return operationField;
550
- case 'cross-reference':
551
- if (csiModelField.type !== 'string' && csiModelField.type !== 'text' && csiModelField.type !== 'json') {
552
- throw new Error(
553
- `update operation with with 'cross-reference' field can be performed on 'string', 'text' and 'json' content-source field types only, got '${csiModelField.type}'`
554
- );
555
- }
556
- const refObject = {
557
- refId: operationField.refId,
558
- refSrcType: operationField.refSrcType,
559
- refProjectId: operationField.refProjectId
560
- };
561
- return {
562
- type: csiModelField.type,
563
- value: csiModelField.type === 'json' ? refObject : JSON.stringify(refObject)
564
- };
565
480
  case 'list': {
566
481
  if (modelField.type !== 'list' || csiModelField.type !== 'list') {
567
482
  throw new Error(`the operation field type '${operationField.type}' does not match the model field type '${modelField.type}'`);
@@ -576,8 +491,6 @@ export async function convertOperationField({
576
491
  fieldPath: fieldPath.concat(index),
577
492
  modelMap,
578
493
  csiModelMap,
579
- contentSourceId,
580
- contentSourceDataById,
581
494
  createDocument
582
495
  });
583
496
  if (result.field.type === 'list') {
@@ -593,9 +506,7 @@ export async function convertOperationField({
593
506
  }
594
507
  case 'enum':
595
508
  if (csiModelField.type !== 'enum' && csiModelField.type !== 'string') {
596
- throw new Error(
597
- `update operation with 'enum' field can be performed on 'string' and 'enum' content-source field types only, got '${csiModelField.type}'`
598
- );
509
+ throw new Error(`the operation field type 'enum' can be performed on 'string' and 'enum' content-source field types '${csiModelField.type}'`);
599
510
  }
600
511
  // When inserting new enum value into a list, the client does not
601
512
  // send value. Set first option as the value.
@@ -632,8 +543,6 @@ export async function convertOperationField({
632
543
  fieldPath,
633
544
  modelMap,
634
545
  csiModelMap,
635
- contentSourceId,
636
- contentSourceDataById,
637
546
  createDocument
638
547
  });
639
548
  return result.field;