Skip to content

Commit fb5b8fb

Browse files
committed
Migrate Schema.js to adaptive mongo collection.
1 parent a163327 commit fb5b8fb

File tree

3 files changed

+25
-32
lines changed

3 files changed

+25
-32
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ export default class MongoCollection {
5454
return this._mongoCollection.findAndModify(query, [], update, { new: true }).then(document => {
5555
// Value is the object where mongo returns multiple fields.
5656
return document.value;
57-
})
57+
});
5858
}
5959

6060
insertOne(object) {
@@ -68,6 +68,10 @@ export default class MongoCollection {
6868
return this._mongoCollection.update(query, update, { upsert: true });
6969
}
7070

71+
updateOne(query, update) {
72+
return this._mongoCollection.updateOne(query, update);
73+
}
74+
7175
updateMany(query, update) {
7276
return this._mongoCollection.updateMany(query, update);
7377
}

src/Controllers/DatabaseController.js

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ DatabaseController.prototype.connect = function() {
2828
return this.adapter.connect();
2929
};
3030

31-
// Returns a promise for a Mongo collection.
32-
// Generally just for internal use.
33-
DatabaseController.prototype.collection = function(className) {
34-
if (!Schema.classNameIsValid(className)) {
35-
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME,
36-
'invalid className: ' + className);
37-
}
38-
return this.adapter.collection(this.collectionPrefix + className);
39-
};
40-
4131
DatabaseController.prototype.adaptiveCollection = function(className) {
4232
return this.adapter.adaptiveCollection(this.collectionPrefix + className);
4333
};
@@ -68,9 +58,9 @@ DatabaseController.prototype.validateClassName = function(className) {
6858
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
6959

7060
if (!this.schemaPromise) {
71-
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
61+
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
7262
delete this.schemaPromise;
73-
return Schema.load(coll);
63+
return Schema.load(collection);
7464
});
7565
return this.schemaPromise;
7666
}
@@ -79,9 +69,9 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
7969
if (acceptor(schema)) {
8070
return schema;
8171
}
82-
this.schemaPromise = this.collection('_SCHEMA').then((coll) => {
72+
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
8373
delete this.schemaPromise;
84-
return Schema.load(coll);
74+
return Schema.load(collection);
8575
});
8676
return this.schemaPromise;
8777
});

src/Schema.js

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,12 @@ function schemaAPITypeToMongoFieldType(type) {
168168
// '_metadata' is ignored for now
169169
// Everything else is expected to be a userspace field.
170170
class Schema {
171-
collection;
171+
_collection;
172172
data;
173173
perms;
174174

175175
constructor(collection) {
176-
this.collection = collection;
176+
this._collection = collection;
177177

178178
// this.data[className][fieldName] tells you the type of that field
179179
this.data = {};
@@ -184,8 +184,8 @@ class Schema {
184184
reloadData() {
185185
this.data = {};
186186
this.perms = {};
187-
return this.collection.find({}, {}).toArray().then(mongoSchema => {
188-
for (let obj of mongoSchema) {
187+
return this._collection.find({}).then(results => {
188+
for (let obj of results) {
189189
let className = null;
190190
let classData = {};
191191
let permsData = null;
@@ -231,7 +231,7 @@ class Schema {
231231
return Promise.reject(mongoObject);
232232
}
233233

234-
return this.collection.insertOne(mongoObject.result)
234+
return this._collection.insertOne(mongoObject.result)
235235
.then(result => result.ops[0])
236236
.catch(error => {
237237
if (error.code === 11000) { //Mongo's duplicate key error
@@ -268,7 +268,7 @@ class Schema {
268268
'schema is frozen, cannot add: ' + className);
269269
}
270270
// We don't have this class. Update the schema
271-
return this.collection.insert([{_id: className}]).then(() => {
271+
return this._collection.insertOne({ _id: className }).then(() => {
272272
// The schema update succeeded. Reload the schema
273273
return this.reloadData();
274274
}, () => {
@@ -280,10 +280,9 @@ class Schema {
280280
}).then(() => {
281281
// Ensure that the schema now validates
282282
return this.validateClassName(className, true);
283-
}, (error) => {
283+
}, () => {
284284
// The schema still doesn't validate. Give up
285-
throw new Parse.Error(Parse.Error.INVALID_JSON,
286-
'schema class name does not revalidate');
285+
throw new Parse.Error(Parse.Error.INVALID_JSON, 'schema class name does not revalidate');
287286
});
288287
}
289288

@@ -296,7 +295,7 @@ class Schema {
296295
}
297296
};
298297
update = {'$set': update};
299-
return this.collection.findAndModify(query, {}, update, {}).then(() => {
298+
return this._collection.updateOne(query, update).then(() => {
300299
// The update succeeded. Reload the schema
301300
return this.reloadData();
302301
});
@@ -354,12 +353,12 @@ class Schema {
354353
// We don't have this field. Update the schema.
355354
// Note that we use the $exists guard and $set to avoid race
356355
// conditions in the database. This is important!
357-
var query = {_id: className};
358-
query[key] = {'$exists': false};
356+
var query = { _id: className };
357+
query[key] = { '$exists': false };
359358
var update = {};
360359
update[key] = type;
361360
update = {'$set': update};
362-
return this.collection.findAndModify(query, {}, update, {}).then(() => {
361+
return this._collection.upsertOne(query, update).then(() => {
363362
// The update succeeded. Reload the schema
364363
return this.reloadData();
365364
}, () => {
@@ -422,14 +421,14 @@ class Schema {
422421

423422
// for non-relations, remove all the data.
424423
// This is necessary to ensure that the data is still gone if they add the same field.
425-
return database.collection(className)
424+
return database.adaptiveCollection(className)
426425
.then(collection => {
427-
var mongoFieldName = this.data[className][fieldName].startsWith('*') ? '_p_' + fieldName : fieldName;
428-
return collection.update({}, { "$unset": { [mongoFieldName] : null } }, { multi: true });
426+
let mongoFieldName = this.data[className][fieldName].startsWith('*') ? `_p_${fieldName}` : fieldName;
427+
return collection.updateMany({}, { "$unset": { [mongoFieldName]: null } });
429428
});
430429
})
431430
// Save the _SCHEMA object
432-
.then(() => this.collection.update({ _id: className }, { $unset: {[fieldName]: null }}));
431+
.then(() => this._collection.updateOne({ _id: className }, { $unset: { [fieldName]: null } }));
433432
});
434433
}
435434

0 commit comments

Comments
 (0)