Skip to content
Merged
Changes from 1 commit
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
59 changes: 19 additions & 40 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,16 +169,18 @@ 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"
}
}]
}
```

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

1. Open the **CloudCode** section at the top of the IDE.
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.

If you need more data validations and/or authorization than what is provided by the template, that will need to be customized too.

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.
For production applications you would also want to 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 to enforce the types in the database.

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

Loading