Skip to content

Commit 172d8aa

Browse files
authored
Add better logger (#55)
1 parent b91917e commit 172d8aa

24 files changed

+417
-281
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ traditional ORM.
1010
`workers-qb` is not intended to provide ORM-like functionality, rather to make it easier to interact with the database
1111
from code for direct SQL access using convenient wrapper methods.
1212

13-
Currently, 2 databases are supported:
13+
Currently, 3 databases are supported:
1414

15-
- [Cloudflare D1](https://developers.cloudflare.com/d1/)
16-
- [PostgreSQL (using node-postgres)](https://developers.cloudflare.com/workers/databases/connect-to-postgres/)
15+
- [Cloudflare D1](https://workers-qb.massadas.com/databases/cloudflare-d1/)
16+
- [Cloudflare Durable Objects](https://workers-qb.massadas.com/databases/cloudflare-do/)
17+
- [PostgreSQL (using node-postgres)](https://workers-qb.massadas.com/databases/postgresql/)
18+
- [Bring your own Database](https://workers-qb.massadas.com/databases/bring-your-own-database/)
1719

1820
## Features
1921

@@ -22,11 +24,9 @@ Currently, 2 databases are supported:
2224
- [x] [Type Checks for data read](https://workers-qb.massadas.com/type-check/)
2325
- [x] [Create/drop tables](https://workers-qb.massadas.com/basic-queries/#dropping-and-creating-tables)
2426
- [x] [Insert/Bulk Inserts/Update/Select/Delete/Join queries](https://workers-qb.massadas.com/basic-queries/)
27+
- [x] [Modular selects](https://workers-qb.massadas.com/modular-selects/)
2528
- [x] [On Conflict for Inserts and Updates](https://workers-qb.massadas.com/advanced-queries/onConflict/)
2629
- [x] [Upsert](https://workers-qb.massadas.com/advanced-queries/upsert/)
27-
- [x] [Support for Cloudflare Workers D1](https://workers-qb.massadas.com/databases/cloudflare-d1/)
28-
- [x] [Support for Cloudflare Workers PostgreSQL (using node-postgres)](https://workers-qb.massadas.com/databases/postgresql/)
29-
- [ ] Named parameters (waiting for full support in D1)
3030

3131
## Installation
3232

docs/mkdocs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ nav:
3737
- type-check.md
3838
- basic-queries.md
3939
- modular-selects.md
40-
- utility-methods.md
40+
- utilities.md
4141
- Advanced Queries:
4242
- advanced-queries/fields.md
4343
- advanced-queries/join.md
@@ -52,7 +52,7 @@ nav:
5252
- advanced-queries/raw-sql.md
5353
- Supported Databases:
5454
- databases/cloudflare-d1.md
55-
- databases/cloudflare-do-srs.md
55+
- databases/cloudflare-do.md
5656
- databases/postgresql.md
5757
- databases/bring-your-own-database.md
5858
markdown_extensions:

docs/pages/databases/cloudflare-d1.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Cloudflare D1
2+
13
## Create a database
24

35
```bash
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Cloudflare Durable Objects
2+
13
## Write queries within your DO
24

35
```ts

docs/pages/databases/postgresql.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# PostgreSQL
2+
13
## Create a database
24

35
After creating your database, you must allow access from outside the private network, due to limitations on accessing

docs/pages/getting-started.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export default {
3434
})
3535
.execute()
3636

37+
// Or in a modular approach
38+
const employeeList = await qb.select<Employee>('employees').where('active = ?', true).execute()
39+
3740
// You get IDE type hints on each employee data, like:
3841
// employeeList.results[0].name
3942

docs/pages/index.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# workers-qb
22

3+
### [Read the documentation here!](https://workers-qb.massadas.com/)
4+
35
Zero dependencies Query Builder for [Cloudflare Workers](https://developers.cloudflare.com/workers/)
46

57
This module provides a simple standardized interface while keeping the benefits and speed of using raw queries over a
@@ -8,10 +10,12 @@ traditional ORM.
810
`workers-qb` is not intended to provide ORM-like functionality, rather to make it easier to interact with the database
911
from code for direct SQL access using convenient wrapper methods.
1012

11-
Currently, 2 databases are supported:
13+
Currently, 3 databases are supported:
1214

13-
- [Cloudflare D1](https://developers.cloudflare.com/d1/)
14-
- [PostgreSQL (using node-postgres)](https://developers.cloudflare.com/workers/databases/connect-to-postgres/)
15+
- [Cloudflare D1](https://workers-qb.massadas.com/databases/cloudflare-d1/)
16+
- [Cloudflare Durable Objects](https://workers-qb.massadas.com/databases/cloudflare-do/)
17+
- [PostgreSQL (using node-postgres)](https://workers-qb.massadas.com/databases/postgresql/)
18+
- [Bring your own Database](https://workers-qb.massadas.com/databases/bring-your-own-database/)
1519

1620
## Features
1721

@@ -20,8 +24,6 @@ Currently, 2 databases are supported:
2024
- [x] [Type Checks for data read](https://workers-qb.massadas.com/type-check/)
2125
- [x] [Create/drop tables](https://workers-qb.massadas.com/basic-queries/#dropping-and-creating-tables)
2226
- [x] [Insert/Bulk Inserts/Update/Select/Delete/Join queries](https://workers-qb.massadas.com/basic-queries/)
27+
- [x] [Modular selects](https://workers-qb.massadas.com/modular-selects/)
2328
- [x] [On Conflict for Inserts and Updates](https://workers-qb.massadas.com/advanced-queries/onConflict/)
2429
- [x] [Upsert](https://workers-qb.massadas.com/advanced-queries/upsert/)
25-
- [x] [Support for Cloudflare Workers D1](https://workers-qb.massadas.com/databases/cloudflare-d1/)
26-
- [x] [Support for Cloudflare Workers PostgreSQL (using node-postgres)](https://workers-qb.massadas.com/databases/postgresql/)
27-
- [ ] Named parameters (waiting for full support in D1)

docs/pages/utilities.md

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
## .count()
2+
3+
On selects, you have the option to `.count()` the rows without having to build a new query everytime.
4+
5+
```ts
6+
import { OrderTypes } from 'workers-qb'
7+
const qb = new D1QB(env.DB)
8+
9+
type EmployeeRoles = {
10+
role: string
11+
count: number
12+
}
13+
14+
async function listEmployees(page = 0) {
15+
const qs = qb.fetchAll<EmployeeRoles>({
16+
tableName: 'employees',
17+
limit: 20,
18+
offset: page * 20,
19+
})
20+
21+
const thisPageEmployees = await qs.execute()
22+
const employeeCount = await qs.count()
23+
24+
return {
25+
employees: thisPageEmployees.results,
26+
total: employeeCount.results.total,
27+
}
28+
}
29+
```
30+
31+
## Logger
32+
33+
To enable simple `console.log(...)` with the query and execution duration
34+
35+
```ts
36+
const qb = new D1QB(env.DB)
37+
qb.setDebugger(true) // This call will define the default logger
38+
39+
await qb
40+
.fetchAll<EmployeeRoles>({
41+
tableName: 'employees',
42+
fields: ['id', 'name'],
43+
})
44+
.execute()
45+
```
46+
47+
Running the example above will print into the console this:
48+
49+
```
50+
[workers-qb][34ms] {"query": "SELECT id, name FROM employees"}
51+
```
52+
53+
### Advanced Logger
54+
55+
You can overwrite the default `console.log()` by passing a parameter to the query builder initializer
56+
57+
```ts
58+
import { RawQuery, QueryLoggerMeta } from 'workers-qb'
59+
60+
const qb = new D1QB(env.DB, {
61+
logger: async (query: RawQuery, meta: QueryLoggerMeta) => {
62+
// run your own logic
63+
// query timmings available in meta.duration in milliseconds
64+
},
65+
})
66+
67+
await qb
68+
.fetchAll<EmployeeRoles>({
69+
tableName: 'employees',
70+
fields: ['id', 'name'],
71+
})
72+
.execute()
73+
```
74+
75+
With this, your function will always be called after the query is executed, even if the query throws an error.

docs/pages/utility-methods.md

Lines changed: 0 additions & 27 deletions
This file was deleted.

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)