Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add documentation & example for ORM support #3

Merged
merged 2 commits into from
Nov 9, 2023
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
23 changes: 23 additions & 0 deletions javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ pnpm add retoolrpc

```javascript
import { RetoolRPC } from 'retoolrpc'
// for CommonJS, uses `require`, e.g.:
// var { RetoolRPC} = require('retoolrpc')

const rpc = new RetoolRPC({
apiToken: 'your-api-token-here', // Replace this token with your API token
Expand All @@ -49,3 +51,24 @@ rpc.register({

await rpc.listen()
```

## ORM Support

For users of [Sequelize](https://sequelize.org/), we offer an ORM mixin that enables the addition of fundamental model functions with a single function call, `registerModel`. When you register a model with `rpc`, it automatically registers various remote functions for the model, including `create`, `update`, `createOrUpdate`, `findByPk`, `findBy`, and `findAll`. You can find additional details [here](https://github.com/tryretool/retoolrpc/blob/main/javascript/src/addons/sequelize.ts#L5-L14).

Following is an example of registering a `User` model:
```javascript
import { RetoolRPC, sequelizeMixin } from 'retoolrpc'
import { User } from './orm/models' // the path to your model may be different

const CustomRPC = sequelizeMixin(RetoolRPC)
const rpc = new CustomRPC({ ... })

rpc.registerModel({
model: Experiment,
findByAttributes: ['id', 'name'],
writeAttributes: ['name'],
})
```

We plan to support other ORMs in the future.