duckdb-tinyorm 1.0.43 → 2.0.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/README.md +387 -46
- package/dist/constants/data-type.decorator.d.ts +20 -1
- package/dist/constants/data-type.decorator.js +62 -14
- package/dist/constants/data-type.decorator.js.map +1 -1
- package/dist/errors/orm-errors.d.ts +27 -0
- package/dist/errors/orm-errors.js +80 -0
- package/dist/errors/orm-errors.js.map +1 -0
- package/dist/helpers/bulk-insert.helper.d.ts +1 -0
- package/dist/helpers/bulk-insert.helper.js +19 -0
- package/dist/helpers/bulk-insert.helper.js.map +1 -0
- package/dist/helpers/db.helper.d.ts +10 -0
- package/dist/helpers/db.helper.js +62 -0
- package/dist/helpers/db.helper.js.map +1 -1
- package/dist/helpers/mapping.helper.d.ts +1 -1
- package/dist/helpers/mapping.helper.js +35 -35
- package/dist/helpers/mapping.helper.js.map +1 -1
- package/dist/helpers/table-util.helper.js +46 -19
- package/dist/helpers/table-util.helper.js.map +1 -1
- package/dist/index.d.ts +9 -3
- package/dist/index.js +24 -1
- package/dist/index.js.map +1 -1
- package/dist/migration/migration.d.ts +22 -0
- package/dist/migration/migration.js +143 -0
- package/dist/migration/migration.js.map +1 -0
- package/dist/pagination/pagination.d.ts +10 -0
- package/dist/pagination/pagination.js +3 -0
- package/dist/pagination/pagination.js.map +1 -0
- package/dist/query/query-builder.d.ts +27 -0
- package/dist/query/query-builder.js +113 -0
- package/dist/query/query-builder.js.map +1 -0
- package/dist/repositories/base.interface.d.ts +19 -2
- package/dist/repositories/base.repository.d.ts +18 -2
- package/dist/repositories/base.repository.js +159 -14
- package/dist/repositories/base.repository.js.map +1 -1
- package/dist/repositories/duckdb.repository.d.ts +18 -0
- package/dist/repositories/duckdb.repository.js +91 -16
- package/dist/repositories/duckdb.repository.js.map +1 -1
- package/dist/repositories/transaction.d.ts +9 -0
- package/dist/repositories/transaction.js +48 -0
- package/dist/repositories/transaction.js.map +1 -0
- package/dist/test.d.ts +4 -3
- package/dist/test.js +126 -28
- package/dist/test.js.map +1 -1
- package/package.json +3 -2
- package/tsconfig.json +9 -1
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MigrationRunner = exports.Migration = void 0;
|
|
4
|
+
class Migration {
|
|
5
|
+
}
|
|
6
|
+
exports.Migration = Migration;
|
|
7
|
+
class MigrationRunner {
|
|
8
|
+
connection;
|
|
9
|
+
migrationTableName;
|
|
10
|
+
constructor(connection, options = {}) {
|
|
11
|
+
this.connection = connection;
|
|
12
|
+
this.migrationTableName = options.tableName ?? 'migrations';
|
|
13
|
+
}
|
|
14
|
+
async initialize() {
|
|
15
|
+
await this.createMigrationTableIfNotExists();
|
|
16
|
+
}
|
|
17
|
+
async createMigrationTableIfNotExists() {
|
|
18
|
+
const query = `
|
|
19
|
+
CREATE TABLE IF NOT EXISTS ${this.migrationTableName} (
|
|
20
|
+
id INTEGER PRIMARY KEY,
|
|
21
|
+
version VARCHAR NOT NULL UNIQUE,
|
|
22
|
+
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
23
|
+
)
|
|
24
|
+
`;
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
this.connection.run(query, (err) => {
|
|
27
|
+
if (err) {
|
|
28
|
+
reject(err);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
resolve();
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
async applyMigrations(migrations) {
|
|
37
|
+
await this.initialize();
|
|
38
|
+
// Sort migrations by version
|
|
39
|
+
migrations.sort((a, b) => a.version.localeCompare(b.version));
|
|
40
|
+
// Check which migrations have been applied
|
|
41
|
+
const appliedMigrations = await this.getAppliedMigrations();
|
|
42
|
+
const appliedVersions = new Set(appliedMigrations.map(m => m.version));
|
|
43
|
+
// Apply migrations that haven't been applied yet
|
|
44
|
+
for (const migration of migrations) {
|
|
45
|
+
if (!appliedVersions.has(migration.version)) {
|
|
46
|
+
await this.applyMigration(migration);
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
async revertMigrations(migrations, targetVersion) {
|
|
51
|
+
await this.initialize();
|
|
52
|
+
// Sort migrations by version in descending order
|
|
53
|
+
migrations.sort((a, b) => b.version.localeCompare(a.version));
|
|
54
|
+
// Check which migrations have been applied
|
|
55
|
+
const appliedMigrations = await this.getAppliedMigrations();
|
|
56
|
+
const appliedVersions = new Map(appliedMigrations.map(m => [m.version, m]));
|
|
57
|
+
// Revert migrations until target version
|
|
58
|
+
for (const migration of migrations) {
|
|
59
|
+
if (appliedVersions.has(migration.version)) {
|
|
60
|
+
if (targetVersion && migration.version <= targetVersion) {
|
|
61
|
+
// Stop if we've reached or gone past the target version
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
await this.revertMigration(migration);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
async getAppliedMigrations() {
|
|
69
|
+
const query = `SELECT * FROM ${this.migrationTableName} ORDER BY id ASC`;
|
|
70
|
+
return new Promise((resolve, reject) => {
|
|
71
|
+
this.connection.all(query, (err, result) => {
|
|
72
|
+
if (err) {
|
|
73
|
+
reject(err);
|
|
74
|
+
}
|
|
75
|
+
else {
|
|
76
|
+
// Cast the result to the expected type
|
|
77
|
+
const typedResult = result.map((row) => ({
|
|
78
|
+
id: typeof row.id === 'number' ? row.id : Number(row.id),
|
|
79
|
+
version: String(row.version),
|
|
80
|
+
applied_at: String(row.applied_at)
|
|
81
|
+
}));
|
|
82
|
+
resolve(typedResult);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async applyMigration(migration) {
|
|
88
|
+
const upQuery = migration.up();
|
|
89
|
+
const insertQuery = `INSERT INTO ${this.migrationTableName} (version) VALUES ('${migration.version}')`;
|
|
90
|
+
// Start transaction
|
|
91
|
+
await this.executeQuery('BEGIN TRANSACTION');
|
|
92
|
+
try {
|
|
93
|
+
// Apply migration
|
|
94
|
+
await this.executeQuery(upQuery);
|
|
95
|
+
// Record migration
|
|
96
|
+
await this.executeQuery(insertQuery);
|
|
97
|
+
// Commit transaction
|
|
98
|
+
await this.executeQuery('COMMIT');
|
|
99
|
+
console.log(`Migration ${migration.version} applied successfully`);
|
|
100
|
+
}
|
|
101
|
+
catch (error) {
|
|
102
|
+
// Rollback transaction on error
|
|
103
|
+
await this.executeQuery('ROLLBACK');
|
|
104
|
+
console.error(`Failed to apply migration ${migration.version}:`, error);
|
|
105
|
+
throw error;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async revertMigration(migration) {
|
|
109
|
+
const downQuery = migration.down();
|
|
110
|
+
const deleteQuery = `DELETE FROM ${this.migrationTableName} WHERE version = '${migration.version}'`;
|
|
111
|
+
// Start transaction
|
|
112
|
+
await this.executeQuery('BEGIN TRANSACTION');
|
|
113
|
+
try {
|
|
114
|
+
// Revert migration
|
|
115
|
+
await this.executeQuery(downQuery);
|
|
116
|
+
// Remove migration record
|
|
117
|
+
await this.executeQuery(deleteQuery);
|
|
118
|
+
// Commit transaction
|
|
119
|
+
await this.executeQuery('COMMIT');
|
|
120
|
+
console.log(`Migration ${migration.version} reverted successfully`);
|
|
121
|
+
}
|
|
122
|
+
catch (error) {
|
|
123
|
+
// Rollback transaction on error
|
|
124
|
+
await this.executeQuery('ROLLBACK');
|
|
125
|
+
console.error(`Failed to revert migration ${migration.version}:`, error);
|
|
126
|
+
throw error;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
async executeQuery(query) {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
this.connection.run(query, (err, result) => {
|
|
132
|
+
if (err) {
|
|
133
|
+
reject(err);
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
resolve(result);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
exports.MigrationRunner = MigrationRunner;
|
|
143
|
+
//# sourceMappingURL=migration.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migration.js","sourceRoot":"","sources":["../../src/migration/migration.ts"],"names":[],"mappings":";;;AASA,MAAsB,SAAS;CAI9B;AAJD,8BAIC;AAED,MAAa,eAAe;IAIH;IAHJ,kBAAkB,CAAS;IAE5C,YACqB,UAAsB,EACvC,UAA4B,EAAE;QADb,eAAU,GAAV,UAAU,CAAY;QAGvC,IAAI,CAAC,kBAAkB,GAAG,OAAO,CAAC,SAAS,IAAI,YAAY,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,UAAU;QACZ,MAAM,IAAI,CAAC,+BAA+B,EAAE,CAAC;IACjD,CAAC;IAEO,KAAK,CAAC,+BAA+B;QACzC,MAAM,KAAK,GAAG;yCACmB,IAAI,CAAC,kBAAkB;;;;;SAKvD,CAAC;QAEF,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACzC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,EAAE;gBAC/B,IAAI,GAAG,EAAE,CAAC;oBACN,MAAM,CAAC,GAAG,CAAC,CAAC;gBAChB,CAAC;qBAAM,CAAC;oBACJ,OAAO,EAAE,CAAC;gBACd,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IAED,KAAK,CAAC,eAAe,CAAC,UAAuB;QACzC,MAAM,IAAI,CAAC,UAAU,EAAE,CAAC;QAExB,6BAA6B;QAC7B,UAAU,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC;QAE9D,2CAA2C;QAC3C,MA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AAY,CAAC,KAAa;QACpC,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;YACnC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,GAAG,EAAE,MAAM,EAAE,EAAE;gBACvC,IAAI,GAAG,EAAE,CAAC;oBACN,MAAM,CAAC,GAAG,CAAC,CAAC;gBAChB,CAAC;qBAAM,CAAC;oBACJ,OAAO,CAAC,MAAM,CAAC,CAAC;gBACpB,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;CACJ;AA7JD,0CA6JC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pagination.js","sourceRoot":"","sources":["../../src/pagination/pagination.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export declare class QueryBuilder<T> {
|
|
2
|
+
private selectClauses;
|
|
3
|
+
private fromTable;
|
|
4
|
+
private whereClauses;
|
|
5
|
+
private orderByClauses;
|
|
6
|
+
private limitValue?;
|
|
7
|
+
private offsetValue?;
|
|
8
|
+
private groupByClauses;
|
|
9
|
+
private havingClauses;
|
|
10
|
+
private joinClauses;
|
|
11
|
+
private parameters;
|
|
12
|
+
constructor(entityClass: new () => T);
|
|
13
|
+
select(columns: string | string[]): QueryBuilder<T>;
|
|
14
|
+
where(condition: string, ...params: any[]): QueryBuilder<T>;
|
|
15
|
+
andWhere(condition: string, ...params: any[]): QueryBuilder<T>;
|
|
16
|
+
orWhere(condition: string, ...params: any[]): QueryBuilder<T>;
|
|
17
|
+
orderBy(column: string, direction?: 'ASC' | 'DESC'): QueryBuilder<T>;
|
|
18
|
+
limit(limit: number): QueryBuilder<T>;
|
|
19
|
+
offset(offset: number): QueryBuilder<T>;
|
|
20
|
+
groupBy(column: string | string[]): QueryBuilder<T>;
|
|
21
|
+
having(condition: string, ...params: any[]): QueryBuilder<T>;
|
|
22
|
+
join(table: string, condition: string): QueryBuilder<T>;
|
|
23
|
+
leftJoin(table: string, condition: string): QueryBuilder<T>;
|
|
24
|
+
rightJoin(table: string, condition: string): QueryBuilder<T>;
|
|
25
|
+
getQuery(): string;
|
|
26
|
+
getParameters(): any[];
|
|
27
|
+
}
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.QueryBuilder = void 0;
|
|
4
|
+
class QueryBuilder {
|
|
5
|
+
selectClauses = ['*'];
|
|
6
|
+
fromTable = '';
|
|
7
|
+
whereClauses = [];
|
|
8
|
+
orderByClauses = [];
|
|
9
|
+
limitValue;
|
|
10
|
+
offsetValue;
|
|
11
|
+
groupByClauses = [];
|
|
12
|
+
havingClauses = [];
|
|
13
|
+
joinClauses = [];
|
|
14
|
+
parameters = [];
|
|
15
|
+
constructor(entityClass) {
|
|
16
|
+
// Use metadata to get the table name
|
|
17
|
+
const tableName = Reflect.getMetadata('TableName', entityClass) || entityClass.name;
|
|
18
|
+
this.fromTable = `main.${tableName}`;
|
|
19
|
+
}
|
|
20
|
+
select(columns) {
|
|
21
|
+
this.selectClauses = Array.isArray(columns) ? columns : [columns];
|
|
22
|
+
return this;
|
|
23
|
+
}
|
|
24
|
+
where(condition, ...params) {
|
|
25
|
+
this.whereClauses.push(condition);
|
|
26
|
+
this.parameters.push(...params);
|
|
27
|
+
return this;
|
|
28
|
+
}
|
|
29
|
+
andWhere(condition, ...params) {
|
|
30
|
+
if (this.whereClauses.length > 0) {
|
|
31
|
+
this.whereClauses.push(`AND ${condition}`);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
this.whereClauses.push(condition);
|
|
35
|
+
}
|
|
36
|
+
this.parameters.push(...params);
|
|
37
|
+
return this;
|
|
38
|
+
}
|
|
39
|
+
orWhere(condition, ...params) {
|
|
40
|
+
if (this.whereClauses.length > 0) {
|
|
41
|
+
this.whereClauses.push(`OR ${condition}`);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.whereClauses.push(condition);
|
|
45
|
+
}
|
|
46
|
+
this.parameters.push(...params);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
orderBy(column, direction = 'ASC') {
|
|
50
|
+
this.orderByClauses.push(`${column} ${direction}`);
|
|
51
|
+
return this;
|
|
52
|
+
}
|
|
53
|
+
limit(limit) {
|
|
54
|
+
this.limitValue = limit;
|
|
55
|
+
return this;
|
|
56
|
+
}
|
|
57
|
+
offset(offset) {
|
|
58
|
+
this.offsetValue = offset;
|
|
59
|
+
return this;
|
|
60
|
+
}
|
|
61
|
+
groupBy(column) {
|
|
62
|
+
const columns = Array.isArray(column) ? column : [column];
|
|
63
|
+
this.groupByClauses.push(...columns);
|
|
64
|
+
return this;
|
|
65
|
+
}
|
|
66
|
+
having(condition, ...params) {
|
|
67
|
+
this.havingClauses.push(condition);
|
|
68
|
+
this.parameters.push(...params);
|
|
69
|
+
return this;
|
|
70
|
+
}
|
|
71
|
+
join(table, condition) {
|
|
72
|
+
this.joinClauses.push(`JOIN ${table} ON ${condition}`);
|
|
73
|
+
return this;
|
|
74
|
+
}
|
|
75
|
+
leftJoin(table, condition) {
|
|
76
|
+
this.joinClauses.push(`LEFT JOIN ${table} ON ${condition}`);
|
|
77
|
+
return this;
|
|
78
|
+
}
|
|
79
|
+
rightJoin(table, condition) {
|
|
80
|
+
this.joinClauses.push(`RIGHT JOIN ${table} ON ${condition}`);
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
getQuery() {
|
|
84
|
+
let query = `SELECT ${this.selectClauses.join(', ')} FROM ${this.fromTable}`;
|
|
85
|
+
if (this.joinClauses.length > 0) {
|
|
86
|
+
query += ` ${this.joinClauses.join(' ')}`;
|
|
87
|
+
}
|
|
88
|
+
if (this.whereClauses.length > 0) {
|
|
89
|
+
query += ` WHERE ${this.whereClauses.join(' ')}`;
|
|
90
|
+
}
|
|
91
|
+
if (this.groupByClauses.length > 0) {
|
|
92
|
+
query += ` GROUP BY ${this.groupByClauses.join(', ')}`;
|
|
93
|
+
}
|
|
94
|
+
if (this.havingClauses.length > 0) {
|
|
95
|
+
query += ` HAVING ${this.havingClauses.join(' AND ')}`;
|
|
96
|
+
}
|
|
97
|
+
if (this.orderByClauses.length > 0) {
|
|
98
|
+
query += ` ORDER BY ${this.orderByClauses.join(', ')}`;
|
|
99
|
+
}
|
|
100
|
+
if (this.limitValue !== undefined) {
|
|
101
|
+
query += ` LIMIT ${this.limitValue}`;
|
|
102
|
+
}
|
|
103
|
+
if (this.offsetValue !== undefined) {
|
|
104
|
+
query += ` OFFSET ${this.offsetValue}`;
|
|
105
|
+
}
|
|
106
|
+
return query;
|
|
107
|
+
}
|
|
108
|
+
getParameters() {
|
|
109
|
+
return this.parameters;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
exports.QueryBuilder = QueryBuilder;
|
|
113
|
+
//# sourceMappingURL=query-builder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"query-builder.js","sourceRoot":"","sources":["../../src/query/query-builder.ts"],"names":[],"mappings":";;;AAAA,MAAa,YAAY;IACb,aAAa,GAAa,CAAC,GAAG,CAAC,CAAC;IAChC,SAAS,GAAG,EAAE,CAAC;IACf,YAAY,GAAa,EAAE,CAAC;IAC5B,cAAc,GAAa,EAAE,CAAC;IAC9B,UAAU,CAAU;IACpB,WAAW,CAAU;IACrB,cAAc,GAAa,EAAE,CAAC;IAC9B,aAAa,GAAa,EAAE,CAAC;IAC7B,WAAW,GAAa,EAAE,CAAC;IAC3B,UAAU,GAAU,EAAE,CAAC;IAE/B,YAAY,WAAwB;QAChC,qCAAqC;QACrC,MAAM,SAAS,GAAG,OAAO,CAAC,WAAW,CAAC,WAAW,EAAE,WAAW,CAAC,IAAI,WAAW,CAAC,IAAI,CAAC;QACpF,IAAI,CAAC,SAAS,GAAG,QAAQ,SAAS,EAAE,CAAC;IACzC,CAAC;IAED,MAAM,CAA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AU,CAAC;IAC3B,CAAC;CACJ;AAhID,oCAgIC"}
|
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
import { Page, Pageable } from '../pagination/pagination';
|
|
2
|
+
import { QueryBuilder } from '../query/query-builder';
|
|
3
|
+
import { Transaction } from './transaction';
|
|
4
|
+
import { ExportOptions } from './duckdb.repository';
|
|
5
|
+
export interface IRepository<T extends object, Tid> {
|
|
6
|
+
init(): Promise<void>;
|
|
2
7
|
save(entity: T): Promise<T>;
|
|
8
|
+
saveAll(entities: T[]): Promise<T[]>;
|
|
9
|
+
insert(entity: T): Promise<T>;
|
|
10
|
+
bulkInsert(entities: T[]): Promise<T[]>;
|
|
3
11
|
findAll(): Promise<T[]>;
|
|
4
12
|
findById(id: Tid): Promise<T>;
|
|
5
|
-
|
|
13
|
+
findByIdOrThrow(id: Tid): Promise<T>;
|
|
14
|
+
existsById(id: Tid): Promise<boolean>;
|
|
15
|
+
findBy(entity: Partial<T>, columns: string[]): Promise<T[]>;
|
|
16
|
+
findWithPagination(pageable: Pageable): Promise<Page<T>>;
|
|
6
17
|
removeById(id: Tid): Promise<T>;
|
|
18
|
+
removeAll(): Promise<void>;
|
|
19
|
+
createQueryBuilder(): Promise<QueryBuilder<T>>;
|
|
20
|
+
withTransaction<R>(callback: (transaction: Transaction) => Promise<R>): Promise<R>;
|
|
21
|
+
toEntity(data: Record<string, any>): T;
|
|
22
|
+
exportData(options: ExportOptions): Promise<void>;
|
|
23
|
+
exportQuery(query: string, options: ExportOptions): Promise<void>;
|
|
7
24
|
}
|
|
@@ -1,16 +1,32 @@
|
|
|
1
1
|
import 'reflect-metadata';
|
|
2
|
-
import { DuckDbRepository } from './duckdb.repository';
|
|
2
|
+
import { DuckDbRepository, ExportOptions } from './duckdb.repository';
|
|
3
3
|
import { IRepository } from './base.interface';
|
|
4
|
-
|
|
4
|
+
import { QueryBuilder } from '../query/query-builder';
|
|
5
|
+
import { Transaction } from './transaction';
|
|
6
|
+
import { Page, Pageable } from '../pagination/pagination';
|
|
7
|
+
export declare class BaseRepository<T extends object, Tid> implements IRepository<T, Tid> {
|
|
5
8
|
protected repository: DuckDbRepository;
|
|
6
9
|
protected classType: new () => T;
|
|
10
|
+
protected tableName: string;
|
|
7
11
|
private primaryColumnId;
|
|
8
12
|
constructor(repository: DuckDbRepository);
|
|
9
13
|
init(): Promise<void>;
|
|
10
14
|
private initializeTable;
|
|
11
15
|
removeById(id: Tid): Promise<T>;
|
|
12
16
|
save(entity: T): Promise<T>;
|
|
17
|
+
saveAll(entities: T[]): Promise<T[]>;
|
|
18
|
+
insert(entity: T): Promise<T>;
|
|
19
|
+
bulkInsert(entities: T[]): Promise<T[]>;
|
|
13
20
|
findAll(): Promise<T[]>;
|
|
14
21
|
findById(id: Tid): Promise<T>;
|
|
22
|
+
existsById(id: Tid): Promise<boolean>;
|
|
23
|
+
findByIdOrThrow(id: Tid): Promise<T>;
|
|
24
|
+
findWithPagination(pageable: Pageable): Promise<Page<T>>;
|
|
15
25
|
findBy(entity: Partial<T>, columns: string[]): Promise<T[]>;
|
|
26
|
+
removeAll(): Promise<void>;
|
|
27
|
+
createQueryBuilder(): Promise<QueryBuilder<T>>;
|
|
28
|
+
withTransaction<R>(callback: (transaction: Transaction) => Promise<R>): Promise<R>;
|
|
29
|
+
toEntity(data: Record<string, any>): T;
|
|
30
|
+
exportData(options: ExportOptions): Promise<void>;
|
|
31
|
+
exportQuery(query: string, options: ExportOptions): Promise<void>;
|
|
16
32
|
}
|
|
@@ -3,9 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BaseRepository = void 0;
|
|
4
4
|
require("reflect-metadata");
|
|
5
5
|
const table_util_helper_1 = require("../helpers/table-util.helper");
|
|
6
|
+
const query_builder_1 = require("../query/query-builder");
|
|
7
|
+
const orm_errors_1 = require("../errors/orm-errors");
|
|
8
|
+
// Add 'extends object' constraint to T
|
|
6
9
|
class BaseRepository {
|
|
7
10
|
repository;
|
|
8
11
|
classType;
|
|
12
|
+
tableName;
|
|
9
13
|
primaryColumnId = '';
|
|
10
14
|
constructor(repository) {
|
|
11
15
|
this.repository = repository;
|
|
@@ -14,6 +18,8 @@ class BaseRepository {
|
|
|
14
18
|
if (!this.classType) {
|
|
15
19
|
throw new Error('Class type is not defined!');
|
|
16
20
|
}
|
|
21
|
+
// Get the table name from metadata or use the class name
|
|
22
|
+
this.tableName = Reflect.getMetadata('TableName', this.classType) || this.classType.name;
|
|
17
23
|
}
|
|
18
24
|
async init() {
|
|
19
25
|
await this.initializeTable();
|
|
@@ -29,37 +35,138 @@ class BaseRepository {
|
|
|
29
35
|
}
|
|
30
36
|
async removeById(id) {
|
|
31
37
|
const deletedItem = await this.findById(id);
|
|
32
|
-
const query = `DELETE FROM main.${this.
|
|
38
|
+
const query = `DELETE FROM main.${this.tableName} WHERE ${this.primaryColumnId}='${id}'`;
|
|
33
39
|
await this.repository.executeQuery(query);
|
|
34
40
|
return deletedItem;
|
|
35
41
|
}
|
|
42
|
+
// Update the save method:
|
|
36
43
|
async save(entity) {
|
|
37
|
-
//
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
//
|
|
42
|
-
|
|
43
|
-
|
|
44
|
+
// Handle auto-increment fields
|
|
45
|
+
const propertyNames = Object.getOwnPropertyNames(entity);
|
|
46
|
+
const fields = [];
|
|
47
|
+
const values = [];
|
|
48
|
+
// Collect non-auto-increment fields and values
|
|
49
|
+
for (const propertyName of propertyNames) {
|
|
50
|
+
const autoIncrement = Reflect.getMetadata('AutoIncrement', this.classType.prototype, propertyName);
|
|
51
|
+
const isPrimaryKey = Reflect.getMetadata('PrimaryKey', this.classType.prototype, propertyName);
|
|
52
|
+
if (autoIncrement && isPrimaryKey) {
|
|
53
|
+
continue; // Skip auto-increment primary key
|
|
54
|
+
}
|
|
55
|
+
const value = entity[propertyName];
|
|
56
|
+
fields.push(propertyName);
|
|
57
|
+
if (value === undefined || value === null) {
|
|
58
|
+
values.push('NULL');
|
|
59
|
+
}
|
|
60
|
+
else if (typeof value === 'string') {
|
|
61
|
+
const escapedValue = value.replace(/'/g, "''");
|
|
62
|
+
values.push(`'${escapedValue}'`);
|
|
63
|
+
}
|
|
64
|
+
else if (typeof value === 'boolean') {
|
|
65
|
+
values.push(value ? 'TRUE' : 'FALSE');
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
values.push(`${value}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Build and execute the INSERT statement
|
|
72
|
+
const insertSQL = `INSERT INTO main.${this.tableName} (${fields.join(', ')}) VALUES (${values.join(', ')})`;
|
|
73
|
+
console.log("Insert SQL:", insertSQL);
|
|
74
|
+
try {
|
|
75
|
+
await this.repository.executeQuery(insertSQL);
|
|
76
|
+
// For auto-increment fields, fetch the last inserted ID
|
|
77
|
+
for (const propertyName of propertyNames) {
|
|
78
|
+
const autoIncrement = Reflect.getMetadata('AutoIncrement', this.classType.prototype, propertyName);
|
|
79
|
+
const isPrimaryKey = Reflect.getMetadata('PrimaryKey', this.classType.prototype, propertyName);
|
|
80
|
+
if (autoIncrement && isPrimaryKey) {
|
|
81
|
+
const query = `SELECT MAX(${propertyName}) as last_id FROM main.${this.tableName}`;
|
|
82
|
+
const result = await this.repository.executeQuery(query);
|
|
83
|
+
if (result && result.length > 0) {
|
|
84
|
+
entity[propertyName] = result[0].last_id;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return entity;
|
|
89
|
+
}
|
|
90
|
+
catch (error) {
|
|
91
|
+
console.error("Error saving entity:", error);
|
|
92
|
+
throw error;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
async saveAll(entities) {
|
|
96
|
+
if (!entities.length)
|
|
97
|
+
return [];
|
|
98
|
+
await this.repository.saveToDuckDB(this.classType.name, this.classType, entities);
|
|
99
|
+
return entities;
|
|
100
|
+
}
|
|
101
|
+
async insert(entity) {
|
|
102
|
+
return this.save(entity);
|
|
103
|
+
}
|
|
104
|
+
async bulkInsert(entities) {
|
|
105
|
+
return this.saveAll(entities);
|
|
44
106
|
}
|
|
45
107
|
async findAll() {
|
|
46
|
-
const query = `SELECT * FROM main.${this.
|
|
47
|
-
|
|
48
|
-
return result;
|
|
108
|
+
const query = `SELECT * FROM main.${this.tableName}`;
|
|
109
|
+
return this.repository.executeQuery(query);
|
|
49
110
|
}
|
|
50
111
|
async findById(id) {
|
|
51
112
|
// Get the property names from the class using reflection
|
|
52
113
|
if (!this.primaryColumnId)
|
|
53
114
|
this.primaryColumnId = (0, table_util_helper_1.getPrimaryId)(this.classType);
|
|
115
|
+
if (!this.primaryColumnId) {
|
|
116
|
+
throw new orm_errors_1.PrimaryKeyError("The table doesn't have any primary key declared!");
|
|
117
|
+
}
|
|
118
|
+
const query = `SELECT * FROM main.${this.tableName} WHERE ${this.primaryColumnId}='${id}'`;
|
|
119
|
+
try {
|
|
120
|
+
const result = await this.repository.executeQuery(query);
|
|
121
|
+
if (!result?.length) {
|
|
122
|
+
throw new orm_errors_1.EntityNotFoundError(this.classType.name, id);
|
|
123
|
+
}
|
|
124
|
+
return result[0];
|
|
125
|
+
}
|
|
126
|
+
catch (error) {
|
|
127
|
+
if (error instanceof orm_errors_1.EntityNotFoundError) {
|
|
128
|
+
throw error;
|
|
129
|
+
}
|
|
130
|
+
throw new orm_errors_1.QueryExecutionError(query, error);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
async existsById(id) {
|
|
134
|
+
if (!this.primaryColumnId) {
|
|
135
|
+
this.primaryColumnId = (0, table_util_helper_1.getPrimaryId)(this.classType);
|
|
136
|
+
}
|
|
54
137
|
if (!this.primaryColumnId) {
|
|
55
138
|
throw new Error("The table doesn't have any primary key declared!");
|
|
56
139
|
}
|
|
57
|
-
const query = `SELECT * FROM main.${this.
|
|
140
|
+
const query = `SELECT COUNT(*) as count FROM main.${this.tableName} WHERE ${this.primaryColumnId}='${id}'`;
|
|
58
141
|
const result = await this.repository.executeQuery(query);
|
|
59
|
-
return result
|
|
142
|
+
return result[0].count > 0;
|
|
143
|
+
}
|
|
144
|
+
async findByIdOrThrow(id) {
|
|
145
|
+
const entity = await this.findById(id);
|
|
146
|
+
if (!entity) {
|
|
147
|
+
throw new Error(`Entity with id ${id} not found`);
|
|
148
|
+
}
|
|
149
|
+
return entity;
|
|
150
|
+
}
|
|
151
|
+
async findWithPagination(pageable) {
|
|
152
|
+
const countQuery = `SELECT COUNT(*) as count FROM main.${this.tableName}`;
|
|
153
|
+
const countResult = await this.repository.executeQuery(countQuery);
|
|
154
|
+
// Convert BigInt to Number before using in Math.ceil
|
|
155
|
+
const totalElements = Number(countResult[0].count);
|
|
156
|
+
const query = `SELECT * FROM main.${this.tableName} LIMIT ${pageable.size} OFFSET ${pageable.page * pageable.size}`;
|
|
157
|
+
const content = await this.repository.executeQuery(query);
|
|
158
|
+
return {
|
|
159
|
+
content,
|
|
160
|
+
pageable: {
|
|
161
|
+
page: pageable.page,
|
|
162
|
+
size: pageable.size
|
|
163
|
+
},
|
|
164
|
+
totalElements,
|
|
165
|
+
totalPages: Math.ceil(totalElements / pageable.size)
|
|
166
|
+
};
|
|
60
167
|
}
|
|
61
168
|
async findBy(entity, columns) {
|
|
62
|
-
let query = `SELECT * FROM main.${this.
|
|
169
|
+
let query = `SELECT * FROM main.${this.tableName} WHERE `;
|
|
63
170
|
for (const column of columns) {
|
|
64
171
|
query += `${column}='${entity[column]}' AND `;
|
|
65
172
|
}
|
|
@@ -67,6 +174,44 @@ class BaseRepository {
|
|
|
67
174
|
const result = await this.repository.executeQuery(query);
|
|
68
175
|
return result;
|
|
69
176
|
}
|
|
177
|
+
async removeAll() {
|
|
178
|
+
const query = `DELETE FROM main.${this.tableName}`;
|
|
179
|
+
await this.repository.executeQuery(query);
|
|
180
|
+
}
|
|
181
|
+
async createQueryBuilder() {
|
|
182
|
+
return new query_builder_1.QueryBuilder(this.classType);
|
|
183
|
+
}
|
|
184
|
+
// Support for transactions
|
|
185
|
+
async withTransaction(callback) {
|
|
186
|
+
const transaction = this.repository.createTransaction();
|
|
187
|
+
try {
|
|
188
|
+
await transaction.begin();
|
|
189
|
+
const result = await callback(transaction);
|
|
190
|
+
await transaction.commit();
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
catch (error) {
|
|
194
|
+
try {
|
|
195
|
+
await transaction.rollback();
|
|
196
|
+
}
|
|
197
|
+
catch (rollbackError) {
|
|
198
|
+
throw new orm_errors_1.TransactionError('rollback', rollbackError);
|
|
199
|
+
}
|
|
200
|
+
throw new orm_errors_1.TransactionError('execution', error);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
toEntity(data) {
|
|
204
|
+
const entity = new this.classType();
|
|
205
|
+
Object.assign(entity, data);
|
|
206
|
+
return entity;
|
|
207
|
+
}
|
|
208
|
+
// Add this method to your BaseRepository class
|
|
209
|
+
async exportData(options) {
|
|
210
|
+
return this.repository.exportTable(this.tableName, options);
|
|
211
|
+
}
|
|
212
|
+
async exportQuery(query, options) {
|
|
213
|
+
return this.repository.exportQuery(query, options);
|
|
214
|
+
}
|
|
70
215
|
}
|
|
71
216
|
exports.BaseRepository = BaseRepository;
|
|
72
217
|
//# sourceMappingURL=base.repository.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.repository.js","sourceRoot":"","sources":["../../src/repositories/base.repository.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,oEAA4D;
|
|
1
|
+
{"version":3,"file":"base.repository.js","sourceRoot":"","sources":["../../src/repositories/base.repository.ts"],"names":[],"mappings":";;;AAAA,4BAA0B;AAE1B,oEAA4D;AAE5D,0DAAsD;AAGtD,qDAAmH;AAEnH,uCAAuC;AACvC,MAAa,cAAc;IAKD;IAJZ,SAAS,CAAa;IACtB,SAAS,CAAS;IACpB,eAAe,GAAG,EAAE,CAAC;IAE7B,YAAsB,UAA4B;QAA5B,eAAU,GAAV,UAAU,CAAkB;QAC9C,2EAA2E;QAC3E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E,MAAM,WAAW,GAAG,IAAI,CAAC,UAAU,CAAC,iBAAiB,EAAE,CAAC;QACxD,IAAI,CAAC;YACD,MAAM,WAAW,CAAC,KAAK,EAAE,CAAC;YAC1B,MAAM,MAAM,GAAG,MAAM,QAAQ,CAAC,WAAW,CAAC,CAAC;YAC3C,MAAM,WAAW,CAAC,MAAM,EAAE,CAAC;YAC3B,OAAO,MAAM,CAAC;QAClB,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,IAAI,CAAC;gBACD,MAAM,WAAW,CAAC,QAAQ,EAAE,CAAC;YACjC,CAAC;YAAC,OAAO,aAAa,EAAE,CAAC;gBACrB,MAAM,IAAI,6BAAgB,CAAC,UAAU,EAAE,aAAsB,CAAC,CAAC;YACnE,CAAC;YACD,MAAM,IAAI,6BAAgB,CAAC,WAAW,EAAE,KAAc,CAAC,CAAC;QAC5D,CAAC;IACL,CAAC;IAED,QAAQ,CAAC,IAAyB;QAC9B,MAAM,MAAM,GAAG,IAAI,IAAI,CAAC,SAAS,EAAE,CAAC;QACpC,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,CAAC;QAC5B,OAAO,MAAM,CAAC;IAClB,CAAC;IAED,+CAA+C;IAE/C,KAAK,CAAC,UAAU,CAAC,OAAsB;QACnC,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,IAAI,CAAC,SAAS,EAAE,OAAO,CAAC,CAAC;IAChE,CAAC;IAED,KAAK,CAAC,WAAW,CAAC,KAAa,EAAE,OAAsB;QACnD,OAAO,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE,OAAO,CAAC,CAAC;IACvD,CAAC;CACJ;AAlPD,wCAkPC"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Transaction } from './transaction';
|
|
1
2
|
export declare enum DuckDbLocation {
|
|
2
3
|
File = "",
|
|
3
4
|
Memory = ":memory:"
|
|
@@ -27,4 +28,21 @@ export declare class DuckDbRepository {
|
|
|
27
28
|
dropTable(tableName: string): Promise<void>;
|
|
28
29
|
deleteTableData(tableName: string): Promise<any[]>;
|
|
29
30
|
saveQueryToParquet(name: string, query: string, folder: string): Promise<void>;
|
|
31
|
+
createTransaction(): Transaction;
|
|
32
|
+
exportTable<T>(tableName: string, options: ExportOptions): Promise<void>;
|
|
33
|
+
exportQuery(query: string, options: ExportOptions): Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
export interface ExportOptions {
|
|
36
|
+
format: 'csv' | 'json' | 'parquet';
|
|
37
|
+
fileName: string;
|
|
38
|
+
csvOptions?: {
|
|
39
|
+
header?: boolean;
|
|
40
|
+
delimiter?: string;
|
|
41
|
+
};
|
|
42
|
+
jsonOptions?: {
|
|
43
|
+
pretty?: boolean;
|
|
44
|
+
};
|
|
45
|
+
parquetOptions?: {
|
|
46
|
+
compression?: string;
|
|
47
|
+
};
|
|
30
48
|
}
|