0.18.14 • Published 8 months ago

@effect/sql v0.18.14

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

Effect SQL

A SQL toolkit for Effect.

Basic example

import { Effect, Struct, pipe } from "effect"
import { PgClient } from "@effect/sql-pg"
import { SqlClient } from "@effect/sql"

const SqlLive = PgClient.layer({
  database: "effect_pg_dev"
})

const program = Effect.gen(function* () {
  const sql = yield* SqlClient.SqlClient

  const people = yield* sql<{
    readonly id: number
    readonly name: string
  }>`SELECT id, name FROM people`

  yield* Effect.log(`Got ${people.length} results!`)
})

pipe(program, Effect.provide(SqlLive), Effect.runPromise)

Alternatively, you can also create the SqlLive layer using the PgClient.layerConfig constructor.

import { Config } from "effect"
import { PgClient } from "@effect/sql-pg"

const SqlLive = PgClient.layer({
  database: Config.string("DATABASE")
})

Migrating from sqlfx

If you are coming from the sqlfx package, here are some differences that should be noted:

All the modules are now re-exported from the top level for easy access

For example, to create the client Layer, instead of:

import { Config } from "effect"
import { PgClient } from "@sqlfx/pg"

const SqlLive = PgClient.makeLayer({
  database: Config.succeed("effect_pg_dev")
})

You now do:

import { PgClient } from "@effect/sql-pg"

const SqlLive = PgClient.layer({
  database: "effect_pg_dev"
})

The default table name for migrations has changed

To continue using your sqlfx migrations table, you can setup your migrator Layer as below:

import { PgMigrator } from "@effect/sql-pg"

const MigratorLive = Layer.provide(
  PgMigrator.layer({
    loader: PgMigrator.fromFileSystem(
      fileURLToPath(new URL("migrations", import.meta.url))
    ),
    table: "sqlfx_migrations"
  }),
  SqlLive
)

Or you can rename the sqlfx_migrations table to effect_sql_migrations.

The resolver & schema apis have moved

  • sql.resolver -> SqlResolver.ordered
  • sql.resolverVoid -> SqlResolver.void
  • sql.resolverId -> SqlResolver.findById
  • sql.resolverIdMany -> SqlResolver.grouped
  • sql.resolverSingle* has been removed in favour of using the effect/Cache module with the schema apis
  • sql.schema -> SqlSchema.findAll
  • sql.schemaSingle -> SqlSchema.single
  • sql.schemaSingleOption -> SqlSchema.findOne
  • sql.schemaVoid -> SqlSchema.void

The array helper has moved

In sqlfx you could pass an array to the sql(array) function to pass an list of items to a SQL IN clause. Now you have to use sql.in(array).

INSERT resolver

import { Effect, Schema, pipe } from "effect"
import { SqlClient } from "@effect/sql"

class Person extends Schema.Class<Person>("Person")({
  id: Schema.Number,
  name: Schema.String,
  createdAt: Schema.DateFromSelf,
  updatedAt: Schema.DateFromSelf
}) {}

const InsertPersonSchema = Schema.Struct(
  Struct.omit(Person.fields, "id", "createdAt", "updatedAt")
)

export const makePersonService = Effect.gen(function* () {
  const sql = yield* SqlClient.SqlClient

  const InsertPerson = yield* SqlResolver.ordered("InsertPerson", {
    Request: InsertPersonSchema,
    Result: Person,
    execute: (requests) =>
      sql`
        INSERT INTO people
        ${sql.insert(requests)}
        RETURNING people.*
      `
  })

  const insert = InsertPerson.execute

  return { insert }
})

SELECT resolver

import { Effect, Schema, pipe } from "effect"
import { SqlResolver, SqlClient } from "@effect/sql"

class Person extends Schema.Class<Person>("Person")({
  id: Schema.Number,
  name: Schema.String,
  createdAt: Schema.DateFromSelf,
  updatedAt: Schema.DateFromSelf
}) {}

export const makePersonService = Effect.gen(function* () {
  const sql = yield* SqlClient.SqlClient

  const GetById = yield* SqlResolver.findById("GetPersonById", {
    Id: Schema.Number,
    Result: Person,
    ResultId: (_) => _.id,
    execute: (ids) => sql`SELECT * FROM people WHERE ${sql.in("id", ids)}`
  })

  const getById = (id: number) =>
    Effect.withRequestCaching("on")(GetById.execute(id))

  return { getById }
})

Building queries

Safe interpolation

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

export const make = (limit: number) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM people LIMIT ${limit}`
    // e.g. SELECT * FROM people LIMIT ?
  })

Identifiers

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

const table = "people"

export const make = (limit: number) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM ${sql(table)} LIMIT ${limit}`
    // e.g. SELECT * FROM "people" LIMIT ?
  })

Unsafe interpolation

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

type OrderBy = "id" | "created_at" | "updated_at"
type SortOrder = "ASC" | "DESC"

export const make = (orderBy: OrderBy, sortOrder: SortOrder) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM people ORDER BY ${sql(orderBy)} ${sql.unsafe(sortOrder)}`
    // e.g. SELECT * FROM people ORDER BY `id` ASC
  })

Where clause combinators

AND

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

export const make = (names: string[], cursor: string) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM people WHERE ${sql.and([
      sql.in("name", names),
      sql`created_at < ${cursor}`
    ])}`
    // SELECT * FROM people WHERE ("name" IN (?,?,?) AND created_at < ?)
  })

OR

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

export const make = (names: string[], cursor: Date) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM people WHERE ${sql.or([
      sql.in("name", names),
      sql`created_at < ${cursor}`
    ])}`
    // SELECT * FROM people WHERE ("name" IN (?,?,?) OR created_at < ?)
  })

Mixed

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

export const make = (names: string[], afterCursor: Date, beforeCursor: Date) =>
  Effect.gen(function* () {
    const sql = yield* SqlClient.SqlClient

    const statement = sql`SELECT * FROM people WHERE ${sql.or([
      sql.in("name", names),
      sql.and([`created_at > ${afterCursor}`, `created_at < ${beforeCursor}`])
    ])}`
    // SELECT * FROM people WHERE ("name" IN (?,?,?) OR (created_at > ? AND created_at < ?))
  })

Migrations

A Migrator module is provided, for running migrations.

Migrations are forward-only, and are written in Typescript as Effect's.

Here is an example migration:

// src/migrations/0001_add_users.ts

import { Effect } from "effect"
import { SqlClient } from "@effect/sql"

export default Effect.flatMap(
  SqlClient.SqlClient,
  (sql) => sql`
    CREATE TABLE users (
      id serial PRIMARY KEY,
      name varchar(255) NOT NULL,
      created_at TIMESTAMP NOT NULL DEFAULT NOW(),
      updated_at TIMESTAMP NOT NULL DEFAULT NOW()
    )
  `
)

To run your migrations:

// src/main.ts

import { Effect, Layer, pipe } from "effect"
import { NodeContext, NodeRuntime } from "@effect/platform-node"
import { PgClient, PgMigrator } from "@effect/sql-pg"
import { fileURLToPath } from "node:url"

const program = Effect.gen(function* () {
  // ...
})

const SqlLive = PgClient.layer({
  database: "example_database"
})

const MigratorLive = PgMigrator.layer({
  loader: PgMigrator.fromFileSystem(
    fileURLToPath(new URL("migrations", import.meta.url))
  ),
  // Where to put the `_schema.sql` file
  schemaDirectory: "src/migrations"
}).pipe(Layer.provide(SqlLive))

const EnvLive = Layer.mergeAll(SqlLive, MigratorLive).pipe(
  Layer.provide(NodeContext.layer)
)

pipe(program, Effect.provide(EnvLive), NodeRuntime.runMain)
0.18.11

8 months ago

0.18.10

8 months ago

0.18.13

8 months ago

0.18.12

8 months ago

0.18.14

8 months ago

0.18.9

8 months ago

0.18.8

8 months ago

0.18.7

8 months ago

0.18.4

8 months ago

0.18.5

8 months ago

0.18.6

8 months ago

0.18.1

8 months ago

0.18.3

8 months ago

0.18.0

8 months ago

0.17.0

8 months ago

0.16.6

8 months ago

0.16.5

8 months ago

0.8.5

10 months ago

0.4.9

12 months ago

0.8.4

10 months ago

0.4.8

12 months ago

0.8.7

10 months ago

0.8.6

10 months ago

0.2.17

1 year ago

0.13.0

9 months ago

0.13.1

9 months ago

0.13.2

9 months ago

0.13.3

9 months ago

0.13.4

9 months ago

0.2.16

1 year ago

0.2.15

1 year ago

0.3.0

1 year ago

0.3.6

1 year ago

0.3.5

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.7.0

11 months ago

0.3.4

1 year ago

0.3.3

1 year ago

0.4.20

11 months ago

0.9.7

10 months ago

0.4.21

11 months ago

0.9.4

10 months ago

0.9.3

10 months ago

0.9.6

10 months ago

0.9.5

10 months ago

0.4.26

11 months ago

0.4.27

11 months ago

0.4.24

11 months ago

0.4.25

11 months ago

0.4.22

11 months ago

0.4.23

11 months ago

0.4.19

12 months ago

0.10.1

9 months ago

0.10.2

9 months ago

0.10.3

9 months ago

0.14.0

9 months ago

0.10.4

9 months ago

0.14.1

8 months ago

0.4.10

12 months ago

0.10.0

10 months ago

0.4.17

12 months ago

0.4.18

12 months ago

0.4.15

12 months ago

0.4.16

12 months ago

0.4.13

12 months ago

0.4.14

12 months ago

0.4.11

12 months ago

0.4.12

12 months ago

0.8.1

11 months ago

0.4.5

12 months ago

0.8.0

11 months ago

0.4.4

12 months ago

0.8.3

10 months ago

0.4.7

12 months ago

0.8.2

10 months ago

0.4.6

12 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.4.3

12 months ago

0.4.2

12 months ago

0.11.0

9 months ago

0.11.1

9 months ago

0.11.2

9 months ago

0.11.3

9 months ago

0.15.0

8 months ago

0.15.1

8 months ago

0.3.18

12 months ago

0.9.0

10 months ago

0.5.3

11 months ago

0.9.2

10 months ago

0.9.1

10 months ago

0.5.0

11 months ago

0.5.2

11 months ago

0.5.1

11 months ago

0.3.9

1 year ago

0.3.17

12 months ago

0.3.16

12 months ago

0.3.15

12 months ago

0.3.14

12 months ago

0.3.13

12 months ago

0.3.12

12 months ago

0.3.11

12 months ago

0.3.10

1 year ago

0.16.3

8 months ago

0.16.4

8 months ago

0.12.0

9 months ago

0.12.1

9 months ago

0.12.2

9 months ago

0.12.3

9 months ago

0.16.0

8 months ago

0.12.4

9 months ago

0.16.1

8 months ago

0.12.5

9 months ago

0.16.2

8 months ago

0.12.6

9 months ago

0.6.3

11 months ago

0.6.2

11 months ago

0.6.1

11 months ago

0.6.0

11 months ago

0.2.14

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.1

1 year ago

0.2.0

1 year ago

0.1.17

1 year ago

0.1.16

1 year ago

0.1.15

1 year ago

0.1.13

1 year ago

0.1.14

1 year ago

0.1.11

1 year ago

0.1.12

1 year ago

0.1.10

1 year ago

0.1.9

1 year ago

0.1.8

1 year ago

0.1.7

1 year ago

0.1.4

1 year ago

0.1.6

1 year ago

0.1.5

1 year ago

0.1.2

1 year ago

0.1.1

1 year ago

0.1.3

1 year ago

0.1.0

1 year ago