workers-qb 1.3.1 → 1.4.1
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 +3 -0
- package/dist/index.d.mts +24 -3
- package/dist/index.d.ts +24 -3
- package/dist/index.js +149 -13
- package/dist/index.mjs +149 -13
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -64,6 +64,9 @@ export default {
|
|
|
64
64
|
})
|
|
65
65
|
.execute()
|
|
66
66
|
|
|
67
|
+
// Or in a modular approach
|
|
68
|
+
const employeeList = await qb.select<Employee>('employees').where('active = ?', true).execute()
|
|
69
|
+
|
|
67
70
|
// You get IDE type hints on each employee data, like:
|
|
68
71
|
// employeeList.results[0].name
|
|
69
72
|
|
package/dist/index.d.mts
CHANGED
|
@@ -52,7 +52,7 @@ type DefaultObject = Record<string, Primitive>;
|
|
|
52
52
|
type DefaultReturnObject = Record<string, null | string | number | boolean | bigint>;
|
|
53
53
|
type Where = {
|
|
54
54
|
conditions: string | Array<string>;
|
|
55
|
-
params?: Primitive[];
|
|
55
|
+
params?: Primitive | Primitive[];
|
|
56
56
|
} | string | Array<string>;
|
|
57
57
|
type Join = {
|
|
58
58
|
type?: string | JoinTypes;
|
|
@@ -66,7 +66,7 @@ type SelectOne = {
|
|
|
66
66
|
where?: Where;
|
|
67
67
|
join?: Join | Array<Join>;
|
|
68
68
|
groupBy?: string | Array<string>;
|
|
69
|
-
having?: string
|
|
69
|
+
having?: string | Array<string>;
|
|
70
70
|
orderBy?: string | Array<string> | Record<string, string | OrderTypes>;
|
|
71
71
|
offset?: number;
|
|
72
72
|
};
|
|
@@ -156,6 +156,26 @@ type OneResult<ResultWrapper, Result> = Merge<ResultWrapper, {
|
|
|
156
156
|
results?: Result;
|
|
157
157
|
}>;
|
|
158
158
|
|
|
159
|
+
declare class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnObject> {
|
|
160
|
+
_debugger: boolean;
|
|
161
|
+
private _options;
|
|
162
|
+
private _queryBuilder;
|
|
163
|
+
constructor(options: Partial<SelectAll>, queryBuilder: (params: SelectAll) => Query);
|
|
164
|
+
setDebugger(state: boolean): void;
|
|
165
|
+
tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
166
|
+
fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
167
|
+
where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
168
|
+
join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
169
|
+
groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
170
|
+
having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
171
|
+
orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
172
|
+
offset(offset: SelectAll['offset']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
173
|
+
limit(limit: SelectAll['limit']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
174
|
+
private _parseArray;
|
|
175
|
+
getQuery(): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
176
|
+
execute(): Promise<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
177
|
+
}
|
|
178
|
+
|
|
159
179
|
declare class QueryBuilder<GenericResultWrapper> {
|
|
160
180
|
_debugger: boolean;
|
|
161
181
|
setDebugger(state: boolean): void;
|
|
@@ -170,6 +190,7 @@ declare class QueryBuilder<GenericResultWrapper> {
|
|
|
170
190
|
tableName: string;
|
|
171
191
|
ifExists?: boolean;
|
|
172
192
|
}): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
193
|
+
select<GenericResult = DefaultReturnObject>(tableName: string): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
173
194
|
fetchOne<GenericResult = DefaultReturnObject>(params: SelectOne): Query<OneResult<GenericResultWrapper, GenericResult>>;
|
|
174
195
|
fetchAll<GenericResult = DefaultReturnObject>(params: SelectAll): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
175
196
|
raw<GenericResult = DefaultReturnObject>(params: RawQueryFetchOne): Query<OneResult<GenericResultWrapper, GenericResult>>;
|
|
@@ -192,7 +213,7 @@ declare class QueryBuilder<GenericResultWrapper> {
|
|
|
192
213
|
_where(value?: Where): string;
|
|
193
214
|
_join(value?: Join | Array<Join>): string;
|
|
194
215
|
_groupBy(value?: string | Array<string>): string;
|
|
195
|
-
_having(value?: string): string;
|
|
216
|
+
_having(value?: string | Array<string>): string;
|
|
196
217
|
_orderBy(value?: string | Array<string> | Record<string, string | OrderTypes>): string;
|
|
197
218
|
_limit(value?: number): string;
|
|
198
219
|
_offset(value?: number): string;
|
package/dist/index.d.ts
CHANGED
|
@@ -52,7 +52,7 @@ type DefaultObject = Record<string, Primitive>;
|
|
|
52
52
|
type DefaultReturnObject = Record<string, null | string | number | boolean | bigint>;
|
|
53
53
|
type Where = {
|
|
54
54
|
conditions: string | Array<string>;
|
|
55
|
-
params?: Primitive[];
|
|
55
|
+
params?: Primitive | Primitive[];
|
|
56
56
|
} | string | Array<string>;
|
|
57
57
|
type Join = {
|
|
58
58
|
type?: string | JoinTypes;
|
|
@@ -66,7 +66,7 @@ type SelectOne = {
|
|
|
66
66
|
where?: Where;
|
|
67
67
|
join?: Join | Array<Join>;
|
|
68
68
|
groupBy?: string | Array<string>;
|
|
69
|
-
having?: string
|
|
69
|
+
having?: string | Array<string>;
|
|
70
70
|
orderBy?: string | Array<string> | Record<string, string | OrderTypes>;
|
|
71
71
|
offset?: number;
|
|
72
72
|
};
|
|
@@ -156,6 +156,26 @@ type OneResult<ResultWrapper, Result> = Merge<ResultWrapper, {
|
|
|
156
156
|
results?: Result;
|
|
157
157
|
}>;
|
|
158
158
|
|
|
159
|
+
declare class SelectBuilder<GenericResultWrapper, GenericResult = DefaultReturnObject> {
|
|
160
|
+
_debugger: boolean;
|
|
161
|
+
private _options;
|
|
162
|
+
private _queryBuilder;
|
|
163
|
+
constructor(options: Partial<SelectAll>, queryBuilder: (params: SelectAll) => Query);
|
|
164
|
+
setDebugger(state: boolean): void;
|
|
165
|
+
tableName(tableName: SelectAll['tableName']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
166
|
+
fields(fields: SelectAll['fields']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
167
|
+
where(conditions: string | Array<string>, params?: Primitive | Primitive[]): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
168
|
+
join(join: SelectAll['join']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
169
|
+
groupBy(groupBy: SelectAll['groupBy']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
170
|
+
having(having: SelectAll['having']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
171
|
+
orderBy(orderBy: SelectAll['orderBy']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
172
|
+
offset(offset: SelectAll['offset']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
173
|
+
limit(limit: SelectAll['limit']): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
174
|
+
private _parseArray;
|
|
175
|
+
getQuery(): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
176
|
+
execute(): Promise<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
177
|
+
}
|
|
178
|
+
|
|
159
179
|
declare class QueryBuilder<GenericResultWrapper> {
|
|
160
180
|
_debugger: boolean;
|
|
161
181
|
setDebugger(state: boolean): void;
|
|
@@ -170,6 +190,7 @@ declare class QueryBuilder<GenericResultWrapper> {
|
|
|
170
190
|
tableName: string;
|
|
171
191
|
ifExists?: boolean;
|
|
172
192
|
}): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
193
|
+
select<GenericResult = DefaultReturnObject>(tableName: string): SelectBuilder<GenericResultWrapper, GenericResult>;
|
|
173
194
|
fetchOne<GenericResult = DefaultReturnObject>(params: SelectOne): Query<OneResult<GenericResultWrapper, GenericResult>>;
|
|
174
195
|
fetchAll<GenericResult = DefaultReturnObject>(params: SelectAll): Query<ArrayResult<GenericResultWrapper, GenericResult>>;
|
|
175
196
|
raw<GenericResult = DefaultReturnObject>(params: RawQueryFetchOne): Query<OneResult<GenericResultWrapper, GenericResult>>;
|
|
@@ -192,7 +213,7 @@ declare class QueryBuilder<GenericResultWrapper> {
|
|
|
192
213
|
_where(value?: Where): string;
|
|
193
214
|
_join(value?: Join | Array<Join>): string;
|
|
194
215
|
_groupBy(value?: string | Array<string>): string;
|
|
195
|
-
_having(value?: string): string;
|
|
216
|
+
_having(value?: string | Array<string>): string;
|
|
196
217
|
_orderBy(value?: string | Array<string> | Record<string, string | OrderTypes>): string;
|
|
197
218
|
_limit(value?: number): string;
|
|
198
219
|
_offset(value?: number): string;
|
package/dist/index.js
CHANGED
|
@@ -82,7 +82,112 @@ var Query = class {
|
|
|
82
82
|
}
|
|
83
83
|
};
|
|
84
84
|
|
|
85
|
-
// src/
|
|
85
|
+
// src/modularBuilder.ts
|
|
86
|
+
var SelectBuilder = class _SelectBuilder {
|
|
87
|
+
_debugger = false;
|
|
88
|
+
_options = {};
|
|
89
|
+
_queryBuilder;
|
|
90
|
+
constructor(options, queryBuilder) {
|
|
91
|
+
this._options = options;
|
|
92
|
+
this._queryBuilder = queryBuilder;
|
|
93
|
+
}
|
|
94
|
+
setDebugger(state) {
|
|
95
|
+
this._debugger = state;
|
|
96
|
+
}
|
|
97
|
+
tableName(tableName) {
|
|
98
|
+
return new _SelectBuilder(
|
|
99
|
+
{
|
|
100
|
+
...this._options,
|
|
101
|
+
tableName
|
|
102
|
+
},
|
|
103
|
+
this._queryBuilder
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
fields(fields) {
|
|
107
|
+
return this._parseArray("fields", this._options.fields, fields);
|
|
108
|
+
}
|
|
109
|
+
where(conditions, params) {
|
|
110
|
+
if (!Array.isArray(conditions)) {
|
|
111
|
+
conditions = [conditions];
|
|
112
|
+
}
|
|
113
|
+
if (params === void 0) params = [];
|
|
114
|
+
if (!Array.isArray(params)) {
|
|
115
|
+
params = [params];
|
|
116
|
+
}
|
|
117
|
+
if (this._options.where?.conditions) {
|
|
118
|
+
conditions = this._options.where.conditions.concat(conditions);
|
|
119
|
+
}
|
|
120
|
+
if (this._options.where?.params) {
|
|
121
|
+
params = this._options.where.params.concat(params);
|
|
122
|
+
}
|
|
123
|
+
return new _SelectBuilder(
|
|
124
|
+
{
|
|
125
|
+
...this._options,
|
|
126
|
+
where: {
|
|
127
|
+
conditions,
|
|
128
|
+
params
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
this._queryBuilder
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
join(join) {
|
|
135
|
+
return this._parseArray("join", this._options.join, join);
|
|
136
|
+
}
|
|
137
|
+
groupBy(groupBy) {
|
|
138
|
+
return this._parseArray("groupBy", this._options.groupBy, groupBy);
|
|
139
|
+
}
|
|
140
|
+
having(having) {
|
|
141
|
+
return this._parseArray("having", this._options.having, having);
|
|
142
|
+
}
|
|
143
|
+
orderBy(orderBy) {
|
|
144
|
+
return this._parseArray("orderBy", this._options.orderBy, orderBy);
|
|
145
|
+
}
|
|
146
|
+
offset(offset) {
|
|
147
|
+
return new _SelectBuilder(
|
|
148
|
+
{
|
|
149
|
+
...this._options,
|
|
150
|
+
offset
|
|
151
|
+
},
|
|
152
|
+
this._queryBuilder
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
limit(limit) {
|
|
156
|
+
return new _SelectBuilder(
|
|
157
|
+
{
|
|
158
|
+
...this._options,
|
|
159
|
+
limit
|
|
160
|
+
},
|
|
161
|
+
this._queryBuilder
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
_parseArray(fieldName, option, value) {
|
|
165
|
+
let val = [];
|
|
166
|
+
if (!Array.isArray(value)) {
|
|
167
|
+
val.push(value);
|
|
168
|
+
} else {
|
|
169
|
+
val = value;
|
|
170
|
+
}
|
|
171
|
+
if (option && Array.isArray(option)) {
|
|
172
|
+
val = [...option, ...val];
|
|
173
|
+
}
|
|
174
|
+
return new _SelectBuilder(
|
|
175
|
+
{
|
|
176
|
+
...this._options,
|
|
177
|
+
[fieldName]: val
|
|
178
|
+
},
|
|
179
|
+
this._queryBuilder
|
|
180
|
+
);
|
|
181
|
+
}
|
|
182
|
+
getQuery() {
|
|
183
|
+
return this._queryBuilder(this._options);
|
|
184
|
+
}
|
|
185
|
+
async execute() {
|
|
186
|
+
return this._queryBuilder(this._options).execute();
|
|
187
|
+
}
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
// src/builder.ts
|
|
86
191
|
var QueryBuilder = class {
|
|
87
192
|
_debugger = false;
|
|
88
193
|
setDebugger(state) {
|
|
@@ -108,13 +213,23 @@ var QueryBuilder = class {
|
|
|
108
213
|
return this.execute(q);
|
|
109
214
|
}, `DROP TABLE ${params.ifExists ? "IF EXISTS" : ""} ${params.tableName}`);
|
|
110
215
|
}
|
|
216
|
+
select(tableName) {
|
|
217
|
+
return new SelectBuilder(
|
|
218
|
+
{
|
|
219
|
+
tableName
|
|
220
|
+
},
|
|
221
|
+
(params) => {
|
|
222
|
+
return this.fetchAll(params);
|
|
223
|
+
}
|
|
224
|
+
);
|
|
225
|
+
}
|
|
111
226
|
fetchOne(params) {
|
|
112
227
|
return new Query(
|
|
113
228
|
(q) => {
|
|
114
229
|
return this.execute(q);
|
|
115
230
|
},
|
|
116
231
|
this._select({ ...params, limit: 1 }),
|
|
117
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
232
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
118
233
|
"ONE" /* ONE */
|
|
119
234
|
);
|
|
120
235
|
}
|
|
@@ -124,7 +239,7 @@ var QueryBuilder = class {
|
|
|
124
239
|
return this.execute(q);
|
|
125
240
|
},
|
|
126
241
|
this._select(params),
|
|
127
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
242
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
128
243
|
"ALL" /* ALL */
|
|
129
244
|
);
|
|
130
245
|
}
|
|
@@ -168,7 +283,11 @@ var QueryBuilder = class {
|
|
|
168
283
|
update(params) {
|
|
169
284
|
let args = this._parse_arguments(params.data);
|
|
170
285
|
if (typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params) {
|
|
171
|
-
|
|
286
|
+
if (Array.isArray(params.where?.params)) {
|
|
287
|
+
args = params.where?.params.concat(args);
|
|
288
|
+
} else {
|
|
289
|
+
args = [params.where?.params].concat(args);
|
|
290
|
+
}
|
|
172
291
|
}
|
|
173
292
|
return new Query(
|
|
174
293
|
(q) => {
|
|
@@ -185,7 +304,7 @@ var QueryBuilder = class {
|
|
|
185
304
|
return this.execute(q);
|
|
186
305
|
},
|
|
187
306
|
this._delete(params),
|
|
188
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
307
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
189
308
|
"ALL" /* ALL */
|
|
190
309
|
);
|
|
191
310
|
}
|
|
@@ -228,7 +347,11 @@ var QueryBuilder = class {
|
|
|
228
347
|
if (params.onConflict && typeof params.onConflict === "object") {
|
|
229
348
|
onConflict = this._onConflict(params.onConflict);
|
|
230
349
|
if (typeof params.onConflict?.where === "object" && !Array.isArray(params.onConflict?.where) && params.onConflict?.where?.params) {
|
|
231
|
-
|
|
350
|
+
if (Array.isArray(params.onConflict.where?.params)) {
|
|
351
|
+
index += (params.onConflict.where?.params).length;
|
|
352
|
+
} else {
|
|
353
|
+
index += 1;
|
|
354
|
+
}
|
|
232
355
|
}
|
|
233
356
|
if (params.onConflict.data) {
|
|
234
357
|
index += this._parse_arguments(params.onConflict.data).length;
|
|
@@ -251,7 +374,7 @@ var QueryBuilder = class {
|
|
|
251
374
|
return `INSERT ${orConflict} INTO ${params.tableName} (${columns}) VALUES ${rows.join(", ")}` + onConflict + this._returning(params.returning);
|
|
252
375
|
}
|
|
253
376
|
_update(params) {
|
|
254
|
-
const whereParamsLength = typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Object.keys(params.where?.params).length : 0;
|
|
377
|
+
const whereParamsLength = typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? Object.keys(params.where?.params).length : 1 : 0;
|
|
255
378
|
const set = [];
|
|
256
379
|
let index = 1;
|
|
257
380
|
for (const [key, value] of Object.entries(params.data)) {
|
|
@@ -311,19 +434,32 @@ var QueryBuilder = class {
|
|
|
311
434
|
}
|
|
312
435
|
_having(value) {
|
|
313
436
|
if (!value) return "";
|
|
314
|
-
return ` HAVING ${value}`;
|
|
437
|
+
if (typeof value === "string") return ` HAVING ${value}`;
|
|
438
|
+
return ` HAVING ${value.join(" AND ")}`;
|
|
315
439
|
}
|
|
316
440
|
_orderBy(value) {
|
|
317
441
|
if (!value) return "";
|
|
318
442
|
if (typeof value === "string") return ` ORDER BY ${value}`;
|
|
443
|
+
const order = [];
|
|
319
444
|
if (Array.isArray(value)) {
|
|
320
|
-
|
|
445
|
+
for (const val of value) {
|
|
446
|
+
order.push(val);
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
order.push(value);
|
|
321
450
|
}
|
|
322
|
-
const
|
|
323
|
-
|
|
324
|
-
|
|
451
|
+
const result = order.map((obj) => {
|
|
452
|
+
if (typeof obj === "object") {
|
|
453
|
+
const objs = [];
|
|
454
|
+
Object.entries(obj).forEach(([key, item]) => {
|
|
455
|
+
objs.push(`${key} ${item}`);
|
|
456
|
+
});
|
|
457
|
+
return objs.join(", ");
|
|
458
|
+
} else {
|
|
459
|
+
return obj;
|
|
460
|
+
}
|
|
325
461
|
});
|
|
326
|
-
return ` ORDER BY ${
|
|
462
|
+
return ` ORDER BY ${result.join(", ")}`;
|
|
327
463
|
}
|
|
328
464
|
_limit(value) {
|
|
329
465
|
if (!value) return "";
|
package/dist/index.mjs
CHANGED
|
@@ -48,7 +48,112 @@ var Query = class {
|
|
|
48
48
|
}
|
|
49
49
|
};
|
|
50
50
|
|
|
51
|
-
// src/
|
|
51
|
+
// src/modularBuilder.ts
|
|
52
|
+
var SelectBuilder = class _SelectBuilder {
|
|
53
|
+
_debugger = false;
|
|
54
|
+
_options = {};
|
|
55
|
+
_queryBuilder;
|
|
56
|
+
constructor(options, queryBuilder) {
|
|
57
|
+
this._options = options;
|
|
58
|
+
this._queryBuilder = queryBuilder;
|
|
59
|
+
}
|
|
60
|
+
setDebugger(state) {
|
|
61
|
+
this._debugger = state;
|
|
62
|
+
}
|
|
63
|
+
tableName(tableName) {
|
|
64
|
+
return new _SelectBuilder(
|
|
65
|
+
{
|
|
66
|
+
...this._options,
|
|
67
|
+
tableName
|
|
68
|
+
},
|
|
69
|
+
this._queryBuilder
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
fields(fields) {
|
|
73
|
+
return this._parseArray("fields", this._options.fields, fields);
|
|
74
|
+
}
|
|
75
|
+
where(conditions, params) {
|
|
76
|
+
if (!Array.isArray(conditions)) {
|
|
77
|
+
conditions = [conditions];
|
|
78
|
+
}
|
|
79
|
+
if (params === void 0) params = [];
|
|
80
|
+
if (!Array.isArray(params)) {
|
|
81
|
+
params = [params];
|
|
82
|
+
}
|
|
83
|
+
if (this._options.where?.conditions) {
|
|
84
|
+
conditions = this._options.where.conditions.concat(conditions);
|
|
85
|
+
}
|
|
86
|
+
if (this._options.where?.params) {
|
|
87
|
+
params = this._options.where.params.concat(params);
|
|
88
|
+
}
|
|
89
|
+
return new _SelectBuilder(
|
|
90
|
+
{
|
|
91
|
+
...this._options,
|
|
92
|
+
where: {
|
|
93
|
+
conditions,
|
|
94
|
+
params
|
|
95
|
+
}
|
|
96
|
+
},
|
|
97
|
+
this._queryBuilder
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
join(join) {
|
|
101
|
+
return this._parseArray("join", this._options.join, join);
|
|
102
|
+
}
|
|
103
|
+
groupBy(groupBy) {
|
|
104
|
+
return this._parseArray("groupBy", this._options.groupBy, groupBy);
|
|
105
|
+
}
|
|
106
|
+
having(having) {
|
|
107
|
+
return this._parseArray("having", this._options.having, having);
|
|
108
|
+
}
|
|
109
|
+
orderBy(orderBy) {
|
|
110
|
+
return this._parseArray("orderBy", this._options.orderBy, orderBy);
|
|
111
|
+
}
|
|
112
|
+
offset(offset) {
|
|
113
|
+
return new _SelectBuilder(
|
|
114
|
+
{
|
|
115
|
+
...this._options,
|
|
116
|
+
offset
|
|
117
|
+
},
|
|
118
|
+
this._queryBuilder
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
limit(limit) {
|
|
122
|
+
return new _SelectBuilder(
|
|
123
|
+
{
|
|
124
|
+
...this._options,
|
|
125
|
+
limit
|
|
126
|
+
},
|
|
127
|
+
this._queryBuilder
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
_parseArray(fieldName, option, value) {
|
|
131
|
+
let val = [];
|
|
132
|
+
if (!Array.isArray(value)) {
|
|
133
|
+
val.push(value);
|
|
134
|
+
} else {
|
|
135
|
+
val = value;
|
|
136
|
+
}
|
|
137
|
+
if (option && Array.isArray(option)) {
|
|
138
|
+
val = [...option, ...val];
|
|
139
|
+
}
|
|
140
|
+
return new _SelectBuilder(
|
|
141
|
+
{
|
|
142
|
+
...this._options,
|
|
143
|
+
[fieldName]: val
|
|
144
|
+
},
|
|
145
|
+
this._queryBuilder
|
|
146
|
+
);
|
|
147
|
+
}
|
|
148
|
+
getQuery() {
|
|
149
|
+
return this._queryBuilder(this._options);
|
|
150
|
+
}
|
|
151
|
+
async execute() {
|
|
152
|
+
return this._queryBuilder(this._options).execute();
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
// src/builder.ts
|
|
52
157
|
var QueryBuilder = class {
|
|
53
158
|
_debugger = false;
|
|
54
159
|
setDebugger(state) {
|
|
@@ -74,13 +179,23 @@ var QueryBuilder = class {
|
|
|
74
179
|
return this.execute(q);
|
|
75
180
|
}, `DROP TABLE ${params.ifExists ? "IF EXISTS" : ""} ${params.tableName}`);
|
|
76
181
|
}
|
|
182
|
+
select(tableName) {
|
|
183
|
+
return new SelectBuilder(
|
|
184
|
+
{
|
|
185
|
+
tableName
|
|
186
|
+
},
|
|
187
|
+
(params) => {
|
|
188
|
+
return this.fetchAll(params);
|
|
189
|
+
}
|
|
190
|
+
);
|
|
191
|
+
}
|
|
77
192
|
fetchOne(params) {
|
|
78
193
|
return new Query(
|
|
79
194
|
(q) => {
|
|
80
195
|
return this.execute(q);
|
|
81
196
|
},
|
|
82
197
|
this._select({ ...params, limit: 1 }),
|
|
83
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
198
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
84
199
|
"ONE" /* ONE */
|
|
85
200
|
);
|
|
86
201
|
}
|
|
@@ -90,7 +205,7 @@ var QueryBuilder = class {
|
|
|
90
205
|
return this.execute(q);
|
|
91
206
|
},
|
|
92
207
|
this._select(params),
|
|
93
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
208
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
94
209
|
"ALL" /* ALL */
|
|
95
210
|
);
|
|
96
211
|
}
|
|
@@ -134,7 +249,11 @@ var QueryBuilder = class {
|
|
|
134
249
|
update(params) {
|
|
135
250
|
let args = this._parse_arguments(params.data);
|
|
136
251
|
if (typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params) {
|
|
137
|
-
|
|
252
|
+
if (Array.isArray(params.where?.params)) {
|
|
253
|
+
args = params.where?.params.concat(args);
|
|
254
|
+
} else {
|
|
255
|
+
args = [params.where?.params].concat(args);
|
|
256
|
+
}
|
|
138
257
|
}
|
|
139
258
|
return new Query(
|
|
140
259
|
(q) => {
|
|
@@ -151,7 +270,7 @@ var QueryBuilder = class {
|
|
|
151
270
|
return this.execute(q);
|
|
152
271
|
},
|
|
153
272
|
this._delete(params),
|
|
154
|
-
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? params.where?.params : void 0,
|
|
273
|
+
typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? params.where?.params : [params.where?.params] : void 0,
|
|
155
274
|
"ALL" /* ALL */
|
|
156
275
|
);
|
|
157
276
|
}
|
|
@@ -194,7 +313,11 @@ var QueryBuilder = class {
|
|
|
194
313
|
if (params.onConflict && typeof params.onConflict === "object") {
|
|
195
314
|
onConflict = this._onConflict(params.onConflict);
|
|
196
315
|
if (typeof params.onConflict?.where === "object" && !Array.isArray(params.onConflict?.where) && params.onConflict?.where?.params) {
|
|
197
|
-
|
|
316
|
+
if (Array.isArray(params.onConflict.where?.params)) {
|
|
317
|
+
index += (params.onConflict.where?.params).length;
|
|
318
|
+
} else {
|
|
319
|
+
index += 1;
|
|
320
|
+
}
|
|
198
321
|
}
|
|
199
322
|
if (params.onConflict.data) {
|
|
200
323
|
index += this._parse_arguments(params.onConflict.data).length;
|
|
@@ -217,7 +340,7 @@ var QueryBuilder = class {
|
|
|
217
340
|
return `INSERT ${orConflict} INTO ${params.tableName} (${columns}) VALUES ${rows.join(", ")}` + onConflict + this._returning(params.returning);
|
|
218
341
|
}
|
|
219
342
|
_update(params) {
|
|
220
|
-
const whereParamsLength = typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Object.keys(params.where?.params).length : 0;
|
|
343
|
+
const whereParamsLength = typeof params.where === "object" && !Array.isArray(params.where) && params.where?.params ? Array.isArray(params.where?.params) ? Object.keys(params.where?.params).length : 1 : 0;
|
|
221
344
|
const set = [];
|
|
222
345
|
let index = 1;
|
|
223
346
|
for (const [key, value] of Object.entries(params.data)) {
|
|
@@ -277,19 +400,32 @@ var QueryBuilder = class {
|
|
|
277
400
|
}
|
|
278
401
|
_having(value) {
|
|
279
402
|
if (!value) return "";
|
|
280
|
-
return ` HAVING ${value}`;
|
|
403
|
+
if (typeof value === "string") return ` HAVING ${value}`;
|
|
404
|
+
return ` HAVING ${value.join(" AND ")}`;
|
|
281
405
|
}
|
|
282
406
|
_orderBy(value) {
|
|
283
407
|
if (!value) return "";
|
|
284
408
|
if (typeof value === "string") return ` ORDER BY ${value}`;
|
|
409
|
+
const order = [];
|
|
285
410
|
if (Array.isArray(value)) {
|
|
286
|
-
|
|
411
|
+
for (const val of value) {
|
|
412
|
+
order.push(val);
|
|
413
|
+
}
|
|
414
|
+
} else {
|
|
415
|
+
order.push(value);
|
|
287
416
|
}
|
|
288
|
-
const
|
|
289
|
-
|
|
290
|
-
|
|
417
|
+
const result = order.map((obj) => {
|
|
418
|
+
if (typeof obj === "object") {
|
|
419
|
+
const objs = [];
|
|
420
|
+
Object.entries(obj).forEach(([key, item]) => {
|
|
421
|
+
objs.push(`${key} ${item}`);
|
|
422
|
+
});
|
|
423
|
+
return objs.join(", ");
|
|
424
|
+
} else {
|
|
425
|
+
return obj;
|
|
426
|
+
}
|
|
291
427
|
});
|
|
292
|
-
return ` ORDER BY ${
|
|
428
|
+
return ` ORDER BY ${result.join(", ")}`;
|
|
293
429
|
}
|
|
294
430
|
_limit(value) {
|
|
295
431
|
if (!value) return "";
|