-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathMongoSchemaCollection.js
More file actions
310 lines (288 loc) · 8.97 KB
/
MongoSchemaCollection.js
File metadata and controls
310 lines (288 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import MongoCollection from './MongoCollection';
import Parse from 'parse/node';
function mongoFieldToParseSchemaField(type) {
if (type[0] === '*') {
return {
type: 'Pointer',
targetClass: type.slice(1),
};
}
if (type.startsWith('relation<')) {
return {
type: 'Relation',
targetClass: type.slice('relation<'.length, type.length - 1),
};
}
switch (type) {
case 'number':
return { type: 'Number' };
case 'string':
return { type: 'String' };
case 'boolean':
return { type: 'Boolean' };
case 'date':
return { type: 'Date' };
case 'map':
case 'object':
return { type: 'Object' };
case 'array':
return { type: 'Array' };
case 'geopoint':
return { type: 'GeoPoint' };
case 'file':
return { type: 'File' };
case 'bytes':
return { type: 'Bytes' };
case 'polygon':
return { type: 'Polygon' };
case 'decimal128':
return { type: 'Decimal128' };
}
}
const nonFieldSchemaKeys = ['_id', '_metadata', '_client_permissions'];
function mongoSchemaFieldsToParseSchemaFields(schema) {
var fieldNames = Object.keys(schema).filter(key => nonFieldSchemaKeys.indexOf(key) === -1);
var response = fieldNames.reduce((obj, fieldName) => {
obj[fieldName] = mongoFieldToParseSchemaField(schema[fieldName]);
if (
schema._metadata &&
schema._metadata.fields_options &&
schema._metadata.fields_options[fieldName]
) {
obj[fieldName] = Object.assign(
{},
obj[fieldName],
schema._metadata.fields_options[fieldName]
);
}
return obj;
}, {});
response.ACL = { type: 'ACL' };
response.createdAt = { type: 'Date' };
response.updatedAt = { type: 'Date' };
response.objectId = { type: 'String' };
return response;
}
const emptyCLPS = Object.freeze({
find: {},
count: {},
get: {},
create: {},
update: {},
delete: {},
addField: {},
protectedFields: {},
});
const defaultCLPS = Object.freeze({
ACL: {
'*': {
read: true,
write: true,
},
},
find: { '*': true },
count: { '*': true },
get: { '*': true },
create: { '*': true },
update: { '*': true },
delete: { '*': true },
addField: { '*': true },
protectedFields: { '*': [] },
});
function mongoSchemaToParseSchema(mongoSchema) {
let clps = defaultCLPS;
let indexes = {};
if (mongoSchema._metadata) {
if (mongoSchema._metadata.class_permissions) {
clps = { ...emptyCLPS, ...mongoSchema._metadata.class_permissions };
}
if (mongoSchema._metadata.indexes) {
indexes = { ...mongoSchema._metadata.indexes };
}
}
return {
className: mongoSchema._id,
fields: mongoSchemaFieldsToParseSchemaFields(mongoSchema),
classLevelPermissions: clps,
indexes: indexes,
};
}
function _mongoSchemaQueryFromNameQuery(name: string, query) {
const object = { _id: name };
if (query) {
Object.keys(query).forEach(key => {
object[key] = query[key];
});
}
return object;
}
// Returns a type suitable for inserting into mongo _SCHEMA collection.
// Does no validation. That is expected to be done in Parse Server.
function parseFieldTypeToMongoFieldType({ type, targetClass }) {
switch (type) {
case 'Pointer':
return `*${targetClass}`;
case 'Relation':
return `relation<${targetClass}>`;
case 'Number':
return 'number';
case 'String':
return 'string';
case 'Boolean':
return 'boolean';
case 'Date':
return 'date';
case 'Object':
return 'object';
case 'Array':
return 'array';
case 'GeoPoint':
return 'geopoint';
case 'File':
return 'file';
case 'Bytes':
return 'bytes';
case 'Polygon':
return 'polygon';
case 'Decimal128':
return 'decimal128';
}
}
class MongoSchemaCollection {
_collection: MongoCollection;
constructor(collection: MongoCollection) {
this._collection = collection;
}
_fetchAllSchemasFrom_SCHEMA() {
return this._collection._rawFind({}).then(schemas => schemas.map(mongoSchemaToParseSchema));
}
_fetchOneSchemaFrom_SCHEMA(name: string) {
return this._collection
._rawFind(_mongoSchemaQueryFromNameQuery(name), { limit: 1 })
.then(results => {
if (results.length === 1) {
return mongoSchemaToParseSchema(results[0]);
} else {
throw undefined;
}
});
}
// Atomically find and delete an object based on query.
findAndDeleteSchema(name: string) {
return this._collection._mongoCollection.findOneAndDelete(_mongoSchemaQueryFromNameQuery(name));
}
insertSchema(schema: any) {
return this._collection
.insertOne(schema)
.then(() => mongoSchemaToParseSchema(schema))
.catch(error => {
if (error.code === 11000) {
//Mongo's duplicate key error
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'Class already exists.');
} else {
throw error;
}
});
}
updateSchema(name: string, update) {
return this._collection.updateOne(_mongoSchemaQueryFromNameQuery(name), update);
}
upsertSchema(name: string, query: string, update) {
return this._collection.upsertOne(_mongoSchemaQueryFromNameQuery(name, query), update);
}
// Add a field to the schema. If database does not support the field
// type (e.g. mongo doesn't support more than one GeoPoint in a class) reject with an "Incorrect Type"
// Parse error with a desciptive message. If the field already exists, this function must
// not modify the schema, and must reject with DUPLICATE_VALUE error.
// If this is called for a class that doesn't exist, this function must create that class.
// TODO: throw an error if an unsupported field type is passed. Deciding whether a type is supported
// should be the job of the adapter. Some adapters may not support GeoPoint at all. Others may
// Support additional types that Mongo doesn't, like Money, or something.
// TODO: don't spend an extra query on finding the schema if the type we are trying to add isn't a GeoPoint.
addFieldIfNotExists(className: string, fieldName: string, fieldType: string) {
return this._fetchOneSchemaFrom_SCHEMA(className)
.then(
schema => {
// If a field with this name already exists, it will be handled elsewhere.
if (schema.fields[fieldName] !== undefined) {
return;
}
// The schema exists. Check for existing GeoPoints.
if (fieldType.type === 'GeoPoint') {
// Make sure there are not other geopoint fields
if (
Object.keys(schema.fields).some(
existingField => schema.fields[existingField].type === 'GeoPoint'
)
) {
throw new Parse.Error(
Parse.Error.INCORRECT_TYPE,
'MongoDB only supports one GeoPoint field in a class.'
);
}
}
return;
},
error => {
// If error is undefined, the schema doesn't exist, and we can create the schema with the field.
// If some other error, reject with it.
if (error === undefined) {
return;
}
throw error;
}
)
.then(() => {
const { type, targetClass, ...fieldOptions } = fieldType;
// We use $exists and $set to avoid overwriting the field type if it
// already exists. (it could have added inbetween the last query and the update)
if (fieldOptions && Object.keys(fieldOptions).length > 0) {
return this.upsertSchema(
className,
{ [fieldName]: { $exists: false } },
{
$set: {
[fieldName]: parseFieldTypeToMongoFieldType({
type,
targetClass,
}),
[`_metadata.fields_options.${fieldName}`]: fieldOptions,
},
}
);
} else {
return this.upsertSchema(
className,
{ [fieldName]: { $exists: false } },
{
$set: {
[fieldName]: parseFieldTypeToMongoFieldType({
type,
targetClass,
}),
},
}
);
}
});
}
async updateFieldOptions(className: string, fieldName: string, fieldType: any) {
const { ...fieldOptions } = fieldType;
delete fieldOptions.type;
delete fieldOptions.targetClass;
await this.upsertSchema(
className,
{ [fieldName]: { $exists: true } },
{
$set: {
[`_metadata.fields_options.${fieldName}`]: fieldOptions,
},
}
);
}
}
// Exported for testing reasons and because we haven't moved all mongo schema format
// related logic into the database adapter yet.
MongoSchemaCollection._TESTmongoSchemaToParseSchema = mongoSchemaToParseSchema;
MongoSchemaCollection.parseFieldTypeToMongoFieldType = parseFieldTypeToMongoFieldType;
export default MongoSchemaCollection;