Skip to content

Commit 7042a47

Browse files
mtrezzaTomWFox
andauthored
Add context to object save in JS SDK (#730)
* added context to object save in JS SDK * Update _includes/js/objects.md Co-Authored-By: Tom Fox <[email protected]> * added context info to cloud code * restructuring cloud code doc - added headings per trigger group - moved context of save triggers into own section * Added required parse server version (tbd) - Insert version before merging * add parse server version Co-authored-by: Tom Fox <[email protected]>
1 parent 5cbd0f1 commit 7042a47

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

_includes/cloudcode/cloud-code.md

+51-40
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,11 @@ We don't support at the moment job scheduling and highly recommend to use a 3rd
167167

168168
Viewing jobs is supported on parse-dashboard starting version 1.0.19, but you can also query the _JobStatus class with a masterKey call to fetch your recent jobs.
169169

170-
# beforeSave Triggers
170+
# Save Triggers
171171

172-
## Implementing validation
172+
## beforeSave
173+
174+
### Implementing validation
173175

174176
Another reason to run code in the cloud is to enforce a particular data format. For example, you might have both an Android and an iOS app, and you want to validate data for each of those. Rather than writing code once for each client environment, you can write it just once with Cloud Code.
175177

@@ -192,7 +194,7 @@ If the function throws, the `Review` object will not be saved, and the client wi
192194

193195
One useful tip is that even if your mobile app has many different versions, the same version of Cloud Code applies to all of them. Thus, if you launch an application that doesn't correctly check the validity of input data, you can still fix this problem by adding a validation with `beforeSave`.
194196

195-
## Modifying Objects on Save
197+
### Modifying Objects on Save
196198

197199
In some cases, you don't want to throw out invalid data. You just want to tweak it a bit before saving it. `beforeSave` can handle this case, too. Any adjustment you make to request.object will be saved.
198200

@@ -208,7 +210,7 @@ Parse.Cloud.beforeSave("Review", (request) => {
208210
});
209211
```
210212

211-
## Predefined Classes
213+
### Predefined Classes
212214
If you want to use `beforeSave` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
213215

214216
```javascript
@@ -217,7 +219,7 @@ Parse.Cloud.beforeSave(Parse.User, async (request) => {
217219
})
218220
```
219221

220-
# afterSave Triggers
222+
## afterSave
221223

222224
In some cases, you may want to perform some action, such as a push, after an object has been saved. You can do this by registering a handler with the `afterSave` method. For example, suppose you want to keep track of the number of comments on a blog post. You can do that by writing a function like this:
223225

@@ -235,15 +237,27 @@ Parse.Cloud.afterSave("Comment", (request) => {
235237
});
236238
```
237239

238-
## Async Behavior
240+
### Async Behavior
239241

240242
In the example above, the client will receive a successful response before the promise in the handler completes, regardless of how the promise resolves. For instance, the client will receive a successful response even if the handler throws an exception. Any errors that occurred while running the handler can be found in the Cloud Code log.
241243

242244
You can use an `afterSave` handler to perform lengthy operations after sending a response back to the client. In order to respond to the client before the `afterSave` handler completes, your handler may not return a promise and your `afterSave` handler may not use async/await.
243245

244-
## Using Request Context
246+
### Predefined Classes
247+
248+
If you want to use `afterSave` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
249+
250+
```javascript
251+
Parse.Cloud.afterSave(Parse.User, async (request) => {
252+
// code here
253+
})
254+
```
255+
256+
## Context
257+
258+
When saving a `Parse.Object` you may pass a `context` dictionary that is accessible in the Cloud Code Save Triggers. More info in the [JavaScript Guide]({{ site.baseUrl }}/js/guide/#cloud-code-context).
245259

246-
State can be passed from a `beforeSave` handler to an `afterSave` handler in the Request Context. The following example sends emails to users who are being added to a [Parse.Role's users relation](https://parseplatform.org/Parse-SDK-JS/api/2.1.0/Parse.Role.html#getUsers) asynchronously, so the client receives a response before the emails complete sending:
260+
The context is also passed from a `beforeSave` handler to an `afterSave` handler. The following example sends emails to users who are being added to a [Parse.Role's users relation](https://parseplatform.org/Parse-SDK-JS/api/2.1.0/Parse.Role.html#getUsers) asynchronously, so the client receives a response before the emails complete sending:
247261

248262
```javascript
249263
const beforeSave = function beforeSave(request) {
@@ -268,18 +282,9 @@ const afterSave = function afterSave(request) {
268282
};
269283

270284
```
285+
# Delete Triggers
271286

272-
## Predefined Classes
273-
274-
If you want to use `afterSave` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
275-
276-
```javascript
277-
Parse.Cloud.afterSave(Parse.User, async (request) => {
278-
// code here
279-
})
280-
```
281-
282-
# beforeDelete Triggers
287+
## beforeDelete
283288

284289
You can run custom Cloud Code before an object is deleted. You can do this with the `beforeDelete` method. For instance, this can be used to implement a restricted delete policy that is more sophisticated than what can be expressed through [ACLs]({{ site.apis.js }}/classes/Parse.ACL.html). For example, suppose you have a photo album app, where many photos are associated with each album, and you want to prevent the user from deleting an album if it still has a photo in it. You can do that by writing a function like this:
285290

@@ -300,7 +305,7 @@ Parse.Cloud.beforeDelete("Album", (request) => {
300305
301306
If the function throws, the `Album` object will not be deleted, and the client will get an error. Otherwise,the object will be deleted normally.
302307
303-
## Predefined Classes
308+
### Predefined Classes
304309
If you want to use `beforeDelete` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
305310
306311
```javascript
@@ -309,7 +314,7 @@ Parse.Cloud.beforeDelete(Parse.User, async (request) => {
309314
})
310315
```
311316
312-
# afterDelete Triggers
317+
## afterDelete
313318
314319
In some cases, you may want to perform some action, such as a push, after an object has been deleted. You can do this by registering a handler with the `afterDelete` method. For example, suppose that after deleting a blog post, you also want to delete all associated comments. You can do that by writing a function like this:
315320
@@ -329,7 +334,7 @@ The `afterDelete` handler can access the object that was deleted through `reques
329334
330335
The client will receive a successful response to the delete request after the handler terminates, regardless of how the `afterDelete` terminates. For instance, the client will receive a successful response even if the handler throws an exception. Any errors that occurred while running the handler can be found in the Cloud Code log.
331336
332-
## Predefined Classes
337+
### Predefined Classes
333338
If you want to use `afterDelete` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
334339
335340
```javascript
@@ -338,11 +343,13 @@ Parse.Cloud.afterDelete(Parse.User, async (request) => {
338343
})
339344
```
340345
341-
# beforeSaveFile Triggers
346+
# File Triggers
347+
348+
## beforeSaveFile
342349
343350
With the `beforeSaveFile` method you can run custom Cloud Code before any file is saved. Returning a new `Parse.File` will save the new file instead of the one sent by the client.
344351
345-
## Examples
352+
### Examples
346353
347354
```javascript
348355
// Changing the file name
@@ -367,7 +374,7 @@ Parse.Cloud.beforeSaveFile((request) => {
367374
});
368375
```
369376
370-
## Metadata and Tags
377+
### Metadata and Tags
371378
372379
Adding Metadata and Tags to your files allows you to add additional bits of data to the files that are stored within your storage solution (i.e AWS S3). The `beforeSaveFile` hook is a great place to set the metadata and/or tags on your files.
373380
@@ -382,7 +389,7 @@ Parse.Cloud.beforeSaveFile((request) => {
382389
});
383390
```
384391
385-
# afterSaveFile Triggers
392+
## afterSaveFile
386393
387394
The `afterSaveFile` method is a great way to keep track of all of the files stored in your app. For example:
388395
@@ -398,7 +405,7 @@ Parse.Cloud.afterSaveFile(async (request) => {
398405
});
399406
```
400407
401-
# beforeDeleteFile Triggers
408+
## beforeDeleteFile
402409
403410
You can run custom Cloud Code before any file gets deleted. For example, lets say you want to add logic that only allows files to be deleted by the user who created it. You could use a combination of the `afterSaveFile` and the `beforeDeleteFile` methods as follows:
404411
@@ -422,7 +429,7 @@ Parse.Cloud.beforeDeleteFile(async (request) => {
422429
});
423430
```
424431
425-
# afterDeleteFile Triggers
432+
## afterDeleteFile
426433
427434
In the above `beforeDeleteFile` example the `FileObject` collection is used to keep track of saved files in your app. The `afterDeleteFile` trigger is a good place to clean up these objects once a file has been successfully deleted.
428435
@@ -436,13 +443,15 @@ Parse.Cloud.afterDeleteFile(async (request) => {
436443
});
437444
```
438445
439-
# beforeFind Triggers
446+
# Find Triggers
447+
448+
## beforeFind
440449
441450
*Available only on parse-server cloud code starting 2.2.20*
442451
443452
In some cases you may want to transform an incoming query, adding an additional limit or increasing the default limit, adding extra includes or restrict the results to a subset of keys. You can do so with the `beforeFind` trigger.
444453
445-
## Examples
454+
### Examples
446455
447456
```javascript
448457
// Properties available
@@ -499,7 +508,7 @@ Parse.Cloud.beforeFind('MyObject2', (req) => {
499508

500509
```
501510
502-
## Predefined Classes
511+
### Predefined Classes
503512
504513
If you want to use `beforeFind` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
505514
@@ -509,7 +518,7 @@ Parse.Cloud.beforeFind(Parse.User, async (request) => {
509518
})
510519
```
511520
512-
# afterFind Triggers
521+
## afterFind
513522
514523
*Available only on parse-server cloud code starting 2.2.25*
515524
@@ -521,7 +530,7 @@ Parse.Cloud.afterFind('MyCustomClass', async (request) => {
521530
})
522531
```
523532
524-
## Predefined Classes
533+
### Predefined Classes
525534
526535
If you want to use `afterFind` for a predefined class in the Parse JavaScript SDK (e.g. [Parse.User]({{ site.apis.js }}classes/Parse.User.html)), you should not pass a String for the first argument. Instead, you should pass the class itself, for example:
527536
@@ -531,7 +540,9 @@ Parse.Cloud.afterFind(Parse.User, async (request) => {
531540
})
532541
```
533542
534-
# beforeLogin Triggers
543+
# Session Triggers
544+
545+
## beforeLogin
535546
536547
*Available only on parse-server cloud code starting 3.3.0*
537548
@@ -546,21 +557,21 @@ Parse.Cloud.beforeLogin(async request => {
546557
});
547558
```
548559
549-
## Some considerations to be aware of
560+
### Some considerations to be aware of
550561
551562
- It waits for any promises to resolve
552563
- The user is not available on the request object - the user has not yet been provided a session until after beforeLogin is successfully completed
553564
- Like `afterSave` on `Parse.User`, it will not save mutations to the user unless explicitly saved
554565
555-
### The trigger will run...
566+
#### The trigger will run...
556567
- On username & password logins
557568
- On `authProvider` logins
558569
559-
### The trigger won't run...
570+
#### The trigger won't run...
560571
- On sign up
561572
- If the login credentials are incorrect
562573
563-
# afterLogout Triggers
574+
## afterLogout
564575
565576
*Available only on parse-server cloud code starting 3.10.0*
566577
@@ -575,10 +586,10 @@ Parse.Cloud.afterLogout(async request => {
575586
});
576587
```
577588
578-
## Some considerations to be aware of
589+
### Some considerations to be aware of
579590
- Like with `afterDelete` triggers, the `_Session` object that is contained in the request has already been deleted.
580591
581-
### The trigger will run...
592+
#### The trigger will run...
582593
- when the user logs out and a `_Session` object was deleted
583594
584595
### The trigger won't run...

_includes/js/objects.md

+25
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ teamMember.save(null, { cascadeSave: false });
199199

200200
```
201201

202+
### Cloud Code context
203+
204+
*Requires Parse Server 4.3.0+*
205+
206+
You may pass a `context` dictionary that is accessible in Cloud Code `beforeSave` and `afterSave` triggers for that `Parse.Object`. This is useful if you want to condition certain operations in Cloud Code triggers on ephemeral information that should not be saved with the `Parse.Object` in the database. The context is ephemeral in the sense that it vanishes after the Cloud Code triggers for that particular `Parse.Object` have executed. For example:
207+
208+
```javascript
209+
var TeamMember = Parse.Object.extend("TeamMember");
210+
var teamMember = new TeamMember();
211+
teamMember.set("team", "A");
212+
213+
var context = { notifyTeam: false };
214+
await teamMember.save(null, { context: context });
215+
```
216+
217+
The context is then accessible in Cloud Code:
218+
219+
```javascript
220+
Parse.Cloud.afterSave("TeamMember", async (req) => {
221+
var notifyTeam = req.context.notifyTeam;
222+
if (notifyTeam) {
223+
// Notify team about new member.
224+
}
225+
});
226+
```
202227

203228
## Retrieving Objects
204229

0 commit comments

Comments
 (0)