diff --git a/src/database_wrapper.mjs b/src/database_wrapper.mjs index 6bb39ad..caa3c74 100644 --- a/src/database_wrapper.mjs +++ b/src/database_wrapper.mjs @@ -1,3 +1,5 @@ +import * as sqlite3 from "sqlite3"; + import { NodeAdapterError } from "./node_adapter_error.mjs"; import { QueryWrapper } from "./query_wrapper.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs"; @@ -5,8 +7,25 @@ import { enableDebug, log } from "./log.mjs"; export class DatabaseWrapper { - constructor(db) { - this.db = db; + constructor(dbName) { + this.dbName = dbName; + } + + open() { + return new Promise((resolve, reject) => { + const db = new sqlite3.Database( + this.dbName, + sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, + (err) => { + if (err) { + reject(NodeAdapterError.from(err)); + return; + } + this.db = db; + resolve(); + } + ); + }); } close() { diff --git a/src/node_adapter.mjs b/src/node_adapter.mjs index 72ef41e..b863561 100644 --- a/src/node_adapter.mjs +++ b/src/node_adapter.mjs @@ -1,5 +1,3 @@ -import * as sqlite3 from "sqlite3"; - import { NodeAdapterError } from "./node_adapter_error.mjs"; import { DatabaseWrapper } from "./database_wrapper.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs"; @@ -11,9 +9,8 @@ export default { name: "PawSQLiteNodeAdapter", open: async (dbName) => { if (!databases.has(dbName)) { - const db = new DatabaseWrapper( - new sqlite3.Database(dbName) - ); + const db = new DatabaseWrapper(dbName); + await db.open(); databases.set(dbName, db); }