sqlite-zod-orm 3.16.0 → 3.17.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/dist/index.js +34 -0
- package/package.json +1 -1
- package/src/database.ts +53 -0
package/dist/index.js
CHANGED
|
@@ -5449,6 +5449,40 @@ class _Database {
|
|
|
5449
5449
|
columns(tableName) {
|
|
5450
5450
|
return this.db.query(`PRAGMA table_info("${tableName}")`).all();
|
|
5451
5451
|
}
|
|
5452
|
+
dump() {
|
|
5453
|
+
const result = {};
|
|
5454
|
+
for (const tableName of Object.keys(this.schemas)) {
|
|
5455
|
+
result[tableName] = this.db.query(`SELECT * FROM "${tableName}"`).all();
|
|
5456
|
+
}
|
|
5457
|
+
return result;
|
|
5458
|
+
}
|
|
5459
|
+
load(data, options) {
|
|
5460
|
+
const txn = this.db.transaction(() => {
|
|
5461
|
+
for (const [tableName, rows] of Object.entries(data)) {
|
|
5462
|
+
if (!this.schemas[tableName])
|
|
5463
|
+
continue;
|
|
5464
|
+
if (!options?.append) {
|
|
5465
|
+
this.db.run(`DELETE FROM "${tableName}"`);
|
|
5466
|
+
}
|
|
5467
|
+
for (const row of rows) {
|
|
5468
|
+
const cols = Object.keys(row).filter((k) => k !== "id");
|
|
5469
|
+
const placeholders = cols.map(() => "?").join(", ");
|
|
5470
|
+
const values = cols.map((c) => {
|
|
5471
|
+
const v = row[c];
|
|
5472
|
+
if (v !== null && v !== undefined && typeof v === "object" && !(v instanceof Buffer)) {
|
|
5473
|
+
return JSON.stringify(v);
|
|
5474
|
+
}
|
|
5475
|
+
return v;
|
|
5476
|
+
});
|
|
5477
|
+
this.db.query(`INSERT INTO "${tableName}" (${cols.map((c) => `"${c}"`).join(", ")}) VALUES (${placeholders})`).run(...values);
|
|
5478
|
+
}
|
|
5479
|
+
}
|
|
5480
|
+
});
|
|
5481
|
+
txn();
|
|
5482
|
+
}
|
|
5483
|
+
seed(fixtures) {
|
|
5484
|
+
this.load(fixtures, { append: true });
|
|
5485
|
+
}
|
|
5452
5486
|
}
|
|
5453
5487
|
var Database = _Database;
|
|
5454
5488
|
export {
|
package/package.json
CHANGED
package/src/database.ts
CHANGED
|
@@ -420,6 +420,59 @@ class _Database<Schemas extends SchemaMap> {
|
|
|
420
420
|
public columns(tableName: string): { name: string; type: string; notnull: number; pk: number }[] {
|
|
421
421
|
return this.db.query(`PRAGMA table_info("${tableName}")`).all() as any[];
|
|
422
422
|
}
|
|
423
|
+
|
|
424
|
+
// =========================================================================
|
|
425
|
+
// Data Import / Export
|
|
426
|
+
// =========================================================================
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Export all data as a JSON-serializable object.
|
|
430
|
+
* Each key is a table name, value is an array of raw row objects.
|
|
431
|
+
*/
|
|
432
|
+
public dump(): Record<string, any[]> {
|
|
433
|
+
const result: Record<string, any[]> = {};
|
|
434
|
+
for (const tableName of Object.keys(this.schemas)) {
|
|
435
|
+
result[tableName] = this.db.query(`SELECT * FROM "${tableName}"`).all();
|
|
436
|
+
}
|
|
437
|
+
return result;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Import data from a dump object. Truncates existing data first.
|
|
442
|
+
* Use `{ append: true }` to insert without truncating.
|
|
443
|
+
*/
|
|
444
|
+
public load(data: Record<string, any[]>, options?: { append?: boolean }): void {
|
|
445
|
+
const txn = this.db.transaction(() => {
|
|
446
|
+
for (const [tableName, rows] of Object.entries(data)) {
|
|
447
|
+
if (!this.schemas[tableName]) continue;
|
|
448
|
+
if (!options?.append) {
|
|
449
|
+
this.db.run(`DELETE FROM "${tableName}"`);
|
|
450
|
+
}
|
|
451
|
+
for (const row of rows) {
|
|
452
|
+
const cols = Object.keys(row).filter(k => k !== 'id');
|
|
453
|
+
const placeholders = cols.map(() => '?').join(', ');
|
|
454
|
+
const values = cols.map(c => {
|
|
455
|
+
const v = row[c];
|
|
456
|
+
// Auto-serialize objects/arrays
|
|
457
|
+
if (v !== null && v !== undefined && typeof v === 'object' && !(v instanceof Buffer)) {
|
|
458
|
+
return JSON.stringify(v);
|
|
459
|
+
}
|
|
460
|
+
return v;
|
|
461
|
+
});
|
|
462
|
+
this.db.query(`INSERT INTO "${tableName}" (${cols.map(c => `"${c}"`).join(', ')}) VALUES (${placeholders})`).run(...values);
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
});
|
|
466
|
+
txn();
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Seed tables with fixture data. Each key is a table name, value is an
|
|
471
|
+
* array of records to insert. Does NOT truncate — use for additive seeding.
|
|
472
|
+
*/
|
|
473
|
+
public seed(fixtures: Record<string, Record<string, any>[]>): void {
|
|
474
|
+
this.load(fixtures, { append: true });
|
|
475
|
+
}
|
|
423
476
|
}
|
|
424
477
|
|
|
425
478
|
// =============================================================================
|