-
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
24 changed files
with
417 additions
and
281 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
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,3 +1,5 @@ | ||
# Cloudflare D1 | ||
|
||
## Create a database | ||
|
||
```bash | ||
|
2 changes: 2 additions & 0 deletions
2
docs/pages/databases/cloudflare-do-srs.md → docs/pages/databases/cloudflare-do.md
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,3 +1,5 @@ | ||
# Cloudflare Durable Objects | ||
|
||
## Write queries within your DO | ||
|
||
```ts | ||
|
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
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,75 @@ | ||
## .count() | ||
|
||
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, | ||
} | ||
} | ||
``` | ||
|
||
## Logger | ||
|
||
To enable simple `console.log(...)` with the query and execution duration | ||
|
||
```ts | ||
const qb = new D1QB(env.DB) | ||
qb.setDebugger(true) // This call will define the default logger | ||
|
||
await qb | ||
.fetchAll<EmployeeRoles>({ | ||
tableName: 'employees', | ||
fields: ['id', 'name'], | ||
}) | ||
.execute() | ||
``` | ||
|
||
Running the example above will print into the console this: | ||
|
||
``` | ||
[workers-qb][34ms] {"query": "SELECT id, name FROM employees"} | ||
``` | ||
|
||
### Advanced Logger | ||
|
||
You can overwrite the default `console.log()` by passing a parameter to the query builder initializer | ||
|
||
```ts | ||
import { RawQuery, QueryLoggerMeta } from 'workers-qb' | ||
|
||
const qb = new D1QB(env.DB, { | ||
logger: async (query: RawQuery, meta: QueryLoggerMeta) => { | ||
// run your own logic | ||
// query timmings available in meta.duration in milliseconds | ||
}, | ||
}) | ||
|
||
await qb | ||
.fetchAll<EmployeeRoles>({ | ||
tableName: 'employees', | ||
fields: ['id', 'name'], | ||
}) | ||
.execute() | ||
``` | ||
|
||
With this, your function will always be called after the query is executed, even if the query throws an error. |
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.