Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 22 additions & 45 deletions usage/tools/cloudcode.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ As of January 2025, we've started adding optional backend functionality for Powe

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

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

<Info>
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.
</Info>

Phase 2 on our roadmap involves fully integrating CloudCode into the PowerSync Cloud environment.
Phase 2 on our roadmap involves fully integrating CloudCode into the PowerSync Cloud environment.


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

The body of the request payload should look like this:
```
```json
{
"op": "PUT",
"table": "lists",
"id": "61d19021-0565-4686-acc4-3ea4f8c48839",
"data": {
"created_at": "2024-10-31 10:33:24",
"name": "Name",
"owner_id": "8ea4310a-b7c0-4dd7-ae54-51d6e1596b83"
}
"batch": [{
"op": "PUT",
"table": "lists",
"id": "61d19021-0565-4686-acc4-3ea4f8c48839",
"data": {
"created_at": "2024-10-31 10:33:24",
"name": "Name",
"owner_id": "8ea4310a-b7c0-4dd7-ae54-51d6e1596b83"
}
}]
}
```

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.
* `batch` should be an array of operations from the PowerSync client SDK.
* `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.

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


## Customization required
## Customization

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).
You can make changes to the way the `upload` task writes data to the source MongoDB database.

Here is how:

1. Open the **CloudCode** section at the top of the IDE.
1. Go to **CloudCode** at the top of the IDE in your JourneyApps Platform project
2. Select and expand the `upload` task in the panel on the left.
3. The `index.ts` contains the entry point function that accepts the HTTP request.
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:

```typescript
/**
* Line 13 in upload/persister.ts
* Sample schema using to-do list demo. Update this based on your DB schema.
*/
export const schema = {
lists: {
_id: types.string,
created_at: types.date,
name: types.string,
owner_id: types.string
},
todos: {
_id: types.string,
completed: types.boolean,
created_at: types.date,
created_by: types.string,
description: types.string,
list_id: types.string,
completed_at: types.date,
completed_by: types.string
}
};
```
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.

## Production considerations

Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.
Before going into production with this solution, you will need to set up authentication on the HTTP endpoints exposed by the CloudCode tasks.

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

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

Loading