Skip to content

Commit

Permalink
Interim
Browse files Browse the repository at this point in the history
  • Loading branch information
benmerckx committed May 7, 2024
1 parent 47d3f51 commit b3bad95
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/core/Database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ export class Database<Meta extends QueryMeta = Either>
return this.close()
}

transact<T>(
gen: (tx: Transaction<Meta>) => Generator<Promise<unknown>, T>,
options?: TransactionOptions[Meta['dialect']]
): Promise<T> {
return (<Database<any>>this).transaction(async (tx: Transaction<Meta>) => {
const iter = gen(tx)
let current: IteratorResult<Promise<unknown>>
while ((current = iter.next(tx))) {
if (current.done) return current.value
await current.value
}
}, options)
}

transaction<T>(
this: Database<Sync>,
run: (tx: Transaction<Meta>) => T,
Expand Down
13 changes: 11 additions & 2 deletions src/core/Query.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -46,6 +46,15 @@ export abstract class Query<Result, Meta extends QueryMeta>
}
}

*[Symbol.iterator](): Generator<Promise<unknown>, Array<Result>, unknown> {
const interim = this.#exec('all')
const isAsync = interim instanceof Promise
if (!isAsync) return interim as Array<Result>
let result: unknown
yield interim.then(v => (result = v))
return result as Array<Result>
}

prepare<Inputs extends Record<string, unknown>>(name: string) {
return <PreparedQuery<Result, Inputs, Meta>>(
getData(this).resolver!.prepare(this, name)
Expand Down
Empty file added src/migrate/Scan.ts
Empty file.
15 changes: 15 additions & 0 deletions test/TestDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "dist",
"declaration": true,
"emitDeclarationOnly": true,
"noEmit": false
},
"include": ["src"]
}

0 comments on commit b3bad95

Please sign in to comment.