@ketrics/sdk-backend 0.2.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 +329 -0
- package/dist/console.d.ts +34 -0
- package/dist/console.d.ts.map +1 -0
- package/dist/console.js +9 -0
- package/dist/console.js.map +1 -0
- package/dist/context.d.ts +65 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +9 -0
- package/dist/context.js.map +1 -0
- package/dist/database-errors.d.ts +197 -0
- package/dist/database-errors.d.ts.map +1 -0
- package/dist/database-errors.js +258 -0
- package/dist/database-errors.js.map +1 -0
- package/dist/databases.d.ts +165 -0
- package/dist/databases.d.ts.map +1 -0
- package/dist/databases.js +31 -0
- package/dist/databases.js.map +1 -0
- package/dist/errors.d.ts +244 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +316 -0
- package/dist/errors.js.map +1 -0
- package/dist/http.d.ts +57 -0
- package/dist/http.d.ts.map +1 -0
- package/dist/http.js +9 -0
- package/dist/http.js.map +1 -0
- package/dist/index.d.ts +377 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +99 -0
- package/dist/index.js.map +1 -0
- package/dist/secret-errors.d.ts +128 -0
- package/dist/secret-errors.d.ts.map +1 -0
- package/dist/secret-errors.js +172 -0
- package/dist/secret-errors.js.map +1 -0
- package/dist/secrets.d.ts +50 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +27 -0
- package/dist/secrets.js.map +1 -0
- package/dist/volumes.d.ts +276 -0
- package/dist/volumes.d.ts.map +1 -0
- package/dist/volumes.js +9 -0
- package/dist/volumes.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Ketrics SDK - Database Error Classes
|
|
4
|
+
*
|
|
5
|
+
* Provides typed errors for database operations in tenant applications.
|
|
6
|
+
*
|
|
7
|
+
* Error Hierarchy:
|
|
8
|
+
* - DatabaseError (base)
|
|
9
|
+
* - DatabaseNotFoundError
|
|
10
|
+
* - DatabaseAccessDeniedError
|
|
11
|
+
* - DatabaseConnectionError
|
|
12
|
+
* - DatabaseQueryError
|
|
13
|
+
* - DatabaseTransactionError
|
|
14
|
+
*
|
|
15
|
+
* Security Note: Error messages MUST NOT expose internal details like:
|
|
16
|
+
* - Connection strings or credentials
|
|
17
|
+
* - Tenant IDs
|
|
18
|
+
* - Internal database names
|
|
19
|
+
* - AWS account information
|
|
20
|
+
*/
|
|
21
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
+
exports.DatabaseTransactionError = exports.DatabaseQueryError = exports.DatabaseConnectionError = exports.DatabaseAccessDeniedError = exports.DatabaseNotFoundError = exports.DatabaseError = void 0;
|
|
23
|
+
exports.isDatabaseError = isDatabaseError;
|
|
24
|
+
exports.isDatabaseErrorType = isDatabaseErrorType;
|
|
25
|
+
// ============================================================================
|
|
26
|
+
// Base Error Class
|
|
27
|
+
// ============================================================================
|
|
28
|
+
/**
|
|
29
|
+
* Base error class for all Database errors
|
|
30
|
+
*
|
|
31
|
+
* All Database errors extend this class and include:
|
|
32
|
+
* - databaseCode: The database code that caused the error
|
|
33
|
+
* - operation: The operation that failed
|
|
34
|
+
* - timestamp: When the error occurred
|
|
35
|
+
*/
|
|
36
|
+
class DatabaseError extends Error {
|
|
37
|
+
constructor(databaseCode, operation, message) {
|
|
38
|
+
super(message);
|
|
39
|
+
this.name = this.constructor.name;
|
|
40
|
+
this.databaseCode = databaseCode;
|
|
41
|
+
this.operation = operation;
|
|
42
|
+
this.timestamp = new Date();
|
|
43
|
+
// Maintains proper stack trace for where error was thrown
|
|
44
|
+
if (Error.captureStackTrace) {
|
|
45
|
+
Error.captureStackTrace(this, this.constructor);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Serialize error for logging
|
|
50
|
+
*/
|
|
51
|
+
toJSON() {
|
|
52
|
+
return {
|
|
53
|
+
name: this.name,
|
|
54
|
+
message: this.message,
|
|
55
|
+
databaseCode: this.databaseCode,
|
|
56
|
+
operation: this.operation,
|
|
57
|
+
timestamp: this.timestamp.toISOString(),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.DatabaseError = DatabaseError;
|
|
62
|
+
// ============================================================================
|
|
63
|
+
// Specific Error Classes
|
|
64
|
+
// ============================================================================
|
|
65
|
+
/**
|
|
66
|
+
* Error thrown when a database is not found
|
|
67
|
+
*
|
|
68
|
+
* Thrown when DatabaseConnection.connect() is called with a database code
|
|
69
|
+
* that doesn't exist in the tenant's namespace.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```typescript
|
|
73
|
+
* try {
|
|
74
|
+
* const db = await ketrics.DatabaseConnection.connect('unknown-db');
|
|
75
|
+
* } catch (error) {
|
|
76
|
+
* if (error instanceof ketrics.DatabaseNotFoundError) {
|
|
77
|
+
* console.log(`Database '${error.databaseCode}' not found`);
|
|
78
|
+
* }
|
|
79
|
+
* }
|
|
80
|
+
* ```
|
|
81
|
+
*/
|
|
82
|
+
class DatabaseNotFoundError extends DatabaseError {
|
|
83
|
+
constructor(databaseCode) {
|
|
84
|
+
super(databaseCode, 'connect', `Database '${databaseCode}' not found`);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
exports.DatabaseNotFoundError = DatabaseNotFoundError;
|
|
88
|
+
/**
|
|
89
|
+
* Error thrown when application does not have access to a database
|
|
90
|
+
*
|
|
91
|
+
* Thrown when DatabaseConnection.connect() is called for a database that exists
|
|
92
|
+
* but the executing application has not been granted access.
|
|
93
|
+
*
|
|
94
|
+
* @example
|
|
95
|
+
* ```typescript
|
|
96
|
+
* try {
|
|
97
|
+
* const db = await ketrics.DatabaseConnection.connect('restricted-db');
|
|
98
|
+
* } catch (error) {
|
|
99
|
+
* if (error instanceof ketrics.DatabaseAccessDeniedError) {
|
|
100
|
+
* console.log(`No access to database '${error.databaseCode}'`);
|
|
101
|
+
* }
|
|
102
|
+
* }
|
|
103
|
+
* ```
|
|
104
|
+
*/
|
|
105
|
+
class DatabaseAccessDeniedError extends DatabaseError {
|
|
106
|
+
constructor(databaseCode) {
|
|
107
|
+
super(databaseCode, 'connect', `Application does not have access to database '${databaseCode}'`);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
exports.DatabaseAccessDeniedError = DatabaseAccessDeniedError;
|
|
111
|
+
/**
|
|
112
|
+
* Error thrown when a database connection cannot be established
|
|
113
|
+
*
|
|
114
|
+
* Thrown when the database connection cannot be established due to
|
|
115
|
+
* network issues, authentication failures, or server unavailability.
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* ```typescript
|
|
119
|
+
* try {
|
|
120
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
121
|
+
* } catch (error) {
|
|
122
|
+
* if (error instanceof ketrics.DatabaseConnectionError) {
|
|
123
|
+
* console.log(`Connection failed: ${error.reason}`);
|
|
124
|
+
* }
|
|
125
|
+
* }
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
class DatabaseConnectionError extends DatabaseError {
|
|
129
|
+
constructor(databaseCode, reason) {
|
|
130
|
+
super(databaseCode, 'connect', `Failed to connect to database '${databaseCode}': ${reason}`);
|
|
131
|
+
this.reason = reason;
|
|
132
|
+
}
|
|
133
|
+
toJSON() {
|
|
134
|
+
return {
|
|
135
|
+
...super.toJSON(),
|
|
136
|
+
reason: this.reason,
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
exports.DatabaseConnectionError = DatabaseConnectionError;
|
|
141
|
+
/**
|
|
142
|
+
* Error thrown when a database query fails
|
|
143
|
+
*
|
|
144
|
+
* Thrown when a SQL query fails to execute due to syntax errors,
|
|
145
|
+
* constraint violations, or other database-level errors.
|
|
146
|
+
*
|
|
147
|
+
* @example
|
|
148
|
+
* ```typescript
|
|
149
|
+
* try {
|
|
150
|
+
* await db.query('SELECT * FROM nonexistent_table');
|
|
151
|
+
* } catch (error) {
|
|
152
|
+
* if (error instanceof ketrics.DatabaseQueryError) {
|
|
153
|
+
* console.log(`Query failed: ${error.reason}`);
|
|
154
|
+
* }
|
|
155
|
+
* }
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
class DatabaseQueryError extends DatabaseError {
|
|
159
|
+
constructor(databaseCode, reason, sql) {
|
|
160
|
+
super(databaseCode, 'query', `Query failed: ${reason}`);
|
|
161
|
+
this.reason = reason;
|
|
162
|
+
// Only include SQL if it's safe (no sensitive data)
|
|
163
|
+
if (sql && sql.length <= 200) {
|
|
164
|
+
this.sql = sql;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
toJSON() {
|
|
168
|
+
return {
|
|
169
|
+
...super.toJSON(),
|
|
170
|
+
reason: this.reason,
|
|
171
|
+
...(this.sql && { sql: this.sql }),
|
|
172
|
+
};
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
exports.DatabaseQueryError = DatabaseQueryError;
|
|
176
|
+
/**
|
|
177
|
+
* Error thrown when a database transaction fails
|
|
178
|
+
*
|
|
179
|
+
* Thrown when a transaction fails to commit or encounters an error
|
|
180
|
+
* during execution that requires rollback.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```typescript
|
|
184
|
+
* try {
|
|
185
|
+
* await db.transaction(async (tx) => {
|
|
186
|
+
* await tx.execute('INSERT INTO users VALUES (...)');
|
|
187
|
+
* throw new Error('Simulated failure');
|
|
188
|
+
* });
|
|
189
|
+
* } catch (error) {
|
|
190
|
+
* if (error instanceof ketrics.DatabaseTransactionError) {
|
|
191
|
+
* console.log(`Transaction failed: ${error.reason}`);
|
|
192
|
+
* console.log(`Rolled back: ${error.rolledBack}`);
|
|
193
|
+
* }
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*/
|
|
197
|
+
class DatabaseTransactionError extends DatabaseError {
|
|
198
|
+
constructor(databaseCode, reason, rolledBack = true) {
|
|
199
|
+
super(databaseCode, 'transaction', `Transaction failed${rolledBack ? ' (rolled back)' : ''}: ${reason}`);
|
|
200
|
+
this.reason = reason;
|
|
201
|
+
this.rolledBack = rolledBack;
|
|
202
|
+
}
|
|
203
|
+
toJSON() {
|
|
204
|
+
return {
|
|
205
|
+
...super.toJSON(),
|
|
206
|
+
reason: this.reason,
|
|
207
|
+
rolledBack: this.rolledBack,
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
exports.DatabaseTransactionError = DatabaseTransactionError;
|
|
212
|
+
// ============================================================================
|
|
213
|
+
// Type Guards
|
|
214
|
+
// ============================================================================
|
|
215
|
+
/**
|
|
216
|
+
* Type guard to check if an error is a DatabaseError
|
|
217
|
+
*
|
|
218
|
+
* @param error - The error to check
|
|
219
|
+
* @returns True if the error is a DatabaseError
|
|
220
|
+
*
|
|
221
|
+
* @example
|
|
222
|
+
* ```typescript
|
|
223
|
+
* try {
|
|
224
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
225
|
+
* } catch (error) {
|
|
226
|
+
* if (isDatabaseError(error)) {
|
|
227
|
+
* console.log(`Database error: ${error.databaseCode}`);
|
|
228
|
+
* }
|
|
229
|
+
* }
|
|
230
|
+
* ```
|
|
231
|
+
*/
|
|
232
|
+
function isDatabaseError(error) {
|
|
233
|
+
return error instanceof DatabaseError;
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* Type guard to check if an error is a specific DatabaseError type
|
|
237
|
+
*
|
|
238
|
+
* @param error - The error to check
|
|
239
|
+
* @param errorClass - The error class to check against
|
|
240
|
+
* @returns True if the error is an instance of the specified class
|
|
241
|
+
*
|
|
242
|
+
* @example
|
|
243
|
+
* ```typescript
|
|
244
|
+
* try {
|
|
245
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
246
|
+
* } catch (error) {
|
|
247
|
+
* if (isDatabaseErrorType(error, DatabaseNotFoundError)) {
|
|
248
|
+
* console.log('Database not found');
|
|
249
|
+
* } else if (isDatabaseErrorType(error, DatabaseAccessDeniedError)) {
|
|
250
|
+
* console.log('Access denied');
|
|
251
|
+
* }
|
|
252
|
+
* }
|
|
253
|
+
* ```
|
|
254
|
+
*/
|
|
255
|
+
function isDatabaseErrorType(error, errorClass) {
|
|
256
|
+
return error instanceof errorClass;
|
|
257
|
+
}
|
|
258
|
+
//# sourceMappingURL=database-errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"database-errors.js","sourceRoot":"","sources":["../src/database-errors.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ketrics SDK - Database Interfaces
|
|
3
|
+
*
|
|
4
|
+
* Provides type definitions for accessing external database connections
|
|
5
|
+
* from tenant applications.
|
|
6
|
+
*
|
|
7
|
+
* Usage in tenant code:
|
|
8
|
+
* ```typescript
|
|
9
|
+
* // Connect to a database
|
|
10
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
11
|
+
*
|
|
12
|
+
* // Query data
|
|
13
|
+
* const result = await db.query('SELECT * FROM users WHERE id = ?', [1]);
|
|
14
|
+
* console.log(result.rows);
|
|
15
|
+
*
|
|
16
|
+
* // Execute statement
|
|
17
|
+
* await db.execute('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']);
|
|
18
|
+
*
|
|
19
|
+
* // Transaction
|
|
20
|
+
* await db.transaction(async (tx) => {
|
|
21
|
+
* await tx.execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
|
|
22
|
+
* await tx.execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
|
|
23
|
+
* });
|
|
24
|
+
*
|
|
25
|
+
* // Close connection
|
|
26
|
+
* await db.close();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
/**
|
|
30
|
+
* Database Query Result
|
|
31
|
+
*
|
|
32
|
+
* Result from executing a SELECT query.
|
|
33
|
+
*/
|
|
34
|
+
export interface DatabaseQueryResult<T = unknown> {
|
|
35
|
+
/** Result rows */
|
|
36
|
+
rows: T[];
|
|
37
|
+
/** Number of rows returned */
|
|
38
|
+
rowCount: number;
|
|
39
|
+
/** Affected rows (for INSERT/UPDATE/DELETE) */
|
|
40
|
+
affectedRows?: number;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Database Execute Result
|
|
44
|
+
*
|
|
45
|
+
* Result from executing an INSERT, UPDATE, or DELETE statement.
|
|
46
|
+
*/
|
|
47
|
+
export interface DatabaseExecuteResult {
|
|
48
|
+
/** Number of affected rows */
|
|
49
|
+
affectedRows: number;
|
|
50
|
+
/** Insert ID (if applicable) */
|
|
51
|
+
insertId?: number | string;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Database Connection Interface
|
|
55
|
+
*
|
|
56
|
+
* Interface for executing SQL queries against external databases.
|
|
57
|
+
* Use `DatabaseConnection.connect()` to obtain an instance.
|
|
58
|
+
*
|
|
59
|
+
* @example
|
|
60
|
+
* ```typescript
|
|
61
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
62
|
+
* try {
|
|
63
|
+
* const result = await db.query<User>('SELECT * FROM users WHERE id = ?', [1]);
|
|
64
|
+
* console.log(result.rows);
|
|
65
|
+
* } finally {
|
|
66
|
+
* await db.close();
|
|
67
|
+
* }
|
|
68
|
+
* ```
|
|
69
|
+
*/
|
|
70
|
+
export interface IDatabaseConnection {
|
|
71
|
+
/** Database code (read-only) */
|
|
72
|
+
readonly code: string;
|
|
73
|
+
/** Granted permissions for this database (read-only) */
|
|
74
|
+
readonly permissions: ReadonlySet<string>;
|
|
75
|
+
/**
|
|
76
|
+
* Execute a SELECT query
|
|
77
|
+
*
|
|
78
|
+
* @param sql - SQL query string with parameter placeholders
|
|
79
|
+
* @param params - Optional array of parameter values
|
|
80
|
+
* @returns Query result with rows and row count
|
|
81
|
+
* @throws DatabaseQueryError if query fails
|
|
82
|
+
*
|
|
83
|
+
* @example
|
|
84
|
+
* ```typescript
|
|
85
|
+
* const result = await db.query('SELECT * FROM users WHERE age > ?', [18]);
|
|
86
|
+
* console.log(result.rows); // [{ id: 1, name: 'John', age: 25 }, ...]
|
|
87
|
+
* console.log(result.rowCount); // 10
|
|
88
|
+
* ```
|
|
89
|
+
*/
|
|
90
|
+
query<T = unknown>(sql: string, params?: unknown[]): Promise<DatabaseQueryResult<T>>;
|
|
91
|
+
/**
|
|
92
|
+
* Execute an INSERT, UPDATE, or DELETE statement
|
|
93
|
+
*
|
|
94
|
+
* @param sql - SQL statement string with parameter placeholders
|
|
95
|
+
* @param params - Optional array of parameter values
|
|
96
|
+
* @returns Execute result with affected row count
|
|
97
|
+
* @throws DatabaseQueryError if statement fails
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
* ```typescript
|
|
101
|
+
* const result = await db.execute(
|
|
102
|
+
* 'INSERT INTO users (name, email) VALUES (?, ?)',
|
|
103
|
+
* ['John', 'john@example.com']
|
|
104
|
+
* );
|
|
105
|
+
* console.log(result.affectedRows); // 1
|
|
106
|
+
* console.log(result.insertId); // 123
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
109
|
+
execute(sql: string, params?: unknown[]): Promise<DatabaseExecuteResult>;
|
|
110
|
+
/**
|
|
111
|
+
* Execute operations within a transaction
|
|
112
|
+
*
|
|
113
|
+
* The transaction automatically commits if the function completes successfully,
|
|
114
|
+
* or rolls back if an error is thrown.
|
|
115
|
+
*
|
|
116
|
+
* @param fn - Async function that receives a transaction connection
|
|
117
|
+
* @returns Result of the transaction function
|
|
118
|
+
* @throws DatabaseTransactionError if transaction fails
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* ```typescript
|
|
122
|
+
* await db.transaction(async (tx) => {
|
|
123
|
+
* await tx.execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
|
|
124
|
+
* await tx.execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
*/
|
|
128
|
+
transaction<T>(fn: (tx: IDatabaseConnection) => Promise<T>): Promise<T>;
|
|
129
|
+
/**
|
|
130
|
+
* Close the database connection
|
|
131
|
+
*
|
|
132
|
+
* Returns the connection to the pool for reuse.
|
|
133
|
+
* Always close connections when done to prevent pool exhaustion.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```typescript
|
|
137
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
138
|
+
* try {
|
|
139
|
+
* // Use database...
|
|
140
|
+
* } finally {
|
|
141
|
+
* await db.close();
|
|
142
|
+
* }
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
close(): Promise<void>;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* @deprecated Use `ketrics.DatabaseConnection.connect()` instead
|
|
149
|
+
*
|
|
150
|
+
* Legacy interface - maintained for backward compatibility.
|
|
151
|
+
* New code should use `ketrics.DatabaseConnection.connect()`.
|
|
152
|
+
*/
|
|
153
|
+
export interface DatabaseManager {
|
|
154
|
+
/**
|
|
155
|
+
* @deprecated Use `ketrics.DatabaseConnection.connect()` instead
|
|
156
|
+
*
|
|
157
|
+
* Connect to a database by code
|
|
158
|
+
*
|
|
159
|
+
* @param databaseCode - Database code (e.g., "main-db", "analytics-db")
|
|
160
|
+
* @returns DatabaseConnection or null if no permission
|
|
161
|
+
* @throws Error if database server not found or connection fails
|
|
162
|
+
*/
|
|
163
|
+
connect(databaseCode: string): Promise<IDatabaseConnection | null>;
|
|
164
|
+
}
|
|
165
|
+
//# sourceMappingURL=databases.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"databases.d.ts","sourceRoot":"","sources":["../src/databases.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAMH;;;;GAIG;AACH,MAAM,WAAW,mBAAmB,CAAC,CAAC,GAAG,OAAO;IAC9C,kBAAkB;IAClB,IAAI,EAAE,CAAC,EAAE,CAAC;IACV,8BAA8B;IAC9B,QAAQ,EAAE,MAAM,CAAC;IACjB,+CAA+C;IAC/C,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED;;;;GAIG;AACH,MAAM,WAAW,qBAAqB;IACpC,8BAA8B;IAC9B,YAAY,EAAE,MAAM,CAAC;IACrB,gCAAgC;IAChC,QAAQ,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;CAC5B;AAMD;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,WAAW,mBAAmB;IAClC,gCAAgC;IAChC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAEtB,wDAAwD;IACxD,QAAQ,CAAC,WAAW,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAE1C;;;;;;;;;;;;;;OAcG;IACH,KAAK,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,mBAAmB,CAAC,CAAC,CAAC,CAAC,CAAC;IAErF;;;;;;;;;;;;;;;;;OAiBG;IACH,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,qBAAqB,CAAC,CAAC;IAEzE;;;;;;;;;;;;;;;;;OAiBG;IACH,WAAW,CAAC,CAAC,EAAE,EAAE,EAAE,CAAC,EAAE,EAAE,mBAAmB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAExE;;;;;;;;;;;;;;;OAeG;IACH,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAMD;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B;;;;;;;;OAQG;IACH,OAAO,CAAC,YAAY,EAAE,MAAM,GAAG,OAAO,CAAC,mBAAmB,GAAG,IAAI,CAAC,CAAC;CACpE"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Ketrics SDK - Database Interfaces
|
|
4
|
+
*
|
|
5
|
+
* Provides type definitions for accessing external database connections
|
|
6
|
+
* from tenant applications.
|
|
7
|
+
*
|
|
8
|
+
* Usage in tenant code:
|
|
9
|
+
* ```typescript
|
|
10
|
+
* // Connect to a database
|
|
11
|
+
* const db = await ketrics.DatabaseConnection.connect('main-db');
|
|
12
|
+
*
|
|
13
|
+
* // Query data
|
|
14
|
+
* const result = await db.query('SELECT * FROM users WHERE id = ?', [1]);
|
|
15
|
+
* console.log(result.rows);
|
|
16
|
+
*
|
|
17
|
+
* // Execute statement
|
|
18
|
+
* await db.execute('INSERT INTO users (name, email) VALUES (?, ?)', ['John', 'john@example.com']);
|
|
19
|
+
*
|
|
20
|
+
* // Transaction
|
|
21
|
+
* await db.transaction(async (tx) => {
|
|
22
|
+
* await tx.execute('UPDATE accounts SET balance = balance - ? WHERE id = ?', [100, 1]);
|
|
23
|
+
* await tx.execute('UPDATE accounts SET balance = balance + ? WHERE id = ?', [100, 2]);
|
|
24
|
+
* });
|
|
25
|
+
*
|
|
26
|
+
* // Close connection
|
|
27
|
+
* await db.close();
|
|
28
|
+
* ```
|
|
29
|
+
*/
|
|
30
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
31
|
+
//# sourceMappingURL=databases.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"databases.js","sourceRoot":"","sources":["../src/databases.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG"}
|