PawSQLite-Node-Adapter/cjs/pawsqlite-node-adapter.js
2021-03-14 21:46:25 -07:00

355 lines
11 KiB
JavaScript

/******/ (() => { // webpackBootstrap
/******/ "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 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");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
class DatabaseWrapper {
constructor(dbName) {
this.dbName = dbName;
}
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() {
return new Promise((resolve, reject) => {
this.db.close((err) => {
if (err) {
reject(_node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_1__.NodeAdapterError.from(err));
return;
}
resolve();
});
});
}
sql(sql, ...args) {
const query = new _query_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.QueryWrapper(sql, ...args);
return query.execute(this.db);
}
}
/***/ }),
/***/ "./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;
log("PawSQLite-Node-Adapter: debugging " + (
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);
}
}
/***/ }),
/***/ "./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 */ });
/* 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 {
constructor(sql, ...args) {
this.sql = sql.trim();
this.args = args;
this.operation = sql.replace(/[^a-z].*/i, "").toUpperCase();
}
isAnyOf(...operations) {
for (const op of operations) {
if(op.toUpperCase() === this.operation) {
return true;
}
}
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) => {
const self = this;
(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 (self.isAnyOf("INSERT")) {
result.insertId = this.lastID;
}
if (self.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}));
});
});
}
}
/***/ }),
/***/ "./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");;
/***/ })
/******/ });
/************************************************************************/
/******/ // 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__, {
/* harmony export */ "PawSQLiteNodeAdapter": () => (/* binding */ PawSQLiteNodeAdapter)
/* 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 _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");
const databases = new Map();
const PawSQLiteNodeAdapter = {
name: "PawSQLiteNodeAdapter",
open: async (dbName) => {
if (!databases.has(dbName)) {
const db = new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName);
await db.open();
databases.set(dbName, db);
}
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
},
close: async (dbName) => {
const db = getDatabase(dbName);
await db.close();
databases.delete(dbName);
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success();
},
sql: async (dbName, sql, ...args) => {
const db = getDatabase(dbName);
const result = await db.sql(sql, ...args);
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result);
},
delete: async (dbName) => {
throw new _node_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.NodeAdapterError("Delete not implemented");
},
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_3__.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__.PawSQLiteNodeAdapter;
/******/ })()
;
//# sourceMappingURL=pawsqlite-node-adapter.js.map