@payloadcms/db-mongodb 3.0.0-canary.98e0119 → 3.0.0-canary.99cbde3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (49) hide show
  1. package/dist/index.d.ts +10 -1
  2. package/dist/index.d.ts.map +1 -1
  3. package/dist/index.js.map +1 -1
  4. package/dist/init.js +4 -4
  5. package/dist/init.js.map +1 -1
  6. package/dist/models/buildCollectionSchema.d.ts +2 -2
  7. package/dist/models/buildCollectionSchema.d.ts.map +1 -1
  8. package/dist/models/buildCollectionSchema.js +4 -4
  9. package/dist/models/buildCollectionSchema.js.map +1 -1
  10. package/dist/models/buildGlobalModel.d.ts +2 -2
  11. package/dist/models/buildGlobalModel.d.ts.map +1 -1
  12. package/dist/models/buildGlobalModel.js +4 -4
  13. package/dist/models/buildGlobalModel.js.map +1 -1
  14. package/dist/models/buildSchema.d.ts +2 -2
  15. package/dist/models/buildSchema.d.ts.map +1 -1
  16. package/dist/models/buildSchema.js +82 -61
  17. package/dist/models/buildSchema.js.map +1 -1
  18. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
  19. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +12 -7
  20. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
  21. package/dist/predefinedMigrations/relationships-v2-v3.js +1 -1
  22. package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -1
  23. package/dist/predefinedMigrations/versions-v1-v2.js +1 -1
  24. package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
  25. package/dist/queries/buildSearchParams.js +1 -0
  26. package/dist/queries/buildSearchParams.js.map +1 -1
  27. package/dist/queries/sanitizeQueryValue.d.ts +3 -2
  28. package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
  29. package/dist/queries/sanitizeQueryValue.js +145 -36
  30. package/dist/queries/sanitizeQueryValue.js.map +1 -1
  31. package/dist/updateGlobal.d.ts.map +1 -1
  32. package/dist/updateGlobal.js +2 -1
  33. package/dist/updateGlobal.js.map +1 -1
  34. package/dist/updateGlobalVersion.d.ts +1 -1
  35. package/dist/updateGlobalVersion.d.ts.map +1 -1
  36. package/dist/updateGlobalVersion.js +2 -1
  37. package/dist/updateGlobalVersion.js.map +1 -1
  38. package/dist/updateVersion.d.ts.map +1 -1
  39. package/dist/updateVersion.js +2 -1
  40. package/dist/updateVersion.js.map +1 -1
  41. package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
  42. package/dist/utilities/buildProjectionFromSelect.js.map +1 -1
  43. package/dist/utilities/sanitizeRelationshipIDs.js +1 -0
  44. package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -1
  45. package/dist/utilities/sanitizeRelationshipIDs.spec.js +408 -0
  46. package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -0
  47. package/package.json +3 -4
  48. package/dist/queries/mock.js +0 -2
  49. package/dist/queries/mock.js.map +0 -1
@@ -34,7 +34,7 @@ const localizeSchema = (entity, schema, localization)=>{
34
34
  }
35
35
  return schema;
36
36
  };
37
- export const buildSchema = (config, configFields, buildSchemaOptions = {})=>{
37
+ export const buildSchema = (payload, configFields, buildSchemaOptions = {})=>{
38
38
  const { allowIDField, options } = buildSchemaOptions;
39
39
  let fields = {};
40
40
  let schemaFields = configFields;
@@ -55,18 +55,18 @@ export const buildSchema = (config, configFields, buildSchemaOptions = {})=>{
55
55
  if (!fieldIsPresentationalOnly(field)) {
56
56
  const addFieldSchema = fieldToSchemaMap[field.type];
57
57
  if (addFieldSchema) {
58
- addFieldSchema(field, schema, config, buildSchemaOptions);
58
+ addFieldSchema(field, schema, payload, buildSchemaOptions);
59
59
  }
60
60
  }
61
61
  });
62
62
  return schema;
63
63
  };
64
64
  const fieldToSchemaMap = {
65
- array: (field, schema, config, buildSchemaOptions)=>{
65
+ array: (field, schema, payload, buildSchemaOptions)=>{
66
66
  const baseSchema = {
67
67
  ...formatBaseSchema(field, buildSchemaOptions),
68
68
  type: [
69
- buildSchema(config, field.fields, {
69
+ buildSchema(payload, field.fields, {
70
70
  allowIDField: true,
71
71
  disableUnique: buildSchemaOptions.disableUnique,
72
72
  draftsEnabled: buildSchemaOptions.draftsEnabled,
@@ -79,10 +79,10 @@ const fieldToSchemaMap = {
79
79
  ]
80
80
  };
81
81
  schema.add({
82
- [field.name]: localizeSchema(field, baseSchema, config.localization)
82
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
83
83
  });
84
84
  },
85
- blocks: (field, schema, config, buildSchemaOptions)=>{
85
+ blocks: (field, schema, payload, buildSchemaOptions)=>{
86
86
  const fieldSchema = {
87
87
  type: [
88
88
  new mongoose.Schema({}, {
@@ -92,7 +92,7 @@ const fieldToSchemaMap = {
92
92
  ]
93
93
  };
94
94
  schema.add({
95
- [field.name]: localizeSchema(field, fieldSchema, config.localization)
95
+ [field.name]: localizeSchema(field, fieldSchema, payload.config.localization)
96
96
  });
97
97
  field.blocks.forEach((blockItem)=>{
98
98
  const blockSchema = new mongoose.Schema({}, {
@@ -102,11 +102,11 @@ const fieldToSchemaMap = {
102
102
  blockItem.fields.forEach((blockField)=>{
103
103
  const addFieldSchema = fieldToSchemaMap[blockField.type];
104
104
  if (addFieldSchema) {
105
- addFieldSchema(blockField, blockSchema, config, buildSchemaOptions);
105
+ addFieldSchema(blockField, blockSchema, payload, buildSchemaOptions);
106
106
  }
107
107
  });
108
- if (field.localized && config.localization) {
109
- config.localization.localeCodes.forEach((localeCode)=>{
108
+ if (field.localized && payload.config.localization) {
109
+ payload.config.localization.localeCodes.forEach((localeCode)=>{
110
110
  // @ts-expect-error Possible incorrect typing in mongoose types, this works
111
111
  schema.path(`${field.name}.${localeCode}`).discriminator(blockItem.slug, blockSchema);
112
112
  });
@@ -116,60 +116,60 @@ const fieldToSchemaMap = {
116
116
  }
117
117
  });
118
118
  },
119
- checkbox: (field, schema, config, buildSchemaOptions)=>{
119
+ checkbox: (field, schema, payload, buildSchemaOptions)=>{
120
120
  const baseSchema = {
121
121
  ...formatBaseSchema(field, buildSchemaOptions),
122
122
  type: Boolean
123
123
  };
124
124
  schema.add({
125
- [field.name]: localizeSchema(field, baseSchema, config.localization)
125
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
126
126
  });
127
127
  },
128
- code: (field, schema, config, buildSchemaOptions)=>{
128
+ code: (field, schema, payload, buildSchemaOptions)=>{
129
129
  const baseSchema = {
130
130
  ...formatBaseSchema(field, buildSchemaOptions),
131
131
  type: String
132
132
  };
133
133
  schema.add({
134
- [field.name]: localizeSchema(field, baseSchema, config.localization)
134
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
135
135
  });
136
136
  },
137
- collapsible: (field, schema, config, buildSchemaOptions)=>{
137
+ collapsible: (field, schema, payload, buildSchemaOptions)=>{
138
138
  field.fields.forEach((subField)=>{
139
139
  if (fieldIsVirtual(subField)) {
140
140
  return;
141
141
  }
142
142
  const addFieldSchema = fieldToSchemaMap[subField.type];
143
143
  if (addFieldSchema) {
144
- addFieldSchema(subField, schema, config, buildSchemaOptions);
144
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
145
145
  }
146
146
  });
147
147
  },
148
- date: (field, schema, config, buildSchemaOptions)=>{
148
+ date: (field, schema, payload, buildSchemaOptions)=>{
149
149
  const baseSchema = {
150
150
  ...formatBaseSchema(field, buildSchemaOptions),
151
151
  type: Date
152
152
  };
153
153
  schema.add({
154
- [field.name]: localizeSchema(field, baseSchema, config.localization)
154
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
155
155
  });
156
156
  },
157
- email: (field, schema, config, buildSchemaOptions)=>{
157
+ email: (field, schema, payload, buildSchemaOptions)=>{
158
158
  const baseSchema = {
159
159
  ...formatBaseSchema(field, buildSchemaOptions),
160
160
  type: String
161
161
  };
162
162
  schema.add({
163
- [field.name]: localizeSchema(field, baseSchema, config.localization)
163
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
164
164
  });
165
165
  },
166
- group: (field, schema, config, buildSchemaOptions)=>{
166
+ group: (field, schema, payload, buildSchemaOptions)=>{
167
167
  const formattedBaseSchema = formatBaseSchema(field, buildSchemaOptions);
168
168
  // carry indexSortableFields through to versions if drafts enabled
169
169
  const indexSortableFields = buildSchemaOptions.indexSortableFields && field.name === 'version' && buildSchemaOptions.draftsEnabled;
170
170
  const baseSchema = {
171
171
  ...formattedBaseSchema,
172
- type: buildSchema(config, field.fields, {
172
+ type: buildSchema(payload, field.fields, {
173
173
  disableUnique: buildSchemaOptions.disableUnique,
174
174
  draftsEnabled: buildSchemaOptions.draftsEnabled,
175
175
  indexSortableFields,
@@ -181,19 +181,19 @@ const fieldToSchemaMap = {
181
181
  })
182
182
  };
183
183
  schema.add({
184
- [field.name]: localizeSchema(field, baseSchema, config.localization)
184
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
185
185
  });
186
186
  },
187
- json: (field, schema, config, buildSchemaOptions)=>{
187
+ json: (field, schema, payload, buildSchemaOptions)=>{
188
188
  const baseSchema = {
189
189
  ...formatBaseSchema(field, buildSchemaOptions),
190
190
  type: mongoose.Schema.Types.Mixed
191
191
  };
192
192
  schema.add({
193
- [field.name]: localizeSchema(field, baseSchema, config.localization)
193
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
194
194
  });
195
195
  },
196
- number: (field, schema, config, buildSchemaOptions)=>{
196
+ number: (field, schema, payload, buildSchemaOptions)=>{
197
197
  const baseSchema = {
198
198
  ...formatBaseSchema(field, buildSchemaOptions),
199
199
  type: field.hasMany ? [
@@ -201,10 +201,10 @@ const fieldToSchemaMap = {
201
201
  ] : Number
202
202
  };
203
203
  schema.add({
204
- [field.name]: localizeSchema(field, baseSchema, config.localization)
204
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
205
205
  });
206
206
  },
207
- point: (field, schema, config, buildSchemaOptions)=>{
207
+ point: (field, schema, payload, buildSchemaOptions)=>{
208
208
  const baseSchema = {
209
209
  type: {
210
210
  type: String,
@@ -227,7 +227,7 @@ const fieldToSchemaMap = {
227
227
  baseSchema.coordinates.sparse = true;
228
228
  }
229
229
  schema.add({
230
- [field.name]: localizeSchema(field, baseSchema, config.localization)
230
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
231
231
  });
232
232
  if (field.index === true || field.index === undefined) {
233
233
  const indexOptions = {};
@@ -235,8 +235,8 @@ const fieldToSchemaMap = {
235
235
  indexOptions.sparse = true;
236
236
  indexOptions.unique = true;
237
237
  }
238
- if (field.localized && config.localization) {
239
- config.localization.locales.forEach((locale)=>{
238
+ if (field.localized && payload.config.localization) {
239
+ payload.config.localization.locales.forEach((locale)=>{
240
240
  schema.index({
241
241
  [`${field.name}.${locale.code}`]: '2dsphere'
242
242
  }, indexOptions);
@@ -248,7 +248,7 @@ const fieldToSchemaMap = {
248
248
  }
249
249
  }
250
250
  },
251
- radio: (field, schema, config, buildSchemaOptions)=>{
251
+ radio: (field, schema, payload, buildSchemaOptions)=>{
252
252
  const baseSchema = {
253
253
  ...formatBaseSchema(field, buildSchemaOptions),
254
254
  type: String,
@@ -260,15 +260,16 @@ const fieldToSchemaMap = {
260
260
  })
261
261
  };
262
262
  schema.add({
263
- [field.name]: localizeSchema(field, baseSchema, config.localization)
263
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
264
264
  });
265
265
  },
266
- relationship: (field, schema, config, buildSchemaOptions)=>{
266
+ relationship: (field, schema, payload, buildSchemaOptions)=>{
267
267
  const hasManyRelations = Array.isArray(field.relationTo);
268
268
  let schemaToReturn = {};
269
- if (field.localized && config.localization) {
269
+ const valueType = getRelationshipValueType(field, payload);
270
+ if (field.localized && payload.config.localization) {
270
271
  schemaToReturn = {
271
- type: config.localization.localeCodes.reduce((locales, locale)=>{
272
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
272
273
  let localeSchema = {};
273
274
  if (hasManyRelations) {
274
275
  localeSchema = {
@@ -280,14 +281,14 @@ const fieldToSchemaMap = {
280
281
  enum: field.relationTo
281
282
  },
282
283
  value: {
283
- type: mongoose.Schema.Types.Mixed,
284
+ type: valueType,
284
285
  refPath: `${field.name}.${locale}.relationTo`
285
286
  }
286
287
  };
287
288
  } else {
288
289
  localeSchema = {
289
290
  ...formatBaseSchema(field, buildSchemaOptions),
290
- type: mongoose.Schema.Types.Mixed,
291
+ type: valueType,
291
292
  ref: field.relationTo
292
293
  };
293
294
  }
@@ -313,7 +314,7 @@ const fieldToSchemaMap = {
313
314
  enum: field.relationTo
314
315
  },
315
316
  value: {
316
- type: mongoose.Schema.Types.Mixed,
317
+ type: valueType,
317
318
  refPath: `${field.name}.relationTo`
318
319
  }
319
320
  };
@@ -328,7 +329,7 @@ const fieldToSchemaMap = {
328
329
  } else {
329
330
  schemaToReturn = {
330
331
  ...formatBaseSchema(field, buildSchemaOptions),
331
- type: mongoose.Schema.Types.Mixed,
332
+ type: valueType,
332
333
  ref: field.relationTo
333
334
  };
334
335
  if (field.hasMany) {
@@ -344,27 +345,27 @@ const fieldToSchemaMap = {
344
345
  [field.name]: schemaToReturn
345
346
  });
346
347
  },
347
- richText: (field, schema, config, buildSchemaOptions)=>{
348
+ richText: (field, schema, payload, buildSchemaOptions)=>{
348
349
  const baseSchema = {
349
350
  ...formatBaseSchema(field, buildSchemaOptions),
350
351
  type: mongoose.Schema.Types.Mixed
351
352
  };
352
353
  schema.add({
353
- [field.name]: localizeSchema(field, baseSchema, config.localization)
354
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
354
355
  });
355
356
  },
356
- row: (field, schema, config, buildSchemaOptions)=>{
357
+ row: (field, schema, payload, buildSchemaOptions)=>{
357
358
  field.fields.forEach((subField)=>{
358
359
  if (fieldIsVirtual(subField)) {
359
360
  return;
360
361
  }
361
362
  const addFieldSchema = fieldToSchemaMap[subField.type];
362
363
  if (addFieldSchema) {
363
- addFieldSchema(subField, schema, config, buildSchemaOptions);
364
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
364
365
  }
365
366
  });
366
367
  },
367
- select: (field, schema, config, buildSchemaOptions)=>{
368
+ select: (field, schema, payload, buildSchemaOptions)=>{
368
369
  const baseSchema = {
369
370
  ...formatBaseSchema(field, buildSchemaOptions),
370
371
  type: String,
@@ -381,17 +382,17 @@ const fieldToSchemaMap = {
381
382
  schema.add({
382
383
  [field.name]: localizeSchema(field, field.hasMany ? [
383
384
  baseSchema
384
- ] : baseSchema, config.localization)
385
+ ] : baseSchema, payload.config.localization)
385
386
  });
386
387
  },
387
- tabs: (field, schema, config, buildSchemaOptions)=>{
388
+ tabs: (field, schema, payload, buildSchemaOptions)=>{
388
389
  field.tabs.forEach((tab)=>{
389
390
  if (tabHasName(tab)) {
390
391
  if (fieldIsVirtual(tab)) {
391
392
  return;
392
393
  }
393
394
  const baseSchema = {
394
- type: buildSchema(config, tab.fields, {
395
+ type: buildSchema(payload, tab.fields, {
395
396
  disableUnique: buildSchemaOptions.disableUnique,
396
397
  draftsEnabled: buildSchemaOptions.draftsEnabled,
397
398
  options: {
@@ -402,7 +403,7 @@ const fieldToSchemaMap = {
402
403
  })
403
404
  };
404
405
  schema.add({
405
- [tab.name]: localizeSchema(tab, baseSchema, config.localization)
406
+ [tab.name]: localizeSchema(tab, baseSchema, payload.config.localization)
406
407
  });
407
408
  } else {
408
409
  tab.fields.forEach((subField)=>{
@@ -411,13 +412,13 @@ const fieldToSchemaMap = {
411
412
  }
412
413
  const addFieldSchema = fieldToSchemaMap[subField.type];
413
414
  if (addFieldSchema) {
414
- addFieldSchema(subField, schema, config, buildSchemaOptions);
415
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
415
416
  }
416
417
  });
417
418
  }
418
419
  });
419
420
  },
420
- text: (field, schema, config, buildSchemaOptions)=>{
421
+ text: (field, schema, payload, buildSchemaOptions)=>{
421
422
  const baseSchema = {
422
423
  ...formatBaseSchema(field, buildSchemaOptions),
423
424
  type: field.hasMany ? [
@@ -425,24 +426,25 @@ const fieldToSchemaMap = {
425
426
  ] : String
426
427
  };
427
428
  schema.add({
428
- [field.name]: localizeSchema(field, baseSchema, config.localization)
429
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
429
430
  });
430
431
  },
431
- textarea: (field, schema, config, buildSchemaOptions)=>{
432
+ textarea: (field, schema, payload, buildSchemaOptions)=>{
432
433
  const baseSchema = {
433
434
  ...formatBaseSchema(field, buildSchemaOptions),
434
435
  type: String
435
436
  };
436
437
  schema.add({
437
- [field.name]: localizeSchema(field, baseSchema, config.localization)
438
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
438
439
  });
439
440
  },
440
- upload: (field, schema, config, buildSchemaOptions)=>{
441
+ upload: (field, schema, payload, buildSchemaOptions)=>{
441
442
  const hasManyRelations = Array.isArray(field.relationTo);
442
443
  let schemaToReturn = {};
443
- if (field.localized && config.localization) {
444
+ const valueType = getRelationshipValueType(field, payload);
445
+ if (field.localized && payload.config.localization) {
444
446
  schemaToReturn = {
445
- type: config.localization.localeCodes.reduce((locales, locale)=>{
447
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
446
448
  let localeSchema = {};
447
449
  if (hasManyRelations) {
448
450
  localeSchema = {
@@ -454,14 +456,14 @@ const fieldToSchemaMap = {
454
456
  enum: field.relationTo
455
457
  },
456
458
  value: {
457
- type: mongoose.Schema.Types.Mixed,
459
+ type: valueType,
458
460
  refPath: `${field.name}.${locale}.relationTo`
459
461
  }
460
462
  };
461
463
  } else {
462
464
  localeSchema = {
463
465
  ...formatBaseSchema(field, buildSchemaOptions),
464
- type: mongoose.Schema.Types.Mixed,
466
+ type: valueType,
465
467
  ref: field.relationTo
466
468
  };
467
469
  }
@@ -487,7 +489,7 @@ const fieldToSchemaMap = {
487
489
  enum: field.relationTo
488
490
  },
489
491
  value: {
490
- type: mongoose.Schema.Types.Mixed,
492
+ type: valueType,
491
493
  refPath: `${field.name}.relationTo`
492
494
  }
493
495
  };
@@ -502,7 +504,7 @@ const fieldToSchemaMap = {
502
504
  } else {
503
505
  schemaToReturn = {
504
506
  ...formatBaseSchema(field, buildSchemaOptions),
505
- type: mongoose.Schema.Types.Mixed,
507
+ type: valueType,
506
508
  ref: field.relationTo
507
509
  };
508
510
  if (field.hasMany) {
@@ -519,5 +521,24 @@ const fieldToSchemaMap = {
519
521
  });
520
522
  }
521
523
  };
524
+ const getRelationshipValueType = (field, payload)=>{
525
+ if (typeof field.relationTo === 'string') {
526
+ const { customIDType } = payload.collections[field.relationTo];
527
+ if (!customIDType) {
528
+ return mongoose.Schema.Types.ObjectId;
529
+ }
530
+ if (customIDType === 'number') {
531
+ return mongoose.Schema.Types.Number;
532
+ }
533
+ return mongoose.Schema.Types.String;
534
+ }
535
+ // has custom id relationTo
536
+ if (field.relationTo.some((relationTo)=>{
537
+ return !!payload.collections[relationTo].customIDType;
538
+ })) {
539
+ return mongoose.Schema.Types.Mixed;
540
+ }
541
+ return mongoose.Schema.Types.ObjectId;
542
+ };
522
543
 
523
544
  //# sourceMappingURL=buildSchema.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/models/buildSchema.ts"],"sourcesContent":["import type { IndexOptions, Schema, SchemaOptions, SchemaTypeOptions } from 'mongoose'\nimport type {\n ArrayField,\n Block,\n BlocksField,\n CheckboxField,\n CodeField,\n CollapsibleField,\n DateField,\n EmailField,\n Field,\n FieldAffectingData,\n GroupField,\n JSONField,\n NonPresentationalField,\n NumberField,\n PointField,\n RadioField,\n RelationshipField,\n RichTextField,\n RowField,\n SanitizedConfig,\n SanitizedLocalizationConfig,\n SelectField,\n Tab,\n TabsField,\n TextareaField,\n TextField,\n UploadField,\n} from 'payload'\n\nimport mongoose from 'mongoose'\nimport {\n fieldAffectsData,\n fieldIsLocalized,\n fieldIsPresentationalOnly,\n fieldIsVirtual,\n tabHasName,\n} from 'payload/shared'\n\nexport type BuildSchemaOptions = {\n allowIDField?: boolean\n disableUnique?: boolean\n draftsEnabled?: boolean\n indexSortableFields?: boolean\n options?: SchemaOptions\n}\n\ntype FieldSchemaGenerator = (\n field: Field,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n) => void\n\n/**\n * get a field's defaultValue only if defined and not dynamic so that it can be set on the field schema\n * @param field\n */\nconst formatDefaultValue = (field: FieldAffectingData) =>\n typeof field.defaultValue !== 'undefined' && typeof field.defaultValue !== 'function'\n ? field.defaultValue\n : undefined\n\nconst formatBaseSchema = (field: FieldAffectingData, buildSchemaOptions: BuildSchemaOptions) => {\n const { disableUnique, draftsEnabled, indexSortableFields } = buildSchemaOptions\n const schema: SchemaTypeOptions<unknown> = {\n default: formatDefaultValue(field),\n index: field.index || (!disableUnique && field.unique) || indexSortableFields || false,\n required: false,\n unique: (!disableUnique && field.unique) || false,\n }\n\n if (\n schema.unique &&\n (field.localized ||\n draftsEnabled ||\n (fieldAffectsData(field) &&\n field.type !== 'group' &&\n field.type !== 'tab' &&\n field.required !== true))\n ) {\n schema.sparse = true\n }\n\n if (field.hidden) {\n schema.hidden = true\n }\n\n return schema\n}\n\nconst localizeSchema = (\n entity: NonPresentationalField | Tab,\n schema,\n localization: false | SanitizedLocalizationConfig,\n) => {\n if (fieldIsLocalized(entity) && localization && Array.isArray(localization.locales)) {\n return {\n type: localization.localeCodes.reduce(\n (localeSchema, locale) => ({\n ...localeSchema,\n [locale]: schema,\n }),\n {\n _id: false,\n },\n ),\n localized: true,\n }\n }\n return schema\n}\n\nexport const buildSchema = (\n config: SanitizedConfig,\n configFields: Field[],\n buildSchemaOptions: BuildSchemaOptions = {},\n): Schema => {\n const { allowIDField, options } = buildSchemaOptions\n let fields = {}\n\n let schemaFields = configFields\n\n if (!allowIDField) {\n const idField = schemaFields.find((field) => fieldAffectsData(field) && field.name === 'id')\n if (idField) {\n fields = {\n _id: idField.type === 'number' ? Number : String,\n }\n schemaFields = schemaFields.filter(\n (field) => !(fieldAffectsData(field) && field.name === 'id'),\n )\n }\n }\n\n const schema = new mongoose.Schema(fields, options)\n\n schemaFields.forEach((field) => {\n if (fieldIsVirtual(field)) {\n return\n }\n\n if (!fieldIsPresentationalOnly(field)) {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[field.type]\n\n if (addFieldSchema) {\n addFieldSchema(field, schema, config, buildSchemaOptions)\n }\n }\n })\n\n return schema\n}\n\nconst fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {\n array: (\n field: ArrayField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ) => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: [\n buildSchema(config, field.fields, {\n allowIDField: true,\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n ],\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n blocks: (\n field: BlocksField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const fieldSchema = {\n type: [new mongoose.Schema({}, { _id: false, discriminatorKey: 'blockType' })],\n }\n\n schema.add({\n [field.name]: localizeSchema(field, fieldSchema, config.localization),\n })\n\n field.blocks.forEach((blockItem: Block) => {\n const blockSchema = new mongoose.Schema({}, { _id: false, id: false })\n\n blockItem.fields.forEach((blockField) => {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[blockField.type]\n if (addFieldSchema) {\n addFieldSchema(blockField, blockSchema, config, buildSchemaOptions)\n }\n })\n\n if (field.localized && config.localization) {\n config.localization.localeCodes.forEach((localeCode) => {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(`${field.name}.${localeCode}`).discriminator(blockItem.slug, blockSchema)\n })\n } else {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(field.name).discriminator(blockItem.slug, blockSchema)\n }\n })\n },\n checkbox: (\n field: CheckboxField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Boolean }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n code: (\n field: CodeField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n collapsible: (\n field: CollapsibleField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, config, buildSchemaOptions)\n }\n })\n },\n date: (\n field: DateField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Date }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n email: (\n field: EmailField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n group: (\n field: GroupField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const formattedBaseSchema = formatBaseSchema(field, buildSchemaOptions)\n\n // carry indexSortableFields through to versions if drafts enabled\n const indexSortableFields =\n buildSchemaOptions.indexSortableFields &&\n field.name === 'version' &&\n buildSchemaOptions.draftsEnabled\n\n const baseSchema = {\n ...formattedBaseSchema,\n type: buildSchema(config, field.fields, {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n indexSortableFields,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n json: (\n field: JSONField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n number: (\n field: NumberField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: field.hasMany ? [Number] : Number,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n point: (\n field: PointField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema: SchemaTypeOptions<unknown> = {\n type: {\n type: String,\n enum: ['Point'],\n ...(typeof field.defaultValue !== 'undefined' && {\n default: 'Point',\n }),\n },\n coordinates: {\n type: [Number],\n default: formatDefaultValue(field),\n required: false,\n },\n }\n if (buildSchemaOptions.disableUnique && field.unique && field.localized) {\n baseSchema.coordinates.sparse = true\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n\n if (field.index === true || field.index === undefined) {\n const indexOptions: IndexOptions = {}\n if (!buildSchemaOptions.disableUnique && field.unique) {\n indexOptions.sparse = true\n indexOptions.unique = true\n }\n if (field.localized && config.localization) {\n config.localization.locales.forEach((locale) => {\n schema.index({ [`${field.name}.${locale.code}`]: '2dsphere' }, indexOptions)\n })\n } else {\n schema.index({ [field.name]: '2dsphere' }, indexOptions)\n }\n }\n },\n radio: (\n field: RadioField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n relationship: (\n field: RelationshipField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ) => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n if (field.localized && config.localization) {\n schemaToReturn = {\n type: config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: mongoose.Schema.Types.Mixed,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: mongoose.Schema.Types.Mixed,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n richText: (\n field: RichTextField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n row: (\n field: RowField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, config, buildSchemaOptions)\n }\n })\n },\n select: (\n field: SelectField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n if (buildSchemaOptions.draftsEnabled || !field.required) {\n baseSchema.enum.push(null)\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n field.hasMany ? [baseSchema] : baseSchema,\n config.localization,\n ),\n })\n },\n tabs: (\n field: TabsField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.tabs.forEach((tab) => {\n if (tabHasName(tab)) {\n if (fieldIsVirtual(tab)) {\n return\n }\n const baseSchema = {\n type: buildSchema(config, tab.fields, {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n }\n\n schema.add({\n [tab.name]: localizeSchema(tab, baseSchema, config.localization),\n })\n } else {\n tab.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, config, buildSchemaOptions)\n }\n })\n }\n })\n },\n text: (\n field: TextField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: field.hasMany ? [String] : String,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n textarea: (\n field: TextareaField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, config.localization),\n })\n },\n upload: (\n field: UploadField,\n schema: Schema,\n config: SanitizedConfig,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n if (field.localized && config.localization) {\n schemaToReturn = {\n type: config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: mongoose.Schema.Types.Mixed,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: mongoose.Schema.Types.Mixed,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n}\n"],"names":["mongoose","fieldAffectsData","fieldIsLocalized","fieldIsPresentationalOnly","fieldIsVirtual","tabHasName","formatDefaultValue","field","defaultValue","undefined","formatBaseSchema","buildSchemaOptions","disableUnique","draftsEnabled","indexSortableFields","schema","default","index","unique","required","localized","type","sparse","hidden","localizeSchema","entity","localization","Array","isArray","locales","localeCodes","reduce","localeSchema","locale","_id","buildSchema","config","configFields","allowIDField","options","fields","schemaFields","idField","find","name","Number","String","filter","Schema","forEach","addFieldSchema","fieldToSchemaMap","array","baseSchema","id","minimize","add","blocks","fieldSchema","discriminatorKey","blockItem","blockSchema","blockField","localeCode","path","discriminator","slug","checkbox","Boolean","code","collapsible","subField","date","Date","email","group","formattedBaseSchema","json","Types","Mixed","number","hasMany","point","enum","coordinates","indexOptions","radio","map","option","value","relationship","hasManyRelations","relationTo","schemaToReturn","refPath","ref","richText","row","select","push","tabs","tab","text","textarea","upload"],"mappings":"AA+BA,OAAOA,cAAc,WAAU;AAC/B,SACEC,gBAAgB,EAChBC,gBAAgB,EAChBC,yBAAyB,EACzBC,cAAc,EACdC,UAAU,QACL,iBAAgB;AAiBvB;;;CAGC,GACD,MAAMC,qBAAqB,CAACC,QAC1B,OAAOA,MAAMC,YAAY,KAAK,eAAe,OAAOD,MAAMC,YAAY,KAAK,aACvED,MAAMC,YAAY,GAClBC;AAEN,MAAMC,mBAAmB,CAACH,OAA2BI;IACnD,MAAM,EAAEC,aAAa,EAAEC,aAAa,EAAEC,mBAAmB,EAAE,GAAGH;IAC9D,MAAMI,SAAqC;QACzCC,SAASV,mBAAmBC;QAC5BU,OAAOV,MAAMU,KAAK,IAAK,CAACL,iBAAiBL,MAAMW,MAAM,IAAKJ,uBAAuB;QACjFK,UAAU;QACVD,QAAQ,AAAC,CAACN,iB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K;gBACjCa,YAAY;oBAAEvE,MAAMyB;oBAAQqC,MAAM5E,MAAMqF,UAAU;gBAAC;gBACnDH,OAAO;oBACLpE,MAAMrB,SAASgD,MAAM,CAAC8B,KAAK,CAACC,KAAK;oBACjCe,SAAS,CAAC,EAAEvF,MAAMqC,IAAI,CAAC,WAAW,CAAC;gBACrC;YACF;YAEA,IAAIrC,MAAM0E,OAAO,EAAE;gBACjBY,iBAAiB;oBACfxE,MAAM;wBAACwE;qBAAe;oBACtB7E,SAASV,mBAAmBC;gBAC9B;YACF;QACF,OAAO;YACLsF,iBAAiB;gBACf,GAAGnF,iBAAiBH,OAAOI,mBAAmB;gBAC9CU,MAAMrB,SAASgD,MAAM,CAAC8B,KAAK,CAACC,KAAK;gBACjCgB,KAAKxF,MAAMqF,UAAU;YACvB;YAEA,IAAIrF,MAAM0E,OAAO,EAAE;gBACjBY,iBAAiB;oBACfxE,MAAM;wBAACwE;qBAAe;oBACtB7E,SAASV,mBAAmBC;gBAC9B;YACF;QACF;QAEAQ,OAAOyC,GAAG,CAAC;YACT,CAACjD,MAAMqC,IAAI,CAAC,EAAEiD;QAChB;IACF;AACF"}
1
+ {"version":3,"sources":["../../src/models/buildSchema.ts"],"sourcesContent":["import type { IndexOptions, Schema, SchemaOptions, SchemaTypeOptions } from 'mongoose'\n\nimport mongoose from 'mongoose'\nimport {\n type ArrayField,\n type Block,\n type BlocksField,\n type CheckboxField,\n type CodeField,\n type CollapsibleField,\n type DateField,\n type EmailField,\n type Field,\n type FieldAffectingData,\n type GroupField,\n type JSONField,\n type NonPresentationalField,\n type NumberField,\n type Payload,\n type PointField,\n type RadioField,\n type RelationshipField,\n type RichTextField,\n type RowField,\n type SanitizedLocalizationConfig,\n type SelectField,\n type Tab,\n type TabsField,\n type TextareaField,\n type TextField,\n type UploadField,\n} from 'payload'\nimport {\n fieldAffectsData,\n fieldIsLocalized,\n fieldIsPresentationalOnly,\n fieldIsVirtual,\n tabHasName,\n} from 'payload/shared'\n\nexport type BuildSchemaOptions = {\n allowIDField?: boolean\n disableUnique?: boolean\n draftsEnabled?: boolean\n indexSortableFields?: boolean\n options?: SchemaOptions\n}\n\ntype FieldSchemaGenerator = (\n field: Field,\n schema: Schema,\n config: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n) => void\n\n/**\n * get a field's defaultValue only if defined and not dynamic so that it can be set on the field schema\n * @param field\n */\nconst formatDefaultValue = (field: FieldAffectingData) =>\n typeof field.defaultValue !== 'undefined' && typeof field.defaultValue !== 'function'\n ? field.defaultValue\n : undefined\n\nconst formatBaseSchema = (field: FieldAffectingData, buildSchemaOptions: BuildSchemaOptions) => {\n const { disableUnique, draftsEnabled, indexSortableFields } = buildSchemaOptions\n const schema: SchemaTypeOptions<unknown> = {\n default: formatDefaultValue(field),\n index: field.index || (!disableUnique && field.unique) || indexSortableFields || false,\n required: false,\n unique: (!disableUnique && field.unique) || false,\n }\n\n if (\n schema.unique &&\n (field.localized ||\n draftsEnabled ||\n (fieldAffectsData(field) &&\n field.type !== 'group' &&\n field.type !== 'tab' &&\n field.required !== true))\n ) {\n schema.sparse = true\n }\n\n if (field.hidden) {\n schema.hidden = true\n }\n\n return schema\n}\n\nconst localizeSchema = (\n entity: NonPresentationalField | Tab,\n schema,\n localization: false | SanitizedLocalizationConfig,\n) => {\n if (fieldIsLocalized(entity) && localization && Array.isArray(localization.locales)) {\n return {\n type: localization.localeCodes.reduce(\n (localeSchema, locale) => ({\n ...localeSchema,\n [locale]: schema,\n }),\n {\n _id: false,\n },\n ),\n localized: true,\n }\n }\n return schema\n}\n\nexport const buildSchema = (\n payload: Payload,\n configFields: Field[],\n buildSchemaOptions: BuildSchemaOptions = {},\n): Schema => {\n const { allowIDField, options } = buildSchemaOptions\n let fields = {}\n\n let schemaFields = configFields\n\n if (!allowIDField) {\n const idField = schemaFields.find((field) => fieldAffectsData(field) && field.name === 'id')\n if (idField) {\n fields = {\n _id: idField.type === 'number' ? Number : String,\n }\n schemaFields = schemaFields.filter(\n (field) => !(fieldAffectsData(field) && field.name === 'id'),\n )\n }\n }\n\n const schema = new mongoose.Schema(fields, options)\n\n schemaFields.forEach((field) => {\n if (fieldIsVirtual(field)) {\n return\n }\n\n if (!fieldIsPresentationalOnly(field)) {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[field.type]\n\n if (addFieldSchema) {\n addFieldSchema(field, schema, payload, buildSchemaOptions)\n }\n }\n })\n\n return schema\n}\n\nconst fieldToSchemaMap: Record<string, FieldSchemaGenerator> = {\n array: (\n field: ArrayField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ) => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: [\n buildSchema(payload, field.fields, {\n allowIDField: true,\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n ],\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n blocks: (\n field: BlocksField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const fieldSchema = {\n type: [new mongoose.Schema({}, { _id: false, discriminatorKey: 'blockType' })],\n }\n\n schema.add({\n [field.name]: localizeSchema(field, fieldSchema, payload.config.localization),\n })\n\n field.blocks.forEach((blockItem: Block) => {\n const blockSchema = new mongoose.Schema({}, { _id: false, id: false })\n\n blockItem.fields.forEach((blockField) => {\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[blockField.type]\n if (addFieldSchema) {\n addFieldSchema(blockField, blockSchema, payload, buildSchemaOptions)\n }\n })\n\n if (field.localized && payload.config.localization) {\n payload.config.localization.localeCodes.forEach((localeCode) => {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(`${field.name}.${localeCode}`).discriminator(blockItem.slug, blockSchema)\n })\n } else {\n // @ts-expect-error Possible incorrect typing in mongoose types, this works\n schema.path(field.name).discriminator(blockItem.slug, blockSchema)\n }\n })\n },\n checkbox: (\n field: CheckboxField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Boolean }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n code: (\n field: CodeField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n collapsible: (\n field: CollapsibleField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions)\n }\n })\n },\n date: (\n field: DateField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: Date }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n email: (\n field: EmailField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n group: (\n field: GroupField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const formattedBaseSchema = formatBaseSchema(field, buildSchemaOptions)\n\n // carry indexSortableFields through to versions if drafts enabled\n const indexSortableFields =\n buildSchemaOptions.indexSortableFields &&\n field.name === 'version' &&\n buildSchemaOptions.draftsEnabled\n\n const baseSchema = {\n ...formattedBaseSchema,\n type: buildSchema(payload, field.fields, {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n indexSortableFields,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n json: (\n field: JSONField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n number: (\n field: NumberField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: field.hasMany ? [Number] : Number,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n point: (\n field: PointField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema: SchemaTypeOptions<unknown> = {\n type: {\n type: String,\n enum: ['Point'],\n ...(typeof field.defaultValue !== 'undefined' && {\n default: 'Point',\n }),\n },\n coordinates: {\n type: [Number],\n default: formatDefaultValue(field),\n required: false,\n },\n }\n if (buildSchemaOptions.disableUnique && field.unique && field.localized) {\n baseSchema.coordinates.sparse = true\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n\n if (field.index === true || field.index === undefined) {\n const indexOptions: IndexOptions = {}\n if (!buildSchemaOptions.disableUnique && field.unique) {\n indexOptions.sparse = true\n indexOptions.unique = true\n }\n if (field.localized && payload.config.localization) {\n payload.config.localization.locales.forEach((locale) => {\n schema.index({ [`${field.name}.${locale.code}`]: '2dsphere' }, indexOptions)\n })\n } else {\n schema.index({ [field.name]: '2dsphere' }, indexOptions)\n }\n }\n },\n radio: (\n field: RadioField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n relationship: (\n field: RelationshipField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ) => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (field.localized && payload.config.localization) {\n schemaToReturn = {\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n richText: (\n field: RichTextField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: mongoose.Schema.Types.Mixed,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n row: (\n field: RowField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions)\n }\n })\n },\n select: (\n field: SelectField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: String,\n enum: field.options.map((option) => {\n if (typeof option === 'object') {\n return option.value\n }\n return option\n }),\n }\n\n if (buildSchemaOptions.draftsEnabled || !field.required) {\n baseSchema.enum.push(null)\n }\n\n schema.add({\n [field.name]: localizeSchema(\n field,\n field.hasMany ? [baseSchema] : baseSchema,\n payload.config.localization,\n ),\n })\n },\n tabs: (\n field: TabsField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n field.tabs.forEach((tab) => {\n if (tabHasName(tab)) {\n if (fieldIsVirtual(tab)) {\n return\n }\n const baseSchema = {\n type: buildSchema(payload, tab.fields, {\n disableUnique: buildSchemaOptions.disableUnique,\n draftsEnabled: buildSchemaOptions.draftsEnabled,\n options: {\n _id: false,\n id: false,\n minimize: false,\n },\n }),\n }\n\n schema.add({\n [tab.name]: localizeSchema(tab, baseSchema, payload.config.localization),\n })\n } else {\n tab.fields.forEach((subField: Field) => {\n if (fieldIsVirtual(subField)) {\n return\n }\n const addFieldSchema: FieldSchemaGenerator = fieldToSchemaMap[subField.type]\n\n if (addFieldSchema) {\n addFieldSchema(subField, schema, payload, buildSchemaOptions)\n }\n })\n }\n })\n },\n text: (\n field: TextField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: field.hasMany ? [String] : String,\n }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n textarea: (\n field: TextareaField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const baseSchema = { ...formatBaseSchema(field, buildSchemaOptions), type: String }\n\n schema.add({\n [field.name]: localizeSchema(field, baseSchema, payload.config.localization),\n })\n },\n upload: (\n field: UploadField,\n schema: Schema,\n payload: Payload,\n buildSchemaOptions: BuildSchemaOptions,\n ): void => {\n const hasManyRelations = Array.isArray(field.relationTo)\n let schemaToReturn: { [key: string]: any } = {}\n\n const valueType = getRelationshipValueType(field, payload)\n\n if (field.localized && payload.config.localization) {\n schemaToReturn = {\n type: payload.config.localization.localeCodes.reduce((locales, locale) => {\n let localeSchema: { [key: string]: any } = {}\n\n if (hasManyRelations) {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.${locale}.relationTo`,\n },\n }\n } else {\n localeSchema = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: valueType,\n ref: field.relationTo,\n }\n }\n\n return {\n ...locales,\n [locale]: field.hasMany\n ? { type: [localeSchema], default: formatDefaultValue(field) }\n : localeSchema,\n }\n }, {}),\n localized: true,\n }\n } else if (hasManyRelations) {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n _id: false,\n type: mongoose.Schema.Types.Mixed,\n relationTo: { type: String, enum: field.relationTo },\n value: {\n type: valueType,\n refPath: `${field.name}.relationTo`,\n },\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n } else {\n schemaToReturn = {\n ...formatBaseSchema(field, buildSchemaOptions),\n type: valueType,\n ref: field.relationTo,\n }\n\n if (field.hasMany) {\n schemaToReturn = {\n type: [schemaToReturn],\n default: formatDefaultValue(field),\n }\n }\n }\n\n schema.add({\n [field.name]: schemaToReturn,\n })\n },\n}\n\nconst getRelationshipValueType = (field: RelationshipField | UploadField, payload: Payload) => {\n if (typeof field.relationTo === 'string') {\n const { customIDType } = payload.collections[field.relationTo]\n\n if (!customIDType) {\n return mongoose.Schema.Types.ObjectId\n }\n\n if (customIDType === 'number') {\n return mongoose.Schema.Types.Number\n }\n\n return mongoose.Schema.Types.String\n }\n\n // has custom id relationTo\n if (\n field.relationTo.some((relationTo) => {\n return !!payload.collections[relationTo].customIDType\n })\n ) {\n return mongoose.Schema.Types.Mixed\n }\n\n return mongoose.Schema.Types.ObjectId\n}\n"],"names":["mongoose","fieldAffectsData","fieldIsLocalized","fieldIsPresentationalOnly","fieldIsVirtual","tabHasName","formatDefaultValue","field","defaultValue","undefined","formatBaseSchema","buildSchemaOptions","disableUnique","draftsEnabled","indexSortableFields","schema","default","index","unique","required","localized","type","sparse","hidden","localizeSchema","entity","localization","Array","isArray","locales","localeCodes","reduce","localeSchema","locale","_id","buildSchema","payload","configFields","allowIDField","options","fields","schemaFields","idField","find","name","Number","String","filter","Schema","forEach","addFieldSchema","fieldToSchemaMap","array","baseSchema","id","minimize","add","config","blocks","fieldSchema","discriminatorKey","blockItem","blockSchema","blockField","localeCode","path","discriminator","slug","checkbox","Boolean","code","collapsible","subField","date","Date","email","group","formattedBaseSchema","json","Types","Mixed","number","hasMany","point","enum","coordinates","indexOptions","radio","map","option","value","relationship","hasManyRelations","relationTo","schemaToReturn","valueType","getRelationshipValueType","refPath","ref","richText","row","select","push","tabs","tab","text","textarea","upload","customIDType","collections","ObjectId","some"],"mappings":"AAEA,O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,SAASV,mBAAmBC;gBAC9B;YACF;QACF;QAEAQ,OAAOyC,GAAG,CAAC;YACT,CAACjD,MAAMqC,IAAI,CAAC,EAAEkD;QAChB;IACF;AACF;AAEA,MAAME,2BAA2B,CAACzF,OAAwC6B;IACxE,IAAI,OAAO7B,MAAMsF,UAAU,KAAK,UAAU;QACxC,MAAM,EAAEe,YAAY,EAAE,GAAGxE,QAAQyE,WAAW,CAACtG,MAAMsF,UAAU,CAAC;QAE9D,IAAI,CAACe,cAAc;YACjB,OAAO5G,SAASgD,MAAM,CAAC+B,KAAK,CAAC+B,QAAQ;QACvC;QAEA,IAAIF,iBAAiB,UAAU;YAC7B,OAAO5G,SAASgD,MAAM,CAAC+B,KAAK,CAAClC,MAAM;QACrC;QAEA,OAAO7C,SAASgD,MAAM,CAAC+B,KAAK,CAACjC,MAAM;IACrC;IAEA,2BAA2B;IAC3B,IACEvC,MAAMsF,UAAU,CAACkB,IAAI,CAAC,CAAClB;QACrB,OAAO,CAAC,CAACzD,QAAQyE,WAAW,CAAChB,WAAW,CAACe,YAAY;IACvD,IACA;QACA,OAAO5G,SAASgD,MAAM,CAAC+B,KAAK,CAACC,KAAK;IACpC;IAEA,OAAOhF,SAASgD,MAAM,CAAC+B,KAAK,CAAC+B,QAAQ;AACvC"}
@@ -1 +1 @@
1
- {"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AAmFrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA8EhB"}
1
+ {"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AAmGrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA4EhB"}
@@ -11,6 +11,9 @@ const migrateModelWithBatching = async ({ batchSize, config, fields, Model, sess
11
11
  session,
12
12
  skip
13
13
  });
14
+ if (docs.length === 0) {
15
+ break;
16
+ }
14
17
  hasNext = docs.length > batchSize;
15
18
  if (hasNext) {
16
19
  docs.pop();
@@ -22,12 +25,14 @@ const migrateModelWithBatching = async ({ batchSize, config, fields, Model, sess
22
25
  fields
23
26
  });
24
27
  }
25
- await Model.bulkWrite(docs.map((doc)=>({
28
+ await Model.collection.bulkWrite(docs.map((doc)=>({
26
29
  updateOne: {
27
30
  filter: {
28
31
  _id: doc._id
29
32
  },
30
- update: doc
33
+ update: {
34
+ $set: doc
35
+ }
31
36
  }
32
37
  })), {
33
38
  session
@@ -111,11 +116,11 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
111
116
  data: doc,
112
117
  fields: global.fields
113
118
  });
114
- await GlobalsModel.updateOne({
115
- globalType: {
116
- $eq: global.slug
117
- }
118
- }, doc, {
119
+ await GlobalsModel.collection.updateOne({
120
+ globalType: global.slug
121
+ }, {
122
+ $set: doc
123
+ }, {
119
124
  session
120
125
  });
121
126
  payload.logger.info(`Migrated global "${global.slug}"`);