-
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
Showing
7 changed files
with
108 additions
and
7 deletions.
There are no files selected for viewing
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,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 | ||
} | ||
} | ||
``` |
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,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}`) | ||
``` |
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,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, | ||
} | ||
} | ||
``` |
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