UNPKG

quicklite

Version:

A lightweight ORM toolkit for SQLite in Node.js applications

87 lines 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DbInitializer = void 0; /** * Database initializer for creating tables and schema */ class DbInitializer { /** * Creates a new DbInitializer instance * @param db SQLite database instance */ constructor(db) { this.entityClasses = []; this.db = db; } /** * Registers an entity class for initialization * @param entityClass Entity class that extends BaseEntity * @returns this instance for chaining */ register(entityClass) { this.entityClasses.push(entityClass); return this; } /** * Registers multiple entity classes for initialization * @param entityClasses Array of entity classes * @returns this instance for chaining */ registerMany(entityClasses) { this.entityClasses.push(...entityClasses); return this; } /** * Initializes all registered tables * @returns this instance for chaining */ initTables() { try { // Create transaction to ensure atomic table creation const transaction = this.db.transaction(() => { for (const entityClass of this.entityClasses) { const createTableSQL = entityClass.getCreateTableSQL(); this.db.exec(createTableSQL); } }); transaction(); return this; } catch (error) { console.error('Table initialization failed:', error); throw error; } } /** * Drops a table if it exists * @param tableName Name of the table to drop * @returns this instance for chaining */ dropTable(tableName) { try { this.db.exec(`DROP TABLE IF EXISTS ${tableName}`); return this; } catch (error) { console.error(`Error dropping table ${tableName}:`, error); throw error; } } /** * Clears all data from a table without dropping the table structure * @param tableName Name of the table to clear * @returns this instance for chaining */ clearTable(tableName) { try { this.db.exec(`DELETE FROM ${tableName}`); return this; } catch (error) { console.error(`Error clearing table ${tableName}:`, error); throw error; } } } exports.DbInitializer = DbInitializer; //# sourceMappingURL=DbInitializer.js.map