refactor(effect-drizzle-sqlite): simplify single-row reads
This commit is contained in:
parent
25e546c837
commit
fd4887d45d
4 changed files with 34 additions and 14 deletions
|
|
@ -63,6 +63,10 @@ type SelectLike<A = unknown> = EffectLikeQuery<A> & {
|
|||
readonly all: () => A
|
||||
}
|
||||
|
||||
type GetLike<A = unknown> = EffectLikeQuery & {
|
||||
readonly get: () => A
|
||||
}
|
||||
|
||||
type MutationLike<A = unknown> = EffectLikeQuery<A> & {
|
||||
readonly all: () => A
|
||||
readonly run: () => A
|
||||
|
|
@ -104,6 +108,8 @@ const fromMutation = (query: MutationLike) => fromSync(query, () => (query.confi
|
|||
|
||||
const fromCount = (query: CountLike) => fromSync(query, () => Number(query.session.values(query.sql)[0]?.[0] ?? 0))
|
||||
|
||||
export const getOne = <A>(query: GetLike<A>) => fromSync(query, () => query.get())
|
||||
|
||||
const fromExecuteResult = (result: unknown) => {
|
||||
if (result && typeof result === "object" && "sync" in result && typeof result.sync === "function") {
|
||||
return result.sync()
|
||||
|
|
@ -155,6 +161,7 @@ const attachTransaction = <
|
|||
TRelations extends AnyRelations = EmptyRelations,
|
||||
>(db: SQLiteBunDatabase<TSchema, TRelations> & { readonly $client: Database }): EffectSQLiteDatabase<TSchema, TRelations> => {
|
||||
const txStack: Array<SQLiteTransaction<"sync", void, TSchema, TRelations>> = []
|
||||
const bound = new WeakMap<object, Map<PropertyKey, unknown>>()
|
||||
const current = () => txStack.at(-1) ?? db
|
||||
const runTransaction = (target: SQLiteBunDatabase<TSchema, TRelations> | SQLiteTransaction<"sync", void, TSchema, TRelations>) =>
|
||||
target.transaction.bind(target) as (
|
||||
|
|
@ -195,7 +202,11 @@ const attachTransaction = <
|
|||
|
||||
const target = current()
|
||||
const value = Reflect.get(target, property)
|
||||
return typeof value === "function" ? value.bind(target) : value
|
||||
if (typeof value !== "function") return value
|
||||
const methods = bound.get(target) ?? new Map<PropertyKey, unknown>()
|
||||
bound.set(target, methods)
|
||||
if (!methods.has(property)) methods.set(property, value.bind(target))
|
||||
return methods.get(property)
|
||||
},
|
||||
}) as EffectSQLiteDatabase<TSchema, TRelations>
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue