https://github.com/sukovanej/effect-pg
node-pg wrapper for effect-ts
https://github.com/sukovanej/effect-pg
effect-ts fp-ts node-pg postgres
Last synced: 10 months ago
JSON representation
node-pg wrapper for effect-ts
- Host: GitHub
- URL: https://github.com/sukovanej/effect-pg
- Owner: sukovanej
- License: mit
- Archived: true
- Created: 2023-04-10T20:35:59.000Z (almost 3 years ago)
- Default Branch: master
- Last Pushed: 2024-06-04T11:51:10.000Z (over 1 year ago)
- Last Synced: 2025-04-12T20:54:45.398Z (10 months ago)
- Topics: effect-ts, fp-ts, node-pg, postgres
- Language: TypeScript
- Homepage: https://sukovanej.github.io/effect-pg/
- Size: 863 KB
- Stars: 13
- Watchers: 3
- Forks: 0
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# effect-pg
[node-pg](https://github.com/brianc/node-postgres) wrapper for
[effect](https://github.com/Effect-TS).
**Under development**
## Quickstart
Install the package using
```bash
pnpm install effect-pg effect
```
The following example assumes you have a postgres running. Setup the environment
variables as follows.
```
$ export POSTGRES_USER=
$ export POSTGRES_PASSWORD=
$ export POSTGRES_DATABASE=
```
Optionally, also set `POSTGRES_HOST` and `POSTGRES_NAME`. The example below will
create a new `users` table, insert a single row and fetch the row. The persisted
row is immediately fetched and logged out.
```typescript
import { Schema } from "@effect/schema"
import { Effect, pipe } from "effect"
import { PgLayer, PgQuery } from "effect-pg"
const User = Schema.Struct({ name: Schema.String })
const createUsersTable = PgQuery.all(
"CREATE TABLE IF NOT EXISTS users (name TEXT NOT NULL)"
)
const insertUser = PgQuery.all("INSERT INTO users (name) VALUES ($1)")
const selectUser = PgQuery.one("SELECT * FROM users", User)
pipe(
createUsersTable(),
Effect.flatMap(() => insertUser("patrik")),
Effect.flatMap(() => selectUser()),
Effect.flatMap((result) => Effect.log(`User: ${JSON.stringify(result)}`)),
Effect.provide(PgLayer.Client),
Effect.provide(PgLayer.setConfig()),
Effect.runPromise
)
```
## Contributing
### Local development
Spawn a postgres instance.
```bash
$ docker run -d -p 5432:5432 --name test-postgres -e POSTGRES_PASSWORD=test -e POSTGRES_USER=test postgres
```
Create a `.env` file.
```bash
TEST_POSTGRES_USER=test
TEST_POSTGRES_DATABASE=test
TEST_POSTGRES_PASSWORD=test
```
Run tests.
```bash
pnpm test
```