Skip to content

Commit a890ae2

Browse files
Changes to the CloudCode template docs. (#96)
* 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>
1 parent 795cfed commit a890ae2

File tree

1 file changed

+22
-45
lines changed

1 file changed

+22
-45
lines changed

usage/tools/cloudcode.mdx

Lines changed: 22 additions & 45 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,65 +169,42 @@ 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

185-
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.
186189

187190
The API will respond with HTTP status `200` if the write was successful.
188191

189192

190-
## Customization required
193+
## Customization
191194

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

194197
Here is how:
195198

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
197200
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-
```
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.
225202

226203
## Production considerations
227204

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

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

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

0 commit comments

Comments
 (0)