workers-qb 1.12.0 → 1.14.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 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/)
@@ -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
- type Employee = {
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<Employee>({
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<Employee>('employees')
119
+ .select('employees')
103
120
  .where('active = ?', true)
104
- .execute()
121
+ .all()
105
122
 
123
+ // Result type is automatically inferred!
106
124
  return Response.json({
107
125
  activeEmployees: employeeList.results?.length || 0,
108
126
  })
@@ -110,51 +128,32 @@ export default {
110
128
  }
111
129
  ```
112
130
 
113
- ### Schema-Aware Type Inference
114
-
115
- Define your database schema once and get autocomplete for table names, column names, and automatic result type inference:
131
+ ### Cloudflare Durable Objects
116
132
 
117
133
  ```typescript
118
- import { D1QB } from 'workers-qb'
134
+ import { DOQB } from 'workers-qb'
119
135
 
120
- // Define your schema
121
136
  type Schema = {
122
137
  employees: {
123
138
  id: number
124
139
  name: string
125
140
  role: string
126
- active: boolean
127
141
  }
128
142
  }
129
143
 
130
- // Initialize with schema type
131
- const qb = new D1QB<Schema>(env.DB)
132
-
133
- // Get full autocomplete and type inference
134
- const employees = await qb.fetchAll({
135
- tableName: 'employees', // Autocomplete: 'employees'
136
- fields: ['id', 'name'], // Autocomplete: 'id' | 'name' | 'role' | 'active'
137
- orderBy: { name: 'ASC' }, // Keys autocomplete to column names
138
- }).execute()
139
-
140
- // Result type is automatically inferred as { id: number; name: string }[]
141
- employees.results?.forEach(emp => {
142
- console.log(emp.id, emp.name) // Fully typed!
143
- })
144
- ```
145
-
146
- ### Cloudflare Durable Objects
144
+ export class MyDurableObject extends DurableObject {
145
+ #qb: DOQB<Schema>
147
146
 
148
- ```typescript
149
- import { DOQB } from 'workers-qb'
147
+ constructor(ctx: DurableObjectState, env: Env) {
148
+ super(ctx, env)
149
+ this.#qb = new DOQB<Schema>(ctx.storage.sql)
150
+ }
150
151
 
151
- export class DOSRS extends DurableObject {
152
152
  getEmployees() {
153
- const qb = new DOQB(this.ctx.storage.sql)
154
-
155
- return qb
153
+ // DOQB operations are synchronous - no await needed!
154
+ return this.#qb
156
155
  .fetchAll({
157
- tableName: 'employees',
156
+ tableName: 'employees', // ✓ Autocomplete
158
157
  })
159
158
  .execute()
160
159
  .results
@@ -179,18 +178,26 @@ Example usage:
179
178
  import { PGQB } from 'workers-qb'
180
179
  import { Client } from 'pg'
181
180
 
181
+ type Schema = {
182
+ employees: {
183
+ id: number
184
+ name: string
185
+ active: boolean
186
+ }
187
+ }
188
+
182
189
  export interface Env {
183
190
  DB_URL: string
184
191
  }
185
192
 
186
193
  export default {
187
194
  async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
188
- const qb = new PGQB(new Client(env.DB_URL))
195
+ const qb = new PGQB<Schema>(new Client(env.DB_URL))
189
196
  await qb.connect()
190
197
 
191
198
  const fetched = await qb
192
199
  .fetchOne({
193
- tableName: 'employees',
200
+ tableName: 'employees', // ✓ Autocomplete
194
201
  fields: 'count(*) as count',
195
202
  where: {
196
203
  conditions: 'active = ?1',
@@ -201,7 +208,7 @@ export default {
201
208
 
202
209
  // Important: Close the connection
203
210
  ctx.waitUntil(qb.close())
204
-
211
+
205
212
  return Response.json({
206
213
  activeEmployees: fetched.results?.count || 0,
207
214
  })