workers-qb 1.11.2 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +61 -21
- package/dist/index.d.mts +549 -26
- package/dist/index.d.ts +549 -26
- package/dist/index.js +735 -77
- package/dist/index.mjs +728 -77
- package/docs/advanced-queries.md +448 -7
- package/docs/basic-queries.md +144 -126
- package/docs/databases/d1.md +61 -34
- package/docs/databases/do.md +67 -52
- package/docs/databases/postgresql.md +67 -20
- package/docs/json-queries.md +55 -13
- package/docs/migrations.md +59 -12
- package/docs/type-check.md +199 -0
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -33,6 +33,17 @@ workers-qb is a lightweight query builder designed specifically for Cloudflare W
|
|
|
33
33
|
- Zero dependencies
|
|
34
34
|
- Lightweight and Worker-optimized
|
|
35
35
|
|
|
36
|
+
## AI Skills
|
|
37
|
+
|
|
38
|
+
workers-qb provides AI coding assistant skills to help you write queries faster. Install them using [npx skills](https://www.npmjs.com/package/skills):
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
npx skills install G4brym/workers-qb
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Available skills:
|
|
45
|
+
- **write-sql-queries** - Comprehensive guide for writing SELECT, INSERT, UPDATE, DELETE queries
|
|
46
|
+
|
|
36
47
|
## Supported Databases
|
|
37
48
|
|
|
38
49
|
- ☁️ [Cloudflare D1](https://workers-qb.massadas.com/databases/d1/)
|
|
@@ -45,8 +56,8 @@ workers-qb is a lightweight query builder designed specifically for Cloudflare W
|
|
|
45
56
|
### Core Features
|
|
46
57
|
- Zero dependencies
|
|
47
58
|
- Full TypeScript support
|
|
59
|
+
- Schema-aware type inference with autocomplete
|
|
48
60
|
- Database schema migrations
|
|
49
|
-
- Type checking for data reads
|
|
50
61
|
- Lazy row loading
|
|
51
62
|
|
|
52
63
|
### Query Operations
|
|
@@ -72,24 +83,30 @@ npm install workers-qb --save
|
|
|
72
83
|
```typescript
|
|
73
84
|
import { D1QB } from 'workers-qb'
|
|
74
85
|
|
|
86
|
+
// Define your database schema for full type safety
|
|
87
|
+
type Schema = {
|
|
88
|
+
employees: {
|
|
89
|
+
id: number
|
|
90
|
+
name: string
|
|
91
|
+
role: string
|
|
92
|
+
level: number
|
|
93
|
+
active: boolean
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
75
97
|
export interface Env {
|
|
76
98
|
DB: D1Database
|
|
77
99
|
}
|
|
78
100
|
|
|
79
101
|
export default {
|
|
80
102
|
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
81
|
-
const qb = new D1QB(env.DB)
|
|
103
|
+
const qb = new D1QB<Schema>(env.DB)
|
|
82
104
|
|
|
83
|
-
|
|
84
|
-
name: string
|
|
85
|
-
role: string
|
|
86
|
-
level: number
|
|
87
|
-
}
|
|
88
|
-
|
|
89
|
-
// Using object syntax
|
|
105
|
+
// Using object syntax - table names and fields autocomplete!
|
|
90
106
|
const employeeList = await qb
|
|
91
|
-
.fetchAll
|
|
92
|
-
tableName: 'employees',
|
|
107
|
+
.fetchAll({
|
|
108
|
+
tableName: 'employees', // ✓ Autocomplete
|
|
109
|
+
fields: ['name', 'role', 'level'], // ✓ Autocomplete
|
|
93
110
|
where: {
|
|
94
111
|
conditions: 'active = ?1',
|
|
95
112
|
params: [true],
|
|
@@ -99,10 +116,11 @@ export default {
|
|
|
99
116
|
|
|
100
117
|
// Using method chaining
|
|
101
118
|
const employeeListModular = await qb
|
|
102
|
-
.select
|
|
119
|
+
.select('employees')
|
|
103
120
|
.where('active = ?', true)
|
|
104
|
-
.
|
|
121
|
+
.all()
|
|
105
122
|
|
|
123
|
+
// Result type is automatically inferred!
|
|
106
124
|
return Response.json({
|
|
107
125
|
activeEmployees: employeeList.results?.length || 0,
|
|
108
126
|
})
|
|
@@ -115,13 +133,27 @@ export default {
|
|
|
115
133
|
```typescript
|
|
116
134
|
import { DOQB } from 'workers-qb'
|
|
117
135
|
|
|
118
|
-
|
|
136
|
+
type Schema = {
|
|
137
|
+
employees: {
|
|
138
|
+
id: number
|
|
139
|
+
name: string
|
|
140
|
+
role: string
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
export class MyDurableObject extends DurableObject {
|
|
145
|
+
#qb: DOQB<Schema>
|
|
146
|
+
|
|
147
|
+
constructor(ctx: DurableObjectState, env: Env) {
|
|
148
|
+
super(ctx, env)
|
|
149
|
+
this.#qb = new DOQB<Schema>(ctx.storage.sql)
|
|
150
|
+
}
|
|
151
|
+
|
|
119
152
|
getEmployees() {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
return qb
|
|
153
|
+
// DOQB operations are synchronous - no await needed!
|
|
154
|
+
return this.#qb
|
|
123
155
|
.fetchAll({
|
|
124
|
-
tableName: 'employees',
|
|
156
|
+
tableName: 'employees', // ✓ Autocomplete
|
|
125
157
|
})
|
|
126
158
|
.execute()
|
|
127
159
|
.results
|
|
@@ -146,18 +178,26 @@ Example usage:
|
|
|
146
178
|
import { PGQB } from 'workers-qb'
|
|
147
179
|
import { Client } from 'pg'
|
|
148
180
|
|
|
181
|
+
type Schema = {
|
|
182
|
+
employees: {
|
|
183
|
+
id: number
|
|
184
|
+
name: string
|
|
185
|
+
active: boolean
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
149
189
|
export interface Env {
|
|
150
190
|
DB_URL: string
|
|
151
191
|
}
|
|
152
192
|
|
|
153
193
|
export default {
|
|
154
194
|
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
155
|
-
const qb = new PGQB(new Client(env.DB_URL))
|
|
195
|
+
const qb = new PGQB<Schema>(new Client(env.DB_URL))
|
|
156
196
|
await qb.connect()
|
|
157
197
|
|
|
158
198
|
const fetched = await qb
|
|
159
199
|
.fetchOne({
|
|
160
|
-
tableName: 'employees',
|
|
200
|
+
tableName: 'employees', // ✓ Autocomplete
|
|
161
201
|
fields: 'count(*) as count',
|
|
162
202
|
where: {
|
|
163
203
|
conditions: 'active = ?1',
|
|
@@ -168,7 +208,7 @@ export default {
|
|
|
168
208
|
|
|
169
209
|
// Important: Close the connection
|
|
170
210
|
ctx.waitUntil(qb.close())
|
|
171
|
-
|
|
211
|
+
|
|
172
212
|
return Response.json({
|
|
173
213
|
activeEmployees: fetched.results?.count || 0,
|
|
174
214
|
})
|