@minatojs/core
Advanced tools
Comparing version 2.6.1 to 2.7.0
@@ -331,2 +331,4 @@ import { Intersect, Dict, Extract, MaybeArray, Awaitable } from 'cosmokit'; | ||
upsert<T extends Keys<S>>(table: T, upsert: Row.Computed<S[T], Update<S[T]>[]>, keys?: MaybeArray<Keys<Flatten<S[T]>, Indexable>>): Promise<Driver.WriteResult>; | ||
withTransaction(callback: (database: Database<S>) => Promise<void>): Promise<void>; | ||
withTransaction<T extends Keys<S>>(table: T, callback: (database: Database<S>) => Promise<void>): Promise<void>; | ||
stopAll(): Promise<void>; | ||
@@ -353,2 +355,3 @@ drop<T extends Keys<S>>(table: T): Promise<void>; | ||
abstract upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<Driver.WriteResult>; | ||
abstract withTransaction(callback: (driver: Driver) => Promise<void>): Promise<void>; | ||
constructor(database: Database); | ||
@@ -355,0 +358,0 @@ model<S = any>(table: string | Selection.Immutable | Dict<string | Selection.Immutable>): Model<S>; |
{ | ||
"name": "@minatojs/core", | ||
"version": "2.6.1", | ||
"version": "2.7.0", | ||
"description": "Type Driven Database Framework", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.cjs", |
@@ -64,2 +64,4 @@ import { Awaitable, Dict, Intersect, makeArray, MaybeArray, valueMap } from 'cosmokit' | ||
const kTransaction = Symbol('transaction') | ||
export class Database<S = any> { | ||
@@ -188,2 +190,20 @@ public tables: { [K in Keys<S>]: Model<S[K]> } = Object.create(null) | ||
async withTransaction(callback: (database: Database<S>) => Promise<void>): Promise<void> | ||
async withTransaction<T extends Keys<S>>(table: T, callback: (database: Database<S>) => Promise<void>): Promise<void> | ||
async withTransaction(arg: any, ...args: any[]) { | ||
if (this[kTransaction]) throw new Error('nested transactions are not supported') | ||
const [table, callback] = typeof arg === 'string' ? [arg, ...args] : [null, arg, ...args] | ||
const driver = this.getDriver(table) | ||
return await driver.withTransaction(async (session) => { | ||
const database = new Proxy(this, { | ||
get(target, p, receiver) { | ||
if (p === kTransaction) return true | ||
else if (p === 'getDriver') return () => session | ||
else return Reflect.get(target, p, receiver) | ||
}, | ||
}) | ||
await callback(database) | ||
}) | ||
} | ||
async stopAll() { | ||
@@ -204,2 +224,3 @@ const drivers = Object.values(this.drivers) | ||
async stats() { | ||
await this.prepared() | ||
const stats: Driver.Stats = { size: 0, tables: {} } | ||
@@ -231,2 +252,3 @@ await Promise.all(Object.values(this.drivers).map(async (driver) => { | ||
abstract upsert(sel: Selection.Mutable, data: any[], keys: string[]): Promise<Driver.WriteResult> | ||
abstract withTransaction(callback: (driver: Driver) => Promise<void>): Promise<void> | ||
@@ -233,0 +255,0 @@ constructor(public database: Database) {} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
167291
3119