Compare commits

..

No commits in common. "2b495367a22d4e582cd4aca17c1d9822497fb254" and "75f0921cce9838361c240423f3f74d840eca8f5f" have entirely different histories.

5 changed files with 143 additions and 196 deletions

View File

@ -12,9 +12,9 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper) /* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper)
/* harmony export */ }); /* harmony export */ });
/* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3"); /* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs"); /* harmony import */ var _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs");
/* harmony import */ var _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs"); /* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs"); /* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
@ -22,34 +22,16 @@ __webpack_require__.r(__webpack_exports__);
class DatabaseWrapper { class DatabaseWrapper {
constructor(dbName) { constructor(db) {
this.dbName = dbName; this.db = db;
}
open() {
return new Promise((resolve, reject) => {
const db = new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(
this.dbName,
sqlite3__WEBPACK_IMPORTED_MODULE_0__.OPEN_READWRITE | sqlite3__WEBPACK_IMPORTED_MODULE_0__.OPEN_CREATE,
(err) => {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err));
return;
}
this.db = db;
resolve();
}
);
});
} }
close() { close() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
this.db.close((err) => { this.db.close((err) => {
if (err) { if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err)); reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return; return;
} }
resolve(); resolve();
@ -58,10 +40,51 @@ class DatabaseWrapper {
} }
sql(sql, ...args) { sql(sql, ...args) {
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.QueryWrapper(sql, ...args); const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.QueryWrapper(sql);
return query.execute(this.db);
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
return this.executeRun(query, ...args);
} else {
return this.executeAll(query, ...args);
}
} }
executeRun(query, ...args) {
return new Promise((resolve, reject) => {
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_3__.log)(query.sql);
this.db.run(query.sql, ...args, function (err) {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
const result = {};
if (query.isAnyOf("INSERT")) {
result.insertId = this.lastID;
}
if (query.isAnyOf("UPDATE", "DELETE")) {
result.rowsAffected = this.changes;
}
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result));
});
});
}
executeAll(query, ...args) {
return new Promise((resolve, reject) => {
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_3__.log)(query.sql);
this.db.all(query.sql, ...args, function (err, rows) {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success({rows}));
});
});
}
} }
/***/ }), /***/ }),
@ -126,18 +149,9 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "QueryWrapper": () => (/* binding */ QueryWrapper) /* harmony export */ "QueryWrapper": () => (/* binding */ QueryWrapper)
/* harmony export */ }); /* harmony export */ });
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
class QueryWrapper { class QueryWrapper {
constructor(sql, ...args) { constructor(sql) {
this.sql = sql.trim(); this.sql = sql.trim();
this.args = args;
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase(); this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
} }
@ -149,51 +163,6 @@ class QueryWrapper {
} }
return false; return false;
} }
execute(db) {
if (this.isAnyOf("INSERT", "UPDATE", "DELETE")) {
return this.executeRun(db);
} else {
return this.executeAll(db);
}
}
executeRun(db) {
return new Promise((resolve, reject) => {
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
db.run(this.sql, ...this.args, function (err) {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
const result = {};
if (this.isAnyOf("INSERT")) {
result.insertId = this.lastID;
}
if (this.isAnyOf("UPDATE", "DELETE")) {
result.rowsAffected = this.changes;
}
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.ResponseWrapper.success(result));
});
});
}
executeAll(db) {
return new Promise((resolve, reject) => {
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
db.all(this.sql, ...this.args, function (err, rows) {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.ResponseWrapper.success({rows}));
});
});
}
} }
/***/ }), /***/ }),
@ -292,10 +261,13 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, { /* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__) /* harmony export */ "default": () => (__WEBPACK_DEFAULT_EXPORT__)
/* harmony export */ }); /* harmony export */ });
/* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs"); /* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3");
/* harmony import */ var _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_wrapper.mjs"); /* harmony import */ var _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./node_adapter_error.mjs */ "./src/node_adapter_error.mjs");
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs"); /* harmony import */ var _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_wrapper.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs"); /* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/response_wrapper.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
@ -307,44 +279,45 @@ const databases = new Map();
name: "PawSQLiteNodeAdapter", name: "PawSQLiteNodeAdapter",
open: async (dbName) => { open: async (dbName) => {
if (!databases.has(dbName)) { if (!databases.has(dbName)) {
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName); const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.DatabaseWrapper(
await db.open(); new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(dbName)
);
databases.set(dbName, db); databases.set(dbName, db);
} }
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(); return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
}, },
close: async (dbName) => { close: async (dbName) => {
const db = getDatabase(dbName); const db = databases.get(dbName);
await db.close(); if (!db) {
databases.delete(dbName); throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
}
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(); await db.close();
databases.delete(dbName);
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
}, },
sql: async (dbName, sql, ...args) => { sql: async (dbName, sql, ...args) => {
const db = getDatabase(dbName); const db = databases.get(dbName);
if (!db) {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
}
const result = await db.sql(sql, ...args); const result = await db.sql(sql, ...args);
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result); return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success(result);
}, },
delete: async (dbName) => { delete: async (dbName) => {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Delete not implemented"); throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Delete not implemented");
}, },
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_3__.enableDebug debug: _log_mjs__WEBPACK_IMPORTED_MODULE_4__.enableDebug
}); });
function getDatabase(dbName) {
const db = databases.get(dbName);
if (!db) {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Database not open");
}
return db;
}
})(); })();
module.exports = __webpack_exports__.default; module.exports = __webpack_exports__.default;

File diff suppressed because one or more lines are too long

View File

@ -1,30 +1,12 @@
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 { log } from "./log.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs";
import { enableDebug, log } from "./log.mjs";
export class DatabaseWrapper { export class DatabaseWrapper {
constructor(dbName) { constructor(db) {
this.dbName = dbName; this.db = db;
}
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() {
@ -40,8 +22,49 @@ export class DatabaseWrapper {
} }
sql(sql, ...args) { sql(sql, ...args) {
const query = new QueryWrapper(sql, ...args); const query = new QueryWrapper(sql);
return query.execute(this.db);
if (query.isAnyOf("INSERT", "UPDATE", "DELETE")) {
return this.executeRun(query, ...args);
} else {
return this.executeAll(query, ...args);
}
} }
executeRun(query, ...args) {
return new Promise((resolve, reject) => {
log(query.sql);
this.db.run(query.sql, ...args, function (err) {
if (err) {
reject(NodeAdapterError.from(err));
return;
}
const result = {};
if (query.isAnyOf("INSERT")) {
result.insertId = this.lastID;
}
if (query.isAnyOf("UPDATE", "DELETE")) {
result.rowsAffected = this.changes;
}
resolve(ResponseWrapper.success(result));
});
});
}
executeAll(query, ...args) {
return new Promise((resolve, reject) => {
log(query.sql);
this.db.all(query.sql, ...args, function (err, rows) {
if (err) {
reject(NodeAdapterError.from(err));
return;
}
resolve(ResponseWrapper.success({rows}));
});
});
}
} }

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 { DatabaseWrapper } from "./database_wrapper.mjs"; import { DatabaseWrapper } from "./database_wrapper.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs"; import { ResponseWrapper } from "./response_wrapper.mjs";
@ -9,8 +11,9 @@ export default {
name: "PawSQLiteNodeAdapter", name: "PawSQLiteNodeAdapter",
open: async (dbName) => { open: async (dbName) => {
if (!databases.has(dbName)) { if (!databases.has(dbName)) {
const db = new DatabaseWrapper(dbName); const db = new DatabaseWrapper(
await db.open(); new sqlite3.Database(dbName)
);
databases.set(dbName, db); databases.set(dbName, db);
} }
@ -18,15 +21,23 @@ export default {
}, },
close: async (dbName) => { close: async (dbName) => {
const db = getDatabase(dbName); const db = databases.get(dbName);
await db.close(); if (!db) {
databases.delete(dbName); throw new NodeAdapterError("Database not open");
}
await db.close();
databases.delete(dbName);
return ResponseWrapper.success(); return ResponseWrapper.success();
}, },
sql: async (dbName, sql, ...args) => { sql: async (dbName, sql, ...args) => {
const db = getDatabase(dbName); const db = databases.get(dbName);
if (!db) {
throw new NodeAdapterError("Database not open");
}
const result = await db.sql(sql, ...args); const result = await db.sql(sql, ...args);
return ResponseWrapper.success(result); return ResponseWrapper.success(result);
@ -38,12 +49,3 @@ export default {
debug: enableDebug debug: enableDebug
}; };
function getDatabase(dbName) {
const db = databases.get(dbName);
if (!db) {
throw new NodeAdapterError("Database not open");
}
return db;
}

View File

@ -1,12 +1,6 @@
import { NodeAdapterError } from "./node_adapter_error.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs";
import { log } from "./log.mjs";
export class QueryWrapper { export class QueryWrapper {
constructor(sql, ...args) { constructor(sql) {
this.sql = sql.trim(); this.sql = sql.trim();
this.args = args;
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase(); this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
} }
@ -18,49 +12,4 @@ export class QueryWrapper {
} }
return false; return false;
} }
execute(db) {
if (this.isAnyOf("INSERT", "UPDATE", "DELETE")) {
return this.executeRun(db);
} else {
return this.executeAll(db);
}
}
executeRun(db) {
return new Promise((resolve, reject) => {
log(this.sql);
db.run(this.sql, ...this.args, function (err) {
if (err) {
reject(NodeAdapterError.from(err));
return;
}
const result = {};
if (this.isAnyOf("INSERT")) {
result.insertId = this.lastID;
}
if (this.isAnyOf("UPDATE", "DELETE")) {
result.rowsAffected = this.changes;
}
resolve(ResponseWrapper.success(result));
});
});
}
executeAll(db) {
return new Promise((resolve, reject) => {
log(this.sql);
db.all(this.sql, ...this.args, function (err, rows) {
if (err) {
reject(NodeAdapterError.from(err));
return;
}
resolve(ResponseWrapper.success({rows}));
});
});
}
} }