progress
This commit is contained in:
@@ -12,18 +12,17 @@ import { InstallationChannel } from "../installation/version"
|
||||
|
||||
const makeDatabase = EffectDrizzleSqlite.makeWithDefaults()
|
||||
type DatabaseShape = Effect.Success<typeof makeDatabase>
|
||||
export type Info = {
|
||||
|
||||
export interface Interface {
|
||||
db: DatabaseShape
|
||||
native: unknown
|
||||
drizzle: Sqlite.DrizzleClient
|
||||
}
|
||||
|
||||
export class Service extends Context.Service<Service, Info>()("@opencode/v2/storage/Database") {}
|
||||
export class Service extends Context.Service<Service, Interface>()("@opencode/v2/storage/Database") {}
|
||||
|
||||
export const layer = Layer.effect(
|
||||
Service,
|
||||
Effect.gen(function* () {
|
||||
const native = yield* Sqlite.Native
|
||||
const drizzle = yield* Sqlite.Drizzle
|
||||
const db = yield* makeDatabase
|
||||
|
||||
@@ -36,7 +35,7 @@ export const layer = Layer.effect(
|
||||
yield* Effect.log("Applying database migrations")
|
||||
yield* DatabaseMigration.apply(db)
|
||||
|
||||
return { db, native, drizzle }
|
||||
return { db, drizzle }
|
||||
}).pipe(Effect.orDie),
|
||||
)
|
||||
|
||||
@@ -44,8 +43,6 @@ export function layerFromPath(filename: string) {
|
||||
return layer.pipe(Layer.provide(sqliteLayer({ filename })))
|
||||
}
|
||||
|
||||
export const memoryLayer = layerFromPath(":memory:")
|
||||
|
||||
export function path() {
|
||||
if (Flag.OPENCODE_DB) {
|
||||
if (Flag.OPENCODE_DB === ":memory:" || isAbsolute(Flag.OPENCODE_DB)) return Flag.OPENCODE_DB
|
||||
|
||||
@@ -172,4 +172,6 @@ export const layer = (config: Config) =>
|
||||
Layer.merge(
|
||||
nativeLayer(config),
|
||||
Layer.merge(sqliteLayer(config), drizzleLayer).pipe(Layer.provide(nativeLayer(config))),
|
||||
).pipe(Layer.provide(Reactivity.layer))
|
||||
).pipe(
|
||||
Layer.provide(Reactivity.layer),
|
||||
)
|
||||
|
||||
@@ -167,4 +167,6 @@ export const layer = (config: Config) =>
|
||||
Layer.merge(
|
||||
nativeLayer(config),
|
||||
Layer.merge(sqliteLayer(config), drizzleLayer).pipe(Layer.provide(nativeLayer(config))),
|
||||
).pipe(Layer.provide(Reactivity.layer))
|
||||
).pipe(
|
||||
Layer.provide(Reactivity.layer),
|
||||
)
|
||||
|
||||
@@ -4,11 +4,5 @@ import { Context } from "effect"
|
||||
import type { drizzle } from "drizzle-orm/bun-sqlite"
|
||||
|
||||
export type DrizzleClient = ReturnType<typeof drizzle>
|
||||
|
||||
export interface Info<Native> {
|
||||
native: Native
|
||||
drizzle: DrizzleClient
|
||||
}
|
||||
|
||||
export class Native extends Context.Service<Native, unknown>()("@opencode-ai/core/database/SqliteNative") {}
|
||||
export class Drizzle extends Context.Service<Drizzle, DrizzleClient>()("@opencode-ai/core/database/SqliteDrizzle") {}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { Database } from "bun:sqlite"
|
||||
import { Sqlite } from "@opencode-ai/core/database/sqlite"
|
||||
import { layer } from "@opencode-ai/core/database/sqlite.bun"
|
||||
import { makeRuntime } from "@opencode-ai/core/effect/runtime"
|
||||
import { drizzle } from "drizzle-orm/bun-sqlite"
|
||||
import { Effect } from "effect"
|
||||
|
||||
export function init(path: string) {
|
||||
const sqlite = new Database(path, { create: true })
|
||||
const db = drizzle({ client: sqlite })
|
||||
return db
|
||||
const runtime = makeRuntime(Sqlite.Native, layer({ filename: path }))
|
||||
const native = runtime.runSync((native) => Effect.succeed(native)) as Database
|
||||
return drizzle({ client: native })
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import { DatabaseSync } from "node:sqlite"
|
||||
import { Sqlite } from "@opencode-ai/core/database/sqlite"
|
||||
import { layer } from "@opencode-ai/core/database/sqlite.node"
|
||||
import { makeRuntime } from "@opencode-ai/core/effect/runtime"
|
||||
import { drizzle } from "drizzle-orm/node-sqlite"
|
||||
import { Effect } from "effect"
|
||||
|
||||
export function init(path: string) {
|
||||
const sqlite = new DatabaseSync(path)
|
||||
const db = drizzle({ client: sqlite })
|
||||
return db
|
||||
const runtime = makeRuntime(Sqlite.Native, layer({ filename: path }))
|
||||
const native = runtime.runSync((native) => Effect.succeed(native)) as DatabaseSync
|
||||
return drizzle({ client: native })
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export const getPath = () => Database.path()
|
||||
|
||||
export type Transaction = SQLiteTransaction<"sync", void, Record<string, unknown>, TablesRelationalConfig>
|
||||
|
||||
type Client = Database.Info["drizzle"]
|
||||
type Client = Database.Interface["drizzle"]
|
||||
|
||||
let client: Client | undefined
|
||||
let loaded = false
|
||||
|
||||
Reference in New Issue
Block a user