Effect CommunityEC
Effect Community13mo ago
3 replies
hel.io

How to provide table schemas to Drizzle with Effect

I'm quite new to Effect and I'm trying to set up Drizzle with Effect passing table schemas.
In a "traditional" Drizzle project, I define the client like so:
import { drizzle } from 'drizzle-orm/node-postgres';

import * as schema from './schema';

export const db = drizzle({
  connection: process.env.DATABASE_URL!,
  logger: process.env.NODE_ENV === 'development',
  schema,
});


I'm trying to set up Drizzle with Effect and so far this is what I've got:
import * as PgDrizzle from '@effect/sql-drizzle/Pg';
import { PgClient } from '@effect/sql-pg';
import { Effect, Layer, Redacted } from 'effect';

import { config } from './Config';

const SqlLive = config.pipe(
  Effect.map((c) =>
    PgClient.layer({
      url: Redacted.make(c.dbUrl),
    })
  ),
  Layer.unwrapEffect
);

const DrizzleLive = PgDrizzle.layer.pipe(Layer.provide(SqlLive));

export const PgDbLive = Layer.mergeAll(SqlLive, DrizzleLive);


However, here I can't pass the schema to it and when I try to do await db.query.users.findFirst() I get this error:
Property users does not exist on type
DrizzleTypeError<"Seems like the schema generic is missing - did you forget to add it to your DB type?">

However, with this await db.select().from(users).where(eq(users.id, id)); I have no errors.

I know this is happening because we are not providing the table schemas to Drizzle can't infer the tables.

Was anyone able to provide schema to @effect/sql-drizzle/Pg?
Was this page helpful?