diff --git a/src/core/Database.ts b/src/core/Database.ts index 1613837..eb4ac84 100644 --- a/src/core/Database.ts +++ b/src/core/Database.ts @@ -37,6 +37,20 @@ export class Database return this.close() } + transact( + gen: (tx: Transaction) => Generator, T>, + options?: TransactionOptions[Meta['dialect']] + ): Promise { + return (>this).transaction(async (tx: Transaction) => { + const iter = gen(tx) + let current: IteratorResult> + while ((current = iter.next(tx))) { + if (current.done) return current.value + await current.value + } + }, options) + } + transaction( this: Database, run: (tx: Transaction) => T, diff --git a/src/core/Query.ts b/src/core/Query.ts index 6705eb4..1e03a78 100644 --- a/src/core/Query.ts +++ b/src/core/Query.ts @@ -1,12 +1,12 @@ import { - type HasTarget, getData, getResolver, hasResolver, internalData, internalQuery, type HasQuery, - type HasResolver + type HasResolver, + type HasTarget } from './Internal.ts' import type {Async, QueryMeta, Sync} from './MetaData.ts' import type {PreparedStatement, Resolver} from './Resolver.ts' @@ -46,6 +46,15 @@ export abstract class Query } } + *[Symbol.iterator](): Generator, Array, unknown> { + const interim = this.#exec('all') + const isAsync = interim instanceof Promise + if (!isAsync) return interim as Array + let result: unknown + yield interim.then(v => (result = v)) + return result as Array + } + prepare>(name: string) { return >( getData(this).resolver!.prepare(this, name) diff --git a/src/migrate/Scan.ts b/src/migrate/Scan.ts new file mode 100644 index 0000000..e69de29 diff --git a/test/TestDriver.ts b/test/TestDriver.ts index d3a5626..5c83cc8 100644 --- a/test/TestDriver.ts +++ b/test/TestDriver.ts @@ -220,6 +220,21 @@ export async function testDriver( } }) + Test.it('universal transactions', async () => { + const result = await db.transact(function* (tx) { + yield* tx.createTable(Node) + yield* tx.insert(Node).values({ + textField: 'hello', + bool: true + }) + const nodes = yield* tx.select().from(Node) + Assert.isEqual(nodes, [{id: 1, textField: 'hello', bool: true}]) + yield* tx.dropTable(Node) + return 1 + }) + Assert.isEqual(result, 1) + }) + Test.it('constraints and indexes', async () => { try { await db.createTable(TableA) diff --git a/tsconfig.build.json b/tsconfig.build.json index 4db1964..b90fc83 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -1,10 +1,4 @@ { "extends": "./tsconfig.json", - "compilerOptions": { - "outDir": "dist", - "declaration": true, - "emitDeclarationOnly": true, - "noEmit": false - }, "include": ["src"] }