Skip to content

Commit

Permalink
Improve do qb (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
G4brym authored Aug 28, 2024
1 parent bd67772 commit b91917e
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 7 deletions.
3 changes: 3 additions & 0 deletions docs/mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ nav:
- getting-started.md
- type-check.md
- basic-queries.md
- modular-selects.md
- utility-methods.md
- Advanced Queries:
- advanced-queries/fields.md
- advanced-queries/join.md
Expand All @@ -50,6 +52,7 @@ nav:
- advanced-queries/raw-sql.md
- Supported Databases:
- databases/cloudflare-d1.md
- databases/cloudflare-do-srs.md
- databases/postgresql.md
- databases/bring-your-own-database.md
markdown_extensions:
Expand Down
19 changes: 19 additions & 0 deletions docs/pages/databases/cloudflare-do-srs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## Write queries within your DO

```ts
import { DOQB } from 'workers-qb'

export class DOSRS extends DurableObject {
async getEmployees() {
const qb = new DOQB(this.ctx.storage.sql)

const fetched = await qb
.fetchAll({
tableName: 'employees',
})
.execute()

return fetched.results
}
}
```
52 changes: 52 additions & 0 deletions docs/pages/modular-selects.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
You can now chain select parameters just like a normal ORM.
You can chain how many parameters as you would like.

For example this "normal" fetchAll query:

```ts
const qb = new D1QB(env.DB)

const result = await qb
.fetchAll({
tableName: 'testTable',
fields: ['id', 'name'],
where: {
conditions: 'field = ?1',
params: ['test'],
},
join: {
type: JoinTypes.LEFT,
table: 'employees',
on: 'testTable.employee_id = employees.id',
},
})
.execute()
```

Could be re-written like this:

```ts
const qb = new D1QB(env.DB)

const result = await qb
.select('testTable')
.fields(['id', 'name'])
.where('field = ?1', 'test')
.join({ type: JoinTypes.LEFT, table: 'employees', on: 'testTable.employee_id = employees.id' })
.execute()
```

Count method is also available for the modular selects

```ts
const qb = new D1QB(env.DB)

const result = await qb
.select('testTable')
.fields(['id', 'name'])
.where('field = ?1', 'test')
.join({ type: JoinTypes.LEFT, table: 'employees', on: 'testTable.employee_id = employees.id' })
.count()

console.log(`Total results: ${result.results.total}`)
```
27 changes: 27 additions & 0 deletions docs/pages/utility-methods.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
On selects, you have the option to `.count()` the rows without having to build a new query everytime.

```ts
import { OrderTypes } from 'workers-qb'
const qb = new D1QB(env.DB)

type EmployeeRoles = {
role: string
count: number
}

async function listEmployees(page = 0) {
const qs = qb.fetchAll<EmployeeRoles>({
tableName: 'employees',
limit: 20,
offset: page * 20,
})

const thisPageEmployees = await qs.execute()
const employeeCount = await qs.count()

return {
employees: thisPageEmployees.results,
total: employeeCount.results.total,
}
}
```
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "workers-qb",
"version": "1.4.2",
"version": "1.4.3",
"description": "Zero dependencies Query Builder for Cloudflare Workers",
"main": "./dist/index.js",
"module": "./dist/index.mjs",
Expand Down
8 changes: 4 additions & 4 deletions src/databases/do.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { SqlStorageCursor, type SqlStorage } from '@cloudflare/workers-types/exp
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<{}> {
export class DOQB extends QueryBuilder<{}> {
public db: SqlStorage

constructor(db: SqlStorage) {
Expand All @@ -27,7 +26,6 @@ export class DurableObjectDatabaseQB extends QueryBuilder<{}> {
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],
Expand All @@ -43,7 +41,6 @@ export class DurableObjectDatabaseQB extends QueryBuilder<{}> {
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],
}
Expand All @@ -55,3 +52,6 @@ export class DurableObjectDatabaseQB extends QueryBuilder<{}> {
}
}
}

// Deprecated class name
export class DurableObjectDatabaseQB extends DOQB {}

0 comments on commit b91917e

Please sign in to comment.