GeezerVison is showing github.com — feat(effect-drizzle-sqlite): add vendored sqlite adapter (#28547) · danmaxis/opencode@5381795
File treeExpand file treeCollapse file tree Open diff view settings packages/effect-drizzle-sqlite migrations/20240101000000_create_users
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +19Lines changed: 19 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: package.json +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 30 | 30 | "catalog": { | 31 | 31 | "@effect/opentelemetry": "4.0.0-beta.66", | 32 | 32 | "@effect/platform-node": "4.0.0-beta.66", | | 33 | + "@effect/sql-sqlite-bun": "4.0.0-beta.66", | 33 | 34 | "@npmcli/arborist": "9.4.0", | 34 | 35 | "@types/bun": "1.3.13", | 35 | 36 | "@types/cross-spawn": "6.0.6", | |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +# Effect Drizzle SQLite | | 2 | + | | 3 | +This package vendors a Drizzle Effect SQLite adapter for this repo. | | 4 | + | | 5 | +- Keep this package generic: Drizzle + Effect + SQLite only. | | 6 | +- Do not add opencode-specific tables, paths, migrations, post-commit hooks, or domain storage APIs here. | | 7 | +- Runtime code should depend on generic `effect/unstable/sql/SqlClient`, not a specific SQLite driver. | | 8 | +- Concrete SQLite clients such as `@effect/sql-sqlite-bun` belong in tests or examples unless this package intentionally adds a driver-specific helper. | | 9 | +- Preserve Drizzle adapter naming and behavior where possible so this can be replaced by upstream `drizzle-orm/effect-sqlite` later. | | 10 | +- If touching copied Drizzle internals, compare with current `[email protected]` declarations and runtime JS. | | 11 | +- If touching Effect APIs, verify against `/Users/kit/code/open-source/effect-smol`. | | 12 | + | | 13 | +Useful entry points: | | 14 | + | | 15 | +- `src/effect-sqlite/driver.ts`: creates the Effect-backed Drizzle database with `make` and `makeWithDefaults`. | | 16 | +- `src/effect-sqlite/session.ts`: adapts generic Effect `SqlClient` execution and transactions to Drizzle SQLite sessions. | | 17 | +- `src/sqlite-core/effect/*`: Effect-yieldable SQLite query builders. | | 18 | +- `src/internal/drizzle-utils.ts`: local typed shims for Drizzle runtime internals that RC2 does not expose in declarations. | | 19 | +- `examples/basic.ts`: minimal usage example with Bun SQLite. |
Collapse file Copy file name to clipboard +92Lines changed: 92 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { SqliteClient } from "@effect/sql-sqlite-bun" | | 2 | +import { eq } from "drizzle-orm" | | 3 | +import { integer, sqliteTable, text } from "drizzle-orm/sqlite-core" | | 4 | +import * as Context from "effect/Context" | | 5 | +import * as Effect from "effect/Effect" | | 6 | +import * as Layer from "effect/Layer" | | 7 | +import * as Schema from "effect/Schema" | | 8 | +import { EffectDrizzleSqlite } from "../src" | | 9 | + | | 10 | +const users = sqliteTable("users", { | | 11 | + id: integer().primaryKey({ autoIncrement: true }), | | 12 | + name: text().notNull(), | | 13 | +}) | | 14 | + | | 15 | +type User = typeof users.$inferSelect | | 16 | + | | 17 | +const makeDatabase = EffectDrizzleSqlite.makeWithDefaults() | | 18 | +type DatabaseShape = Effect.Success<typeof makeDatabase> | | 19 | + | | 20 | +const sqliteLayer = SqliteClient.layer({ filename: ":memory:", disableWAL: true }) | | 21 | + | | 22 | +class Database extends Context.Service<Database, DatabaseShape>()("@opencode/example/Database") { | | 23 | + static layer = Layer.effect(Database, makeDatabase).pipe(Layer.provide(sqliteLayer)) | | 24 | +} | | 25 | + | | 26 | +class UserStoreError extends Schema.TaggedErrorClass<UserStoreError>()("UserStoreError", { | | 27 | + message: Schema.String, | | 28 | + cause: Schema.optional(Schema.Defect), | | 29 | +}) {} | | 30 | + | | 31 | +const mapStoreError = (message: string) => (cause: unknown) => new UserStoreError({ message, cause }) | | 32 | + | | 33 | +interface UserStoreShape { | | 34 | + migrate(): Effect.Effect<void, UserStoreError> | | 35 | + create(name: string): Effect.Effect<void, UserStoreError> | | 36 | + rename(from: string, to: string): Effect.Effect<void, UserStoreError> | | 37 | + list(): Effect.Effect<User[], UserStoreError> | | 38 | +} | | 39 | + | | 40 | +class UserStore extends Context.Service<UserStore, UserStoreShape>()("@opencode/example/UserStore") { | | 41 | + static layer = Layer.effect( | | 42 | + UserStore, | | 43 | + Effect.gen(function* () { | | 44 | + const db = yield* Database | | 45 | + | | 46 | + return UserStore.of({ | | 47 | + migrate: Effect.fn("UserStore.migrate")(function* () { | | 48 | + yield* EffectDrizzleSqlite.migrate(db, { migrationsFolder: `${import.meta.dirname}/migrations` }).pipe( | | 49 | + Effect.mapError((cause) => new UserStoreError({ message: "Failed to migrate users", cause })), | | 50 | + ) | | 51 | + }), | | 52 | + create: Effect.fn("UserStore.create")(function* (name: string) { | | 53 | + yield* db | | 54 | + .insert(users) | | 55 | + .values({ name }) | | 56 | + .pipe(Effect.asVoid, Effect.mapError(mapStoreError("Failed to create user"))) | | 57 | + }), | | 58 | + rename: Effect.fn("UserStore.rename")(function* (from: string, to: string) { | | 59 | + yield* db | | 60 | + .transaction( | | 61 | + Effect.fnUntraced(function* (tx) { | | 62 | + yield* tx.insert(users).values({ name: from }) | | 63 | + yield* tx.update(users).set({ name: to }).where(eq(users.name, from)) | | 64 | + }), | | 65 | + { behavior: "immediate" }, | | 66 | + ) | | 67 | + .pipe(Effect.asVoid, Effect.mapError(mapStoreError("Failed to rename user"))) | | 68 | + }), | | 69 | + list: Effect.fn("UserStore.list")(function* () { | | 70 | + return yield* db | | 71 | + .select() | | 72 | + .from(users) | | 73 | + .pipe(Effect.mapError(mapStoreError("Failed to list users"))) | | 74 | + }), | | 75 | + }) | | 76 | + }), | | 77 | + ).pipe(Layer.provide(Database.layer)) | | 78 | +} | | 79 | + | | 80 | +const program = Effect.gen(function* () { | | 81 | + const userStore = yield* UserStore | | 82 | + | | 83 | + yield* userStore.migrate() | | 84 | + yield* userStore.create("Ada") | | 85 | + yield* userStore.rename("Grace", "Grace Hopper") | | 86 | + | | 87 | + return yield* userStore.list() | | 88 | +}) | | 89 | + | | 90 | +const rows = await Effect.runPromise(program.pipe(Effect.provide(UserStore.layer))) | | 91 | + | | 92 | +console.log(rows) |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +CREATE TABLE users ( | | 2 | + id integer PRIMARY KEY AUTOINCREMENT NOT NULL, | | 3 | + name text NOT NULL | | 4 | +); |
Collapse file Copy file name to clipboard +29Lines changed: 29 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +{ | | 2 | + "$schema": "https://json.schemastore.org/package.json", | | 3 | + "version": "1.15.5", | | 4 | + "name": "@opencode-ai/effect-drizzle-sqlite", | | 5 | + "type": "module", | | 6 | + "license": "MIT", | | 7 | + "private": true, | | 8 | + "scripts": { | | 9 | + "test": "bun test --timeout 30000", | | 10 | + "test:ci": "mkdir -p .artifacts/unit && bun test --timeout 30000 --reporter=junit --reporter-outfile=.artifacts/unit/junit.xml", | | 11 | + "typecheck": "tsgo --noEmit" | | 12 | + }, | | 13 | + "exports": { | | 14 | + ".": "./src/index.ts", | | 15 | + "./effect-sqlite": "./src/effect-sqlite/index.ts", | | 16 | + "./effect-sqlite/migrator": "./src/effect-sqlite/migrator.ts", | | 17 | + "./sqlite-core/effect": "./src/sqlite-core/effect/index.ts" | | 18 | + }, | | 19 | + "devDependencies": { | | 20 | + "@effect/sql-sqlite-bun": "catalog:", | | 21 | + "@tsconfig/bun": "catalog:", | | 22 | + "@types/bun": "catalog:", | | 23 | + "@typescript/native-preview": "catalog:" | | 24 | + }, | | 25 | + "dependencies": { | | 26 | + "drizzle-orm": "catalog:", | | 27 | + "effect": "catalog:" | | 28 | + } | | 29 | +} |
Collapse file Copy file name to clipboard +77Lines changed: 77 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | + | | 15 | +export class EffectSQLiteDatabase<TRelations extends AnyRelations = EmptyRelations> extends SQLiteEffectDatabase< | | 16 | + EffectSQLiteQueryEffectHKT, | | 17 | + EffectSQLiteRunResult, | | 18 | + TRelations | | 19 | +> { | | 20 | + static override readonly [entityKind]: string = "EffectSQLiteDatabase" | | 21 | +} | | 22 | + | | 23 | +export type EffectDrizzleSQLiteConfig<TRelations extends AnyRelations = EmptyRelations> = Omit< | | 24 | + DrizzleConfig<Record<string, never>, TRelations>, | | 25 | + "cache" | "logger" | "schema" | | 26 | +> | | 27 | + | | 28 | +export const DefaultServices = Layer.merge(EffectCache.Default, EffectLogger.Default) | | 29 | + | | 30 | +/** | | 31 | + * Creates an EffectSQLiteDatabase instance. | | 32 | + * | | 33 | + * Requires a generic Effect `SqlClient`, `EffectLogger`, and `EffectCache` services to be provided. | | 34 | + * Drizzle only depends on the generic `SqlClient`; install and provide a compatible SQLite provider such as | | 35 | + * `@effect/sql-sqlite-node`, `@effect/sql-sqlite-bun`, or another package that exposes `SqlClient`. | | 36 | + * | | 37 | + * @example | | 38 | + * ```ts | | 39 | + * import { SqliteClient } from '@effect/sql-sqlite-node'; | | 40 | + * import * as SQLiteDrizzle from 'drizzle-orm/effect-sqlite'; | | 41 | + * import * as Effect from 'effect/Effect'; | | 42 | + * | | 43 | + * const db = yield* SQLiteDrizzle.make({ relations }).pipe( | | 44 | + * Effect.provide(SQLiteDrizzle.DefaultServices), | | 45 | + * Effect.provide(SqliteClient.layer({ filename: 'sqlite.db' })), | | 46 | + * ); | | 47 | + * ``` | | 48 | + */ | | 49 | +export const make = Effect.fn("SQLiteDrizzle.make")(function* <TRelations extends AnyRelations = EmptyRelations>( | | 50 | + config: EffectDrizzleSQLiteConfig<TRelations> = {}, | | 51 | +) { | | 52 | + const client = yield* SqlClient | | 53 | + const cache = yield* EffectCache | | 54 | + const logger = yield* EffectLogger | | 55 | + | | 56 | + const dialect = new SQLiteAsyncDialect() | | 57 | + const relations = config.relations ?? ({} as TRelations) | | 58 | + const session = new EffectSQLiteSession(client, dialect, relations, { | | 59 | + logger, | | 60 | + cache, | | 61 | + useJitMappers: jitCompatCheck(config.jit), | | 62 | + }) | | 63 | + const db = new EffectSQLiteDatabase(dialect, session, relations) as EffectSQLiteDatabase<TRelations> & { | | 64 | + $client: SqlClient | | 65 | + } | | 66 | + db.$client = client | | 67 | + db.$cache.invalidate = cache.onMutate | | 68 | + | | 69 | + return db | | 70 | +}) | | 71 | + | | 72 | +/** | | 73 | + * Convenience function that creates an EffectSQLiteDatabase with `DefaultServices` already provided. | | 74 | + */ | | 75 | +export const makeWithDefaults = <TRelations extends AnyRelations = EmptyRelations>( | | 76 | + config: EffectDrizzleSQLiteConfig<TRelations> = {}, | | 77 | +) => make(config).pipe(Effect.provide(DefaultServices)) |
Collapse file Copy file name to clipboard +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* oxlint-disable */ | | 2 | +export { EffectLogger } from "drizzle-orm/effect-core" | | 3 | +export * from "./driver" | | 4 | +export * from "./session" |
Collapse file Copy file name to clipboard +14Lines changed: 14 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +/* oxlint-disable */ | | 2 | +import type { MigrationConfig } from "drizzle-orm/migrator" | | 3 | +import { readMigrationFiles } from "drizzle-orm/migrator" | | 4 | +import type { AnyRelations } from "drizzle-orm/relations" | | 5 | +import { migrate as coreMigrate } from "../sqlite-core/effect/session" | | 6 | +import type { EffectSQLiteDatabase } from "./driver" | | 7 | + | | 8 | +export function migrate<TRelations extends AnyRelations>( | | 9 | + db: EffectSQLiteDatabase<TRelations>, | | 10 | + config: MigrationConfig, | | 11 | +) { | | 12 | + const migrations = readMigrationFiles(config) | | 13 | + return coreMigrate(migrations, db.session, config) | | 14 | +} |
|