@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.
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +17 -3
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +8 -1
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts +3 -0
- package/dist/countGlobalVersions.d.ts.map +1 -0
- package/dist/countGlobalVersions.js +40 -0
- package/dist/countGlobalVersions.js.map +1 -0
- package/dist/countVersions.d.ts +3 -0
- package/dist/countVersions.d.ts.map +1 -0
- package/dist/countVersions.js +40 -0
- package/dist/countVersions.js.map +1 -0
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +10 -1
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +10 -5
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +13 -4
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +8 -6
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +31 -9
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +11 -3
- package/dist/deleteOne.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +3 -0
- package/dist/exports/migration-utils.d.ts.map +1 -0
- package/dist/exports/migration-utils.js +4 -0
- package/dist/exports/migration-utils.js.map +1 -0
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +34 -4
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +9 -3
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +17 -5
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +27 -3
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +16 -4
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +64 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -7
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +20 -18
- package/dist/init.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +2 -3
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +19 -7
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +2 -2
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +6 -6
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +2 -3
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +190 -74
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts +6 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +141 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -0
- package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts +5 -0
- package/dist/predefinedMigrations/migrateVersionsV1_V2.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js +107 -0
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.js +9 -0
- package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
- package/dist/predefinedMigrations/versions-v1-v2.d.ts +4 -0
- package/dist/predefinedMigrations/versions-v1-v2.d.ts.map +1 -0
- package/dist/predefinedMigrations/versions-v1-v2.js +5 -95
- package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +2 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +1 -2
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +29 -18
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/buildSortParam.d.ts +2 -2
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.js +26 -17
- package/dist/queries/buildSortParam.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +7 -5
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +3 -2
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +263 -37
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +36 -8
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +2 -0
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +3 -1
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/types.d.ts +7 -4
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +18 -4
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +2 -2
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +19 -4
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +18 -4
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +18 -3
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsert.d.ts +3 -0
- package/dist/upsert.d.ts.map +1 -0
- package/dist/upsert.js +15 -0
- package/dist/upsert.js.map +1 -0
- package/dist/utilities/buildJoinAggregation.d.ts +18 -0
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -0
- package/dist/utilities/buildJoinAggregation.js +159 -0
- package/dist/utilities/buildJoinAggregation.js.map +1 -0
- package/dist/utilities/buildProjectionFromSelect.d.ts +8 -0
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -0
- package/dist/utilities/buildProjectionFromSelect.js +171 -0
- package/dist/utilities/buildProjectionFromSelect.js.map +1 -0
- package/dist/utilities/getDBName.d.ts.map +1 -1
- package/dist/utilities/getDBName.js +3 -1
- package/dist/utilities/getDBName.js.map +1 -1
- package/dist/utilities/handleError.js +2 -2
- package/dist/utilities/handleError.js.map +1 -1
- package/dist/utilities/sanitizeInternalFields.d.ts +1 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +1 -1
- package/dist/utilities/sanitizeInternalFields.js +1 -2
- package/dist/utilities/sanitizeInternalFields.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +9 -0
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -0
- package/dist/utilities/sanitizeRelationshipIDs.js +125 -0
- package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -0
- package/dist/utilities/sanitizeRelationshipIDs.spec.js +408 -0
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -0
- package/dist/withSession.d.ts +1 -1
- package/dist/withSession.d.ts.map +1 -1
- package/dist/withSession.js +5 -3
- package/dist/withSession.js.map +1 -1
- package/license.md +22 -0
- package/package.json +24 -12
- package/dist/queries/mock.js +0 -2
- 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 = (
|
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,
|
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,
|
65
|
+
array: (field, schema, payload, buildSchemaOptions)=>{
|
58
66
|
const baseSchema = {
|
59
67
|
...formatBaseSchema(field, buildSchemaOptions),
|
60
68
|
type: [
|
61
|
-
buildSchema(
|
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,
|
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,
|
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,
|
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,
|
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,
|
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,
|
144
|
+
addFieldSchema(subField, schema, payload, buildSchemaOptions);
|
136
145
|
}
|
137
146
|
});
|
138
147
|
},
|
139
|
-
date: (field, schema,
|
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,
|
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,
|
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(
|
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,
|
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,
|
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,
|
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
|
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,
|
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')
|
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,
|
266
|
+
relationship: (field, schema, payload, buildSchemaOptions)=>{
|
253
267
|
const hasManyRelations = Array.isArray(field.relationTo);
|
254
268
|
let schemaToReturn = {};
|
255
|
-
|
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:
|
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:
|
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:
|
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:
|
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:
|
326
|
+
default: formatDefaultValue(field)
|
312
327
|
};
|
313
328
|
}
|
314
329
|
} else {
|
315
330
|
schemaToReturn = {
|
316
331
|
...formatBaseSchema(field, buildSchemaOptions),
|
317
|
-
type:
|
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:
|
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,
|
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,
|
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,
|
364
|
+
addFieldSchema(subField, schema, payload, buildSchemaOptions);
|
347
365
|
}
|
348
366
|
});
|
349
367
|
},
|
350
|
-
select: (field, schema,
|
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')
|
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,
|
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(
|
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,
|
415
|
+
addFieldSchema(subField, schema, payload, buildSchemaOptions);
|
390
416
|
}
|
391
417
|
});
|
392
418
|
}
|
393
419
|
});
|
394
420
|
},
|
395
|
-
text: (field, schema,
|
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,
|
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,
|
416
|
-
const
|
417
|
-
|
418
|
-
|
419
|
-
|
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]:
|
520
|
+
[field.name]: schemaToReturn
|
423
521
|
});
|
424
522
|
}
|
425
523
|
};
|
426
|
-
|
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
|