Skip to content

Commit

Permalink
Add DO SRS storage (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
LuisDuarte1 authored Aug 27, 2024
1 parent ea038a5 commit 15b8356
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
12 changes: 10 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"url": "https://github.com/G4brym/workers-qb/issues"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20240815.0",
"@types/jest": "^29.5.12",
"@typescript-eslint/eslint-plugin": "^4.31.1",
"@typescript-eslint/parser": "^4.31.1",
Expand Down
57 changes: 57 additions & 0 deletions src/databases/do.ts
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),
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
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'

0 comments on commit 15b8356

Please sign in to comment.