Skip to content

Commit ca74929

Browse files
committed
wip
1 parent 21c9289 commit ca74929

File tree

3 files changed

+77
-72
lines changed

3 files changed

+77
-72
lines changed

spec/CloudCode.spec.js

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,39 +200,42 @@ describe('Cloud Code', () => {
200200
done();
201201
}
202202
});
203-
204-
it('beforeFind can return object without DB operation', async () => {
203+
fit('beforeFind can return object without DB operation', async () => {
204+
await reconfigureServer({ silent: false });
205205
Parse.Cloud.beforeFind('beforeFind', () => {
206206
return new Parse.Object('TestObject', { foo: 'bar' });
207207
});
208-
Parse.Cloud.afterFind('beforeFind', () => {
209-
throw 'afterFind should not run';
208+
Parse.Cloud.afterFind('beforeFind', req => {
209+
expect(req.objects).toBeDefined();
210+
expect(req.objects[0].className).toBe('TestObject');
210211
});
211212
const newObj = await new Parse.Query('beforeFind').first();
212213
expect(newObj.className).toBe('TestObject');
213214
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
214215
await newObj.save();
215216
});
216217

217-
it('beforeFind can return array of objects without DB operation', async () => {
218+
fit('beforeFind can return array of objects without DB operation', async () => {
218219
Parse.Cloud.beforeFind('beforeFind', () => {
219220
return [new Parse.Object('TestObject', { foo: 'bar' })];
220221
});
221-
Parse.Cloud.afterFind('beforeFind', () => {
222-
throw 'afterFind should not run';
222+
Parse.Cloud.afterFind('beforeFind', req => {
223+
expect(req.objects).toBeDefined();
224+
expect(req.objects[0].className).toBe('TestObject');
223225
});
224226
const newObj = await new Parse.Query('beforeFind').first();
225227
expect(newObj.className).toBe('TestObject');
226228
expect(newObj.toJSON()).toEqual({ foo: 'bar' });
227229
await newObj.save();
228230
});
229231

230-
it('beforeFind can return object for get query without DB operation', async () => {
232+
fit('beforeFind can return object for get query without DB operation', async () => {
231233
Parse.Cloud.beforeFind('beforeFind', () => {
232234
return [new Parse.Object('TestObject', { foo: 'bar' })];
233235
});
234-
Parse.Cloud.afterFind('beforeFind', () => {
235-
throw 'afterFind should not run';
236+
Parse.Cloud.afterFind('beforeFind', req => {
237+
expect(req.objects).toBeDefined();
238+
expect(req.objects[0].className).toBe('TestObject');
236239
});
237240
const newObj = await new Parse.Query('beforeFind').get('objId');
238241
expect(newObj.className).toBe('TestObject');

src/rest.js

Lines changed: 61 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -23,76 +23,75 @@ function checkLiveQuery(className, config) {
2323
return config.liveQueryController && config.liveQueryController.hasLiveQuery(className);
2424
}
2525

26-
// Returns a promise for an object with optional keys 'results' and 'count'.
27-
function find(config, auth, className, restWhere, restOptions, clientSDK, context) {
28-
enforceRoleSecurity('find', className, auth);
29-
return triggers
30-
.maybeRunQueryTrigger(
31-
triggers.Types.beforeFind,
26+
async function runFindTriggers(
27+
config,
28+
auth,
29+
className,
30+
restWhere,
31+
restOptions,
32+
clientSDK,
33+
context,
34+
isGet
35+
) {
36+
const result = await triggers.maybeRunQueryTrigger(
37+
triggers.Types.beforeFind,
38+
className,
39+
restWhere,
40+
restOptions,
41+
config,
42+
auth,
43+
context,
44+
isGet
45+
);
46+
restWhere = result.restWhere || restWhere;
47+
restOptions = result.restOptions || restOptions;
48+
if (result?.objects) {
49+
const objects = result.objects;
50+
await triggers.maybeRunAfterFindTrigger(
51+
triggers.Types.afterFind,
52+
auth,
3253
className,
33-
restWhere,
34-
restOptions,
54+
objects,
3555
config,
36-
auth,
56+
restWhere,
3757
context
38-
)
39-
.then(result => {
40-
restWhere = result.restWhere || restWhere;
41-
restOptions = result.restOptions || restOptions;
42-
if (result?.objects) {
43-
return {
44-
results: result.objects.map(row => row._toFullJSON()),
45-
};
46-
}
47-
const query = new RestQuery(
48-
config,
49-
auth,
50-
className,
51-
restWhere,
52-
restOptions,
53-
clientSDK,
54-
true,
55-
context
56-
);
57-
return query.execute();
58-
});
58+
);
59+
return {
60+
results: objects.map(row => row._toFullJSON()),
61+
};
62+
}
63+
const query = new RestQuery(
64+
config,
65+
auth,
66+
className,
67+
restWhere,
68+
restOptions,
69+
clientSDK,
70+
true,
71+
context
72+
);
73+
return await query.execute();
5974
}
6075

76+
// Returns a promise for an object with optional keys 'results' and 'count'.
77+
const find = (config, auth, className, restWhere, restOptions, clientSDK, context) => {
78+
enforceRoleSecurity('find', className, auth);
79+
return runFindTriggers(config, auth, className, restWhere, restOptions, clientSDK, context);
80+
};
81+
6182
// get is just like find but only queries an objectId.
6283
const get = (config, auth, className, objectId, restOptions, clientSDK, context) => {
63-
var restWhere = { objectId };
6484
enforceRoleSecurity('get', className, auth);
65-
return triggers
66-
.maybeRunQueryTrigger(
67-
triggers.Types.beforeFind,
68-
className,
69-
restWhere,
70-
restOptions,
71-
config,
72-
auth,
73-
context,
74-
true
75-
)
76-
.then(result => {
77-
restWhere = result.restWhere || restWhere;
78-
restOptions = result.restOptions || restOptions;
79-
if (result?.objects) {
80-
return {
81-
results: result.objects.map(row => row._toFullJSON()),
82-
};
83-
}
84-
const query = new RestQuery(
85-
config,
86-
auth,
87-
className,
88-
restWhere,
89-
restOptions,
90-
clientSDK,
91-
true,
92-
context
93-
);
94-
return query.execute();
95-
});
85+
return runFindTriggers(
86+
config,
87+
auth,
88+
className,
89+
{ objectId },
90+
restOptions,
91+
clientSDK,
92+
context,
93+
true
94+
);
9695
};
9796

9897
// Returns a promise that doesn't resolve to any useful value.

src/triggers.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,9 @@ export function maybeRunAfterFindTrigger(
454454
);
455455
request.objects = objects.map(object => {
456456
//setting the class name to transform into parse object
457+
if (object instanceof Parse.Object) {
458+
return object;
459+
}
457460
object.className = className;
458461
return Parse.Object.fromJSON(object);
459462
});

0 commit comments

Comments
 (0)