driver.ts 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* oxlint-disable */
  2. import * as Effect from "effect/Effect"
  3. import * as Layer from "effect/Layer"
  4. import { SqlClient } from "effect/unstable/sql/SqlClient"
  5. import { EffectCache } from "drizzle-orm/cache/core/cache-effect"
  6. import { EffectLogger } from "drizzle-orm/effect-core"
  7. import { entityKind } from "drizzle-orm/entity"
  8. import type { AnyRelations, EmptyRelations } from "drizzle-orm/relations"
  9. import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
  10. import { SQLiteEffectDatabase } from "../sqlite-core/effect/db"
  11. import type { DrizzleConfig } from "drizzle-orm/utils"
  12. import { jitCompatCheck } from "../internal/drizzle-utils"
  13. import { type EffectSQLiteQueryEffectHKT, type EffectSQLiteRunResult, EffectSQLiteSession } from "./session"
  14. export class EffectSQLiteDatabase<TRelations extends AnyRelations = EmptyRelations> extends SQLiteEffectDatabase<
  15. EffectSQLiteQueryEffectHKT,
  16. EffectSQLiteRunResult,
  17. TRelations
  18. > {
  19. static override readonly [entityKind]: string = "EffectSQLiteDatabase"
  20. }
  21. export type EffectDrizzleSQLiteConfig<TRelations extends AnyRelations = EmptyRelations> = Omit<
  22. DrizzleConfig<Record<string, never>, TRelations>,
  23. "cache" | "logger" | "schema"
  24. >
  25. export const DefaultServices = Layer.merge(EffectCache.Default, EffectLogger.Default)
  26. /**
  27. * Creates an EffectSQLiteDatabase instance.
  28. *
  29. * Requires a generic Effect `SqlClient`, `EffectLogger`, and `EffectCache` services to be provided.
  30. * Drizzle only depends on the generic `SqlClient`; install and provide a compatible SQLite provider such as
  31. * `@effect/sql-sqlite-node`, `@effect/sql-sqlite-bun`, or another package that exposes `SqlClient`.
  32. *
  33. * @example
  34. * ```ts
  35. * import { SqliteClient } from '@effect/sql-sqlite-node';
  36. * import * as SQLiteDrizzle from 'drizzle-orm/effect-sqlite';
  37. * import * as Effect from 'effect/Effect';
  38. *
  39. * const db = yield* SQLiteDrizzle.make({ relations }).pipe(
  40. * Effect.provide(SQLiteDrizzle.DefaultServices),
  41. * Effect.provide(SqliteClient.layer({ filename: 'sqlite.db' })),
  42. * );
  43. * ```
  44. */
  45. export const make = Effect.fn("SQLiteDrizzle.make")(function* <TRelations extends AnyRelations = EmptyRelations>(
  46. config: EffectDrizzleSQLiteConfig<TRelations> = {},
  47. ) {
  48. const client = yield* SqlClient
  49. const cache = yield* EffectCache
  50. const logger = yield* EffectLogger
  51. const dialect = new SQLiteAsyncDialect()
  52. const relations = config.relations ?? ({} as TRelations)
  53. const session = new EffectSQLiteSession(client, dialect, relations, {
  54. logger,
  55. cache,
  56. useJitMappers: jitCompatCheck(config.jit),
  57. })
  58. const db = new EffectSQLiteDatabase(dialect, session, relations) as EffectSQLiteDatabase<TRelations> & {
  59. $client: SqlClient
  60. }
  61. db.$client = client
  62. db.$cache.invalidate = cache.onMutate
  63. return db
  64. })
  65. /**
  66. * Convenience function that creates an EffectSQLiteDatabase with `DefaultServices` already provided.
  67. */
  68. export const makeWithDefaults = <TRelations extends AnyRelations = EmptyRelations>(
  69. config: EffectDrizzleSQLiteConfig<TRelations> = {},
  70. ) => make(config).pipe(Effect.provide(DefaultServices))