Refactored database opening
This commit is contained in:
parent
75f0921cce
commit
f55b00dbf5
@ -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.db = db;
|
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() {
|
close() {
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user