Added rest option: excludeKeys (#5737)
* Added restOption: excludeKeys * improve tests
This commit is contained in:
committed by
Diamond Lewis
parent
378e70afdc
commit
9816285205
@@ -3348,6 +3348,122 @@ describe('Parse.Query testing', () => {
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
it('exclude keys', async () => {
|
||||||
|
const obj = new TestObject({ foo: 'baz', hello: 'world' });
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
excludeKeys: 'foo',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].foo).toBeUndefined();
|
||||||
|
expect(response.data.results[0].hello).toBe('world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exclude keys with select same key', async () => {
|
||||||
|
const obj = new TestObject({ foo: 'baz', hello: 'world' });
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
keys: 'foo',
|
||||||
|
excludeKeys: 'foo',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].foo).toBeUndefined();
|
||||||
|
expect(response.data.results[0].hello).toBeUndefined();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exclude keys with select different key', async () => {
|
||||||
|
const obj = new TestObject({ foo: 'baz', hello: 'world' });
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
keys: 'foo,hello',
|
||||||
|
excludeKeys: 'foo',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].foo).toBeUndefined();
|
||||||
|
expect(response.data.results[0].hello).toBe('world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exclude keys with include same key', async () => {
|
||||||
|
const pointer = new TestObject();
|
||||||
|
await pointer.save();
|
||||||
|
const obj = new TestObject({ child: pointer, hello: 'world' });
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
include: 'child',
|
||||||
|
excludeKeys: 'child',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].child).toBeUndefined();
|
||||||
|
expect(response.data.results[0].hello).toBe('world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exclude keys with include different key', async () => {
|
||||||
|
const pointer = new TestObject();
|
||||||
|
await pointer.save();
|
||||||
|
const obj = new TestObject({
|
||||||
|
child1: pointer,
|
||||||
|
child2: pointer,
|
||||||
|
hello: 'world',
|
||||||
|
});
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
include: 'child1,child2',
|
||||||
|
excludeKeys: 'child1',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].child1).toBeUndefined();
|
||||||
|
expect(response.data.results[0].child2.objectId).toEqual(pointer.id);
|
||||||
|
expect(response.data.results[0].hello).toBe('world');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('exclude keys with includeAll', async () => {
|
||||||
|
const pointer = new TestObject();
|
||||||
|
await pointer.save();
|
||||||
|
const obj = new TestObject({
|
||||||
|
child1: pointer,
|
||||||
|
child2: pointer,
|
||||||
|
hello: 'world',
|
||||||
|
});
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const response = await request({
|
||||||
|
url: Parse.serverURL + '/classes/TestObject',
|
||||||
|
qs: {
|
||||||
|
includeAll: true,
|
||||||
|
excludeKeys: 'child1',
|
||||||
|
where: JSON.stringify({ objectId: obj.id }),
|
||||||
|
},
|
||||||
|
headers: masterKeyHeaders,
|
||||||
|
});
|
||||||
|
expect(response.data.results[0].child).toBeUndefined();
|
||||||
|
expect(response.data.results[0].child2.objectId).toEqual(pointer.id);
|
||||||
|
expect(response.data.results[0].hello).toBe('world');
|
||||||
|
});
|
||||||
|
|
||||||
it('select keys with each query', function(done) {
|
it('select keys with each query', function(done) {
|
||||||
const obj = new TestObject({ foo: 'baz', bar: 1 });
|
const obj = new TestObject({ foo: 'baz', bar: 1 });
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ const AlwaysSelectedKeys = ['objectId', 'createdAt', 'updatedAt', 'ACL'];
|
|||||||
// count
|
// count
|
||||||
// include
|
// include
|
||||||
// keys
|
// keys
|
||||||
|
// excludeKeys
|
||||||
// redirectClassNameForKey
|
// redirectClassNameForKey
|
||||||
// readPreference
|
// readPreference
|
||||||
// includeReadPreference
|
// includeReadPreference
|
||||||
@@ -102,6 +103,13 @@ function RestQuery(
|
|||||||
this.keys = Array.from(new Set(keys));
|
this.keys = Array.from(new Set(keys));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
case 'excludeKeys': {
|
||||||
|
const exclude = restOptions.excludeKeys
|
||||||
|
.split(',')
|
||||||
|
.filter(k => AlwaysSelectedKeys.indexOf(k) < 0);
|
||||||
|
this.excludeKeys = Array.from(new Set(exclude));
|
||||||
|
break;
|
||||||
|
}
|
||||||
case 'count':
|
case 'count':
|
||||||
this.doCount = true;
|
this.doCount = true;
|
||||||
break;
|
break;
|
||||||
@@ -184,6 +192,9 @@ RestQuery.prototype.execute = function(executeOptions) {
|
|||||||
.then(() => {
|
.then(() => {
|
||||||
return this.handleIncludeAll();
|
return this.handleIncludeAll();
|
||||||
})
|
})
|
||||||
|
.then(() => {
|
||||||
|
return this.handleExcludeKeys();
|
||||||
|
})
|
||||||
.then(() => {
|
.then(() => {
|
||||||
return this.runFind(executeOptions);
|
return this.runFind(executeOptions);
|
||||||
})
|
})
|
||||||
@@ -705,6 +716,24 @@ RestQuery.prototype.handleIncludeAll = function() {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Updates property `this.keys` to contain all keys but the ones unselected.
|
||||||
|
RestQuery.prototype.handleExcludeKeys = function() {
|
||||||
|
if (!this.excludeKeys) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.keys) {
|
||||||
|
this.keys = this.keys.filter(k => !this.excludeKeys.includes(k));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
return this.config.database
|
||||||
|
.loadSchema()
|
||||||
|
.then(schemaController => schemaController.getOneSchema(this.className))
|
||||||
|
.then(schema => {
|
||||||
|
const fields = Object.keys(schema.fields);
|
||||||
|
this.keys = fields.filter(k => !this.excludeKeys.includes(k));
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// Augments this.response with data at the paths provided in this.include.
|
// Augments this.response with data at the paths provided in this.include.
|
||||||
RestQuery.prototype.handleInclude = function() {
|
RestQuery.prototype.handleInclude = function() {
|
||||||
if (this.include.length == 0) {
|
if (this.include.length == 0) {
|
||||||
|
|||||||
@@ -165,6 +165,7 @@ export class ClassesRouter extends PromiseRouter {
|
|||||||
'order',
|
'order',
|
||||||
'count',
|
'count',
|
||||||
'keys',
|
'keys',
|
||||||
|
'excludeKeys',
|
||||||
'include',
|
'include',
|
||||||
'includeAll',
|
'includeAll',
|
||||||
'redirectClassNameForKey',
|
'redirectClassNameForKey',
|
||||||
@@ -200,6 +201,9 @@ export class ClassesRouter extends PromiseRouter {
|
|||||||
if (typeof body.keys == 'string') {
|
if (typeof body.keys == 'string') {
|
||||||
options.keys = body.keys;
|
options.keys = body.keys;
|
||||||
}
|
}
|
||||||
|
if (typeof body.excludeKeys == 'string') {
|
||||||
|
options.excludeKeys = body.excludeKeys;
|
||||||
|
}
|
||||||
if (body.include) {
|
if (body.include) {
|
||||||
options.include = String(body.include);
|
options.include = String(body.include);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user