Skip to content

Commit 8de0edb

Browse files
committed
Merge branch 'beforeFind-objects' of https://github.com/dblythy/parse-server into beforeFind-fix
2 parents 09fcde2 + d66fa48 commit 8de0edb

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

spec/CloudCode.spec.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,60 @@ describe('Cloud Code', () => {
201201
done();
202202
}
203203
});
204+
it('beforeFind can return object without DB operation', async () => {
205+
Parse.Cloud.beforeFind('beforeFind', () => {
206+
return new Parse.Object('TestObject', { foo: 'bar' });
207+
});
208+
Parse.Cloud.afterFind('beforeFind', req => {
209+
expect(req.objects).toBeDefined();
210+
expect(req.objects[0].get('foo')).toBe('bar');
211+
});
212+
const newObj = await new Parse.Query('beforeFind').first();
213+
expect(newObj.className).toBe('TestObject');
214+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
215+
await newObj.save();
216+
});
217+
218+
it('beforeFind can return array of objects without DB operation', async () => {
219+
Parse.Cloud.beforeFind('beforeFind', () => {
220+
return [new Parse.Object('TestObject', { foo: 'bar' })];
221+
});
222+
Parse.Cloud.afterFind('beforeFind', req => {
223+
expect(req.objects).toBeDefined();
224+
expect(req.objects[0].get('foo')).toBe('bar');
225+
});
226+
const newObj = await new Parse.Query('beforeFind').first();
227+
expect(newObj.className).toBe('TestObject');
228+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
229+
await newObj.save();
230+
});
231+
232+
it('beforeFind can return object for get query without DB operation', async () => {
233+
Parse.Cloud.beforeFind('beforeFind', () => {
234+
return [new Parse.Object('TestObject', { foo: 'bar' })];
235+
});
236+
Parse.Cloud.afterFind('beforeFind', req => {
237+
expect(req.objects).toBeDefined();
238+
expect(req.objects[0].get('foo')).toBe('bar');
239+
});
240+
const newObj = await new Parse.Query('beforeFind').get('objId');
241+
expect(newObj.className).toBe('TestObject');
242+
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
243+
await newObj.save();
244+
});
245+
246+
it('beforeFind can return empty array without DB operation', async () => {
247+
Parse.Cloud.beforeFind('beforeFind', () => {
248+
return [];
249+
});
250+
Parse.Cloud.afterFind('beforeFind', req => {
251+
expect(req.objects.length).toBe(0);
252+
});
253+
const obj = new Parse.Object('beforeFind');
254+
await obj.save();
255+
const newObj = await new Parse.Query('beforeFind').first();
256+
expect(newObj).toBeUndefined();
257+
});
204258

205259
it('beforeFind can return object without DB operation', async () => {
206260
Parse.Cloud.beforeFind('beforeFind', () => {

src/rest.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ async function runFindTriggers(
5050
if (result?.objects) {
5151
const objects = result.objects;
5252

53+
// Déclencher le trigger afterFind si des objets sont retournés
5354
await triggers.maybeRunAfterFindTrigger(
5455
triggers.Types.afterFind,
5556
auth,
@@ -65,6 +66,7 @@ async function runFindTriggers(
6566
};
6667
}
6768

69+
// Conserver la distinction entre get et find
6870
const query = await RestQuery({
6971
method: isGet ? RestQuery.Method.get : RestQuery.Method.find,
7072
config,
@@ -79,6 +81,7 @@ async function runFindTriggers(
7981
return query.execute();
8082
}
8183

84+
// Returns a promise for an object with optional keys 'results' and 'count'.
8285
const find = async (config, auth, className, restWhere, restOptions, clientSDK, context) => {
8386
enforceRoleSecurity('find', className, auth);
8487
return runFindTriggers(
@@ -93,6 +96,7 @@ const find = async (config, auth, className, restWhere, restOptions, clientSDK,
9396
);
9497
};
9598

99+
// get is just like find but only queries an objectId.
96100
const get = async (config, auth, className, objectId, restOptions, clientSDK, context) => {
97101
enforceRoleSecurity('get', className, auth);
98102
return runFindTriggers(
@@ -107,6 +111,7 @@ const get = async (config, auth, className, objectId, restOptions, clientSDK, co
107111
);
108112
};
109113

114+
// Returns a promise that doesn't resolve to any useful value.
110115
function del(config, auth, className, objectId, context) {
111116
if (typeof objectId !== 'string') {
112117
throw new Parse.Error(Parse.Error.INVALID_JSON, 'bad objectId');

0 commit comments

Comments
 (0)