PawSQLite-Node-Adapter/cjs/pawsqlite-node-adapter.js

379 lines
12 KiB
JavaScript
Raw Normal View History

2021-03-02 23:16:52 -08:00
/******/ (() => { // webpackBootstrap
/******/ "use strict";
/******/ var __webpack_modules__ = ({
2021-03-04 21:57:37 -08:00
/***/ "./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 */ });
2021-03-05 12:02:25 -08:00
/* 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 _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./query_wrapper.mjs */ "./src/query_wrapper.mjs");
2021-03-04 21:57:37 -08:00
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
2021-03-05 12:02:25 -08:00
2021-03-04 21:57:37 -08:00
class DatabaseWrapper {
2021-03-05 12:02:25 -08:00
constructor(dbName) {
this.dbName = dbName;
2021-03-04 21:57:37 -08:00
}
2021-03-05 12:02:25 -08:00
open() {
2021-03-04 21:57:37 -08:00
return new Promise((resolve, reject) => {
2021-03-05 12:02:25 -08:00
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();
2021-03-04 21:57:37 -08:00
}
2021-03-05 12:02:25 -08:00
);
2021-03-04 21:57:37 -08:00
});
}
2021-03-05 12:02:25 -08:00
close() {
2021-03-04 21:57:37 -08:00
return new Promise((resolve, reject) => {
2021-03-05 12:02:25 -08:00
this.db.close((err) => {
2021-03-04 21:57:37 -08:00
if (err) {
2021-03-05 12:02:25 -08:00
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err));
2021-03-04 21:57:37 -08:00
return;
}
2021-03-05 12:02:25 -08:00
resolve();
2021-03-04 21:57:37 -08:00
});
});
}
2021-03-05 12:02:25 -08:00
sql(sql, ...args) {
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.QueryWrapper(sql, ...args);
return query.execute(this.db);
2021-03-04 21:57:37 -08:00
}
2021-03-05 12:02:25 -08:00
2021-03-04 21:57:37 -08:00
}
/***/ }),
2021-03-02 23:16:52 -08:00
/***/ "./src/log.mjs":
/*!*********************!*\
!*** ./src/log.mjs ***!
\*********************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "log": () => (/* binding */ log),
/* harmony export */ "enableDebug": () => (/* binding */ enableDebug)
/* harmony export */ });
let DEBUG = false;
function log(...args) {
if (DEBUG) {
console.log(...args);
}
}
function enableDebug(active) {
DEBUG = !!active;
2021-03-14 21:46:25 -07:00
log("PawSQLite-Node-Adapter: debugging " + (
2021-03-02 23:16:52 -08:00
DEBUG ? "enabled" : "disabled")
);
}
/***/ }),
/***/ "./src/node_adapter_error.mjs":
/*!************************************!*\
!*** ./src/node_adapter_error.mjs ***!
\************************************/
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */ "NodeAdapterError": () => (/* binding */ NodeAdapterError)
/* harmony export */ });
class NodeAdapterError extends Error {
static from(e) {
let message = "";
if (e && e.message) {
message = e.message;
}
return new NodeAdapterError(message);
}
}
2021-03-04 21:57:37 -08:00
/***/ }),
/***/ "./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 */ });
2021-03-05 12:02:25 -08:00
/* 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");
2021-03-04 21:57:37 -08:00
class QueryWrapper {
2021-03-05 12:02:25 -08:00
constructor(sql, ...args) {
2021-03-04 21:57:37 -08:00
this.sql = sql.trim();
2021-03-05 12:02:25 -08:00
this.args = args;
2021-03-04 21:57:37 -08:00
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
}
isAnyOf(...operations) {
for (const op of operations) {
if(op.toUpperCase() === this.operation) {
return true;
}
}
return false;
}
2021-03-05 12:02:25 -08:00
execute(db) {
if (this.isAnyOf("INSERT", "UPDATE", "DELETE")) {
return this.executeRun(db);
2021-03-16 09:59:06 -07:00
} else if (this.isAnyOf("PRAGMA")) {
return this.executeGet(db);
2021-03-05 12:02:25 -08:00
} else {
return this.executeAll(db);
}
}
2021-03-16 09:59:06 -07:00
executeGet(db) {
return new Promise((resolve, reject) => {
const self = this;
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
db.get(this.sql, ...this.args, function (err, row) {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
const result = {};
if (row) {
result.rows = [row];
}
resolve(_response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.ResponseWrapper.success(result));
});
});
}
2021-03-05 12:02:25 -08:00
executeRun(db) {
return new Promise((resolve, reject) => {
2021-03-05 13:30:56 -08:00
const self = this;
2021-03-05 12:02:25 -08:00
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(this.sql);
2021-03-16 09:59:06 -07:00
db.get(this.sql, ...this.args, function (err) {
2021-03-05 12:02:25 -08:00
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError.from(err));
return;
}
const result = {};
2021-03-05 13:30:56 -08:00
if (self.isAnyOf("INSERT")) {
2021-03-05 12:02:25 -08:00
result.insertId = this.lastID;
}
2021-03-05 13:30:56 -08:00
if (self.isAnyOf("UPDATE", "DELETE")) {
2021-03-05 12:02:25 -08:00
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}));
});
});
}
2021-03-04 21:57:37 -08:00
}
/***/ }),
/***/ "./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");;
2021-03-02 23:16:52 -08:00
/***/ })
/******/ });
/************************************************************************/
/******/ // The module cache
/******/ var __webpack_module_cache__ = {};
/******/
/******/ // The require function
/******/ function __webpack_require__(moduleId) {
/******/ // Check if module is in cache
/******/ if(__webpack_module_cache__[moduleId]) {
/******/ return __webpack_module_cache__[moduleId].exports;
/******/ }
/******/ // Create a new module (and put it into the cache)
/******/ var module = __webpack_module_cache__[moduleId] = {
/******/ // no module.id needed
/******/ // no module.loaded needed
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
/******/
/******/ // Return the exports of the module
/******/ return module.exports;
/******/ }
/******/
/************************************************************************/
/******/ /* webpack/runtime/define property getters */
/******/ (() => {
/******/ // define getter functions for harmony exports
/******/ __webpack_require__.d = (exports, definition) => {
/******/ for(var key in definition) {
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
/******/ }
/******/ }
/******/ };
/******/ })();
/******/
/******/ /* webpack/runtime/hasOwnProperty shorthand */
/******/ (() => {
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
/******/ })();
/******/
/******/ /* webpack/runtime/make namespace object */
/******/ (() => {
/******/ // define __esModule on exports
/******/ __webpack_require__.r = (exports) => {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/ })();
/******/
/************************************************************************/
var __webpack_exports__ = {};
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
(() => {
/*!******************************!*\
!*** ./src/node_adapter.mjs ***!
\******************************/
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
2021-03-14 21:46:25 -07:00
/* harmony export */ "PawSQLiteNodeAdapter": () => (/* binding */ PawSQLiteNodeAdapter)
2021-03-02 23:16:52 -08:00
/* harmony export */ });
2021-03-05 12:02:25 -08:00
/* 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 _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/database_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");
2021-03-04 21:57:37 -08:00
2021-03-02 23:16:52 -08:00
const databases = new Map();
2021-03-14 21:46:25 -07:00
const PawSQLiteNodeAdapter = {
2021-03-02 23:16:52 -08:00
name: "PawSQLiteNodeAdapter",
open: async (dbName) => {
2021-03-04 21:57:37 -08:00
if (!databases.has(dbName)) {
2021-03-05 12:02:25 -08:00
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName);
await db.open();
2021-03-04 21:57:37 -08:00
databases.set(dbName, db);
}
2021-03-02 23:16:52 -08:00
2021-03-05 12:02:25 -08:00
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
2021-03-02 23:16:52 -08:00
},
close: async (dbName) => {
2021-03-05 12:02:25 -08:00
const db = getDatabase(dbName);
2021-03-04 21:57:37 -08:00
await db.close();
databases.delete(dbName);
2021-03-05 12:02:25 -08:00
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
2021-03-02 23:16:52 -08:00
},
sql: async (dbName, sql, ...args) => {
2021-03-05 12:02:25 -08:00
const db = getDatabase(dbName);
2021-03-04 21:57:37 -08:00
const result = await db.sql(sql, ...args);
2021-03-02 23:16:52 -08:00
2021-03-05 12:02:25 -08:00
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result);
2021-03-02 23:16:52 -08:00
},
delete: async (dbName) => {
2021-03-05 12:02:25 -08:00
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Delete not implemented");
2021-03-02 23:16:52 -08:00
},
2021-03-05 12:02:25 -08:00
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_3__.enableDebug
2021-03-14 21:46:25 -07:00
};
2021-03-02 23:16:52 -08:00
2021-03-05 12:02:25 -08:00
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;
}
2021-03-02 23:16:52 -08:00
})();
2021-03-14 21:46:25 -07:00
module.exports = __webpack_exports__.PawSQLiteNodeAdapter;
2021-03-02 23:16:52 -08:00
/******/ })()
;
//# sourceMappingURL=pawsqlite-node-adapter.js.map