Added core functionality

This commit is contained in:
Ben Ashton 2021-03-04 21:57:37 -08:00
parent 380cf17d49
commit 308c1f3709
7 changed files with 299 additions and 36 deletions

View File

@ -2,6 +2,93 @@
/******/ "use strict";
/******/ var __webpack_modules__ = ({
/***/ "./src/database_wrapper.mjs":
/*!**********************************!*\
!*** ./src/database_wrapper.mjs ***!
\**********************************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "DatabaseWrapper": () => (/* binding */ DatabaseWrapper)
/* 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 _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __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");
class DatabaseWrapper {
constructor(db) {
this.db = db;
}
close() {
return new Promise((resolve, reject) => {
this.db.close((err) => {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
resolve();
});
});
}
sql(sql, ...args) {
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.QueryWrapper(sql);
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}));
});
});
}
}
/***/ }),
/***/ "./src/log.mjs":
/*!*********************!*\
!*** ./src/log.mjs ***!
@ -50,6 +137,63 @@ class NodeAdapterError extends Error {
}
}
/***/ }),
/***/ "./src/query_wrapper.mjs":
/*!*******************************!*\
!*** ./src/query_wrapper.mjs ***!
\*******************************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "QueryWrapper": () => (/* binding */ QueryWrapper)
/* harmony export */ });
class QueryWrapper {
constructor(sql) {
this.sql = sql.trim();
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
}
isAnyOf(...operations) {
for (const op of operations) {
if(op.toUpperCase() === this.operation) {
return true;
}
}
return false;
}
}
/***/ }),
/***/ "./src/response_wrapper.mjs":
/*!**********************************!*\
!*** ./src/response_wrapper.mjs ***!
\**********************************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "ResponseWrapper": () => (/* binding */ ResponseWrapper)
/* harmony export */ });
const ResponseWrapper = {
success: (obj = {}) => ({
success: true,
...obj
})
};
/***/ }),
/***/ "sqlite3":
/*!**************************!*\
!*** external "sqlite3" ***!
\**************************/
/***/ ((module) => {
module.exports = require("sqlite3");;
/***/ })
/******/ });
@ -117,8 +261,15 @@ __webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "default": () => (__WEBPACK_DEFAULT_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 _log_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
/* harmony import */ var sqlite3__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! sqlite3 */ "sqlite3");
/* 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 _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_wrapper.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");
@ -127,37 +278,44 @@ const databases = new Map();
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({
name: "PawSQLiteNodeAdapter",
open: async (dbName) => {
// const version = "1.0";
if (!databases.has(dbName)) {
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.DatabaseWrapper(
new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(dbName)
);
databases.set(dbName, db);
}
// if (!databases.has(dbName)) {
// databases.set(dbName, new DatabaseWrapper(dbName, version));
// }
// return ResponseWrapper.success({ version });
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
},
close: async (dbName) => {
// databases.delete(dbName);
// return ResponseWrapper.success();
const db = databases.get(dbName);
if (!db) {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
}
await db.close();
databases.delete(dbName);
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success();
},
sql: async (dbName, sql, ...args) => {
// log(sql);
const db = databases.get(dbName);
if (!db) {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
}
// 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 _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success(result);
},
delete: async (dbName) => {
// throw new NodeAdapterError("Delete not implemented");
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Delete not implemented");
},
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_1__.enableDebug
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_4__.enableDebug
});
})();

File diff suppressed because one or more lines are too long

70
src/database_wrapper.mjs Normal file
View File

@ -0,0 +1,70 @@
import { NodeAdapterError } from "./node_adapter_error.mjs";
import { QueryWrapper } from "./query_wrapper.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs";
import { enableDebug, log } from "./log.mjs";
export class DatabaseWrapper {
constructor(db) {
this.db = db;
}
close() {
return new Promise((resolve, reject) => {
this.db.close((err) => {
if (err) {
reject(NodeAdapterError.from(err));
return;
}
resolve();
});
});
}
sql(sql, ...args) {
const query = new QueryWrapper(sql);
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,4 +1,8 @@
import * as sqlite3 from "sqlite3";
import { NodeAdapterError } from "./node_adapter_error.mjs";
import { DatabaseWrapper } from "./database_wrapper.mjs";
import { ResponseWrapper } from "./response_wrapper.mjs";
import { enableDebug, log } from "./log.mjs";
const databases = new Map();
@ -6,34 +10,41 @@ const databases = new Map();
export default {
name: "PawSQLiteNodeAdapter",
open: async (dbName) => {
// const version = "1.0";
if (!databases.has(dbName)) {
const db = new DatabaseWrapper(
new sqlite3.Database(dbName)
);
databases.set(dbName, db);
}
// if (!databases.has(dbName)) {
// databases.set(dbName, new DatabaseWrapper(dbName, version));
// }
// return ResponseWrapper.success({ version });
return ResponseWrapper.success();
},
close: async (dbName) => {
// databases.delete(dbName);
// return ResponseWrapper.success();
const db = databases.get(dbName);
if (!db) {
throw new NodeAdapterError("Database not open");
}
await db.close();
databases.delete(dbName);
return ResponseWrapper.success();
},
sql: async (dbName, sql, ...args) => {
// log(sql);
const db = databases.get(dbName);
if (!db) {
throw new NodeAdapterError("Database not open");
}
// 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);
},
delete: async (dbName) => {
// throw new NodeAdapterError("Delete not implemented");
throw new NodeAdapterError("Delete not implemented");
},
debug: enableDebug

15
src/query_wrapper.mjs Normal file
View File

@ -0,0 +1,15 @@
export class QueryWrapper {
constructor(sql) {
this.sql = sql.trim();
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
}
isAnyOf(...operations) {
for (const op of operations) {
if(op.toUpperCase() === this.operation) {
return true;
}
}
return false;
}
}

6
src/response_wrapper.mjs Normal file
View File

@ -0,0 +1,6 @@
export const ResponseWrapper = {
success: (obj = {}) => ({
success: true,
...obj
})
};

View File

@ -6,6 +6,9 @@ var config = {
mode: 'development',
entry: __dirname + '/src/node_adapter.mjs',
devtool: 'source-map',
externals: {
sqlite3: 'sqlite3'
},
output: {
path: __dirname + '/lib',
filename: outputFile,