@payloadcms/db-mongodb 3.0.0-canary.5390729 → 3.0.0-canary.5624723

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