workers-qb 0.1.1 → 0.1.3
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 +129 -93
- package/dist/cjs/Builder.js +36 -17
- package/dist/cjs/Databases.js +0 -1
- package/dist/cjs/enums.js +15 -1
- package/dist/esm/Builder.js +37 -18
- package/dist/esm/Databases.js +2 -3
- package/dist/esm/enums.js +14 -0
- package/dist/types/Builder.d.ts +11 -9
- package/dist/types/Databases.d.ts +2 -2
- package/dist/types/enums.d.ts +12 -0
- package/dist/types/interfaces.d.ts +25 -1
- package/dist/umd/index.js +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -1,17 +1,26 @@
|
|
|
1
1
|
# workers-qb
|
|
2
2
|
|
|
3
|
-
Zero dependencies
|
|
3
|
+
Zero dependencies Query Builder for [Cloudflare D1](https://blog.cloudflare.com/introducing-d1/)
|
|
4
4
|
[Workers](https://developers.cloudflare.com/workers/)
|
|
5
5
|
|
|
6
|
+
This module provides a simple standardized interface while keeping the
|
|
7
|
+
benefits and speed of using raw queries over a traditional ORM.
|
|
8
|
+
|
|
9
|
+
`workers-qb` is not intended to provide ORM-like functionality, rather to make it easier to interact with the database from
|
|
10
|
+
code for direct SQL access using convenient wrapper methods.
|
|
11
|
+
|
|
12
|
+
Read the documentation [Here](https://workers-qb.massadas.com/)!
|
|
13
|
+
|
|
6
14
|
## Features
|
|
7
|
-
|
|
8
|
-
- [x]
|
|
15
|
+
|
|
16
|
+
- [x] Zero dependencies.
|
|
17
|
+
- [x] Fully typed/TypeScript support
|
|
9
18
|
- [x] SQL Type checking with compatible IDE's
|
|
10
|
-
- [x] Insert/
|
|
19
|
+
- [x] Insert/Update/Select/Delete queries
|
|
11
20
|
- [x] Create/drop tables
|
|
12
|
-
- [x]
|
|
13
|
-
- [ ] Bulk insert/
|
|
14
|
-
- [ ] Named parameters (waiting
|
|
21
|
+
- [x] Keep where conditions simple in code
|
|
22
|
+
- [ ] Bulk insert/update
|
|
23
|
+
- [ ] Named parameters (waiting for full support in D1)
|
|
15
24
|
|
|
16
25
|
## Installation
|
|
17
26
|
|
|
@@ -19,106 +28,133 @@ Zero dependencies SQL Builder for [Cloudflare D1](https://blog.cloudflare.com/in
|
|
|
19
28
|
npm install workers-qb
|
|
20
29
|
```
|
|
21
30
|
|
|
22
|
-
##
|
|
23
|
-
|
|
31
|
+
## Basic Usage
|
|
32
|
+
|
|
24
33
|
```ts
|
|
25
34
|
import { D1QB } from 'workers-qb'
|
|
26
35
|
const qb = new D1QB(env.DB)
|
|
27
36
|
|
|
28
|
-
const
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
returning: "*",
|
|
37
|
+
const fetched = await qb.fetchOne({
|
|
38
|
+
tableName: 'employees',
|
|
39
|
+
fields: 'count(*) as count',
|
|
40
|
+
where: {
|
|
41
|
+
conditions: 'active = ?1',
|
|
42
|
+
params: [true],
|
|
43
|
+
},
|
|
36
44
|
})
|
|
37
|
-
console.log(inserted) // This will contain the data after SQL triggers and primary keys that are automated
|
|
38
45
|
|
|
46
|
+
console.log(`Company has ${fetched.results.count} active employees`)
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### Fetching a single record
|
|
39
50
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
51
|
+
```ts
|
|
52
|
+
const qb = new D1QB(env.DB)
|
|
53
|
+
|
|
54
|
+
const fetched = await qb.fetchOne({
|
|
55
|
+
tableName: 'employees',
|
|
56
|
+
fields: 'count(*) as count',
|
|
57
|
+
where: {
|
|
58
|
+
conditions: 'department = ?1',
|
|
59
|
+
params: ['HQ'],
|
|
60
|
+
},
|
|
47
61
|
})
|
|
48
62
|
|
|
63
|
+
console.log(`There are ${fetched.results.count} employees in the HR department`)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
#### Fetching multiple records
|
|
49
67
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
```ts
|
|
69
|
+
import { OrderTypes } from 'workers-qb'
|
|
70
|
+
const qb = new D1QB(env.DB)
|
|
71
|
+
|
|
72
|
+
const fetched = await qb.fetchAll({
|
|
73
|
+
tableName: 'employees',
|
|
74
|
+
fields: ['role', 'count(*) as count'],
|
|
75
|
+
where: {
|
|
76
|
+
conditions: 'department = ?1',
|
|
77
|
+
params: ['HR'],
|
|
78
|
+
},
|
|
79
|
+
groupBy: 'role',
|
|
80
|
+
orderBy: {
|
|
81
|
+
count: OrderTypes.DESC,
|
|
82
|
+
},
|
|
60
83
|
})
|
|
61
84
|
|
|
85
|
+
console.log(`Roles in the HR department:`)
|
|
62
86
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
where: {
|
|
66
|
-
conditions: "id = ?1",
|
|
67
|
-
params: [inserted.results[0].id]
|
|
68
|
-
},
|
|
87
|
+
fetched.results.forEach((employee) => {
|
|
88
|
+
console.log(`${employee.role} has ${employee.count} employees`)
|
|
69
89
|
})
|
|
70
90
|
```
|
|
71
91
|
|
|
72
|
-
####
|
|
92
|
+
#### Inserting rows
|
|
93
|
+
|
|
73
94
|
```ts
|
|
74
|
-
import { D1QB, OrderTypes } from 'workers-qb'
|
|
75
95
|
const qb = new D1QB(env.DB)
|
|
76
96
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
],
|
|
86
|
-
params: ["HQ"]
|
|
87
|
-
},
|
|
88
|
-
orderBy: {
|
|
89
|
-
"timestamp": OrderTypes.DESC,
|
|
90
|
-
},
|
|
97
|
+
const inserted = await qb.insert({
|
|
98
|
+
tableName: 'employees',
|
|
99
|
+
data: {
|
|
100
|
+
name: 'Joe',
|
|
101
|
+
role: 'manager',
|
|
102
|
+
department: 'store',
|
|
103
|
+
},
|
|
104
|
+
returning: '*',
|
|
91
105
|
})
|
|
106
|
+
|
|
107
|
+
console.log(inserted) // This will contain the data after SQL triggers and primary keys that are automated
|
|
92
108
|
```
|
|
93
109
|
|
|
94
|
-
####
|
|
110
|
+
#### Updating rows
|
|
111
|
+
|
|
95
112
|
```ts
|
|
96
|
-
|
|
97
|
-
|
|
113
|
+
const updated = await qb.update({
|
|
114
|
+
tableName: 'employees',
|
|
115
|
+
data: {
|
|
116
|
+
role: 'CEO',
|
|
117
|
+
department: 'HQ',
|
|
118
|
+
},
|
|
119
|
+
where: {
|
|
120
|
+
conditions: 'id = ?1',
|
|
121
|
+
params: [123],
|
|
122
|
+
},
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
console.log(`Lines affected in this query: ${updated.changes}`)
|
|
126
|
+
```
|
|
98
127
|
|
|
128
|
+
#### Deleting rows
|
|
99
129
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
params: [department]
|
|
111
|
-
},
|
|
112
|
-
groupBy: "role",
|
|
113
|
-
orderBy: {
|
|
114
|
-
"count": OrderTypes.DESC,
|
|
115
|
-
},
|
|
116
|
-
})
|
|
117
|
-
|
|
118
|
-
return result.results
|
|
119
|
-
}
|
|
130
|
+
```ts
|
|
131
|
+
const deleted = await qb.delete({
|
|
132
|
+
tableName: 'employees',
|
|
133
|
+
where: {
|
|
134
|
+
conditions: 'id = ?1',
|
|
135
|
+
params: [123],
|
|
136
|
+
},
|
|
137
|
+
})
|
|
138
|
+
|
|
139
|
+
console.log(`Lines affected in this query: ${deleted.changes}`)
|
|
120
140
|
```
|
|
121
141
|
|
|
142
|
+
#### Dropping and creating tables
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
const created = await qb.createTable({
|
|
146
|
+
tableName: 'testTable',
|
|
147
|
+
schema: `
|
|
148
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
149
|
+
name TEXT NOT NULL
|
|
150
|
+
`,
|
|
151
|
+
ifNotExists: true,
|
|
152
|
+
})
|
|
153
|
+
|
|
154
|
+
const dropped = await qb.dropTable({
|
|
155
|
+
tableName: 'testTable',
|
|
156
|
+
})
|
|
157
|
+
```
|
|
122
158
|
|
|
123
159
|
## Development
|
|
124
160
|
|
|
@@ -188,31 +224,31 @@ You can then create a, for example, **testsql.ts** file with the content:
|
|
|
188
224
|
import { D1QB } from 'workers-qb'
|
|
189
225
|
const qb = new D1QB(env.DB)
|
|
190
226
|
|
|
191
|
-
console.log(
|
|
227
|
+
console.log('Creating table...')
|
|
192
228
|
const created = await qb.createTable({
|
|
193
|
-
|
|
194
|
-
|
|
229
|
+
tableName: 'testTable',
|
|
230
|
+
schema: `
|
|
195
231
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
196
232
|
name TEXT NOT NULL
|
|
197
233
|
`,
|
|
198
|
-
|
|
234
|
+
ifNotExists: true,
|
|
199
235
|
})
|
|
200
236
|
console.log(created)
|
|
201
237
|
|
|
202
|
-
console.log(
|
|
238
|
+
console.log('Inserting rows...')
|
|
203
239
|
const inserted = await qb.insert({
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
240
|
+
tableName: 'testTable',
|
|
241
|
+
data: {
|
|
242
|
+
name: 'my name',
|
|
243
|
+
},
|
|
244
|
+
returning: '*',
|
|
209
245
|
})
|
|
210
246
|
console.log(inserted)
|
|
211
247
|
|
|
212
|
-
console.log(
|
|
248
|
+
console.log('Selecting rows...')
|
|
213
249
|
const selected = await qb.fetchAll({
|
|
214
|
-
|
|
215
|
-
|
|
250
|
+
tableName: 'testTable',
|
|
251
|
+
fields: '*',
|
|
216
252
|
})
|
|
217
253
|
console.log(selected)
|
|
218
254
|
```
|
package/dist/cjs/Builder.js
CHANGED
|
@@ -55,7 +55,7 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
55
55
|
QueryBuilder.prototype.execute = function (params) {
|
|
56
56
|
return __awaiter(this, void 0, void 0, function () {
|
|
57
57
|
return __generator(this, function (_a) {
|
|
58
|
-
throw new Error(
|
|
58
|
+
throw new Error('Execute method not implemented');
|
|
59
59
|
});
|
|
60
60
|
});
|
|
61
61
|
};
|
|
@@ -63,7 +63,7 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
63
63
|
return __awaiter(this, void 0, void 0, function () {
|
|
64
64
|
return __generator(this, function (_a) {
|
|
65
65
|
return [2 /*return*/, this.execute({
|
|
66
|
-
query: "CREATE TABLE " + (
|
|
66
|
+
query: "CREATE TABLE " + (params.ifNotExists ? 'IF NOT EXISTS' : '') + " " + params.tableName + " (" + params.schema + ")",
|
|
67
67
|
})];
|
|
68
68
|
});
|
|
69
69
|
});
|
|
@@ -72,19 +72,25 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
72
72
|
return __awaiter(this, void 0, void 0, function () {
|
|
73
73
|
return __generator(this, function (_a) {
|
|
74
74
|
return [2 /*return*/, this.execute({
|
|
75
|
-
query: "DROP TABLE " + (
|
|
75
|
+
query: "DROP TABLE " + (params.ifExists ? 'IF EXISTS' : '') + " " + params.tableName,
|
|
76
76
|
})];
|
|
77
77
|
});
|
|
78
78
|
});
|
|
79
79
|
};
|
|
80
80
|
QueryBuilder.prototype.fetchOne = function (params) {
|
|
81
81
|
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
+
var data;
|
|
82
83
|
return __generator(this, function (_a) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0: return [4 /*yield*/, this.execute({
|
|
86
|
+
query: this._select(__assign(__assign({}, params), { limit: 1 })),
|
|
87
|
+
arguments: params.where ? params.where.params : undefined,
|
|
88
|
+
fetchType: enums_1.FetchTypes.ALL,
|
|
89
|
+
})];
|
|
90
|
+
case 1:
|
|
91
|
+
data = _a.sent();
|
|
92
|
+
return [2 /*return*/, __assign(__assign({}, data), { results: data.results[0] })];
|
|
93
|
+
}
|
|
88
94
|
});
|
|
89
95
|
});
|
|
90
96
|
};
|
|
@@ -115,7 +121,9 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
115
121
|
return __generator(this, function (_a) {
|
|
116
122
|
return [2 /*return*/, this.execute({
|
|
117
123
|
query: this._update(params),
|
|
118
|
-
arguments: params.where && params.where.params
|
|
124
|
+
arguments: params.where && params.where.params
|
|
125
|
+
? params.where.params.concat(Object.values(params.data))
|
|
126
|
+
: Object.values(params.data),
|
|
119
127
|
fetchType: enums_1.FetchTypes.ALL,
|
|
120
128
|
})];
|
|
121
129
|
});
|
|
@@ -132,14 +140,19 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
132
140
|
});
|
|
133
141
|
});
|
|
134
142
|
};
|
|
143
|
+
QueryBuilder.prototype._onConflict = function (resolution) {
|
|
144
|
+
if (resolution) {
|
|
145
|
+
return "OR " + resolution + " ";
|
|
146
|
+
}
|
|
147
|
+
return '';
|
|
148
|
+
};
|
|
135
149
|
QueryBuilder.prototype._insert = function (params) {
|
|
136
|
-
var columns = Object.keys(params.data).join(
|
|
150
|
+
var columns = Object.keys(params.data).join(', ');
|
|
137
151
|
var values = [];
|
|
138
152
|
Object.keys(params.data).forEach(function (key, index) {
|
|
139
153
|
values.push("?" + (index + 1));
|
|
140
154
|
});
|
|
141
|
-
return ("INSERT INTO " + params.tableName + " (" + columns + ") VALUES(" + values.join(
|
|
142
|
-
this._returning(params.returning));
|
|
155
|
+
return ("INSERT " + this._onConflict(params.onConflict) + "INTO " + params.tableName + " (" + columns + ") VALUES(" + values.join(', ') + ")" + this._returning(params.returning));
|
|
143
156
|
};
|
|
144
157
|
QueryBuilder.prototype._update = function (params) {
|
|
145
158
|
var _a;
|
|
@@ -149,19 +162,18 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
149
162
|
var key = _a[0], value = _a[1];
|
|
150
163
|
set.push(key + " = ?" + (whereParamsLength + index + 1));
|
|
151
164
|
});
|
|
152
|
-
return ("UPDATE " + params.tableName + " SET
|
|
165
|
+
return ("UPDATE " + this._onConflict(params.onConflict) + params.tableName + " SET " + set.join(', ') +
|
|
153
166
|
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
154
167
|
this._returning(params.returning));
|
|
155
168
|
};
|
|
156
169
|
QueryBuilder.prototype._delete = function (params) {
|
|
157
170
|
var _a;
|
|
158
|
-
return
|
|
159
|
-
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
160
|
-
this._returning(params.returning));
|
|
171
|
+
return "DELETE FROM " + params.tableName + this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) + this._returning(params.returning);
|
|
161
172
|
};
|
|
162
173
|
QueryBuilder.prototype._select = function (params) {
|
|
163
174
|
var _a;
|
|
164
175
|
return ("SELECT " + this._fields(params.fields) + " FROM " + params.tableName +
|
|
176
|
+
this._join(params.join) +
|
|
165
177
|
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
166
178
|
this._groupBy(params.groupBy) +
|
|
167
179
|
this._having(params.having) +
|
|
@@ -181,6 +193,12 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
181
193
|
return " WHERE " + value;
|
|
182
194
|
return " WHERE " + value.join(' AND ');
|
|
183
195
|
};
|
|
196
|
+
QueryBuilder.prototype._join = function (value) {
|
|
197
|
+
if (!value)
|
|
198
|
+
return '';
|
|
199
|
+
var type = value.type ? " " + value.type : '';
|
|
200
|
+
return type + " JOIN " + value.table + " ON " + value.on;
|
|
201
|
+
};
|
|
184
202
|
QueryBuilder.prototype._groupBy = function (value) {
|
|
185
203
|
if (!value)
|
|
186
204
|
return '';
|
|
@@ -198,7 +216,8 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
198
216
|
return '';
|
|
199
217
|
if (typeof value === 'string')
|
|
200
218
|
return " ORDER BY " + value;
|
|
201
|
-
if (value.constructor.name.toLowerCase() === 'array') {
|
|
219
|
+
if (value.constructor.name.toLowerCase() === 'array') {
|
|
220
|
+
// @ts-ignore
|
|
202
221
|
return " ORDER BY " + value.join(', ');
|
|
203
222
|
}
|
|
204
223
|
var order = [];
|
package/dist/cjs/Databases.js
CHANGED
package/dist/cjs/enums.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FetchTypes = exports.OrderTypes = void 0;
|
|
3
|
+
exports.JoinTypes = exports.ConflictTypes = exports.FetchTypes = exports.OrderTypes = void 0;
|
|
4
4
|
var OrderTypes;
|
|
5
5
|
(function (OrderTypes) {
|
|
6
6
|
OrderTypes["ASC"] = "ASC";
|
|
@@ -11,3 +11,17 @@ var FetchTypes;
|
|
|
11
11
|
FetchTypes["ONE"] = "ONE";
|
|
12
12
|
FetchTypes["ALL"] = "ALL";
|
|
13
13
|
})(FetchTypes = exports.FetchTypes || (exports.FetchTypes = {}));
|
|
14
|
+
var ConflictTypes;
|
|
15
|
+
(function (ConflictTypes) {
|
|
16
|
+
ConflictTypes["ROLLBACK"] = "ROLLBACK";
|
|
17
|
+
ConflictTypes["ABORT"] = "ABORT";
|
|
18
|
+
ConflictTypes["FAIL"] = "FAIL";
|
|
19
|
+
ConflictTypes["IGNORE"] = "IGNORE";
|
|
20
|
+
ConflictTypes["REPLACE"] = "REPLACE";
|
|
21
|
+
})(ConflictTypes = exports.ConflictTypes || (exports.ConflictTypes = {}));
|
|
22
|
+
var JoinTypes;
|
|
23
|
+
(function (JoinTypes) {
|
|
24
|
+
JoinTypes["INNER"] = "INNER";
|
|
25
|
+
JoinTypes["LEFT"] = "LEFT";
|
|
26
|
+
JoinTypes["CROSS"] = "CROSS";
|
|
27
|
+
})(JoinTypes = exports.JoinTypes || (exports.JoinTypes = {}));
|
package/dist/esm/Builder.js
CHANGED
|
@@ -45,14 +45,14 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
45
45
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
46
46
|
}
|
|
47
47
|
};
|
|
48
|
-
import { FetchTypes } from
|
|
48
|
+
import { FetchTypes } from './enums';
|
|
49
49
|
var QueryBuilder = /** @class */ (function () {
|
|
50
50
|
function QueryBuilder() {
|
|
51
51
|
}
|
|
52
52
|
QueryBuilder.prototype.execute = function (params) {
|
|
53
53
|
return __awaiter(this, void 0, void 0, function () {
|
|
54
54
|
return __generator(this, function (_a) {
|
|
55
|
-
throw new Error(
|
|
55
|
+
throw new Error('Execute method not implemented');
|
|
56
56
|
});
|
|
57
57
|
});
|
|
58
58
|
};
|
|
@@ -60,7 +60,7 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
60
60
|
return __awaiter(this, void 0, void 0, function () {
|
|
61
61
|
return __generator(this, function (_a) {
|
|
62
62
|
return [2 /*return*/, this.execute({
|
|
63
|
-
query: "CREATE TABLE " + (
|
|
63
|
+
query: "CREATE TABLE " + (params.ifNotExists ? 'IF NOT EXISTS' : '') + " " + params.tableName + " (" + params.schema + ")",
|
|
64
64
|
})];
|
|
65
65
|
});
|
|
66
66
|
});
|
|
@@ -69,19 +69,25 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
69
69
|
return __awaiter(this, void 0, void 0, function () {
|
|
70
70
|
return __generator(this, function (_a) {
|
|
71
71
|
return [2 /*return*/, this.execute({
|
|
72
|
-
query: "DROP TABLE " + (
|
|
72
|
+
query: "DROP TABLE " + (params.ifExists ? 'IF EXISTS' : '') + " " + params.tableName,
|
|
73
73
|
})];
|
|
74
74
|
});
|
|
75
75
|
});
|
|
76
76
|
};
|
|
77
77
|
QueryBuilder.prototype.fetchOne = function (params) {
|
|
78
78
|
return __awaiter(this, void 0, void 0, function () {
|
|
79
|
+
var data;
|
|
79
80
|
return __generator(this, function (_a) {
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
switch (_a.label) {
|
|
82
|
+
case 0: return [4 /*yield*/, this.execute({
|
|
83
|
+
query: this._select(__assign(__assign({}, params), { limit: 1 })),
|
|
84
|
+
arguments: params.where ? params.where.params : undefined,
|
|
85
|
+
fetchType: FetchTypes.ALL,
|
|
86
|
+
})];
|
|
87
|
+
case 1:
|
|
88
|
+
data = _a.sent();
|
|
89
|
+
return [2 /*return*/, __assign(__assign({}, data), { results: data.results[0] })];
|
|
90
|
+
}
|
|
85
91
|
});
|
|
86
92
|
});
|
|
87
93
|
};
|
|
@@ -112,7 +118,9 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
112
118
|
return __generator(this, function (_a) {
|
|
113
119
|
return [2 /*return*/, this.execute({
|
|
114
120
|
query: this._update(params),
|
|
115
|
-
arguments: params.where && params.where.params
|
|
121
|
+
arguments: params.where && params.where.params
|
|
122
|
+
? params.where.params.concat(Object.values(params.data))
|
|
123
|
+
: Object.values(params.data),
|
|
116
124
|
fetchType: FetchTypes.ALL,
|
|
117
125
|
})];
|
|
118
126
|
});
|
|
@@ -129,14 +137,19 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
129
137
|
});
|
|
130
138
|
});
|
|
131
139
|
};
|
|
140
|
+
QueryBuilder.prototype._onConflict = function (resolution) {
|
|
141
|
+
if (resolution) {
|
|
142
|
+
return "OR " + resolution + " ";
|
|
143
|
+
}
|
|
144
|
+
return '';
|
|
145
|
+
};
|
|
132
146
|
QueryBuilder.prototype._insert = function (params) {
|
|
133
|
-
var columns = Object.keys(params.data).join(
|
|
147
|
+
var columns = Object.keys(params.data).join(', ');
|
|
134
148
|
var values = [];
|
|
135
149
|
Object.keys(params.data).forEach(function (key, index) {
|
|
136
150
|
values.push("?" + (index + 1));
|
|
137
151
|
});
|
|
138
|
-
return ("INSERT INTO " + params.tableName + " (" + columns + ") VALUES(" + values.join(
|
|
139
|
-
this._returning(params.returning));
|
|
152
|
+
return ("INSERT " + this._onConflict(params.onConflict) + "INTO " + params.tableName + " (" + columns + ") VALUES(" + values.join(', ') + ")" + this._returning(params.returning));
|
|
140
153
|
};
|
|
141
154
|
QueryBuilder.prototype._update = function (params) {
|
|
142
155
|
var _a;
|
|
@@ -146,19 +159,18 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
146
159
|
var key = _a[0], value = _a[1];
|
|
147
160
|
set.push(key + " = ?" + (whereParamsLength + index + 1));
|
|
148
161
|
});
|
|
149
|
-
return ("UPDATE " + params.tableName + " SET
|
|
162
|
+
return ("UPDATE " + this._onConflict(params.onConflict) + params.tableName + " SET " + set.join(', ') +
|
|
150
163
|
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
151
164
|
this._returning(params.returning));
|
|
152
165
|
};
|
|
153
166
|
QueryBuilder.prototype._delete = function (params) {
|
|
154
167
|
var _a;
|
|
155
|
-
return
|
|
156
|
-
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
157
|
-
this._returning(params.returning));
|
|
168
|
+
return "DELETE FROM " + params.tableName + this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) + this._returning(params.returning);
|
|
158
169
|
};
|
|
159
170
|
QueryBuilder.prototype._select = function (params) {
|
|
160
171
|
var _a;
|
|
161
172
|
return ("SELECT " + this._fields(params.fields) + " FROM " + params.tableName +
|
|
173
|
+
this._join(params.join) +
|
|
162
174
|
this._where((_a = params.where) === null || _a === void 0 ? void 0 : _a.conditions) +
|
|
163
175
|
this._groupBy(params.groupBy) +
|
|
164
176
|
this._having(params.having) +
|
|
@@ -178,6 +190,12 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
178
190
|
return " WHERE " + value;
|
|
179
191
|
return " WHERE " + value.join(' AND ');
|
|
180
192
|
};
|
|
193
|
+
QueryBuilder.prototype._join = function (value) {
|
|
194
|
+
if (!value)
|
|
195
|
+
return '';
|
|
196
|
+
var type = value.type ? " " + value.type : '';
|
|
197
|
+
return type + " JOIN " + value.table + " ON " + value.on;
|
|
198
|
+
};
|
|
181
199
|
QueryBuilder.prototype._groupBy = function (value) {
|
|
182
200
|
if (!value)
|
|
183
201
|
return '';
|
|
@@ -195,7 +213,8 @@ var QueryBuilder = /** @class */ (function () {
|
|
|
195
213
|
return '';
|
|
196
214
|
if (typeof value === 'string')
|
|
197
215
|
return " ORDER BY " + value;
|
|
198
|
-
if (value.constructor.name.toLowerCase() === 'array') {
|
|
216
|
+
if (value.constructor.name.toLowerCase() === 'array') {
|
|
217
|
+
// @ts-ignore
|
|
199
218
|
return " ORDER BY " + value.join(', ');
|
|
200
219
|
}
|
|
201
220
|
var order = [];
|
package/dist/esm/Databases.js
CHANGED
|
@@ -49,8 +49,8 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
49
49
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
50
50
|
}
|
|
51
51
|
};
|
|
52
|
-
import { QueryBuilder } from
|
|
53
|
-
import { FetchTypes } from
|
|
52
|
+
import { QueryBuilder } from './Builder';
|
|
53
|
+
import { FetchTypes } from './enums';
|
|
54
54
|
var D1QB = /** @class */ (function (_super) {
|
|
55
55
|
__extends(D1QB, _super);
|
|
56
56
|
function D1QB(db) {
|
|
@@ -66,7 +66,6 @@ var D1QB = /** @class */ (function (_super) {
|
|
|
66
66
|
if (params.arguments) {
|
|
67
67
|
stmt = stmt.bind.apply(stmt, params.arguments);
|
|
68
68
|
}
|
|
69
|
-
console.log(params);
|
|
70
69
|
if (params.fetchType === FetchTypes.ONE) {
|
|
71
70
|
return [2 /*return*/, stmt.first()];
|
|
72
71
|
}
|
package/dist/esm/enums.js
CHANGED
|
@@ -8,3 +8,17 @@ export var FetchTypes;
|
|
|
8
8
|
FetchTypes["ONE"] = "ONE";
|
|
9
9
|
FetchTypes["ALL"] = "ALL";
|
|
10
10
|
})(FetchTypes || (FetchTypes = {}));
|
|
11
|
+
export var ConflictTypes;
|
|
12
|
+
(function (ConflictTypes) {
|
|
13
|
+
ConflictTypes["ROLLBACK"] = "ROLLBACK";
|
|
14
|
+
ConflictTypes["ABORT"] = "ABORT";
|
|
15
|
+
ConflictTypes["FAIL"] = "FAIL";
|
|
16
|
+
ConflictTypes["IGNORE"] = "IGNORE";
|
|
17
|
+
ConflictTypes["REPLACE"] = "REPLACE";
|
|
18
|
+
})(ConflictTypes || (ConflictTypes = {}));
|
|
19
|
+
export var JoinTypes;
|
|
20
|
+
(function (JoinTypes) {
|
|
21
|
+
JoinTypes["INNER"] = "INNER";
|
|
22
|
+
JoinTypes["LEFT"] = "LEFT";
|
|
23
|
+
JoinTypes["CROSS"] = "CROSS";
|
|
24
|
+
})(JoinTypes || (JoinTypes = {}));
|
package/dist/types/Builder.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Delete, Insert, SelectAll, SelectOne, Update } from
|
|
2
|
-
import { FetchTypes, OrderTypes } from
|
|
1
|
+
import { Delete, Insert, Join, Result, ResultOne, SelectAll, SelectOne, Update } from './interfaces';
|
|
2
|
+
import { ConflictTypes, FetchTypes, OrderTypes } from './enums';
|
|
3
3
|
export declare class QueryBuilder {
|
|
4
4
|
execute(params: {
|
|
5
5
|
query: String;
|
|
@@ -10,22 +10,24 @@ export declare class QueryBuilder {
|
|
|
10
10
|
tableName: string;
|
|
11
11
|
schema: string;
|
|
12
12
|
ifNotExists?: boolean;
|
|
13
|
-
}): Promise<
|
|
13
|
+
}): Promise<Result>;
|
|
14
14
|
dropTable(params: {
|
|
15
15
|
tableName: string;
|
|
16
16
|
ifExists?: boolean;
|
|
17
|
-
}): Promise<
|
|
18
|
-
fetchOne(params: SelectOne): Promise<
|
|
19
|
-
fetchAll(params: SelectAll): Promise<
|
|
20
|
-
insert(params: Insert): Promise<
|
|
21
|
-
update(params: Update): Promise<
|
|
22
|
-
delete(params: Delete): Promise<
|
|
17
|
+
}): Promise<Result>;
|
|
18
|
+
fetchOne(params: SelectOne): Promise<ResultOne>;
|
|
19
|
+
fetchAll(params: SelectAll): Promise<Result>;
|
|
20
|
+
insert(params: Insert): Promise<Result>;
|
|
21
|
+
update(params: Update): Promise<Result>;
|
|
22
|
+
delete(params: Delete): Promise<Result>;
|
|
23
|
+
_onConflict(resolution?: string | ConflictTypes): string;
|
|
23
24
|
_insert(params: Insert): string;
|
|
24
25
|
_update(params: Update): string;
|
|
25
26
|
_delete(params: Delete): string;
|
|
26
27
|
_select(params: SelectAll): string;
|
|
27
28
|
_fields(value: string | Array<string>): string;
|
|
28
29
|
_where(value?: string | Array<string>): string;
|
|
30
|
+
_join(value?: Join): string;
|
|
29
31
|
_groupBy(value?: string | Array<string>): string;
|
|
30
32
|
_having(value?: string): string;
|
|
31
33
|
_orderBy(value?: string | Array<string> | Record<string, string | OrderTypes>): string;
|
package/dist/types/enums.d.ts
CHANGED
|
@@ -6,3 +6,15 @@ export declare enum FetchTypes {
|
|
|
6
6
|
ONE = "ONE",
|
|
7
7
|
ALL = "ALL"
|
|
8
8
|
}
|
|
9
|
+
export declare enum ConflictTypes {
|
|
10
|
+
ROLLBACK = "ROLLBACK",
|
|
11
|
+
ABORT = "ABORT",
|
|
12
|
+
FAIL = "FAIL",
|
|
13
|
+
IGNORE = "IGNORE",
|
|
14
|
+
REPLACE = "REPLACE"
|
|
15
|
+
}
|
|
16
|
+
export declare enum JoinTypes {
|
|
17
|
+
INNER = "INNER",
|
|
18
|
+
LEFT = "LEFT",
|
|
19
|
+
CROSS = "CROSS"
|
|
20
|
+
}
|
|
@@ -1,12 +1,18 @@
|
|
|
1
|
-
import { OrderTypes } from
|
|
1
|
+
import { ConflictTypes, JoinTypes, OrderTypes } from './enums';
|
|
2
2
|
export interface Where {
|
|
3
3
|
conditions: string | Array<string>;
|
|
4
4
|
params?: (string | boolean | number | null)[];
|
|
5
5
|
}
|
|
6
|
+
export interface Join {
|
|
7
|
+
type?: string | JoinTypes;
|
|
8
|
+
table: string;
|
|
9
|
+
on: string;
|
|
10
|
+
}
|
|
6
11
|
export interface SelectOne {
|
|
7
12
|
tableName: string;
|
|
8
13
|
fields: string | Array<string>;
|
|
9
14
|
where?: Where;
|
|
15
|
+
join?: Join;
|
|
10
16
|
groupBy?: string | Array<string>;
|
|
11
17
|
having?: string;
|
|
12
18
|
orderBy?: string | Array<string> | Record<string, string | OrderTypes>;
|
|
@@ -19,15 +25,33 @@ export interface Insert {
|
|
|
19
25
|
tableName: string;
|
|
20
26
|
data: Record<string, string | boolean | number | null>;
|
|
21
27
|
returning?: string | Array<string>;
|
|
28
|
+
onConflict?: string | ConflictTypes;
|
|
22
29
|
}
|
|
23
30
|
export interface Update {
|
|
24
31
|
tableName: string;
|
|
25
32
|
data: Record<string, string | boolean | number | null>;
|
|
26
33
|
where: Where;
|
|
27
34
|
returning?: string | Array<string>;
|
|
35
|
+
onConflict?: string | ConflictTypes;
|
|
28
36
|
}
|
|
29
37
|
export interface Delete {
|
|
30
38
|
tableName: string;
|
|
31
39
|
where: Where;
|
|
32
40
|
returning?: string | Array<string>;
|
|
33
41
|
}
|
|
42
|
+
export interface Result {
|
|
43
|
+
changes?: number;
|
|
44
|
+
duration: number;
|
|
45
|
+
lastRowId?: number;
|
|
46
|
+
results?: Array<Record<string, string | boolean | number | null>>;
|
|
47
|
+
served_by: string;
|
|
48
|
+
success: boolean;
|
|
49
|
+
}
|
|
50
|
+
export interface ResultOne {
|
|
51
|
+
changes?: number;
|
|
52
|
+
duration: number;
|
|
53
|
+
lastRowId?: number;
|
|
54
|
+
results?: Record<string, string | boolean | number | null>;
|
|
55
|
+
served_by: string;
|
|
56
|
+
success: boolean;
|
|
57
|
+
}
|
package/dist/umd/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["workers-qb"]=t():e["workers-qb"]=t()}(this,(function(){return(()=>{"use strict";var e={501:function(e,t,r){var n=this&&this.__assign||function(){return n=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},n.apply(this,arguments)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function u(e){try{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=r(40),c=function(){function e(){}return e.prototype.execute=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(e){throw new Error("Execute method not implemented")}))}))},e.prototype.createTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"CREATE TABLE "+(e.ifNotExists?"IF NOT EXISTS":"")+" "+e.tableName+" ("+e.schema+")"})]}))}))},e.prototype.dropTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"DROP TABLE "+(e.ifExists?"IF EXISTS":"")+" "+e.tableName})]}))}))},e.prototype.fetchOne=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(
|
|
1
|
+
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports["workers-qb"]=t():e["workers-qb"]=t()}(this,(function(){return(()=>{"use strict";var e={501:function(e,t,r){var n=this&&this.__assign||function(){return n=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e},n.apply(this,arguments)},o=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function u(e){try{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((n=n.apply(e,t||[])).next())}))},i=this&&this.__generator||function(e,t){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.QueryBuilder=void 0;var u=r(40),c=function(){function e(){}return e.prototype.execute=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(e){throw new Error("Execute method not implemented")}))}))},e.prototype.createTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"CREATE TABLE "+(e.ifNotExists?"IF NOT EXISTS":"")+" "+e.tableName+" ("+e.schema+")"})]}))}))},e.prototype.dropTable=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:"DROP TABLE "+(e.ifExists?"IF EXISTS":"")+" "+e.tableName})]}))}))},e.prototype.fetchOne=function(e){return o(this,void 0,void 0,(function(){var t;return i(this,(function(r){switch(r.label){case 0:return[4,this.execute({query:this._select(n(n({},e),{limit:1})),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})];case 1:return t=r.sent(),[2,n(n({},t),{results:t.results[0]})]}}))}))},e.prototype.fetchAll=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._select(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.insert=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._insert(e),arguments:Object.values(e.data),fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.update=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._update(e),arguments:e.where&&e.where.params?e.where.params.concat(Object.values(e.data)):Object.values(e.data),fetchType:u.FetchTypes.ALL})]}))}))},e.prototype.delete=function(e){return o(this,void 0,void 0,(function(){return i(this,(function(t){return[2,this.execute({query:this._delete(e),arguments:e.where?e.where.params:void 0,fetchType:u.FetchTypes.ALL})]}))}))},e.prototype._onConflict=function(e){return e?"OR "+e+" ":""},e.prototype._insert=function(e){var t=Object.keys(e.data).join(", "),r=[];return Object.keys(e.data).forEach((function(e,t){r.push("?"+(t+1))})),"INSERT "+this._onConflict(e.onConflict)+"INTO "+e.tableName+" ("+t+") VALUES("+r.join(", ")+")"+this._returning(e.returning)},e.prototype._update=function(e){var t,r=e.where&&e.where.params?Object.keys(e.where.params).length:0,n=[];return Object.entries(e.data).forEach((function(e,t){var o=e[0];e[1],n.push(o+" = ?"+(r+t+1))})),"UPDATE "+this._onConflict(e.onConflict)+e.tableName+" SET "+n.join(", ")+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._delete=function(e){var t;return"DELETE FROM "+e.tableName+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._returning(e.returning)},e.prototype._select=function(e){var t;return"SELECT "+this._fields(e.fields)+" FROM "+e.tableName+this._join(e.join)+this._where(null===(t=e.where)||void 0===t?void 0:t.conditions)+this._groupBy(e.groupBy)+this._having(e.having)+this._orderBy(e.orderBy)+this._limit(e.limit)+this._offset(e.offset)},e.prototype._fields=function(e){return"string"==typeof e?e:e.join(", ")},e.prototype._where=function(e){return e?"string"==typeof e?" WHERE "+e:" WHERE "+e.join(" AND "):""},e.prototype._join=function(e){return e?(e.type?" "+e.type:"")+" JOIN "+e.table+" ON "+e.on:""},e.prototype._groupBy=function(e){return e?"string"==typeof e?" GROUP BY "+e:" GROUP BY "+e.join(", "):""},e.prototype._having=function(e){return e?" HAVING "+e:""},e.prototype._orderBy=function(e){if(!e)return"";if("string"==typeof e)return" ORDER BY "+e;if("array"===e.constructor.name.toLowerCase())return" ORDER BY "+e.join(", ");var t=[];return Object.entries(e).forEach((function(e){var r=e[0],n=e[1];t.push(r+" "+n)}))," ORDER BY "+t.join(", ")},e.prototype._limit=function(e){return e?" LIMIT "+e:""},e.prototype._offset=function(e){return e?" OFFSET "+e:""},e.prototype._returning=function(e){return e?"string"==typeof e?" RETURNING "+e:" RETURNING "+e.join(", "):""},e}();t.QueryBuilder=c},513:function(e,t,r){var n,o=this&&this.__extends||(n=function(e,t){return n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)Object.prototype.hasOwnProperty.call(t,r)&&(e[r]=t[r])},n(e,t)},function(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Class extends value "+String(t)+" is not a constructor or null");function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}),i=this&&this.__awaiter||function(e,t,r,n){return new(r||(r=Promise))((function(o,i){function u(e){try{s(n.next(e))}catch(e){i(e)}}function c(e){try{s(n.throw(e))}catch(e){i(e)}}function s(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(u,c)}s((n=n.apply(e,t||[])).next())}))},u=this&&this.__generator||function(e,t){var r,n,o,i,u={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function c(i){return function(c){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;u;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return u.label++,{value:i[1],done:!1};case 5:u.label++,n=i[1],i=[0];continue;case 7:i=u.ops.pop(),u.trys.pop();continue;default:if(!((o=(o=u.trys).length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){u=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){u.label=i[1];break}if(6===i[0]&&u.label<o[1]){u.label=o[1],o=i;break}if(o&&u.label<o[2]){u.label=o[2],u.ops.push(i);break}o[2]&&u.ops.pop(),u.trys.pop();continue}i=t.call(e,u)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,c])}}};Object.defineProperty(t,"__esModule",{value:!0}),t.D1QB=void 0;var c=r(501),s=r(40),a=function(e){function t(t){var r=e.call(this)||this;return r.db=t,r}return o(t,e),t.prototype.execute=function(e){return i(this,void 0,void 0,(function(){var t;return u(this,(function(r){return t=this.db.prepare(e.query),e.arguments&&(t=t.bind.apply(t,e.arguments)),e.fetchType===s.FetchTypes.ONE?[2,t.first()]:e.fetchType===s.FetchTypes.ALL?[2,t.all()]:[2,t.run()]}))}))},t}(c.QueryBuilder);t.D1QB=a},40:(e,t)=>{var r,n,o,i;Object.defineProperty(t,"__esModule",{value:!0}),t.JoinTypes=t.ConflictTypes=t.FetchTypes=t.OrderTypes=void 0,(i=t.OrderTypes||(t.OrderTypes={})).ASC="ASC",i.DESC="DESC",(o=t.FetchTypes||(t.FetchTypes={})).ONE="ONE",o.ALL="ALL",(n=t.ConflictTypes||(t.ConflictTypes={})).ROLLBACK="ROLLBACK",n.ABORT="ABORT",n.FAIL="FAIL",n.IGNORE="IGNORE",n.REPLACE="REPLACE",(r=t.JoinTypes||(t.JoinTypes={})).INNER="INNER",r.LEFT="LEFT",r.CROSS="CROSS"}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n].call(i.exports,i,i.exports,r),i.exports}var n={};return(()=>{var e=n;Object.defineProperty(e,"__esModule",{value:!0}),e.FetchTypes=e.OrderTypes=e.D1QB=e.QueryBuilder=void 0;var t=r(501);Object.defineProperty(e,"QueryBuilder",{enumerable:!0,get:function(){return t.QueryBuilder}});var o=r(513);Object.defineProperty(e,"D1QB",{enumerable:!0,get:function(){return o.D1QB}});var i=r(40);Object.defineProperty(e,"OrderTypes",{enumerable:!0,get:function(){return i.OrderTypes}}),Object.defineProperty(e,"FetchTypes",{enumerable:!0,get:function(){return i.FetchTypes}})})(),n})()}));
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "workers-qb",
|
|
3
|
-
"version": "0.1.
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "0.1.3",
|
|
4
|
+
"description": "Zero dependencies Query Builder for Cloudflare D1 Workers",
|
|
5
5
|
"main": "dist/cjs/index.js",
|
|
6
6
|
"module": "dist/esm/index.js",
|
|
7
7
|
"umd:main": "dist/umd/index.js",
|
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
"package": "npm run build && npm pack",
|
|
20
20
|
"test": "jest --no-cache --runInBand",
|
|
21
21
|
"test:cov": "jest --coverage --no-cache --runInBand",
|
|
22
|
-
"addscope": "node tools/packagejson name @g4brym/workers-qb"
|
|
22
|
+
"addscope": "node tools/packagejson name @g4brym/workers-qb",
|
|
23
|
+
"prettify": "prettier . --write --ignore-unknown",
|
|
24
|
+
"prepare": "husky install"
|
|
23
25
|
},
|
|
24
26
|
"publishConfig": {
|
|
25
27
|
"access": "public"
|
|
@@ -61,7 +63,6 @@
|
|
|
61
63
|
"bugs": {
|
|
62
64
|
"url": "https://github.com/G4brym/workers-qb/issues"
|
|
63
65
|
},
|
|
64
|
-
"dependencies": {},
|
|
65
66
|
"devDependencies": {
|
|
66
67
|
"@commitlint/cli": "^13.1.0",
|
|
67
68
|
"@commitlint/config-conventional": "^13.1.0",
|