diff --git a/package-lock.json b/package-lock.json index 1100bd3..255400a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,14 +1,15 @@ { "name": "workers-qb", - "version": "1.4.0", + "version": "1.4.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "workers-qb", - "version": "1.4.0", + "version": "1.4.1", "license": "MIT", "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", @@ -556,6 +557,13 @@ "integrity": "sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==", "dev": true }, + "node_modules/@cloudflare/workers-types": { + "version": "4.20240815.0", + "resolved": "https://registry.npmjs.org/@cloudflare/workers-types/-/workers-types-4.20240815.0.tgz", + "integrity": "sha512-H/IXCOahT1lr4RKzsiCkyjM7+LCPLtl2wjxyLG8xMTNERR0XuD1Vcfns6TraE0cd5+IcKe7j3rpzBlSCjZ+61A==", + "dev": true, + "license": "MIT OR Apache-2.0" + }, "node_modules/@esbuild/aix-ppc64": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.21.5.tgz", diff --git a/package.json b/package.json index 467ae25..6b37e48 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/databases/do.ts b/src/databases/do.ts new file mode 100644 index 0000000..8e54289 --- /dev/null +++ b/src/databases/do.ts @@ -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), + } + } +} diff --git a/src/index.ts b/src/index.ts index a48f431..018fe32 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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'