mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-25 22:04:22 +00:00
feat(effect-drizzle-sqlite): add vendored sqlite adapter (#28547)
This commit is contained in:
77
packages/effect-drizzle-sqlite/src/effect-sqlite/driver.ts
Normal file
77
packages/effect-drizzle-sqlite/src/effect-sqlite/driver.ts
Normal file
@@ -0,0 +1,77 @@
|
||||
/* oxlint-disable */
|
||||
import * as Effect from "effect/Effect"
|
||||
import * as Layer from "effect/Layer"
|
||||
import { SqlClient } from "effect/unstable/sql/SqlClient"
|
||||
import { EffectCache } from "drizzle-orm/cache/core/cache-effect"
|
||||
import { EffectLogger } from "drizzle-orm/effect-core"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import type { AnyRelations, EmptyRelations } from "drizzle-orm/relations"
|
||||
import { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import { SQLiteEffectDatabase } from "../sqlite-core/effect/db"
|
||||
import type { DrizzleConfig } from "drizzle-orm/utils"
|
||||
import { jitCompatCheck } from "../internal/drizzle-utils"
|
||||
import { type EffectSQLiteQueryEffectHKT, type EffectSQLiteRunResult, EffectSQLiteSession } from "./session"
|
||||
|
||||
export class EffectSQLiteDatabase<TRelations extends AnyRelations = EmptyRelations> extends SQLiteEffectDatabase<
|
||||
EffectSQLiteQueryEffectHKT,
|
||||
EffectSQLiteRunResult,
|
||||
TRelations
|
||||
> {
|
||||
static override readonly [entityKind]: string = "EffectSQLiteDatabase"
|
||||
}
|
||||
|
||||
export type EffectDrizzleSQLiteConfig<TRelations extends AnyRelations = EmptyRelations> = Omit<
|
||||
DrizzleConfig<Record<string, never>, TRelations>,
|
||||
"cache" | "logger" | "schema"
|
||||
>
|
||||
|
||||
export const DefaultServices = Layer.merge(EffectCache.Default, EffectLogger.Default)
|
||||
|
||||
/**
|
||||
* Creates an EffectSQLiteDatabase instance.
|
||||
*
|
||||
* Requires a generic Effect `SqlClient`, `EffectLogger`, and `EffectCache` services to be provided.
|
||||
* Drizzle only depends on the generic `SqlClient`; install and provide a compatible SQLite provider such as
|
||||
* `@effect/sql-sqlite-node`, `@effect/sql-sqlite-bun`, or another package that exposes `SqlClient`.
|
||||
*
|
||||
* @example
|
||||
* ```ts
|
||||
* import { SqliteClient } from '@effect/sql-sqlite-node';
|
||||
* import * as SQLiteDrizzle from 'drizzle-orm/effect-sqlite';
|
||||
* import * as Effect from 'effect/Effect';
|
||||
*
|
||||
* const db = yield* SQLiteDrizzle.make({ relations }).pipe(
|
||||
* Effect.provide(SQLiteDrizzle.DefaultServices),
|
||||
* Effect.provide(SqliteClient.layer({ filename: 'sqlite.db' })),
|
||||
* );
|
||||
* ```
|
||||
*/
|
||||
export const make = Effect.fn("SQLiteDrizzle.make")(function* <TRelations extends AnyRelations = EmptyRelations>(
|
||||
config: EffectDrizzleSQLiteConfig<TRelations> = {},
|
||||
) {
|
||||
const client = yield* SqlClient
|
||||
const cache = yield* EffectCache
|
||||
const logger = yield* EffectLogger
|
||||
|
||||
const dialect = new SQLiteAsyncDialect()
|
||||
const relations = config.relations ?? ({} as TRelations)
|
||||
const session = new EffectSQLiteSession(client, dialect, relations, {
|
||||
logger,
|
||||
cache,
|
||||
useJitMappers: jitCompatCheck(config.jit),
|
||||
})
|
||||
const db = new EffectSQLiteDatabase(dialect, session, relations) as EffectSQLiteDatabase<TRelations> & {
|
||||
$client: SqlClient
|
||||
}
|
||||
db.$client = client
|
||||
db.$cache.invalidate = cache.onMutate
|
||||
|
||||
return db
|
||||
})
|
||||
|
||||
/**
|
||||
* Convenience function that creates an EffectSQLiteDatabase with `DefaultServices` already provided.
|
||||
*/
|
||||
export const makeWithDefaults = <TRelations extends AnyRelations = EmptyRelations>(
|
||||
config: EffectDrizzleSQLiteConfig<TRelations> = {},
|
||||
) => make(config).pipe(Effect.provide(DefaultServices))
|
||||
@@ -0,0 +1,4 @@
|
||||
/* oxlint-disable */
|
||||
export { EffectLogger } from "drizzle-orm/effect-core"
|
||||
export * from "./driver"
|
||||
export * from "./session"
|
||||
14
packages/effect-drizzle-sqlite/src/effect-sqlite/migrator.ts
Normal file
14
packages/effect-drizzle-sqlite/src/effect-sqlite/migrator.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
/* oxlint-disable */
|
||||
import type { MigrationConfig } from "drizzle-orm/migrator"
|
||||
import { readMigrationFiles } from "drizzle-orm/migrator"
|
||||
import type { AnyRelations } from "drizzle-orm/relations"
|
||||
import { migrate as coreMigrate } from "../sqlite-core/effect/session"
|
||||
import type { EffectSQLiteDatabase } from "./driver"
|
||||
|
||||
export function migrate<TRelations extends AnyRelations>(
|
||||
db: EffectSQLiteDatabase<TRelations>,
|
||||
config: MigrationConfig,
|
||||
) {
|
||||
const migrations = readMigrationFiles(config)
|
||||
return coreMigrate(migrations, db.session, config)
|
||||
}
|
||||
214
packages/effect-drizzle-sqlite/src/effect-sqlite/session.ts
Normal file
214
packages/effect-drizzle-sqlite/src/effect-sqlite/session.ts
Normal file
@@ -0,0 +1,214 @@
|
||||
/* oxlint-disable */
|
||||
import * as Context from "effect/Context"
|
||||
import * as Effect from "effect/Effect"
|
||||
import * as Exit from "effect/Exit"
|
||||
import * as Scope from "effect/Scope"
|
||||
import type { SqlClient } from "effect/unstable/sql/SqlClient"
|
||||
import type { SqlError } from "effect/unstable/sql/SqlError"
|
||||
import type { EffectCacheShape } from "drizzle-orm/cache/core/cache-effect"
|
||||
import type { WithCacheConfig } from "drizzle-orm/cache/core/types"
|
||||
import type { EffectDrizzleQueryError } from "drizzle-orm/effect-core/errors"
|
||||
import type { EffectLoggerShape } from "drizzle-orm/effect-core/logger"
|
||||
import type { QueryEffectHKTBase } from "drizzle-orm/effect-core/query-effect"
|
||||
import { entityKind } from "drizzle-orm/entity"
|
||||
import type { AnyRelations } from "drizzle-orm/relations"
|
||||
import type { RelationalQueryMapperConfig } from "drizzle-orm/relations"
|
||||
import type { Query } from "drizzle-orm/sql/sql"
|
||||
import type { SQLiteAsyncDialect } from "drizzle-orm/sqlite-core/dialect"
|
||||
import { SQLiteEffectPreparedQuery, SQLiteEffectSession, SQLiteEffectTransaction } from "../sqlite-core/effect/session"
|
||||
import type { SelectedFieldsOrdered } from "drizzle-orm/sqlite-core/query-builders/select.types"
|
||||
import type { PreparedQueryConfig, SQLiteExecuteMethod, SQLiteTransactionConfig } from "drizzle-orm/sqlite-core/session"
|
||||
|
||||
export interface EffectSQLiteQueryEffectHKT extends QueryEffectHKTBase {
|
||||
readonly error: EffectDrizzleQueryError
|
||||
readonly context: never
|
||||
}
|
||||
|
||||
export type EffectSQLiteRunResult = readonly never[]
|
||||
|
||||
export interface EffectSQLiteSessionOptions {
|
||||
logger: EffectLoggerShape
|
||||
cache: EffectCacheShape
|
||||
useJitMappers?: boolean
|
||||
}
|
||||
|
||||
export class EffectSQLiteSession<TRelations extends AnyRelations> extends SQLiteEffectSession<
|
||||
EffectSQLiteQueryEffectHKT,
|
||||
EffectSQLiteRunResult,
|
||||
TRelations
|
||||
> {
|
||||
static override readonly [entityKind]: string = "EffectSQLiteSession"
|
||||
|
||||
constructor(
|
||||
private client: SqlClient,
|
||||
dialect: SQLiteAsyncDialect,
|
||||
protected relations: TRelations,
|
||||
private options: EffectSQLiteSessionOptions,
|
||||
) {
|
||||
super(dialect)
|
||||
}
|
||||
|
||||
override prepareQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper?: (rows: unknown[][], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
queryMetadata?: {
|
||||
type: "select" | "update" | "delete" | "insert"
|
||||
tables: string[]
|
||||
},
|
||||
cacheConfig?: WithCacheConfig,
|
||||
): SQLiteEffectPreparedQuery<T, EffectSQLiteQueryEffectHKT> {
|
||||
return new SQLiteEffectPreparedQuery<T, EffectSQLiteQueryEffectHKT>(
|
||||
(params, method) => this.execute(query, params, method),
|
||||
query,
|
||||
this.options.logger,
|
||||
this.options.cache,
|
||||
queryMetadata,
|
||||
cacheConfig,
|
||||
fields,
|
||||
executeMethod,
|
||||
this.options.useJitMappers,
|
||||
customResultMapper,
|
||||
undefined,
|
||||
undefined,
|
||||
this.isInTransaction(),
|
||||
)
|
||||
}
|
||||
|
||||
override prepareRelationalQuery<T extends PreparedQueryConfig = PreparedQueryConfig>(
|
||||
query: Query,
|
||||
fields: SelectedFieldsOrdered | undefined,
|
||||
executeMethod: SQLiteExecuteMethod,
|
||||
customResultMapper: (rows: Record<string, unknown>[], mapColumnValue?: (value: unknown) => unknown) => unknown,
|
||||
config: RelationalQueryMapperConfig,
|
||||
): SQLiteEffectPreparedQuery<T, EffectSQLiteQueryEffectHKT, true> {
|
||||
return new SQLiteEffectPreparedQuery<T, EffectSQLiteQueryEffectHKT, true>(
|
||||
(params, method) => this.execute(query, params, method),
|
||||
query,
|
||||
this.options.logger,
|
||||
this.options.cache,
|
||||
undefined,
|
||||
undefined,
|
||||
fields,
|
||||
executeMethod,
|
||||
this.options.useJitMappers,
|
||||
customResultMapper,
|
||||
true,
|
||||
config,
|
||||
this.isInTransaction(),
|
||||
)
|
||||
}
|
||||
|
||||
private execute(query: Query, params: unknown[], method: SQLiteExecuteMethod | "values") {
|
||||
const statement = this.client.unsafe(query.sql, params)
|
||||
if (method === "values") return statement.values
|
||||
if (method === "get") return statement.withoutTransform.pipe(Effect.map((rows) => rows[0]))
|
||||
return statement.withoutTransform
|
||||
}
|
||||
|
||||
private isInTransaction() {
|
||||
return Effect.serviceOption(this.client.transactionService).pipe(Effect.map((option) => option._tag === "Some"))
|
||||
}
|
||||
|
||||
private executeTransactionStatement(connection: Effect.Success<SqlClient["reserve"]>, query: string) {
|
||||
return connection.executeUnprepared(query, [], undefined).pipe(Effect.asVoid)
|
||||
}
|
||||
|
||||
private withTransaction<A, E, R>(effect: Effect.Effect<A, E, R>, config: SQLiteTransactionConfig | undefined) {
|
||||
return Effect.uninterruptibleMask((restore) =>
|
||||
Effect.withFiber<A, E | SqlError, R>((fiber) => {
|
||||
const services = fiber.context
|
||||
const connectionOption = Context.getOption(services, this.client.transactionService)
|
||||
const connection: Effect.Effect<
|
||||
readonly [Scope.Closeable | undefined, Effect.Success<SqlClient["reserve"]>],
|
||||
SqlError
|
||||
> =
|
||||
connectionOption._tag === "Some"
|
||||
? Effect.succeed([undefined, connectionOption.value[0]] as const)
|
||||
: Scope.make().pipe(
|
||||
Effect.flatMap((scope) =>
|
||||
Scope.provide(this.client.reserve, scope).pipe(
|
||||
Effect.map((connection) => [scope, connection] as const),
|
||||
Effect.catch((error) =>
|
||||
Scope.close(scope, Exit.fail(error)).pipe(Effect.andThen(Effect.fail(error))),
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
const id = connectionOption._tag === "Some" ? connectionOption.value[1] + 1 : 0
|
||||
|
||||
return connection.pipe(
|
||||
Effect.flatMap(([scope, connection]) =>
|
||||
this.executeTransactionStatement(
|
||||
connection,
|
||||
id === 0 ? `begin ${config?.behavior ?? "deferred"}` : `savepoint effect_sql_${id}`,
|
||||
).pipe(
|
||||
Effect.flatMap(() =>
|
||||
Effect.provideContext(
|
||||
restore(effect),
|
||||
Context.add(services, this.client.transactionService, [connection, id]),
|
||||
),
|
||||
),
|
||||
Effect.exit,
|
||||
Effect.flatMap((exit) => {
|
||||
const finalize = Exit.isSuccess(exit)
|
||||
? id === 0
|
||||
? this.executeTransactionStatement(connection, "commit").pipe(
|
||||
// SQLite keeps the transaction open after deferred constraint commit failures.
|
||||
Effect.catch((error) =>
|
||||
this.executeTransactionStatement(connection, "rollback").pipe(
|
||||
Effect.catch(() => Effect.void),
|
||||
Effect.andThen(Effect.fail(error)),
|
||||
),
|
||||
),
|
||||
)
|
||||
: this.executeTransactionStatement(connection, `release savepoint effect_sql_${id}`)
|
||||
: id === 0
|
||||
? this.executeTransactionStatement(connection, "rollback")
|
||||
: this.executeTransactionStatement(connection, `rollback to savepoint effect_sql_${id}`).pipe(
|
||||
Effect.andThen(
|
||||
this.executeTransactionStatement(connection, `release savepoint effect_sql_${id}`),
|
||||
),
|
||||
)
|
||||
const scoped = scope === undefined ? finalize : Effect.ensuring(finalize, Scope.close(scope, exit))
|
||||
|
||||
return scoped.pipe(Effect.flatMap(() => exit))
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
override transaction<A, E, R>(
|
||||
transaction: (tx: EffectSQLiteTransaction<TRelations>) => Effect.Effect<A, E, R>,
|
||||
config?: SQLiteTransactionConfig,
|
||||
): Effect.Effect<A, E | SqlError, R> {
|
||||
const { dialect, relations } = this
|
||||
|
||||
return this.withTransaction(
|
||||
Effect.gen({ self: this }, function* () {
|
||||
const tx = new EffectSQLiteTransaction<TRelations>(dialect, this, relations)
|
||||
|
||||
return yield* transaction(tx)
|
||||
}),
|
||||
config,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export class EffectSQLiteTransaction<TRelations extends AnyRelations> extends SQLiteEffectTransaction<
|
||||
EffectSQLiteQueryEffectHKT,
|
||||
EffectSQLiteRunResult,
|
||||
TRelations
|
||||
> {
|
||||
static override readonly [entityKind]: string = "EffectSQLiteTransaction"
|
||||
|
||||
override transaction: <A, E, R>(
|
||||
transaction: (
|
||||
tx: SQLiteEffectTransaction<EffectSQLiteQueryEffectHKT, EffectSQLiteRunResult, TRelations>,
|
||||
) => Effect.Effect<A, E, R>,
|
||||
) => Effect.Effect<A, SqlError | E, R> = (tx) => this.session.transaction(tx)
|
||||
}
|
||||
Reference in New Issue
Block a user