@iskra-bun/db-kit 0.1.0 → 0.3.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/CHANGELOG.md +74 -0
- package/README.md +9 -3
- package/dist/index.d.ts +139 -10
- package/dist/index.js +319 -107
- package/dist/index.js.map +1 -1
- package/package.json +9 -6
- package/src/cli.ts +11 -2
- package/src/driver.ts +299 -43
- package/src/drizzle.config.template.ts +16 -5
- package/src/errors.ts +1 -1
- package/src/index.ts +1 -1
- package/src/migrations.ts +78 -11
- package/src/secrets.ts +2 -0
package/dist/index.js
CHANGED
|
@@ -4,6 +4,8 @@ import { drizzle } from "drizzle-orm/postgres-js";
|
|
|
4
4
|
import { drizzle as drizzleMysql } from "drizzle-orm/mysql2";
|
|
5
5
|
import postgres from "postgres";
|
|
6
6
|
import mysql from "mysql2/promise";
|
|
7
|
+
import { sql } from "drizzle-orm";
|
|
8
|
+
import { AsyncLocalStorage } from "async_hooks";
|
|
7
9
|
|
|
8
10
|
// src/errors.ts
|
|
9
11
|
import { IskraError, ErrorCodes } from "@iskra-bun/core";
|
|
@@ -26,7 +28,290 @@ var MigrationError = class extends IskraError {
|
|
|
26
28
|
}
|
|
27
29
|
};
|
|
28
30
|
|
|
31
|
+
// src/secrets.ts
|
|
32
|
+
var SENSITIVE_URL_PARAM = /pass|token|secret|key|auth|credential|signature/i;
|
|
33
|
+
|
|
34
|
+
// src/driver.ts
|
|
35
|
+
function scrubUrl(url) {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = new URL(url);
|
|
38
|
+
if (parsed.username) parsed.username = "***";
|
|
39
|
+
if (parsed.password) parsed.password = "***";
|
|
40
|
+
for (const key of [...new Set(parsed.searchParams.keys())]) {
|
|
41
|
+
if (SENSITIVE_URL_PARAM.test(key)) parsed.searchParams.set(key, "***");
|
|
42
|
+
}
|
|
43
|
+
return parsed.toString();
|
|
44
|
+
} catch {
|
|
45
|
+
return void 0;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
var DbDriver = class {
|
|
49
|
+
name = "db";
|
|
50
|
+
client;
|
|
51
|
+
/** Serializes transactions on the single bun:sqlite connection. */
|
|
52
|
+
sqliteTxQueue = Promise.resolve();
|
|
53
|
+
inSqliteTx = new AsyncLocalStorage();
|
|
54
|
+
db;
|
|
55
|
+
app;
|
|
56
|
+
onQuery;
|
|
57
|
+
/**
|
|
58
|
+
* Register an observability callback that receives every SQL statement (and
|
|
59
|
+
* its bound params) Drizzle executes. Must be called before {@link start},
|
|
60
|
+
* since Drizzle's logger is wired at connection time. A throwing callback is
|
|
61
|
+
* swallowed so observability never breaks a real query.
|
|
62
|
+
*/
|
|
63
|
+
setOnQuery(onQuery) {
|
|
64
|
+
this.onQuery = onQuery;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Build the Drizzle `logger` option that forwards to {@link onQuery} when a
|
|
68
|
+
* hook is registered, or `undefined` to leave Drizzle's default logging off.
|
|
69
|
+
*/
|
|
70
|
+
buildLogger() {
|
|
71
|
+
const hook = this.onQuery;
|
|
72
|
+
if (!hook) return void 0;
|
|
73
|
+
return {
|
|
74
|
+
logQuery: (query, params) => {
|
|
75
|
+
try {
|
|
76
|
+
hook(query, params);
|
|
77
|
+
} catch {
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
async init(app) {
|
|
83
|
+
this.app = app;
|
|
84
|
+
app.context.set("db", this);
|
|
85
|
+
}
|
|
86
|
+
async start() {
|
|
87
|
+
if (!this.app) return;
|
|
88
|
+
const config = this.app.config.db;
|
|
89
|
+
if (!config) {
|
|
90
|
+
this.app.logger.warn("No DB configuration found. Skipping DB initialization.");
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
this.app.logger.info(`Initializing DB driver: ${config.driver}`);
|
|
94
|
+
const logger = this.buildLogger();
|
|
95
|
+
try {
|
|
96
|
+
switch (config.driver) {
|
|
97
|
+
case "postgres": {
|
|
98
|
+
const client = postgres(config.url);
|
|
99
|
+
this.client = client;
|
|
100
|
+
this.db = drizzle(client, { logger });
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case "mysql": {
|
|
104
|
+
const client = mysql.createPool(config.url);
|
|
105
|
+
this.client = client;
|
|
106
|
+
this.db = drizzleMysql(client, { logger });
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
case "sqlite": {
|
|
110
|
+
const { Database } = await import("bun:sqlite");
|
|
111
|
+
const { drizzle: drizzleSqlite } = await import("drizzle-orm/bun-sqlite");
|
|
112
|
+
const client = new Database(config.url);
|
|
113
|
+
this.client = client;
|
|
114
|
+
this.db = drizzleSqlite(client, { logger });
|
|
115
|
+
break;
|
|
116
|
+
}
|
|
117
|
+
case "libsql": {
|
|
118
|
+
const { createClient } = await import("@libsql/client");
|
|
119
|
+
const { drizzle: drizzleLibsql } = await import("drizzle-orm/libsql");
|
|
120
|
+
const client = createClient({ url: config.url, authToken: config.authToken });
|
|
121
|
+
this.client = client;
|
|
122
|
+
this.db = drizzleLibsql(client, { logger });
|
|
123
|
+
break;
|
|
124
|
+
}
|
|
125
|
+
default:
|
|
126
|
+
throw new DriverError(`Unsupported DB driver: ${config.driver}`, {
|
|
127
|
+
code: "DRIVER_START_FAILED",
|
|
128
|
+
context: { driver: config.driver }
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
await this.roundTrip();
|
|
132
|
+
this.app.logger.info("DB connected successfully.");
|
|
133
|
+
} catch (error) {
|
|
134
|
+
if (error instanceof DriverError) throw error;
|
|
135
|
+
await this.stop();
|
|
136
|
+
this.app.logger.error({ error }, "Failed to connect to DB");
|
|
137
|
+
const safeUrl = scrubUrl(config.url);
|
|
138
|
+
throw new ConnectionError("Failed to connect to DB", {
|
|
139
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
140
|
+
context: {
|
|
141
|
+
driver: config.driver,
|
|
142
|
+
...safeUrl !== void 0 ? { url: safeUrl } : {}
|
|
143
|
+
}
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Applies the pending migrations in `migrationsDir` (generated with
|
|
149
|
+
* `drizzle-kit generate`) over the live connection, using Drizzle's
|
|
150
|
+
* migrator for the configured dialect. Requires `start()`.
|
|
151
|
+
*
|
|
152
|
+
* It used to shell out to `drizzle-kit migrate`, which ignored both
|
|
153
|
+
* arguments and failed without a drizzle.config.ts. `schemaPath` is kept for
|
|
154
|
+
* compatibility; applying migrations does not need the schema.
|
|
155
|
+
*/
|
|
156
|
+
async runMigrations(_schemaPath, migrationsDir = "./drizzle") {
|
|
157
|
+
const config = this.app?.config.db;
|
|
158
|
+
if (!config) {
|
|
159
|
+
throw new DriverError("Cannot run migrations: no DB configuration found", {
|
|
160
|
+
code: "DRIVER_START_FAILED"
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (!this.db) {
|
|
164
|
+
throw new DriverError("Cannot run migrations: DB is not started", {
|
|
165
|
+
code: "DRIVER_START_FAILED",
|
|
166
|
+
context: { driver: config.driver }
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const options = { migrationsFolder: migrationsDir };
|
|
170
|
+
const db = this.db;
|
|
171
|
+
try {
|
|
172
|
+
switch (config.driver) {
|
|
173
|
+
case "postgres":
|
|
174
|
+
await (await import("drizzle-orm/postgres-js/migrator")).migrate(db, options);
|
|
175
|
+
break;
|
|
176
|
+
case "mysql":
|
|
177
|
+
await (await import("drizzle-orm/mysql2/migrator")).migrate(db, options);
|
|
178
|
+
break;
|
|
179
|
+
case "sqlite":
|
|
180
|
+
(await import("drizzle-orm/bun-sqlite/migrator")).migrate(db, options);
|
|
181
|
+
break;
|
|
182
|
+
case "libsql":
|
|
183
|
+
await (await import("drizzle-orm/libsql/migrator")).migrate(db, options);
|
|
184
|
+
break;
|
|
185
|
+
}
|
|
186
|
+
this.app.logger.info({ migrationsDir }, "Migrations applied");
|
|
187
|
+
} catch (error) {
|
|
188
|
+
throw new MigrationError("Failed to apply migrations", {
|
|
189
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
190
|
+
context: { driver: config.driver, migrationsDir }
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Run `fn` inside a database transaction, delegating to Drizzle's
|
|
196
|
+
* `db.transaction`. Callers receive the transaction-scoped db handle instead
|
|
197
|
+
* of reaching into the raw `db`. The dialect union means `tx` is typed as
|
|
198
|
+
* {@link IskraDrizzleTx}; narrow by dialect if you need dialect-specific APIs.
|
|
199
|
+
* Failures are wrapped in {@link QueryError}; the transaction is rolled back.
|
|
200
|
+
*
|
|
201
|
+
* With `sqlite`, transactions run one at a time on the single connection,
|
|
202
|
+
* and a query made outside `tx` while one is open is part of it. A nested
|
|
203
|
+
* `transaction()` call is rejected: it would wait for itself.
|
|
204
|
+
*/
|
|
205
|
+
async transaction(fn) {
|
|
206
|
+
if (!this.db) {
|
|
207
|
+
throw new QueryError("Cannot run transaction: DB is not started", {
|
|
208
|
+
context: { driver: this.app?.config.db?.driver }
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
if (this.app?.config.db?.driver === "sqlite") return await this.sqliteTransaction(fn);
|
|
213
|
+
return await this.db.transaction((tx) => fn(tx));
|
|
214
|
+
} catch (error) {
|
|
215
|
+
if (error instanceof QueryError) throw error;
|
|
216
|
+
throw new QueryError("Transaction failed", {
|
|
217
|
+
cause: error instanceof Error ? error : new Error(String(error)),
|
|
218
|
+
context: { driver: this.app?.config.db?.driver }
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Drizzle's bun-sqlite `transaction()` is synchronous: it commits as soon
|
|
224
|
+
* as the callback returns its promise, so an async callback that threw
|
|
225
|
+
* afterwards never rolled back. The transaction is opened and closed here
|
|
226
|
+
* around the awaited callback instead.
|
|
227
|
+
*/
|
|
228
|
+
async sqliteTransaction(fn) {
|
|
229
|
+
if (this.inSqliteTx.getStore()) {
|
|
230
|
+
throw new QueryError("Nested transaction() calls are not supported with sqlite", {
|
|
231
|
+
context: { driver: "sqlite" }
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
const db = this.db;
|
|
235
|
+
const run = () => this.inSqliteTx.run(true, async () => {
|
|
236
|
+
const tx = db.transaction((t) => t);
|
|
237
|
+
db.run(sql`begin`);
|
|
238
|
+
try {
|
|
239
|
+
const result2 = await fn(tx);
|
|
240
|
+
db.run(sql`commit`);
|
|
241
|
+
return result2;
|
|
242
|
+
} catch (error) {
|
|
243
|
+
db.run(sql`rollback`);
|
|
244
|
+
throw error;
|
|
245
|
+
}
|
|
246
|
+
});
|
|
247
|
+
const result = this.sqliteTxQueue.then(run, run);
|
|
248
|
+
this.sqliteTxQueue = result.catch(() => void 0);
|
|
249
|
+
return result;
|
|
250
|
+
}
|
|
251
|
+
/**
|
|
252
|
+
* Liveness probe for readiness checks (e.g. web-kit's addReadinessCheck /
|
|
253
|
+
* k8s readiness). Runs a trivial `SELECT 1` against the active dialect and
|
|
254
|
+
* resolves `true` on success or `false` on any failure — it never rejects.
|
|
255
|
+
*/
|
|
256
|
+
async ping() {
|
|
257
|
+
if (!this.db) return false;
|
|
258
|
+
try {
|
|
259
|
+
await this.roundTrip();
|
|
260
|
+
return true;
|
|
261
|
+
} catch {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
/** `SELECT 1` on the active connection; throws on failure. */
|
|
266
|
+
async roundTrip() {
|
|
267
|
+
const handle = this.db;
|
|
268
|
+
if (typeof handle.run === "function") {
|
|
269
|
+
await handle.run(sql`SELECT 1`);
|
|
270
|
+
} else if (typeof handle.execute === "function") {
|
|
271
|
+
await handle.execute(sql`SELECT 1`);
|
|
272
|
+
} else {
|
|
273
|
+
throw new Error("DB handle exposes neither run() nor execute()");
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
async stop() {
|
|
277
|
+
const client = this.client;
|
|
278
|
+
try {
|
|
279
|
+
if (client?.end) {
|
|
280
|
+
await client.end();
|
|
281
|
+
} else if (client?.close) {
|
|
282
|
+
client.close();
|
|
283
|
+
}
|
|
284
|
+
} catch (error) {
|
|
285
|
+
this.app?.logger.error({ error }, "Failed to close DB connection cleanly");
|
|
286
|
+
} finally {
|
|
287
|
+
this.client = void 0;
|
|
288
|
+
this.db = void 0;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
};
|
|
292
|
+
|
|
29
293
|
// src/migrations.ts
|
|
294
|
+
import { existsSync } from "fs";
|
|
295
|
+
import path from "path";
|
|
296
|
+
function drizzleKitCommand(args, cwd = process.cwd()) {
|
|
297
|
+
for (let dir = path.resolve(cwd); ; dir = path.dirname(dir)) {
|
|
298
|
+
const bin = path.join(dir, "node_modules", ".bin", "drizzle-kit");
|
|
299
|
+
if (["", ".exe", ".cmd"].some((ext) => existsSync(bin + ext))) {
|
|
300
|
+
return ["bunx", "--no-install", "drizzle-kit", ...args];
|
|
301
|
+
}
|
|
302
|
+
if (path.dirname(dir) === dir) break;
|
|
303
|
+
}
|
|
304
|
+
throw new MigrationError(
|
|
305
|
+
"drizzle-kit is not installed in this project (it is never downloaded at run time): add it with `bun add -d drizzle-kit`",
|
|
306
|
+
{ context: { cwd } }
|
|
307
|
+
);
|
|
308
|
+
}
|
|
309
|
+
function scrubCredentials(text) {
|
|
310
|
+
return text.replace(/(\/\/)(?:[^\s'"`@]*@)+/g, "$1***:***@").replace(
|
|
311
|
+
new RegExp(`([?&][^=\\s&'"\`]*(?:${SENSITIVE_URL_PARAM.source})[^=\\s&'"\`]*=)[^&\\s'"\`]*`, "gi"),
|
|
312
|
+
"$1***"
|
|
313
|
+
);
|
|
314
|
+
}
|
|
30
315
|
var MigrationHelper = class {
|
|
31
316
|
config;
|
|
32
317
|
app;
|
|
@@ -36,40 +321,59 @@ var MigrationHelper = class {
|
|
|
36
321
|
}
|
|
37
322
|
/**
|
|
38
323
|
* Genera archivos de migración basados en los cambios del schema.
|
|
324
|
+
* drizzle-kit generate soporta --schema y --out, así que ambos se reenvían
|
|
325
|
+
* desde la config (antes se ignoraban silenciosamente).
|
|
39
326
|
*/
|
|
40
327
|
async generate(name) {
|
|
41
|
-
const args = ["
|
|
328
|
+
const args = ["generate"];
|
|
329
|
+
if (this.config.schemaPath) args.push("--schema", this.config.schemaPath);
|
|
330
|
+
if (this.config.migrationsDir) args.push("--out", this.config.migrationsDir);
|
|
42
331
|
if (name) args.push("--name", name);
|
|
332
|
+
if (this.config.configPath) args.push("--config", this.config.configPath);
|
|
43
333
|
await this.exec(args, "generate");
|
|
44
334
|
}
|
|
45
335
|
/**
|
|
46
336
|
* Aplica las migraciones pendientes a la base de datos.
|
|
337
|
+
* `migrate` sólo acepta --config; schema y out no son flags válidos en este
|
|
338
|
+
* comando, por eso únicamente reenviamos configPath cuando está presente.
|
|
47
339
|
*/
|
|
48
340
|
async migrate() {
|
|
49
|
-
|
|
341
|
+
const args = ["migrate"];
|
|
342
|
+
if (this.config.configPath) args.push("--config", this.config.configPath);
|
|
343
|
+
await this.exec(args, "migrate");
|
|
50
344
|
}
|
|
51
345
|
/**
|
|
52
346
|
* Empuja el schema directamente a la base de datos (sin generar archivos de migración).
|
|
53
|
-
* Útil para desarrollo rápido.
|
|
347
|
+
* Útil para desarrollo rápido. `push` acepta --schema pero no --out.
|
|
54
348
|
*/
|
|
55
349
|
async push() {
|
|
56
|
-
|
|
350
|
+
const args = ["push"];
|
|
351
|
+
if (this.config.schemaPath) args.push("--schema", this.config.schemaPath);
|
|
352
|
+
if (this.config.configPath) args.push("--config", this.config.configPath);
|
|
353
|
+
await this.exec(args, "push");
|
|
57
354
|
}
|
|
58
355
|
/**
|
|
59
|
-
* Elimina
|
|
356
|
+
* Elimina un archivo de migración ya generado (`drizzle-kit drop`, interactivo).
|
|
357
|
+
* No borra tablas ni datos de la base.
|
|
358
|
+
* `drop` acepta --out (dónde viven las migraciones) pero no --schema.
|
|
60
359
|
*/
|
|
61
360
|
async drop() {
|
|
62
|
-
|
|
361
|
+
const args = ["drop"];
|
|
362
|
+
if (this.config.migrationsDir) args.push("--out", this.config.migrationsDir);
|
|
363
|
+
if (this.config.configPath) args.push("--config", this.config.configPath);
|
|
364
|
+
await this.exec(args, "drop");
|
|
63
365
|
}
|
|
64
366
|
async exec(args, operation) {
|
|
65
367
|
const env = {
|
|
66
368
|
...process.env,
|
|
67
369
|
DATABASE_URL: this.config.dbUrl
|
|
68
370
|
};
|
|
371
|
+
const cwd = process.cwd();
|
|
372
|
+
const command = drizzleKitCommand(args, cwd);
|
|
69
373
|
this.app?.logger.info(`Running migration: ${operation}`);
|
|
70
374
|
try {
|
|
71
|
-
const proc = Bun.spawn(
|
|
72
|
-
cwd
|
|
375
|
+
const proc = Bun.spawn(command, {
|
|
376
|
+
cwd,
|
|
73
377
|
env,
|
|
74
378
|
stdout: "pipe",
|
|
75
379
|
stderr: "pipe"
|
|
@@ -80,7 +384,7 @@ var MigrationHelper = class {
|
|
|
80
384
|
if (stdout) this.app?.logger.info(stdout.trim());
|
|
81
385
|
if (exitCode !== 0) {
|
|
82
386
|
throw new MigrationError(`Migration ${operation} failed with exit code ${exitCode}`, {
|
|
83
|
-
context: { operation, exitCode, stderr: stderr.trim() }
|
|
387
|
+
context: { operation, exitCode, stderr: scrubCredentials(stderr.trim()) }
|
|
84
388
|
});
|
|
85
389
|
}
|
|
86
390
|
this.app?.logger.info(`Migration ${operation} completed successfully`);
|
|
@@ -109,111 +413,16 @@ function mapDialect(driver) {
|
|
|
109
413
|
}
|
|
110
414
|
}
|
|
111
415
|
|
|
112
|
-
// src/driver.ts
|
|
113
|
-
var DbDriver = class {
|
|
114
|
-
name = "db";
|
|
115
|
-
client;
|
|
116
|
-
db;
|
|
117
|
-
app;
|
|
118
|
-
async init(app) {
|
|
119
|
-
this.app = app;
|
|
120
|
-
app.context.set("db", this);
|
|
121
|
-
}
|
|
122
|
-
async start() {
|
|
123
|
-
if (!this.app) return;
|
|
124
|
-
const config = this.app.config.db;
|
|
125
|
-
if (!config) {
|
|
126
|
-
this.app.logger.warn("No DB configuration found. Skipping DB initialization.");
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
this.app.logger.info(`Initializing DB driver: ${config.driver}`);
|
|
130
|
-
try {
|
|
131
|
-
switch (config.driver) {
|
|
132
|
-
case "postgres":
|
|
133
|
-
this.client = postgres(config.url);
|
|
134
|
-
this.db = drizzle(this.client);
|
|
135
|
-
break;
|
|
136
|
-
case "mysql":
|
|
137
|
-
this.client = await mysql.createConnection(config.url);
|
|
138
|
-
this.db = drizzleMysql(this.client);
|
|
139
|
-
break;
|
|
140
|
-
case "sqlite": {
|
|
141
|
-
const { Database } = await import("bun:sqlite");
|
|
142
|
-
const { drizzle: drizzleSqlite } = await import("drizzle-orm/bun-sqlite");
|
|
143
|
-
this.client = new Database(config.url);
|
|
144
|
-
this.db = drizzleSqlite(this.client);
|
|
145
|
-
break;
|
|
146
|
-
}
|
|
147
|
-
case "libsql": {
|
|
148
|
-
const { createClient } = await import("@libsql/client");
|
|
149
|
-
const { drizzle: drizzleLibsql } = await import("drizzle-orm/libsql");
|
|
150
|
-
this.client = createClient({ url: config.url, authToken: config.authToken });
|
|
151
|
-
this.db = drizzleLibsql(this.client);
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
default:
|
|
155
|
-
throw new DriverError(`Unsupported DB driver: ${config.driver}`, {
|
|
156
|
-
code: "DRIVER_START_FAILED",
|
|
157
|
-
context: { driver: config.driver }
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
this.app.logger.info("DB connected successfully.");
|
|
161
|
-
} catch (error) {
|
|
162
|
-
if (error instanceof DriverError) throw error;
|
|
163
|
-
this.app.logger.error({ error }, "Failed to connect to DB");
|
|
164
|
-
throw new ConnectionError("Failed to connect to DB", {
|
|
165
|
-
cause: error instanceof Error ? error : new Error(String(error)),
|
|
166
|
-
context: { driver: config.driver, url: config.url }
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
/**
|
|
171
|
-
* Ejecuta migraciones pendientes usando Drizzle Kit.
|
|
172
|
-
*/
|
|
173
|
-
async runMigrations(schemaPath, migrationsDir = "./drizzle") {
|
|
174
|
-
if (!this.app?.config.db) {
|
|
175
|
-
throw new DriverError("Cannot run migrations: no DB configuration found", {
|
|
176
|
-
code: "DRIVER_START_FAILED"
|
|
177
|
-
});
|
|
178
|
-
}
|
|
179
|
-
const config = this.app.config.db;
|
|
180
|
-
const helper = new MigrationHelper(
|
|
181
|
-
{
|
|
182
|
-
dialect: mapDialect(config.driver),
|
|
183
|
-
dbUrl: config.url,
|
|
184
|
-
schemaPath,
|
|
185
|
-
migrationsDir
|
|
186
|
-
},
|
|
187
|
-
this.app
|
|
188
|
-
);
|
|
189
|
-
await helper.migrate();
|
|
190
|
-
}
|
|
191
|
-
async stop() {
|
|
192
|
-
if (this.client) {
|
|
193
|
-
if (this.client.end) {
|
|
194
|
-
await this.client.end();
|
|
195
|
-
} else if (this.client.close) {
|
|
196
|
-
this.client.close();
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
};
|
|
201
|
-
|
|
202
|
-
// ../../node_modules/drizzle-kit/index.mjs
|
|
203
|
-
function defineConfig(config) {
|
|
204
|
-
return config;
|
|
205
|
-
}
|
|
206
|
-
|
|
207
416
|
// src/drizzle.config.template.ts
|
|
208
417
|
function createDrizzleConfig(options) {
|
|
209
|
-
return
|
|
418
|
+
return {
|
|
210
419
|
dialect: options.dialect,
|
|
211
420
|
schema: options.schemaPath,
|
|
212
421
|
out: options.migrationsDir || "./drizzle",
|
|
213
422
|
dbCredentials: {
|
|
214
423
|
url: options.dbUrl
|
|
215
424
|
}
|
|
216
|
-
}
|
|
425
|
+
};
|
|
217
426
|
}
|
|
218
427
|
export {
|
|
219
428
|
ConnectionError,
|
|
@@ -222,6 +431,9 @@ export {
|
|
|
222
431
|
MigrationHelper,
|
|
223
432
|
QueryError,
|
|
224
433
|
createDrizzleConfig,
|
|
225
|
-
|
|
434
|
+
drizzleKitCommand,
|
|
435
|
+
mapDialect,
|
|
436
|
+
scrubCredentials,
|
|
437
|
+
scrubUrl
|
|
226
438
|
};
|
|
227
439
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/driver.ts","../src/errors.ts","../src/migrations.ts","../../../node_modules/drizzle-kit/index.mjs","../src/drizzle.config.template.ts"],"sourcesContent":["import { type App, type Driver, type AppConfig, DriverError } from '@iskra-bun/core';\nimport { drizzle } from 'drizzle-orm/postgres-js';\nimport { drizzle as drizzleMysql } from 'drizzle-orm/mysql2';\nimport postgres from 'postgres';\nimport mysql from 'mysql2/promise';\nimport { ConnectionError } from './errors';\nimport { MigrationHelper, mapDialect } from './migrations';\n\nexport class DbDriver implements Driver {\n name = 'db';\n private client: any;\n public db: any;\n\n private app: App | undefined;\n\n async init(app: App) {\n this.app = app;\n app.context.set('db', this);\n }\n\n async start() {\n if (!this.app) return;\n const config = this.app.config.db;\n if (!config) {\n this.app!.logger.warn('No DB configuration found. Skipping DB initialization.');\n return;\n }\n\n this.app!.logger.info(`Initializing DB driver: ${config.driver}`);\n\n try {\n switch (config.driver) {\n case 'postgres':\n this.client = postgres(config.url);\n this.db = drizzle(this.client);\n break;\n case 'mysql':\n this.client = await mysql.createConnection(config.url);\n this.db = drizzleMysql(this.client);\n break;\n case 'sqlite': {\n const { Database } = await import(\"bun:sqlite\");\n const { drizzle: drizzleSqlite } = await import(\"drizzle-orm/bun-sqlite\");\n this.client = new Database(config.url);\n this.db = drizzleSqlite(this.client);\n break;\n }\n case 'libsql': {\n const { createClient } = await import('@libsql/client');\n const { drizzle: drizzleLibsql } = await import('drizzle-orm/libsql');\n this.client = createClient({ url: config.url, authToken: config.authToken });\n this.db = drizzleLibsql(this.client);\n break;\n }\n default:\n throw new DriverError(`Unsupported DB driver: ${config.driver}`, {\n code: 'DRIVER_START_FAILED',\n context: { driver: config.driver },\n });\n }\n this.app!.logger.info('DB connected successfully.');\n } catch (error) {\n if (error instanceof DriverError) throw error;\n this.app!.logger.error({ error }, 'Failed to connect to DB');\n throw new ConnectionError('Failed to connect to DB', {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: { driver: config.driver, url: config.url },\n });\n }\n }\n\n /**\n * Ejecuta migraciones pendientes usando Drizzle Kit.\n */\n async runMigrations(schemaPath: string, migrationsDir: string = './drizzle'): Promise<void> {\n if (!this.app?.config.db) {\n throw new DriverError('Cannot run migrations: no DB configuration found', {\n code: 'DRIVER_START_FAILED',\n });\n }\n\n const config = this.app.config.db;\n const helper = new MigrationHelper(\n {\n dialect: mapDialect(config.driver),\n dbUrl: config.url,\n schemaPath,\n migrationsDir,\n },\n this.app,\n );\n\n await helper.migrate();\n }\n\n async stop() {\n if (this.client) {\n // Close connections based on client type\n if (this.client.end) { // Postgres usage with postgres.js usually handles itself or has end. \n // mysql2 has end()\n await this.client.end();\n } else if (this.client.close) { // bun:sqlite / libsql\n this.client.close();\n }\n // postgres.js handles cleanup usually but explicit close might be needed depending on version/usage\n }\n }\n}\n","import { IskraError, ErrorCodes, type ErrorCode } from '@iskra-bun/core';\n\n// ─── Connection Error ────────────────────────────────────────────────────────\n\nexport class ConnectionError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.CONNECTION_ERROR, ...options });\n this.name = 'ConnectionError';\n }\n}\n\n// ─── Query Error ─────────────────────────────────────────────────────────────\n\nexport class QueryError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.QUERY_ERROR, ...options });\n this.name = 'QueryError';\n }\n}\n\n// ─── Migration Error ─────────────────────────────────────────────────────────\n\nexport class MigrationError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.MIGRATION_ERROR, ...options });\n this.name = 'MigrationError';\n }\n}\n","import { type App } from '@iskra-bun/core';\nimport { MigrationError } from './errors';\n\nexport interface MigrationConfig {\n /** Dialecto de la base de datos */\n dialect: 'postgresql' | 'mysql' | 'sqlite';\n /** URL de conexión a la base de datos */\n dbUrl: string;\n /** Ruta al archivo de schema Drizzle (ej: './src/db/schema.ts') */\n schemaPath: string;\n /** Directorio donde se generan las migraciones (ej: './drizzle') */\n migrationsDir: string;\n}\n\n/**\n * Helper para ejecutar migraciones de Drizzle Kit.\n * Usa `bunx drizzle-kit` como subproceso para generar y aplicar migraciones.\n */\nexport class MigrationHelper {\n private config: MigrationConfig;\n private app?: App;\n\n constructor(config: MigrationConfig, app?: App) {\n this.config = config;\n this.app = app;\n }\n\n /**\n * Genera archivos de migración basados en los cambios del schema.\n */\n async generate(name?: string): Promise<void> {\n const args = ['drizzle-kit', 'generate'];\n if (name) args.push('--name', name);\n await this.exec(args, 'generate');\n }\n\n /**\n * Aplica las migraciones pendientes a la base de datos.\n */\n async migrate(): Promise<void> {\n await this.exec(['drizzle-kit', 'migrate'], 'migrate');\n }\n\n /**\n * Empuja el schema directamente a la base de datos (sin generar archivos de migración).\n * Útil para desarrollo rápido.\n */\n async push(): Promise<void> {\n await this.exec(['drizzle-kit', 'push'], 'push');\n }\n\n /**\n * Elimina todas las tablas de la base de datos.\n */\n async drop(): Promise<void> {\n await this.exec(['drizzle-kit', 'drop'], 'drop');\n }\n\n private async exec(args: string[], operation: string): Promise<void> {\n const env: Record<string, string> = {\n ...process.env as Record<string, string>,\n DATABASE_URL: this.config.dbUrl,\n };\n\n this.app?.logger.info(`Running migration: ${operation}`);\n\n try {\n const proc = Bun.spawn(['bunx', ...args], {\n cwd: process.cwd(),\n env,\n stdout: 'pipe',\n stderr: 'pipe',\n });\n\n const exitCode = await proc.exited;\n const stdout = await new Response(proc.stdout).text();\n const stderr = await new Response(proc.stderr).text();\n\n if (stdout) this.app?.logger.info(stdout.trim());\n\n if (exitCode !== 0) {\n throw new MigrationError(`Migration ${operation} failed with exit code ${exitCode}`, {\n context: { operation, exitCode, stderr: stderr.trim() },\n });\n }\n\n this.app?.logger.info(`Migration ${operation} completed successfully`);\n } catch (error) {\n if (error instanceof MigrationError) throw error;\n throw new MigrationError(`Migration ${operation} failed`, {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: { operation },\n });\n }\n }\n}\n\n/**\n * Mapea el driver de Iskra al dialecto de Drizzle Kit.\n */\nexport function mapDialect(driver: string): MigrationConfig['dialect'] {\n switch (driver) {\n case 'postgres':\n return 'postgresql';\n case 'mysql':\n return 'mysql';\n case 'sqlite':\n case 'libsql':\n return 'sqlite';\n default:\n throw new MigrationError(`Cannot map driver \"${driver}\" to a Drizzle dialect`, {\n context: { driver },\n });\n }\n}\n","// src/index.ts\nfunction defineConfig(config) {\n return config;\n}\nexport {\n defineConfig\n};\n","import { defineConfig } from 'drizzle-kit';\n\nexport interface DrizzleConfigOptions {\n /** Dialecto: 'postgresql', 'mysql', 'sqlite' */\n dialect: 'postgresql' | 'mysql' | 'sqlite';\n /** URL de conexión a la base de datos */\n dbUrl: string;\n /** Ruta al archivo de schema (ej: './src/db/schema.ts') */\n schemaPath: string;\n /** Directorio de migraciones (ej: './drizzle') */\n migrationsDir?: string;\n}\n\n/**\n * Crea una configuración de drizzle-kit reutilizable.\n *\n * Uso en tu proyecto:\n * ```ts\n * // drizzle.config.ts\n * import { createDrizzleConfig } from '@iskra-bun/db-kit';\n *\n * export default createDrizzleConfig({\n * dialect: 'sqlite',\n * dbUrl: process.env.DATABASE_URL || 'app.db',\n * schemaPath: './src/db/schema.ts',\n * });\n * ```\n */\nexport function createDrizzleConfig(options: DrizzleConfigOptions) {\n return defineConfig({\n dialect: options.dialect,\n schema: options.schemaPath,\n out: options.migrationsDir || './drizzle',\n dbCredentials: {\n url: options.dbUrl,\n },\n });\n}\n"],"mappings":";AAAA,SAAgD,mBAAmB;AACnE,SAAS,eA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SAAS,QAAQ;AAAA,IACjB,QAAQ,QAAQ;AAAA,IAChB,KAAK,QAAQ,iBAAiB;AAAA,IAC9B,eAAe;AAAA,MACX,KAAK,QAAQ;AAAA,IACjB;AAAA,EACJ,CAAC;AACL;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/driver.ts","../src/errors.ts","../src/secrets.ts","../src/migrations.ts","../src/drizzle.config.template.ts"],"sourcesContent":["import { type App, type Driver, DriverError } from '@iskra-bun/core';\nimport { drizzle, type PostgresJsDatabase } from 'drizzle-orm/postgres-js';\nimport { drizzle as drizzleMysql, type MySql2Database } from 'drizzle-orm/mysql2';\nimport type { BunSQLiteDatabase } from 'drizzle-orm/bun-sqlite';\nimport type { LibSQLDatabase } from 'drizzle-orm/libsql';\nimport postgres from 'postgres';\nimport mysql from 'mysql2/promise';\nimport { sql } from 'drizzle-orm';\nimport { AsyncLocalStorage } from 'node:async_hooks';\nimport { ConnectionError, MigrationError, QueryError } from './errors';\nimport { SENSITIVE_URL_PARAM } from './secrets';\n\n/**\n * Observability callback invoked for every SQL statement Drizzle executes.\n * Receives the rendered query and its bound parameters.\n */\nexport type OnQueryHook = (query: string, params: unknown[]) => void;\n\n/**\n * The transaction handle passed to {@link DbDriver.transaction}. Drizzle types\n * the transaction object per dialect, so — like {@link IskraDrizzleDb} — this is\n * the union of the supported dialect databases for the same schema. Callers can\n * narrow by dialect if they need dialect-specific transaction APIs.\n */\nexport type IskraDrizzleTx<TSchema extends Record<string, unknown> = Record<string, never>> = IskraDrizzleDb<TSchema>;\n\n/**\n * The Drizzle database handle exposed by {@link DbDriver}, parameterized by the\n * caller's schema. Because the concrete dialect is chosen at runtime, this is a\n * union of the supported dialect databases — all four share the same\n * `TSchema extends Record<string, unknown> = Record<string, never>` parameter,\n * so passing a schema types `db.query.*` for opt-in callers while the default\n * `Record<string, never>` reproduces the historical untyped behavior.\n */\nexport type IskraDrizzleDb<TSchema extends Record<string, unknown> = Record<string, never>> =\n | PostgresJsDatabase<TSchema>\n | MySql2Database<TSchema>\n | BunSQLiteDatabase<TSchema>\n | LibSQLDatabase<TSchema>;\n\n/**\n * Redact the username, password and secret query parameters (`authToken`,\n * `password`, `sslpassword`...) from a database URL so it is safe to log.\n * Returns the scrubbed URL string, or undefined if parsing fails.\n *\n * e.g. postgres://user:pass@host:5432/db → postgres://***:***@host:5432/db\n * libsql://db.turso.io?authToken=eyJ… → libsql://db.turso.io?authToken=***\n */\nexport function scrubUrl(url: string): string | undefined {\n try {\n const parsed = new URL(url);\n if (parsed.username) parsed.username = '***';\n if (parsed.password) parsed.password = '***';\n for (const key of [...new Set(parsed.searchParams.keys())]) {\n if (SENSITIVE_URL_PARAM.test(key)) parsed.searchParams.set(key, '***');\n }\n return parsed.toString();\n } catch {\n return undefined;\n }\n}\n\n/**\n * The minimal teardown surface {@link DbDriver.stop} probes on the underlying\n * client. postgres-js and mysql2 expose async `end()`; bun:sqlite and libsql\n * expose synchronous `close()`. Typed as optional so either shape satisfies it.\n */\ninterface DbClient {\n end?(): Promise<void>;\n close?(): void;\n}\n\nexport class DbDriver<TSchema extends Record<string, unknown> = Record<string, never>> implements Driver {\n name = 'db';\n private client: DbClient | undefined;\n /** Serializes transactions on the single bun:sqlite connection. */\n private sqliteTxQueue: Promise<unknown> = Promise.resolve();\n private readonly inSqliteTx = new AsyncLocalStorage<true>();\n public db: IskraDrizzleDb<TSchema> | undefined;\n\n private app: App | undefined;\n private onQuery: OnQueryHook | undefined;\n\n /**\n * Register an observability callback that receives every SQL statement (and\n * its bound params) Drizzle executes. Must be called before {@link start},\n * since Drizzle's logger is wired at connection time. A throwing callback is\n * swallowed so observability never breaks a real query.\n */\n setOnQuery(onQuery: OnQueryHook): void {\n this.onQuery = onQuery;\n }\n\n /**\n * Build the Drizzle `logger` option that forwards to {@link onQuery} when a\n * hook is registered, or `undefined` to leave Drizzle's default logging off.\n */\n private buildLogger(): { logQuery(query: string, params: unknown[]): void } | undefined {\n const hook = this.onQuery;\n if (!hook) return undefined;\n return {\n logQuery: (query: string, params: unknown[]) => {\n try {\n hook(query, params);\n } catch {\n // Observability must never break the underlying query.\n }\n },\n };\n }\n\n async init(app: App) {\n this.app = app;\n app.context.set('db', this);\n }\n\n async start() {\n if (!this.app) return;\n const config = this.app.config.db;\n if (!config) {\n this.app!.logger.warn('No DB configuration found. Skipping DB initialization.');\n return;\n }\n\n this.app!.logger.info(`Initializing DB driver: ${config.driver}`);\n\n const logger = this.buildLogger();\n\n try {\n switch (config.driver) {\n case 'postgres': {\n const client = postgres(config.url);\n this.client = client;\n this.db = drizzle<TSchema>(client, { logger });\n break;\n }\n case 'mysql': {\n const client = mysql.createPool(config.url);\n this.client = client;\n // Name the client type: with only TSchema given, drizzle's\n // TClient defaults to mysql2's callback Pool, not this promise Pool.\n this.db = drizzleMysql<TSchema, typeof client>(client, { logger });\n break;\n }\n case 'sqlite': {\n const { Database } = await import('bun:sqlite');\n const { drizzle: drizzleSqlite } = await import('drizzle-orm/bun-sqlite');\n const client = new Database(config.url);\n this.client = client;\n this.db = drizzleSqlite<TSchema>(client, { logger });\n break;\n }\n case 'libsql': {\n const { createClient } = await import('@libsql/client');\n const { drizzle: drizzleLibsql } = await import('drizzle-orm/libsql');\n const client = createClient({ url: config.url, authToken: config.authToken });\n this.client = client;\n this.db = drizzleLibsql<TSchema>(client, { logger });\n break;\n }\n default:\n throw new DriverError(`Unsupported DB driver: ${config.driver}`, {\n code: 'DRIVER_START_FAILED',\n context: { driver: config.driver },\n });\n }\n // postgres-js and mysql2 pools connect lazily: without a round-trip\n // a wrong host or password only surfaced on the first query.\n await this.roundTrip();\n this.app!.logger.info('DB connected successfully.');\n } catch (error) {\n if (error instanceof DriverError) throw error;\n await this.stop();\n this.app!.logger.error({ error }, 'Failed to connect to DB');\n const safeUrl = scrubUrl(config.url);\n throw new ConnectionError('Failed to connect to DB', {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: {\n driver: config.driver,\n ...(safeUrl !== undefined ? { url: safeUrl } : {}),\n },\n });\n }\n }\n\n /**\n * Applies the pending migrations in `migrationsDir` (generated with\n * `drizzle-kit generate`) over the live connection, using Drizzle's\n * migrator for the configured dialect. Requires `start()`.\n *\n * It used to shell out to `drizzle-kit migrate`, which ignored both\n * arguments and failed without a drizzle.config.ts. `schemaPath` is kept for\n * compatibility; applying migrations does not need the schema.\n */\n async runMigrations(_schemaPath?: string, migrationsDir: string = './drizzle'): Promise<void> {\n const config = this.app?.config.db;\n if (!config) {\n throw new DriverError('Cannot run migrations: no DB configuration found', {\n code: 'DRIVER_START_FAILED',\n });\n }\n if (!this.db) {\n throw new DriverError('Cannot run migrations: DB is not started', {\n code: 'DRIVER_START_FAILED',\n context: { driver: config.driver },\n });\n }\n\n const options = { migrationsFolder: migrationsDir };\n const db = this.db as never;\n try {\n switch (config.driver) {\n case 'postgres':\n await (await import('drizzle-orm/postgres-js/migrator')).migrate(db, options);\n break;\n case 'mysql':\n await (await import('drizzle-orm/mysql2/migrator')).migrate(db, options);\n break;\n case 'sqlite':\n (await import('drizzle-orm/bun-sqlite/migrator')).migrate(db, options);\n break;\n case 'libsql':\n await (await import('drizzle-orm/libsql/migrator')).migrate(db, options);\n break;\n }\n this.app!.logger.info({ migrationsDir }, 'Migrations applied');\n } catch (error) {\n throw new MigrationError('Failed to apply migrations', {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: { driver: config.driver, migrationsDir },\n });\n }\n }\n\n /**\n * Run `fn` inside a database transaction, delegating to Drizzle's\n * `db.transaction`. Callers receive the transaction-scoped db handle instead\n * of reaching into the raw `db`. The dialect union means `tx` is typed as\n * {@link IskraDrizzleTx}; narrow by dialect if you need dialect-specific APIs.\n * Failures are wrapped in {@link QueryError}; the transaction is rolled back.\n *\n * With `sqlite`, transactions run one at a time on the single connection,\n * and a query made outside `tx` while one is open is part of it. A nested\n * `transaction()` call is rejected: it would wait for itself.\n */\n async transaction<R>(fn: (tx: IskraDrizzleTx<TSchema>) => Promise<R>): Promise<R> {\n if (!this.db) {\n throw new QueryError('Cannot run transaction: DB is not started', {\n context: { driver: this.app?.config.db?.driver },\n });\n }\n try {\n if (this.app?.config.db?.driver === 'sqlite') return await this.sqliteTransaction(fn);\n // The dialect-specific `transaction` overloads do not unify across the\n // union, so we route through the runtime method with a faithful cast\n // of the public handle types.\n return await (\n this.db as IskraDrizzleDb<TSchema> & {\n transaction(cb: (tx: IskraDrizzleTx<TSchema>) => Promise<R>): Promise<R>;\n }\n ).transaction((tx) => fn(tx));\n } catch (error) {\n if (error instanceof QueryError) throw error;\n throw new QueryError('Transaction failed', {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: { driver: this.app?.config.db?.driver },\n });\n }\n }\n\n /**\n * Drizzle's bun-sqlite `transaction()` is synchronous: it commits as soon\n * as the callback returns its promise, so an async callback that threw\n * afterwards never rolled back. The transaction is opened and closed here\n * around the awaited callback instead.\n */\n private async sqliteTransaction<R>(fn: (tx: IskraDrizzleTx<TSchema>) => Promise<R>): Promise<R> {\n if (this.inSqliteTx.getStore()) {\n throw new QueryError('Nested transaction() calls are not supported with sqlite', {\n context: { driver: 'sqlite' },\n });\n }\n const db = this.db as BunSQLiteDatabase<TSchema>;\n const run = () =>\n this.inSqliteTx.run(true, async () => {\n // A transaction handle (tx.rollback(), tx.query.*) bound to the\n // connection; the empty native transaction it comes from is done.\n const tx = db.transaction((t) => t);\n db.run(sql`begin`);\n try {\n const result = await fn(tx as unknown as IskraDrizzleTx<TSchema>);\n db.run(sql`commit`);\n return result;\n } catch (error) {\n db.run(sql`rollback`);\n throw error;\n }\n });\n const result = this.sqliteTxQueue.then(run, run);\n this.sqliteTxQueue = result.catch(() => undefined);\n return result;\n }\n\n /**\n * Liveness probe for readiness checks (e.g. web-kit's addReadinessCheck /\n * k8s readiness). Runs a trivial `SELECT 1` against the active dialect and\n * resolves `true` on success or `false` on any failure — it never rejects.\n */\n async ping(): Promise<boolean> {\n if (!this.db) return false;\n try {\n await this.roundTrip();\n return true;\n } catch {\n return false;\n }\n }\n\n /** `SELECT 1` on the active connection; throws on failure. */\n private async roundTrip(): Promise<void> {\n // bun-sqlite exposes the synchronous `.run()`; postgres-js, mysql2 and\n // libsql expose the async `.execute()`. Prefer whichever exists.\n const handle = this.db as {\n run?(query: unknown): unknown;\n execute?(query: unknown): Promise<unknown>;\n };\n if (typeof handle.run === 'function') {\n await handle.run(sql`SELECT 1`);\n } else if (typeof handle.execute === 'function') {\n await handle.execute(sql`SELECT 1`);\n } else {\n throw new Error('DB handle exposes neither run() nor execute()');\n }\n }\n\n async stop() {\n const client = this.client;\n try {\n // postgres-js / mysql2 expose async end(); bun:sqlite / libsql expose\n // synchronous close(). Probe for whichever this client provides.\n if (client?.end) {\n await client.end();\n } else if (client?.close) {\n client.close();\n }\n } catch (error) {\n // A throwing teardown must never abort the orderly shutdown of other\n // drivers; log and continue so the handles below are still cleared.\n this.app?.logger.error({ error }, 'Failed to close DB connection cleanly');\n } finally {\n // Null the handles so a post-stop ping()/transaction() hits the\n // not-started guard instead of an already-closed connection.\n this.client = undefined;\n this.db = undefined;\n }\n }\n}\n\n// `app.context.get('db')` is the DbDriver registered on the app.\ndeclare module '@iskra-bun/core' {\n interface AppContextRegistry {\n db: DbDriver;\n }\n}\n","import { IskraError, ErrorCodes } from '@iskra-bun/core';\n\n// ─── Connection Error ────────────────────────────────────────────────────────\n\nexport class ConnectionError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.CONNECTION_ERROR, ...options });\n this.name = 'ConnectionError';\n }\n}\n\n// ─── Query Error ─────────────────────────────────────────────────────────────\n\nexport class QueryError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.QUERY_ERROR, ...options });\n this.name = 'QueryError';\n }\n}\n\n// ─── Migration Error ─────────────────────────────────────────────────────────\n\nexport class MigrationError extends IskraError {\n constructor(message: string, options?: { cause?: Error; context?: Record<string, unknown> }) {\n super(message, { code: ErrorCodes.MIGRATION_ERROR, ...options });\n this.name = 'MigrationError';\n }\n}\n","/** Query parameters that carry secrets, such as libsql's `authToken` or `password`. */\nexport const SENSITIVE_URL_PARAM = /pass|token|secret|key|auth|credential|signature/i;\n","import { type App } from '@iskra-bun/core';\nimport { existsSync } from 'node:fs';\nimport path from 'node:path';\nimport { MigrationError } from './errors';\nimport { SENSITIVE_URL_PARAM } from './secrets';\n\n/**\n * The drizzle-kit command to run `args` with, from the project's own install:\n * `node_modules/.bin` in `cwd` or a parent (workspaces hoist it). drizzle-kit\n * is a devDependency, and `bunx drizzle-kit` downloaded its latest release\n * from npm where it was missing (a production install) and ran it with\n * DATABASE_URL in its environment. `--no-install` keeps bunx from doing so.\n */\nexport function drizzleKitCommand(args: string[], cwd: string = process.cwd()): string[] {\n for (let dir = path.resolve(cwd); ; dir = path.dirname(dir)) {\n const bin = path.join(dir, 'node_modules', '.bin', 'drizzle-kit');\n if (['', '.exe', '.cmd'].some((ext) => existsSync(bin + ext))) {\n return ['bunx', '--no-install', 'drizzle-kit', ...args];\n }\n if (path.dirname(dir) === dir) break;\n }\n throw new MigrationError(\n 'drizzle-kit is not installed in this project (it is never downloaded at run time): ' +\n 'add it with `bun add -d drizzle-kit`',\n { context: { cwd } },\n );\n}\n\n/**\n * Redacta credenciales `//user:pass@host` y parámetros secretos (`authToken`,\n * `password`...) embebidos en texto arbitrario (p. ej. el stderr de drizzle-kit,\n * que suele imprimir la cadena de conexión completa al fallar). A diferencia de\n * `scrubUrl`, opera sobre texto libre y no requiere que el contenido sea una\n * URL parseable, dejando intacto el resto del diagnóstico. Una contraseña con\n * `@` o `/` sin codificar también se redacta: se toma hasta el último `@`.\n *\n * e.g. \"... postgres://user:pass@host:5432/db\" → \"... postgres://***:***@host:5432/db\"\n */\nexport function scrubCredentials(text: string): string {\n return text\n .replace(/(\\/\\/)(?:[^\\s'\"`@]*@)+/g, '$1***:***@')\n .replace(\n new RegExp(`([?&][^=\\\\s&'\"\\`]*(?:${SENSITIVE_URL_PARAM.source})[^=\\\\s&'\"\\`]*=)[^&\\\\s'\"\\`]*`, 'gi'),\n '$1***',\n );\n}\n\nexport interface MigrationConfig {\n /** Dialecto de la base de datos */\n dialect: 'postgresql' | 'mysql' | 'sqlite';\n /** URL de conexión a la base de datos */\n dbUrl: string;\n /** Ruta al archivo de schema Drizzle (ej: './src/db/schema.ts') */\n schemaPath: string;\n /** Directorio donde se generan las migraciones (ej: './drizzle') */\n migrationsDir: string;\n /** Ruta opcional a un drizzle.config.ts; cuando se define se pasa como --config. */\n configPath?: string;\n}\n\n/**\n * Helper para ejecutar migraciones de Drizzle Kit.\n * Usa el drizzle-kit instalado en el proyecto (`bunx --no-install drizzle-kit`)\n * como subproceso para generar y aplicar migraciones; nunca lo descarga.\n */\nexport class MigrationHelper {\n private config: MigrationConfig;\n private app?: App;\n\n constructor(config: MigrationConfig, app?: App) {\n this.config = config;\n this.app = app;\n }\n\n /**\n * Genera archivos de migración basados en los cambios del schema.\n * drizzle-kit generate soporta --schema y --out, así que ambos se reenvían\n * desde la config (antes se ignoraban silenciosamente).\n */\n async generate(name?: string): Promise<void> {\n const args = ['generate'];\n if (this.config.schemaPath) args.push('--schema', this.config.schemaPath);\n if (this.config.migrationsDir) args.push('--out', this.config.migrationsDir);\n if (name) args.push('--name', name);\n if (this.config.configPath) args.push('--config', this.config.configPath);\n await this.exec(args, 'generate');\n }\n\n /**\n * Aplica las migraciones pendientes a la base de datos.\n * `migrate` sólo acepta --config; schema y out no son flags válidos en este\n * comando, por eso únicamente reenviamos configPath cuando está presente.\n */\n async migrate(): Promise<void> {\n const args = ['migrate'];\n if (this.config.configPath) args.push('--config', this.config.configPath);\n await this.exec(args, 'migrate');\n }\n\n /**\n * Empuja el schema directamente a la base de datos (sin generar archivos de migración).\n * Útil para desarrollo rápido. `push` acepta --schema pero no --out.\n */\n async push(): Promise<void> {\n const args = ['push'];\n if (this.config.schemaPath) args.push('--schema', this.config.schemaPath);\n if (this.config.configPath) args.push('--config', this.config.configPath);\n await this.exec(args, 'push');\n }\n\n /**\n * Elimina un archivo de migración ya generado (`drizzle-kit drop`, interactivo).\n * No borra tablas ni datos de la base.\n * `drop` acepta --out (dónde viven las migraciones) pero no --schema.\n */\n async drop(): Promise<void> {\n const args = ['drop'];\n if (this.config.migrationsDir) args.push('--out', this.config.migrationsDir);\n if (this.config.configPath) args.push('--config', this.config.configPath);\n await this.exec(args, 'drop');\n }\n\n private async exec(args: string[], operation: string): Promise<void> {\n const env: Record<string, string> = {\n ...(process.env as Record<string, string>),\n DATABASE_URL: this.config.dbUrl,\n };\n const cwd = process.cwd();\n // Before anything runs with DATABASE_URL: never a downloaded drizzle-kit.\n const command = drizzleKitCommand(args, cwd);\n\n this.app?.logger.info(`Running migration: ${operation}`);\n\n try {\n const proc = Bun.spawn(command, {\n cwd,\n env,\n stdout: 'pipe',\n stderr: 'pipe',\n });\n\n const exitCode = await proc.exited;\n const stdout = await new Response(proc.stdout).text();\n const stderr = await new Response(proc.stderr).text();\n\n if (stdout) this.app?.logger.info(stdout.trim());\n\n if (exitCode !== 0) {\n throw new MigrationError(`Migration ${operation} failed with exit code ${exitCode}`, {\n context: { operation, exitCode, stderr: scrubCredentials(stderr.trim()) },\n });\n }\n\n this.app?.logger.info(`Migration ${operation} completed successfully`);\n } catch (error) {\n if (error instanceof MigrationError) throw error;\n throw new MigrationError(`Migration ${operation} failed`, {\n cause: error instanceof Error ? error : new Error(String(error)),\n context: { operation },\n });\n }\n }\n}\n\n/**\n * Mapea el driver de Iskra al dialecto de Drizzle Kit.\n */\nexport function mapDialect(driver: string): MigrationConfig['dialect'] {\n switch (driver) {\n case 'postgres':\n return 'postgresql';\n case 'mysql':\n return 'mysql';\n case 'sqlite':\n case 'libsql':\n return 'sqlite';\n default:\n throw new MigrationError(`Cannot map driver \"${driver}\" to a Drizzle dialect`, {\n context: { driver },\n });\n }\n}\n","export interface DrizzleConfigOptions {\n /** Dialecto: 'postgresql', 'mysql', 'sqlite' */\n dialect: 'postgresql' | 'mysql' | 'sqlite';\n /** URL de conexión a la base de datos */\n dbUrl: string;\n /** Ruta al archivo de schema (ej: './src/db/schema.ts') */\n schemaPath: string;\n /** Directorio de migraciones (ej: './drizzle') */\n migrationsDir?: string;\n}\n\n/**\n * La configuración que lee drizzle-kit (el `export default` de `drizzle.config.ts`).\n * Se declara aquí para no importar drizzle-kit: es una herramienta de desarrollo,\n * y el entry point de db-kit (que usa Bun vía la condición `bun`) fallaba al\n * importarse donde no estaba instalada, por ejemplo con `--production`.\n */\nexport interface DrizzleKitConfig {\n dialect: DrizzleConfigOptions['dialect'];\n schema: string;\n out: string;\n dbCredentials: { url: string };\n}\n\n/**\n * Crea una configuración de drizzle-kit reutilizable.\n *\n * Uso en tu proyecto:\n * ```ts\n * // drizzle.config.ts\n * import { createDrizzleConfig } from '@iskra-bun/db-kit';\n *\n * export default createDrizzleConfig({\n * dialect: 'sqlite',\n * dbUrl: process.env.DATABASE_URL || 'app.db',\n * schemaPath: './src/db/schema.ts',\n * });\n * ```\n */\nexport function createDrizzleConfig(options: DrizzleConfigOptions): DrizzleKitConfig {\n return {\n dialect: options.dialect,\n schema: options.schemaPath,\n out: options.migrationsDir || './drizzle',\n dbCredentials: {\n url: options.dbUrl,\n },\n };\n}\n"],"mappings":";AAAA,SAAgC,mBAAmB;AACnD,SAAS,eAAwC;AACjD,SAAS,WAAW,oBAAyC;AAG7D,OAAO,cAAc;AACrB,OAAO,WAAW;AAClB,SAAS,WAAW;AACpB,SAAS,yBAAyB;;;ACRlC,SAAS,YAAY,kBAAkB;AAIhC,IAAM,kBAAN,cAA8B,WAAW;AAAA,EAC5C,YAAY,SAAiB,SAAgE;AACzF,UAAM,SAAS,EAAE,MAAM,WAAW,kBAAkB,GAAG,QAAQ,CAAC;AAChE,SAAK,OAAO;AAAA,EAChB;AACJ;AAIO,IAAM,aAAN,cAAyB,WAAW;AAAA,EACvC,YAAY,SAAiB,SAAgE;AACzF,UAAM,SAAS,EAAE,MAAM,WAAW,aAAa,GAAG,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0.3.0",
|
|
4
4
|
"description": "Base de datos SQL de Iskra con Drizzle ORM (PostgreSQL, MySQL, SQLite, LibSQL).",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"iskra",
|
|
@@ -22,6 +22,9 @@
|
|
|
22
22
|
},
|
|
23
23
|
"homepage": "https://github.com/fearful/iskra/tree/main/packages/db-kit#readme",
|
|
24
24
|
"bugs": "https://github.com/fearful/iskra/issues",
|
|
25
|
+
"engines": {
|
|
26
|
+
"bun": ">=1.3.0"
|
|
27
|
+
},
|
|
25
28
|
"type": "module",
|
|
26
29
|
"main": "./dist/index.js",
|
|
27
30
|
"module": "./dist/index.js",
|
|
@@ -49,14 +52,14 @@
|
|
|
49
52
|
"build": "tsup --config ../../tsup.config.ts"
|
|
50
53
|
},
|
|
51
54
|
"dependencies": {
|
|
52
|
-
"@iskra-bun/core": "0.
|
|
53
|
-
"drizzle-orm": "^0.
|
|
55
|
+
"@iskra-bun/core": "^0.2.0",
|
|
56
|
+
"drizzle-orm": "^0.45.2",
|
|
54
57
|
"postgres": "^3.4.4",
|
|
55
|
-
"mysql2": "^3.
|
|
56
|
-
"@libsql/client": "^0.
|
|
58
|
+
"mysql2": "^3.24.4",
|
|
59
|
+
"@libsql/client": "^0.18.0"
|
|
57
60
|
},
|
|
58
61
|
"devDependencies": {
|
|
59
62
|
"@types/node": "^22.10.2",
|
|
60
|
-
"drizzle-kit": "^0.
|
|
63
|
+
"drizzle-kit": "^0.31.11"
|
|
61
64
|
}
|
|
62
65
|
}
|