nanodb-orm 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +211 -378
- package/dist/cli.d.ts +13 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +270 -0
- package/dist/cli.js.map +1 -0
- package/dist/constants/index.d.ts +7 -54
- package/dist/constants/index.d.ts.map +1 -1
- package/dist/constants/index.js +9 -61
- package/dist/constants/index.js.map +1 -1
- package/dist/core/config.d.ts +3 -13
- package/dist/core/config.d.ts.map +1 -1
- package/dist/core/config.js +5 -27
- package/dist/core/config.js.map +1 -1
- package/dist/core/connection.d.ts +10 -31
- package/dist/core/connection.d.ts.map +1 -1
- package/dist/core/connection.js +28 -78
- package/dist/core/connection.js.map +1 -1
- package/dist/core/index.d.ts +2 -3
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +3 -18
- package/dist/core/index.js.map +1 -1
- package/dist/index.d.ts +40 -10
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +76 -31
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +79 -22
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +246 -48
- package/dist/init.js.map +1 -1
- package/dist/jest.setup.d.ts +4 -0
- package/dist/jest.setup.d.ts.map +1 -0
- package/dist/jest.setup.js +40 -0
- package/dist/jest.setup.js.map +1 -0
- package/dist/types/errors.d.ts +30 -12
- package/dist/types/errors.d.ts.map +1 -1
- package/dist/types/errors.js +98 -23
- package/dist/types/errors.js.map +1 -1
- package/dist/types/index.d.ts +130 -4
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +2 -4
- package/dist/types/index.js.map +1 -1
- package/dist/utils/error-handler.d.ts +6 -31
- package/dist/utils/error-handler.d.ts.map +1 -1
- package/dist/utils/error-handler.js +24 -81
- package/dist/utils/error-handler.js.map +1 -1
- package/dist/utils/index.d.ts +1 -3
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +4 -6
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/logger.d.ts +6 -25
- package/dist/utils/logger.d.ts.map +1 -1
- package/dist/utils/logger.js +20 -38
- package/dist/utils/logger.js.map +1 -1
- package/dist/utils/migrations.d.ts +16 -90
- package/dist/utils/migrations.d.ts.map +1 -1
- package/dist/utils/migrations.js +216 -423
- package/dist/utils/migrations.js.map +1 -1
- package/dist/utils/schema-introspection.d.ts +30 -169
- package/dist/utils/schema-introspection.d.ts.map +1 -1
- package/dist/utils/schema-introspection.js +128 -462
- package/dist/utils/schema-introspection.js.map +1 -1
- package/dist/utils/seeds.d.ts +15 -48
- package/dist/utils/seeds.d.ts.map +1 -1
- package/dist/utils/seeds.js +101 -188
- package/dist/utils/seeds.js.map +1 -1
- package/dist/utils/sync.d.ts +16 -41
- package/dist/utils/sync.d.ts.map +1 -1
- package/dist/utils/sync.js +69 -172
- package/dist/utils/sync.js.map +1 -1
- package/dist/utils/transactions.d.ts +8 -47
- package/dist/utils/transactions.d.ts.map +1 -1
- package/dist/utils/transactions.js +32 -147
- package/dist/utils/transactions.js.map +1 -1
- package/package.json +29 -10
- package/dist/example.d.ts +0 -67
- package/dist/example.d.ts.map +0 -1
- package/dist/example.js +0 -86
- package/dist/example.js.map +0 -1
- package/dist/types/database.d.ts +0 -74
- package/dist/types/database.d.ts.map +0 -1
- package/dist/types/database.js +0 -6
- package/dist/types/database.js.map +0 -1
- package/dist/types/types.d.ts +0 -30
- package/dist/types/types.d.ts.map +0 -1
- package/dist/types/types.js +0 -6
- package/dist/types/types.js.map +0 -1
- package/llm.txt +0 -336
package/dist/utils/sync.js
CHANGED
|
@@ -1,222 +1,119 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Completely Generic Database Sync Utilities
|
|
4
|
-
* Reads from schema.ts and performs database operations
|
|
5
|
-
* No hardcoded table names or structures - completely dynamic!
|
|
6
|
-
*/
|
|
7
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
3
|
exports.DatabaseSync = void 0;
|
|
9
4
|
const drizzle_orm_1 = require("drizzle-orm");
|
|
10
5
|
const connection_1 = require("../core/connection");
|
|
11
6
|
const logger_1 = require("./logger");
|
|
12
|
-
const
|
|
13
|
-
const schema_introspection_1 = require("./schema-introspection");
|
|
7
|
+
const error_handler_1 = require("./error-handler");
|
|
14
8
|
const migrations_1 = require("./migrations");
|
|
15
9
|
const seeds_1 = require("./seeds");
|
|
10
|
+
const schema_introspection_1 = require("./schema-introspection");
|
|
16
11
|
/**
|
|
17
|
-
*
|
|
18
|
-
* Works with any table structure defined in schema.ts
|
|
19
|
-
* No hardcoded values - everything is derived from schema.ts
|
|
12
|
+
* High-level database operations for setup, sync, and health checks.
|
|
20
13
|
*/
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
*/
|
|
26
|
-
static async setup() {
|
|
27
|
-
try {
|
|
14
|
+
exports.DatabaseSync = {
|
|
15
|
+
/** Initialize schema and seed data */
|
|
16
|
+
async setup() {
|
|
17
|
+
return (0, error_handler_1.withErrorHandling)('setup', async () => {
|
|
28
18
|
logger_1.logger.info('Setting up database...');
|
|
29
|
-
// Initialize schema from schema.ts
|
|
30
19
|
await migrations_1.DatabaseMigrations.initializeSchema();
|
|
31
|
-
// Seed with sample data
|
|
32
20
|
await seeds_1.DatabaseSeeds.seed();
|
|
33
|
-
logger_1.logger.info('Database setup
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
21
|
+
logger_1.logger.info('Database setup complete');
|
|
22
|
+
});
|
|
23
|
+
},
|
|
24
|
+
/** Reset database (drop, recreate, seed) */
|
|
25
|
+
async reset() {
|
|
26
|
+
return (0, error_handler_1.withErrorHandling)('reset', async () => {
|
|
27
|
+
logger_1.logger.warn('Resetting database...');
|
|
28
|
+
await migrations_1.DatabaseMigrations.resetDatabase();
|
|
29
|
+
await seeds_1.DatabaseSeeds.seed();
|
|
30
|
+
logger_1.logger.info('Database reset complete');
|
|
31
|
+
});
|
|
32
|
+
},
|
|
33
|
+
/** Sync with remote (Turso) if supported */
|
|
34
|
+
async sync() {
|
|
35
|
+
const tableNames = schema_introspection_1.SchemaIntrospection.getAllTableNames();
|
|
46
36
|
try {
|
|
47
|
-
logger_1.logger.info('Checking sync capabilities...');
|
|
48
37
|
const db = await connection_1.DatabaseConnection.getInstance();
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
if (client && typeof client.sync === 'function') {
|
|
55
|
-
await client.sync();
|
|
56
|
-
logger_1.logger.info('Database synced successfully with Turso');
|
|
57
|
-
return {
|
|
58
|
-
success: true,
|
|
59
|
-
message: 'Database synced successfully with Turso',
|
|
60
|
-
tablesSynced: tableNames,
|
|
61
|
-
errors: []
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
else {
|
|
65
|
-
throw new errors_1.DatabaseError('Sync not supported', 'sync');
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
catch (error) {
|
|
69
|
-
// Handle specific Turso sync errors
|
|
70
|
-
if (error && typeof error === 'object' && 'code' in error && error.code === 'SYNC_NOT_SUPPORTED') {
|
|
71
|
-
logger_1.logger.info('Sync not supported in HTTP mode - this is expected for remote connections');
|
|
72
|
-
return {
|
|
73
|
-
success: true,
|
|
74
|
-
message: 'Sync not supported in HTTP mode - this is expected for remote connections',
|
|
75
|
-
tablesSynced: tableNames,
|
|
76
|
-
errors: []
|
|
77
|
-
};
|
|
78
|
-
}
|
|
79
|
-
else {
|
|
80
|
-
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
81
|
-
logger_1.logger.warn('Sync failed:', errorMessage);
|
|
82
|
-
return {
|
|
83
|
-
success: false,
|
|
84
|
-
message: `Sync failed: ${errorMessage}`,
|
|
85
|
-
tablesSynced: [],
|
|
86
|
-
errors: [errorMessage]
|
|
87
|
-
};
|
|
88
|
-
}
|
|
38
|
+
const client = db.$client;
|
|
39
|
+
if (client?.sync) {
|
|
40
|
+
await client.sync();
|
|
41
|
+
logger_1.logger.info('Synced with remote database');
|
|
42
|
+
return { success: true, message: 'Synced', tablesSynced: tableNames, errors: [] };
|
|
89
43
|
}
|
|
44
|
+
return { success: true, message: 'Sync not supported (local mode)', tablesSynced: tableNames, errors: [] };
|
|
90
45
|
}
|
|
91
46
|
catch (error) {
|
|
92
|
-
const
|
|
93
|
-
logger_1.logger.
|
|
94
|
-
return {
|
|
95
|
-
success: false,
|
|
96
|
-
message: dbError.message,
|
|
97
|
-
tablesSynced: [],
|
|
98
|
-
errors: [dbError.message]
|
|
99
|
-
};
|
|
47
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
48
|
+
logger_1.logger.warn(`Sync failed: ${msg}`);
|
|
49
|
+
return { success: false, message: msg, tablesSynced: [], errors: [msg] };
|
|
100
50
|
}
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
|
|
104
|
-
* Completely generic - works with any table structure defined in schema.ts
|
|
105
|
-
*/
|
|
106
|
-
static async getDatabaseInfo() {
|
|
51
|
+
},
|
|
52
|
+
/** Get database information */
|
|
53
|
+
async getDatabaseInfo() {
|
|
107
54
|
try {
|
|
108
55
|
const db = await connection_1.DatabaseConnection.getInstance();
|
|
109
56
|
const tableNames = schema_introspection_1.SchemaIntrospection.getAllTableNames();
|
|
110
|
-
// Get table counts
|
|
111
57
|
const tableCounts = {};
|
|
112
58
|
let totalRecords = 0;
|
|
113
|
-
for (const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
}
|
|
122
|
-
catch (error) {
|
|
123
|
-
logger_1.logger.warn(`Could not get count for table ${tableName}:`, error);
|
|
124
|
-
tableCounts[tableName] = 0;
|
|
125
|
-
}
|
|
59
|
+
for (const name of tableNames) {
|
|
60
|
+
try {
|
|
61
|
+
const result = await db.run((0, drizzle_orm_1.sql) `
|
|
62
|
+
SELECT COUNT(*) as c FROM ${drizzle_orm_1.sql.identifier(name)}
|
|
63
|
+
`);
|
|
64
|
+
const count = Number(result.rows?.[0]?.c ?? 0);
|
|
65
|
+
tableCounts[name] = count;
|
|
66
|
+
totalRecords += count;
|
|
126
67
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
const schemaValidation = await migrations_1.DatabaseMigrations.validateSchema();
|
|
130
|
-
// Check sync support
|
|
131
|
-
let syncSupported = false;
|
|
132
|
-
try {
|
|
133
|
-
const client = db.$client;
|
|
134
|
-
if (client && typeof client.sync === 'function') {
|
|
135
|
-
await client.sync();
|
|
136
|
-
syncSupported = true;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
catch (error) {
|
|
140
|
-
if (error && typeof error === 'object' && 'code' in error && error.code === 'SYNC_NOT_SUPPORTED') {
|
|
141
|
-
syncSupported = false;
|
|
68
|
+
catch {
|
|
69
|
+
tableCounts[name] = 0;
|
|
142
70
|
}
|
|
143
71
|
}
|
|
72
|
+
const validation = await migrations_1.DatabaseMigrations.validateSchema();
|
|
144
73
|
return {
|
|
145
74
|
tables: tableNames,
|
|
146
75
|
tableCounts,
|
|
147
76
|
totalRecords,
|
|
148
|
-
schemaValid:
|
|
149
|
-
syncSupported
|
|
77
|
+
schemaValid: validation.isValid,
|
|
150
78
|
};
|
|
151
79
|
}
|
|
152
|
-
catch
|
|
153
|
-
|
|
154
|
-
return {
|
|
155
|
-
tables: [],
|
|
156
|
-
tableCounts: {},
|
|
157
|
-
totalRecords: 0,
|
|
158
|
-
schemaValid: false,
|
|
159
|
-
syncSupported: false
|
|
160
|
-
};
|
|
80
|
+
catch {
|
|
81
|
+
return { tables: [], tableCounts: {}, totalRecords: 0, schemaValid: false };
|
|
161
82
|
}
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
|
|
165
|
-
* Completely generic - works with any table structure defined in schema.ts
|
|
166
|
-
*/
|
|
167
|
-
static async reset() {
|
|
83
|
+
},
|
|
84
|
+
/** Health check */
|
|
85
|
+
async healthCheck() {
|
|
168
86
|
try {
|
|
169
|
-
|
|
170
|
-
await migrations_1.DatabaseMigrations.resetDatabase();
|
|
171
|
-
await seeds_1.DatabaseSeeds.seed();
|
|
172
|
-
logger_1.logger.info('Database reset completed');
|
|
173
|
-
}
|
|
174
|
-
catch (error) {
|
|
175
|
-
const dbError = new errors_1.DatabaseError('Database reset failed', 'reset', error);
|
|
176
|
-
logger_1.logger.error(dbError.message, dbError);
|
|
177
|
-
throw dbError;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Health check for database
|
|
182
|
-
* Completely generic - works with any table structure defined in schema.ts
|
|
183
|
-
*/
|
|
184
|
-
static async healthCheck() {
|
|
185
|
-
try {
|
|
186
|
-
const dbInfo = await this.getDatabaseInfo();
|
|
87
|
+
const info = await this.getDatabaseInfo();
|
|
187
88
|
const errors = [];
|
|
188
|
-
|
|
189
|
-
if (!dbInfo.schemaValid) {
|
|
89
|
+
if (!info.schemaValid)
|
|
190
90
|
errors.push('Schema validation failed');
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (dbInfo.totalRecords === 0) {
|
|
194
|
-
errors.push('No data found in database');
|
|
195
|
-
}
|
|
196
|
-
const healthy = errors.length === 0;
|
|
91
|
+
if (info.totalRecords === 0)
|
|
92
|
+
errors.push('No data in database');
|
|
197
93
|
return {
|
|
198
|
-
healthy,
|
|
199
|
-
tables:
|
|
200
|
-
tableCounts:
|
|
201
|
-
totalRecords:
|
|
202
|
-
schemaValid:
|
|
203
|
-
|
|
204
|
-
errors
|
|
94
|
+
healthy: errors.length === 0,
|
|
95
|
+
tables: info.tables,
|
|
96
|
+
tableCounts: info.tableCounts,
|
|
97
|
+
totalRecords: info.totalRecords,
|
|
98
|
+
schemaValid: info.schemaValid,
|
|
99
|
+
errors,
|
|
205
100
|
};
|
|
206
101
|
}
|
|
207
102
|
catch (error) {
|
|
208
|
-
logger_1.logger.error('Error during health check:', error);
|
|
209
103
|
return {
|
|
210
104
|
healthy: false,
|
|
211
105
|
tables: [],
|
|
212
106
|
tableCounts: {},
|
|
213
107
|
totalRecords: 0,
|
|
214
108
|
schemaValid: false,
|
|
215
|
-
|
|
216
|
-
errors: [error.message]
|
|
109
|
+
errors: [error instanceof Error ? error.message : String(error)],
|
|
217
110
|
};
|
|
218
111
|
}
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
|
|
112
|
+
},
|
|
113
|
+
/** Check if database is ready */
|
|
114
|
+
async isReady() {
|
|
115
|
+
const health = await this.healthCheck();
|
|
116
|
+
return health.healthy;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
222
119
|
//# sourceMappingURL=sync.js.map
|
package/dist/utils/sync.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../../utils/sync.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"sync.js","sourceRoot":"","sources":["../../utils/sync.ts"],"names":[],"mappings":";;;AAAA,6CAAkC;AAClC,mDAAwD;AACxD,qCAAkC;AAClC,mDAAoD;AACpD,6CAAkD;AAClD,mCAAwC;AACxC,iEAA6D;AAG7D;;GAEG;AACU,QAAA,YAAY,GAAG;IAC1B,sCAAsC;IACtC,KAAK,CAAC,KAAK;QACT,OAAO,IAAA,iCAAiB,EAAC,OAAO,EAAE,KAAK,IAAI,EAAE;YAC3C,eAAM,CAAC,IAAI,CAAC,wBAAwB,CAAC,CAAC;YACtC,MAAM,+BAAkB,CAAC,gBAAgB,EAAE,CAAC;YAC5C,MAAM,qBAAa,CAAC,IAAI,EAAE,CAAC;YAC3B,eAAM,CAAC,IAAI,CAAC,yBAAyB,CAAC,CAAC;QACzC,CAAC,CAAC,CAAC;IACL,CAAC;IAED,4CAA4C;I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f,IAAI,CAAC;YACH,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,eAAe,EAAE,CAAC;YAC1C,MAAM,MAAM,GAAa,EAAE,CAAC;YAE5B,IAAI,CAAC,IAAI,CAAC,WAAW;gBAAE,MAAM,CAAC,IAAI,CAAC,0BAA0B,CAAC,CAAC;YAC/D,IAAI,IAAI,CAAC,YAAY,KAAK,CAAC;gBAAE,MAAM,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC;YAEhE,OAAO;gBACL,OAAO,EAAE,MAAM,CAAC,MAAM,KAAK,CAAC;gBAC5B,MAAM,EAAE,IAAI,CAAC,MAAM;gBACnB,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,YAAY,EAAE,IAAI,CAAC,YAAY;gBAC/B,WAAW,EAAE,IAAI,CAAC,WAAW;gBAC7B,MAAM;aACP,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO;gBACL,OAAO,EAAE,KAAK;gBACd,MAAM,EAAE,EAAE;gBACV,WAAW,EAAE,EAAE;gBACf,YAAY,EAAE,CAAC;gBACf,WAAW,EAAE,KAAK;gBAClB,MAAM,EAAE,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;aACjE,CAAC;QACJ,CAAC;IACH,CAAC;IAED,iCAAiC;IACjC,KAAK,CAAC,OAAO;QACX,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QACxC,OAAO,MAAM,CAAC,OAAO,CAAC;IACxB,CAAC;CACF,CAAC"}
|
|
@@ -1,55 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
* Transaction utilities for atomic database operations
|
|
3
|
-
*/
|
|
4
|
-
import { DatabaseClient } from '../types/database';
|
|
5
|
-
export interface TransactionOptions {
|
|
6
|
-
isolationLevel?: 'READ_UNCOMMITTED' | 'READ_COMMITTED' | 'REPEATABLE_READ' | 'SERIALIZABLE';
|
|
7
|
-
timeout?: number;
|
|
8
|
-
}
|
|
1
|
+
import type { DatabaseClient } from '../types';
|
|
9
2
|
export interface TransactionResult<T> {
|
|
10
3
|
success: boolean;
|
|
11
4
|
result?: T;
|
|
12
5
|
error?: Error;
|
|
13
|
-
rollbackError?: Error;
|
|
14
6
|
}
|
|
15
7
|
/**
|
|
16
|
-
*
|
|
8
|
+
* Execute operations within a database transaction.
|
|
9
|
+
* Automatically commits on success, rolls back on failure.
|
|
17
10
|
*/
|
|
18
|
-
export declare
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Execute multiple operations in a single transaction
|
|
25
|
-
*/
|
|
26
|
-
static executeBatch<T>(operations: Array<(db: DatabaseClient) => Promise<T>>, options?: TransactionOptions): Promise<TransactionResult<T[]>>;
|
|
27
|
-
/**
|
|
28
|
-
* Execute a migration operation with transaction support
|
|
29
|
-
*/
|
|
30
|
-
static executeMigration(migrationOperation: (db: DatabaseClient) => Promise<void>, options?: TransactionOptions): Promise<TransactionResult<void>>;
|
|
31
|
-
/**
|
|
32
|
-
* Execute table recreation with transaction support
|
|
33
|
-
*/
|
|
34
|
-
static recreateTable(tableName: string, createOperation: (db: DatabaseClient) => Promise<void>, options?: TransactionOptions): Promise<TransactionResult<void>>;
|
|
35
|
-
/**
|
|
36
|
-
* Execute data seeding with transaction support
|
|
37
|
-
*/
|
|
38
|
-
static seedData(seedOperation: (db: DatabaseClient) => Promise<void>, options?: TransactionOptions): Promise<TransactionResult<void>>;
|
|
39
|
-
/**
|
|
40
|
-
* Execute database reset with transaction support
|
|
41
|
-
*/
|
|
42
|
-
static resetDatabase(resetOperation: (db: DatabaseClient) => Promise<void>, options?: TransactionOptions): Promise<TransactionResult<void>>;
|
|
43
|
-
/**
|
|
44
|
-
* Check if the database supports transactions
|
|
45
|
-
*/
|
|
46
|
-
static supportsTransactions(): Promise<boolean>;
|
|
47
|
-
/**
|
|
48
|
-
* Get transaction status
|
|
49
|
-
*/
|
|
50
|
-
static getTransactionStatus(): Promise<{
|
|
51
|
-
inTransaction: boolean;
|
|
52
|
-
supportsTransactions: boolean;
|
|
53
|
-
}>;
|
|
54
|
-
}
|
|
11
|
+
export declare function transaction<T>(operation: (db: DatabaseClient) => Promise<T>): Promise<TransactionResult<T>>;
|
|
12
|
+
/**
|
|
13
|
+
* Drop and recreate a table within a transaction.
|
|
14
|
+
*/
|
|
15
|
+
export declare function recreateTable(tableName: string, createSql: string): Promise<TransactionResult<void>>;
|
|
55
16
|
//# sourceMappingURL=transactions.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactions.d.ts","sourceRoot":"","sources":["../../utils/transactions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transactions.d.ts","sourceRoot":"","sources":["../../utils/transactions.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE/C,MAAM,WAAW,iBAAiB,CAAC,CAAC;IAClC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,CAAC,EAAE,CAAC,CAAC;IACX,KAAK,CAAC,EAAE,KAAK,CAAC;CACf;AAED;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EACjC,SAAS,EAAE,CAAC,EAAE,EAAE,cAAc,KAAK,OAAO,CAAC,CAAC,CAAC,GAC5C,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAuB/B;AAED;;GAEG;AACH,wBAAsB,aAAa,CACjC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,iBAAiB,CAAC,IAAI,CAAC,CAAC,CAMlC"}
|
|
@@ -1,159 +1,44 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Transaction utilities for atomic database operations
|
|
4
|
-
*/
|
|
5
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.
|
|
3
|
+
exports.transaction = transaction;
|
|
4
|
+
exports.recreateTable = recreateTable;
|
|
7
5
|
const connection_1 = require("../core/connection");
|
|
8
6
|
const logger_1 = require("./logger");
|
|
9
|
-
const error_handler_1 = require("./error-handler");
|
|
10
7
|
/**
|
|
11
|
-
*
|
|
8
|
+
* Execute operations within a database transaction.
|
|
9
|
+
* Automatically commits on success, rolls back on failure.
|
|
12
10
|
*/
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
await client.run('BEGIN TRANSACTION');
|
|
24
|
-
// Set isolation level if specified
|
|
25
|
-
if (options.isolationLevel) {
|
|
26
|
-
await client.run(`SET TRANSACTION ISOLATION LEVEL ${options.isolationLevel}`);
|
|
27
|
-
}
|
|
28
|
-
// Execute the operation
|
|
29
|
-
const result = await operation(client);
|
|
30
|
-
// Commit transaction
|
|
31
|
-
await client.run('COMMIT');
|
|
32
|
-
logger_1.logger.debug('Transaction committed successfully');
|
|
33
|
-
return {
|
|
34
|
-
success: true,
|
|
35
|
-
result
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
catch (error) {
|
|
39
|
-
logger_1.logger.error('Transaction failed, attempting rollback:', error);
|
|
40
|
-
try {
|
|
41
|
-
await client.run('ROLLBACK');
|
|
42
|
-
logger_1.logger.debug('Transaction rolled back successfully');
|
|
43
|
-
}
|
|
44
|
-
catch (rollbackError) {
|
|
45
|
-
logger_1.logger.error('Failed to rollback transaction:', rollbackError);
|
|
46
|
-
return {
|
|
47
|
-
success: false,
|
|
48
|
-
error: error instanceof Error ? error : new Error(String(error)),
|
|
49
|
-
rollbackError: rollbackError instanceof Error ? rollbackError : new Error(String(rollbackError))
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
return {
|
|
53
|
-
success: false,
|
|
54
|
-
error: error instanceof Error ? error : new Error(String(error))
|
|
55
|
-
};
|
|
56
|
-
}
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Execute multiple operations in a single transaction
|
|
60
|
-
*/
|
|
61
|
-
static async executeBatch(operations, options = {}) {
|
|
62
|
-
return this.execute(async (db) => {
|
|
63
|
-
const results = [];
|
|
64
|
-
for (const operation of operations) {
|
|
65
|
-
const result = await operation(db);
|
|
66
|
-
results.push(result);
|
|
67
|
-
}
|
|
68
|
-
return results;
|
|
69
|
-
}, options);
|
|
70
|
-
}
|
|
71
|
-
/**
|
|
72
|
-
* Execute a migration operation with transaction support
|
|
73
|
-
*/
|
|
74
|
-
static async executeMigration(migrationOperation, options = {}) {
|
|
75
|
-
return this.execute(async (db) => {
|
|
76
|
-
logger_1.logger.info('Executing migration within transaction');
|
|
77
|
-
await migrationOperation(db);
|
|
78
|
-
logger_1.logger.info('Migration completed successfully');
|
|
79
|
-
}, options);
|
|
80
|
-
}
|
|
81
|
-
/**
|
|
82
|
-
* Execute table recreation with transaction support
|
|
83
|
-
*/
|
|
84
|
-
static async recreateTable(tableName, createOperation, options = {}) {
|
|
85
|
-
return this.execute(async (db) => {
|
|
86
|
-
logger_1.logger.info(`Recreating table ${tableName} within transaction`);
|
|
87
|
-
// Drop table if exists
|
|
88
|
-
await db.run(`DROP TABLE IF EXISTS ${tableName}`);
|
|
89
|
-
logger_1.logger.debug(`Dropped table ${tableName}`);
|
|
90
|
-
// Create new table
|
|
91
|
-
await createOperation(db);
|
|
92
|
-
logger_1.logger.debug(`Created table ${tableName}`);
|
|
93
|
-
logger_1.logger.info(`Table ${tableName} recreated successfully`);
|
|
94
|
-
}, options);
|
|
95
|
-
}
|
|
96
|
-
/**
|
|
97
|
-
* Execute data seeding with transaction support
|
|
98
|
-
*/
|
|
99
|
-
static async seedData(seedOperation, options = {}) {
|
|
100
|
-
return this.execute(async (db) => {
|
|
101
|
-
logger_1.logger.info('Seeding data within transaction');
|
|
102
|
-
await seedOperation(db);
|
|
103
|
-
logger_1.logger.info('Data seeding completed successfully');
|
|
104
|
-
}, options);
|
|
105
|
-
}
|
|
106
|
-
/**
|
|
107
|
-
* Execute database reset with transaction support
|
|
108
|
-
*/
|
|
109
|
-
static async resetDatabase(resetOperation, options = {}) {
|
|
110
|
-
return this.execute(async (db) => {
|
|
111
|
-
logger_1.logger.warn('Resetting database within transaction');
|
|
112
|
-
await resetOperation(db);
|
|
113
|
-
logger_1.logger.info('Database reset completed successfully');
|
|
114
|
-
}, options);
|
|
115
|
-
}
|
|
116
|
-
/**
|
|
117
|
-
* Check if the database supports transactions
|
|
118
|
-
*/
|
|
119
|
-
static async supportsTransactions() {
|
|
11
|
+
async function transaction(operation) {
|
|
12
|
+
const db = await connection_1.DatabaseConnection.getInstance();
|
|
13
|
+
try {
|
|
14
|
+
await db.run('BEGIN TRANSACTION');
|
|
15
|
+
const result = await operation(db);
|
|
16
|
+
await db.run('COMMIT');
|
|
17
|
+
logger_1.logger.debug('Transaction committed');
|
|
18
|
+
return { success: true, result };
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
120
21
|
try {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
// Try to start a transaction
|
|
124
|
-
await client.run('BEGIN TRANSACTION');
|
|
125
|
-
await client.run('ROLLBACK');
|
|
126
|
-
return true;
|
|
22
|
+
await db.run('ROLLBACK');
|
|
23
|
+
logger_1.logger.debug('Transaction rolled back');
|
|
127
24
|
}
|
|
128
|
-
catch (
|
|
129
|
-
|
|
130
|
-
return false;
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
/**
|
|
134
|
-
* Get transaction status
|
|
135
|
-
*/
|
|
136
|
-
static async getTransactionStatus() {
|
|
137
|
-
try {
|
|
138
|
-
const db = await connection_1.DatabaseConnection.getInstance();
|
|
139
|
-
const client = db;
|
|
140
|
-
// Check if we're in a transaction
|
|
141
|
-
const result = await client.run('SELECT * FROM pragma_transaction_state');
|
|
142
|
-
const inTransaction = result.rows.length > 0 && result.rows[0].state === 'active';
|
|
143
|
-
const supportsTransactions = await this.supportsTransactions();
|
|
144
|
-
return {
|
|
145
|
-
inTransaction,
|
|
146
|
-
supportsTransactions
|
|
147
|
-
};
|
|
148
|
-
}
|
|
149
|
-
catch (error) {
|
|
150
|
-
error_handler_1.ErrorHandler.handleNonThrowingError(error, 'get-transaction-status', 'Failed to get transaction status');
|
|
151
|
-
return {
|
|
152
|
-
inTransaction: false,
|
|
153
|
-
supportsTransactions: false
|
|
154
|
-
};
|
|
25
|
+
catch (rollbackError) {
|
|
26
|
+
logger_1.logger.error('Rollback failed', rollbackError);
|
|
155
27
|
}
|
|
28
|
+
return {
|
|
29
|
+
success: false,
|
|
30
|
+
error: error instanceof Error ? error : new Error(String(error)),
|
|
31
|
+
};
|
|
156
32
|
}
|
|
157
33
|
}
|
|
158
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Drop and recreate a table within a transaction.
|
|
36
|
+
*/
|
|
37
|
+
async function recreateTable(tableName, createSql) {
|
|
38
|
+
return transaction(async (db) => {
|
|
39
|
+
await db.run(`DROP TABLE IF EXISTS "${tableName}"`);
|
|
40
|
+
await db.run(createSql);
|
|
41
|
+
logger_1.logger.info(`Recreated table: ${tableName}`);
|
|
42
|
+
});
|
|
43
|
+
}
|
|
159
44
|
//# sourceMappingURL=transactions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"transactions.js","sourceRoot":"","sources":["../../utils/transactions.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"transactions.js","sourceRoot":"","sources":["../../utils/transactions.ts"],"names":[],"mappings":";;AAcA,kCAyBC;AAKD,sCASC;AArDD,mDAAwD;AACxD,qCAAkC;AASlC;;;GAGG;AACI,KAAK,UAAU,WAAW,CAC/B,SAA6C;IAE7C,MAAM,EAAE,GAAG,MAAM,+BAAkB,CAAC,WAAW,EAA+B,CAAC;IAE/E,IAAI,CAAC;QACH,MAAM,EAAE,CAAC,GAAG,CAAC,mBAAm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