Compare commits

..

No commits in common. "308c1f3709a290d7208cefac25e5b700ad021232" and "d104a77bcf97a8054b7a18dfbec3a8630d68e448" have entirely different histories.

9 changed files with 58 additions and 1176 deletions

View File

@ -2,93 +2,6 @@
/******/ "use strict"; /******/ "use strict";
/******/ var __webpack_modules__ = ({ /******/ 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":
/*!*********************!*\ /*!*********************!*\
!*** ./src/log.mjs ***! !*** ./src/log.mjs ***!
@ -137,63 +50,6 @@ 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");;
/***/ }) /***/ })
/******/ }); /******/ });
@ -261,15 +117,8 @@ __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 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 _log_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./log.mjs */ "./src/log.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");
@ -278,44 +127,37 @@ const databases = new Map();
/* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({ /* harmony default export */ const __WEBPACK_DEFAULT_EXPORT__ = ({
name: "PawSQLiteNodeAdapter", name: "PawSQLiteNodeAdapter",
open: async (dbName) => { open: async (dbName) => {
if (!databases.has(dbName)) { // const version = "1.0";
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.DatabaseWrapper(
new sqlite3__WEBPACK_IMPORTED_MODULE_0__.Database(dbName)
);
databases.set(dbName, db);
}
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success(); // if (!databases.has(dbName)) {
// databases.set(dbName, new DatabaseWrapper(dbName, version));
// }
// return ResponseWrapper.success({ version });
}, },
close: async (dbName) => { close: async (dbName) => {
const db = databases.get(dbName); // databases.delete(dbName);
if (!db) { // return ResponseWrapper.success();
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) => { sql: async (dbName, sql, ...args) => {
const db = databases.get(dbName); // log(sql);
if (!db) {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Database not open");
}
const result = await db.sql(sql, ...args); // const db = databases.get(dbName);
// if (!db) {
// throw new NodeAdapterError("Database not open");
// }
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_3__.ResponseWrapper.success(result); // const result = await db.sql(sql, ...args);
// return ResponseWrapper.success(result);
}, },
delete: async (dbName) => { delete: async (dbName) => {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError("Delete not implemented"); // throw new NodeAdapterError("Delete not implemented");
}, },
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_4__.enableDebug debug: _log_mjs__WEBPACK_IMPORTED_MODULE_1__.enableDebug
}); });
})(); })();

File diff suppressed because one or more lines are too long

896
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -16,8 +16,5 @@
"devDependencies": { "devDependencies": {
"webpack": "5.x", "webpack": "5.x",
"webpack-cli": "4.x" "webpack-cli": "4.x"
},
"dependencies": {
"sqlite3": "^5.0.2"
} }
} }

View File

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

View File

@ -1,15 +0,0 @@
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;
}
}

View File

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

View File

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