@iamkirbki/database-handler-core 4.3.1 → 4.4.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/dist/abstract/Controller.d.ts +17 -0
- package/dist/abstract/Controller.d.ts.map +1 -0
- package/dist/abstract/Controller.js +47 -0
- package/dist/abstract/Model.d.ts +33 -19
- package/dist/abstract/Model.d.ts.map +1 -1
- package/dist/abstract/Model.js +177 -119
- package/dist/abstract/SchemaTableBuilder.d.ts +1 -1
- package/dist/abstract/SchemaTableBuilder.d.ts.map +1 -1
- package/dist/abstract/model/ModelRelation.d.ts +23 -0
- package/dist/abstract/model/ModelRelation.d.ts.map +1 -0
- package/dist/abstract/model/ModelRelation.js +130 -0
- package/dist/base/Query.d.ts +6 -4
- package/dist/base/Query.d.ts.map +1 -1
- package/dist/base/Query.js +62 -21
- package/dist/base/Record.d.ts.map +1 -1
- package/dist/base/Record.js +7 -5
- package/dist/base/Table.d.ts +5 -4
- package/dist/base/Table.d.ts.map +1 -1
- package/dist/base/Table.js +56 -31
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.js +6 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.js +6 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts +4 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts.map +1 -0
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.js +6 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts +4 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts.map +1 -0
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.js +6 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts +4 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts.map +1 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js +6 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts +4 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts.map +1 -0
- package/dist/helpers/Errors/TableErrors/UnknownTableError.js +6 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseDeleteQueryBuilder.js +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseInsertQueryBuilder.js +11 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts +11 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.js +27 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.d.ts +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseUpdateQueryBuilder.js +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts +9 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.js +102 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts +8 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.js +63 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts +8 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.js +49 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts +19 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.js +60 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.js +27 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts +15 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.js +74 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts +10 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.js +29 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.js +27 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts +8 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.js +5 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.d.ts +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/ValuesDecorator.js +17 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts +9 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.js +29 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts +54 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.js +102 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts +16 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.js +169 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.d.ts +15 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/SqlRenderer.js +79 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.d.ts +160 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.js +217 -0
- package/dist/interfaces/IExpressionBuilder.d.ts +7 -0
- package/dist/interfaces/IExpressionBuilder.d.ts.map +1 -0
- package/dist/interfaces/IExpressionBuilder.js +1 -0
- package/dist/interfaces/IQueryBuilder.d.ts +5 -0
- package/dist/interfaces/IQueryBuilder.d.ts.map +1 -0
- package/dist/interfaces/IQueryBuilder.js +1 -0
- package/dist/runtime/Container.d.ts +1 -0
- package/dist/runtime/Container.d.ts.map +1 -1
- package/dist/runtime/Container.js +5 -2
- package/dist/runtime/QueryCache.d.ts +12 -0
- package/dist/runtime/QueryCache.d.ts.map +1 -0
- package/dist/runtime/QueryCache.js +26 -0
- package/dist/runtime/Repository.d.ts +6 -4
- package/dist/runtime/Repository.d.ts.map +1 -1
- package/dist/runtime/Repository.js +95 -60
- package/dist/types/expressions.d.ts +154 -0
- package/dist/types/expressions.d.ts.map +1 -0
- package/dist/types/expressions.js +6 -0
- package/dist/types/factories.d.ts +3 -2
- package/dist/types/factories.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/model.d.ts +8 -8
- package/dist/types/model.d.ts.map +1 -1
- package/dist/types/query.d.ts +56 -2
- package/dist/types/query.d.ts.map +1 -1
- package/package.json +11 -3
- package/dist/helpers/QueryStatementBuilder.d.ts +0 -305
- package/dist/helpers/QueryStatementBuilder.d.ts.map +0 -1
- package/dist/helpers/QueryStatementBuilder.js +0 -416
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ModelRelation.d.ts","sourceRoot":"","sources":["../../../src/abstract/model/ModelRelation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAA6B,mBAAmB,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AACvI,OAAO,KAAK,MAAM,yBAAyB,CAAC;AAC5C,OAAO,UAAU,MAAM,6BAA6B,CAAC;AAErD,MAAM,CAAC,OAAO,CAAC,QAAQ,OAAO,cAAc,CACxC,IAAI,SAAS,UAAU,EACvB,IAAI,SAAS,KAAK,CAAC,IAAI,CAAC,GAAG,KAAK,CAAC,IAAI,CAAC;IAEtC,SAAS,CAAC,cAAc,EAAE,YAAY,EAAE,CAAM;IAC9C,SAAS,CAAC,SAAS,EAAE,QAAQ,EAAE,CAAM;IAErC,QAAQ,KAAK,aAAa,IAAI,WAAW,CAAC;IAC1C,SAAS,CAAC,QAAQ,KAAK,UAAU,IAAI,UAAU,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;IAC5D,SAAS,CAAC,QAAQ,KAAK,IAAI,IAAI,IAAI,CAAC;IAEpC,IAAW,cAAc,IAAI,YAAY,EAAE,CAE1C;IAED,IAAW,SAAS,IAAI,QAAQ,EAAE,CAEjC;IAEY,0BAA0B,CACnC,UAAU,EAAE,MAAM,EAClB,UAAU,EAAE,MAAM,GACnB,OAAO,CAAC,IAAI,CAAC;cAYA,UAAU,CAAC,SAAS,SAAS,KAAK,CAAC,UAAU,CAAC,EAC1D,KAAK,EAAE,SAAS,EAChB,UAAU,GAAE,MAA+E,EAC3F,QAAQ,GAAE,MAAsC,EAChD,UAAU,GAAE,MAAuC,EACnD,eAAe,GAAE,MAAkD,EACnE,aAAa,GAAE,MAAqD,GACrE,OAAO,CAAC,IAAI,CAAC;IAoBhB,SAAS,CAAC,OAAO,CAAC,SAAS,SAAS,KAAK,CAAC,UAAU,CAAC,EACjD,KAAK,EAAE,SAAS,EAChB,UAAU,GAAE,MAAuE,EACnF,QAAQ,GAAE,MAAsC,GACjD,IAAI;IAUP,SAAS,CAAC,MAAM,CAAC,SAAS,SAAS,KAAK,CAAC,UAAU,CAAC,EAChD,KAAK,EAAE,SAAS,EAChB,UAAU,GAAE,MAA4C,EACxD,QAAQ,GAAE,MAAyE,GACpF,IAAI;IAUP,SAAS,CAAC,SAAS,CAAC,SAAS,SAAS,KAAK,CAAC,UAAU,CAAC,EACnD,KAAK,EAAE,SAAS,EAChB,UAAU,GAAE,MAAyE,EACrF,QAAQ,GAAE,MAAuC,GAClD,IAAI;WAUO,IAAI,CAAC,kBAAkB,SAAS,KAAK,CAAC,UAAU,CAAC,EAC3D,IAAI,EAAE,UAAU,kBAAkB,EAClC,QAAQ,EAAE,MAAM,EAChB,WAAW,CAAC,EAAE,mBAAmB,GAClC,kBAAkB;IAKd,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,mBAAmB,GAAG,IAAI;IAsBzD,SAAS,CAAC,QAAQ,EAAE,MAAM,EAAE,WAAW,CAAC,EAAE,mBAAmB,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBnF,kBAAkB,CAAC,QAAQ,EAAE,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC;IAYjE,OAAO,CAAC,oBAAoB;CAuB/B"}
|
|
@@ -0,0 +1,130 @@
|
|
|
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
|
+
export default class ModelRelations {
|
|
11
|
+
constructor() {
|
|
12
|
+
this.joinedEntities = [];
|
|
13
|
+
this.relations = [];
|
|
14
|
+
}
|
|
15
|
+
get JoinedEntities() {
|
|
16
|
+
return this.joinedEntities;
|
|
17
|
+
}
|
|
18
|
+
get Relations() {
|
|
19
|
+
return this.relations;
|
|
20
|
+
}
|
|
21
|
+
insertRecordIntoPivotTable(otherTable, foreignKey) {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
yield this.callRelationMethod(otherTable);
|
|
24
|
+
const relation = this.relations.pop();
|
|
25
|
+
if (!relation) {
|
|
26
|
+
throw new Error(`Relation for pivot table insertion not found.`);
|
|
27
|
+
}
|
|
28
|
+
yield this.repository.insertRecordIntoPivotTable(foreignKey, this.self, relation);
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
ManyToMany(model_1) {
|
|
32
|
+
return __awaiter(this, arguments, void 0, function* (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}`) {
|
|
33
|
+
const relation = yield this.repository.getManyToManyRelation({
|
|
34
|
+
type: 'manyToMany',
|
|
35
|
+
model: model,
|
|
36
|
+
pivotTable: pivotTable,
|
|
37
|
+
foreignKey: foreignKey,
|
|
38
|
+
pivotForeignKey: pivotForeignKey,
|
|
39
|
+
localKey: localKey,
|
|
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;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
hasMany(model, foreignKey = `${this.Configuration.table}_${this.Configuration.primaryKey}`, localKey = this.Configuration.primaryKey) {
|
|
50
|
+
this.relations.push({
|
|
51
|
+
type: 'hasMany',
|
|
52
|
+
model: model,
|
|
53
|
+
foreignKey: foreignKey,
|
|
54
|
+
localKey: localKey,
|
|
55
|
+
});
|
|
56
|
+
return this;
|
|
57
|
+
}
|
|
58
|
+
hasOne(model, foreignKey = `${model.Configuration.primaryKey}`, localKey = `${model.Configuration.table}_${model.Configuration.primaryKey}`) {
|
|
59
|
+
this.relations.push({
|
|
60
|
+
type: 'hasOne',
|
|
61
|
+
model: model,
|
|
62
|
+
foreignKey: foreignKey,
|
|
63
|
+
localKey: localKey,
|
|
64
|
+
});
|
|
65
|
+
return this;
|
|
66
|
+
}
|
|
67
|
+
belongsTo(model, foreignKey = `${model.Configuration.table}_${model.Configuration.primaryKey}`, localKey = model.Configuration.primaryKey) {
|
|
68
|
+
this.relations.push({
|
|
69
|
+
type: 'belongsTo',
|
|
70
|
+
model: model,
|
|
71
|
+
foreignKey: foreignKey,
|
|
72
|
+
localKey: localKey,
|
|
73
|
+
});
|
|
74
|
+
return this;
|
|
75
|
+
}
|
|
76
|
+
static with(relation, queryScopes) {
|
|
77
|
+
const instance = new this();
|
|
78
|
+
return instance.with(relation, queryScopes);
|
|
79
|
+
}
|
|
80
|
+
with(relation, queryScopes) {
|
|
81
|
+
const result = this.callRelationMethod(relation);
|
|
82
|
+
if (result instanceof Promise) {
|
|
83
|
+
throw new Error(`Relation method '${relation}' is asynchronous. Use asyncWith() instead of with().`);
|
|
84
|
+
}
|
|
85
|
+
const lastRelation = this.relations[this.relations.length - 1];
|
|
86
|
+
const tableName = lastRelation.model.Configuration.table;
|
|
87
|
+
const normalizedScopes = this.normalizeQueryScopes(queryScopes, tableName);
|
|
88
|
+
this.joinedEntities.push({
|
|
89
|
+
relation: relation,
|
|
90
|
+
queryScopes: normalizedScopes
|
|
91
|
+
});
|
|
92
|
+
return this;
|
|
93
|
+
}
|
|
94
|
+
asyncWith(relation, queryScopes) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
yield this.callRelationMethod(relation);
|
|
97
|
+
const lastRelation = this.relations[this.relations.length - 1];
|
|
98
|
+
const tableName = lastRelation.model.Configuration.table;
|
|
99
|
+
const normalizedScopes = this.normalizeQueryScopes(queryScopes, tableName);
|
|
100
|
+
this.joinedEntities.push({
|
|
101
|
+
relation: relation,
|
|
102
|
+
queryScopes: normalizedScopes
|
|
103
|
+
});
|
|
104
|
+
return this;
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
callRelationMethod(relation) {
|
|
108
|
+
const method = Reflect.get(this, relation);
|
|
109
|
+
if (typeof method !== 'function') {
|
|
110
|
+
throw new Error(`Relation method '${relation}' does not exist`);
|
|
111
|
+
}
|
|
112
|
+
const result = method.call(this);
|
|
113
|
+
//@TODO: check if method is not static
|
|
114
|
+
// Only return promise if the method is actually async
|
|
115
|
+
return result instanceof Promise ? result : undefined;
|
|
116
|
+
}
|
|
117
|
+
normalizeQueryScopes(queryScopes, tableName) {
|
|
118
|
+
if (!queryScopes) {
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
const isSingleParameter = Object.keys(queryScopes).length === 3 &&
|
|
122
|
+
'column' in queryScopes &&
|
|
123
|
+
'operator' in queryScopes &&
|
|
124
|
+
'value' in queryScopes;
|
|
125
|
+
const scopesArray = isSingleParameter
|
|
126
|
+
? [queryScopes]
|
|
127
|
+
: this.repository.ConvertParamsToArray(queryScopes);
|
|
128
|
+
return scopesArray.map(scope => (Object.assign(Object.assign({}, scope), { column: `${tableName}.${scope.column}` })));
|
|
129
|
+
}
|
|
130
|
+
}
|
package/dist/base/Query.d.ts
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
|
-
import { columnType, QueryWhereCondition, QueryIsEqualParameter, TableColumnInfo, QueryComparisonParameters, QueryConstructorType } from
|
|
2
|
-
import { Record } from
|
|
1
|
+
import { columnType, QueryWhereCondition, QueryIsEqualParameter, TableColumnInfo, QueryComparisonParameters, QueryConstructorType } from '../types/index.js';
|
|
2
|
+
import { Record } from '../index.js';
|
|
3
3
|
/** Query class for executing custom SQL queries */
|
|
4
4
|
export default class Query {
|
|
5
5
|
readonly TableName: string;
|
|
6
6
|
private readonly _adapter;
|
|
7
|
+
private readonly _recordFactory;
|
|
8
|
+
private readonly _queryCache;
|
|
7
9
|
private _query?;
|
|
8
10
|
private _parameters;
|
|
9
|
-
private readonly _recordFactory;
|
|
10
11
|
get Parameters(): QueryWhereCondition;
|
|
11
|
-
constructor({ tableName, query, parameters, adapterName, recordFactory }: QueryConstructorType);
|
|
12
|
+
constructor({ tableName, query, parameters, adapterName, recordFactory, }: QueryConstructorType);
|
|
13
|
+
private throwIfTableNotExists;
|
|
12
14
|
/** Execute a non-SELECT query (INSERT, UPDATE, DELETE, etc.) */
|
|
13
15
|
Run<Type>(): Promise<Type>;
|
|
14
16
|
/** Execute a SELECT query and return all matching rows */
|
package/dist/base/Query.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../src/base/Query.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"Query.d.ts","sourceRoot":"","sources":["../../src/base/Query.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,UAAU,EACV,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,yBAAyB,EACzB,oBAAoB,EAErB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAa,MAAM,EAAoB,MAAM,gBAAgB,CAAC;AAKrE,mDAAmD;AACnD,MAAM,CAAC,OAAO,OAAO,KAAK;IACxB,SAAgB,SAAS,EAAE,MAAM,CAAC;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAmB;IAC5C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAa;IACzC,OAAO,CAAC,MAAM,CAAC,CAAS;IACxB,OAAO,CAAC,WAAW,CAA2B;IAE9C,IAAW,UAAU,IAAI,mBAAmB,CAE3C;gBAEW,EACV,SAAS,EACT,KAAK,EACL,UAAU,EACV,WAAW,EACX,aACoC,GACrC,EAAE,oBAAoB;YAaT,qBAAqB;IAWnC,gEAAgE;IACnD,GAAG,CAAC,IAAI,KAAK,OAAO,CAAC,IAAI,CAAC;IAUvC,0DAA0D;IAC7C,GAAG,CAAC,IAAI,SAAS,UAAU,KAAK,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IAWpE,+DAA+D;IAClD,GAAG,CAAC,IAAI,SAAS,UAAU,KAAK,OAAO,CAClD,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CACzB;IAaY,sBAAsB,CACjC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,eAAe,EAAE,CAAC;IAUhB,cAAc,IAAI,OAAO,CAAC,OAAO,CAAC;IAalC,KAAK,IAAI,OAAO,CAAC,MAAM,CAAC;IAW9B,oBAAoB,CACzB,MAAM,EAAE,mBAAmB,GAC1B,yBAAyB,EAAE;IAkB9B,sEAAsE;IAC/D,qBAAqB,CAC1B,MAAM,EAAE,mBAAmB,GAC1B,qBAAqB;IAaxB,sEAAsE;IAC/D,oBAAoB,CACzB,MAAM,EAAE,qBAAqB,GAC5B,qBAAqB;CAYzB"}
|
package/dist/base/Query.js
CHANGED
|
@@ -7,71 +7,106 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import { Container, Record } from
|
|
10
|
+
import { Container, Record } from '../index.js';
|
|
11
|
+
import UnknownTableError from '../helpers/Errors/TableErrors/UnknownTableError.js';
|
|
12
|
+
import UnexpectedEmptyQueryError from '../helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js';
|
|
13
|
+
import QueryCache from '../runtime/QueryCache.js';
|
|
11
14
|
/** Query class for executing custom SQL queries */
|
|
12
15
|
export default class Query {
|
|
13
16
|
get Parameters() {
|
|
14
17
|
return this._parameters;
|
|
15
18
|
}
|
|
16
|
-
constructor({ tableName, query, parameters, adapterName, recordFactory = (table, values, adapter) => new Record(table, values, adapter) }) {
|
|
17
|
-
this._query = "";
|
|
19
|
+
constructor({ tableName, query, parameters, adapterName, recordFactory = (table, values, adapter) => new Record(table, values, adapter), }) {
|
|
18
20
|
this._parameters = {};
|
|
19
21
|
this.TableName = tableName;
|
|
20
22
|
this._query = query;
|
|
21
23
|
if (parameters)
|
|
22
24
|
this._parameters = this.ConvertParamsToObject(parameters);
|
|
25
|
+
// eslint-disable-next-line no-undef
|
|
26
|
+
if (Container.getInstance().logging)
|
|
27
|
+
this._query ? console.info(this._query, "\n", this._parameters) : console.info("No query found, probably checking if a table exists or getting the table column information.");
|
|
23
28
|
this._adapter = Container.getInstance().getAdapter(adapterName);
|
|
29
|
+
this._queryCache = QueryCache.getInstance();
|
|
24
30
|
this._recordFactory = recordFactory;
|
|
25
31
|
}
|
|
32
|
+
throwIfTableNotExists() {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
if (!this._queryCache.doesTableExist(this.TableName)) {
|
|
35
|
+
const exists = yield this.DoesTableExist();
|
|
36
|
+
if (!exists) {
|
|
37
|
+
throw new UnknownTableError(this.TableName);
|
|
38
|
+
}
|
|
39
|
+
this._queryCache.addExistingTable(this.TableName);
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
26
43
|
/** Execute a non-SELECT query (INSERT, UPDATE, DELETE, etc.) */
|
|
27
44
|
Run() {
|
|
28
45
|
return __awaiter(this, void 0, void 0, function* () {
|
|
46
|
+
yield this.throwIfTableNotExists();
|
|
29
47
|
if (!this._query) {
|
|
30
|
-
throw new
|
|
48
|
+
throw new UnexpectedEmptyQueryError();
|
|
31
49
|
}
|
|
32
50
|
const stmt = yield this._adapter.prepare(this._query);
|
|
33
|
-
return yield stmt.run(this.Parameters);
|
|
51
|
+
return (yield stmt.run(this.Parameters));
|
|
34
52
|
});
|
|
35
53
|
}
|
|
36
54
|
/** Execute a SELECT query and return all matching rows */
|
|
37
55
|
All() {
|
|
38
56
|
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
+
yield this.throwIfTableNotExists();
|
|
39
58
|
if (!this._query) {
|
|
40
|
-
throw new Error(
|
|
59
|
+
throw new Error('No query defined to run.');
|
|
41
60
|
}
|
|
42
61
|
const stmt = yield this._adapter.prepare(this._query);
|
|
43
|
-
const results = yield stmt.all(this.Parameters);
|
|
44
|
-
return results.map(res => this._recordFactory(this.TableName, res));
|
|
62
|
+
const results = (yield stmt.all(this.Parameters));
|
|
63
|
+
return results.map((res) => this._recordFactory(this.TableName, res));
|
|
45
64
|
});
|
|
46
65
|
}
|
|
47
66
|
/** Execute a SELECT query and return the first matching row */
|
|
48
67
|
Get() {
|
|
49
68
|
return __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
yield this.throwIfTableNotExists();
|
|
50
70
|
if (!this._query) {
|
|
51
|
-
throw new Error(
|
|
71
|
+
throw new Error('No query defined to run.');
|
|
52
72
|
}
|
|
53
73
|
const stmt = yield this._adapter.prepare(this._query);
|
|
54
|
-
const results = yield stmt.get(this.Parameters);
|
|
55
|
-
return results
|
|
74
|
+
const results = (yield stmt.get(this.Parameters));
|
|
75
|
+
return results
|
|
76
|
+
? this._recordFactory(this.TableName, results)
|
|
77
|
+
: undefined;
|
|
56
78
|
});
|
|
57
79
|
}
|
|
58
80
|
TableColumnInformation(tableName) {
|
|
59
81
|
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
-
|
|
82
|
+
let tableColumnInfo = this._queryCache.getTableColumnInformation(tableName);
|
|
83
|
+
if (tableColumnInfo)
|
|
84
|
+
return tableColumnInfo;
|
|
85
|
+
tableColumnInfo = yield this._adapter.tableColumnInformation(tableName);
|
|
86
|
+
this._queryCache.setTableColumnInformation(tableName, tableColumnInfo);
|
|
87
|
+
return tableColumnInfo;
|
|
61
88
|
});
|
|
62
89
|
}
|
|
63
90
|
DoesTableExist() {
|
|
64
91
|
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
-
|
|
92
|
+
if (this._queryCache.doesTableExist(this.TableName)) {
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
const exists = yield this._adapter.tableExists(this.TableName);
|
|
96
|
+
if (exists) {
|
|
97
|
+
this._queryCache.addExistingTable(this.TableName);
|
|
98
|
+
}
|
|
99
|
+
return exists;
|
|
66
100
|
});
|
|
67
101
|
}
|
|
68
102
|
Count() {
|
|
69
103
|
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
yield this.throwIfTableNotExists();
|
|
70
105
|
if (!this._query) {
|
|
71
|
-
throw new Error(
|
|
106
|
+
throw new Error('No query defined to run.');
|
|
72
107
|
}
|
|
73
108
|
const stmt = yield this._adapter.prepare(this._query);
|
|
74
|
-
const result = yield stmt.get(this.Parameters);
|
|
109
|
+
const result = (yield stmt.get(this.Parameters));
|
|
75
110
|
return parseInt(result.count) || 0;
|
|
76
111
|
});
|
|
77
112
|
}
|
|
@@ -84,8 +119,8 @@ export default class Query {
|
|
|
84
119
|
Object.entries(params).forEach(([key, value]) => {
|
|
85
120
|
return paramArray.push({
|
|
86
121
|
column: key,
|
|
87
|
-
operator:
|
|
88
|
-
value
|
|
122
|
+
operator: '=',
|
|
123
|
+
value,
|
|
89
124
|
});
|
|
90
125
|
});
|
|
91
126
|
}
|
|
@@ -95,7 +130,7 @@ export default class Query {
|
|
|
95
130
|
ConvertParamsToObject(params) {
|
|
96
131
|
const paramObject = {};
|
|
97
132
|
if (Array.isArray(params)) {
|
|
98
|
-
params.forEach(param => {
|
|
133
|
+
params.forEach((param) => {
|
|
99
134
|
paramObject[param.column] = param.value;
|
|
100
135
|
});
|
|
101
136
|
}
|
|
@@ -106,8 +141,14 @@ export default class Query {
|
|
|
106
141
|
}
|
|
107
142
|
/** Databases don't like numeric values when inserting with a query */
|
|
108
143
|
ConvertValueToString(params) {
|
|
109
|
-
return Object.entries(params)
|
|
110
|
-
|
|
111
|
-
|
|
144
|
+
return Object.entries(params)
|
|
145
|
+
.map(([key, value]) => {
|
|
146
|
+
return {
|
|
147
|
+
[key]: value !== null && !(value instanceof Date) && value !== undefined
|
|
148
|
+
? value.toString()
|
|
149
|
+
: value,
|
|
150
|
+
};
|
|
151
|
+
})
|
|
152
|
+
.reduce((acc, curr) => (Object.assign(Object.assign({}, acc), curr)), {});
|
|
112
153
|
}
|
|
113
154
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Record.d.ts","sourceRoot":"","sources":["../../src/base/Record.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,
|
|
1
|
+
{"version":3,"file":"Record.d.ts","sourceRoot":"","sources":["../../src/base/Record.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,OAAO,EACH,UAAU,EAGV,qBAAqB,EACrB,YAAY,EACZ,aAAa,EAChB,MAAM,sBAAsB,CAAC;AAI9B,oDAAoD;AACpD,MAAM,CAAC,OAAO,OAAO,MAAM,CAAC,gBAAgB,SAAS,UAAU;IAC3D,OAAO,CAAC,OAAO,CAA4C;IAC3D,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAS;IACpC,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;gBAG3C,KAAK,EAAE,MAAM,EACb,MAAM,EAAE,gBAAgB,EACxB,OAAO,CAAC,EAAE,MAAM,EAChB,YAAY,GAAE,YAA4C,EAC1D,aAAa,GAAE,aAA8E;IASjG,gDAAgD;IAChD,IAAW,MAAM,IAAI,gBAAgB,CAEpC;IAEY,MAAM,IAAI,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;IAkDhD,yCAAyC;IAC5B,MAAM,CAAC,SAAS,EAAE,OAAO,CAAC,gBAAgB,CAAC,EAAE,eAAe,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IA2BhH,2CAA2C;IAC9B,MAAM,CAAC,UAAU,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAmBtE,qDAAqD;IAC9C,MAAM,IAAI,gBAAgB;IAIjC,mDAAmD;IAC5C,QAAQ,IAAI,MAAM;IAIzB,uCAAuC;IACvC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,gBAAgB;CAGvC"}
|
package/dist/base/Record.js
CHANGED
|
@@ -9,7 +9,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
9
9
|
};
|
|
10
10
|
import { inspect } from "util";
|
|
11
11
|
import Query from "./Query.js";
|
|
12
|
-
import QueryStatementBuilder from "../helpers/QueryStatementBuilder.js";
|
|
12
|
+
import QueryStatementBuilder from "../helpers/QueryBuilders/QueryStatementBuilder.js";
|
|
13
|
+
import oldQueryStatementBuilder from "../helpers/QueryBuilders/oldQueryStatementBuilder.js";
|
|
13
14
|
/** Record class represents a single database row */
|
|
14
15
|
export default class Record {
|
|
15
16
|
constructor(table, values, adapter, queryFactory = (config) => new Query(config), recordFactory = (table, values, adapter) => new Record(table, values, adapter)) {
|
|
@@ -32,7 +33,7 @@ export default class Record {
|
|
|
32
33
|
if (columns.length === 0) {
|
|
33
34
|
throw new Error("Cannot insert record with no columns");
|
|
34
35
|
}
|
|
35
|
-
const queryStr =
|
|
36
|
+
const queryStr = yield oldQueryStatementBuilder.BuildInsert(this._tableName, this._values);
|
|
36
37
|
const query = this._queryFactory({
|
|
37
38
|
tableName: this._tableName,
|
|
38
39
|
query: queryStr,
|
|
@@ -52,7 +53,8 @@ export default class Record {
|
|
|
52
53
|
if (recordId === undefined) {
|
|
53
54
|
return undefined;
|
|
54
55
|
}
|
|
55
|
-
const
|
|
56
|
+
const builder = new QueryStatementBuilder({ base: { from: this._tableName, where: Object.assign({}, this._values) } });
|
|
57
|
+
const queryStrSelect = yield builder.build();
|
|
56
58
|
const querySelect = this._queryFactory({
|
|
57
59
|
tableName: this._tableName,
|
|
58
60
|
query: queryStrSelect,
|
|
@@ -75,7 +77,7 @@ export default class Record {
|
|
|
75
77
|
if (originalValues.updated_at !== undefined) {
|
|
76
78
|
newValues.updated_at = new Date().toISOString();
|
|
77
79
|
}
|
|
78
|
-
const queryStr =
|
|
80
|
+
const queryStr = yield oldQueryStatementBuilder.BuildUpdate(this._tableName, newValues, whereParameters);
|
|
79
81
|
// Merge newValues and originalValues for parameters (with 'where_' prefix for where clause)
|
|
80
82
|
const params = Object.assign({}, newValues);
|
|
81
83
|
Object.entries(originalValues).forEach(([key, value]) => {
|
|
@@ -102,7 +104,7 @@ export default class Record {
|
|
|
102
104
|
yield this.Update(this._values, this._values.id ? { id: this._values.id } : primaryKey || {});
|
|
103
105
|
return;
|
|
104
106
|
}
|
|
105
|
-
const queryStr =
|
|
107
|
+
const queryStr = yield oldQueryStatementBuilder.BuildDelete(this._tableName, this._values);
|
|
106
108
|
const _query = this._queryFactory({
|
|
107
109
|
tableName: this._tableName,
|
|
108
110
|
query: queryStr,
|
package/dist/base/Table.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ReadableTableColumnInfo, TableColumnInfo, columnType, QueryFactory, RecordFactory, QueryLayers } from "../types/index.js";
|
|
2
2
|
import { Record, Query } from "../index.js";
|
|
3
3
|
/** Table class for interacting with a database table */
|
|
4
4
|
export default class Table {
|
|
@@ -16,16 +16,17 @@ export default class Table {
|
|
|
16
16
|
ReadableTableColumnInformation(): Promise<ReadableTableColumnInfo[]>;
|
|
17
17
|
Drop(): Promise<void>;
|
|
18
18
|
/** Fetch records with optional filtering, ordering, and pagination */
|
|
19
|
-
Records<Type extends columnType>(
|
|
19
|
+
Records<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type>[]>;
|
|
20
20
|
/** Fetch a single record from the table */
|
|
21
|
-
Record<Type extends columnType>(
|
|
21
|
+
Record<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type> | undefined>;
|
|
22
22
|
/** Get the total count of records */
|
|
23
23
|
RecordsCount(): Promise<number>;
|
|
24
24
|
exists(): Promise<boolean>;
|
|
25
25
|
/** Insert a record into the table */
|
|
26
26
|
Insert<Type extends columnType>(values: Type): Promise<Record<Type> | undefined>;
|
|
27
27
|
/** Perform JOIN operations with other tables */
|
|
28
|
-
Join<Type extends columnType>(
|
|
28
|
+
Join<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type>[]>;
|
|
29
29
|
private splitJoinValues;
|
|
30
|
+
toSql(queryLayers: QueryLayers): Promise<string>;
|
|
30
31
|
}
|
|
31
32
|
//# sourceMappingURL=Table.d.ts.map
|
package/dist/base/Table.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Table.d.ts","sourceRoot":"","sources":["../../src/base/Table.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,
|
|
1
|
+
{"version":3,"file":"Table.d.ts","sourceRoot":"","sources":["../../src/base/Table.ts"],"names":[],"mappings":"AAAA,OAAO,EACH,uBAAuB,EACvB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,EACd,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAE/C,wDAAwD;AACxD,MAAM,CAAC,OAAO,OAAO,KAAK;IACtB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAC,CAAS;IACzC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAAgB;IAE/C,+CAA+C;gBAE3C,IAAI,EAAE,MAAM,EACZ,aAAa,CAAC,EAAE,MAAM,EACtB,YAAY,GAAE,YAA4C,EAC1D,aAAa,GAAE,aAA8E;IAcjG,IAAW,iBAAiB,IAAI,KAAK,CAEpC;IAED,iCAAiC;IACpB,sBAAsB,CAAC,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,EAAE,CAAC;IAInF,iDAAiD;IACpC,8BAA8B,IAAI,OAAO,CAAC,uBAAuB,EAAE,CAAC;IAWpE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAWlC,sEAAsE;IACzD,OAAO,CAAC,IAAI,SAAS,UAAU,EACxC,WAAW,EAAE,WAAW,GACzB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IAqB1B,2CAA2C;IAC9B,MAAM,CAAC,IAAI,SAAS,UAAU,EACvC,WAAW,EAAE,WAAW,GACzB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAYpC,qCAAqC;IACxB,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAU/B,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;IAUvC,qCAAqC;IACxB,MAAM,CAAC,IAAI,SAAS,UAAU,EAAE,MAAM,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAM7F,gDAAgD;IACnC,IAAI,CAAC,IAAI,SAAS,UAAU,EACrC,WAAW,EAAE,WAAW,GACzB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;YAsCZ,eAAe;IAkChB,KAAK,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;CAoBhE"}
|
package/dist/base/Table.js
CHANGED
|
@@ -7,7 +7,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
7
7
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
|
-
import QueryStatementBuilder from "../helpers/QueryStatementBuilder.js";
|
|
10
|
+
import QueryStatementBuilder from "../helpers/QueryBuilders/QueryStatementBuilder.js";
|
|
11
11
|
import { Record, Query } from "../index.js";
|
|
12
12
|
/** Table class for interacting with a database table */
|
|
13
13
|
export default class Table {
|
|
@@ -58,18 +58,16 @@ export default class Table {
|
|
|
58
58
|
});
|
|
59
59
|
}
|
|
60
60
|
/** Fetch records with optional filtering, ordering, and pagination */
|
|
61
|
-
Records(
|
|
61
|
+
Records(queryLayers) {
|
|
62
62
|
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
orderBy: options === null || options === void 0 ? void 0 : options.orderBy,
|
|
67
|
-
limit: options === null || options === void 0 ? void 0 : options.limit,
|
|
68
|
-
offset: options === null || options === void 0 ? void 0 : options.offset,
|
|
69
|
-
});
|
|
63
|
+
var _a, _b;
|
|
64
|
+
const builder = new QueryStatementBuilder(queryLayers);
|
|
65
|
+
const queryStr = yield builder.build();
|
|
70
66
|
let params = {};
|
|
71
|
-
if ((
|
|
72
|
-
params =
|
|
67
|
+
if (((_a = queryLayers === null || queryLayers === void 0 ? void 0 : queryLayers.base) === null || _a === void 0 ? void 0 : _a.where) && Object.keys(queryLayers.base.where).length > 0)
|
|
68
|
+
params = queryLayers.base.where;
|
|
69
|
+
if (((_b = queryLayers === null || queryLayers === void 0 ? void 0 : queryLayers.pretty) === null || _b === void 0 ? void 0 : _b.where) && Object.keys(queryLayers.pretty.where).length > 0)
|
|
70
|
+
params = Object.assign(Object.assign({}, params), queryLayers.pretty.where);
|
|
73
71
|
const query = this._queryFactory({
|
|
74
72
|
tableName: this._name,
|
|
75
73
|
query: queryStr,
|
|
@@ -81,14 +79,9 @@ export default class Table {
|
|
|
81
79
|
});
|
|
82
80
|
}
|
|
83
81
|
/** Fetch a single record from the table */
|
|
84
|
-
Record(
|
|
82
|
+
Record(queryLayers) {
|
|
85
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
const results = yield this.Records({
|
|
87
|
-
select: options === null || options === void 0 ? void 0 : options.select,
|
|
88
|
-
where: options === null || options === void 0 ? void 0 : options.where,
|
|
89
|
-
orderBy: options === null || options === void 0 ? void 0 : options.orderBy,
|
|
90
|
-
limit: 1
|
|
91
|
-
});
|
|
84
|
+
const results = yield this.Records(Object.assign(Object.assign({}, queryLayers), { final: Object.assign(Object.assign({}, queryLayers === null || queryLayers === void 0 ? void 0 : queryLayers.final), { limit: 1 }) }));
|
|
92
85
|
return results[0];
|
|
93
86
|
});
|
|
94
87
|
}
|
|
@@ -123,22 +116,33 @@ export default class Table {
|
|
|
123
116
|
});
|
|
124
117
|
}
|
|
125
118
|
/** Perform JOIN operations with other tables */
|
|
126
|
-
Join(
|
|
119
|
+
Join(queryLayers) {
|
|
127
120
|
return __awaiter(this, void 0, void 0, function* () {
|
|
128
|
-
|
|
121
|
+
var _a, _b;
|
|
122
|
+
if (queryLayers.base.joins === undefined || (Array.isArray(queryLayers.base.joins) && queryLayers.base.joins.length === 0)) {
|
|
123
|
+
throw new Error("No joins defined for the Join operation.");
|
|
124
|
+
}
|
|
125
|
+
const joinedTables = queryLayers.base.joins.map(j => j.fromTable);
|
|
126
|
+
const tableColumnCache = new Map();
|
|
127
|
+
const columnInfo = yield this._query.TableColumnInformation(this._name);
|
|
128
|
+
tableColumnCache.set(this._name, columnInfo);
|
|
129
|
+
for (const tableName of joinedTables) {
|
|
130
|
+
const columnInfo = yield this._query.TableColumnInformation(tableName);
|
|
131
|
+
tableColumnCache.set(tableName, columnInfo);
|
|
132
|
+
}
|
|
133
|
+
const builder = new QueryStatementBuilder(queryLayers, tableColumnCache);
|
|
134
|
+
const queryString = yield builder.build();
|
|
129
135
|
let params = {};
|
|
130
|
-
if (
|
|
131
|
-
params =
|
|
136
|
+
if ((_a = queryLayers === null || queryLayers === void 0 ? void 0 : queryLayers.base) === null || _a === void 0 ? void 0 : _a.where)
|
|
137
|
+
params = this.QueryHelperObject.ConvertParamsToObject(queryLayers.base.where);
|
|
138
|
+
if ((_b = queryLayers === null || queryLayers === void 0 ? void 0 : queryLayers.pretty) === null || _b === void 0 ? void 0 : _b.where)
|
|
139
|
+
params = Object.assign(Object.assign({}, params), this.QueryHelperObject.ConvertParamsToObject(queryLayers.pretty.where));
|
|
132
140
|
const query = this._queryFactory({
|
|
133
141
|
tableName: this._name,
|
|
134
142
|
query: queryString,
|
|
135
143
|
parameters: params,
|
|
136
144
|
recordFactory: this._recordFactory
|
|
137
145
|
});
|
|
138
|
-
const joinedTables = Array.isArray(Joins) ? Joins.map(j => j.fromTable) : [Joins.fromTable];
|
|
139
|
-
if (options) {
|
|
140
|
-
options.select = joinedTables.map(table => `${table}.*`).join(', ');
|
|
141
|
-
}
|
|
142
146
|
const records = yield query.All();
|
|
143
147
|
const splitTables = yield this.splitJoinValues(records, joinedTables);
|
|
144
148
|
return splitTables;
|
|
@@ -147,13 +151,11 @@ export default class Table {
|
|
|
147
151
|
splitJoinValues(records, joinedTables) {
|
|
148
152
|
return __awaiter(this, void 0, void 0, function* () {
|
|
149
153
|
return records.map(record => {
|
|
154
|
+
var _a;
|
|
150
155
|
if (!record.values)
|
|
151
156
|
return record;
|
|
152
157
|
const mainTableData = {};
|
|
153
158
|
const joinedTableData = {};
|
|
154
|
-
for (const tableName of joinedTables) {
|
|
155
|
-
joinedTableData[tableName] = {};
|
|
156
|
-
}
|
|
157
159
|
for (const [aliasedKey, value] of Object.entries(record.values)) {
|
|
158
160
|
if (aliasedKey.includes('__')) {
|
|
159
161
|
const [tableName, columnName] = aliasedKey.split('__');
|
|
@@ -161,6 +163,7 @@ export default class Table {
|
|
|
161
163
|
mainTableData[columnName] = value;
|
|
162
164
|
}
|
|
163
165
|
else if (joinedTables.includes(tableName)) {
|
|
166
|
+
(_a = joinedTableData[tableName]) !== null && _a !== void 0 ? _a : (joinedTableData[tableName] = {});
|
|
164
167
|
joinedTableData[tableName][columnName] = value;
|
|
165
168
|
}
|
|
166
169
|
}
|
|
@@ -168,10 +171,32 @@ export default class Table {
|
|
|
168
171
|
mainTableData[aliasedKey] = value;
|
|
169
172
|
}
|
|
170
173
|
}
|
|
171
|
-
|
|
172
|
-
|
|
174
|
+
const filteredJoinedData = Object.fromEntries(
|
|
175
|
+
// eslint-disable-next-line no-unused-vars
|
|
176
|
+
Object.entries(joinedTableData).filter(([_, data]) => Object.keys(data).length > 0));
|
|
177
|
+
const combinedData = Object.assign(Object.assign({}, mainTableData), filteredJoinedData);
|
|
173
178
|
return this._recordFactory(this._name, combinedData, this._customAdapter);
|
|
174
179
|
});
|
|
175
180
|
});
|
|
176
181
|
}
|
|
182
|
+
toSql(queryLayers) {
|
|
183
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
184
|
+
if (queryLayers.base.joins && queryLayers.base.joins.length > 0) {
|
|
185
|
+
const joinedTables = queryLayers.base.joins.map(j => j.fromTable);
|
|
186
|
+
const tableColumnCache = new Map();
|
|
187
|
+
const columnInfo = yield this._query.TableColumnInformation(this._name);
|
|
188
|
+
tableColumnCache.set(this._name, columnInfo);
|
|
189
|
+
for (const tableName of joinedTables) {
|
|
190
|
+
const columnInfo = yield this._query.TableColumnInformation(tableName);
|
|
191
|
+
tableColumnCache.set(tableName, columnInfo);
|
|
192
|
+
}
|
|
193
|
+
const builder = new QueryStatementBuilder(queryLayers, tableColumnCache);
|
|
194
|
+
return yield builder.build();
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
const builder = new QueryStatementBuilder(queryLayers);
|
|
198
|
+
return yield builder.build();
|
|
199
|
+
}
|
|
200
|
+
});
|
|
201
|
+
}
|
|
177
202
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AdapterNotFoundError.d.ts","sourceRoot":"","sources":["../../../../src/helpers/Errors/AdapterErrors/AdapterNotFoundError.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,oBAAqB,SAAQ,KAAK;gBACvC,WAAW,EAAE,MAAM;CAIlC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ConnectionFailedError.d.ts","sourceRoot":"","sources":["../../../../src/helpers/Errors/AdapterErrors/ConnectionFailedError.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,KAAK;gBACxC,OAAO,EAAE,MAAM;CAI9B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NoDefaultAdapterError.d.ts","sourceRoot":"","sources":["../../../../src/helpers/Errors/AdapterErrors/NoDefaultAdapterError.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,qBAAsB,SAAQ,KAAK;;CAKvD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InvalidExpressionParametersError.d.ts","sourceRoot":"","sources":["../../../../src/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,OAAO,OAAO,gCAAiC,SAAQ,KAAK;gBACnD,OAAO,EAAE,MAAM;CAI9B"}
|