Skip to content

Added rest option: excludeKeys #5737

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 72 additions & 2 deletions spec/ParseQuery.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3266,8 +3266,8 @@ describe('Parse.Query testing', () => {
);
});

it('select keys query', function(done) {
const obj = new TestObject({ foo: 'baz', bar: 1 });
it('select/unselect keys query', function(done) {
const obj = new TestObject({ foo: 'baz', bar: 1, wooz: [1, 2, 3] });

obj
.save()
Expand All @@ -3288,11 +3288,17 @@ describe('Parse.Query testing', () => {
undefined,
"expected 'bar' field to be unset"
);
strictEqual(
result.get('wooz'),
undefined,
"expected 'wooz' field to be unset"
);
return result.fetch();
})
.then(function(result) {
strictEqual(result.get('foo'), 'baz');
strictEqual(result.get('bar'), 1);
strictEqual(result.get('wooz')[0], 1);
})
.then(function() {
obj._clearServerData();
Expand All @@ -3313,6 +3319,11 @@ describe('Parse.Query testing', () => {
undefined,
"expected 'bar' field to be unset"
);
strictEqual(
result.get('wooz'),
undefined,
"expected 'wooz' field to be unset"
);
})
.then(function() {
obj._clearServerData();
Expand All @@ -3325,6 +3336,11 @@ describe('Parse.Query testing', () => {
ok(!result.dirty(), 'expected result not to be dirty');
strictEqual(result.get('foo'), 'baz');
strictEqual(result.get('bar'), 1);
strictEqual(
result.get('wooz'),
undefined,
"expected 'wooz' field to be unset"
);
})
.then(function() {
obj._clearServerData();
Expand All @@ -3337,6 +3353,60 @@ describe('Parse.Query testing', () => {
ok(!result.dirty(), 'expected result not to be dirty');
strictEqual(result.get('foo'), 'baz');
strictEqual(result.get('bar'), 1);
strictEqual(
result.get('wooz'),
undefined,
"expected 'wooz' field to be unset"
);
})
.then(function() {
obj._clearServerData();
return request({
url: Parse.serverURL + '/classes/TestObject',
qs: {
excludeKeys: 'bar,wooz',
},
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey,
'Content-Type': 'application/json',
},
});
})
.then(function(result) {
result = result.data.results[0];
strictEqual(result.foo, 'baz');
strictEqual(result.bar, undefined, "expected 'bar' field to be unset");
strictEqual(
result.wooz,
undefined,
"expected 'wooz' field to be unset"
);
})
.then(function() {
obj._clearServerData();
return request({
url: Parse.serverURL + '/classes/TestObject',
qs: {
keys: 'foo',
excludeKeys: 'foo',
},
headers: {
'X-Parse-Application-Id': Parse.applicationId,
'X-Parse-Javascript-Key': Parse.javaScriptKey,
'Content-Type': 'application/json',
},
});
})
.then(function(result) {
result = result.data.results[0];
strictEqual(result.foo, undefined, "expected 'foo' field to be unset");
strictEqual(result.bar, undefined, "expected 'bar' field to be unset");
strictEqual(
result.wooz,
undefined,
"expected 'wooz' field to be unset"
);
})
.then(
function() {
Expand Down
29 changes: 28 additions & 1 deletion src/RestQuery.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const AlwaysSelectedKeys = ['objectId', 'createdAt', 'updatedAt', 'ACL'];
// count
// include
// keys
// excludeKeys
// redirectClassNameForKey
// readPreference
// includeReadPreference
Expand Down Expand Up @@ -102,6 +103,13 @@ function RestQuery(
this.keys = Array.from(new Set(keys));
break;
}
case 'excludeKeys': {
const exclude = restOptions.excludeKeys
.split(',')
.filter(k => AlwaysSelectedKeys.indexOf(k) < 0);
this.excludeKeys = Array.from(new Set(exclude));
break;
}
case 'count':
this.doCount = true;
break;
Expand Down Expand Up @@ -184,6 +192,9 @@ RestQuery.prototype.execute = function(executeOptions) {
.then(() => {
return this.handleIncludeAll();
})
.then(() => {
return this.handleExcludeKeys();
})
.then(() => {
return this.runFind(executeOptions);
})
Expand Down Expand Up @@ -700,11 +711,27 @@ RestQuery.prototype.handleIncludeAll = function() {
this.include = [...new Set([...this.include, ...includeFields])];
// if this.keys not set, then all keys are already included
if (this.keys) {
this.keys = [...new Set([...this.keys, ...keyFields])];
this.keys = [...new Set([...this.keys, ...keyFields])]
// Ignore excluded keys
.filter(k => !this.excludeKeys || this.excludeKeys.indexOf(k) < 0);
}
});
};

// Upadates property `this.keys` to contain all keys but the ones unselected.
RestQuery.prototype.handleExcludeKeys = function() {
if (!this.excludeKeys) {
return;
}
return this.config.database
.loadSchema()
.then(schemaController => schemaController.getOneSchema(this.className))
.then(schema => {
const fields = this.keys ? this.keys : Object.keys(schema.fields);
this.keys = fields.filter(k => this.excludeKeys.indexOf(k) < 0);
});
};

// Augments this.response with data at the paths provided in this.include.
RestQuery.prototype.handleInclude = function() {
if (this.include.length == 0) {
Expand Down
4 changes: 4 additions & 0 deletions src/Routers/ClassesRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export class ClassesRouter extends PromiseRouter {
'order',
'count',
'keys',
'excludeKeys',
'include',
'includeAll',
'redirectClassNameForKey',
Expand Down Expand Up @@ -200,6 +201,9 @@ export class ClassesRouter extends PromiseRouter {
if (typeof body.keys == 'string') {
options.keys = body.keys;
}
if (typeof body.excludeKeys == 'string') {
options.excludeKeys = body.excludeKeys;
}
if (body.include) {
options.include = String(body.include);
}
Expand Down