beforeFind: Improve request.query object (#6237)
* beforeFind: Improve request.query object Closes: https://github.com/parse-community/parse-server/issues/6164 * can modify exclude query
This commit is contained in:
@@ -878,7 +878,10 @@ describe('Cloud Code', () => {
|
|||||||
url: 'https://some.url',
|
url: 'https://some.url',
|
||||||
}),
|
}),
|
||||||
array: ['a', 'b', 'c'],
|
array: ['a', 'b', 'c'],
|
||||||
arrayOfArray: [['a', 'b', 'c'], ['d', 'e', 'f']],
|
arrayOfArray: [
|
||||||
|
['a', 'b', 'c'],
|
||||||
|
['d', 'e', 'f'],
|
||||||
|
],
|
||||||
};
|
};
|
||||||
Parse.Cloud.run('params', params).then(() => {
|
Parse.Cloud.run('params', params).then(() => {
|
||||||
done();
|
done();
|
||||||
@@ -1763,8 +1766,15 @@ describe('beforeFind hooks', () => {
|
|||||||
expect(jsonQuery.where.key).toEqual('value');
|
expect(jsonQuery.where.key).toEqual('value');
|
||||||
expect(jsonQuery.where.some).toEqual({ $gt: 10 });
|
expect(jsonQuery.where.some).toEqual({ $gt: 10 });
|
||||||
expect(jsonQuery.include).toEqual('otherKey,otherValue');
|
expect(jsonQuery.include).toEqual('otherKey,otherValue');
|
||||||
|
expect(jsonQuery.excludeKeys).toBe('exclude');
|
||||||
expect(jsonQuery.limit).toEqual(100);
|
expect(jsonQuery.limit).toEqual(100);
|
||||||
expect(jsonQuery.skip).toBe(undefined);
|
expect(jsonQuery.skip).toBe(undefined);
|
||||||
|
expect(jsonQuery.order).toBe('key');
|
||||||
|
expect(jsonQuery.keys).toBe('select');
|
||||||
|
expect(jsonQuery.readPreference).toBe('PRIMARY');
|
||||||
|
expect(jsonQuery.includeReadPreference).toBe('SECONDARY');
|
||||||
|
expect(jsonQuery.subqueryReadPreference).toBe('SECONDARY_PREFERRED');
|
||||||
|
|
||||||
expect(req.isGet).toEqual(false);
|
expect(req.isGet).toEqual(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -1773,6 +1783,10 @@ describe('beforeFind hooks', () => {
|
|||||||
query.greaterThan('some', 10);
|
query.greaterThan('some', 10);
|
||||||
query.include('otherKey');
|
query.include('otherKey');
|
||||||
query.include('otherValue');
|
query.include('otherValue');
|
||||||
|
query.ascending('key');
|
||||||
|
query.select('select');
|
||||||
|
query.exclude('exclude');
|
||||||
|
query.readPreference('PRIMARY', 'SECONDARY', 'SECONDARY_PREFERRED');
|
||||||
query.find().then(() => {
|
query.find().then(() => {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
@@ -1824,6 +1838,25 @@ describe('beforeFind hooks', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should use the modified exclude query', async () => {
|
||||||
|
Parse.Cloud.beforeFind('MyObject', req => {
|
||||||
|
const q = req.query;
|
||||||
|
q.exclude('number');
|
||||||
|
});
|
||||||
|
|
||||||
|
const obj = new Parse.Object('MyObject');
|
||||||
|
obj.set('number', 100);
|
||||||
|
obj.set('string', 'hello');
|
||||||
|
await obj.save();
|
||||||
|
|
||||||
|
const query = new Parse.Query('MyObject');
|
||||||
|
query.equalTo('objectId', obj.id);
|
||||||
|
const results = await query.find();
|
||||||
|
expect(results.length).toBe(1);
|
||||||
|
expect(results[0].get('number')).toBeUndefined();
|
||||||
|
expect(results[0].get('string')).toBe('hello');
|
||||||
|
});
|
||||||
|
|
||||||
it('should reject queries', done => {
|
it('should reject queries', done => {
|
||||||
Parse.Cloud.beforeFind('MyObject', () => {
|
Parse.Cloud.beforeFind('MyObject', () => {
|
||||||
return Promise.reject('Do not run that query');
|
return Promise.reject('Do not run that query');
|
||||||
|
|||||||
@@ -451,22 +451,14 @@ export function maybeRunQueryTrigger(
|
|||||||
restOptions,
|
restOptions,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
const json = Object.assign({}, restOptions);
|
||||||
|
json.where = restWhere;
|
||||||
|
|
||||||
const parseQuery = new Parse.Query(className);
|
const parseQuery = new Parse.Query(className);
|
||||||
if (restWhere) {
|
parseQuery.withJSON(json);
|
||||||
parseQuery._where = restWhere;
|
|
||||||
}
|
|
||||||
let count = false;
|
let count = false;
|
||||||
if (restOptions) {
|
if (restOptions) {
|
||||||
if (restOptions.include && restOptions.include.length > 0) {
|
|
||||||
parseQuery._include = restOptions.include.split(',');
|
|
||||||
}
|
|
||||||
if (restOptions.skip) {
|
|
||||||
parseQuery._skip = restOptions.skip;
|
|
||||||
}
|
|
||||||
if (restOptions.limit) {
|
|
||||||
parseQuery._limit = restOptions.limit;
|
|
||||||
}
|
|
||||||
count = !!restOptions.count;
|
count = !!restOptions.count;
|
||||||
}
|
}
|
||||||
const requestObject = getRequestQueryObject(
|
const requestObject = getRequestQueryObject(
|
||||||
@@ -503,6 +495,10 @@ export function maybeRunQueryTrigger(
|
|||||||
restOptions = restOptions || {};
|
restOptions = restOptions || {};
|
||||||
restOptions.include = jsonQuery.include;
|
restOptions.include = jsonQuery.include;
|
||||||
}
|
}
|
||||||
|
if (jsonQuery.excludeKeys) {
|
||||||
|
restOptions = restOptions || {};
|
||||||
|
restOptions.excludeKeys = jsonQuery.excludeKeys;
|
||||||
|
}
|
||||||
if (jsonQuery.keys) {
|
if (jsonQuery.keys) {
|
||||||
restOptions = restOptions || {};
|
restOptions = restOptions || {};
|
||||||
restOptions.keys = jsonQuery.keys;
|
restOptions.keys = jsonQuery.keys;
|
||||||
|
|||||||
Reference in New Issue
Block a user