UNPKG

@energyweb/node-red-contrib-green-proof-worker

Version:

## Peer dependencies

58 lines (57 loc) 2.27 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SqliteConfig = void 0; const tslib_1 = require("tslib"); const better_sqlite3_1 = tslib_1.__importDefault(require("better-sqlite3")); const kysely_1 = require("kysely"); const z = tslib_1.__importStar(require("zod")); const node_1 = require("../node"); const nodes_config_env_1 = require("../nodes-config-env"); const migration_provider_1 = require("./migration-provider"); const InputMessage = z.object({}); const Config = z.object({ dbLocation: z.string(), }); const SqliteConfig = (api) => class SqliteConfig extends node_1.Node { constructor(config) { super(api, config, InputMessage); const { dbLocation } = Config.parse({ dbLocation: config.dbLocation || this.getNodeEnvConfig().EWX_SQLITE_PATH || nodes_config_env_1.nodesGlobalEnvConfig.SQLITE_CONFIG_DB_LOCATION, }); this.database = new Promise((resolve, reject) => { const db = new kysely_1.Kysely({ dialect: new kysely_1.SqliteDialect({ database: new better_sqlite3_1.default(dbLocation), }), }); const migrator = new kysely_1.Migrator({ db, provider: new migration_provider_1.LocalMigrationProvider(), }); void migrator.migrateToLatest().then(({ error, results }) => { results?.forEach((it) => { if (it.status === 'Success') { this.api.log(`migration "${it.migrationName}" was executed successfully`); } else if (it.status === 'Error') { this.api.error(`failed to execute migration "${it.migrationName}"`); } }); if (error) { this.api.error('failed to migrate'); this.api.error(error.message); reject(error); } else { resolve(db); } }); }); } onInput() { } async onDestroy() { const db = await this.database; await db.destroy(); } }; exports.SqliteConfig = SqliteConfig;