0.47.0 • Published 4 days ago

@sqlfx/sql v0.47.0

Weekly downloads
-
License
MIT
Repository
github
Last release
4 days ago

sqlfx

A SQL toolkit for Effect-TS

https://tim-smart.github.io/sqlfx

Basic example

import { pipe } from "effect/Function"
import * as Config from "effect/Config"
import * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

const PgLive = Pg.makeLayer({
  database: Config.succeed("effect_pg_dev"),
})

const program = Effect.gen(function* (_) {
  const sql = yield* _(Pg.tag)

  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.provideLayer(PgLive), Effect.runPromise)

INSERT resolver

import { pipe } from "effect/Function"
import * as Effect from "effect/Effect"
import * as Schema from "@effect/schema/Schema"
import * as Pg from "@sqlfx/pg"

class Person extends Schema.class({
  id: Schema.number,
  name: Schema.string,
  createdAt: Schema.DateFromSelf,
  updatedAt: Schema.DateFromSelf,
}) {}

const InsertPersonSchema = pipe(
  Person.schemaStruct(),
  Schema.omit("id", "createdAt", "updatedAt"),
)

export const makePersonService = Effect.gen(function* (_) {
  const sql = yield* _(Pg.tag)

  const insert = sql.resolver(
    "InsertPerson",
    InsertPersonSchema,
    Person.schema(),
    requests =>
      sql`
        INSERT INTO people
        ${sql.insert(requests)}
        RETURNING people.*
      `,
  ).execute

  return { insert }
})

SELECT resolver

import * as Effect from "effect/Effect"
import * as Schema from "@effect/schema/Schema"
import * as Pg from "@sqlfx/pg"

class Person extends Schema.Class({
  id: Schema.number,
  name: Schema.string,
  createdAt: Schema.DateFromSelf,
  updatedAt: Schema.DateFromSelf,
}) {}

export const makePersonService = Effect.gen(function* (_) {
  const sql = yield* _(Pg.tag)

  const getByIdResolver = sql.idResolver(
    "GetPersonById",
    Schema.number,
    Person.schema(),
    _ => _.id,
    ids => sql`SELECT * FROM people WHERE id IN ${sql(ids)}`,
  )

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

  return { getById }
})

Building queries

Safe interpolation

import * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

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

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

Unsafe interpolation

import * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

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

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

    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 * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

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

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

OR

import * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

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

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

Mixed

import * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

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

    const statement = sql`SELECT * FROM people WHERE ${sql.or([
      sql`name IN ${sql(names)}`,
      sql.and([
        `created_at >${sql(afterCursor)}`,
        `created_at < ${sql(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 * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"

export default Effect.flatMap(
  Pg.tag,
  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 * as Effect from "effect/Effect"
import * as Pg from "@sqlfx/pg"
import * as Migrator from "@sqlfx/pg/Migrator"
import * as Config from "effect/Config"
import { fileURLToPath } from "node:url"
import * as Layer from "effect/Layer"
import { pipe } from "effect/Function"

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

const PgLive = Pg.makeLayer({
  database: Config.succeed("example_database"),
})

const MigratorLive = Layer.provide(
  Migrator.makeLayer({
    directory: fileURLToPath(new URL("migrations", import.meta.url)),
    // Where to put the `_schema.sql` file
    schemaDirectory: "src/migrations",
  }),
  PgLive,
)

const EnvLive = Layer.mergeAll(PgLive, MigratorLive)

pipe(
  program,
  Effect.provideLayer(EnvLive),
  Effect.tapErrorCause(Effect.logErrorCause),
  Effect.runFork,
)
0.47.0

4 days ago

0.46.1

1 month ago

0.46.0

2 months ago

0.45.0

3 months ago

0.44.2

3 months ago

0.44.1

3 months ago

0.44.0

3 months ago

0.43.2

3 months ago

0.43.0

3 months ago

0.43.1

3 months ago

0.42.0

4 months ago

0.41.0

4 months ago

0.40.0

4 months ago

0.39.1

4 months ago

0.39.0

4 months ago

0.38.0

4 months ago

0.37.0

5 months ago

0.36.1

5 months ago

0.36.0

5 months ago

0.35.0

5 months ago

0.34.0

5 months ago

0.20.0

8 months ago

0.19.0

8 months ago

0.17.2

8 months ago

0.19.1

8 months ago

0.19.2

8 months ago

0.19.3

8 months ago

0.19.4

8 months ago

0.19.5

8 months ago

0.19.6

8 months ago

0.30.3

6 months ago

0.11.0

10 months ago

0.13.0

10 months ago

0.15.0

9 months ago

0.17.0

8 months ago

0.17.1

8 months ago

0.32.0

6 months ago

0.30.2

6 months ago

0.30.1

6 months ago

0.30.0

6 months ago

0.29.0

7 months ago

0.27.0

7 months ago

0.25.1

7 months ago

0.25.0

7 months ago

0.23.1

7 months ago

0.21.3

7 months ago

0.23.0

7 months ago

0.21.2

7 months ago

0.21.1

7 months ago

0.9.0

10 months ago

0.7.2

10 months ago

0.7.1

10 months ago

0.7.0

10 months ago

0.29.1

7 months ago

0.21.0

7 months ago

0.18.1

8 months ago

0.33.1

5 months ago

0.33.0

5 months ago

0.12.0

10 months ago

0.14.0

10 months ago

0.14.1

10 months ago

0.16.0

9 months ago

0.14.2

10 months ago

0.16.1

9 months ago

0.18.0

8 months ago

0.10.0

10 months ago

0.31.1

6 months ago

0.31.0

6 months ago

0.28.0

7 months ago

0.26.2

7 months ago

0.26.1

7 months ago

0.26.0

7 months ago

0.24.0

7 months ago

0.22.1

7 months ago

0.22.0

7 months ago

0.8.0

10 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.5.2

11 months ago

0.5.1

12 months ago

0.5.0

12 months ago

0.4.4

12 months ago

0.4.3

12 months ago

0.4.2

12 months ago

0.4.1

12 months ago

0.4.0

12 months ago

0.3.0

12 months ago

0.2.4

12 months ago

0.2.3

12 months ago

0.2.2

12 months ago

0.2.1

12 months ago

0.2.0

12 months ago

0.1.3

12 months ago

0.1.2

12 months ago

0.1.1

12 months ago

0.1.0

12 months ago