@iamkirbki/database-handler-core 4.4.3 → 4.4.5
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.d.ts +4 -4
- package/dist/abstract/model/ModelRelation.d.ts.map +1 -1
- package/dist/abstract/model/ModelRelation.js +47 -49
- package/dist/base/Query.js +56 -79
- package/dist/base/Record.js +75 -90
- package/dist/base/Table.d.ts.map +1 -1
- package/dist/base/Table.js +130 -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 +98 -102
- package/dist/types/model.d.ts +3 -0
- package/dist/types/model.d.ts.map +1 -1
- package/dist/types/table.d.ts +1 -0
- package/dist/types/table.d.ts.map +1 -1
- package/package.json +1 -1
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
|
}
|
package/dist/base/Record.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 { inspect } from "util";
|
|
11
2
|
import Query from "./Query.js";
|
|
12
3
|
import QueryStatementBuilder from "../helpers/QueryBuilders/QueryStatementBuilder.js";
|
|
@@ -26,94 +17,88 @@ export default class Record {
|
|
|
26
17
|
return this._values;
|
|
27
18
|
}
|
|
28
19
|
;
|
|
29
|
-
Insert() {
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
recordFactory: this._recordFactory
|
|
43
|
-
});
|
|
44
|
-
const result = yield query.Run();
|
|
45
|
-
let recordId;
|
|
46
|
-
// For PostgreSQL compatibility: use 'id' from values if lastInsertRowid is undefined
|
|
47
|
-
if (Array.isArray(this._values)) {
|
|
48
|
-
recordId = (_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);
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
recordId = (_b = result === null || result === void 0 ? void 0 : result.lastInsertRowid) !== null && _b !== void 0 ? _b : this._values.id;
|
|
52
|
-
}
|
|
53
|
-
if (recordId === undefined) {
|
|
54
|
-
return undefined;
|
|
55
|
-
}
|
|
56
|
-
const builder = new QueryStatementBuilder({ base: { from: this._tableName, where: Object.assign({}, this._values) } });
|
|
57
|
-
const queryStrSelect = yield builder.build();
|
|
58
|
-
const querySelect = this._queryFactory({
|
|
59
|
-
tableName: this._tableName,
|
|
60
|
-
query: queryStrSelect,
|
|
61
|
-
parameters: this._values,
|
|
62
|
-
adapterName: this._customAdapter,
|
|
63
|
-
recordFactory: this._recordFactory
|
|
64
|
-
});
|
|
65
|
-
const insertedRecord = yield querySelect.All();
|
|
66
|
-
if (insertedRecord.length > 0) {
|
|
67
|
-
this._values = insertedRecord[insertedRecord.length - 1].values;
|
|
68
|
-
}
|
|
69
|
-
this._values = Object.assign(Object.assign({}, this._values), { id: recordId });
|
|
70
|
-
return this;
|
|
20
|
+
async Insert() {
|
|
21
|
+
var _a, _b;
|
|
22
|
+
const columns = Object.keys(this._values);
|
|
23
|
+
if (columns.length === 0) {
|
|
24
|
+
throw new Error("Cannot insert record with no columns");
|
|
25
|
+
}
|
|
26
|
+
const queryStr = await oldQueryStatementBuilder.BuildInsert(this._tableName, this._values);
|
|
27
|
+
const query = this._queryFactory({
|
|
28
|
+
tableName: this._tableName,
|
|
29
|
+
query: queryStr,
|
|
30
|
+
parameters: this._values,
|
|
31
|
+
adapterName: this._customAdapter,
|
|
32
|
+
recordFactory: this._recordFactory
|
|
71
33
|
});
|
|
34
|
+
const result = await query.Run();
|
|
35
|
+
let recordId;
|
|
36
|
+
// For PostgreSQL compatibility: use 'id' from values if lastInsertRowid is undefined
|
|
37
|
+
if (Array.isArray(this._values)) {
|
|
38
|
+
recordId = (_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
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
recordId = (_b = result === null || result === void 0 ? void 0 : result.lastInsertRowid) !== null && _b !== void 0 ? _b : this._values.id;
|
|
42
|
+
}
|
|
43
|
+
if (recordId === undefined) {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
const builder = new QueryStatementBuilder({ base: { from: this._tableName, where: { ...this._values } } });
|
|
47
|
+
const queryStrSelect = await builder.build();
|
|
48
|
+
const querySelect = this._queryFactory({
|
|
49
|
+
tableName: this._tableName,
|
|
50
|
+
query: queryStrSelect,
|
|
51
|
+
parameters: this._values,
|
|
52
|
+
adapterName: this._customAdapter,
|
|
53
|
+
recordFactory: this._recordFactory
|
|
54
|
+
});
|
|
55
|
+
const insertedRecord = await querySelect.All();
|
|
56
|
+
if (insertedRecord.length > 0) {
|
|
57
|
+
this._values = insertedRecord[insertedRecord.length - 1].values;
|
|
58
|
+
}
|
|
59
|
+
this._values = { ...this._values, id: recordId };
|
|
60
|
+
return this;
|
|
72
61
|
}
|
|
73
62
|
/** Update this record in the database */
|
|
74
|
-
Update(newValues, whereParameters) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
recordFactory: this._recordFactory
|
|
92
|
-
});
|
|
93
|
-
yield _query.Run();
|
|
94
|
-
this._values = Object.assign(Object.assign({}, this._values), newValues);
|
|
95
|
-
return this;
|
|
63
|
+
async Update(newValues, whereParameters) {
|
|
64
|
+
const originalValues = this._values;
|
|
65
|
+
if (originalValues.updated_at !== undefined) {
|
|
66
|
+
newValues.updated_at = new Date().toISOString();
|
|
67
|
+
}
|
|
68
|
+
const queryStr = await oldQueryStatementBuilder.BuildUpdate(this._tableName, newValues, whereParameters);
|
|
69
|
+
// Merge newValues and originalValues for parameters (with 'where_' prefix for where clause)
|
|
70
|
+
const params = { ...newValues };
|
|
71
|
+
Object.entries(originalValues).forEach(([key, value]) => {
|
|
72
|
+
params[`where_${key}`] = value;
|
|
73
|
+
});
|
|
74
|
+
const _query = this._queryFactory({
|
|
75
|
+
tableName: this._tableName,
|
|
76
|
+
query: queryStr,
|
|
77
|
+
parameters: params,
|
|
78
|
+
adapterName: this._customAdapter,
|
|
79
|
+
recordFactory: this._recordFactory
|
|
96
80
|
});
|
|
81
|
+
await _query.Run();
|
|
82
|
+
this._values = { ...this._values, ...newValues };
|
|
83
|
+
return this;
|
|
97
84
|
}
|
|
98
85
|
/** Delete this record from the database */
|
|
99
|
-
Delete(primaryKey) {
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
recordFactory: this._recordFactory
|
|
114
|
-
});
|
|
115
|
-
yield _query.Run();
|
|
86
|
+
async Delete(primaryKey) {
|
|
87
|
+
const originalValues = this._values;
|
|
88
|
+
if (originalValues.deleted_at !== undefined) {
|
|
89
|
+
this._values.deleted_at = new Date().toISOString();
|
|
90
|
+
await this.Update(this._values, this._values.id ? { id: this._values.id } : primaryKey || {});
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
const queryStr = await oldQueryStatementBuilder.BuildDelete(this._tableName, this._values);
|
|
94
|
+
const _query = this._queryFactory({
|
|
95
|
+
tableName: this._tableName,
|
|
96
|
+
query: queryStr,
|
|
97
|
+
parameters: this.values,
|
|
98
|
+
adapterName: this._customAdapter,
|
|
99
|
+
recordFactory: this._recordFactory
|
|
116
100
|
});
|
|
101
|
+
await _query.Run();
|
|
117
102
|
}
|
|
118
103
|
/** Returns the values object for JSON.stringify() */
|
|
119
104
|
toJSON() {
|
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,uBAAuB,EACvB,eAAe,EACf,UAAU,EACV,YAAY,EACZ,aAAa,EACb,WAAW,
|
|
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,EAEd,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;IAoChB,KAAK,CAAC,WAAW,EAAE,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC;CAoBhE"}
|