diff options
| author | Frank <[email protected]> | 2025-09-18 01:32:40 -0400 |
|---|---|---|
| committer | Frank <[email protected]> | 2025-09-18 01:32:40 -0400 |
| commit | fc4f281408c56ab12db571a470456212a479edf5 (patch) | |
| tree | 309d23b0c497bc61af6f8e650a6036fa41d7cbdb /cloud/core/src/drizzle | |
| parent | f8c4f713a5b48892899d0ac195c3470ab7ef764c (diff) | |
| download | opencode-fc4f281408c56ab12db571a470456212a479edf5.tar.gz opencode-fc4f281408c56ab12db571a470456212a479edf5.zip | |
wip: zen
Diffstat (limited to 'cloud/core/src/drizzle')
| -rw-r--r-- | cloud/core/src/drizzle/index.ts | 86 | ||||
| -rw-r--r-- | cloud/core/src/drizzle/types.ts | 33 |
2 files changed, 0 insertions, 119 deletions
diff --git a/cloud/core/src/drizzle/index.ts b/cloud/core/src/drizzle/index.ts deleted file mode 100644 index 806037996..000000000 --- a/cloud/core/src/drizzle/index.ts +++ /dev/null @@ -1,86 +0,0 @@ -import { drizzle } from "drizzle-orm/planetscale-serverless" -import { Resource } from "@opencode/cloud-resource" -export * from "drizzle-orm" -import { Client } from "@planetscale/database" - -import { MySqlTransaction, type MySqlTransactionConfig } from "drizzle-orm/mysql-core" -import type { ExtractTablesWithRelations } from "drizzle-orm" -import type { PlanetScalePreparedQueryHKT, PlanetscaleQueryResultHKT } from "drizzle-orm/planetscale-serverless" -import { Context } from "../context" -import { memo } from "../util/memo" - -export namespace Database { - export type Transaction = MySqlTransaction< - PlanetscaleQueryResultHKT, - PlanetScalePreparedQueryHKT, - Record<string, never>, - ExtractTablesWithRelations<Record<string, never>> - > - - const client = memo(() => { - const result = new Client({ - host: Resource.Database.host, - username: Resource.Database.username, - password: Resource.Database.password, - }) - const db = drizzle(result, {}) - return db - }) - - export type TxOrDb = Transaction | ReturnType<typeof client> - - const TransactionContext = Context.create<{ - tx: TxOrDb - effects: (() => void | Promise<void>)[] - }>() - - export async function use<T>(callback: (trx: TxOrDb) => Promise<T>) { - try { - const { tx } = TransactionContext.use() - return tx.transaction(callback) - } catch (err) { - if (err instanceof Context.NotFound) { - const effects: (() => void | Promise<void>)[] = [] - const result = await TransactionContext.provide( - { - effects, - tx: client(), - }, - () => callback(client()), - ) - await Promise.all(effects.map((x) => x())) - return result - } - throw err - } - } - export async function fn<Input, T>(callback: (input: Input, trx: TxOrDb) => Promise<T>) { - return (input: Input) => use(async (tx) => callback(input, tx)) - } - - export async function effect(effect: () => any | Promise<any>) { - try { - const { effects } = TransactionContext.use() - effects.push(effect) - } catch { - await effect() - } - } - - export async function transaction<T>(callback: (tx: TxOrDb) => Promise<T>, config?: MySqlTransactionConfig) { - try { - const { tx } = TransactionContext.use() - return callback(tx) - } catch (err) { - if (err instanceof Context.NotFound) { - const effects: (() => void | Promise<void>)[] = [] - const result = await client().transaction(async (tx) => { - return TransactionContext.provide({ tx, effects }, () => callback(tx)) - }, config) - await Promise.all(effects.map((x) => x())) - return result - } - throw err - } - } -} diff --git a/cloud/core/src/drizzle/types.ts b/cloud/core/src/drizzle/types.ts deleted file mode 100644 index f16ad5a8a..000000000 --- a/cloud/core/src/drizzle/types.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { sql } from "drizzle-orm" -import { bigint, timestamp, varchar } from "drizzle-orm/mysql-core" - -export const ulid = (name: string) => varchar(name, { length: 30 }) - -export const workspaceColumns = { - get id() { - return ulid("id").notNull() - }, - get workspaceID() { - return ulid("workspace_id").notNull() - }, -} - -export const id = () => ulid("id").notNull() - -export const utc = (name: string) => - timestamp(name, { - fsp: 3, - }) - -export const currency = (name: string) => - bigint(name, { - mode: "number", - }) - -export const timestamps = { - timeCreated: utc("time_created").notNull().defaultNow(), - timeUpdated: utc("time_updated") - .notNull() - .default(sql`CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3)`), - timeDeleted: utc("time_deleted"), -} |
