Refactored database opening

This commit is contained in:
Ben Ashton 2021-03-05 11:49:36 -08:00
parent 75f0921cce
commit f55b00dbf5
2 changed files with 23 additions and 7 deletions

View File

@ -1,3 +1,5 @@
import * as sqlite3 from "sqlite3";
import { NodeAdapterError } from "./node_adapter_error.mjs"; import { NodeAdapterError } from "./node_adapter_error.mjs";
import { QueryWrapper } from "./query_wrapper.mjs"; import { QueryWrapper } from "./query_wrapper.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs";
@ -5,8 +7,25 @@ import { enableDebug, log } from "./log.mjs";
export class DatabaseWrapper { export class DatabaseWrapper {
constructor(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; this.db = db;
resolve();
}
);
});
} }
close() { close() {

View File

@ -1,5 +1,3 @@
import * as sqlite3 from "sqlite3";
import { NodeAdapterError } from "./node_adapter_error.mjs"; import { NodeAdapterError } from "./node_adapter_error.mjs";
import { DatabaseWrapper } from "./database_wrapper.mjs"; import { DatabaseWrapper } from "./database_wrapper.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs";
@ -11,9 +9,8 @@ export default {
name: "PawSQLiteNodeAdapter", name: "PawSQLiteNodeAdapter",
open: async (dbName) => { open: async (dbName) => {
if (!databases.has(dbName)) { if (!databases.has(dbName)) {
const db = new DatabaseWrapper( const db = new DatabaseWrapper(dbName);
new sqlite3.Database(dbName) await db.open();
);
databases.set(dbName, db); databases.set(dbName, db);
} }