You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Made small changes to the CloudCode doc to expand on schema validation and the updated JSON example for the upload endpoint
* Remove outdated part about schema
---------
Co-authored-by: Conrad Hofmeyr <cahofmeyr@gmail.com>
Copy file name to clipboardExpand all lines: usage/tools/cloudcode.mdx
+22-45Lines changed: 22 additions & 45 deletions
Original file line number
Diff line number
Diff line change
@@ -7,13 +7,13 @@ As of January 2025, we've started adding optional backend functionality for Powe
7
7
8
8
This makes PowerSync easier to implement for developers who prefer not having to maintain their own backend code and infrastructure (PowerSync's [usual architecture](/installation/app-backend-setup) is to use your own backend to process writes and generate JWTs).
9
9
10
-
We are approaching this in phases, and phase 1 allows using the CloudCode feature of JourneyApps Platform, a [sibling product](https://www.powersync.com/company) of PowerSync. [CloudCode](https://docs.journeyapps.com/reference/cloudcode/cloudcode-overview) is a serverless cloud functions engine based on Node.js and AWS Lambda. It's provided as a fully-managed service running on the same cloud infrastructure as the rest of PowerSync Cloud. PowerSync and JourneyApps Platform share the same login system, so you don’t need to create a separate account to use CloudCode.
10
+
We are approaching this in phases, and phase 1 allows using the CloudCode feature of JourneyApps Platform, a [sibling product](https://www.powersync.com/company) of PowerSync. [CloudCode](https://docs.journeyapps.com/reference/cloudcode/cloudcode-overview) is a serverless cloud functions engine based on Node.js and AWS Lambda. It's provided as a fully-managed service running on the same cloud infrastructure as the rest of PowerSync Cloud. PowerSync and JourneyApps Platform share the same login system, so you don’t need to create a separate account to use CloudCode.
11
11
12
12
<Info>
13
13
We are currently making JourneyApps Platform CloudCode available for free to all our customers who use PowerSync with MongoDB. It does require a bit of "white glove" onboarding from our team. [Contact us](/resources/contact-us) if you want to use this functionality.
14
14
</Info>
15
15
16
-
Phase 2 on our roadmap involves fully integrating CloudCode into the PowerSync Cloud environment.
16
+
Phase 2 on our roadmap involves fully integrating CloudCode into the PowerSync Cloud environment.
17
17
18
18
19
19
# Using CloudCode in JourneyApps Platform for MongoDB Backend Functionality
@@ -169,65 +169,42 @@ You would call the `upload` HTTP API endpoint when you [implement](/installation
169
169
Send an HTTP POST request to `<domain_name>.poweredbyjourney.com/upload`.
170
170
171
171
The body of the request payload should look like this:
The `op` refers to the type of operation recorded by the PowerSync client SDK (`PUT`, `PATCH` or `DELETE`). Refer to [Writing Client Changes](/installation/app-backend-setup/writing-client-changes) for details.
187
+
*`batch` should be an array of operations from the PowerSync client SDK.
188
+
*`op` refers to the type of each operation recorded by the PowerSync client SDK (`PUT`, `PATCH` or `DELETE`). Refer to [Writing Client Changes](/installation/app-backend-setup/writing-client-changes) for details.
186
189
187
190
The API will respond with HTTP status `200` if the write was successful.
188
191
189
192
190
-
## Customization required
193
+
## Customization
191
194
192
-
You can make changes to the way the `upload` task writes data to the source MongoDB database. At a minimum, at this time you will need to modify it to take your specific MongoDB database "schema" into consideration (Note: We are working on updating the template to not require adapting it to your schema. We will update this documentation page accordingly once that change is shipped).
195
+
You can make changes to the way the `upload` task writes data to the source MongoDB database.
193
196
194
197
Here is how:
195
198
196
-
1.Open the**CloudCode**section at the top of the IDE.
199
+
1.Go to**CloudCode** at the top of the IDE in your JourneyApps Platform project
197
200
2. Select and expand the `upload` task in the panel on the left.
198
-
3. The `index.ts` contains the entry point function that accepts the HTTP request.
199
-
4. The `persister.ts` file connects to the MongoDB database and writes the data to the MongoDB database. You can update this file to introduce your database schema. The default template has an example schema for a To-Do List application:
200
-
201
-
```typescript
202
-
/**
203
-
* Line 13 in upload/persister.ts
204
-
* Sample schema using to-do list demo. Update this based on your DB schema.
205
-
*/
206
-
exportconst schema = {
207
-
lists: {
208
-
_id: types.string,
209
-
created_at: types.date,
210
-
name: types.string,
211
-
owner_id: types.string
212
-
},
213
-
todos: {
214
-
_id: types.string,
215
-
completed: types.boolean,
216
-
created_at: types.date,
217
-
created_by: types.string,
218
-
description: types.string,
219
-
list_id: types.string,
220
-
completed_at: types.date,
221
-
completed_by: types.string
222
-
}
223
-
};
224
-
```
201
+
3. The `index.ts` contains the entry point function that accepts the HTTP request and has a `MongoDBStorage` class which interacts with the MongoDB database to perform inserts, updates and deletes. To adjust how updates are performed, take a look at the `updateBatch` function.
225
202
226
203
## Production considerations
227
204
228
-
Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.
205
+
Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.
229
206
230
-
In addition, if you need more data validations and/or authorization than what is provided by the template, that will need to be customized too.
207
+
If you need more data validations and/or authorization than what is provided by the template, that will need to be customized too. Consider introducing schema validation of the data being written to the source MongoDB database. You should use a [purpose-built](https://json-schema.org/tools?query=&sortBy=name&sortOrder=ascending&groupBy=toolingTypes&licenses=&languages=&drafts=&toolingTypes=&environments=&showObsolete=false) library for this, and use [MongoDB Schema Validation](https://www.mongodb.com/docs/manual/core/schema-validation/) to enforce the types in the database.
231
208
232
-
Please [contact us](/resources/contact-us) for assistance on either of the above.
209
+
Please [contact us](/resources/contact-us) for assistance on any of the above.
0 commit comments