Skip to content

Commit d86f0a8

Browse files
committed
Use schema collection instead of adaptive collection for all schema operations.
1 parent 2730398 commit d86f0a8

File tree

4 files changed

+19
-32
lines changed

4 files changed

+19
-32
lines changed

src/Adapters/Storage/Mongo/MongoCollection.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,17 +76,6 @@ export default class MongoCollection {
7676
return this._mongoCollection.updateMany(query, update);
7777
}
7878

79-
// Atomically find and delete an object based on query.
80-
// The result is the promise with an object that was in the database before deleting.
81-
// Postgres Note: Translates directly to `DELETE * FROM ... RETURNING *`, which will return data after delete is done.
82-
findOneAndDelete(query) {
83-
// arguments: query, sort
84-
return this._mongoCollection.findAndRemove(query, []).then(document => {
85-
// Value is the object where mongo returns multiple fields.
86-
return document.value;
87-
});
88-
}
89-
9079
deleteOne(query) {
9180
return this._mongoCollection.deleteOne(query);
9281
}

src/Controllers/DatabaseController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ DatabaseController.prototype.validateClassName = function(className) {
6363
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
6464

6565
if (!this.schemaPromise) {
66-
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
66+
this.schemaPromise = this.schemaCollection().then(collection => {
6767
delete this.schemaPromise;
6868
return Schema.load(collection);
6969
});
@@ -74,7 +74,7 @@ DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
7474
if (acceptor(schema)) {
7575
return schema;
7676
}
77-
this.schemaPromise = this.adaptiveCollection('_SCHEMA').then(collection => {
77+
this.schemaPromise = this.schemaCollection().then(collection => {
7878
delete this.schemaPromise;
7979
return Schema.load(collection);
8080
});

src/Routers/SchemasRouter.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@ function classNameMismatchResponse(bodyClass, pathClass) {
1515
}
1616

1717
function getAllSchemas(req) {
18-
return req.config.database.adaptiveCollection('_SCHEMA')
19-
.then(collection => collection.find({}))
18+
return req.config.database.schemaCollection()
19+
.then(collection => collection.getAllSchemas())
2020
.then(schemas => schemas.map(Schema.mongoSchemaToSchemaAPIResponse))
2121
.then(schemas => ({ response: { results: schemas } }));
2222
}
2323

2424
function getOneSchema(req) {
2525
const className = req.params.className;
26-
return req.config.database.adaptiveCollection('_SCHEMA')
27-
.then(collection => collection.find({ '_id': className }, { limit: 1 }))
28-
.then(results => {
29-
if (results.length != 1) {
26+
return req.config.database.schemaCollection()
27+
.then(collection => collection.findSchema(className))
28+
.then(mongoSchema => {
29+
if (!mongoSchema) {
3030
throw new Parse.Error(Parse.Error.INVALID_CLASS_NAME, `Class ${className} does not exist.`);
3131
}
32-
return results[0];
33-
})
34-
.then(schema => ({ response: Schema.mongoSchemaToSchemaAPIResponse(schema) }));
32+
return { response: Schema.mongoSchemaToSchemaAPIResponse(mongoSchema) };
33+
});
3534
}
3635

3736
function createSchema(req) {
@@ -142,8 +141,8 @@ function deleteSchema(req) {
142141
.then(() => {
143142
// We've dropped the collection now, so delete the item from _SCHEMA
144143
// and clear the _Join collections
145-
return req.config.database.adaptiveCollection('_SCHEMA')
146-
.then(coll => coll.findOneAndDelete({ _id: req.params.className }))
144+
return req.config.database.schemaCollection()
145+
.then(coll => coll.findAndDeleteSchema(req.params.className))
147146
.then(document => {
148147
if (document === null) {
149148
//tried to delete non-existent class

src/Schema.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ class Schema {
184184
reloadData() {
185185
this.data = {};
186186
this.perms = {};
187-
return this._collection.find({}).then(results => {
187+
return this._collection.getAllSchemas().then(results => {
188188
for (let obj of results) {
189189
let className = null;
190190
let classData = {};
@@ -231,7 +231,7 @@ class Schema {
231231
return Promise.reject(mongoObject);
232232
}
233233

234-
return this._collection.insertOne(mongoObject.result)
234+
return this._collection.addSchema(className, 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.insertOne({ _id: className }).then(() => {
271+
return this._collection.addSchema(className).then(() => {
272272
// The schema update succeeded. Reload the schema
273273
return this.reloadData();
274274
}, () => {
@@ -288,14 +288,13 @@ class Schema {
288288

289289
// Sets the Class-level permissions for a given className, which must exist.
290290
setPermissions(className, perms) {
291-
var query = {_id: className};
292291
var update = {
293292
_metadata: {
294293
class_permissions: perms
295294
}
296295
};
297296
update = {'$set': update};
298-
return this._collection.updateOne(query, update).then(() => {
297+
return this._collection.updateSchema(className, update).then(() => {
299298
// The update succeeded. Reload the schema
300299
return this.reloadData();
301300
});
@@ -353,12 +352,12 @@ class Schema {
353352
// We don't have this field. Update the schema.
354353
// Note that we use the $exists guard and $set to avoid race
355354
// conditions in the database. This is important!
356-
var query = { _id: className };
355+
let query = {};
357356
query[key] = { '$exists': false };
358357
var update = {};
359358
update[key] = type;
360359
update = {'$set': update};
361-
return this._collection.upsertOne(query, update).then(() => {
360+
return this._collection.upsertSchema(className, query, update).then(() => {
362361
// The update succeeded. Reload the schema
363362
return this.reloadData();
364363
}, () => {
@@ -428,7 +427,7 @@ class Schema {
428427
});
429428
})
430429
// Save the _SCHEMA object
431-
.then(() => this._collection.updateOne({ _id: className }, { $unset: { [fieldName]: null } }));
430+
.then(() => this._collection.updateSchema(className, { $unset: { [fieldName]: null } }));
432431
});
433432
}
434433

0 commit comments

Comments
 (0)