add includeAll option

This commit is contained in:
Diamond Lewis
2018-06-14 11:50:45 -05:00
committed by Florent Vilmart
parent c8b303a9d2
commit 48ddcdb303
3 changed files with 102 additions and 1 deletions

View File

@@ -44,6 +44,7 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
}
this.doCount = false;
this.includeAll = false;
// The format for this.include is not the same as the format for the
// include option - it's the paths we should include, in order,
@@ -86,6 +87,9 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
case 'count':
this.doCount = true;
break;
case 'includeAll':
this.includeAll = true;
break;
case 'distinct':
case 'pipeline':
case 'skip':
@@ -149,6 +153,8 @@ function RestQuery(config, auth, className, restWhere = {}, restOptions = {}, cl
RestQuery.prototype.execute = function(executeOptions) {
return Promise.resolve().then(() => {
return this.buildRestWhere();
}).then(() => {
return this.handleIncludeAll();
}).then(() => {
return this.runFind(executeOptions);
}).then(() => {
@@ -552,6 +558,29 @@ RestQuery.prototype.runCount = function() {
});
};
RestQuery.prototype.handleIncludeAll = function() {
if (!this.includeAll) {
return;
}
return this.config.database.loadSchema()
.then(schemaController => schemaController.getOneSchema(this.className))
.then(schema => {
const includeFields = [];
const keyFields = [];
for (const field in schema.fields) {
if (schema.fields[field].type && schema.fields[field].type === 'Pointer') {
includeFields.push([field]);
keyFields.push(field);
}
}
// Add fields to include, keys, remove dups
this.include = [...new Set([...this.include, ...includeFields])];
if (this.keys) {
this.keys = [...new Set([...this.keys, ...keyFields])];
}
});
};
// Augments this.response with data at the paths provided in this.include.
RestQuery.prototype.handleInclude = function() {
if (this.include.length == 0) {

View File

@@ -100,7 +100,7 @@ export class ClassesRouter extends PromiseRouter {
static optionsFromBody(body) {
const allowConstraints = ['skip', 'limit', 'order', 'count', 'keys',
'include', 'redirectClassNameForKey', 'where'];
'include', 'includeAll', 'redirectClassNameForKey', 'where'];
for (const key of Object.keys(body)) {
if (allowConstraints.indexOf(key) === -1) {
@@ -128,6 +128,9 @@ export class ClassesRouter extends PromiseRouter {
if (body.include) {
options.include = String(body.include);
}
if (body.includeAll) {
options.includeAll = true;
}
return options;
}