driftsql 2.0.0-beta.1 → 2.0.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1 +1,1568 @@
1
- import{t as e}from"./postgres-9C7eE0wB.js";import{A as t,C as n,D as r,E as i,F as a,I as o,L as s,M as c,N as l,O as u,P as d,R as f,S as p,T as m,_ as h,a as g,b as _,c as v,d as y,f as b,g as x,h as S,i as C,j as w,k as T,l as E,m as D,n as O,o as k,p as A,r as j,s as M,t as N,u as P,v as F,w as I,x as L,y as R,z}from"./src-B1L6LdRV.js";import{n as B,t as V}from"./type-generator-CpqI7vBb.js";export{D as Column,N as DriftSQLClient,v as MigrationBuilder,M as MigrationRunner,y as MySQLGenerator,f as PostgresDriver,e as PostgresGenerator,O as SQLClient,P as SQLiteGenerator,C as SchemaDiffer,j as SnapshotManager,b as Table,V as TypeGenerator,S as bigint,x as bigserial,h as boolean,F as bytea,R as char,E as createMigration,s as createMySQLHelpers,z as createPostgresHelpers,o as createSQLiteHelpers,A as createTable,_ as date,L as decimal,g as detectChanges,p as doublePrecision,k as generateMigrationFromChanges,B as generateTypesFromSchema,n as integer,I as json,m as jsonb,i as numeric,r as real,u as serial,T as smallint,t as text,w as time,c as timestamp,l as timestamptz,d as uuid,a as varchar};
1
+ import { a as __export, i as __esm, n as init_postgres, o as __toCommonJS, r as postgres_exports, t as PostgresGenerator } from "./postgres-CjYq8GvL.js";
2
+ import { n as generateTypesFromSchema, t as TypeGenerator } from "./type-generator-Ba8bgnMm.js";
3
+ import { Command } from "commander";
4
+ import consola from "consola";
5
+ import fs from "node:fs/promises";
6
+ import path from "node:path";
7
+ import chalk from "chalk";
8
+ import postgres from "postgres";
9
+
10
+ //#region src/schema/runner.ts
11
+ var MigrationRunner = class {
12
+ client;
13
+ constructor(client) {
14
+ this.client = client;
15
+ }
16
+ async ensureMigrationsTable() {
17
+ await this.client.query(`
18
+ CREATE TABLE IF NOT EXISTS _migrations (
19
+ version VARCHAR(255) PRIMARY KEY,
20
+ name VARCHAR(255) NOT NULL,
21
+ applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
22
+ );
23
+ `);
24
+ }
25
+ async getAppliedMigrations() {
26
+ await this.ensureMigrationsTable();
27
+ return (await this.client.query("SELECT version FROM _migrations ORDER BY version")).rows.map((row) => row.version);
28
+ }
29
+ async up(migration) {
30
+ if ((await this.getAppliedMigrations()).includes(migration.version)) {
31
+ consola.info(`Migration ${migration.version} (${migration.name}) already applied`);
32
+ return;
33
+ }
34
+ consola.start(`Running migration ${migration.version} (${migration.name})`);
35
+ for (const statement of migration.up) await this.client.query(statement);
36
+ await this.client.query("INSERT INTO _migrations (version, name) VALUES ($1, $2)", [migration.version, migration.name]);
37
+ consola.success(`Migration ${migration.version} (${migration.name}) applied successfully`);
38
+ }
39
+ async down(migration) {
40
+ if (!(await this.getAppliedMigrations()).includes(migration.version)) {
41
+ consola.info(`Migration ${migration.version} (${migration.name}) not applied`);
42
+ return;
43
+ }
44
+ consola.start(`Rolling back migration ${migration.version} (${migration.name})`);
45
+ for (const statement of migration.down) await this.client.query(statement);
46
+ await this.client.query("DELETE FROM _migrations WHERE version = $1", [migration.version]);
47
+ consola.success(`Migration ${migration.version} (${migration.name}) rolled back successfully`);
48
+ }
49
+ async upAll(migrations) {
50
+ const applied = await this.getAppliedMigrations();
51
+ const pending = migrations.filter((m) => !applied.includes(m.version));
52
+ if (pending.length === 0) {
53
+ consola.info("No pending migrations");
54
+ return;
55
+ }
56
+ consola.info(`Found ${pending.length} pending migrations`);
57
+ for (const migration of pending) await this.up(migration);
58
+ }
59
+ async downAll(migrations) {
60
+ const applied = await this.getAppliedMigrations();
61
+ const toRollback = migrations.filter((m) => applied.includes(m.version)).reverse();
62
+ if (toRollback.length === 0) {
63
+ consola.info("No migrations to rollback");
64
+ return;
65
+ }
66
+ consola.info(`Rolling back ${toRollback.length} migrations`);
67
+ for (const migration of toRollback) await this.down(migration);
68
+ }
69
+ async reset(migrations) {
70
+ await this.downAll(migrations);
71
+ await this.upAll(migrations);
72
+ }
73
+ };
74
+
75
+ //#endregion
76
+ //#region src/schema/snapshot.ts
77
+ var SnapshotManager = class {
78
+ snapshotPath;
79
+ constructor(snapshotPath = "./.driftsql/snapshot.json") {
80
+ this.snapshotPath = snapshotPath;
81
+ }
82
+ async save(tables) {
83
+ const snapshot = {
84
+ version: "1",
85
+ timestamp: Date.now(),
86
+ tables
87
+ };
88
+ const dir = path.dirname(this.snapshotPath);
89
+ await fs.mkdir(dir, { recursive: true });
90
+ await fs.writeFile(this.snapshotPath, JSON.stringify(snapshot, null, 2), "utf8");
91
+ }
92
+ async load() {
93
+ try {
94
+ const content = await fs.readFile(this.snapshotPath, "utf8");
95
+ return JSON.parse(content);
96
+ } catch {
97
+ return null;
98
+ }
99
+ }
100
+ async exists() {
101
+ try {
102
+ await fs.access(this.snapshotPath);
103
+ return true;
104
+ } catch {
105
+ return false;
106
+ }
107
+ }
108
+ };
109
+
110
+ //#endregion
111
+ //#region src/schema/generators/mysql.ts
112
+ var mysql_exports = /* @__PURE__ */ __export({ MySQLGenerator: () => MySQLGenerator });
113
+ var MySQLGenerator;
114
+ var init_mysql = __esm({ "src/schema/generators/mysql.ts": (() => {
115
+ MySQLGenerator = class {
116
+ generateCreateTable(table) {
117
+ const lines = [];
118
+ lines.push(`CREATE TABLE \`${table.name}\` (`);
119
+ const columnDefs = [];
120
+ for (const column of table.columns) columnDefs.push(" " + this.generateColumnDefinition(column));
121
+ if (table.primaryKey && table.primaryKey.length > 0) {
122
+ const pkColumns = table.primaryKey.map((col) => `\`${col}\``).join(", ");
123
+ columnDefs.push(` PRIMARY KEY (${pkColumns})`);
124
+ }
125
+ if (table.checks && table.checks.length > 0) for (const check of table.checks) columnDefs.push(` CONSTRAINT \`${check.name}\` CHECK (${check.expression})`);
126
+ lines.push(columnDefs.join(",\n"));
127
+ lines.push(") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;");
128
+ const sql = [lines.join("\n")];
129
+ if (table.indexes && table.indexes.length > 0) for (const index of table.indexes) sql.push(this.generateIndex(table.name, index));
130
+ return sql.join("\n\n");
131
+ }
132
+ generateDropTable(tableName) {
133
+ return `DROP TABLE IF EXISTS \`${tableName}\`;`;
134
+ }
135
+ generateColumnDefinition(column) {
136
+ const parts = [`\`${column.name}\``];
137
+ parts.push(this.getColumnType(column));
138
+ if (column.notNull && !column.primaryKey && column.type !== "serial" && column.type !== "bigserial") parts.push("NOT NULL");
139
+ if (column.unique) parts.push("UNIQUE");
140
+ if (column.type === "serial" || column.type === "bigserial") parts.push("AUTO_INCREMENT");
141
+ if (column.default !== void 0) parts.push(`DEFAULT ${this.formatDefault(column.default)}`);
142
+ if (column.references) {
143
+ const ref = column.references;
144
+ parts.push(`REFERENCES \`${ref.table}\`(\`${ref.column}\`)`);
145
+ if (ref.onDelete) parts.push(`ON DELETE ${ref.onDelete}`);
146
+ if (ref.onUpdate) parts.push(`ON UPDATE ${ref.onUpdate}`);
147
+ }
148
+ if (column.check) parts.push(`CHECK (${column.check})`);
149
+ return parts.join(" ");
150
+ }
151
+ getColumnType(column) {
152
+ let typeStr = column.type;
153
+ if (typeStr === "serial") return "INT";
154
+ if (typeStr === "bigserial") return "BIGINT";
155
+ if (typeStr === "timestamptz") return "TIMESTAMP";
156
+ if (typeStr === "jsonb") return "JSON";
157
+ if (typeStr === "bytea") return "BLOB";
158
+ if (typeStr === "double precision") return "DOUBLE";
159
+ let result = typeStr.toUpperCase();
160
+ if (column.length) result += `(${column.length})`;
161
+ else if (column.precision) if (column.scale !== void 0) result += `(${column.precision}, ${column.scale})`;
162
+ else result += `(${column.precision})`;
163
+ return result;
164
+ }
165
+ formatDefault(value) {
166
+ if (typeof value === "string") {
167
+ if (value.toUpperCase() === "NOW()" || value.toUpperCase() === "CURRENT_TIMESTAMP") return "CURRENT_TIMESTAMP";
168
+ return `'${value}'`;
169
+ }
170
+ if (typeof value === "boolean") return value ? "1" : "0";
171
+ return String(value);
172
+ }
173
+ generateIndex(tableName, index) {
174
+ const unique = index.unique ? "UNIQUE " : "";
175
+ const type = index.type && index.type !== "btree" ? ` USING ${index.type.toUpperCase()}` : "";
176
+ const columns = index.columns.map((col) => `\`${col}\``).join(", ");
177
+ return `CREATE ${unique}INDEX \`${index.name}\` ON \`${tableName}\`${type} (${columns});`;
178
+ }
179
+ };
180
+ }) });
181
+
182
+ //#endregion
183
+ //#region src/schema/generators/sqlite.ts
184
+ var sqlite_exports = /* @__PURE__ */ __export({ SQLiteGenerator: () => SQLiteGenerator });
185
+ var SQLiteGenerator;
186
+ var init_sqlite = __esm({ "src/schema/generators/sqlite.ts": (() => {
187
+ SQLiteGenerator = class {
188
+ generateCreateTable(table) {
189
+ const lines = [];
190
+ lines.push(`CREATE TABLE "${table.name}" (`);
191
+ const columnDefs = [];
192
+ for (const column of table.columns) columnDefs.push(" " + this.generateColumnDefinition(column));
193
+ if (table.primaryKey && table.primaryKey.length > 0) {
194
+ const pkColumns = table.primaryKey.map((col) => `"${col}"`).join(", ");
195
+ columnDefs.push(` PRIMARY KEY (${pkColumns})`);
196
+ }
197
+ if (table.checks && table.checks.length > 0) for (const check of table.checks) columnDefs.push(` CONSTRAINT "${check.name}" CHECK (${check.expression})`);
198
+ lines.push(columnDefs.join(",\n"));
199
+ lines.push(");");
200
+ const sql = [lines.join("\n")];
201
+ if (table.indexes && table.indexes.length > 0) for (const index of table.indexes) sql.push(this.generateIndex(table.name, index));
202
+ return sql.join("\n\n");
203
+ }
204
+ generateDropTable(tableName) {
205
+ return `DROP TABLE IF EXISTS "${tableName}";`;
206
+ }
207
+ generateColumnDefinition(column) {
208
+ const parts = [`"${column.name}"`];
209
+ parts.push(this.getColumnType(column));
210
+ if (column.primaryKey) {
211
+ parts.push("PRIMARY KEY");
212
+ if (column.type === "serial" || column.type === "bigserial") parts.push("AUTOINCREMENT");
213
+ }
214
+ if (column.notNull && !column.primaryKey) parts.push("NOT NULL");
215
+ if (column.unique) parts.push("UNIQUE");
216
+ if (column.default !== void 0) parts.push(`DEFAULT ${this.formatDefault(column.default)}`);
217
+ if (column.references) {
218
+ const ref = column.references;
219
+ parts.push(`REFERENCES "${ref.table}"("${ref.column}")`);
220
+ if (ref.onDelete) parts.push(`ON DELETE ${ref.onDelete}`);
221
+ if (ref.onUpdate) parts.push(`ON UPDATE ${ref.onUpdate}`);
222
+ }
223
+ if (column.check) parts.push(`CHECK (${column.check})`);
224
+ return parts.join(" ");
225
+ }
226
+ getColumnType(column) {
227
+ let type = column.type;
228
+ if (type === "serial" || type === "bigserial") return "INTEGER";
229
+ if (type === "bigint") return "INTEGER";
230
+ if (type === "smallint") return "INTEGER";
231
+ if (type === "varchar" || type === "char") return "TEXT";
232
+ if (type === "timestamptz") return "TEXT";
233
+ if (type === "timestamp") return "TEXT";
234
+ if (type === "date") return "TEXT";
235
+ if (type === "time") return "TEXT";
236
+ if (type === "uuid") return "TEXT";
237
+ if (type === "json" || type === "jsonb") return "TEXT";
238
+ if (type === "bytea") return "BLOB";
239
+ if (type === "decimal" || type === "numeric") return "REAL";
240
+ if (type === "real" || type === "double precision") return "REAL";
241
+ if (type === "boolean") return "INTEGER";
242
+ return type.toUpperCase();
243
+ }
244
+ formatDefault(value) {
245
+ if (typeof value === "string") {
246
+ if (value.toUpperCase() === "NOW()" || value.toUpperCase() === "CURRENT_TIMESTAMP") return "CURRENT_TIMESTAMP";
247
+ return `'${value}'`;
248
+ }
249
+ if (typeof value === "boolean") return value ? "1" : "0";
250
+ return String(value);
251
+ }
252
+ generateIndex(tableName, index) {
253
+ const unique = index.unique ? "UNIQUE " : "";
254
+ const columns = index.columns.map((col) => `"${col}"`).join(", ");
255
+ return `CREATE ${unique}INDEX "${index.name}" ON "${tableName}" (${columns});`;
256
+ }
257
+ };
258
+ }) });
259
+
260
+ //#endregion
261
+ //#region src/schema/migration.ts
262
+ init_postgres();
263
+ init_mysql();
264
+ init_sqlite();
265
+ var MigrationBuilder = class {
266
+ upStatements = [];
267
+ downStatements = [];
268
+ dialect;
269
+ constructor(dialect = "postgres") {
270
+ this.dialect = dialect;
271
+ }
272
+ createTable(table) {
273
+ const tableDef = table.getDefinition();
274
+ const generator = this.getGenerator();
275
+ this.upStatements.push(generator.generateCreateTable(tableDef));
276
+ this.downStatements.unshift(generator.generateDropTable(tableDef.name));
277
+ return this;
278
+ }
279
+ dropTable(tableName) {
280
+ const generator = this.getGenerator();
281
+ this.upStatements.push(generator.generateDropTable(tableName));
282
+ return this;
283
+ }
284
+ addColumn(tableName, columnSql) {
285
+ this.upStatements.push(`ALTER TABLE "${tableName}" ADD COLUMN ${columnSql};`);
286
+ return this;
287
+ }
288
+ dropColumn(tableName, columnName) {
289
+ this.upStatements.push(`ALTER TABLE "${tableName}" DROP COLUMN "${columnName}";`);
290
+ return this;
291
+ }
292
+ renameTable(oldName, newName) {
293
+ if (this.dialect === "postgres") this.upStatements.push(`ALTER TABLE "${oldName}" RENAME TO "${newName}";`);
294
+ else if (this.dialect === "mysql") this.upStatements.push(`RENAME TABLE \`${oldName}\` TO \`${newName}\`;`);
295
+ else this.upStatements.push(`ALTER TABLE "${oldName}" RENAME TO "${newName}";`);
296
+ return this;
297
+ }
298
+ raw(sql, downSql) {
299
+ this.upStatements.push(sql);
300
+ if (downSql) this.downStatements.unshift(downSql);
301
+ return this;
302
+ }
303
+ build(version, name) {
304
+ return {
305
+ version,
306
+ name,
307
+ up: this.upStatements,
308
+ down: this.downStatements
309
+ };
310
+ }
311
+ getGenerator() {
312
+ switch (this.dialect) {
313
+ case "postgres": return new PostgresGenerator();
314
+ case "mysql": return new MySQLGenerator();
315
+ case "sqlite": return new SQLiteGenerator();
316
+ default: return new PostgresGenerator();
317
+ }
318
+ }
319
+ };
320
+ function createMigration(dialect = "postgres") {
321
+ return new MigrationBuilder(dialect);
322
+ }
323
+
324
+ //#endregion
325
+ //#region src/schema/differ.ts
326
+ var SchemaDiffer = class {
327
+ dialect;
328
+ constructor(dialect = "postgres") {
329
+ this.dialect = dialect;
330
+ }
331
+ detectChanges(oldSchema, newSchema) {
332
+ const changes = [];
333
+ const oldTables = new Map(oldSchema.map((t) => [t.name, t]));
334
+ const newTables = new Map(newSchema.map((t) => [t.name, t]));
335
+ for (const [tableName, newTable] of newTables) {
336
+ const oldTable = oldTables.get(tableName);
337
+ if (!oldTable) changes.push({
338
+ type: "create_table",
339
+ table: tableName,
340
+ details: newTable
341
+ });
342
+ else {
343
+ changes.push(...this.detectColumnChanges(oldTable, newTable));
344
+ changes.push(...this.detectIndexChanges(oldTable, newTable));
345
+ }
346
+ }
347
+ for (const [tableName] of oldTables) if (!newTables.has(tableName)) changes.push({
348
+ type: "drop_table",
349
+ table: tableName
350
+ });
351
+ return changes;
352
+ }
353
+ detectColumnChanges(oldTable, newTable) {
354
+ const changes = [];
355
+ const oldColumns = new Map(oldTable.columns.map((c) => [c.name, c]));
356
+ const newColumns = new Map(newTable.columns.map((c) => [c.name, c]));
357
+ for (const [colName, newCol] of newColumns) {
358
+ const oldCol = oldColumns.get(colName);
359
+ if (!oldCol) changes.push({
360
+ type: "add_column",
361
+ table: newTable.name,
362
+ details: newCol
363
+ });
364
+ else if (this.hasColumnChanged(oldCol, newCol)) changes.push({
365
+ type: "modify_column",
366
+ table: newTable.name,
367
+ details: {
368
+ old: oldCol,
369
+ new: newCol
370
+ }
371
+ });
372
+ }
373
+ for (const [colName] of oldColumns) if (!newColumns.has(colName)) changes.push({
374
+ type: "drop_column",
375
+ table: newTable.name,
376
+ details: { name: colName }
377
+ });
378
+ return changes;
379
+ }
380
+ detectIndexChanges(oldTable, newTable) {
381
+ const changes = [];
382
+ const oldIndexes = new Map((oldTable.indexes || []).map((i) => [i.name, i]));
383
+ const newIndexes = new Map((newTable.indexes || []).map((i) => [i.name, i]));
384
+ for (const [idxName, newIdx] of newIndexes) if (!oldIndexes.has(idxName)) changes.push({
385
+ type: "create_index",
386
+ table: newTable.name,
387
+ details: newIdx
388
+ });
389
+ for (const [idxName] of oldIndexes) if (!newIndexes.has(idxName)) changes.push({
390
+ type: "drop_index",
391
+ table: newTable.name,
392
+ details: { name: idxName }
393
+ });
394
+ return changes;
395
+ }
396
+ hasColumnChanged(oldCol, newCol) {
397
+ return oldCol.type !== newCol.type || oldCol.length !== newCol.length || oldCol.precision !== newCol.precision || oldCol.scale !== newCol.scale || oldCol.notNull !== newCol.notNull || oldCol.unique !== newCol.unique || oldCol.default !== newCol.default || JSON.stringify(oldCol.references) !== JSON.stringify(newCol.references);
398
+ }
399
+ generateMigration(changes, version, name) {
400
+ const migration = new MigrationBuilder(this.dialect);
401
+ for (const change of changes) switch (change.type) {
402
+ case "create_table": {
403
+ const tableDef = change.details;
404
+ const generator = this.getGenerator();
405
+ const sql = generator.generateCreateTable(tableDef);
406
+ migration.raw(sql, generator.generateDropTable(tableDef.name));
407
+ break;
408
+ }
409
+ case "drop_table": {
410
+ const generator = this.getGenerator();
411
+ migration.raw(generator.generateDropTable(change.table));
412
+ break;
413
+ }
414
+ case "add_column": {
415
+ const col = change.details;
416
+ const colSql = this.generateColumnSQL(col);
417
+ migration.raw(`ALTER TABLE "${change.table}" ADD COLUMN ${colSql};`, `ALTER TABLE "${change.table}" DROP COLUMN "${col.name}";`);
418
+ break;
419
+ }
420
+ case "drop_column": {
421
+ const colName = change.details.name;
422
+ migration.raw(`ALTER TABLE "${change.table}" DROP COLUMN "${colName}";`);
423
+ break;
424
+ }
425
+ case "create_index": {
426
+ const idx = change.details;
427
+ const sql = this.generateIndexSQL(change.table, idx);
428
+ migration.raw(sql, `DROP INDEX "${idx.name}";`);
429
+ break;
430
+ }
431
+ case "drop_index": {
432
+ const idxName = change.details.name;
433
+ migration.raw(`DROP INDEX "${idxName}";`);
434
+ break;
435
+ }
436
+ }
437
+ return migration;
438
+ }
439
+ generateColumnSQL(col) {
440
+ const parts = [`"${col.name}"`];
441
+ parts.push(col.type.toUpperCase());
442
+ if (col.length) parts[1] += `(${col.length})`;
443
+ if (col.notNull) parts.push("NOT NULL");
444
+ if (col.unique) parts.push("UNIQUE");
445
+ if (col.default !== void 0) parts.push(`DEFAULT ${this.formatDefault(col.default)}`);
446
+ return parts.join(" ");
447
+ }
448
+ generateIndexSQL(tableName, idx) {
449
+ const unique = idx.unique ? "UNIQUE " : "";
450
+ const columns = idx.columns.map((col) => `"${col}"`).join(", ");
451
+ return `CREATE ${unique}INDEX "${idx.name}" ON "${tableName}" (${columns});`;
452
+ }
453
+ formatDefault(value) {
454
+ if (typeof value === "string") {
455
+ if (value.toUpperCase() === "NOW()" || value.toUpperCase() === "CURRENT_TIMESTAMP") return value.toUpperCase();
456
+ return `'${value}'`;
457
+ }
458
+ if (typeof value === "boolean") return value ? "TRUE" : "FALSE";
459
+ return String(value);
460
+ }
461
+ getGenerator() {
462
+ const { PostgresGenerator: PostgresGenerator$1 } = (init_postgres(), __toCommonJS(postgres_exports));
463
+ const { MySQLGenerator: MySQLGenerator$1 } = (init_mysql(), __toCommonJS(mysql_exports));
464
+ const { SQLiteGenerator: SQLiteGenerator$1 } = (init_sqlite(), __toCommonJS(sqlite_exports));
465
+ switch (this.dialect) {
466
+ case "postgres": return new PostgresGenerator$1();
467
+ case "mysql": return new MySQLGenerator$1();
468
+ case "sqlite": return new SQLiteGenerator$1();
469
+ default: return new PostgresGenerator$1();
470
+ }
471
+ }
472
+ };
473
+ function detectChanges(oldSchema, newSchema, dialect = "postgres") {
474
+ return new SchemaDiffer(dialect).detectChanges(oldSchema, newSchema);
475
+ }
476
+ function generateMigrationFromChanges(changes, version, name, dialect = "postgres") {
477
+ return new SchemaDiffer(dialect).generateMigration(changes, version, name);
478
+ }
479
+
480
+ //#endregion
481
+ //#region src/cli/index.ts
482
+ const program = new Command();
483
+ program.name("driftsql").description("DriftSQL CLI - Database migrations and schema management").version("0.0.1");
484
+ program.command("migrate:up").description("Run all pending migrations").option("-d, --dir <directory>", "Migrations directory", "./migrations").option("-c, --config <path>", "Config file path", "./driftsql.config.ts").action(async (options) => {
485
+ try {
486
+ const { client, migrations } = await loadMigrations(options.dir, options.config);
487
+ await new MigrationRunner(client).upAll(migrations);
488
+ await client.close();
489
+ consola.success("All migrations applied successfully");
490
+ } catch (error) {
491
+ consola.error("Migration failed:", error);
492
+ process.exit(1);
493
+ }
494
+ });
495
+ program.command("migrate:down").description("Rollback the last migration").option("-d, --dir <directory>", "Migrations directory", "./migrations").option("-c, --config <path>", "Config file path", "./driftsql.config.ts").action(async (options) => {
496
+ try {
497
+ const { client, migrations } = await loadMigrations(options.dir, options.config);
498
+ const runner = new MigrationRunner(client);
499
+ const applied = await runner.getAppliedMigrations();
500
+ const lastMigration = migrations.find((m) => m.version === applied[applied.length - 1]);
501
+ if (lastMigration) await runner.down(lastMigration);
502
+ else consola.info("No migrations to rollback");
503
+ await client.close();
504
+ } catch (error) {
505
+ consola.error("Rollback failed:", error);
506
+ process.exit(1);
507
+ }
508
+ });
509
+ program.command("migrate:reset").description("Reset all migrations (down then up)").option("-d, --dir <directory>", "Migrations directory", "./migrations").option("-c, --config <path>", "Config file path", "./driftsql.config.ts").action(async (options) => {
510
+ try {
511
+ const { client, migrations } = await loadMigrations(options.dir, options.config);
512
+ await new MigrationRunner(client).reset(migrations);
513
+ await client.close();
514
+ consola.success("All migrations reset successfully");
515
+ } catch (error) {
516
+ consola.error("Reset failed:", error);
517
+ process.exit(1);
518
+ }
519
+ });
520
+ program.command("migrate:status").description("Show migration status").option("-d, --dir <directory>", "Migrations directory", "./migrations").option("-c, --config <path>", "Config file path", "./driftsql.config.ts").action(async (options) => {
521
+ try {
522
+ const { client, migrations } = await loadMigrations(options.dir, options.config);
523
+ const applied = await new MigrationRunner(client).getAppliedMigrations();
524
+ consola.info("Migration Status:\n");
525
+ for (const migration of migrations) {
526
+ const status = applied.includes(migration.version) ? "✓ Applied" : "✗ Pending";
527
+ consola.log(`${status} - ${migration.version} (${migration.name})`);
528
+ }
529
+ await client.close();
530
+ } catch (error) {
531
+ consola.error("Failed to get status:", error);
532
+ process.exit(1);
533
+ }
534
+ });
535
+ program.command("migrate:create <name>").description("Create a new migration file").option("-d, --dir <directory>", "Migrations directory", "./migrations").action(async (name, options) => {
536
+ try {
537
+ const version = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:T.]/g, "").slice(0, 14);
538
+ const fileName = `${version}_${name}.ts`;
539
+ const filePath = path.join(options.dir, fileName);
540
+ await fs.mkdir(options.dir, { recursive: true });
541
+ const template = `import { createMigration } from 'driftsql'
542
+
543
+ export const migration = createMigration('postgres')
544
+ .raw('-- Add your migration SQL here')
545
+ .build('${version}', '${name}')
546
+ `;
547
+ await fs.writeFile(filePath, template, "utf8");
548
+ consola.success(`Created migration: ${fileName}`);
549
+ } catch (error) {
550
+ consola.error("Failed to create migration:", error);
551
+ process.exit(1);
552
+ }
553
+ });
554
+ program.command("migrate:generate [name]").description("Automatically generate migration from schema changes").option("-s, --schema <path>", "Schema file path", "./schema.ts").option("-d, --dir <directory>", "Migrations directory", "./migrations").action(async (name, options) => {
555
+ try {
556
+ const snapshotManager = new SnapshotManager();
557
+ const schemaPath = path.resolve(process.cwd(), options.schema);
558
+ consola.start("Loading schema...");
559
+ const schemaModule = await import(schemaPath);
560
+ const schema = schemaModule.default || schemaModule.schema;
561
+ if (!schema || !Array.isArray(schema)) {
562
+ consola.error("Schema file must export default or named \"schema\" as an array of table definitions");
563
+ consola.info("Example: export default [usersTable.getDefinition(), postsTable.getDefinition()]");
564
+ process.exit(1);
565
+ }
566
+ const currentSchema = schema;
567
+ const snapshot = await snapshotManager.load();
568
+ if (!snapshot) {
569
+ consola.info("No previous schema snapshot found, creating initial migration...");
570
+ const version$1 = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:T.]/g, "").slice(0, 14);
571
+ const migrationName$1 = name || "initial";
572
+ const fileName$1 = `${version$1}_${migrationName$1}.ts`;
573
+ const filePath$1 = path.join(options.dir, fileName$1);
574
+ await fs.mkdir(options.dir, { recursive: true });
575
+ const { PostgresGenerator: PostgresGenerator$1 } = await import("./postgres-CFQEP3ft.js");
576
+ const generator = new PostgresGenerator$1();
577
+ const upSQL$1 = currentSchema.map((table) => generator.generateCreateTable(table)).map((sql) => ` .raw(\`${sql.replace(/`/g, "\\`")}\`)`).join("\n");
578
+ const migrationContent$1 = `import { createMigration } from 'driftsql'
579
+
580
+ // Initial migration - automatically generated
581
+ // Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}
582
+
583
+ export const migration = createMigration('postgres')
584
+ ${upSQL$1}
585
+ .build('${version$1}', '${migrationName$1}')
586
+ `;
587
+ await fs.writeFile(filePath$1, migrationContent$1, "utf8");
588
+ await snapshotManager.save(currentSchema);
589
+ const { generateTypesFromSchema: generateTypesFromSchema$2 } = await import("./type-generator-DbcqBk-4.js");
590
+ await generateTypesFromSchema$2(currentSchema, path.join(process.cwd(), "db-types.ts"));
591
+ consola.success(`\nCreated initial migration: ${fileName$1}`);
592
+ consola.success(`Generated TypeScript types: db-types.ts`);
593
+ consola.info("Snapshot saved. Run \"driftsql migrate:up\" to apply changes.");
594
+ return;
595
+ }
596
+ consola.info("Detecting changes...");
597
+ const changes = detectChanges(snapshot.tables, currentSchema, "postgres");
598
+ if (changes.length === 0) {
599
+ consola.success("No schema changes detected!");
600
+ return;
601
+ }
602
+ consola.info(`Found ${changes.length} change(s):`);
603
+ changes.forEach((change, idx) => {
604
+ consola.log(` ${idx + 1}. ${change.type} - ${change.table}`);
605
+ });
606
+ const version = (/* @__PURE__ */ new Date()).toISOString().replace(/[-:T.]/g, "").slice(0, 14);
607
+ const migrationName = name || "auto_migration";
608
+ const fileName = `${version}_${migrationName}.ts`;
609
+ const filePath = path.join(options.dir, fileName);
610
+ await fs.mkdir(options.dir, { recursive: true });
611
+ const upSQL = generateMigrationFromChanges(changes, version, migrationName).build(version, migrationName).up.map((sql) => ` .raw(\`${sql.replace(/`/g, "\\`")}\``).join("\n");
612
+ const migrationContent = `import { createMigration } from 'driftsql'
613
+
614
+ // This migration was automatically generated
615
+ // Generated at: ${(/* @__PURE__ */ new Date()).toISOString()}
616
+ // Changes: ${changes.length}
617
+
618
+ export const migration = createMigration('postgres')
619
+ ${upSQL}
620
+ .build('${version}', '${migrationName}')
621
+ `;
622
+ await fs.writeFile(filePath, migrationContent, "utf8");
623
+ await snapshotManager.save(currentSchema);
624
+ const { generateTypesFromSchema: generateTypesFromSchema$1 } = await import("./type-generator-DbcqBk-4.js");
625
+ await generateTypesFromSchema$1(currentSchema, path.join(process.cwd(), "db-types.ts"));
626
+ consola.success(`\nGenerated migration: ${fileName}`);
627
+ consola.success(`Updated TypeScript types: db-types.ts`);
628
+ consola.info("Snapshot updated. Run \"driftsql migrate:up\" to apply changes.");
629
+ } catch (error) {
630
+ consola.error("Failed to generate migration:", error);
631
+ process.exit(1);
632
+ }
633
+ });
634
+ program.command("db:inspect").description("Inspect database and generate TypeScript types").option("-c, --config <path>", "Config file path", "./driftsql.config.ts").option("-o, --output <path>", "Output file path", "./db-types.ts").action(async (options) => {
635
+ try {
636
+ const client = await loadClient(options.config);
637
+ await client.inspectDB({
638
+ driver: client.getDriver(),
639
+ outputFile: options.output
640
+ });
641
+ await client.close();
642
+ } catch (error) {
643
+ consola.error("Inspection failed:", error);
644
+ process.exit(1);
645
+ }
646
+ });
647
+ program.command("generate:types").description("Generate TypeScript types from schema file").option("-s, --schema <path>", "Schema file path", "./schema.ts").option("-o, --output <path>", "Output file path", "./db-types.ts").action(async (options) => {
648
+ try {
649
+ const schemaPath = path.resolve(process.cwd(), options.schema);
650
+ const outputPath = path.resolve(process.cwd(), options.output);
651
+ consola.start("Loading schema...");
652
+ const schemaModule = await import(schemaPath);
653
+ const schema = schemaModule.default || schemaModule.schema;
654
+ if (!schema || !Array.isArray(schema)) {
655
+ consola.error("Schema file must export default or named \"schema\" as an array of table definitions");
656
+ process.exit(1);
657
+ }
658
+ const { generateTypesFromSchema: generateTypesFromSchema$1 } = await import("./type-generator-DbcqBk-4.js");
659
+ await generateTypesFromSchema$1(schema, outputPath);
660
+ consola.success(`TypeScript types generated: ${options.output}`);
661
+ } catch (error) {
662
+ consola.error("Type generation failed:", error);
663
+ process.exit(1);
664
+ }
665
+ });
666
+ program.command("init").description("Initialize DriftSQL with a basic notes schema").option("-d, --dir <directory>", "Migrations directory", "./migrations").action(async (options) => {
667
+ try {
668
+ await fs.mkdir(options.dir, { recursive: true });
669
+ const configContent = `import { SQLClient, PostgresDriver } from 'driftsql'
670
+
671
+ export default new SQLClient({
672
+ driver: new PostgresDriver({
673
+ connectionString: process.env.DATABASE_URL!,
674
+ }),
675
+ })
676
+ `;
677
+ const schemaContent = `import { createTable, serial, varchar, text, timestamp, boolean } from 'driftsql'
678
+
679
+ const notesTable = createTable('notes', (table) => {
680
+ table
681
+ .column(serial('id').primaryKey())
682
+ .column(varchar('title', 255).notNull())
683
+ .column(text('content'))
684
+ .column(boolean('is_archived').default(false).notNull())
685
+ .column(timestamp('created_at').default('CURRENT_TIMESTAMP').notNull())
686
+ .column(timestamp('updated_at').default('CURRENT_TIMESTAMP').notNull())
687
+ .index('idx_notes_archived', ['is_archived'])
688
+ .index('idx_notes_created_at', ['created_at'])
689
+ })
690
+
691
+ // Export array of table definitions
692
+ export default [notesTable.getDefinition()]
693
+ `;
694
+ const envContent = `DATABASE_URL=postgresql://user:password@localhost:5432/mydb
695
+ `;
696
+ const gitignoreContent = `.driftsql/
697
+ .env
698
+ `;
699
+ const configPath = path.join(process.cwd(), "driftsql.config.ts");
700
+ const schemaPath = path.join(process.cwd(), "schema.ts");
701
+ const envPath = path.join(process.cwd(), ".env.example");
702
+ const gitignorePath = path.join(process.cwd(), ".gitignore");
703
+ const filesToCreate = [
704
+ {
705
+ path: configPath,
706
+ content: configContent,
707
+ name: "driftsql.config.ts"
708
+ },
709
+ {
710
+ path: schemaPath,
711
+ content: schemaContent,
712
+ name: "schema.ts"
713
+ },
714
+ {
715
+ path: envPath,
716
+ content: envContent,
717
+ name: ".env.example"
718
+ }
719
+ ];
720
+ for (const file of filesToCreate) try {
721
+ await fs.access(file.path);
722
+ consola.warn(`${file.name} already exists, skipping...`);
723
+ } catch {
724
+ await fs.writeFile(file.path, file.content, "utf8");
725
+ consola.success(`Created ${file.name}`);
726
+ }
727
+ try {
728
+ if (!(await fs.readFile(gitignorePath, "utf8")).includes(".driftsql/")) {
729
+ await fs.appendFile(gitignorePath, "\n" + gitignoreContent);
730
+ consola.success("Updated .gitignore");
731
+ }
732
+ } catch {
733
+ await fs.writeFile(gitignorePath, gitignoreContent, "utf8");
734
+ consola.success("Created .gitignore");
735
+ }
736
+ consola.success("\n✨ DriftSQL initialized successfully!\n");
737
+ consola.info("Next steps:");
738
+ consola.info("1. Copy .env.example to .env and update DATABASE_URL");
739
+ consola.info("2. Run: driftsql migrate:generate initial (generates migration + types)");
740
+ consola.info("3. Run: driftsql migrate:up");
741
+ consola.info("4. Import Database type from db-types.ts for type safety");
742
+ consola.info("5. Edit schema.ts and run migrate:generate to auto-detect changes!\n");
743
+ } catch (error) {
744
+ consola.error("Initialization failed:", error);
745
+ process.exit(1);
746
+ }
747
+ });
748
+ async function loadClient(configPath) {
749
+ try {
750
+ const config = await import(path.resolve(process.cwd(), configPath));
751
+ return config.default || config.client;
752
+ } catch (error) {
753
+ consola.error(`Failed to load config from ${configPath}`);
754
+ throw error;
755
+ }
756
+ }
757
+ async function loadMigrations(migrationDir, configPath) {
758
+ const client = await loadClient(configPath);
759
+ const migrations = [];
760
+ try {
761
+ const dir = path.resolve(process.cwd(), migrationDir);
762
+ const tsFiles = (await fs.readdir(dir)).filter((f) => f.endsWith(".ts") || f.endsWith(".js")).sort();
763
+ for (const file of tsFiles) {
764
+ const module = await import(path.join(dir, file));
765
+ const migration = module.migration || module.default;
766
+ if (migration) migrations.push(migration);
767
+ }
768
+ return {
769
+ client,
770
+ migrations
771
+ };
772
+ } catch (error) {
773
+ consola.error(`Failed to load migrations from ${migrationDir}`);
774
+ throw error;
775
+ }
776
+ }
777
+ program.parse();
778
+
779
+ //#endregion
780
+ //#region src/types.ts
781
+ function hasTransactionSupport(driver) {
782
+ return "transaction" in driver && typeof driver.transaction === "function";
783
+ }
784
+ function hasPreparedStatementSupport(driver) {
785
+ return "prepare" in driver && typeof driver.prepare === "function";
786
+ }
787
+ var DatabaseError = class extends Error {
788
+ constructor(message, driverType, originalError) {
789
+ super(message);
790
+ this.driverType = driverType;
791
+ this.originalError = originalError;
792
+ this.name = "DatabaseError";
793
+ }
794
+ };
795
+ var QueryError = class extends DatabaseError {
796
+ constructor(driverType, sql, originalError) {
797
+ super(`Query failed: ${sql}`, driverType, originalError);
798
+ this.name = "QueryError";
799
+ }
800
+ };
801
+
802
+ //#endregion
803
+ //#region src/utils/inspect.ts
804
+ const withTimeout = (promise, timeoutMs = 3e4) => {
805
+ return Promise.race([promise, new Promise((_, reject) => setTimeout(() => reject(/* @__PURE__ */ new Error(`Query timeout after ${timeoutMs}ms`)), timeoutMs))]);
806
+ };
807
+ const retryQuery = async (queryFn, maxRetries = 3, baseDelay = 1e3) => {
808
+ for (let attempt = 1; attempt <= maxRetries; attempt++) try {
809
+ return await queryFn();
810
+ } catch (error) {
811
+ if (attempt === maxRetries) throw error;
812
+ const delay = baseDelay * Math.pow(2, attempt - 1);
813
+ consola.warn(`Query attempt ${attempt} failed, retrying in ${delay}ms...`, error);
814
+ await new Promise((resolve) => setTimeout(resolve, delay));
815
+ }
816
+ throw new Error("Max retries exceeded");
817
+ };
818
+ const mapDatabaseTypeToTypeScript = (dataType, isNullable = false, driverType = "postgres") => {
819
+ const nullable = isNullable ? " | null" : "";
820
+ switch (dataType.toLowerCase()) {
821
+ case "uuid": return `string${nullable}`;
822
+ case "character varying":
823
+ case "varchar":
824
+ case "text":
825
+ case "char":
826
+ case "character":
827
+ case "longtext":
828
+ case "mediumtext":
829
+ case "tinytext": return `string${nullable}`;
830
+ case "integer":
831
+ case "int":
832
+ case "int4":
833
+ case "smallint":
834
+ case "int2":
835
+ case "bigint":
836
+ case "int8":
837
+ case "serial":
838
+ case "bigserial":
839
+ case "numeric":
840
+ case "decimal":
841
+ case "real":
842
+ case "float4":
843
+ case "double precision":
844
+ case "float8":
845
+ case "tinyint":
846
+ case "mediumint":
847
+ case "float":
848
+ case "double": return `number${nullable}`;
849
+ case "boolean":
850
+ case "bool":
851
+ case "bit": return `boolean${nullable}`;
852
+ case "timestamp":
853
+ case "timestamp with time zone":
854
+ case "timestamp without time zone":
855
+ case "timestamptz":
856
+ case "date":
857
+ case "time":
858
+ case "time with time zone":
859
+ case "time without time zone":
860
+ case "timetz":
861
+ case "interval":
862
+ case "datetime":
863
+ case "year": return `Date${nullable}`;
864
+ case "json":
865
+ case "jsonb": return `any${nullable}`;
866
+ case "array": return `any[]${nullable}`;
867
+ case "bytea":
868
+ case "binary":
869
+ case "varbinary":
870
+ case "blob":
871
+ case "longblob":
872
+ case "mediumblob":
873
+ case "tinyblob": return `Buffer${nullable}`;
874
+ case "enum":
875
+ case "set": return `string${nullable}`;
876
+ default:
877
+ consola.warn(`Unknown ${driverType} type: ${dataType}, defaulting to 'any'`);
878
+ return `any${nullable}`;
879
+ }
880
+ };
881
+ const getDriverType = (driver) => {
882
+ return `${driver.constructor.name || "unknown driver"}`;
883
+ };
884
+ const inspectDB = async (options) => {
885
+ consola.warn("inspectDB is experimental and may make mistakes when inspecting your database. However it will not destroy your database.");
886
+ const { driver, outputFile = "db-types.ts" } = options;
887
+ const driverType = getDriverType(driver);
888
+ consola.start(`Inspecting database using ${driverType} driver`);
889
+ const client = new SQLClient({ driver });
890
+ let generatedTypes = "";
891
+ try {
892
+ let tablesQuery;
893
+ let tableSchemaFilter;
894
+ if (driverType === "mysql") {
895
+ const currentDatabase = (await withTimeout(retryQuery(() => client.query("SELECT DATABASE() as `database`", [])), 1e4)).rows[0]?.database;
896
+ if (!currentDatabase) throw new Error("Could not determine current MySQL database name");
897
+ consola.success(`Using MySQL database: ${currentDatabase}`);
898
+ tablesQuery = `SELECT TABLE_NAME as table_name
899
+ FROM information_schema.tables
900
+ WHERE TABLE_SCHEMA = ?
901
+ AND TABLE_TYPE = 'BASE TABLE'
902
+ ORDER BY TABLE_NAME`;
903
+ tableSchemaFilter = currentDatabase;
904
+ } else if (driverType === "postgres" || driverType === "neon") {
905
+ tablesQuery = `SELECT table_name
906
+ FROM information_schema.tables
907
+ WHERE table_schema = $1
908
+ AND table_type = 'BASE TABLE'
909
+ ORDER BY table_name`;
910
+ tableSchemaFilter = "public";
911
+ } else if (driverType === "libsql" || driverType === "sqlite" || driverType === "sqlitecloud") {
912
+ tablesQuery = `SELECT name as table_name
913
+ FROM sqlite_master
914
+ WHERE type = 'table'
915
+ ORDER BY name`;
916
+ tableSchemaFilter = void 0;
917
+ } else throw new Error(`Unsupported driver type: ${driverType}`);
918
+ const tables = await withTimeout(retryQuery(() => client.query(tablesQuery, tableSchemaFilter ? [tableSchemaFilter] : [])), 3e4);
919
+ consola.info("Tables in the database:", tables.rows.map((t) => t.table_name).join(", "));
920
+ let processedTables = 0;
921
+ const totalTables = tables.rows.length;
922
+ for (const table of tables.rows) {
923
+ const tableName = table.table_name;
924
+ if (tableName.startsWith("sqlite_sequence") || tableName.startsWith("_prisma_migrations")) continue;
925
+ processedTables++;
926
+ consola.info(`[${processedTables}/${totalTables}] Inspecting table: ${tableName}`);
927
+ try {
928
+ let columnsQuery;
929
+ let queryParams;
930
+ if (driverType === "mysql") {
931
+ columnsQuery = `
932
+ SELECT
933
+ COLUMN_NAME as column_name,
934
+ DATA_TYPE as data_type,
935
+ IS_NULLABLE as is_nullable,
936
+ COLUMN_DEFAULT as column_default
937
+ FROM information_schema.columns
938
+ WHERE TABLE_NAME = ?
939
+ AND TABLE_SCHEMA = ?
940
+ ORDER BY ORDINAL_POSITION
941
+ `;
942
+ queryParams = [tableName, tableSchemaFilter];
943
+ } else if (driverType === "postgres" || driverType === "neon") {
944
+ columnsQuery = `
945
+ SELECT
946
+ column_name,
947
+ data_type,
948
+ is_nullable,
949
+ column_default
950
+ FROM information_schema.columns
951
+ WHERE table_name = $1
952
+ AND table_schema = $2
953
+ ORDER BY ordinal_position
954
+ `;
955
+ queryParams = [tableName, tableSchemaFilter];
956
+ } else {
957
+ columnsQuery = `
958
+ SELECT
959
+ name as column_name,
960
+ type as data_type,
961
+ CASE WHEN "notnull" = 0 THEN 'YES' ELSE 'NO' END as is_nullable,
962
+ dflt_value as column_default
963
+ FROM pragma_table_info(?)
964
+ ORDER BY cid
965
+ `;
966
+ queryParams = [tableName];
967
+ }
968
+ const columns = await withTimeout(retryQuery(() => client.query(columnsQuery, queryParams)), 15e3);
969
+ if (columns.rows.length === 0) {
970
+ consola.info(`No columns found for table: ${tableName}`);
971
+ continue;
972
+ }
973
+ consola.info(`Columns in '${tableName}'`);
974
+ columns.rows.forEach((c) => {
975
+ const typeDisplay = c.data_type + (c.is_nullable === "YES" ? " (nullable)" : "");
976
+ consola.info(` > ${chalk.bold.yellow(typeDisplay)} > ${c.column_name}`);
977
+ });
978
+ const uniqueColumns = /* @__PURE__ */ new Map();
979
+ columns.rows.forEach((col) => {
980
+ if (!uniqueColumns.has(col.column_name)) uniqueColumns.set(col.column_name, col);
981
+ });
982
+ generatedTypes += `export interface ${tableName.charAt(0).toUpperCase() + tableName.slice(1)} {\n`;
983
+ for (const col of uniqueColumns.values()) {
984
+ const tsType = mapDatabaseTypeToTypeScript(col.data_type, col.is_nullable === "YES", driverType);
985
+ generatedTypes += ` ${col.column_name}: ${tsType};\n`;
986
+ }
987
+ generatedTypes += "}\n\n";
988
+ } catch (error) {
989
+ consola.error(`Failed to process table ${tableName}:`, error);
990
+ consola.info(`Skipping table ${tableName} and continuing...`);
991
+ continue;
992
+ }
993
+ }
994
+ generatedTypes += "export interface Database {\n";
995
+ for (const table of tables.rows) {
996
+ if (table.table_name.startsWith("sqlite_sequence") || table.table_name.startsWith("_prisma_migrations")) continue;
997
+ const interfaceName = table.table_name.charAt(0).toUpperCase() + table.table_name.slice(1);
998
+ generatedTypes += ` ${table.table_name}: ${interfaceName};\n`;
999
+ }
1000
+ generatedTypes += "}\n\n";
1001
+ await fs.writeFile(outputFile, generatedTypes, "utf8");
1002
+ consola.success(`TypeScript types written to ${outputFile}`);
1003
+ consola.success(`Successfully processed ${processedTables} tables`);
1004
+ } catch (error) {
1005
+ consola.error("Fatal error during database inspection:", error);
1006
+ throw error;
1007
+ } finally {
1008
+ await client.close().catch((error) => consola.error("Error closing client:", error));
1009
+ }
1010
+ };
1011
+ var inspect_default = inspectDB;
1012
+
1013
+ //#endregion
1014
+ //#region src/drivers/helpers/postgres.ts
1015
+ function createPostgresHelpers(driver) {
1016
+ return {
1017
+ async findFirst(table, where) {
1018
+ const conditions = [];
1019
+ const params = [];
1020
+ let paramIndex = 1;
1021
+ if (where) for (const [key, value] of Object.entries(where)) {
1022
+ conditions.push(`"${key}" = $${paramIndex}`);
1023
+ params.push(value);
1024
+ paramIndex++;
1025
+ }
1026
+ const sql = `SELECT * FROM "${table}" ${conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : ""} LIMIT 1`;
1027
+ return await driver.query(sql, params);
1028
+ },
1029
+ async findMany(table, options) {
1030
+ const conditions = [];
1031
+ const params = [];
1032
+ let paramIndex = 1;
1033
+ if (options?.where) for (const [key, value] of Object.entries(options.where)) {
1034
+ conditions.push(`"${key}" = $${paramIndex}`);
1035
+ params.push(value);
1036
+ paramIndex++;
1037
+ }
1038
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1039
+ const limitClause = options?.limit ? `LIMIT $${paramIndex++}` : "";
1040
+ const offsetClause = options?.offset ? `OFFSET $${paramIndex++}` : "";
1041
+ if (options?.limit) params.push(options.limit);
1042
+ if (options?.offset) params.push(options.offset);
1043
+ const sql = `SELECT * FROM "${table}" ${whereClause} ${limitClause} ${offsetClause}`.trim();
1044
+ return await driver.query(sql, params);
1045
+ },
1046
+ async insert(table, data) {
1047
+ const keys = Object.keys(data);
1048
+ const values = Object.values(data);
1049
+ const sql = `INSERT INTO "${table}" (${keys.map((k) => `"${k}"`).join(", ")}) VALUES (${keys.map((_, i) => `$${i + 1}`).join(", ")}) RETURNING *`;
1050
+ return await driver.query(sql, values);
1051
+ },
1052
+ async update(table, data, where) {
1053
+ const setEntries = Object.entries(data);
1054
+ const whereEntries = Object.entries(where);
1055
+ const params = [];
1056
+ let paramIndex = 1;
1057
+ const setClauses = setEntries.map(([key, value]) => {
1058
+ params.push(value);
1059
+ return `"${key}" = $${paramIndex++}`;
1060
+ });
1061
+ const whereClauses = whereEntries.map(([key, value]) => {
1062
+ params.push(value);
1063
+ return `"${key}" = $${paramIndex++}`;
1064
+ });
1065
+ const sql = `UPDATE "${table}" SET ${setClauses.join(", ")} WHERE ${whereClauses.join(" AND ")} RETURNING *`;
1066
+ return await driver.query(sql, params);
1067
+ },
1068
+ async delete(table, where) {
1069
+ const conditions = [];
1070
+ const params = [];
1071
+ let paramIndex = 1;
1072
+ for (const [key, value] of Object.entries(where)) {
1073
+ conditions.push(`"${key}" = $${paramIndex}`);
1074
+ params.push(value);
1075
+ paramIndex++;
1076
+ }
1077
+ const sql = `DELETE FROM "${table}" WHERE ${conditions.join(" AND ")}`;
1078
+ return (await driver.query(sql, params)).rowCount;
1079
+ }
1080
+ };
1081
+ }
1082
+
1083
+ //#endregion
1084
+ //#region src/drivers/postgres.ts
1085
+ var PostgresDriver = class {
1086
+ client;
1087
+ findFirst;
1088
+ findMany;
1089
+ insert;
1090
+ update;
1091
+ delete;
1092
+ constructor(config) {
1093
+ this.client = postgres(config.connectionString, {
1094
+ max: config.max ?? 10,
1095
+ idle_timeout: config.idle_timeout ?? 30,
1096
+ connect_timeout: config.connect_timeout ?? 10,
1097
+ prepare: false,
1098
+ transform: { undefined: null }
1099
+ });
1100
+ const helpers = createPostgresHelpers(this);
1101
+ this.findFirst = helpers.findFirst;
1102
+ this.findMany = helpers.findMany;
1103
+ this.insert = helpers.insert;
1104
+ this.update = helpers.update;
1105
+ this.delete = helpers.delete;
1106
+ }
1107
+ async query(sql, params) {
1108
+ try {
1109
+ const result = await this.client.unsafe(sql, params || []);
1110
+ const rows = Array.isArray(result) ? [...result] : [];
1111
+ return {
1112
+ rows,
1113
+ rowCount: rows.length,
1114
+ command: result.command
1115
+ };
1116
+ } catch (error) {
1117
+ throw new QueryError("postgres", sql, error);
1118
+ }
1119
+ }
1120
+ async close() {
1121
+ await this.client.end();
1122
+ }
1123
+ };
1124
+
1125
+ //#endregion
1126
+ //#region src/drivers/helpers/mysql.ts
1127
+ function createMySQLHelpers(driver) {
1128
+ return {
1129
+ async findFirst(table, where) {
1130
+ const conditions = [];
1131
+ const params = [];
1132
+ if (where) for (const [key, value] of Object.entries(where)) {
1133
+ conditions.push(`\`${key}\` = ?`);
1134
+ params.push(value);
1135
+ }
1136
+ const sql = `SELECT * FROM \`${table}\` ${conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : ""} LIMIT 1`;
1137
+ return await driver.query(sql, params);
1138
+ },
1139
+ async findMany(table, options) {
1140
+ const conditions = [];
1141
+ const params = [];
1142
+ if (options?.where) for (const [key, value] of Object.entries(options.where)) {
1143
+ conditions.push(`\`${key}\` = ?`);
1144
+ params.push(value);
1145
+ }
1146
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1147
+ const limitClause = options?.limit ? `LIMIT ?` : "";
1148
+ const offsetClause = options?.offset ? `OFFSET ?` : "";
1149
+ if (options?.limit) params.push(options.limit);
1150
+ if (options?.offset) params.push(options.offset);
1151
+ const sql = `SELECT * FROM \`${table}\` ${whereClause} ${limitClause} ${offsetClause}`.trim();
1152
+ return await driver.query(sql, params);
1153
+ },
1154
+ async insert(table, data) {
1155
+ const keys = Object.keys(data);
1156
+ const values = Object.values(data);
1157
+ const sql = `INSERT INTO \`${table}\` (${keys.map((k) => `\`${k}\``).join(", ")}) VALUES (${keys.map(() => "?").join(", ")})`;
1158
+ await driver.query(sql, values);
1159
+ const selectSql = `SELECT * FROM \`${table}\` WHERE id = LAST_INSERT_ID()`;
1160
+ return await driver.query(selectSql);
1161
+ },
1162
+ async update(table, data, where) {
1163
+ const setEntries = Object.entries(data);
1164
+ const whereEntries = Object.entries(where);
1165
+ const params = [];
1166
+ const setClauses = setEntries.map(([key, value]) => {
1167
+ params.push(value);
1168
+ return `\`${key}\` = ?`;
1169
+ });
1170
+ const whereClauses = whereEntries.map(([key, value]) => {
1171
+ params.push(value);
1172
+ return `\`${key}\` = ?`;
1173
+ });
1174
+ const sql = `UPDATE \`${table}\` SET ${setClauses.join(", ")} WHERE ${whereClauses.join(" AND ")}`;
1175
+ await driver.query(sql, params);
1176
+ const selectParams = Object.values(where);
1177
+ const selectSql = `SELECT * FROM \`${table}\` WHERE ${Object.keys(where).map((k) => `\`${k}\` = ?`).join(" AND ")}`;
1178
+ return await driver.query(selectSql, selectParams);
1179
+ },
1180
+ async delete(table, where) {
1181
+ const conditions = [];
1182
+ const params = [];
1183
+ for (const [key, value] of Object.entries(where)) {
1184
+ conditions.push(`\`${key}\` = ?`);
1185
+ params.push(value);
1186
+ }
1187
+ const sql = `DELETE FROM \`${table}\` WHERE ${conditions.join(" AND ")}`;
1188
+ return (await driver.query(sql, params)).rowCount;
1189
+ }
1190
+ };
1191
+ }
1192
+
1193
+ //#endregion
1194
+ //#region src/drivers/helpers/sqlite.ts
1195
+ function createSQLiteHelpers(driver) {
1196
+ return {
1197
+ async findFirst(table, where) {
1198
+ const conditions = [];
1199
+ const params = [];
1200
+ if (where) for (const [key, value] of Object.entries(where)) {
1201
+ conditions.push(`"${key}" = ?`);
1202
+ params.push(value);
1203
+ }
1204
+ const sql = `SELECT * FROM "${table}" ${conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : ""} LIMIT 1`;
1205
+ return await driver.query(sql, params);
1206
+ },
1207
+ async findMany(table, options) {
1208
+ const conditions = [];
1209
+ const params = [];
1210
+ if (options?.where) for (const [key, value] of Object.entries(options.where)) {
1211
+ conditions.push(`"${key}" = ?`);
1212
+ params.push(value);
1213
+ }
1214
+ const whereClause = conditions.length > 0 ? `WHERE ${conditions.join(" AND ")}` : "";
1215
+ const limitClause = options?.limit ? `LIMIT ?` : "";
1216
+ const offsetClause = options?.offset ? `OFFSET ?` : "";
1217
+ if (options?.limit) params.push(options.limit);
1218
+ if (options?.offset) params.push(options.offset);
1219
+ const sql = `SELECT * FROM "${table}" ${whereClause} ${limitClause} ${offsetClause}`.trim();
1220
+ return await driver.query(sql, params);
1221
+ },
1222
+ async insert(table, data) {
1223
+ const keys = Object.keys(data);
1224
+ const values = Object.values(data);
1225
+ const sql = `INSERT INTO "${table}" (${keys.map((k) => `"${k}"`).join(", ")}) VALUES (${keys.map(() => "?").join(", ")}) RETURNING *`;
1226
+ return await driver.query(sql, values);
1227
+ },
1228
+ async update(table, data, where) {
1229
+ const setEntries = Object.entries(data);
1230
+ const whereEntries = Object.entries(where);
1231
+ const params = [];
1232
+ const setClauses = setEntries.map(([key, value]) => {
1233
+ params.push(value);
1234
+ return `"${key}" = ?`;
1235
+ });
1236
+ const whereClauses = whereEntries.map(([key, value]) => {
1237
+ params.push(value);
1238
+ return `"${key}" = ?`;
1239
+ });
1240
+ const sql = `UPDATE "${table}" SET ${setClauses.join(", ")} WHERE ${whereClauses.join(" AND ")} RETURNING *`;
1241
+ return await driver.query(sql, params);
1242
+ },
1243
+ async delete(table, where) {
1244
+ const conditions = [];
1245
+ const params = [];
1246
+ for (const [key, value] of Object.entries(where)) {
1247
+ conditions.push(`"${key}" = ?`);
1248
+ params.push(value);
1249
+ }
1250
+ const sql = `DELETE FROM "${table}" WHERE ${conditions.join(" AND ")}`;
1251
+ return (await driver.query(sql, params)).rowCount;
1252
+ }
1253
+ };
1254
+ }
1255
+
1256
+ //#endregion
1257
+ //#region src/schema/column.ts
1258
+ var Column = class {
1259
+ definition;
1260
+ constructor(name, type) {
1261
+ this.definition = {
1262
+ name,
1263
+ type
1264
+ };
1265
+ }
1266
+ length(len) {
1267
+ this.definition.length = len;
1268
+ return this;
1269
+ }
1270
+ precision(p, s) {
1271
+ this.definition.precision = p;
1272
+ if (s !== void 0) this.definition.scale = s;
1273
+ return this;
1274
+ }
1275
+ primaryKey() {
1276
+ this.definition.primaryKey = true;
1277
+ this.definition.notNull = true;
1278
+ return this;
1279
+ }
1280
+ notNull() {
1281
+ this.definition.notNull = true;
1282
+ return this;
1283
+ }
1284
+ unique() {
1285
+ this.definition.unique = true;
1286
+ return this;
1287
+ }
1288
+ default(value) {
1289
+ this.definition.default = value;
1290
+ return this;
1291
+ }
1292
+ references(table, column = "id") {
1293
+ this.definition.references = {
1294
+ table,
1295
+ column
1296
+ };
1297
+ return this;
1298
+ }
1299
+ onDelete(action) {
1300
+ if (this.definition.references) this.definition.references.onDelete = action;
1301
+ return this;
1302
+ }
1303
+ onUpdate(action) {
1304
+ if (this.definition.references) this.definition.references.onUpdate = action;
1305
+ return this;
1306
+ }
1307
+ check(expression) {
1308
+ this.definition.check = expression;
1309
+ return this;
1310
+ }
1311
+ getDefinition() {
1312
+ return this.definition;
1313
+ }
1314
+ };
1315
+ function serial(name) {
1316
+ return new Column(name, "serial");
1317
+ }
1318
+ function bigserial(name) {
1319
+ return new Column(name, "bigserial");
1320
+ }
1321
+ function integer(name) {
1322
+ return new Column(name, "integer");
1323
+ }
1324
+ function bigint(name) {
1325
+ return new Column(name, "bigint");
1326
+ }
1327
+ function smallint(name) {
1328
+ return new Column(name, "smallint");
1329
+ }
1330
+ function text(name) {
1331
+ return new Column(name, "text");
1332
+ }
1333
+ function varchar(name, length) {
1334
+ const col = new Column(name, "varchar");
1335
+ if (length) col.length(length);
1336
+ return col;
1337
+ }
1338
+ function char(name, length) {
1339
+ const col = new Column(name, "char");
1340
+ if (length) col.length(length);
1341
+ return col;
1342
+ }
1343
+ function boolean(name) {
1344
+ return new Column(name, "boolean");
1345
+ }
1346
+ function timestamp(name) {
1347
+ return new Column(name, "timestamp");
1348
+ }
1349
+ function timestamptz(name) {
1350
+ return new Column(name, "timestamptz");
1351
+ }
1352
+ function date(name) {
1353
+ return new Column(name, "date");
1354
+ }
1355
+ function time(name) {
1356
+ return new Column(name, "time");
1357
+ }
1358
+ function json(name) {
1359
+ return new Column(name, "json");
1360
+ }
1361
+ function jsonb(name) {
1362
+ return new Column(name, "jsonb");
1363
+ }
1364
+ function uuid(name) {
1365
+ return new Column(name, "uuid");
1366
+ }
1367
+ function decimal(name, precision, scale) {
1368
+ const col = new Column(name, "decimal");
1369
+ if (precision) col.precision(precision, scale);
1370
+ return col;
1371
+ }
1372
+ function numeric(name, precision, scale) {
1373
+ const col = new Column(name, "numeric");
1374
+ if (precision) col.precision(precision, scale);
1375
+ return col;
1376
+ }
1377
+ function real(name) {
1378
+ return new Column(name, "real");
1379
+ }
1380
+ function doublePrecision(name) {
1381
+ return new Column(name, "double precision");
1382
+ }
1383
+ function bytea(name) {
1384
+ return new Column(name, "bytea");
1385
+ }
1386
+
1387
+ //#endregion
1388
+ //#region src/schema/table.ts
1389
+ var Table = class {
1390
+ definition;
1391
+ constructor(name) {
1392
+ this.definition = {
1393
+ name,
1394
+ columns: [],
1395
+ indexes: [],
1396
+ checks: []
1397
+ };
1398
+ }
1399
+ column(column) {
1400
+ this.definition.columns.push(column.getDefinition());
1401
+ return this;
1402
+ }
1403
+ primaryKey(...columns) {
1404
+ this.definition.primaryKey = columns;
1405
+ return this;
1406
+ }
1407
+ index(name, columns, options) {
1408
+ const index = {
1409
+ name,
1410
+ columns,
1411
+ unique: options?.unique,
1412
+ type: options?.type
1413
+ };
1414
+ this.definition.indexes?.push(index);
1415
+ return this;
1416
+ }
1417
+ unique(name, ...columns) {
1418
+ return this.index(name, columns, { unique: true });
1419
+ }
1420
+ check(name, expression) {
1421
+ this.definition.checks?.push({
1422
+ name,
1423
+ expression
1424
+ });
1425
+ return this;
1426
+ }
1427
+ getDefinition() {
1428
+ return this.definition;
1429
+ }
1430
+ };
1431
+ function createTable(name, callback) {
1432
+ const table = new Table(name);
1433
+ callback(table);
1434
+ return table;
1435
+ }
1436
+
1437
+ //#endregion
1438
+ //#region src/schema/index.ts
1439
+ init_postgres();
1440
+ init_mysql();
1441
+ init_sqlite();
1442
+
1443
+ //#endregion
1444
+ //#region src/index.ts
1445
+ var SQLClient = class SQLClient {
1446
+ primaryDriver;
1447
+ fallbackDrivers;
1448
+ queryCache;
1449
+ cacheTTL;
1450
+ maxCacheSize = 100;
1451
+ constructor(options) {
1452
+ this.primaryDriver = options.driver;
1453
+ this.fallbackDrivers = options.fallbackDrivers || [];
1454
+ this.queryCache = /* @__PURE__ */ new Map();
1455
+ this.cacheTTL = options.cacheTTL ?? 5e3;
1456
+ }
1457
+ isCacheValid(entry) {
1458
+ return Date.now() - entry.timestamp < this.cacheTTL;
1459
+ }
1460
+ getCacheKey(sql, params) {
1461
+ return JSON.stringify({
1462
+ sql,
1463
+ params
1464
+ });
1465
+ }
1466
+ maintainCacheSize() {
1467
+ if (this.queryCache.size > this.maxCacheSize) {
1468
+ const firstKey = this.queryCache.keys().next().value;
1469
+ if (firstKey !== void 0) this.queryCache.delete(firstKey);
1470
+ }
1471
+ }
1472
+ cacheResult(cacheKey, result) {
1473
+ this.queryCache.set(cacheKey, {
1474
+ result,
1475
+ timestamp: Date.now()
1476
+ });
1477
+ this.maintainCacheSize();
1478
+ }
1479
+ async tryFallbackDrivers(sql, params) {
1480
+ consola.warn(`Query failed with ${this.primaryDriver.constructor.name}, trying fallbacks`);
1481
+ let lastError;
1482
+ for (const driver of this.fallbackDrivers) try {
1483
+ return await driver.query(sql, params);
1484
+ } catch (error) {
1485
+ lastError = error;
1486
+ }
1487
+ throw lastError || new DatabaseError("All drivers failed to execute query", "unknown");
1488
+ }
1489
+ async query(sql, params) {
1490
+ const cacheKey = this.getCacheKey(sql, params);
1491
+ const cached = this.queryCache.get(cacheKey);
1492
+ if (cached && this.isCacheValid(cached)) return cached.result;
1493
+ try {
1494
+ const result = await this.primaryDriver.query(sql, params);
1495
+ this.cacheResult(cacheKey, result);
1496
+ return result;
1497
+ } catch (primaryError) {
1498
+ if (this.fallbackDrivers.length === 0) throw primaryError;
1499
+ return await this.tryFallbackDrivers(sql, params);
1500
+ }
1501
+ }
1502
+ async transaction(callback) {
1503
+ if (!hasTransactionSupport(this.primaryDriver)) throw new DatabaseError("Primary driver does not support transactions", this.primaryDriver.constructor.name);
1504
+ return await this.primaryDriver.transaction(async (transactionDriver) => {
1505
+ return await callback(new SQLClient({
1506
+ driver: transactionDriver,
1507
+ fallbackDrivers: []
1508
+ }));
1509
+ });
1510
+ }
1511
+ async prepare(sql) {
1512
+ if (!hasPreparedStatementSupport(this.primaryDriver)) throw new DatabaseError("Primary driver does not support prepared statements", this.primaryDriver.constructor.name);
1513
+ return await this.primaryDriver.prepare(sql);
1514
+ }
1515
+ async findFirst(table, where) {
1516
+ if (!this.primaryDriver.findFirst) throw new DatabaseError("Primary driver does not support findFirst", this.primaryDriver.constructor.name);
1517
+ return (await this.primaryDriver.findFirst(table, where))?.rows[0] || null;
1518
+ }
1519
+ async findMany(table, options) {
1520
+ if (!this.primaryDriver.findMany) throw new DatabaseError("Primary driver does not support findMany", this.primaryDriver.constructor.name);
1521
+ return (await this.primaryDriver.findMany(table, options)).rows;
1522
+ }
1523
+ async insert(table, data) {
1524
+ if (!this.primaryDriver.insert) throw new DatabaseError("Primary driver does not support insert", this.primaryDriver.constructor.name);
1525
+ return (await this.primaryDriver.insert(table, data)).rows[0];
1526
+ }
1527
+ async update(table, data, where) {
1528
+ if (!this.primaryDriver.update) throw new DatabaseError("Primary driver does not support update", this.primaryDriver.constructor.name);
1529
+ return (await this.primaryDriver.update(table, data, where)).rows[0] || null;
1530
+ }
1531
+ async delete(table, where) {
1532
+ if (!this.primaryDriver.delete) throw new DatabaseError("Primary driver does not support delete", this.primaryDriver.constructor.name);
1533
+ return await this.primaryDriver.delete(table, where);
1534
+ }
1535
+ getDriver() {
1536
+ return this.primaryDriver;
1537
+ }
1538
+ async inspectDB(options) {
1539
+ return await inspect_default({
1540
+ driver: this.getDriver(),
1541
+ ...options
1542
+ });
1543
+ }
1544
+ supportsTransactions() {
1545
+ return hasTransactionSupport(this.primaryDriver);
1546
+ }
1547
+ supportsPreparedStatements() {
1548
+ return hasPreparedStatementSupport(this.primaryDriver);
1549
+ }
1550
+ clearCache() {
1551
+ this.queryCache.clear();
1552
+ }
1553
+ getCacheStats() {
1554
+ return {
1555
+ size: this.queryCache.size,
1556
+ ttl: this.cacheTTL
1557
+ };
1558
+ }
1559
+ async close() {
1560
+ this.queryCache.clear();
1561
+ const closePromises = [this.primaryDriver, ...this.fallbackDrivers].map((driver) => driver.close().catch((err) => consola.warn(`Error closing ${driver.constructor.name}:`, err)));
1562
+ await Promise.all(closePromises);
1563
+ }
1564
+ };
1565
+ const DriftSQLClient = SQLClient;
1566
+
1567
+ //#endregion
1568
+ export { Column, DriftSQLClient, MigrationBuilder, MigrationRunner, MySQLGenerator, PostgresDriver, PostgresGenerator, SQLClient, SQLiteGenerator, SchemaDiffer, SnapshotManager, Table, TypeGenerator, bigint, bigserial, boolean, bytea, char, createMigration, createMySQLHelpers, createPostgresHelpers, createSQLiteHelpers, createTable, date, decimal, detectChanges, doublePrecision, generateMigrationFromChanges, generateTypesFromSchema, integer, json, jsonb, numeric, real, serial, smallint, text, time, timestamp, timestamptz, uuid, varchar };