Effect CommunityEC
Effect Community3mo ago
4 replies
Berend

Providing Drizzle Without SqlClient in Requirements

Is it possible to provide just drizzle as a service (the db property) in RequirementsOut without also providing sql? Here's the example (taken from the docs):

import { SqlClient } from "@effect/sql"
import * as SqliteDrizzle from "@effect/sql-drizzle/Sqlite"
import { SqliteClient } from "@effect/sql-sqlite-node"
import * as D from "drizzle-orm/sqlite-core"
import { Effect, Layer } from "effect"

// setup

const SqlLive = SqliteClient.layer({
  filename: "test.db"
})
const DrizzleLive = SqliteDrizzle.layer.pipe(
  Layer.provide(SqlLive)
)
const DatabaseLive = Layer.mergeAll(SqlLive, DrizzleLive)

// usage

const users = D.sqliteTable("users", {
  id: D.integer("id").primaryKey(),
  name: D.text("name")
})

Effect.gen(function*() {
  const sql = yield* SqlClient.SqlClient
  const db = yield* SqliteDrizzle.SqliteDrizzle
  yield* sql`CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)`
  yield* db.delete(users)
  yield* db.insert(users).values({ id: 1, name: "Alice" })
  const results = yield* db.select().from(users)
  console.log(results)
}).pipe(
  Effect.provide(DatabaseLive),
  Effect.runPromise
)


As you can see you can get access to the raw SqlClient. What if I want to provide a DatabaseLive which just provides SqliteDrizzle? The issue is the Layer.mergeAll, but I couldn't get something to work where we just provide SqliteDrizzle if this service itself needs SqlClient. You always can get both now in your requirements.
Was this page helpful?