// @ts-check import chokidar from 'chokidar'; import Database from 'better-sqlite3'; import { SafeExit } from '../class/SafeExit.mjs'; import { Paths } from '../class/Paths.mjs'; import { TrySync } from '../function/TrySync.mjs'; import { exportDatabaseToSqlMigration } from './exportDatabaseToSqlMigration.mjs'; import { LazyFactory } from '../function/LazyFactory.mjs'; import { QChannel } from '../class/QChannel.mjs'; import { Timeout } from '../function/Timeout.mjs'; /** * @typedef {import("../typehints/VivthCleanup.mjs").VivthCleanup} VivthCleanup * @typedef {import("chokidar").FSWatcher} FSWatcher */ /** * @description * - helper to convert already working `sqlite3` db file into sql migration file; * - this class assume `Paths` and `SafeExit` already instantiated; * @implements { VivthCleanup } */ export class SQLite3AutoMigration { /** * @description * - cleanup method; * - can be manually called to cleanup resources that are being used by this `instance`; * - for row migration, `id` must be used as primary key; */ vivthCleanup = async () => { this.#connection?.close(); this.#watcher?.removeAllListeners(); this.#watcher?.close(); }; /** * @type {FSWatcher | undefined} */ #watcher; /** * @type {string} */ #sqlite3Path; /** * @type {Database.Database | null} */ #connection = null; #reConnect = () => { if (this.#connection && typeof this.#connection.close === 'function') { const [, err] = TrySync(() => { this.#connection?.close(); }); if (err) { console.error('Error closing existing SQLite connection:', err); } } this.#connection = new Database(this.#sqlite3Path); }; static #q = LazyFactory(() => new QChannel('SQLite3AutoMigration:#q')); /** * @description * - This constructor provides extremely low abstraction with this filter; * - You have to filter out from full `rawSQLMigrationResult`; * * @param {string} sqlite3Path * - The file path to the SQLite database, relative to project root; * @param {(filter:{tableName:string, columnName:string, value:any})=>("table" | "column" | "row")[]} migrationFilter * - return `column`: `true` -> will include the `column`(as long as returning `row` also `true`); * - return `column`: `false` -> will NOT include the `column`; * - return `row`: `true` -> will include the `row`; * - return `row`: `false` -> will NOT include the `row`; * @param {number} [debounceMS] * - default: `200`; * @example * import { Paths } from 'vivth/neutral'; * import { SQLite3AutoMigration, SafeExit } from 'vivth/node'; * new Paths({ * root: process.env.INIT_CWD ?? process.cwd(), * }); * new SafeExit('SIGTERM', 'SIGINT'); * new SQLite3AutoMigration('/initial_design.db', ({tableName, columnName, value}) => { * // simple logical to pick which data which to migrate and to not to; * switch (tableName) { * case 'tbl_do_not_insert_table_to_migration': * return []; * default: * switch (columnName) { * case 'col_do_not_insert_column_to_migration': * return ['table', 'row']; * default: * switch (value) { * case 'val_do_not_insert_row_to_migration': * return []; * default: * return ['table', 'row', 'column']; * } * } * break; * } * }, 300) */ constructor(sqlite3Path, migrationFilter, debounceMS = 200) { this.#sqlite3Path = Paths.diskAbsolute(sqlite3Path); this.#reConnect(); const watcher = (this.#watcher = chokidar.watch(this.#sqlite3Path)); SafeExit.instance?.addCallback(this.vivthCleanup); const migrationPath = Paths.diskAbsolute('sql/Migration.sql'); watcher.addListener('all', (event, path, stats) => { path = Paths.normalize(path); SQLite3AutoMigration.#q.callback(path, async ({ isLastOnQ }) => { if (!isLastOnQ()) { return; } await Timeout(debounceMS); switch (event) { case 'add': case 'change': if (!stats || !stats.isFile()) { this.#connection?.close(); break; } if (!this.#connection) { this.#reConnect(); } /** * @type {Database.Database} */ // @ts-expect-error const connection = this.#connection; if (!connection) { return; } const [, errorMigrating] = await exportDatabaseToSqlMigration( connection, migrationPath, migrationFilter, ); if (errorMigrating) { this.#connection?.close(); break; } break; default: this.#connection?.close(); break; } }); }); } }