@iamkirbki/database-handler-core 4.4.7 → 4.4.8
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 +2 -2
- package/dist/abstract/Controller.d.ts.map +1 -1
- package/dist/abstract/Controller.js +1 -1
- package/dist/abstract/Model.d.ts +10 -9
- package/dist/abstract/Model.d.ts.map +1 -1
- package/dist/abstract/Model.js +92 -70
- package/dist/abstract/SchemaTableBuilder.d.ts +1 -1
- package/dist/abstract/SchemaTableBuilder.d.ts.map +1 -1
- package/dist/abstract/SchemaTableBuilder.js +1 -1
- package/dist/abstract/model/ModelRelation.d.ts +5 -4
- package/dist/abstract/model/ModelRelation.d.ts.map +1 -1
- package/dist/abstract/model/ModelRelation.js +35 -27
- package/dist/base/Query.d.ts +4 -8
- package/dist/base/Query.d.ts.map +1 -1
- package/dist/base/Query.js +91 -73
- package/dist/base/Record.d.ts +5 -4
- package/dist/base/Record.d.ts.map +1 -1
- package/dist/base/Record.js +44 -23
- package/dist/base/Table.d.ts +17 -14
- package/dist/base/Table.d.ts.map +1 -1
- package/dist/base/Table.js +123 -89
- package/dist/factories/ModelFactory.d.ts +7 -0
- package/dist/factories/ModelFactory.d.ts.map +1 -0
- package/dist/factories/ModelFactory.js +6 -0
- package/dist/factories/QueryFactory.d.ts +7 -0
- package/dist/factories/QueryFactory.d.ts.map +1 -0
- package/dist/factories/QueryFactory.js +6 -0
- package/dist/factories/RecordFactory.d.ts +8 -0
- package/dist/factories/RecordFactory.d.ts.map +1 -0
- package/dist/factories/RecordFactory.js +6 -0
- package/dist/factories/TableFactory.d.ts +7 -0
- package/dist/factories/TableFactory.d.ts.map +1 -0
- package/dist/factories/TableFactory.js +6 -0
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts +2 -1
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.d.ts.map +1 -1
- package/dist/helpers/Errors/AdapterErrors/AdapterNotFoundError.js +3 -3
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts +2 -1
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.d.ts.map +1 -1
- package/dist/helpers/Errors/AdapterErrors/ConnectionFailedError.js +3 -3
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts +2 -1
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.d.ts.map +1 -1
- package/dist/helpers/Errors/AdapterErrors/NoDefaultAdapterError.js +3 -3
- package/dist/helpers/Errors/DatabaseHandlerError.d.ts +9 -0
- package/dist/helpers/Errors/DatabaseHandlerError.d.ts.map +1 -0
- package/dist/helpers/Errors/DatabaseHandlerError.js +10 -0
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts +2 -1
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.d.ts.map +1 -1
- package/dist/helpers/Errors/ExpressionErrors/InvalidExpressionParametersError.js +5 -3
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts +2 -1
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.d.ts.map +1 -1
- package/dist/helpers/Errors/ExpressionErrors/UnknownExpressionTypeError.js +5 -3
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts +2 -1
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.d.ts.map +1 -1
- package/dist/helpers/Errors/ExpressionErrors/UnsupportedQueryPhaseError.js +3 -3
- package/dist/helpers/Errors/ModelErrors/InvalidOperationError.d.ts +5 -0
- package/dist/helpers/Errors/ModelErrors/InvalidOperationError.d.ts.map +1 -0
- package/dist/helpers/Errors/ModelErrors/InvalidOperationError.js +6 -0
- package/dist/helpers/Errors/ModelErrors/RecordNotFoundError.d.ts +6 -0
- package/dist/helpers/Errors/ModelErrors/RecordNotFoundError.d.ts.map +1 -0
- package/dist/helpers/Errors/ModelErrors/RecordNotFoundError.js +6 -0
- package/dist/helpers/Errors/ModelErrors/RelationError.d.ts +5 -0
- package/dist/helpers/Errors/ModelErrors/RelationError.d.ts.map +1 -0
- package/dist/helpers/Errors/ModelErrors/RelationError.js +6 -0
- package/dist/helpers/Errors/QueryErrors/InvalidWhereConditionError.d.ts +5 -0
- package/dist/helpers/Errors/QueryErrors/InvalidWhereConditionError.d.ts.map +1 -0
- package/dist/helpers/Errors/QueryErrors/InvalidWhereConditionError.js +6 -0
- package/dist/helpers/Errors/QueryErrors/QueryExecutionError.d.ts +5 -0
- package/dist/helpers/Errors/QueryErrors/QueryExecutionError.d.ts.map +1 -0
- package/dist/helpers/Errors/QueryErrors/QueryExecutionError.js +6 -0
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts +2 -1
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.d.ts.map +1 -1
- package/dist/helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js +3 -3
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts +3 -2
- package/dist/helpers/Errors/TableErrors/UnknownTableError.d.ts.map +1 -1
- package/dist/helpers/Errors/TableErrors/UnknownTableError.js +4 -4
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts +2 -2
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/BaseQueryBuilders/BaseSelectQueryBuilder.js +1 -1
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts +3 -4
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/ExpressionBuilders/JsonAggregateExpression.js +46 -44
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts +3 -4
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/ExpressionBuilders/SpatialDistanceExpression.js +34 -32
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts +3 -4
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/ExpressionBuilders/TextRelevanceExpression.js +9 -20
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts +4 -4
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/ExpressionDecorator.js +14 -17
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts +3 -3
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/GroupByDecorator.js +2 -2
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts +4 -4
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/JoinDecorator.js +17 -14
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts +3 -3
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/LimitDecorator.js +2 -2
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts +3 -3
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/OrderByDecorator.js +2 -2
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts +2 -2
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/QueryDecorator.js +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts +5 -5
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryDecorators/WhereDecorator.js +3 -4
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts +1 -3
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryExpressionBuilder.js +4 -22
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts +3 -4
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.d.ts.map +1 -1
- package/dist/helpers/QueryBuilders/QueryStatementBuilder.js +59 -45
- package/dist/helpers/QueryBuilders/{SqlRenderer.d.ts → SqlGenerator.d.ts} +3 -3
- package/dist/helpers/QueryBuilders/SqlGenerator.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/{SqlRenderer.js → SqlGenerator.js} +30 -19
- package/dist/helpers/QueryBuilders/{oldQueryStatementBuilder.d.ts → depricatedQueryStatementBuilder.d.ts} +19 -46
- package/dist/helpers/QueryBuilders/depricatedQueryStatementBuilder.d.ts.map +1 -0
- package/dist/helpers/QueryBuilders/{oldQueryStatementBuilder.js → depricatedQueryStatementBuilder.js} +31 -67
- package/dist/index.d.ts +13 -13
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -7
- package/dist/interfaces/IController.d.ts +1 -1
- package/dist/interfaces/IController.d.ts.map +1 -1
- package/dist/interfaces/IDatabaseAdapter.d.ts +2 -2
- package/dist/interfaces/IDatabaseAdapter.d.ts.map +1 -1
- package/dist/interfaces/IExpressionBuilder.d.ts +1 -2
- package/dist/interfaces/IExpressionBuilder.d.ts.map +1 -1
- package/dist/interfaces/IFactory.d.ts +4 -0
- package/dist/interfaces/IFactory.d.ts.map +1 -0
- package/dist/interfaces/IFactory.js +1 -0
- package/dist/interfaces/IMigration.d.ts +4 -4
- package/dist/interfaces/IMigration.d.ts.map +1 -1
- package/dist/interfaces/IQueryBuilder.d.ts +1 -1
- package/dist/interfaces/IQueryBuilder.d.ts.map +1 -1
- package/dist/interfaces/ISchemaBuilder.d.ts +2 -2
- package/dist/interfaces/ISchemaBuilder.d.ts.map +1 -1
- package/dist/interfaces/IStatementAdapter.d.ts +1 -1
- package/dist/interfaces/IStatementAdapter.d.ts.map +1 -1
- package/dist/runtime/Container.d.ts +2 -3
- package/dist/runtime/Container.d.ts.map +1 -1
- package/dist/runtime/Container.js +3 -4
- package/dist/runtime/QueryCache.d.ts +1 -1
- package/dist/runtime/QueryCache.d.ts.map +1 -1
- package/dist/runtime/QueryCache.js +1 -1
- package/dist/runtime/Repository.d.ts +23 -12
- package/dist/runtime/Repository.d.ts.map +1 -1
- package/dist/runtime/Repository.js +324 -65
- package/dist/types/expressions.d.ts +12 -47
- package/dist/types/expressions.d.ts.map +1 -1
- package/dist/types/expressions.js +1 -6
- package/dist/types/factories.d.ts +5 -3
- package/dist/types/factories.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -1
- package/dist/types/model.d.ts +5 -23
- package/dist/types/model.d.ts.map +1 -1
- package/dist/types/query.d.ts +5 -4
- package/dist/types/query.d.ts.map +1 -1
- package/dist/types/record.d.ts +11 -0
- package/dist/types/record.d.ts.map +1 -0
- package/dist/types/record.js +1 -0
- package/dist/types/repository.d.ts +8 -0
- package/dist/types/repository.d.ts.map +1 -0
- package/dist/types/repository.js +1 -0
- package/dist/types/table.d.ts +9 -1
- package/dist/types/table.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/helpers/QueryBuilders/SqlRenderer.d.ts.map +0 -1
- package/dist/helpers/QueryBuilders/oldQueryStatementBuilder.d.ts.map +0 -1
package/dist/base/Query.js
CHANGED
|
@@ -1,21 +1,44 @@
|
|
|
1
|
-
import { Container
|
|
2
|
-
import UnknownTableError from '../helpers/Errors/TableErrors/UnknownTableError.js';
|
|
3
|
-
import UnexpectedEmptyQueryError from '../helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js';
|
|
4
|
-
import
|
|
1
|
+
import { Container } from '../index.js';
|
|
2
|
+
import { UnknownTableError } from '../helpers/Errors/TableErrors/UnknownTableError.js';
|
|
3
|
+
import { UnexpectedEmptyQueryError } from '../helpers/Errors/QueryErrors/UnexpectedEmptyQueryError.js';
|
|
4
|
+
import { QueryExecutionError } from '../helpers/Errors/QueryErrors/QueryExecutionError.js';
|
|
5
|
+
import { QueryCache } from '../runtime/QueryCache.js';
|
|
6
|
+
import { RecordFactory } from '../factories/RecordFactory.js';
|
|
5
7
|
/** Query class for executing custom SQL queries */
|
|
6
|
-
export
|
|
8
|
+
export class Query {
|
|
7
9
|
get Parameters() {
|
|
8
10
|
return this._parameters;
|
|
9
11
|
}
|
|
10
|
-
|
|
12
|
+
serializeParameters(parameters) {
|
|
13
|
+
const serialized = {};
|
|
14
|
+
for (const [key, value] of Object.entries(parameters)) {
|
|
15
|
+
if (value !== null &&
|
|
16
|
+
typeof value === 'object' &&
|
|
17
|
+
!(value instanceof Date) &&
|
|
18
|
+
!Array.isArray(value)) {
|
|
19
|
+
serialized[key] = JSON.stringify(value);
|
|
20
|
+
}
|
|
21
|
+
else if (Array.isArray(value)) {
|
|
22
|
+
serialized[key] = JSON.stringify(value);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
serialized[key] = value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return serialized;
|
|
29
|
+
}
|
|
30
|
+
constructor({ tableName, query, parameters, adapterName, recordFactory = new RecordFactory(), }) {
|
|
11
31
|
this._parameters = {};
|
|
12
32
|
this.TableName = tableName;
|
|
13
33
|
this._query = query;
|
|
14
34
|
if (parameters)
|
|
15
|
-
this._parameters = this.
|
|
16
|
-
// eslint-disable-next-line no-undef
|
|
35
|
+
this._parameters = this.serializeParameters(parameters);
|
|
17
36
|
if (Container.getInstance().logging)
|
|
18
|
-
this._query
|
|
37
|
+
this._query
|
|
38
|
+
// eslint-disable-next-line no-undef
|
|
39
|
+
? console.info(this._query, '\n', this._parameters)
|
|
40
|
+
// eslint-disable-next-line no-undef
|
|
41
|
+
: console.info('No query found, probably checking if a table exists or getting the table column information.');
|
|
19
42
|
this._adapter = Container.getInstance().getAdapter(adapterName);
|
|
20
43
|
this._queryCache = QueryCache.getInstance();
|
|
21
44
|
this._recordFactory = recordFactory;
|
|
@@ -35,97 +58,92 @@ export default class Query {
|
|
|
35
58
|
if (!this._query) {
|
|
36
59
|
throw new UnexpectedEmptyQueryError();
|
|
37
60
|
}
|
|
38
|
-
|
|
39
|
-
|
|
61
|
+
try {
|
|
62
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
63
|
+
return (await stmt.run(this.Parameters));
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new QueryExecutionError(this._query, error);
|
|
67
|
+
}
|
|
40
68
|
}
|
|
41
69
|
/** Execute a SELECT query and return all matching rows */
|
|
42
70
|
async All() {
|
|
43
71
|
await this.throwIfTableNotExists();
|
|
44
72
|
if (!this._query) {
|
|
45
|
-
throw new
|
|
73
|
+
throw new UnexpectedEmptyQueryError();
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
77
|
+
const results = (await stmt.all(this.Parameters));
|
|
78
|
+
return results.map((res) => this._recordFactory.create({
|
|
79
|
+
table: this.TableName,
|
|
80
|
+
values: res,
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
catch (error) {
|
|
84
|
+
throw new QueryExecutionError(this._query, error);
|
|
46
85
|
}
|
|
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));
|
|
50
86
|
}
|
|
51
87
|
/** Execute a SELECT query and return the first matching row */
|
|
52
88
|
async Get() {
|
|
53
89
|
await this.throwIfTableNotExists();
|
|
54
90
|
if (!this._query) {
|
|
55
|
-
throw new
|
|
91
|
+
throw new UnexpectedEmptyQueryError();
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
95
|
+
const results = (await stmt.get(this.Parameters));
|
|
96
|
+
return results
|
|
97
|
+
? this._recordFactory.create({
|
|
98
|
+
table: this.TableName,
|
|
99
|
+
values: results,
|
|
100
|
+
})
|
|
101
|
+
: undefined;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
throw new QueryExecutionError(this._query, error);
|
|
56
105
|
}
|
|
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;
|
|
62
106
|
}
|
|
63
107
|
async TableColumnInformation(tableName) {
|
|
64
108
|
let tableColumnInfo = this._queryCache.getTableColumnInformation(tableName);
|
|
65
109
|
if (tableColumnInfo)
|
|
66
110
|
return tableColumnInfo;
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
111
|
+
try {
|
|
112
|
+
tableColumnInfo = await this._adapter.tableColumnInformation(tableName);
|
|
113
|
+
this._queryCache.setTableColumnInformation(tableName, tableColumnInfo);
|
|
114
|
+
return tableColumnInfo;
|
|
115
|
+
}
|
|
116
|
+
catch (error) {
|
|
117
|
+
throw new QueryExecutionError(`TableColumnInformation for ${tableName}`, error);
|
|
118
|
+
}
|
|
70
119
|
}
|
|
71
120
|
async DoesTableExist() {
|
|
72
121
|
if (this._queryCache.doesTableExist(this.TableName)) {
|
|
73
122
|
return true;
|
|
74
123
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
124
|
+
try {
|
|
125
|
+
const exists = await this._adapter.tableExists(this.TableName);
|
|
126
|
+
if (exists) {
|
|
127
|
+
this._queryCache.addExistingTable(this.TableName);
|
|
128
|
+
}
|
|
129
|
+
return exists;
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
throw new QueryExecutionError(`DoesTableExist for ${this.TableName}`, error);
|
|
78
133
|
}
|
|
79
|
-
return exists;
|
|
80
134
|
}
|
|
81
135
|
async Count() {
|
|
82
136
|
await this.throwIfTableNotExists();
|
|
83
137
|
if (!this._query) {
|
|
84
|
-
throw new
|
|
138
|
+
throw new UnexpectedEmptyQueryError();
|
|
139
|
+
}
|
|
140
|
+
try {
|
|
141
|
+
const stmt = await this._adapter.prepare(this._query);
|
|
142
|
+
const result = (await stmt.get(this.Parameters));
|
|
143
|
+
return parseInt(result.count) || 0;
|
|
144
|
+
}
|
|
145
|
+
catch (error) {
|
|
146
|
+
throw new QueryExecutionError(this._query, error);
|
|
85
147
|
}
|
|
86
|
-
const stmt = await this._adapter.prepare(this._query);
|
|
87
|
-
const result = (await stmt.get(this.Parameters));
|
|
88
|
-
return parseInt(result.count) || 0;
|
|
89
|
-
}
|
|
90
|
-
ConvertParamsToArray(params) {
|
|
91
|
-
const paramArray = [];
|
|
92
|
-
if (Array.isArray(params)) {
|
|
93
|
-
return params;
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
Object.entries(params).forEach(([key, value]) => {
|
|
97
|
-
return paramArray.push({
|
|
98
|
-
column: key,
|
|
99
|
-
operator: '=',
|
|
100
|
-
value,
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
}
|
|
104
|
-
return paramArray;
|
|
105
|
-
}
|
|
106
|
-
/** Convert various parameter formats to a consistent object format */
|
|
107
|
-
ConvertParamsToObject(params) {
|
|
108
|
-
const paramObject = {};
|
|
109
|
-
if (Array.isArray(params)) {
|
|
110
|
-
params.forEach((param) => {
|
|
111
|
-
paramObject[param.column] = param.value;
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
Object.assign(paramObject, params);
|
|
116
|
-
}
|
|
117
|
-
return this.ConvertValueToString(paramObject);
|
|
118
|
-
}
|
|
119
|
-
/** Databases don't like numeric values when inserting with a query */
|
|
120
|
-
ConvertValueToString(params) {
|
|
121
|
-
return Object.entries(params)
|
|
122
|
-
.map(([key, value]) => {
|
|
123
|
-
return {
|
|
124
|
-
[key]: value !== null && !(value instanceof Date) && value !== undefined
|
|
125
|
-
? value.toString()
|
|
126
|
-
: value,
|
|
127
|
-
};
|
|
128
|
-
})
|
|
129
|
-
.reduce((acc, curr) => ({ ...acc, ...curr }), {});
|
|
130
148
|
}
|
|
131
149
|
}
|
package/dist/base/Record.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { inspect } from
|
|
2
|
-
import { columnType, QueryIsEqualParameter
|
|
1
|
+
import { inspect } from 'util';
|
|
2
|
+
import { columnType, QueryIsEqualParameter } from '../types/index.js';
|
|
3
|
+
import { RecordConstructorType } from '../types/record.js';
|
|
3
4
|
/** Record class represents a single database row */
|
|
4
|
-
export
|
|
5
|
+
export declare class Record<ColumnValuesType extends columnType> {
|
|
5
6
|
private _values;
|
|
6
7
|
private readonly _tableName;
|
|
7
8
|
private readonly _customAdapter?;
|
|
8
9
|
private readonly _queryFactory;
|
|
9
10
|
private readonly _recordFactory;
|
|
10
|
-
constructor(table
|
|
11
|
+
constructor({ table, values, adapter, queryFactory, recordFactory, }: RecordConstructorType<ColumnValuesType>);
|
|
11
12
|
/** Get the raw values object for this record */
|
|
12
13
|
get values(): ColumnValuesType;
|
|
13
14
|
Insert(): Promise<this | undefined>;
|
|
@@ -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;
|
|
1
|
+
{"version":3,"file":"Record.d.ts","sourceRoot":"","sources":["../../src/base/Record.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,EACL,UAAU,EAGV,qBAAqB,EAEtB,MAAM,sBAAsB,CAAC;AAK9B,OAAO,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAG9D,oDAAoD;AACpD,qBAAa,MAAM,CAAC,gBAAgB,SAAS,UAAU;IACrD,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,CAAkC;gBAErD,EACV,KAAK,EACL,MAAM,EACN,OAAO,EACP,YAAiC,EACjC,aAAqD,GACtD,EAAE,qBAAqB,CAAC,gBAAgB,CAAC;IAQ1C,gDAAgD;IAChD,IAAW,MAAM,IAAI,gBAAgB,CAEpC;IAEY,MAAM,IAAI,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC;IAoEhD,yCAAyC;IAC5B,MAAM,CACjB,SAAS,EAAE,OAAO,CAAC,gBAAgB,CAAC,EACpC,eAAe,EAAE,qBAAqB,GACrC,OAAO,CAAC,IAAI,CAAC;IA0ChB,2CAA2C;IAC9B,MAAM,CAAC,UAAU,CAAC,EAAE,qBAAqB,GAAG,OAAO,CAAC,IAAI,CAAC;IAoCtE,qDAAqD;IAC9C,MAAM,IAAI,gBAAgB;IAIjC,mDAAmD;IAC5C,QAAQ,IAAI,MAAM;IAIzB,uCAAuC;IACvC,CAAC,OAAO,CAAC,MAAM,CAAC,IAAI,gBAAgB;CAGrC"}
|
package/dist/base/Record.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
|
-
import { inspect } from
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
1
|
+
import { inspect } from 'util';
|
|
2
|
+
import { QueryStatementBuilder } from '../helpers/QueryBuilders/QueryStatementBuilder.js';
|
|
3
|
+
import { DepricatedQueryStatementBuilder } from '../helpers/QueryBuilders/depricatedQueryStatementBuilder.js';
|
|
4
|
+
import { QueryFactory } from '../factories/QueryFactory.js';
|
|
5
|
+
import { RecordFactory } from '../factories/RecordFactory.js';
|
|
6
|
+
import { InvalidOperationError } from '../helpers/Errors/ModelErrors/InvalidOperationError.js';
|
|
5
7
|
/** Record class represents a single database row */
|
|
6
|
-
export
|
|
7
|
-
constructor(table, values, adapter, queryFactory =
|
|
8
|
+
export class Record {
|
|
9
|
+
constructor({ table, values, adapter, queryFactory = new QueryFactory(), recordFactory = new RecordFactory(), }) {
|
|
8
10
|
this._values = {};
|
|
9
11
|
this._tableName = table;
|
|
10
12
|
this._values = values;
|
|
@@ -16,26 +18,26 @@ export default class Record {
|
|
|
16
18
|
get values() {
|
|
17
19
|
return this._values;
|
|
18
20
|
}
|
|
19
|
-
;
|
|
20
21
|
async Insert() {
|
|
21
22
|
var _a, _b;
|
|
22
23
|
const columns = Object.keys(this._values);
|
|
23
24
|
if (columns.length === 0) {
|
|
24
|
-
throw new
|
|
25
|
+
throw new InvalidOperationError('Cannot insert record with no columns');
|
|
25
26
|
}
|
|
26
|
-
const queryStr = await
|
|
27
|
-
const query = this._queryFactory({
|
|
27
|
+
const queryStr = await DepricatedQueryStatementBuilder.BuildInsert(this._tableName, this._values);
|
|
28
|
+
const query = this._queryFactory.create({
|
|
28
29
|
tableName: this._tableName,
|
|
29
30
|
query: queryStr,
|
|
30
31
|
parameters: this._values,
|
|
31
32
|
adapterName: this._customAdapter,
|
|
32
|
-
recordFactory: this._recordFactory
|
|
33
|
+
recordFactory: this._recordFactory,
|
|
33
34
|
});
|
|
34
35
|
const result = await query.Run();
|
|
35
36
|
let recordId;
|
|
36
37
|
// For PostgreSQL compatibility: use 'id' from values if lastInsertRowid is undefined
|
|
37
38
|
if (Array.isArray(this._values)) {
|
|
38
|
-
recordId =
|
|
39
|
+
recordId =
|
|
40
|
+
(_a = result === null || result === void 0 ? void 0 : result.lastInsertRowid) !== null && _a !== void 0 ? _a : this._values.map((v) => (v.column === 'id' ? v.value : undefined));
|
|
39
41
|
}
|
|
40
42
|
else {
|
|
41
43
|
recordId = (_b = result === null || result === void 0 ? void 0 : result.lastInsertRowid) !== null && _b !== void 0 ? _b : this._values.id;
|
|
@@ -43,14 +45,21 @@ export default class Record {
|
|
|
43
45
|
if (recordId === undefined) {
|
|
44
46
|
return undefined;
|
|
45
47
|
}
|
|
46
|
-
const
|
|
48
|
+
const whereParams = Object.entries(this._values).map(([column, value]) => ({
|
|
49
|
+
column,
|
|
50
|
+
operator: '=',
|
|
51
|
+
value,
|
|
52
|
+
}));
|
|
53
|
+
const builder = new QueryStatementBuilder({
|
|
54
|
+
base: { from: this._tableName, where: whereParams },
|
|
55
|
+
});
|
|
47
56
|
const queryStrSelect = await builder.build();
|
|
48
|
-
const querySelect = this._queryFactory({
|
|
57
|
+
const querySelect = this._queryFactory.create({
|
|
49
58
|
tableName: this._tableName,
|
|
50
59
|
query: queryStrSelect,
|
|
51
60
|
parameters: this._values,
|
|
52
61
|
adapterName: this._customAdapter,
|
|
53
|
-
recordFactory: this._recordFactory
|
|
62
|
+
recordFactory: this._recordFactory,
|
|
54
63
|
});
|
|
55
64
|
const insertedRecord = await querySelect.All();
|
|
56
65
|
if (insertedRecord.length > 0) {
|
|
@@ -63,20 +72,26 @@ export default class Record {
|
|
|
63
72
|
async Update(newValues, whereParameters) {
|
|
64
73
|
const originalValues = this._values;
|
|
65
74
|
if (originalValues.updated_at !== undefined) {
|
|
66
|
-
newValues.updated_at =
|
|
75
|
+
newValues.updated_at =
|
|
76
|
+
new Date().toISOString();
|
|
67
77
|
}
|
|
68
|
-
const
|
|
78
|
+
const whereParams = Object.entries(whereParameters).map(([column, value]) => ({
|
|
79
|
+
column,
|
|
80
|
+
operator: '=',
|
|
81
|
+
value,
|
|
82
|
+
}));
|
|
83
|
+
const queryStr = await DepricatedQueryStatementBuilder.BuildUpdate(this._tableName, newValues, whereParams);
|
|
69
84
|
// Merge newValues and originalValues for parameters (with 'where_' prefix for where clause)
|
|
70
85
|
const params = { ...newValues };
|
|
71
86
|
Object.entries(originalValues).forEach(([key, value]) => {
|
|
72
87
|
params[`where_${key}`] = value;
|
|
73
88
|
});
|
|
74
|
-
const _query = this._queryFactory({
|
|
89
|
+
const _query = this._queryFactory.create({
|
|
75
90
|
tableName: this._tableName,
|
|
76
91
|
query: queryStr,
|
|
77
92
|
parameters: params,
|
|
78
93
|
adapterName: this._customAdapter,
|
|
79
|
-
recordFactory: this._recordFactory
|
|
94
|
+
recordFactory: this._recordFactory,
|
|
80
95
|
});
|
|
81
96
|
await _query.Run();
|
|
82
97
|
this._values = { ...this._values, ...newValues };
|
|
@@ -86,17 +101,23 @@ export default class Record {
|
|
|
86
101
|
async Delete(primaryKey) {
|
|
87
102
|
const originalValues = this._values;
|
|
88
103
|
if (originalValues.deleted_at !== undefined) {
|
|
89
|
-
this._values.deleted_at =
|
|
104
|
+
this._values.deleted_at =
|
|
105
|
+
new Date().toISOString();
|
|
90
106
|
await this.Update(this._values, this._values.id ? { id: this._values.id } : primaryKey || {});
|
|
91
107
|
return;
|
|
92
108
|
}
|
|
93
|
-
const
|
|
94
|
-
|
|
109
|
+
const whereParams = Object.entries(this._values).map(([column, value]) => ({
|
|
110
|
+
column,
|
|
111
|
+
operator: '=',
|
|
112
|
+
value,
|
|
113
|
+
}));
|
|
114
|
+
const queryStr = await DepricatedQueryStatementBuilder.BuildDelete(this._tableName, whereParams);
|
|
115
|
+
const _query = this._queryFactory.create({
|
|
95
116
|
tableName: this._tableName,
|
|
96
117
|
query: queryStr,
|
|
97
118
|
parameters: this.values,
|
|
98
119
|
adapterName: this._customAdapter,
|
|
99
|
-
recordFactory: this._recordFactory
|
|
120
|
+
recordFactory: this._recordFactory,
|
|
100
121
|
});
|
|
101
122
|
await _query.Run();
|
|
102
123
|
}
|
package/dist/base/Table.d.ts
CHANGED
|
@@ -1,32 +1,35 @@
|
|
|
1
|
-
import { ReadableTableColumnInfo, TableColumnInfo, columnType,
|
|
2
|
-
import { Record, Query } from
|
|
1
|
+
import { ReadableTableColumnInfo, TableColumnInfo, columnType, QueryLayers, TableConstructorType } from '../types/index.js';
|
|
2
|
+
import { Record, Query } from '../index.js';
|
|
3
3
|
/** Table class for interacting with a database table */
|
|
4
|
-
export
|
|
4
|
+
export declare class Table {
|
|
5
5
|
private readonly _query;
|
|
6
|
-
private readonly
|
|
6
|
+
private readonly _adapter?;
|
|
7
7
|
private readonly _name;
|
|
8
8
|
private readonly _queryFactory;
|
|
9
9
|
private readonly _recordFactory;
|
|
10
|
-
/** Private constructor - use
|
|
11
|
-
constructor(name
|
|
12
|
-
get
|
|
10
|
+
/** Private constructor - use TableFactory.create() */
|
|
11
|
+
constructor({ name, adapter, queryFactory, recordFactory, }: TableConstructorType);
|
|
12
|
+
get QueryHelperClient(): Query;
|
|
13
13
|
/** Get raw column information */
|
|
14
14
|
TableColumnInformation(tableName?: string): Promise<TableColumnInfo[]>;
|
|
15
15
|
/** Get readable, formatted column information */
|
|
16
16
|
ReadableTableColumnInformation(): Promise<ReadableTableColumnInfo[]>;
|
|
17
|
-
|
|
17
|
+
exists(): Promise<boolean>;
|
|
18
18
|
/** Fetch records with optional filtering, ordering, and pagination */
|
|
19
|
-
|
|
19
|
+
FetchRecords<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type>[]>;
|
|
20
20
|
/** Fetch a single record from the table */
|
|
21
|
-
|
|
21
|
+
FetchSingleRecord<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type> | undefined>;
|
|
22
22
|
/** Get the total count of records */
|
|
23
23
|
RecordsCount(): Promise<number>;
|
|
24
|
-
exists(): Promise<boolean>;
|
|
25
24
|
/** Insert a record into the table */
|
|
26
|
-
|
|
25
|
+
CreateRecord<Type extends columnType>(values: Type): Promise<Record<Type> | undefined>;
|
|
26
|
+
Drop(): Promise<void>;
|
|
27
27
|
/** Perform JOIN operations with other tables */
|
|
28
|
-
|
|
29
|
-
private splitJoinValues;
|
|
28
|
+
FetchJoined<Type extends columnType>(queryLayers: QueryLayers): Promise<Record<Type>[]>;
|
|
30
29
|
toSql(queryLayers: QueryLayers): Promise<string>;
|
|
30
|
+
private buildSelectQuery;
|
|
31
|
+
private extractWhereParameters;
|
|
32
|
+
private fetchSchemaInformation;
|
|
33
|
+
private mapJoinedResults;
|
|
31
34
|
}
|
|
32
35
|
//# 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,
|
|
1
|
+
{"version":3,"file":"Table.d.ts","sourceRoot":"","sources":["../../src/base/Table.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,uBAAuB,EACvB,eAAe,EACf,UAAU,EACV,WAAW,EACX,oBAAoB,EAErB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAK/C,wDAAwD;AACxD,qBAAa,KAAK;IAChB,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAQ;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAS;IACnC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAS;IAC/B,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAe;IAC7C,OAAO,CAAC,QAAQ,CAAC,cAAc,CAA4B;IAM3D,sDAAsD;gBAC1C,EACV,IAAI,EACJ,OAAO,EACP,YAAiC,EACjC,aAA+C,GAChD,EAAE,oBAAoB;IAiBvB,IAAW,iBAAiB,IAAI,KAAK,CAEpC;IAMD,iCAAiC;IACpB,sBAAsB,CACjC,SAAS,CAAC,EAAE,MAAM,GACjB,OAAO,CAAC,eAAe,EAAE,CAAC;IAI7B,iDAAiD;IACpC,8BAA8B,IAAI,OAAO,CACpD,uBAAuB,EAAE,CAC1B;IAWY,MAAM,IAAI,OAAO,CAAC,OAAO,CAAC;IAcvC,sEAAsE;IACzD,YAAY,CAAC,IAAI,SAAS,UAAU,EAC/C,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IAc1B,2CAA2C;IAC9B,iBAAiB,CAAC,IAAI,SAAS,UAAU,EACpD,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAYpC,qCAAqC;IACxB,YAAY,IAAI,OAAO,CAAC,MAAM,CAAC;IAc5C,qCAAqC;IACxB,YAAY,CAAC,IAAI,SAAS,UAAU,EAC/C,MAAM,EAAE,IAAI,GACX,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC;IAUvB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAelC,gDAAgD;IACnC,WAAW,CAAC,IAAI,SAAS,UAAU,EAC9C,WAAW,EAAE,WAAW,GACvB,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,EAAE,CAAC;IAuCb,KAAK,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;YAe/C,gBAAgB;IAQ9B,OAAO,CAAC,sBAAsB;YAoBhB,sBAAsB;YAgBtB,gBAAgB;CA4C/B"}
|