Skip to content

Commit 223bc3a

Browse files
committed
restructuring cloud code doc
- added headings per trigger group - moved context of save triggers into own section
1 parent 3b159bf commit 223bc3a

File tree

1 file changed

+48
-40
lines changed

1 file changed

+48
-40
lines changed

_includes/cloudcode/cloud-code.md

Lines changed: 48 additions & 40 deletions
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,19 +237,28 @@ 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
245257

246258
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).
247259

248260
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:
249261

250-
251262
```javascript
252263
const beforeSave = function beforeSave(request) {
253264
const { object: role } = request;
@@ -271,18 +282,9 @@ const afterSave = function afterSave(request) {
271282
};
272283

273284
```
285+
# Delete Triggers
274286

275-
## Predefined Classes
276-
277-
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:
278-
279-
```javascript
280-
Parse.Cloud.afterSave(Parse.User, async (request) => {
281-
// code here
282-
})
283-
```
284-
285-
# beforeDelete Triggers
287+
## beforeDelete
286288

287289
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:
288290

@@ -303,7 +305,7 @@ Parse.Cloud.beforeDelete("Album", (request) => {
303305
304306
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.
305307
306-
## Predefined Classes
308+
### Predefined Classes
307309
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:
308310
309311
```javascript
@@ -312,7 +314,7 @@ Parse.Cloud.beforeDelete(Parse.User, async (request) => {
312314
})
313315
```
314316
315-
# afterDelete Triggers
317+
## afterDelete
316318
317319
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:
318320
@@ -332,7 +334,7 @@ The `afterDelete` handler can access the object that was deleted through `reques
332334
333335
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.
334336
335-
## Predefined Classes
337+
### Predefined Classes
336338
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:
337339
338340
```javascript
@@ -341,11 +343,13 @@ Parse.Cloud.afterDelete(Parse.User, async (request) => {
341343
})
342344
```
343345
344-
# beforeSaveFile Triggers
346+
# File Triggers
347+
348+
## beforeSaveFile
345349
346350
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.
347351
348-
## Examples
352+
### Examples
349353
350354
```javascript
351355
// Changing the file name
@@ -370,7 +374,7 @@ Parse.Cloud.beforeSaveFile((request) => {
370374
});
371375
```
372376
373-
## Metadata and Tags
377+
### Metadata and Tags
374378
375379
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.
376380
@@ -385,7 +389,7 @@ Parse.Cloud.beforeSaveFile((request) => {
385389
});
386390
```
387391
388-
# afterSaveFile Triggers
392+
## afterSaveFile
389393
390394
The `afterSaveFile` method is a great way to keep track of all of the files stored in your app. For example:
391395
@@ -401,7 +405,7 @@ Parse.Cloud.afterSaveFile(async (request) => {
401405
});
402406
```
403407
404-
# beforeDeleteFile Triggers
408+
## beforeDeleteFile
405409
406410
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:
407411
@@ -425,7 +429,7 @@ Parse.Cloud.beforeDeleteFile(async (request) => {
425429
});
426430
```
427431
428-
# afterDeleteFile Triggers
432+
## afterDeleteFile
429433
430434
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.
431435
@@ -439,13 +443,15 @@ Parse.Cloud.afterDeleteFile(async (request) => {
439443
});
440444
```
441445
442-
# beforeFind Triggers
446+
# Find Triggers
447+
448+
## beforeFind
443449
444450
*Available only on parse-server cloud code starting 2.2.20*
445451
446452
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.
447453
448-
## Examples
454+
### Examples
449455
450456
```javascript
451457
// Properties available
@@ -502,7 +508,7 @@ Parse.Cloud.beforeFind('MyObject2', (req) => {
502508

503509
```
504510
505-
## Predefined Classes
511+
### Predefined Classes
506512
507513
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:
508514
@@ -512,7 +518,7 @@ Parse.Cloud.beforeFind(Parse.User, async (request) => {
512518
})
513519
```
514520
515-
# afterFind Triggers
521+
## afterFind
516522
517523
*Available only on parse-server cloud code starting 2.2.25*
518524
@@ -524,7 +530,7 @@ Parse.Cloud.afterFind('MyCustomClass', async (request) => {
524530
})
525531
```
526532
527-
## Predefined Classes
533+
### Predefined Classes
528534
529535
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:
530536
@@ -534,7 +540,9 @@ Parse.Cloud.afterFind(Parse.User, async (request) => {
534540
})
535541
```
536542
537-
# beforeLogin Triggers
543+
# Session Triggers
544+
545+
## beforeLogin
538546
539547
*Available only on parse-server cloud code starting 3.3.0*
540548
@@ -549,21 +557,21 @@ Parse.Cloud.beforeLogin(async request => {
549557
});
550558
```
551559
552-
## Some considerations to be aware of
560+
### Some considerations to be aware of
553561
554562
- It waits for any promises to resolve
555563
- The user is not available on the request object - the user has not yet been provided a session until after beforeLogin is successfully completed
556564
- Like `afterSave` on `Parse.User`, it will not save mutations to the user unless explicitly saved
557565
558-
### The trigger will run...
566+
#### The trigger will run...
559567
- On username & password logins
560568
- On `authProvider` logins
561569
562-
### The trigger won't run...
570+
#### The trigger won't run...
563571
- On sign up
564572
- If the login credentials are incorrect
565573
566-
# afterLogout Triggers
574+
## afterLogout
567575
568576
*Available only on parse-server cloud code starting 3.10.0*
569577
@@ -578,10 +586,10 @@ Parse.Cloud.afterLogout(async request => {
578586
});
579587
```
580588
581-
## Some considerations to be aware of
589+
### Some considerations to be aware of
582590
- Like with `afterDelete` triggers, the `_Session` object that is contained in the request has already been deleted.
583591
584-
### The trigger will run...
592+
#### The trigger will run...
585593
- when the user logs out and a `_Session` object was deleted
586594
587595
### The trigger won't run...

0 commit comments

Comments
 (0)