Skip to content

Commit 9824645

Browse files
author
Mike Barnes
committed
Made small changes to the CloudCode doc to expand on schema validation and the updated JSON example for the upload endpoint
1 parent 795cfed commit 9824645

File tree

1 file changed

+19
-40
lines changed

1 file changed

+19
-40
lines changed

usage/tools/cloudcode.mdx

Lines changed: 19 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ As of January 2025, we've started adding optional backend functionality for Powe
77

88
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).
99

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.
1111

1212
<Info>
1313
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.
1414
</Info>
1515

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.
1717

1818

1919
# Using CloudCode in JourneyApps Platform for MongoDB Backend Functionality
@@ -169,16 +169,18 @@ You would call the `upload` HTTP API endpoint when you [implement](/installation
169169
Send an HTTP POST request to `<domain_name>.poweredbyjourney.com/upload`.
170170

171171
The body of the request payload should look like this:
172-
```
172+
```json
173173
{
174-
"op": "PUT",
175-
"table": "lists",
176-
"id": "61d19021-0565-4686-acc4-3ea4f8c48839",
177-
"data": {
178-
"created_at": "2024-10-31 10:33:24",
179-
"name": "Name",
180-
"owner_id": "8ea4310a-b7c0-4dd7-ae54-51d6e1596b83"
181-
}
174+
"batch": [{
175+
"op": "PUT",
176+
"table": "lists",
177+
"id": "61d19021-0565-4686-acc4-3ea4f8c48839",
178+
"data": {
179+
"created_at": "2024-10-31 10:33:24",
180+
"name": "Name",
181+
"owner_id": "8ea4310a-b7c0-4dd7-ae54-51d6e1596b83"
182+
}
183+
}]
182184
}
183185
```
184186

@@ -195,39 +197,16 @@ Here is how:
195197

196198
1. Open the **CloudCode** section at the top of the IDE.
197199
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-
export const 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-
```
200+
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.
225201

226202
## Production considerations
227203

228-
Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.
204+
Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.
205+
206+
If you need more data validations and/or authorization than what is provided by the template, that will need to be customized too.
229207

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.
208+
For production applications you would also want to consider introducing schema validation of the data being written to the source MongoDB database.
209+
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 to enforce the types in the database.
231210

232211
Please [contact us](/resources/contact-us) for assistance on either of the above.
233212

0 commit comments

Comments
 (0)