import { TaskManager } from "./task_manager.mjs"; import { Transaction } from "./transaction.mjs"; import { query } from "./query.mjs"; export class Database { constructor(dbName, adapter) { this.dbName = dbName; this.adapter = adapter; this.version = null; this.path = null; this.taskManager = new TaskManager(); } async open() { let response = await this.adapter.open(this.dbName); if (response) { if (response.hasOwnProperty('version')) { this.version = response.version; } if (response.hasOwnProperty('path')) { this.path = response.path; } } // Allow chaining return this; } close() { return this.adapter.close(this.dbName); } transaction() { return new Transaction( this.dbName, this.adapter, this.taskManager.enqueue.bind(this.taskManager) ); } async autoTransaction(cb, inheritTx) { let tx = inheritTx || this.transaction(); let result; try { result = await cb(tx); } catch (e) { if (!inheritTx) { await tx.rollback(); } throw e; } if (!inheritTx) { await tx.commit(); } return result; } // Execute a single SQL statement async sql(sql, ...args) { const completeCb = await this.taskManager.enqueue(); let result; let error; try { result = await this.adapter.sql(this.dbName, ...query(sql, ...args)); } catch (e) { error = e; } completeCb(); if (error) { throw error; } return new Result(result); } }