-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea038a5
commit 15b8356
Showing
4 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { QueryBuilder } from '../builder' | ||
import { SqlStorageCursor, type SqlStorage } from '@cloudflare/workers-types/experimental' | ||
import { Query } from '../tools' | ||
import { FetchTypes } from '../enums' | ||
|
||
// There's no wrapper because DO databases return the Cursor to be consumed | ||
export class DurableObjectDatabaseQB extends QueryBuilder<{}> { | ||
public db: SqlStorage | ||
|
||
constructor(db: SqlStorage) { | ||
super() | ||
this.db = db | ||
} | ||
|
||
async execute(query: Query) { | ||
if (this._debugger) { | ||
console.log({ | ||
'workers-qb': { | ||
query: query.query, | ||
arguments: query.arguments, | ||
fetchType: query.fetchType, | ||
}, | ||
}) | ||
} | ||
|
||
if (query.arguments) { | ||
let stmt = this.db.prepare(query.query) | ||
// @ts-expect-error Their types appear to be wrong here | ||
const result = stmt(...query.arguments) as SqlStorageCursor | ||
//FIXME: not efficient but the iterator that SRS is weird and I can only get it with a object form this way | ||
if (query.fetchType == FetchTypes.ONE) { | ||
return { | ||
results: Array.from(result)[0], | ||
} | ||
} | ||
|
||
// by default return everything | ||
return { | ||
results: Array.from(result), | ||
} | ||
} | ||
|
||
const cursor = this.db.exec(query.query) | ||
|
||
if (query.fetchType == FetchTypes.ONE) { | ||
//FIXME: not efficient but the iterator that SRS is weird and I can only get it with a object form this way | ||
return { | ||
results: Array.from(cursor)[0], | ||
} | ||
} | ||
|
||
// by default return everything | ||
return { | ||
results: Array.from(cursor), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './builder' | ||
export * from './databases/d1' | ||
export * from './databases/pg' | ||
export * from './databases/do' | ||
export * from './enums' | ||
export * from './interfaces' | ||
export * from './tools' |