@payloadcms/db-mongodb 3.0.0-canary.b750ebf → 3.0.0-canary.b8c9483

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. package/dist/connect.d.ts.map +1 -1
  2. package/dist/connect.js +17 -3
  3. package/dist/connect.js.map +1 -1
  4. package/dist/count.d.ts.map +1 -1
  5. package/dist/count.js +8 -1
  6. package/dist/count.js.map +1 -1
  7. package/dist/countGlobalVersions.d.ts +3 -0
  8. package/dist/countGlobalVersions.d.ts.map +1 -0
  9. package/dist/countGlobalVersions.js +40 -0
  10. package/dist/countGlobalVersions.js.map +1 -0
  11. package/dist/countVersions.d.ts +3 -0
  12. package/dist/countVersions.d.ts.map +1 -0
  13. package/dist/countVersions.js +40 -0
  14. package/dist/countVersions.js.map +1 -0
  15. package/dist/create.d.ts.map +1 -1
  16. package/dist/create.js +10 -1
  17. package/dist/create.js.map +1 -1
  18. package/dist/createGlobal.d.ts.map +1 -1
  19. package/dist/createGlobal.js +10 -5
  20. package/dist/createGlobal.js.map +1 -1
  21. package/dist/createGlobalVersion.d.ts +1 -1
  22. package/dist/createGlobalVersion.d.ts.map +1 -1
  23. package/dist/createGlobalVersion.js +13 -4
  24. package/dist/createGlobalVersion.js.map +1 -1
  25. package/dist/createMigration.d.ts.map +1 -1
  26. package/dist/createMigration.js +8 -6
  27. package/dist/createMigration.js.map +1 -1
  28. package/dist/createVersion.d.ts +1 -1
  29. package/dist/createVersion.d.ts.map +1 -1
  30. package/dist/createVersion.js +31 -9
  31. package/dist/createVersion.js.map +1 -1
  32. package/dist/deleteOne.d.ts.map +1 -1
  33. package/dist/deleteOne.js +11 -3
  34. package/dist/deleteOne.js.map +1 -1
  35. package/dist/exports/migration-utils.d.ts +3 -0
  36. package/dist/exports/migration-utils.d.ts.map +1 -0
  37. package/dist/exports/migration-utils.js +4 -0
  38. package/dist/exports/migration-utils.js.map +1 -0
  39. package/dist/find.d.ts.map +1 -1
  40. package/dist/find.js +34 -4
  41. package/dist/find.js.map +1 -1
  42. package/dist/findGlobal.d.ts.map +1 -1
  43. package/dist/findGlobal.js +9 -3
  44. package/dist/findGlobal.js.map +1 -1
  45. package/dist/findGlobalVersions.d.ts.map +1 -1
  46. package/dist/findGlobalVersions.js +17 -5
  47. package/dist/findGlobalVersions.js.map +1 -1
  48. package/dist/findOne.d.ts.map +1 -1
  49. package/dist/findOne.js +27 -3
  50. package/dist/findOne.js.map +1 -1
  51. package/dist/findVersions.d.ts.map +1 -1
  52. package/dist/findVersions.js +16 -4
  53. package/dist/findVersions.js.map +1 -1
  54. package/dist/index.d.ts +64 -6
  55. package/dist/index.d.ts.map +1 -1
  56. package/dist/index.js +18 -7
  57. package/dist/index.js.map +1 -1
  58. package/dist/init.d.ts.map +1 -1
  59. package/dist/init.js +20 -18
  60. package/dist/init.js.map +1 -1
  61. package/dist/models/buildCollectionSchema.d.ts +2 -3
  62. package/dist/models/buildCollectionSchema.d.ts.map +1 -1
  63. package/dist/models/buildCollectionSchema.js +19 -7
  64. package/dist/models/buildCollectionSchema.js.map +1 -1
  65. package/dist/models/buildGlobalModel.d.ts +2 -2
  66. package/dist/models/buildGlobalModel.d.ts.map +1 -1
  67. package/dist/models/buildGlobalModel.js +6 -6
  68. package/dist/models/buildGlobalModel.js.map +1 -1
  69. package/dist/models/buildSchema.d.ts +2 -3
  70. package/dist/models/buildSchema.d.ts.map +1 -1
  71. package/dist/models/buildSchema.js +190 -74
  72. package/dist/models/buildSchema.js.map +1 -1
  73. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts +6 -0
  74. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -0
  75. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +141 -0
  76. package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -0
  77. package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts +5 -0
  78. package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts.map +1 -0
  79. package/dist/predefinedMigrations/migrateVersionsV1_V2.js +107 -0
  80. package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -0
  81. package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
  82. package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
  83. package/dist/predefinedMigrations/relationships-v2-v3.js +9 -0
  84. package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
  85. package/dist/predefinedMigrations/versions-v1-v2.d.ts +4 -0
  86. package/dist/predefinedMigrations/versions-v1-v2.d.ts.map +1 -0
  87. package/dist/predefinedMigrations/versions-v1-v2.js +5 -95
  88. package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
  89. package/dist/queries/buildQuery.d.ts +2 -2
  90. package/dist/queries/buildQuery.d.ts.map +1 -1
  91. package/dist/queries/buildQuery.js +1 -2
  92. package/dist/queries/buildQuery.js.map +1 -1
  93. package/dist/queries/buildSearchParams.d.ts.map +1 -1
  94. package/dist/queries/buildSearchParams.js +29 -18
  95. package/dist/queries/buildSearchParams.js.map +1 -1
  96. package/dist/queries/buildSortParam.d.ts +2 -2
  97. package/dist/queries/buildSortParam.d.ts.map +1 -1
  98. package/dist/queries/buildSortParam.js +26 -17
  99. package/dist/queries/buildSortParam.js.map +1 -1
  100. package/dist/queries/getLocalizedSortProperty.spec.js +1 -1
  101. package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
  102. package/dist/queries/parseParams.d.ts.map +1 -1
  103. package/dist/queries/parseParams.js +7 -5
  104. package/dist/queries/parseParams.js.map +1 -1
  105. package/dist/queries/sanitizeQueryValue.d.ts +3 -2
  106. package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
  107. package/dist/queries/sanitizeQueryValue.js +263 -37
  108. package/dist/queries/sanitizeQueryValue.js.map +1 -1
  109. package/dist/queryDrafts.d.ts.map +1 -1
  110. package/dist/queryDrafts.js +36 -8
  111. package/dist/queryDrafts.js.map +1 -1
  112. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  113. package/dist/transactions/beginTransaction.js +2 -0
  114. package/dist/transactions/beginTransaction.js.map +1 -1
  115. package/dist/transactions/commitTransaction.d.ts.map +1 -1
  116. package/dist/transactions/commitTransaction.js +3 -1
  117. package/dist/transactions/commitTransaction.js.map +1 -1
  118. package/dist/types.d.ts +7 -4
  119. package/dist/types.d.ts.map +1 -1
  120. package/dist/types.js.map +1 -1
  121. package/dist/updateGlobal.d.ts.map +1 -1
  122. package/dist/updateGlobal.js +18 -4
  123. package/dist/updateGlobal.js.map +1 -1
  124. package/dist/updateGlobalVersion.d.ts +2 -2
  125. package/dist/updateGlobalVersion.d.ts.map +1 -1
  126. package/dist/updateGlobalVersion.js +19 -4
  127. package/dist/updateGlobalVersion.js.map +1 -1
  128. package/dist/updateOne.d.ts.map +1 -1
  129. package/dist/updateOne.js +18 -4
  130. package/dist/updateOne.js.map +1 -1
  131. package/dist/updateVersion.d.ts +1 -1
  132. package/dist/updateVersion.d.ts.map +1 -1
  133. package/dist/updateVersion.js +18 -3
  134. package/dist/updateVersion.js.map +1 -1
  135. package/dist/upsert.d.ts +3 -0
  136. package/dist/upsert.d.ts.map +1 -0
  137. package/dist/upsert.js +15 -0
  138. package/dist/upsert.js.map +1 -0
  139. package/dist/utilities/buildJoinAggregation.d.ts +18 -0
  140. package/dist/utilities/buildJoinAggregation.d.ts.map +1 -0
  141. package/dist/utilities/buildJoinAggregation.js +159 -0
  142. package/dist/utilities/buildJoinAggregation.js.map +1 -0
  143. package/dist/utilities/buildProjectionFromSelect.d.ts +8 -0
  144. package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -0
  145. package/dist/utilities/buildProjectionFromSelect.js +171 -0
  146. package/dist/utilities/buildProjectionFromSelect.js.map +1 -0
  147. package/dist/utilities/getDBName.d.ts.map +1 -1
  148. package/dist/utilities/getDBName.js +3 -1
  149. package/dist/utilities/getDBName.js.map +1 -1
  150. package/dist/utilities/handleError.js +2 -2
  151. package/dist/utilities/handleError.js.map +1 -1
  152. package/dist/utilities/sanitizeInternalFields.d.ts +1 -2
  153. package/dist/utilities/sanitizeInternalFields.d.ts.map +1 -1
  154. package/dist/utilities/sanitizeInternalFields.js +1 -2
  155. package/dist/utilities/sanitizeInternalFields.js.map +1 -1
  156. package/dist/utilities/sanitizeRelationshipIDs.d.ts +9 -0
  157. package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -0
  158. package/dist/utilities/sanitizeRelationshipIDs.js +125 -0
  159. package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -0
  160. package/dist/utilities/sanitizeRelationshipIDs.spec.js +408 -0
  161. package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -0
  162. package/dist/withSession.d.ts +1 -1
  163. package/dist/withSession.d.ts.map +1 -1
  164. package/dist/withSession.js +5 -3
  165. package/dist/withSession.js.map +1 -1
  166. package/license.md +22 -0
  167. package/package.json +24 -12
  168. package/dist/queries/mock.js +0 -2
  169. package/dist/queries/mock.js.map +0 -1
@@ -1,8 +1,13 @@
1
1
  import mongoose from 'mongoose';
2
- import { fieldAffectsData, fieldIsLocalized, fieldIsPresentationalOnly, tabHasName } from 'payload/shared';
2
+ import { fieldAffectsData, fieldIsLocalized, fieldIsPresentationalOnly, fieldIsVirtual, tabHasName } from 'payload/shared';
3
+ /**
4
+ * get a field's defaultValue only if defined and not dynamic so that it can be set on the field schema
5
+ * @param field
6
+ */ const formatDefaultValue = (field)=>typeof field.defaultValue !== 'undefined' && typeof field.defaultValue !== 'function' ? field.defaultValue : undefined;
3
7
  const formatBaseSchema = (field, buildSchemaOptions)=>{
4
8
  const { disableUnique, draftsEnabled, indexSortableFields } = buildSchemaOptions;
5
9
  const schema = {
10
+ default: formatDefaultValue(field),
6
11
  index: field.index || !disableUnique && field.unique || indexSortableFields || false,
7
12
  required: false,
8
13
  unique: !disableUnique && field.unique || false
@@ -29,7 +34,7 @@ const localizeSchema = (entity, schema, localization)=>{
29
34
  }
30
35
  return schema;
31
36
  };
32
- const buildSchema = (config, configFields, buildSchemaOptions = {})=>{
37
+ export const buildSchema = (payload, configFields, buildSchemaOptions = {})=>{
33
38
  const { allowIDField, options } = buildSchemaOptions;
34
39
  let fields = {};
35
40
  let schemaFields = configFields;
@@ -44,21 +49,24 @@ const buildSchema = (config, configFields, buildSchemaOptions = {})=>{
44
49
  }
45
50
  const schema = new mongoose.Schema(fields, options);
46
51
  schemaFields.forEach((field)=>{
52
+ if (fieldIsVirtual(field)) {
53
+ return;
54
+ }
47
55
  if (!fieldIsPresentationalOnly(field)) {
48
56
  const addFieldSchema = fieldToSchemaMap[field.type];
49
57
  if (addFieldSchema) {
50
- addFieldSchema(field, schema, config, buildSchemaOptions);
58
+ addFieldSchema(field, schema, payload, buildSchemaOptions);
51
59
  }
52
60
  }
53
61
  });
54
62
  return schema;
55
63
  };
56
64
  const fieldToSchemaMap = {
57
- array: (field, schema, config, buildSchemaOptions)=>{
65
+ array: (field, schema, payload, buildSchemaOptions)=>{
58
66
  const baseSchema = {
59
67
  ...formatBaseSchema(field, buildSchemaOptions),
60
68
  type: [
61
- buildSchema(config, field.fields, {
69
+ buildSchema(payload, field.fields, {
62
70
  allowIDField: true,
63
71
  disableUnique: buildSchemaOptions.disableUnique,
64
72
  draftsEnabled: buildSchemaOptions.draftsEnabled,
@@ -68,25 +76,23 @@ const fieldToSchemaMap = {
68
76
  minimize: false
69
77
  }
70
78
  })
71
- ],
72
- default: undefined
79
+ ]
73
80
  };
74
81
  schema.add({
75
- [field.name]: localizeSchema(field, baseSchema, config.localization)
82
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
76
83
  });
77
84
  },
78
- blocks: (field, schema, config, buildSchemaOptions)=>{
85
+ blocks: (field, schema, payload, buildSchemaOptions)=>{
79
86
  const fieldSchema = {
80
87
  type: [
81
88
  new mongoose.Schema({}, {
82
89
  _id: false,
83
90
  discriminatorKey: 'blockType'
84
91
  })
85
- ],
86
- default: undefined
92
+ ]
87
93
  };
88
94
  schema.add({
89
- [field.name]: localizeSchema(field, fieldSchema, config.localization)
95
+ [field.name]: localizeSchema(field, fieldSchema, payload.config.localization)
90
96
  });
91
97
  field.blocks.forEach((blockItem)=>{
92
98
  const blockSchema = new mongoose.Schema({}, {
@@ -96,11 +102,11 @@ const fieldToSchemaMap = {
96
102
  blockItem.fields.forEach((blockField)=>{
97
103
  const addFieldSchema = fieldToSchemaMap[blockField.type];
98
104
  if (addFieldSchema) {
99
- addFieldSchema(blockField, blockSchema, config, buildSchemaOptions);
105
+ addFieldSchema(blockField, blockSchema, payload, buildSchemaOptions);
100
106
  }
101
107
  });
102
- if (field.localized && config.localization) {
103
- config.localization.localeCodes.forEach((localeCode)=>{
108
+ if (field.localized && payload.config.localization) {
109
+ payload.config.localization.localeCodes.forEach((localeCode)=>{
104
110
  // @ts-expect-error Possible incorrect typing in mongoose types, this works
105
111
  schema.path(`${field.name}.${localeCode}`).discriminator(blockItem.slug, blockSchema);
106
112
  });
@@ -110,57 +116,60 @@ const fieldToSchemaMap = {
110
116
  }
111
117
  });
112
118
  },
113
- checkbox: (field, schema, config, buildSchemaOptions)=>{
119
+ checkbox: (field, schema, payload, buildSchemaOptions)=>{
114
120
  const baseSchema = {
115
121
  ...formatBaseSchema(field, buildSchemaOptions),
116
122
  type: Boolean
117
123
  };
118
124
  schema.add({
119
- [field.name]: localizeSchema(field, baseSchema, config.localization)
125
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
120
126
  });
121
127
  },
122
- code: (field, schema, config, buildSchemaOptions)=>{
128
+ code: (field, schema, payload, buildSchemaOptions)=>{
123
129
  const baseSchema = {
124
130
  ...formatBaseSchema(field, buildSchemaOptions),
125
131
  type: String
126
132
  };
127
133
  schema.add({
128
- [field.name]: localizeSchema(field, baseSchema, config.localization)
134
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
129
135
  });
130
136
  },
131
- collapsible: (field, schema, config, buildSchemaOptions)=>{
137
+ collapsible: (field, schema, payload, buildSchemaOptions)=>{
132
138
  field.fields.forEach((subField)=>{
139
+ if (fieldIsVirtual(subField)) {
140
+ return;
141
+ }
133
142
  const addFieldSchema = fieldToSchemaMap[subField.type];
134
143
  if (addFieldSchema) {
135
- addFieldSchema(subField, schema, config, buildSchemaOptions);
144
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
136
145
  }
137
146
  });
138
147
  },
139
- date: (field, schema, config, buildSchemaOptions)=>{
148
+ date: (field, schema, payload, buildSchemaOptions)=>{
140
149
  const baseSchema = {
141
150
  ...formatBaseSchema(field, buildSchemaOptions),
142
151
  type: Date
143
152
  };
144
153
  schema.add({
145
- [field.name]: localizeSchema(field, baseSchema, config.localization)
154
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
146
155
  });
147
156
  },
148
- email: (field, schema, config, buildSchemaOptions)=>{
157
+ email: (field, schema, payload, buildSchemaOptions)=>{
149
158
  const baseSchema = {
150
159
  ...formatBaseSchema(field, buildSchemaOptions),
151
160
  type: String
152
161
  };
153
162
  schema.add({
154
- [field.name]: localizeSchema(field, baseSchema, config.localization)
163
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
155
164
  });
156
165
  },
157
- group: (field, schema, config, buildSchemaOptions)=>{
166
+ group: (field, schema, payload, buildSchemaOptions)=>{
158
167
  const formattedBaseSchema = formatBaseSchema(field, buildSchemaOptions);
159
168
  // carry indexSortableFields through to versions if drafts enabled
160
169
  const indexSortableFields = buildSchemaOptions.indexSortableFields && field.name === 'version' && buildSchemaOptions.draftsEnabled;
161
170
  const baseSchema = {
162
171
  ...formattedBaseSchema,
163
- type: buildSchema(config, field.fields, {
172
+ type: buildSchema(payload, field.fields, {
164
173
  disableUnique: buildSchemaOptions.disableUnique,
165
174
  draftsEnabled: buildSchemaOptions.draftsEnabled,
166
175
  indexSortableFields,
@@ -172,19 +181,19 @@ const fieldToSchemaMap = {
172
181
  })
173
182
  };
174
183
  schema.add({
175
- [field.name]: localizeSchema(field, baseSchema, config.localization)
184
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
176
185
  });
177
186
  },
178
- json: (field, schema, config, buildSchemaOptions)=>{
187
+ json: (field, schema, payload, buildSchemaOptions)=>{
179
188
  const baseSchema = {
180
189
  ...formatBaseSchema(field, buildSchemaOptions),
181
190
  type: mongoose.Schema.Types.Mixed
182
191
  };
183
192
  schema.add({
184
- [field.name]: localizeSchema(field, baseSchema, config.localization)
193
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
185
194
  });
186
195
  },
187
- number: (field, schema, config, buildSchemaOptions)=>{
196
+ number: (field, schema, payload, buildSchemaOptions)=>{
188
197
  const baseSchema = {
189
198
  ...formatBaseSchema(field, buildSchemaOptions),
190
199
  type: field.hasMany ? [
@@ -192,22 +201,25 @@ const fieldToSchemaMap = {
192
201
  ] : Number
193
202
  };
194
203
  schema.add({
195
- [field.name]: localizeSchema(field, baseSchema, config.localization)
204
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
196
205
  });
197
206
  },
198
- point: (field, schema, config, buildSchemaOptions)=>{
207
+ point: (field, schema, payload, buildSchemaOptions)=>{
199
208
  const baseSchema = {
200
209
  type: {
201
210
  type: String,
202
211
  enum: [
203
212
  'Point'
204
- ]
213
+ ],
214
+ ...typeof field.defaultValue !== 'undefined' && {
215
+ default: 'Point'
216
+ }
205
217
  },
206
218
  coordinates: {
207
219
  type: [
208
220
  Number
209
221
  ],
210
- default: field.defaultValue || undefined,
222
+ default: formatDefaultValue(field),
211
223
  required: false
212
224
  }
213
225
  };
@@ -215,7 +227,7 @@ const fieldToSchemaMap = {
215
227
  baseSchema.coordinates.sparse = true;
216
228
  }
217
229
  schema.add({
218
- [field.name]: localizeSchema(field, baseSchema, config.localization)
230
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
219
231
  });
220
232
  if (field.index === true || field.index === undefined) {
221
233
  const indexOptions = {};
@@ -223,8 +235,8 @@ const fieldToSchemaMap = {
223
235
  indexOptions.sparse = true;
224
236
  indexOptions.unique = true;
225
237
  }
226
- if (field.localized && config.localization) {
227
- config.localization.locales.forEach((locale)=>{
238
+ if (field.localized && payload.config.localization) {
239
+ payload.config.localization.locales.forEach((locale)=>{
228
240
  schema.index({
229
241
  [`${field.name}.${locale.code}`]: '2dsphere'
230
242
  }, indexOptions);
@@ -236,25 +248,28 @@ const fieldToSchemaMap = {
236
248
  }
237
249
  }
238
250
  },
239
- radio: (field, schema, config, buildSchemaOptions)=>{
251
+ radio: (field, schema, payload, buildSchemaOptions)=>{
240
252
  const baseSchema = {
241
253
  ...formatBaseSchema(field, buildSchemaOptions),
242
254
  type: String,
243
255
  enum: field.options.map((option)=>{
244
- if (typeof option === 'object') return option.value;
256
+ if (typeof option === 'object') {
257
+ return option.value;
258
+ }
245
259
  return option;
246
260
  })
247
261
  };
248
262
  schema.add({
249
- [field.name]: localizeSchema(field, baseSchema, config.localization)
263
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
250
264
  });
251
265
  },
252
- relationship: (field, schema, config, buildSchemaOptions)=>{
266
+ relationship: (field, schema, payload, buildSchemaOptions)=>{
253
267
  const hasManyRelations = Array.isArray(field.relationTo);
254
268
  let schemaToReturn = {};
255
- if (field.localized && config.localization) {
269
+ const valueType = getRelationshipValueType(field, payload);
270
+ if (field.localized && payload.config.localization) {
256
271
  schemaToReturn = {
257
- type: config.localization.localeCodes.reduce((locales, locale)=>{
272
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
258
273
  let localeSchema = {};
259
274
  if (hasManyRelations) {
260
275
  localeSchema = {
@@ -266,14 +281,14 @@ const fieldToSchemaMap = {
266
281
  enum: field.relationTo
267
282
  },
268
283
  value: {
269
- type: mongoose.Schema.Types.Mixed,
284
+ type: valueType,
270
285
  refPath: `${field.name}.${locale}.relationTo`
271
286
  }
272
287
  };
273
288
  } else {
274
289
  localeSchema = {
275
290
  ...formatBaseSchema(field, buildSchemaOptions),
276
- type: mongoose.Schema.Types.Mixed,
291
+ type: valueType,
277
292
  ref: field.relationTo
278
293
  };
279
294
  }
@@ -283,7 +298,7 @@ const fieldToSchemaMap = {
283
298
  type: [
284
299
  localeSchema
285
300
  ],
286
- default: undefined
301
+ default: formatDefaultValue(field)
287
302
  } : localeSchema
288
303
  };
289
304
  }, {}),
@@ -299,7 +314,7 @@ const fieldToSchemaMap = {
299
314
  enum: field.relationTo
300
315
  },
301
316
  value: {
302
- type: mongoose.Schema.Types.Mixed,
317
+ type: valueType,
303
318
  refPath: `${field.name}.relationTo`
304
319
  }
305
320
  };
@@ -308,13 +323,13 @@ const fieldToSchemaMap = {
308
323
  type: [
309
324
  schemaToReturn
310
325
  ],
311
- default: undefined
326
+ default: formatDefaultValue(field)
312
327
  };
313
328
  }
314
329
  } else {
315
330
  schemaToReturn = {
316
331
  ...formatBaseSchema(field, buildSchemaOptions),
317
- type: mongoose.Schema.Types.Mixed,
332
+ type: valueType,
318
333
  ref: field.relationTo
319
334
  };
320
335
  if (field.hasMany) {
@@ -322,7 +337,7 @@ const fieldToSchemaMap = {
322
337
  type: [
323
338
  schemaToReturn
324
339
  ],
325
- default: undefined
340
+ default: formatDefaultValue(field)
326
341
  };
327
342
  }
328
343
  }
@@ -330,29 +345,34 @@ const fieldToSchemaMap = {
330
345
  [field.name]: schemaToReturn
331
346
  });
332
347
  },
333
- richText: (field, schema, config, buildSchemaOptions)=>{
348
+ richText: (field, schema, payload, buildSchemaOptions)=>{
334
349
  const baseSchema = {
335
350
  ...formatBaseSchema(field, buildSchemaOptions),
336
351
  type: mongoose.Schema.Types.Mixed
337
352
  };
338
353
  schema.add({
339
- [field.name]: localizeSchema(field, baseSchema, config.localization)
354
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
340
355
  });
341
356
  },
342
- row: (field, schema, config, buildSchemaOptions)=>{
357
+ row: (field, schema, payload, buildSchemaOptions)=>{
343
358
  field.fields.forEach((subField)=>{
359
+ if (fieldIsVirtual(subField)) {
360
+ return;
361
+ }
344
362
  const addFieldSchema = fieldToSchemaMap[subField.type];
345
363
  if (addFieldSchema) {
346
- addFieldSchema(subField, schema, config, buildSchemaOptions);
364
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
347
365
  }
348
366
  });
349
367
  },
350
- select: (field, schema, config, buildSchemaOptions)=>{
368
+ select: (field, schema, payload, buildSchemaOptions)=>{
351
369
  const baseSchema = {
352
370
  ...formatBaseSchema(field, buildSchemaOptions),
353
371
  type: String,
354
372
  enum: field.options.map((option)=>{
355
- if (typeof option === 'object') return option.value;
373
+ if (typeof option === 'object') {
374
+ return option.value;
375
+ }
356
376
  return option;
357
377
  })
358
378
  };
@@ -362,14 +382,17 @@ const fieldToSchemaMap = {
362
382
  schema.add({
363
383
  [field.name]: localizeSchema(field, field.hasMany ? [
364
384
  baseSchema
365
- ] : baseSchema, config.localization)
385
+ ] : baseSchema, payload.config.localization)
366
386
  });
367
387
  },
368
- tabs: (field, schema, config, buildSchemaOptions)=>{
388
+ tabs: (field, schema, payload, buildSchemaOptions)=>{
369
389
  field.tabs.forEach((tab)=>{
370
390
  if (tabHasName(tab)) {
391
+ if (fieldIsVirtual(tab)) {
392
+ return;
393
+ }
371
394
  const baseSchema = {
372
- type: buildSchema(config, tab.fields, {
395
+ type: buildSchema(payload, tab.fields, {
373
396
  disableUnique: buildSchemaOptions.disableUnique,
374
397
  draftsEnabled: buildSchemaOptions.draftsEnabled,
375
398
  options: {
@@ -380,19 +403,22 @@ const fieldToSchemaMap = {
380
403
  })
381
404
  };
382
405
  schema.add({
383
- [tab.name]: localizeSchema(tab, baseSchema, config.localization)
406
+ [tab.name]: localizeSchema(tab, baseSchema, payload.config.localization)
384
407
  });
385
408
  } else {
386
409
  tab.fields.forEach((subField)=>{
410
+ if (fieldIsVirtual(subField)) {
411
+ return;
412
+ }
387
413
  const addFieldSchema = fieldToSchemaMap[subField.type];
388
414
  if (addFieldSchema) {
389
- addFieldSchema(subField, schema, config, buildSchemaOptions);
415
+ addFieldSchema(subField, schema, payload, buildSchemaOptions);
390
416
  }
391
417
  });
392
418
  }
393
419
  });
394
420
  },
395
- text: (field, schema, config, buildSchemaOptions)=>{
421
+ text: (field, schema, payload, buildSchemaOptions)=>{
396
422
  const baseSchema = {
397
423
  ...formatBaseSchema(field, buildSchemaOptions),
398
424
  type: field.hasMany ? [
@@ -400,29 +426,119 @@ const fieldToSchemaMap = {
400
426
  ] : String
401
427
  };
402
428
  schema.add({
403
- [field.name]: localizeSchema(field, baseSchema, config.localization)
429
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
404
430
  });
405
431
  },
406
- textarea: (field, schema, config, buildSchemaOptions)=>{
432
+ textarea: (field, schema, payload, buildSchemaOptions)=>{
407
433
  const baseSchema = {
408
434
  ...formatBaseSchema(field, buildSchemaOptions),
409
435
  type: String
410
436
  };
411
437
  schema.add({
412
- [field.name]: localizeSchema(field, baseSchema, config.localization)
438
+ [field.name]: localizeSchema(field, baseSchema, payload.config.localization)
413
439
  });
414
440
  },
415
- upload: (field, schema, config, buildSchemaOptions)=>{
416
- const baseSchema = {
417
- ...formatBaseSchema(field, buildSchemaOptions),
418
- type: mongoose.Schema.Types.Mixed,
419
- ref: field.relationTo
420
- };
441
+ upload: (field, schema, payload, buildSchemaOptions)=>{
442
+ const hasManyRelations = Array.isArray(field.relationTo);
443
+ let schemaToReturn = {};
444
+ const valueType = getRelationshipValueType(field, payload);
445
+ if (field.localized && payload.config.localization) {
446
+ schemaToReturn = {
447
+ type: payload.config.localization.localeCodes.reduce((locales, locale)=>{
448
+ let localeSchema = {};
449
+ if (hasManyRelations) {
450
+ localeSchema = {
451
+ ...formatBaseSchema(field, buildSchemaOptions),
452
+ _id: false,
453
+ type: mongoose.Schema.Types.Mixed,
454
+ relationTo: {
455
+ type: String,
456
+ enum: field.relationTo
457
+ },
458
+ value: {
459
+ type: valueType,
460
+ refPath: `${field.name}.${locale}.relationTo`
461
+ }
462
+ };
463
+ } else {
464
+ localeSchema = {
465
+ ...formatBaseSchema(field, buildSchemaOptions),
466
+ type: valueType,
467
+ ref: field.relationTo
468
+ };
469
+ }
470
+ return {
471
+ ...locales,
472
+ [locale]: field.hasMany ? {
473
+ type: [
474
+ localeSchema
475
+ ],
476
+ default: formatDefaultValue(field)
477
+ } : localeSchema
478
+ };
479
+ }, {}),
480
+ localized: true
481
+ };
482
+ } else if (hasManyRelations) {
483
+ schemaToReturn = {
484
+ ...formatBaseSchema(field, buildSchemaOptions),
485
+ _id: false,
486
+ type: mongoose.Schema.Types.Mixed,
487
+ relationTo: {
488
+ type: String,
489
+ enum: field.relationTo
490
+ },
491
+ value: {
492
+ type: valueType,
493
+ refPath: `${field.name}.relationTo`
494
+ }
495
+ };
496
+ if (field.hasMany) {
497
+ schemaToReturn = {
498
+ type: [
499
+ schemaToReturn
500
+ ],
501
+ default: formatDefaultValue(field)
502
+ };
503
+ }
504
+ } else {
505
+ schemaToReturn = {
506
+ ...formatBaseSchema(field, buildSchemaOptions),
507
+ type: valueType,
508
+ ref: field.relationTo
509
+ };
510
+ if (field.hasMany) {
511
+ schemaToReturn = {
512
+ type: [
513
+ schemaToReturn
514
+ ],
515
+ default: formatDefaultValue(field)
516
+ };
517
+ }
518
+ }
421
519
  schema.add({
422
- [field.name]: localizeSchema(field, baseSchema, config.localization)
520
+ [field.name]: schemaToReturn
423
521
  });
424
522
  }
425
523
  };
426
- export default buildSchema;
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
+ };
427
543
 
428
544
  //# sourceMappingURL=buildSchema.js.map