Effect sql
Effect SQL fixtures: generate.runtime: 'effect-v3' | 'effect-v4-unstable' emits functions that return Effect values
and read SqlClient.SqlClient from the Effect environment instead of taking a sqlfu Client.
emits Effect v3 SQL functions that require SqlClient from context
Section titled “emits Effect v3 SQL functions that require SqlClient from context”input
create table posts ( id integer primary key, slug text not null, title text not null);export default { db: './app.db', migrations: './migrations', definitions: './definitions.sql', queries: './sql', generate: { runtime: 'effect-v3', },};select id, slug, title from posts order by id limit :limit;output
import * as Effect from 'effect/Effect';import {SqlClient} from '@effect/sql';
const sql = `select id, slug, title from posts order by id limit ?;`;const query = (params: listPosts.Params) => ({ name: "listPosts", sql, args: [params.limit] });
export const listPosts = Object.assign( function listPosts(params: listPosts.Params) { return Effect.gen(function*() { const sqlClient = yield* SqlClient.SqlClient; const generatedQuery = query(params); const rows = yield* sqlClient.unsafe<listPosts.Result>(generatedQuery.sql, generatedQuery.args); return rows; }); }, { sql, query },);
export namespace listPosts { export type Params = { limit: number; }; export type Result = { id: number; slug: string; title: string; };}export * from "./tables.js";export * from "./queries.js";// Generated by `sqlfu generate`. Do not edit.// Row types for every table and view in your project's schema.
export type PostsRow = { id: number; slug: string; title: string;};emits Effect v4 unstable SQL functions with the unstable Effect SQL import
Section titled “emits Effect v4 unstable SQL functions with the unstable Effect SQL import”input
create table posts ( id integer primary key, slug text not null, title text not null);export default { db: './app.db', migrations: './migrations', definitions: './definitions.sql', queries: './sql', generate: { runtime: 'effect-v4-unstable', },};select id, slug, title from posts order by id limit :limit;output
import * as Effect from 'effect/Effect';import {SqlClient} from 'effect/unstable/sql';
const sql = `select id, slug, title from posts order by id limit ?;`;const query = (params: listPosts.Params) => ({ name: "listPosts", sql, args: [params.limit] });
export const listPosts = Object.assign( function listPosts(params: listPosts.Params) { return Effect.gen(function*() { const sqlClient = yield* SqlClient.SqlClient; const generatedQuery = query(params); const rows = yield* sqlClient.unsafe<listPosts.Result>(generatedQuery.sql, generatedQuery.args); return rows; }); }, { sql, query },);
export namespace listPosts { export type Params = { limit: number; }; export type Result = { id: number; slug: string; title: string; };}export * from "./tables.js";export * from "./queries.js";// Generated by `sqlfu generate`. Do not edit.// Row types for every table and view in your project's schema.
export type PostsRow = { id: number; slug: string; title: string;};