@iamkirbki/database-handler-core 4.4.3 → 4.4.4
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/dist/abstract/Controller.js +18 -39
- package/dist/abstract/Model.d.ts.map +1 -1
- package/dist/abstract/Model.js +100 -98
- package/dist/abstract/SchemaTableBuilder.js +4 -1
- package/dist/abstract/model/ModelRelation.js +34 -46
- package/dist/base/Query.js +56 -79
- package/dist/base/Record.js +75 -90
- package/dist/base/Table.js +128 -153
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.js +7 -18
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.js +5 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.js +7 -18
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.js +8 -19
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.js +26 -39
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.js +9 -20
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.js +8 -19
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.js +9 -20
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.js +87 -101
- package/dist/runtime/Repository.d.ts +1 -1
- package/dist/runtime/Repository.d.ts.map +1 -1
- package/dist/runtime/Repository.js +71 -95
- package/package.json +1 -1
|
@@ -1,47 +1,26 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
export default class Controller {
|
|
11
|
-
static index() {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return yield instance.index();
|
|
15
|
-
});
|
|
2
|
+
static async index() {
|
|
3
|
+
const instance = new this();
|
|
4
|
+
return await instance.index();
|
|
16
5
|
}
|
|
17
|
-
static show(value) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
return yield instance.show(value);
|
|
21
|
-
});
|
|
6
|
+
static async show(value) {
|
|
7
|
+
const instance = new this();
|
|
8
|
+
return await instance.show(value);
|
|
22
9
|
}
|
|
23
|
-
static edit(value) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
return yield instance.edit(value);
|
|
27
|
-
});
|
|
10
|
+
static async edit(value) {
|
|
11
|
+
const instance = new this();
|
|
12
|
+
return await instance.edit(value);
|
|
28
13
|
}
|
|
29
|
-
static update(id, newValues) {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return yield instance.update(id, newValues);
|
|
33
|
-
});
|
|
14
|
+
static async update(id, newValues) {
|
|
15
|
+
const instance = new this();
|
|
16
|
+
return await instance.update(id, newValues);
|
|
34
17
|
}
|
|
35
|
-
static create(data) {
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
return yield instance.create(data);
|
|
39
|
-
});
|
|
18
|
+
static async create(data) {
|
|
19
|
+
const instance = new this();
|
|
20
|
+
return await instance.create(data);
|
|
40
21
|
}
|
|
41
|
-
static delete(id) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
return yield instance.delete(id);
|
|
45
|
-
});
|
|
22
|
+
static async delete(id) {
|
|
23
|
+
const instance = new this();
|
|
24
|
+
return await instance.delete(id);
|
|
46
25
|
}
|
|
47
26
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Model.d.ts","sourceRoot":"","sources":["../../src/abstract/Model.ts"],"names":[],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AAC;QACf,gBAAgB,CAAC,EAAE,KAAK,GAAG,MAAM,CAAC;KACrC,GAAG,IAAI;IA0CD,aAAa,CAAC,MAAM,EAAE;QACzB,KAAK,EAAE,MAAM,CAAC;QACd,OAAO,EAAE,MAAM,EAAE,CAAC;QAClB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;QAC1B,KAAK,CAAC,EAAE,MAAM,CAAC;QACf,MAAM,CAAC,EAAE,6BAA6B,CAAC,MAAM,CAAC,EAAE,CAAC;QACjD,MAAM,CAAC,EAAE,mBAAmB,CAAC;KAChC,GAAG,IAAI;IAgDK,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAerC,OAAO,CAAC,oBAAoB;IAY5B,OAAO,CAAC,aAAa;IAad,MAAM,IAAI,OAAO,CAAC,SAAS,CAAC,GAAG,SAAS;IAIxC,QAAQ,IAAI,OAAO,CAAC,SAAS,CAAC,GAAG,SAAS;CAGpD"}
|
package/dist/abstract/Model.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import Repository from '../runtime/Repository.js';
|
|
11
2
|
import ModelRelations from '../abstract/model/ModelRelation.js';
|
|
12
3
|
import { QueryEvaluationPhase, } from '../types/index.js';
|
|
@@ -133,78 +124,86 @@ export default class Model extends ModelRelations {
|
|
|
133
124
|
this.queryLayers.base.where = [{ column: this.primaryKeyColumn, operator: '=', value: primaryKeyValue }];
|
|
134
125
|
return this;
|
|
135
126
|
}
|
|
136
|
-
static findOrFail(primaryKeyValue) {
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
return (yield instance.findOrFail(primaryKeyValue));
|
|
140
|
-
});
|
|
127
|
+
static async findOrFail(primaryKeyValue) {
|
|
128
|
+
const instance = new this();
|
|
129
|
+
return (await instance.findOrFail(primaryKeyValue));
|
|
141
130
|
}
|
|
142
|
-
findOrFail(primaryKeyValue) {
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
});
|
|
131
|
+
async findOrFail(primaryKeyValue) {
|
|
132
|
+
var _a;
|
|
133
|
+
if (primaryKeyValue) {
|
|
134
|
+
this.queryLayers.base.where = [{ column: this.primaryKeyColumn, operator: '=', value: primaryKeyValue }];
|
|
135
|
+
}
|
|
136
|
+
this.queryLayers.base.from = this.Configuration.table;
|
|
137
|
+
const query = this.queryLayers;
|
|
138
|
+
const record = await ((_a = this.repository) === null || _a === void 0 ? void 0 : _a.first(query, this));
|
|
139
|
+
if (!record) {
|
|
140
|
+
throw new Error(`Record with primary key ${primaryKeyValue} not found.`);
|
|
141
|
+
}
|
|
142
|
+
this.attributes = record;
|
|
143
|
+
this.originalAttributes = { ...this.attributes };
|
|
144
|
+
this.exists = true;
|
|
145
|
+
return this;
|
|
158
146
|
}
|
|
159
|
-
static first(primaryKeyValue) {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return instance.first(primaryKeyValue);
|
|
163
|
-
});
|
|
147
|
+
static async first(primaryKeyValue) {
|
|
148
|
+
const instance = new this();
|
|
149
|
+
return instance.first(primaryKeyValue);
|
|
164
150
|
}
|
|
165
|
-
first(primaryKeyValue) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
this.
|
|
174
|
-
this.
|
|
175
|
-
this.
|
|
151
|
+
async first(primaryKeyValue) {
|
|
152
|
+
var _a;
|
|
153
|
+
if (primaryKeyValue !== undefined) {
|
|
154
|
+
this.queryLayers.base.where = [{ column: this.primaryKeyColumn, operator: '=', value: primaryKeyValue }, ...this.normalizeConditions(this.queryLayers.base.where || [])];
|
|
155
|
+
}
|
|
156
|
+
const attributes = (await ((_a = this.repository) === null || _a === void 0 ? void 0 : _a.first({
|
|
157
|
+
...this.queryLayers,
|
|
158
|
+
base: {
|
|
159
|
+
...this.queryLayers.base,
|
|
160
|
+
from: this.Configuration.table,
|
|
161
|
+
where: this.normalizeConditions(this.queryLayers.base.where || []),
|
|
176
162
|
}
|
|
177
|
-
|
|
178
|
-
|
|
163
|
+
}, this)));
|
|
164
|
+
if (attributes) {
|
|
165
|
+
this.attributes = attributes;
|
|
166
|
+
this.originalAttributes = { ...attributes };
|
|
167
|
+
this.exists = true;
|
|
168
|
+
}
|
|
169
|
+
return this;
|
|
179
170
|
}
|
|
180
|
-
get() {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
171
|
+
async get() {
|
|
172
|
+
const records = await this.repository.get({
|
|
173
|
+
...this.queryLayers,
|
|
174
|
+
base: {
|
|
175
|
+
...this.queryLayers.base,
|
|
176
|
+
from: this.Configuration.table,
|
|
177
|
+
}
|
|
178
|
+
}, this);
|
|
179
|
+
return records.map((record) => {
|
|
180
|
+
const instance = new this.constructor();
|
|
181
|
+
instance.set(record);
|
|
182
|
+
instance.exists = true;
|
|
183
|
+
instance.originalAttributes = { ...record };
|
|
184
|
+
instance.attributes = { ...record };
|
|
185
|
+
return instance;
|
|
191
186
|
});
|
|
192
187
|
}
|
|
193
188
|
static all() {
|
|
194
189
|
const instance = new this();
|
|
195
190
|
return instance.all();
|
|
196
191
|
}
|
|
197
|
-
all() {
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
192
|
+
async all() {
|
|
193
|
+
const records = await this.repository.all(this, {
|
|
194
|
+
...this.queryLayers,
|
|
195
|
+
base: {
|
|
196
|
+
...this.queryLayers.base,
|
|
197
|
+
from: this.Configuration.table,
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
return records.map((record) => {
|
|
201
|
+
const instance = new this.constructor();
|
|
202
|
+
instance.set(record);
|
|
203
|
+
instance.exists = true;
|
|
204
|
+
instance.originalAttributes = { ...record };
|
|
205
|
+
instance.attributes = { ...record };
|
|
206
|
+
return instance;
|
|
208
207
|
});
|
|
209
208
|
}
|
|
210
209
|
static set(attributes) {
|
|
@@ -215,32 +214,31 @@ export default class Model extends ModelRelations {
|
|
|
215
214
|
if (attributes[this.primaryKeyColumn] !== undefined && !this.exists) {
|
|
216
215
|
this.repository.syncModel(this);
|
|
217
216
|
}
|
|
218
|
-
this.attributes =
|
|
217
|
+
this.attributes = { ...this.attributes, ...attributes };
|
|
219
218
|
this.dirty = true;
|
|
220
219
|
return this;
|
|
221
220
|
}
|
|
222
|
-
save() {
|
|
223
|
-
|
|
224
|
-
this.originalAttributes
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
221
|
+
async save() {
|
|
222
|
+
this.originalAttributes = {
|
|
223
|
+
...this.originalAttributes,
|
|
224
|
+
...this.attributes,
|
|
225
|
+
};
|
|
226
|
+
await this.repository.save(this.originalAttributes);
|
|
227
|
+
this.exists = true;
|
|
228
|
+
this.dirty = false;
|
|
229
|
+
return this;
|
|
230
230
|
}
|
|
231
|
-
update(attributes) {
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
return this;
|
|
243
|
-
});
|
|
231
|
+
async update(attributes) {
|
|
232
|
+
var _a;
|
|
233
|
+
if (this.primaryKey === undefined) {
|
|
234
|
+
throw new Error('Primary key value is undefined. Cannot update record without a valid primary key.');
|
|
235
|
+
}
|
|
236
|
+
const newRecord = await ((_a = this.repository) === null || _a === void 0 ? void 0 : _a.update({ [this.primaryKeyColumn]: this.primaryKey }, attributes, this.Configuration.table));
|
|
237
|
+
if (newRecord) {
|
|
238
|
+
this.originalAttributes = newRecord.values;
|
|
239
|
+
this.exists = true;
|
|
240
|
+
}
|
|
241
|
+
return this;
|
|
244
242
|
}
|
|
245
243
|
near(params) {
|
|
246
244
|
var _a;
|
|
@@ -347,12 +345,16 @@ export default class Model extends ModelRelations {
|
|
|
347
345
|
this.queryLayers.final.blacklistTables.push(...Array.from(new Set(this.collectTables({ table, nested: nested || [] }))));
|
|
348
346
|
return this;
|
|
349
347
|
}
|
|
350
|
-
toSql() {
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
348
|
+
async toSql() {
|
|
349
|
+
var _a;
|
|
350
|
+
const sql = await ((_a = this.repository) === null || _a === void 0 ? void 0 : _a.toSql({
|
|
351
|
+
...this.queryLayers,
|
|
352
|
+
base: {
|
|
353
|
+
...this.queryLayers.base,
|
|
354
|
+
from: this.Configuration.table,
|
|
355
|
+
}
|
|
356
|
+
}, this));
|
|
357
|
+
return sql === null || sql === void 0 ? void 0 : sql.replace(/\s+/g, ' ').replace(/\n/g, '');
|
|
356
358
|
}
|
|
357
359
|
collectSelectAliases(def) {
|
|
358
360
|
const columnAliases = def.columns.map(col => `${def.table}.${col} AS ${def.table}_${col}`);
|
|
@@ -5,7 +5,10 @@ export default class SchemaTableBuilder {
|
|
|
5
5
|
addColumn(data) {
|
|
6
6
|
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
7
7
|
if (data.name) {
|
|
8
|
-
this.columns.push(
|
|
8
|
+
this.columns.push({
|
|
9
|
+
...data,
|
|
10
|
+
constraints: ['NOT NULL', ...((_a = data.constraints) !== null && _a !== void 0 ? _a : [])],
|
|
11
|
+
});
|
|
9
12
|
}
|
|
10
13
|
else {
|
|
11
14
|
const lastColumn = this.columns[this.columns.length - 1];
|
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
export default class ModelRelations {
|
|
11
2
|
constructor() {
|
|
12
3
|
this.joinedEntities = [];
|
|
@@ -18,33 +9,29 @@ export default class ModelRelations {
|
|
|
18
9
|
get Relations() {
|
|
19
10
|
return this.relations;
|
|
20
11
|
}
|
|
21
|
-
insertRecordIntoPivotTable(otherTable, foreignKey) {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
yield this.repository.insertRecordIntoPivotTable(foreignKey, this.self, relation);
|
|
29
|
-
});
|
|
12
|
+
async insertRecordIntoPivotTable(otherTable, foreignKey) {
|
|
13
|
+
await this.callRelationMethod(otherTable);
|
|
14
|
+
const relation = this.relations.pop();
|
|
15
|
+
if (!relation) {
|
|
16
|
+
throw new Error(`Relation for pivot table insertion not found.`);
|
|
17
|
+
}
|
|
18
|
+
await this.repository.insertRecordIntoPivotTable(foreignKey, this.self, relation);
|
|
30
19
|
}
|
|
31
|
-
ManyToMany(
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
pivotLocalKey: pivotLocalKey,
|
|
41
|
-
});
|
|
42
|
-
if (!relation) {
|
|
43
|
-
throw new Error(`Failed to create many-to-many relation for model ${model.Configuration.table}`);
|
|
44
|
-
}
|
|
45
|
-
this.relations.push(relation);
|
|
46
|
-
return this;
|
|
20
|
+
async ManyToMany(model, pivotTable = [this.Configuration.table, model.Configuration.table].sort().join('_'), localKey = this.Configuration.primaryKey, foreignKey = model.Configuration.primaryKey, pivotForeignKey = `${this.Configuration.table}_${localKey}`, pivotLocalKey = `${model.Configuration.table}_${foreignKey}`) {
|
|
21
|
+
const relation = await this.repository.getManyToManyRelation({
|
|
22
|
+
type: 'manyToMany',
|
|
23
|
+
model: model,
|
|
24
|
+
pivotTable: pivotTable,
|
|
25
|
+
foreignKey: foreignKey,
|
|
26
|
+
pivotForeignKey: pivotForeignKey,
|
|
27
|
+
localKey: localKey,
|
|
28
|
+
pivotLocalKey: pivotLocalKey,
|
|
47
29
|
});
|
|
30
|
+
if (!relation) {
|
|
31
|
+
throw new Error(`Failed to create many-to-many relation for model ${model.Configuration.table}`);
|
|
32
|
+
}
|
|
33
|
+
this.relations.push(relation);
|
|
34
|
+
return this;
|
|
48
35
|
}
|
|
49
36
|
hasMany(model, foreignKey = `${this.Configuration.table}_${this.Configuration.primaryKey}`, localKey = this.Configuration.primaryKey) {
|
|
50
37
|
this.relations.push({
|
|
@@ -91,18 +78,16 @@ export default class ModelRelations {
|
|
|
91
78
|
});
|
|
92
79
|
return this;
|
|
93
80
|
}
|
|
94
|
-
asyncWith(relation, queryScopes) {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
queryScopes: normalizedScopes
|
|
103
|
-
});
|
|
104
|
-
return this;
|
|
81
|
+
async asyncWith(relation, queryScopes) {
|
|
82
|
+
await this.callRelationMethod(relation);
|
|
83
|
+
const lastRelation = this.relations[this.relations.length - 1];
|
|
84
|
+
const tableName = lastRelation.model.Configuration.table;
|
|
85
|
+
const normalizedScopes = this.normalizeQueryScopes(queryScopes, tableName);
|
|
86
|
+
this.joinedEntities.push({
|
|
87
|
+
relation: relation,
|
|
88
|
+
queryScopes: normalizedScopes
|
|
105
89
|
});
|
|
90
|
+
return this;
|
|
106
91
|
}
|
|
107
92
|
callRelationMethod(relation) {
|
|
108
93
|
const method = Reflect.get(this, relation);
|
|
@@ -125,6 +110,9 @@ export default class ModelRelations {
|
|
|
125
110
|
const scopesArray = isSingleParameter
|
|
126
111
|
? [queryScopes]
|
|
127
112
|
: this.repository.ConvertParamsToArray(queryScopes);
|
|
128
|
-
return scopesArray.map(scope => (
|
|
113
|
+
return scopesArray.map(scope => ({
|
|
114
|
+
...scope,
|
|
115
|
+
column: `${tableName}.${scope.column}`
|
|
116
|
+
}));
|
|
129
117
|
}
|
|
130
118
|
}
|
package/dist/base/Query.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { Container, Record } from '../index.js';
|
|
11
2
|
import UnknownTableError from '../helpers/Errors/TableErrors/UnknownTableError.js';
|
|
12
3
|
import UnexpectedEmptyQueryError from '../helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js';
|
|
@@ -29,86 +20,72 @@ export default class Query {
|
|
|
29
20
|
this._queryCache = QueryCache.getInstance();
|
|
30
21
|
this._recordFactory = recordFactory;
|
|
31
22
|
}
|
|
32
|
-
throwIfTableNotExists() {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
throw new UnknownTableError(this.TableName);
|
|
38
|
-
}
|
|
39
|
-
this._queryCache.addExistingTable(this.TableName);
|
|
23
|
+
async throwIfTableNotExists() {
|
|
24
|
+
if (!this._queryCache.doesTableExist(this.TableName)) {
|
|
25
|
+
const exists = await this.DoesTableExist();
|
|
26
|
+
if (!exists) {
|
|
27
|
+
throw new UnknownTableError(this.TableName);
|
|
40
28
|
}
|
|
41
|
-
|
|
29
|
+
this._queryCache.addExistingTable(this.TableName);
|
|
30
|
+
}
|
|
42
31
|
}
|
|
43
32
|
/** Execute a non-SELECT query (INSERT, UPDATE, DELETE, etc.) */
|
|
44
|
-
Run() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
return (yield stmt.run(this.Parameters));
|
|
52
|
-
});
|
|
33
|
+
async Run() {
|
|
34
|
+
await this.throwIfTableNotExists();
|
|
35
|
+
if (!this._query) {
|
|
36
|
+
throw new UnexpectedEmptyQueryError();
|
|
37
|
+
}
|
|
38
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
39
|
+
return (await stmt.run(this.Parameters));
|
|
53
40
|
}
|
|
54
41
|
/** Execute a SELECT query and return all matching rows */
|
|
55
|
-
All() {
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return results.map((res) => this._recordFactory(this.TableName, res));
|
|
64
|
-
});
|
|
42
|
+
async All() {
|
|
43
|
+
await this.throwIfTableNotExists();
|
|
44
|
+
if (!this._query) {
|
|
45
|
+
throw new Error('No query defined to run.');
|
|
46
|
+
}
|
|
47
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
48
|
+
const results = (await stmt.all(this.Parameters));
|
|
49
|
+
return results.map((res) => this._recordFactory(this.TableName, res));
|
|
65
50
|
}
|
|
66
51
|
/** Execute a SELECT query and return the first matching row */
|
|
67
|
-
Get() {
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
: undefined;
|
|
78
|
-
});
|
|
52
|
+
async Get() {
|
|
53
|
+
await this.throwIfTableNotExists();
|
|
54
|
+
if (!this._query) {
|
|
55
|
+
throw new Error('No query defined to run.');
|
|
56
|
+
}
|
|
57
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
58
|
+
const results = (await stmt.get(this.Parameters));
|
|
59
|
+
return results
|
|
60
|
+
? this._recordFactory(this.TableName, results)
|
|
61
|
+
: undefined;
|
|
79
62
|
}
|
|
80
|
-
TableColumnInformation(tableName) {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
if (tableColumnInfo)
|
|
84
|
-
return tableColumnInfo;
|
|
85
|
-
tableColumnInfo = yield this._adapter.tableColumnInformation(tableName);
|
|
86
|
-
this._queryCache.setTableColumnInformation(tableName, tableColumnInfo);
|
|
63
|
+
async TableColumnInformation(tableName) {
|
|
64
|
+
let tableColumnInfo = this._queryCache.getTableColumnInformation(tableName);
|
|
65
|
+
if (tableColumnInfo)
|
|
87
66
|
return tableColumnInfo;
|
|
88
|
-
|
|
67
|
+
tableColumnInfo = await this._adapter.tableColumnInformation(tableName);
|
|
68
|
+
this._queryCache.setTableColumnInformation(tableName, tableColumnInfo);
|
|
69
|
+
return tableColumnInfo;
|
|
89
70
|
}
|
|
90
|
-
DoesTableExist() {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return exists;
|
|
100
|
-
});
|
|
71
|
+
async DoesTableExist() {
|
|
72
|
+
if (this._queryCache.doesTableExist(this.TableName)) {
|
|
73
|
+
return true;
|
|
74
|
+
}
|
|
75
|
+
const exists = await this._adapter.tableExists(this.TableName);
|
|
76
|
+
if (exists) {
|
|
77
|
+
this._queryCache.addExistingTable(this.TableName);
|
|
78
|
+
}
|
|
79
|
+
return exists;
|
|
101
80
|
}
|
|
102
|
-
Count() {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return parseInt(result.count) || 0;
|
|
111
|
-
});
|
|
81
|
+
async Count() {
|
|
82
|
+
await this.throwIfTableNotExists();
|
|
83
|
+
if (!this._query) {
|
|
84
|
+
throw new Error('No query defined to run.');
|
|
85
|
+
}
|
|
86
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
87
|
+
const result = (await stmt.get(this.Parameters));
|
|
88
|
+
return parseInt(result.count) || 0;
|
|
112
89
|
}
|
|
113
90
|
ConvertParamsToArray(params) {
|
|
114
91
|
const paramArray = [];
|
|
@@ -149,6 +126,6 @@ export default class Query {
|
|
|
149
126
|
: value,
|
|
150
127
|
};
|
|
151
128
|
})
|
|
152
|
-
.reduce((acc, curr) => (
|
|
129
|
+
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
|
|
153
130
|
}
|
|
154
131
|
}
|