workers-qb 1.7.0 → 1.8.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
@@ -19,12 +19,13 @@ Currently, 3 databases are supported:
19
19
 
20
20
  ## Features
21
21
 
22
- - [x] Zero dependencies.
22
+ - [x] Zero dependencies
23
23
  - [x] Fully typed/TypeScript support
24
+ - [x] [Migrations](https://workers-qb.massadas.com/migrations/)
24
25
  - [x] [Type Checks for data read](https://workers-qb.massadas.com/type-check/)
25
26
  - [x] [Create/drop tables](https://workers-qb.massadas.com/basic-queries/#dropping-and-creating-tables)
26
27
  - [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/)
28
+ - [x] [Modular selects](https://workers-qb.massadas.com/modular-selects/) (qb.select(...).where(...).where(...).one())
28
29
  - [x] [On Conflict for Inserts and Updates](https://workers-qb.massadas.com/advanced-queries/onConflict/)
29
30
  - [x] [Upsert](https://workers-qb.massadas.com/advanced-queries/upsert/)
30
31
 
@@ -40,7 +41,7 @@ npm install workers-qb --save
40
41
  import { D1QB } from 'workers-qb'
41
42
 
42
43
  export interface Env {
43
- DB: any
44
+ DB: D1Database
44
45
  }
45
46
 
46
47
  export default {
@@ -65,7 +66,7 @@ export default {
65
66
  .execute()
66
67
 
67
68
  // Or in a modular approach
68
- const employeeList = await qb.select<Employee>('employees').where('active = ?', true).execute()
69
+ const employeeListModular = await qb.select<Employee>('employees').where('active = ?', true).execute()
69
70
 
70
71
  // You get IDE type hints on each employee data, like:
71
72
  // employeeList.results[0].name
@@ -77,6 +78,26 @@ export default {
77
78
  }
78
79
  ```
79
80
 
81
+ ## Example for Cloudflare Durable Objects
82
+
83
+ ```ts
84
+ import { DOQB } from 'workers-qb'
85
+
86
+ export class DOSRS extends DurableObject {
87
+ getEmployees() {
88
+ const qb = new DOQB(this.ctx.storage.sql)
89
+
90
+ const fetched = qb
91
+ .fetchAll({
92
+ tableName: 'employees',
93
+ })
94
+ .execute()
95
+
96
+ return fetched.results
97
+ }
98
+ }
99
+ ```
100
+
80
101
  ## Example for Cloudflare Workers with PostgreSQL
81
102
 
82
103
  Remember to close the connection using `ctx.waitUntil(qb.close());` or `await qb.close();` at the end of your request.
package/dist/index.d.mts CHANGED
@@ -192,6 +192,7 @@ declare class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnO
192
192
  tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
193
193
  fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
194
194
  where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
195
+ whereIn<T extends string | Array<string>, P extends T extends Array<string> ? Primitive[][] : Primitive[]>(fields: T, values: P): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
195
196
  join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
196
197
  groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
197
198
  having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
package/dist/index.d.ts CHANGED
@@ -192,6 +192,7 @@ declare class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnO
192
192
  tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
193
193
  fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
194
194
  where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
195
+ whereIn<T extends string | Array<string>, P extends T extends Array<string> ? Primitive[][] : Primitive[]>(fields: T, values: P): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
195
196
  join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
196
197
  groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
197
198
  having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper, GenericResult, IsAsync>;
package/dist/index.js CHANGED
@@ -160,6 +160,52 @@ var SelectBuilder = class _SelectBuilder {
160
160
  this._fetchOne
161
161
  );
162
162
  }
163
+ whereIn(fields, values) {
164
+ let whereInCondition;
165
+ let whereInParams;
166
+ const seperateWithComma = (prev, next) => prev + ", " + next;
167
+ if (values.length === 0) {
168
+ return new _SelectBuilder(
169
+ {
170
+ ...this._options
171
+ },
172
+ this._fetchAll,
173
+ this._fetchOne
174
+ );
175
+ }
176
+ if (!Array.isArray(fields)) {
177
+ whereInCondition = `(${fields}) IN (VALUES `;
178
+ whereInCondition += values.map(() => "(?)").reduce(seperateWithComma);
179
+ whereInCondition += ")";
180
+ whereInParams = values;
181
+ } else {
182
+ const fieldLength = fields.length;
183
+ whereInCondition = `(${fields.map((val) => val).reduce(seperateWithComma)}) IN (VALUES `;
184
+ const valuesString = `(${[...new Array(fieldLength).keys()].map(() => "?").reduce(seperateWithComma)})`;
185
+ whereInCondition += [...new Array(fieldLength).keys()].map(() => valuesString).reduce(seperateWithComma);
186
+ whereInCondition += ")";
187
+ whereInParams = values.flat();
188
+ }
189
+ let conditions = [whereInCondition];
190
+ let params = whereInParams;
191
+ if (this._options.where?.conditions) {
192
+ conditions = this._options.where?.conditions.concat(conditions);
193
+ }
194
+ if (this._options.where?.params) {
195
+ params = this._options.where?.params.concat(params);
196
+ }
197
+ return new _SelectBuilder(
198
+ {
199
+ ...this._options,
200
+ where: {
201
+ conditions,
202
+ params
203
+ }
204
+ },
205
+ this._fetchAll,
206
+ this._fetchOne
207
+ );
208
+ }
163
209
  join(join) {
164
210
  return this._parseArray("join", this._options.join, join);
165
211
  }
package/dist/index.mjs CHANGED
@@ -118,6 +118,52 @@ var SelectBuilder = class _SelectBuilder {
118
118
  this._fetchOne
119
119
  );
120
120
  }
121
+ whereIn(fields, values) {
122
+ let whereInCondition;
123
+ let whereInParams;
124
+ const seperateWithComma = (prev, next) => prev + ", " + next;
125
+ if (values.length === 0) {
126
+ return new _SelectBuilder(
127
+ {
128
+ ...this._options
129
+ },
130
+ this._fetchAll,
131
+ this._fetchOne
132
+ );
133
+ }
134
+ if (!Array.isArray(fields)) {
135
+ whereInCondition = `(${fields}) IN (VALUES `;
136
+ whereInCondition += values.map(() => "(?)").reduce(seperateWithComma);
137
+ whereInCondition += ")";
138
+ whereInParams = values;
139
+ } else {
140
+ const fieldLength = fields.length;
141
+ whereInCondition = `(${fields.map((val) => val).reduce(seperateWithComma)}) IN (VALUES `;
142
+ const valuesString = `(${[...new Array(fieldLength).keys()].map(() => "?").reduce(seperateWithComma)})`;
143
+ whereInCondition += [...new Array(fieldLength).keys()].map(() => valuesString).reduce(seperateWithComma);
144
+ whereInCondition += ")";
145
+ whereInParams = values.flat();
146
+ }
147
+ let conditions = [whereInCondition];
148
+ let params = whereInParams;
149
+ if (this._options.where?.conditions) {
150
+ conditions = this._options.where?.conditions.concat(conditions);
151
+ }
152
+ if (this._options.where?.params) {
153
+ params = this._options.where?.params.concat(params);
154
+ }
155
+ return new _SelectBuilder(
156
+ {
157
+ ...this._options,
158
+ where: {
159
+ conditions,
160
+ params
161
+ }
162
+ },
163
+ this._fetchAll,
164
+ this._fetchOne
165
+ );
166
+ }
121
167
  join(join) {
122
168
  return this._parseArray("join", this._options.join, join);
123
169
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "workers-qb",
3
- "version": "1.7.0",
3
+ "version": "1.8.0",
4
4
  "description": "Zero dependencies Query Builder for Cloudflare Workers",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -14,7 +14,7 @@
14
14
  "build": "tsup src/index.ts --format cjs,esm --dts",
15
15
  "lint": "npx @biomejs/biome check src/ tests/ || (npx @biomejs/biome check --write src/ tests/; exit 1)",
16
16
  "test": "vitest run --root tests",
17
- "prepare": "husky install"
17
+ "prepare": "husky"
18
18
  },
19
19
  "publishConfig": {
20
20
  "access": "public"
@@ -62,7 +62,7 @@
62
62
  "husky": "^9.1.6",
63
63
  "tsup": "^8.3.5",
64
64
  "typescript": "^5.6.3",
65
- "vitest": "2.0.5",
65
+ "vitest": "2.1.8",
66
66
  "wrangler": "^3.86.0"
67
67
  }
68
68
  }