@payloadcms/db-mongodb 3.25.0 → 3.26.0

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 (164) hide show
  1. package/dist/connect.d.ts.map +1 -1
  2. package/dist/connect.js +5 -1
  3. package/dist/connect.js.map +1 -1
  4. package/dist/count.d.ts.map +1 -1
  5. package/dist/count.js +8 -4
  6. package/dist/count.js.map +1 -1
  7. package/dist/countGlobalVersions.d.ts.map +1 -1
  8. package/dist/countGlobalVersions.js +8 -3
  9. package/dist/countGlobalVersions.js.map +1 -1
  10. package/dist/countVersions.d.ts.map +1 -1
  11. package/dist/countVersions.js +8 -3
  12. package/dist/countVersions.js.map +1 -1
  13. package/dist/create.d.ts.map +1 -1
  14. package/dist/create.js +13 -6
  15. package/dist/create.js.map +1 -1
  16. package/dist/createGlobal.d.ts +1 -1
  17. package/dist/createGlobal.d.ts.map +1 -1
  18. package/dist/createGlobal.js +12 -5
  19. package/dist/createGlobal.js.map +1 -1
  20. package/dist/createGlobalVersion.d.ts.map +1 -1
  21. package/dist/createGlobalVersion.js +13 -5
  22. package/dist/createGlobalVersion.js.map +1 -1
  23. package/dist/createMigration.d.ts.map +1 -1
  24. package/dist/createMigration.js.map +1 -1
  25. package/dist/createVersion.d.ts.map +1 -1
  26. package/dist/createVersion.js +13 -5
  27. package/dist/createVersion.js.map +1 -1
  28. package/dist/deleteMany.d.ts +1 -1
  29. package/dist/deleteMany.d.ts.map +1 -1
  30. package/dist/deleteMany.js +8 -4
  31. package/dist/deleteMany.js.map +1 -1
  32. package/dist/deleteOne.d.ts.map +1 -1
  33. package/dist/deleteOne.js +14 -6
  34. package/dist/deleteOne.js.map +1 -1
  35. package/dist/deleteVersions.d.ts.map +1 -1
  36. package/dist/deleteVersions.js +9 -4
  37. package/dist/deleteVersions.js.map +1 -1
  38. package/dist/find.d.ts.map +1 -1
  39. package/dist/find.js +25 -8
  40. package/dist/find.js.map +1 -1
  41. package/dist/findGlobal.d.ts.map +1 -1
  42. package/dist/findGlobal.js +8 -5
  43. package/dist/findGlobal.js.map +1 -1
  44. package/dist/findGlobalVersions.d.ts.map +1 -1
  45. package/dist/findGlobalVersions.js +8 -4
  46. package/dist/findGlobalVersions.js.map +1 -1
  47. package/dist/findOne.d.ts +1 -1
  48. package/dist/findOne.d.ts.map +1 -1
  49. package/dist/findOne.js +18 -8
  50. package/dist/findOne.js.map +1 -1
  51. package/dist/findVersions.d.ts.map +1 -1
  52. package/dist/findVersions.js +8 -4
  53. package/dist/findVersions.js.map +1 -1
  54. package/dist/index.d.ts +6 -7
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +6 -1
  57. package/dist/index.js.map +1 -1
  58. package/dist/init.d.ts.map +1 -1
  59. package/dist/init.js +1 -5
  60. package/dist/init.js.map +1 -1
  61. package/dist/migrateFresh.d.ts.map +1 -1
  62. package/dist/migrateFresh.js.map +1 -1
  63. package/dist/models/buildCollectionSchema.d.ts.map +1 -1
  64. package/dist/models/buildCollectionSchema.js +0 -4
  65. package/dist/models/buildCollectionSchema.js.map +1 -1
  66. package/dist/models/buildSchema.d.ts.map +1 -1
  67. package/dist/models/buildSchema.js +545 -515
  68. package/dist/models/buildSchema.js.map +1 -1
  69. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
  70. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +21 -8
  71. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
  72. package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts.map +1 -1
  73. package/dist/predefinedMigrations/migrateVersionsV1_V2.js +20 -8
  74. package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -1
  75. package/dist/queries/buildSearchParams.d.ts +1 -1
  76. package/dist/queries/buildSearchParams.d.ts.map +1 -1
  77. package/dist/queries/buildSearchParams.js +42 -29
  78. package/dist/queries/buildSearchParams.js.map +1 -1
  79. package/dist/queries/buildSortParam.d.ts +2 -3
  80. package/dist/queries/buildSortParam.d.ts.map +1 -1
  81. package/dist/queries/buildSortParam.js +1 -1
  82. package/dist/queries/buildSortParam.js.map +1 -1
  83. package/dist/queries/getBuildQueryPlugin.d.ts.map +1 -1
  84. package/dist/queries/getBuildQueryPlugin.js +15 -8
  85. package/dist/queries/getBuildQueryPlugin.js.map +1 -1
  86. package/dist/queries/getLocalizedSortProperty.d.ts +1 -1
  87. package/dist/queries/getLocalizedSortProperty.d.ts.map +1 -1
  88. package/dist/queries/getLocalizedSortProperty.js +11 -6
  89. package/dist/queries/getLocalizedSortProperty.js.map +1 -1
  90. package/dist/queries/getLocalizedSortProperty.spec.js +8 -0
  91. package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
  92. package/dist/queries/operatorMap.d.ts +1 -0
  93. package/dist/queries/operatorMap.d.ts.map +1 -1
  94. package/dist/queries/operatorMap.js.map +1 -1
  95. package/dist/queries/parseParams.d.ts +1 -1
  96. package/dist/queries/parseParams.d.ts.map +1 -1
  97. package/dist/queries/parseParams.js +3 -3
  98. package/dist/queries/parseParams.js.map +1 -1
  99. package/dist/queries/sanitizeQueryValue.d.ts +1 -1
  100. package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
  101. package/dist/queries/sanitizeQueryValue.js +30 -26
  102. package/dist/queries/sanitizeQueryValue.js.map +1 -1
  103. package/dist/queryDrafts.d.ts.map +1 -1
  104. package/dist/queryDrafts.js +26 -8
  105. package/dist/queryDrafts.js.map +1 -1
  106. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  107. package/dist/transactions/beginTransaction.js +4 -2
  108. package/dist/transactions/beginTransaction.js.map +1 -1
  109. package/dist/transactions/commitTransaction.d.ts.map +1 -1
  110. package/dist/transactions/commitTransaction.js +1 -1
  111. package/dist/transactions/commitTransaction.js.map +1 -1
  112. package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
  113. package/dist/transactions/rollbackTransaction.js +3 -3
  114. package/dist/transactions/rollbackTransaction.js.map +1 -1
  115. package/dist/types.d.ts +2 -2
  116. package/dist/types.d.ts.map +1 -1
  117. package/dist/types.js.map +1 -1
  118. package/dist/updateGlobal.d.ts.map +1 -1
  119. package/dist/updateGlobal.js +17 -7
  120. package/dist/updateGlobal.js.map +1 -1
  121. package/dist/updateGlobalVersion.d.ts +1 -1
  122. package/dist/updateGlobalVersion.d.ts.map +1 -1
  123. package/dist/updateGlobalVersion.js +14 -6
  124. package/dist/updateGlobalVersion.js.map +1 -1
  125. package/dist/updateMany.d.ts +3 -0
  126. package/dist/updateMany.d.ts.map +1 -0
  127. package/dist/updateMany.js +75 -0
  128. package/dist/updateMany.js.map +1 -0
  129. package/dist/updateOne.d.ts.map +1 -1
  130. package/dist/updateOne.js +17 -8
  131. package/dist/updateOne.js.map +1 -1
  132. package/dist/updateVersion.d.ts.map +1 -1
  133. package/dist/updateVersion.js +14 -5
  134. package/dist/updateVersion.js.map +1 -1
  135. package/dist/upsert.d.ts.map +1 -1
  136. package/dist/upsert.js +2 -1
  137. package/dist/upsert.js.map +1 -1
  138. package/dist/utilities/aggregatePaginate.d.ts +19 -0
  139. package/dist/utilities/aggregatePaginate.d.ts.map +1 -0
  140. package/dist/utilities/aggregatePaginate.js +78 -0
  141. package/dist/utilities/aggregatePaginate.js.map +1 -0
  142. package/dist/utilities/buildJoinAggregation.d.ts +5 -6
  143. package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
  144. package/dist/utilities/buildJoinAggregation.js +39 -36
  145. package/dist/utilities/buildJoinAggregation.js.map +1 -1
  146. package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
  147. package/dist/utilities/buildProjectionFromSelect.js +8 -4
  148. package/dist/utilities/buildProjectionFromSelect.js.map +1 -1
  149. package/dist/utilities/getDBName.d.ts +1 -1
  150. package/dist/utilities/getDBName.d.ts.map +1 -1
  151. package/dist/utilities/getDBName.js +6 -2
  152. package/dist/utilities/getDBName.js.map +1 -1
  153. package/dist/utilities/getEntity.d.ts +33 -0
  154. package/dist/utilities/getEntity.d.ts.map +1 -0
  155. package/dist/utilities/getEntity.js +49 -0
  156. package/dist/utilities/getEntity.js.map +1 -0
  157. package/dist/utilities/handleError.d.ts +1 -1
  158. package/dist/utilities/handleError.d.ts.map +1 -1
  159. package/dist/utilities/handleError.js +3 -2
  160. package/dist/utilities/handleError.js.map +1 -1
  161. package/dist/utilities/transform.d.ts.map +1 -1
  162. package/dist/utilities/transform.js +10 -9
  163. package/dist/utilities/transform.js.map +1 -1
  164. package/package.json +5 -4
@@ -60,160 +60,27 @@ export const buildSchema = (args)=>{
60
60
  return;
61
61
  }
62
62
  if (!fieldIsPresentationalOnly(field)) {
63
- const addFieldSchema = fieldToSchemaMap[field.type];
63
+ const addFieldSchema = getSchemaGenerator(field.type);
64
64
  if (addFieldSchema) {
65
- addFieldSchema(field, schema, payload, buildSchemaOptions, parentIsLocalized);
65
+ addFieldSchema(field, schema, payload, buildSchemaOptions, parentIsLocalized ?? false);
66
66
  }
67
67
  }
68
68
  });
69
69
  return schema;
70
70
  };
71
- const fieldToSchemaMap = {
72
- array: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
73
- const baseSchema = {
74
- ...formatBaseSchema({
75
- buildSchemaOptions,
76
- field,
77
- parentIsLocalized
78
- }),
79
- type: [
80
- buildSchema({
81
- buildSchemaOptions: {
82
- allowIDField: true,
83
- disableUnique: buildSchemaOptions.disableUnique,
84
- draftsEnabled: buildSchemaOptions.draftsEnabled,
85
- options: {
86
- _id: false,
87
- id: false,
88
- minimize: false
89
- }
90
- },
91
- configFields: field.fields,
92
- parentIsLocalized: parentIsLocalized || field.localized,
93
- payload
94
- })
95
- ]
96
- };
97
- schema.add({
98
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
99
- });
100
- },
101
- blocks: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
102
- const fieldSchema = {
103
- type: [
104
- new mongoose.Schema({}, {
105
- _id: false,
106
- discriminatorKey: 'blockType'
107
- })
108
- ]
109
- };
110
- schema.add({
111
- [field.name]: localizeSchema(field, fieldSchema, payload.config.localization, parentIsLocalized)
112
- });
113
- (field.blockReferences ?? field.blocks).forEach((blockItem)=>{
114
- const blockSchema = new mongoose.Schema({}, {
115
- _id: false,
116
- id: false
117
- });
118
- const block = typeof blockItem === 'string' ? payload.blocks[blockItem] : blockItem;
119
- block.fields.forEach((blockField)=>{
120
- const addFieldSchema = fieldToSchemaMap[blockField.type];
121
- if (addFieldSchema) {
122
- addFieldSchema(blockField, blockSchema, payload, buildSchemaOptions, parentIsLocalized || field.localized);
123
- }
124
- });
125
- if (fieldShouldBeLocalized({
126
- field,
127
- parentIsLocalized
128
- }) && payload.config.localization) {
129
- payload.config.localization.localeCodes.forEach((localeCode)=>{
130
- // @ts-expect-error Possible incorrect typing in mongoose types, this works
131
- schema.path(`${field.name}.${localeCode}`).discriminator(block.slug, blockSchema);
132
- });
133
- } else {
134
- // @ts-expect-error Possible incorrect typing in mongoose types, this works
135
- schema.path(field.name).discriminator(block.slug, blockSchema);
136
- }
137
- });
138
- },
139
- checkbox: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
140
- const baseSchema = {
141
- ...formatBaseSchema({
142
- buildSchemaOptions,
143
- field,
144
- parentIsLocalized
145
- }),
146
- type: Boolean
147
- };
148
- schema.add({
149
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
150
- });
151
- },
152
- code: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
153
- const baseSchema = {
154
- ...formatBaseSchema({
155
- buildSchemaOptions,
156
- field,
157
- parentIsLocalized
158
- }),
159
- type: String
160
- };
161
- schema.add({
162
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
163
- });
164
- },
165
- collapsible: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
166
- field.fields.forEach((subField)=>{
167
- if (fieldIsVirtual(subField)) {
168
- return;
169
- }
170
- const addFieldSchema = fieldToSchemaMap[subField.type];
171
- if (addFieldSchema) {
172
- addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized);
173
- }
174
- });
175
- },
176
- date: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
177
- const baseSchema = {
178
- ...formatBaseSchema({
179
- buildSchemaOptions,
180
- field,
181
- parentIsLocalized
182
- }),
183
- type: Date
184
- };
185
- schema.add({
186
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
187
- });
188
- },
189
- email: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
190
- const baseSchema = {
191
- ...formatBaseSchema({
192
- buildSchemaOptions,
193
- field,
194
- parentIsLocalized
195
- }),
196
- type: String
197
- };
198
- schema.add({
199
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
200
- });
201
- },
202
- group: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
203
- const formattedBaseSchema = formatBaseSchema({
71
+ const array = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
72
+ const baseSchema = {
73
+ ...formatBaseSchema({
204
74
  buildSchemaOptions,
205
75
  field,
206
76
  parentIsLocalized
207
- });
208
- // carry indexSortableFields through to versions if drafts enabled
209
- const indexSortableFields = buildSchemaOptions.indexSortableFields && field.name === 'version' && buildSchemaOptions.draftsEnabled;
210
- const baseSchema = {
211
- ...formattedBaseSchema,
212
- type: buildSchema({
77
+ }),
78
+ type: [
79
+ buildSchema({
213
80
  buildSchemaOptions: {
81
+ allowIDField: true,
214
82
  disableUnique: buildSchemaOptions.disableUnique,
215
83
  draftsEnabled: buildSchemaOptions.draftsEnabled,
216
- indexSortableFields,
217
84
  options: {
218
85
  _id: false,
219
86
  id: false,
@@ -224,430 +91,593 @@ const fieldToSchemaMap = {
224
91
  parentIsLocalized: parentIsLocalized || field.localized,
225
92
  payload
226
93
  })
227
- };
228
- schema.add({
229
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
230
- });
231
- },
232
- json: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
233
- const baseSchema = {
234
- ...formatBaseSchema({
235
- buildSchemaOptions,
236
- field,
237
- parentIsLocalized
238
- }),
239
- type: mongoose.Schema.Types.Mixed
240
- };
241
- schema.add({
242
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
94
+ ]
95
+ };
96
+ schema.add({
97
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
98
+ });
99
+ };
100
+ const blocks = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
101
+ const fieldSchema = {
102
+ type: [
103
+ new mongoose.Schema({}, {
104
+ _id: false,
105
+ discriminatorKey: 'blockType'
106
+ })
107
+ ]
108
+ };
109
+ schema.add({
110
+ [field.name]: localizeSchema(field, fieldSchema, payload.config.localization, parentIsLocalized)
111
+ });
112
+ (field.blockReferences ?? field.blocks).forEach((blockItem)=>{
113
+ const blockSchema = new mongoose.Schema({}, {
114
+ _id: false,
115
+ id: false
243
116
  });
244
- },
245
- number: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
246
- const baseSchema = {
247
- ...formatBaseSchema({
248
- buildSchemaOptions,
249
- field,
250
- parentIsLocalized
251
- }),
252
- type: field.hasMany ? [
253
- Number
254
- ] : Number
255
- };
256
- schema.add({
257
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
117
+ const block = typeof blockItem === 'string' ? payload.blocks[blockItem] : blockItem;
118
+ if (!block) {
119
+ return;
120
+ }
121
+ block.fields.forEach((blockField)=>{
122
+ const addFieldSchema = getSchemaGenerator(blockField.type);
123
+ if (addFieldSchema) {
124
+ addFieldSchema(blockField, blockSchema, payload, buildSchemaOptions, (parentIsLocalized || field.localized) ?? false);
125
+ }
258
126
  });
259
- },
260
- point: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
261
- const baseSchema = {
262
- type: {
263
- type: String,
264
- enum: [
265
- 'Point'
266
- ],
267
- ...typeof field.defaultValue !== 'undefined' && {
268
- default: 'Point'
127
+ if (fieldShouldBeLocalized({
128
+ field,
129
+ parentIsLocalized
130
+ }) && payload.config.localization) {
131
+ payload.config.localization.localeCodes.forEach((localeCode)=>{
132
+ // @ts-expect-error Possible incorrect typing in mongoose types, this works
133
+ schema.path(`${field.name}.${localeCode}`).discriminator(block.slug, blockSchema);
134
+ });
135
+ } else {
136
+ // @ts-expect-error Possible incorrect typing in mongoose types, this works
137
+ schema.path(field.name).discriminator(block.slug, blockSchema);
138
+ }
139
+ });
140
+ };
141
+ const checkbox = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
142
+ const baseSchema = {
143
+ ...formatBaseSchema({
144
+ buildSchemaOptions,
145
+ field,
146
+ parentIsLocalized
147
+ }),
148
+ type: Boolean
149
+ };
150
+ schema.add({
151
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
152
+ });
153
+ };
154
+ const code = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
155
+ const baseSchema = {
156
+ ...formatBaseSchema({
157
+ buildSchemaOptions,
158
+ field,
159
+ parentIsLocalized
160
+ }),
161
+ type: String
162
+ };
163
+ schema.add({
164
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
165
+ });
166
+ };
167
+ const collapsible = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
168
+ field.fields.forEach((subField)=>{
169
+ if (fieldIsVirtual(subField)) {
170
+ return;
171
+ }
172
+ const addFieldSchema = getSchemaGenerator(subField.type);
173
+ if (addFieldSchema) {
174
+ addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized);
175
+ }
176
+ });
177
+ };
178
+ const date = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
179
+ const baseSchema = {
180
+ ...formatBaseSchema({
181
+ buildSchemaOptions,
182
+ field,
183
+ parentIsLocalized
184
+ }),
185
+ type: Date
186
+ };
187
+ schema.add({
188
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
189
+ });
190
+ };
191
+ const email = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
192
+ const baseSchema = {
193
+ ...formatBaseSchema({
194
+ buildSchemaOptions,
195
+ field,
196
+ parentIsLocalized
197
+ }),
198
+ type: String
199
+ };
200
+ schema.add({
201
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
202
+ });
203
+ };
204
+ const group = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
205
+ const formattedBaseSchema = formatBaseSchema({
206
+ buildSchemaOptions,
207
+ field,
208
+ parentIsLocalized
209
+ });
210
+ // carry indexSortableFields through to versions if drafts enabled
211
+ const indexSortableFields = buildSchemaOptions.indexSortableFields && field.name === 'version' && buildSchemaOptions.draftsEnabled;
212
+ const baseSchema = {
213
+ ...formattedBaseSchema,
214
+ type: buildSchema({
215
+ buildSchemaOptions: {
216
+ disableUnique: buildSchemaOptions.disableUnique,
217
+ draftsEnabled: buildSchemaOptions.draftsEnabled,
218
+ indexSortableFields,
219
+ options: {
220
+ _id: false,
221
+ id: false,
222
+ minimize: false
269
223
  }
270
224
  },
271
- coordinates: {
272
- type: [
273
- Number
274
- ],
275
- default: formatDefaultValue(field),
276
- required: false
277
- }
278
- };
279
- if (buildSchemaOptions.disableUnique && field.unique && fieldShouldBeLocalized({
225
+ configFields: field.fields,
226
+ parentIsLocalized: parentIsLocalized || field.localized,
227
+ payload
228
+ })
229
+ };
230
+ schema.add({
231
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
232
+ });
233
+ };
234
+ const json = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
235
+ const baseSchema = {
236
+ ...formatBaseSchema({
237
+ buildSchemaOptions,
280
238
  field,
281
239
  parentIsLocalized
282
- })) {
283
- baseSchema.coordinates.sparse = true;
284
- }
285
- schema.add({
286
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
287
- });
288
- if (field.index === true || field.index === undefined) {
289
- const indexOptions = {};
290
- if (!buildSchemaOptions.disableUnique && field.unique) {
291
- indexOptions.sparse = true;
292
- indexOptions.unique = true;
293
- }
294
- if (fieldShouldBeLocalized({
295
- field,
296
- parentIsLocalized
297
- }) && payload.config.localization) {
298
- payload.config.localization.locales.forEach((locale)=>{
299
- schema.index({
300
- [`${field.name}.${locale.code}`]: '2dsphere'
301
- }, indexOptions);
302
- });
303
- } else {
304
- schema.index({
305
- [field.name]: '2dsphere'
306
- }, indexOptions);
240
+ }),
241
+ type: mongoose.Schema.Types.Mixed
242
+ };
243
+ schema.add({
244
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
245
+ });
246
+ };
247
+ const number = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
248
+ const baseSchema = {
249
+ ...formatBaseSchema({
250
+ buildSchemaOptions,
251
+ field,
252
+ parentIsLocalized
253
+ }),
254
+ type: field.hasMany ? [
255
+ Number
256
+ ] : Number
257
+ };
258
+ schema.add({
259
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
260
+ });
261
+ };
262
+ const point = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
263
+ const baseSchema = {
264
+ type: {
265
+ type: String,
266
+ enum: [
267
+ 'Point'
268
+ ],
269
+ ...typeof field.defaultValue !== 'undefined' && {
270
+ default: 'Point'
307
271
  }
272
+ },
273
+ coordinates: {
274
+ type: [
275
+ Number
276
+ ],
277
+ default: formatDefaultValue(field),
278
+ required: false
279
+ }
280
+ };
281
+ if (buildSchemaOptions.disableUnique && field.unique && fieldShouldBeLocalized({
282
+ field,
283
+ parentIsLocalized
284
+ })) {
285
+ baseSchema.coordinates.sparse = true;
286
+ }
287
+ schema.add({
288
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
289
+ });
290
+ if (field.index === true || field.index === undefined) {
291
+ const indexOptions = {};
292
+ if (!buildSchemaOptions.disableUnique && field.unique) {
293
+ indexOptions.sparse = true;
294
+ indexOptions.unique = true;
308
295
  }
309
- },
310
- radio: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
311
- const baseSchema = {
312
- ...formatBaseSchema({
313
- buildSchemaOptions,
314
- field,
315
- parentIsLocalized
316
- }),
317
- type: String,
318
- enum: field.options.map((option)=>{
319
- if (typeof option === 'object') {
320
- return option.value;
321
- }
322
- return option;
323
- })
324
- };
325
- schema.add({
326
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
327
- });
328
- },
329
- relationship: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
330
- const hasManyRelations = Array.isArray(field.relationTo);
331
- let schemaToReturn = {};
332
- const valueType = getRelationshipValueType(field, payload);
333
296
  if (fieldShouldBeLocalized({
334
297
  field,
335
298
  parentIsLocalized
336
299
  }) && payload.config.localization) {
337
- schemaToReturn = {
338
- _id: false,
339
- type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
340
- let localeSchema = {};
341
- if (hasManyRelations) {
342
- localeSchema = {
343
- ...formatBaseSchema({
344
- buildSchemaOptions,
345
- field,
346
- parentIsLocalized
347
- }),
348
- _id: false,
349
- type: mongoose.Schema.Types.Mixed,
350
- relationTo: {
351
- type: String,
352
- enum: field.relationTo
353
- },
354
- value: {
355
- type: valueType,
356
- refPath: `${field.name}.${locale}.relationTo`
357
- }
358
- };
359
- } else {
360
- localeSchema = {
361
- ...formatBaseSchema({
362
- buildSchemaOptions,
363
- field,
364
- parentIsLocalized
365
- }),
300
+ payload.config.localization.locales.forEach((locale)=>{
301
+ schema.index({
302
+ [`${field.name}.${locale.code}`]: '2dsphere'
303
+ }, indexOptions);
304
+ });
305
+ } else {
306
+ schema.index({
307
+ [field.name]: '2dsphere'
308
+ }, indexOptions);
309
+ }
310
+ }
311
+ };
312
+ const radio = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
313
+ const baseSchema = {
314
+ ...formatBaseSchema({
315
+ buildSchemaOptions,
316
+ field,
317
+ parentIsLocalized
318
+ }),
319
+ type: String,
320
+ enum: field.options.map((option)=>{
321
+ if (typeof option === 'object') {
322
+ return option.value;
323
+ }
324
+ return option;
325
+ })
326
+ };
327
+ schema.add({
328
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
329
+ });
330
+ };
331
+ const relationship = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
332
+ const hasManyRelations = Array.isArray(field.relationTo);
333
+ let schemaToReturn = {};
334
+ const valueType = getRelationshipValueType(field, payload);
335
+ if (fieldShouldBeLocalized({
336
+ field,
337
+ parentIsLocalized
338
+ }) && payload.config.localization) {
339
+ schemaToReturn = {
340
+ _id: false,
341
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
342
+ let localeSchema = {};
343
+ if (hasManyRelations) {
344
+ localeSchema = {
345
+ ...formatBaseSchema({
346
+ buildSchemaOptions,
347
+ field,
348
+ parentIsLocalized
349
+ }),
350
+ _id: false,
351
+ type: mongoose.Schema.Types.Mixed,
352
+ relationTo: {
353
+ type: String,
354
+ enum: field.relationTo
355
+ },
356
+ value: {
366
357
  type: valueType,
367
- ref: field.relationTo
368
- };
369
- }
370
- return {
371
- ...locales,
372
- [locale]: field.hasMany ? {
373
- type: [
374
- localeSchema
375
- ],
376
- default: formatDefaultValue(field)
377
- } : localeSchema
358
+ refPath: `${field.name}.${locale}.relationTo`
359
+ }
360
+ };
361
+ } else {
362
+ localeSchema = {
363
+ ...formatBaseSchema({
364
+ buildSchemaOptions,
365
+ field,
366
+ parentIsLocalized
367
+ }),
368
+ type: valueType,
369
+ ref: field.relationTo
378
370
  };
379
- }, {}),
380
- localized: true
381
- };
382
- } else if (hasManyRelations) {
383
- schemaToReturn = {
384
- ...formatBaseSchema({
385
- buildSchemaOptions,
386
- field,
387
- parentIsLocalized
388
- }),
389
- _id: false,
390
- type: mongoose.Schema.Types.Mixed,
391
- relationTo: {
392
- type: String,
393
- enum: field.relationTo
394
- },
395
- value: {
396
- type: valueType,
397
- refPath: `${field.name}.relationTo`
398
371
  }
399
- };
400
- if (field.hasMany) {
401
- schemaToReturn = {
402
- type: [
403
- schemaToReturn
404
- ],
405
- default: formatDefaultValue(field)
372
+ return {
373
+ ...locales,
374
+ [locale]: field.hasMany ? {
375
+ type: [
376
+ localeSchema
377
+ ],
378
+ default: formatDefaultValue(field)
379
+ } : localeSchema
406
380
  };
407
- }
408
- } else {
409
- schemaToReturn = {
410
- ...formatBaseSchema({
411
- buildSchemaOptions,
412
- field,
413
- parentIsLocalized
414
- }),
415
- type: valueType,
416
- ref: field.relationTo
417
- };
418
- if (field.hasMany) {
419
- schemaToReturn = {
420
- type: [
421
- schemaToReturn
422
- ],
423
- default: formatDefaultValue(field)
424
- };
425
- }
426
- }
427
- schema.add({
428
- [field.name]: schemaToReturn
429
- });
430
- },
431
- richText: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
432
- const baseSchema = {
381
+ }, {}),
382
+ localized: true
383
+ };
384
+ } else if (hasManyRelations) {
385
+ schemaToReturn = {
433
386
  ...formatBaseSchema({
434
387
  buildSchemaOptions,
435
388
  field,
436
389
  parentIsLocalized
437
390
  }),
438
- type: mongoose.Schema.Types.Mixed
439
- };
440
- schema.add({
441
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
442
- });
443
- },
444
- row: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
445
- field.fields.forEach((subField)=>{
446
- if (fieldIsVirtual(subField)) {
447
- return;
448
- }
449
- const addFieldSchema = fieldToSchemaMap[subField.type];
450
- if (addFieldSchema) {
451
- addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized);
391
+ _id: false,
392
+ type: mongoose.Schema.Types.Mixed,
393
+ relationTo: {
394
+ type: String,
395
+ enum: field.relationTo
396
+ },
397
+ value: {
398
+ type: valueType,
399
+ refPath: `${field.name}.relationTo`
452
400
  }
453
- });
454
- },
455
- select: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
456
- const baseSchema = {
401
+ };
402
+ if (field.hasMany) {
403
+ schemaToReturn = {
404
+ type: [
405
+ schemaToReturn
406
+ ],
407
+ default: formatDefaultValue(field)
408
+ };
409
+ }
410
+ } else {
411
+ schemaToReturn = {
457
412
  ...formatBaseSchema({
458
413
  buildSchemaOptions,
459
414
  field,
460
415
  parentIsLocalized
461
416
  }),
462
- type: String,
463
- enum: field.options.map((option)=>{
464
- if (typeof option === 'object') {
465
- return option.value;
466
- }
467
- return option;
468
- })
417
+ type: valueType,
418
+ ref: field.relationTo
469
419
  };
470
- if (buildSchemaOptions.draftsEnabled || !field.required) {
471
- baseSchema.enum.push(null);
420
+ if (field.hasMany) {
421
+ schemaToReturn = {
422
+ type: [
423
+ schemaToReturn
424
+ ],
425
+ default: formatDefaultValue(field)
426
+ };
472
427
  }
473
- schema.add({
474
- [field.name]: localizeSchema(field, field.hasMany ? [
475
- baseSchema
476
- ] : baseSchema, payload.config.localization, parentIsLocalized)
477
- });
478
- },
479
- tabs: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
480
- field.tabs.forEach((tab)=>{
481
- if (tabHasName(tab)) {
482
- if (fieldIsVirtual(tab)) {
428
+ }
429
+ schema.add({
430
+ [field.name]: schemaToReturn
431
+ });
432
+ };
433
+ const richText = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
434
+ const baseSchema = {
435
+ ...formatBaseSchema({
436
+ buildSchemaOptions,
437
+ field,
438
+ parentIsLocalized
439
+ }),
440
+ type: mongoose.Schema.Types.Mixed
441
+ };
442
+ schema.add({
443
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
444
+ });
445
+ };
446
+ const row = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
447
+ field.fields.forEach((subField)=>{
448
+ if (fieldIsVirtual(subField)) {
449
+ return;
450
+ }
451
+ const addFieldSchema = getSchemaGenerator(subField.type);
452
+ if (addFieldSchema) {
453
+ addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized);
454
+ }
455
+ });
456
+ };
457
+ const select = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
458
+ const baseSchema = {
459
+ ...formatBaseSchema({
460
+ buildSchemaOptions,
461
+ field,
462
+ parentIsLocalized
463
+ }),
464
+ type: String,
465
+ enum: field.options.map((option)=>{
466
+ if (typeof option === 'object') {
467
+ return option.value;
468
+ }
469
+ return option;
470
+ })
471
+ };
472
+ if (buildSchemaOptions.draftsEnabled || !field.required) {
473
+ ;
474
+ baseSchema.enum.push(null);
475
+ }
476
+ schema.add({
477
+ [field.name]: localizeSchema(field, field.hasMany ? [
478
+ baseSchema
479
+ ] : baseSchema, payload.config.localization, parentIsLocalized)
480
+ });
481
+ };
482
+ const tabs = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
483
+ field.tabs.forEach((tab)=>{
484
+ if (tabHasName(tab)) {
485
+ if (fieldIsVirtual(tab)) {
486
+ return;
487
+ }
488
+ const baseSchema = {
489
+ type: buildSchema({
490
+ buildSchemaOptions: {
491
+ disableUnique: buildSchemaOptions.disableUnique,
492
+ draftsEnabled: buildSchemaOptions.draftsEnabled,
493
+ options: {
494
+ _id: false,
495
+ id: false,
496
+ minimize: false
497
+ }
498
+ },
499
+ configFields: tab.fields,
500
+ parentIsLocalized: parentIsLocalized || tab.localized,
501
+ payload
502
+ })
503
+ };
504
+ schema.add({
505
+ [tab.name]: localizeSchema(tab, baseSchema, payload.config.localization, parentIsLocalized)
506
+ });
507
+ } else {
508
+ tab.fields.forEach((subField)=>{
509
+ if (fieldIsVirtual(subField)) {
483
510
  return;
484
511
  }
485
- const baseSchema = {
486
- type: buildSchema({
487
- buildSchemaOptions: {
488
- disableUnique: buildSchemaOptions.disableUnique,
489
- draftsEnabled: buildSchemaOptions.draftsEnabled,
490
- options: {
491
- _id: false,
492
- id: false,
493
- minimize: false
494
- }
512
+ const addFieldSchema = getSchemaGenerator(subField.type);
513
+ if (addFieldSchema) {
514
+ addFieldSchema(subField, schema, payload, buildSchemaOptions, (parentIsLocalized || tab.localized) ?? false);
515
+ }
516
+ });
517
+ }
518
+ });
519
+ };
520
+ const text = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
521
+ const baseSchema = {
522
+ ...formatBaseSchema({
523
+ buildSchemaOptions,
524
+ field,
525
+ parentIsLocalized
526
+ }),
527
+ type: field.hasMany ? [
528
+ String
529
+ ] : String
530
+ };
531
+ schema.add({
532
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
533
+ });
534
+ };
535
+ const textarea = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
536
+ const baseSchema = {
537
+ ...formatBaseSchema({
538
+ buildSchemaOptions,
539
+ field,
540
+ parentIsLocalized
541
+ }),
542
+ type: String
543
+ };
544
+ schema.add({
545
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
546
+ });
547
+ };
548
+ const upload = (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
549
+ const hasManyRelations = Array.isArray(field.relationTo);
550
+ let schemaToReturn = {};
551
+ const valueType = getRelationshipValueType(field, payload);
552
+ if (fieldShouldBeLocalized({
553
+ field,
554
+ parentIsLocalized
555
+ }) && payload.config.localization) {
556
+ schemaToReturn = {
557
+ _id: false,
558
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
559
+ let localeSchema = {};
560
+ if (hasManyRelations) {
561
+ localeSchema = {
562
+ ...formatBaseSchema({
563
+ buildSchemaOptions,
564
+ field,
565
+ parentIsLocalized
566
+ }),
567
+ _id: false,
568
+ type: mongoose.Schema.Types.Mixed,
569
+ relationTo: {
570
+ type: String,
571
+ enum: field.relationTo
495
572
  },
496
- configFields: tab.fields,
497
- parentIsLocalized: parentIsLocalized || tab.localized,
498
- payload
499
- })
573
+ value: {
574
+ type: valueType,
575
+ refPath: `${field.name}.${locale}.relationTo`
576
+ }
577
+ };
578
+ } else {
579
+ localeSchema = {
580
+ ...formatBaseSchema({
581
+ buildSchemaOptions,
582
+ field,
583
+ parentIsLocalized
584
+ }),
585
+ type: valueType,
586
+ ref: field.relationTo
587
+ };
588
+ }
589
+ return {
590
+ ...locales,
591
+ [locale]: field.hasMany ? {
592
+ type: [
593
+ localeSchema
594
+ ],
595
+ default: formatDefaultValue(field)
596
+ } : localeSchema
500
597
  };
501
- schema.add({
502
- [tab.name]: localizeSchema(tab, baseSchema, payload.config.localization, parentIsLocalized)
503
- });
504
- } else {
505
- tab.fields.forEach((subField)=>{
506
- if (fieldIsVirtual(subField)) {
507
- return;
508
- }
509
- const addFieldSchema = fieldToSchemaMap[subField.type];
510
- if (addFieldSchema) {
511
- addFieldSchema(subField, schema, payload, buildSchemaOptions, parentIsLocalized || tab.localized);
512
- }
513
- });
514
- }
515
- });
516
- },
517
- text: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
518
- const baseSchema = {
598
+ }, {}),
599
+ localized: true
600
+ };
601
+ } else if (hasManyRelations) {
602
+ schemaToReturn = {
519
603
  ...formatBaseSchema({
520
604
  buildSchemaOptions,
521
605
  field,
522
606
  parentIsLocalized
523
607
  }),
524
- type: field.hasMany ? [
525
- String
526
- ] : String
608
+ _id: false,
609
+ type: mongoose.Schema.Types.Mixed,
610
+ relationTo: {
611
+ type: String,
612
+ enum: field.relationTo
613
+ },
614
+ value: {
615
+ type: valueType,
616
+ refPath: `${field.name}.relationTo`
617
+ }
527
618
  };
528
- schema.add({
529
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
530
- });
531
- },
532
- textarea: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
533
- const baseSchema = {
619
+ if (field.hasMany) {
620
+ schemaToReturn = {
621
+ type: [
622
+ schemaToReturn
623
+ ],
624
+ default: formatDefaultValue(field)
625
+ };
626
+ }
627
+ } else {
628
+ schemaToReturn = {
534
629
  ...formatBaseSchema({
535
630
  buildSchemaOptions,
536
631
  field,
537
632
  parentIsLocalized
538
633
  }),
539
- type: String
634
+ type: valueType,
635
+ ref: field.relationTo
540
636
  };
541
- schema.add({
542
- [field.name]: localizeSchema(field, baseSchema, payload.config.localization, parentIsLocalized)
543
- });
544
- },
545
- upload: (field, schema, payload, buildSchemaOptions, parentIsLocalized)=>{
546
- const hasManyRelations = Array.isArray(field.relationTo);
547
- let schemaToReturn = {};
548
- const valueType = getRelationshipValueType(field, payload);
549
- if (fieldShouldBeLocalized({
550
- field,
551
- parentIsLocalized
552
- }) && payload.config.localization) {
637
+ if (field.hasMany) {
553
638
  schemaToReturn = {
554
- _id: false,
555
- type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
556
- let localeSchema = {};
557
- if (hasManyRelations) {
558
- localeSchema = {
559
- ...formatBaseSchema({
560
- buildSchemaOptions,
561
- field,
562
- parentIsLocalized
563
- }),
564
- _id: false,
565
- type: mongoose.Schema.Types.Mixed,
566
- relationTo: {
567
- type: String,
568
- enum: field.relationTo
569
- },
570
- value: {
571
- type: valueType,
572
- refPath: `${field.name}.${locale}.relationTo`
573
- }
574
- };
575
- } else {
576
- localeSchema = {
577
- ...formatBaseSchema({
578
- buildSchemaOptions,
579
- field,
580
- parentIsLocalized
581
- }),
582
- type: valueType,
583
- ref: field.relationTo
584
- };
585
- }
586
- return {
587
- ...locales,
588
- [locale]: field.hasMany ? {
589
- type: [
590
- localeSchema
591
- ],
592
- default: formatDefaultValue(field)
593
- } : localeSchema
594
- };
595
- }, {}),
596
- localized: true
597
- };
598
- } else if (hasManyRelations) {
599
- schemaToReturn = {
600
- ...formatBaseSchema({
601
- buildSchemaOptions,
602
- field,
603
- parentIsLocalized
604
- }),
605
- _id: false,
606
- type: mongoose.Schema.Types.Mixed,
607
- relationTo: {
608
- type: String,
609
- enum: field.relationTo
610
- },
611
- value: {
612
- type: valueType,
613
- refPath: `${field.name}.relationTo`
614
- }
615
- };
616
- if (field.hasMany) {
617
- schemaToReturn = {
618
- type: [
619
- schemaToReturn
620
- ],
621
- default: formatDefaultValue(field)
622
- };
623
- }
624
- } else {
625
- schemaToReturn = {
626
- ...formatBaseSchema({
627
- buildSchemaOptions,
628
- field,
629
- parentIsLocalized
630
- }),
631
- type: valueType,
632
- ref: field.relationTo
639
+ type: [
640
+ schemaToReturn
641
+ ],
642
+ default: formatDefaultValue(field)
633
643
  };
634
- if (field.hasMany) {
635
- schemaToReturn = {
636
- type: [
637
- schemaToReturn
638
- ],
639
- default: formatDefaultValue(field)
640
- };
641
- }
642
644
  }
643
- schema.add({
644
- [field.name]: schemaToReturn
645
- });
646
645
  }
646
+ schema.add({
647
+ [field.name]: schemaToReturn
648
+ });
649
+ };
650
+ const getSchemaGenerator = (fieldType)=>{
651
+ if (fieldType in fieldToSchemaMap) {
652
+ return fieldToSchemaMap[fieldType];
653
+ }
654
+ return null;
655
+ };
656
+ const fieldToSchemaMap = {
657
+ array,
658
+ blocks,
659
+ checkbox,
660
+ code,
661
+ collapsible,
662
+ date,
663
+ email,
664
+ group,
665
+ json,
666
+ number,
667
+ point,
668
+ radio,
669
+ relationship,
670
+ richText,
671
+ row,
672
+ select,
673
+ tabs,
674
+ text,
675
+ textarea,
676
+ upload
647
677
  };
648
678
  const getRelationshipValueType = (field, payload)=>{
649
679
  if (typeof field.relationTo === 'string') {
650
- const { customIDType } = payload.collections[field.relationTo];
680
+ const customIDType = payload.collections[field.relationTo]?.customIDType;
651
681
  if (!customIDType) {
652
682
  return mongoose.Schema.Types.ObjectId;
653
683
  }
@@ -658,7 +688,7 @@ const getRelationshipValueType = (field, payload)=>{
658
688
  }
659
689
  // has custom id relationTo
660
690
  if (field.relationTo.some((relationTo)=>{
661
- return !!payload.collections[relationTo].customIDType;
691
+ return !!payload.collections[relationTo]?.customIDType;
662
692
  })) {
663
693
  return mongoose.Schema.Types.Mixed;
664
694
  }