Skip to content

Commit

Permalink
docs for SQL-in-ReScript mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zth committed Apr 30, 2024
1 parent 354b930 commit 4539527
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions RESCRIPT.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ This small readme focuses on the differences between regular `pgtyped` and this
## Differences to regular `pgtyped`

- It outputs ReScript instead of TypeScript.
- It only supports using SQL files, not SQL-in-ReScript (yet! support coming at some point).

Everything else should work pretty much the same as stock `pgtyped`.

## Getting started

Make sure you have ReScript `v11.1`, and [ReScript Core](https://github.com/rescript-association/rescript-core) (plus `RescriptCore` opened globally).

1. `npm install -D pgtyped-rescript`
1. `npm install -D pgtyped-rescript rescript-embed-lang` (install `rescript-embed-lang` if you want to use the SQL-in-ReScript mode)
2. `npm install @pgtyped/runtime pg rescript @rescript/core` (`@pgtyped/runtime` and `pg` are the only required runtime dependencies of pgtyped)
3. Create a PgTyped `pgtyped.config.json` file.
4. Run `npx pgtyped-rescript -w -c pgtyped.config.json` to start PgTyped in watch mode.
Expand All @@ -29,18 +28,27 @@ Here's a sample `pgtyped.config.json` file:
"mode": "sql",
"include": "**/*.sql",
"emitTemplate": "{{dir}}/{{name}}__sql.res"
},
{
"mode": "res",
"include": "**/*.res",
"emitTemplate": "{{dir}}/{{name}}__sql.res"
}
],
"srcDir": "./src/sql",
"srcDir": "./src",
"dbUrl": "postgres://pgtyped:pgtyped@localhost/pgtyped"
}
```

> Notice how we're configuring what we want the generated ReScript files to be named under `emitTemplate`.
> Notice how we're configuring what we want the generated ReScript files to be named under `emitTemplate`. For SQL-in-ReScript mode, you need to configure the generated file names exactly as above.
Please refer to the `pgtyped` docs for all configuration options.

Please refer to the `pgtyped` docs for all configuration options. The only thing that's important to remember for the ReScript version is that _only SQL files are supported_.
## Separate SQL files mode

Now we can create a SQL file in `src/sql`. We call this one `books.sql`:
`pgtyped-rescript` supports writing queries in separate SQL files, as well as embedded directly in ReScript source code. Below details the separate SQL files approach:

Create a SQL file anywhere in `src`. We call this one `books.sql`:

```sql
/* @name findBookById */
Expand Down Expand Up @@ -76,6 +84,28 @@ let main = async () => {
main()->Promise.done
```

## SQL-in-ReScript

Optionally, you can write SQL directly in your ReScript code and have a seamless, fully typed experience. The above example but with SQL-in-ReScript:

```rescript
let query = %sql.one(`
/* @name findBookById */
SELECT * FROM books WHERE id = :id!;
`)
let res = await client->query({id: 1})
Console.log(res)
```

In order for this mode to work, you need one more thing - configure the `rescript-embed-lang` PPX in `rescript.json`:

```json
"ppx-flags": ["rescript-embed-lang/ppx"],
```

With that, you should be able to write queries directly in your ReScript source, and with the `watch` mode enabled have a seamless experience with types autogenerated and wired up for you.

## API

### `PgTyped`
Expand Down

0 comments on commit 4539527

Please sign in to comment.