Skip to content

Commit 83f6175

Browse files
committed
destructuring in DB controller
1 parent d49d539 commit 83f6175

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

src/Adapters/Storage/Mongo/MongoTransform.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ function transformWhere(schema, className, restWhere, options = {validate: true}
195195
let transformKeyOptions = {query: true};
196196
transformKeyOptions.validate = options.validate;
197197
for (let restKey in restWhere) {
198-
let out = transformKeyValue(schema, className, restKey, restWhere[restKey],
199-
transformKeyOptions);
198+
let out = transformKeyValue(schema, className, restKey, restWhere[restKey], transformKeyOptions);
200199
mongoWhere[out.key] = out.value;
201200
}
202201
return mongoWhere;
@@ -333,8 +332,7 @@ function transformUpdate(schema, className, restUpdate) {
333332
}
334333

335334
for (var restKey in restUpdate) {
336-
var out = transformKeyValue(schema, className, restKey, restUpdate[restKey],
337-
{update: true});
335+
var out = transformKeyValue(schema, className, restKey, restUpdate[restKey], {update: true});
338336

339337
// If the output value is an object with any $ keys, it's an
340338
// operator that needs to be lifted onto the top level update

src/Controllers/DatabaseController.js

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ DatabaseController.prototype.redirectClassNameForKey = function(className, key)
8787
// Returns a promise that resolves to the new schema.
8888
// This does not update this.schema, because in a situation like a
8989
// batch request, that could confuse other users of the schema.
90-
DatabaseController.prototype.validateObject = function(className, object, query, options) {
90+
DatabaseController.prototype.validateObject = function(className, object, query, { acl }) {
9191
let schema;
92-
let isMaster = !('acl' in options);
93-
var aclGroup = options.acl || [];
92+
let isMaster = acl === undefined;
93+
var aclGroup = acl || [];
9494
return this.loadSchema().then(s => {
9595
schema = s;
9696
if (isMaster) {
@@ -131,14 +131,18 @@ DatabaseController.prototype.untransformObject = function(
131131
// acl: a list of strings. If the object to be updated has an ACL,
132132
// one of the provided strings must provide the caller with
133133
// write permissions.
134-
DatabaseController.prototype.update = function(className, query, update, options = {}) {
134+
DatabaseController.prototype.update = function(className, query, update, {
135+
acl,
136+
many,
137+
upsert,
138+
} = {}) {
135139

136140
const originalUpdate = update;
137141
// Make a copy of the object, so we don't mutate the incoming data.
138142
update = deepcopy(update);
139143

140-
var isMaster = !('acl' in options);
141-
var aclGroup = options.acl || [];
144+
var isMaster = acl === undefined;
145+
var aclGroup = acl || [];
142146
var mongoUpdate, schema;
143147
return this.loadSchema()
144148
.then(s => {
@@ -158,13 +162,13 @@ DatabaseController.prototype.update = function(className, query, update, options
158162
return Promise.resolve();
159163
}
160164
var mongoWhere = this.transform.transformWhere(schema, className, query, {validate: !this.skipValidation});
161-
if (options.acl) {
162-
mongoWhere = this.transform.addWriteACL(mongoWhere, options.acl);
165+
if (acl) {
166+
mongoWhere = this.transform.addWriteACL(mongoWhere, acl);
163167
}
164168
mongoUpdate = this.transform.transformUpdate(schema, className, update, {validate: !this.skipValidation});
165-
if (options.many) {
169+
if (many) {
166170
return collection.updateMany(mongoWhere, mongoUpdate);
167-
}else if (options.upsert) {
171+
} else if (upsert) {
168172
return collection.upsertOne(mongoWhere, mongoUpdate);
169173
} else {
170174
return collection.findOneAndUpdate(mongoWhere, mongoUpdate);
@@ -203,9 +207,7 @@ function sanitizeDatabaseResult(originalObject, result) {
203207
// Returns a promise that resolves successfully when these are
204208
// processed.
205209
// This mutates update.
206-
DatabaseController.prototype.handleRelationUpdates = function(className,
207-
objectId,
208-
update) {
210+
DatabaseController.prototype.handleRelationUpdates = function(className, objectId, update) {
209211
var pending = [];
210212
var deleteMe = [];
211213
objectId = update.objectId || objectId;
@@ -282,9 +284,9 @@ DatabaseController.prototype.removeRelation = function(key, fromClassName, fromI
282284
// acl: a list of strings. If the object to be updated has an ACL,
283285
// one of the provided strings must provide the caller with
284286
// write permissions.
285-
DatabaseController.prototype.destroy = function(className, query, options = {}) {
286-
var isMaster = !('acl' in options);
287-
var aclGroup = options.acl || [];
287+
DatabaseController.prototype.destroy = function(className, query, { acl } = {}) {
288+
var isMaster = acl !== undefined;
289+
var aclGroup = acl || [];
288290

289291
var schema;
290292
return this.loadSchema()
@@ -304,8 +306,8 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
304306
}
305307
}
306308
let mongoWhere = this.transform.transformWhere(schema, className, query, {validate: !this.skipValidation});
307-
if (options.acl) {
308-
mongoWhere = this.transform.addWriteACL(mongoWhere, options.acl);
309+
if (acl) {
310+
mongoWhere = this.transform.addWriteACL(mongoWhere, acl);
309311
}
310312
return collection.deleteMany(mongoWhere);
311313
})
@@ -320,13 +322,13 @@ DatabaseController.prototype.destroy = function(className, query, options = {})
320322

321323
// Inserts an object into the database.
322324
// Returns a promise that resolves successfully iff the object saved.
323-
DatabaseController.prototype.create = function(className, object, options = {}) {
325+
DatabaseController.prototype.create = function(className, object, { acl } = {}) {
324326
// Make a copy of the object, so we don't mutate the incoming data.
325327
let originalObject = object;
326328
object = deepcopy(object);
327329

328-
var isMaster = !('acl' in options);
329-
var aclGroup = options.acl || [];
330+
var isMaster = acl === undefined;
331+
var aclGroup = acl || [];
330332

331333
return this.validateClassName(className)
332334
.then(() => this.loadSchema())
@@ -570,27 +572,33 @@ DatabaseController.prototype.addNotInObjectIdsIds = function(ids = null, query)
570572
// TODO: make userIds not needed here. The db adapter shouldn't know
571573
// anything about users, ideally. Then, improve the format of the ACL
572574
// arg to work like the others.
573-
DatabaseController.prototype.find = function(className, query, options = {}) {
575+
DatabaseController.prototype.find = function(className, query, {
576+
skip,
577+
limit,
578+
acl,
579+
sort,
580+
count,
581+
} = {}) {
574582
let mongoOptions = {};
575-
if (options.skip) {
576-
mongoOptions.skip = options.skip;
583+
if (skip) {
584+
mongoOptions.skip = skip;
577585
}
578-
if (options.limit) {
579-
mongoOptions.limit = options.limit;
586+
if (limit) {
587+
mongoOptions.limit = limit;
580588
}
581-
let isMaster = !('acl' in options);
582-
let aclGroup = options.acl || [];
589+
let isMaster = acl === undefined;
590+
let aclGroup = acl || [];
583591
let schema = null;
584592
let op = typeof query.objectId == 'string' && Object.keys(query).length === 1 ?
585593
'get' :
586594
'find';
587595
return this.loadSchema().then(s => {
588596
schema = s;
589-
if (options.sort) {
597+
if (sort) {
590598
mongoOptions.sort = {};
591-
for (let key in options.sort) {
599+
for (let key in sort) {
592600
let mongoKey = this.transform.transformKey(schema, className, key);
593-
mongoOptions.sort[mongoKey] = options.sort[key];
601+
mongoOptions.sort[mongoKey] = sort[key];
594602
}
595603
}
596604

@@ -618,7 +626,7 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
618626
if (!isMaster) {
619627
mongoWhere = this.transform.addReadACL(mongoWhere, aclGroup);
620628
}
621-
if (options.count) {
629+
if (count) {
622630
delete mongoOptions.limit;
623631
return collection.count(mongoWhere, mongoOptions);
624632
} else {

0 commit comments

Comments
 (0)