Ben Ashton
3 years ago
commit
c26b014aed
13 changed files with 1795 additions and 0 deletions
@ -0,0 +1,444 @@ |
|||||||
|
/******/ (() => { // 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 _transaction_manager_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./transaction_manager.mjs */ "./src/transaction_manager.mjs"); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class DatabaseWrapper { |
||||||
|
constructor(dbName, version) { |
||||||
|
this.name = dbName; |
||||||
|
this.db = openDatabase(this.name, version, "", 5 * 1024 * 1024); |
||||||
|
this.transManager = new _transaction_manager_mjs__WEBPACK_IMPORTED_MODULE_0__.TransactionManager(this.db); |
||||||
|
} |
||||||
|
|
||||||
|
sql(sql, ...args) { |
||||||
|
const reg = /^\s*(BEGIN|END|COMMIT|ROLLBACK)(?:[^A-Z]|$)/i; |
||||||
|
const match = reg.exec(sql); |
||||||
|
if (match) { |
||||||
|
const statement = match[1].toUpperCase(); |
||||||
|
|
||||||
|
switch(statement) { |
||||||
|
case "BEGIN": |
||||||
|
return this.transManager.begin(); |
||||||
|
case "END": |
||||||
|
case "COMMIT": |
||||||
|
return this.transManager.commit(); |
||||||
|
case "ROLLBACK": |
||||||
|
return this.transManager.rollback(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
return this.transManager.sql(sql, args); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/***/ }), |
||||||
|
|
||||||
|
/***/ "./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-WebSQL-Adapter: debugging " + ( |
||||||
|
DEBUG ? "enabled" : "disabled") |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
/***/ }), |
||||||
|
|
||||||
|
/***/ "./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 |
||||||
|
}) |
||||||
|
}; |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/***/ }), |
||||||
|
|
||||||
|
/***/ "./src/result_mapper.mjs": |
||||||
|
/*!*******************************!*\ |
||||||
|
!*** ./src/result_mapper.mjs ***! |
||||||
|
\*******************************/ |
||||||
|
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { |
||||||
|
|
||||||
|
__webpack_require__.r(__webpack_exports__); |
||||||
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, { |
||||||
|
/* harmony export */ "mapResult": () => (/* binding */ mapResult) |
||||||
|
/* harmony export */ }); |
||||||
|
function mapResult(originalResult) { |
||||||
|
const newResult = {}; |
||||||
|
if (!originalResult) { |
||||||
|
return newResult; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
newResult.insertId = originalResult.insertId; |
||||||
|
} catch (e) {} |
||||||
|
|
||||||
|
newResult.rowsAffected = originalResult.rowsAffected; |
||||||
|
|
||||||
|
if ("rows" in originalResult) { |
||||||
|
newResult.rows = []; |
||||||
|
|
||||||
|
for (let i = 0; i < originalResult.rows.length; i++) { |
||||||
|
newResult.rows.push(originalResult.rows.item(i)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return newResult; |
||||||
|
} |
||||||
|
|
||||||
|
/***/ }), |
||||||
|
|
||||||
|
/***/ "./src/transaction_manager.mjs": |
||||||
|
/*!*************************************!*\ |
||||||
|
!*** ./src/transaction_manager.mjs ***! |
||||||
|
\*************************************/ |
||||||
|
/***/ ((__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => { |
||||||
|
|
||||||
|
__webpack_require__.r(__webpack_exports__); |
||||||
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, { |
||||||
|
/* harmony export */ "TransactionManager": () => (/* binding */ TransactionManager) |
||||||
|
/* harmony export */ }); |
||||||
|
/* harmony import */ var _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./websql_adapter_error.mjs */ "./src/websql_adapter_error.mjs"); |
||||||
|
/* harmony import */ var _result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./result_mapper.mjs */ "./src/result_mapper.mjs"); |
||||||
|
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs"); |
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Task { |
||||||
|
constructor(job, startsTransaction=false, endsTransaction=false) { |
||||||
|
this._job = job; |
||||||
|
|
||||||
|
if (startsTransaction && endsTransaction) { |
||||||
|
throw new Error("Task cannot start and end a transaction."); |
||||||
|
} |
||||||
|
|
||||||
|
this.startsTransaction = startsTransaction; |
||||||
|
this.endsTransaction = endsTransaction; |
||||||
|
|
||||||
|
this.result = new Promise ((resolve, reject) => { |
||||||
|
this._resolve = resolve; |
||||||
|
this._reject = reject; |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
run(tx) { |
||||||
|
return this._job(tx).then((result) => { |
||||||
|
this._resolve(result); |
||||||
|
return result; |
||||||
|
}, (e) => { |
||||||
|
this._reject(e); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
class TransactionManager { |
||||||
|
constructor(db) { |
||||||
|
this.db = db; |
||||||
|
|
||||||
|
this._txCount = 0; |
||||||
|
this._tasks = []; |
||||||
|
this._processing = false; |
||||||
|
} |
||||||
|
|
||||||
|
begin() { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
if (tx) { |
||||||
|
reject(new _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.WebSQLAdapterError("BEGIN called with an active " + |
||||||
|
"transaction. This should not happen")); |
||||||
|
return; |
||||||
|
} |
||||||
|
this.db.transaction((tx) => { |
||||||
|
resolve(tx); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}, true)).then(() => (0,_result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__.mapResult)()); |
||||||
|
} |
||||||
|
|
||||||
|
sql(sql, args = []) { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return this._executeSql(tx, sql, args); |
||||||
|
})); |
||||||
|
} |
||||||
|
|
||||||
|
commit() { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return Promise.resolve((0,_result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__.mapResult)()); |
||||||
|
}, false, true)); |
||||||
|
} |
||||||
|
|
||||||
|
rollback() { |
||||||
|
// Hack to manually cause rollback:
|
||||||
|
// Intentionally cause an error with rollbackOnError set to true
|
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return this._executeSql(tx, "", [], true).catch(() => (0,_result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__.mapResult)()); |
||||||
|
}, false, true)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
async _process() { |
||||||
|
if (this._processing) { |
||||||
|
return; |
||||||
|
} |
||||||
|
this._processing = true; |
||||||
|
|
||||||
|
let tx = null; |
||||||
|
let keepaliveCount = 0; |
||||||
|
|
||||||
|
while (true) { |
||||||
|
const tasks = this._tasks; |
||||||
|
this._tasks = []; |
||||||
|
|
||||||
|
|
||||||
|
if (tasks.length) { |
||||||
|
const promises = []; |
||||||
|
|
||||||
|
for (const task of tasks) { |
||||||
|
const promise = task.run(tx); |
||||||
|
|
||||||
|
if (task.startsTransaction) { |
||||||
|
tx = await promise; |
||||||
|
this._txCount++; |
||||||
|
} else { |
||||||
|
if (task.endsTransaction) { |
||||||
|
tx = null; |
||||||
|
keepaliveCount = 0; |
||||||
|
} |
||||||
|
|
||||||
|
promises.push(promise); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
await Promise.all(promises); |
||||||
|
} else { |
||||||
|
if (tx) { |
||||||
|
await this._nop(tx); |
||||||
|
keepaliveCount++; |
||||||
|
if (keepaliveCount % 5000 === 0) { |
||||||
|
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_2__.log)(`Transaction: ${ this._txCount } Keepalive: #${ keepaliveCount }`); |
||||||
|
} |
||||||
|
} else { |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
this._processing = false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
_addTask(task) { |
||||||
|
this._tasks.push(task); |
||||||
|
this._process(); |
||||||
|
return task.result; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
_executeSql(tx, sql, sqlArgs = [], rollbackOnError = false) { |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
if (!tx) { |
||||||
|
reject(new _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.WebSQLAdapterError("No transaction. This should not be " + |
||||||
|
" possible")); |
||||||
|
return; |
||||||
|
} |
||||||
|
tx.executeSql(sql, sqlArgs, (tx, result) => { |
||||||
|
resolve((0,_result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__.mapResult)(result)); |
||||||
|
}, (tx, e) => { |
||||||
|
reject(_websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.WebSQLAdapterError.from(e)); |
||||||
|
return rollbackOnError; |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
_nop(tx) { |
||||||
|
return this._executeSql(tx, "SELECT 1"); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/***/ }), |
||||||
|
|
||||||
|
/***/ "./src/websql_adapter_error.mjs": |
||||||
|
/*!**************************************!*\ |
||||||
|
!*** ./src/websql_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 */ "WebSQLAdapterError": () => (/* binding */ WebSQLAdapterError) |
||||||
|
/* harmony export */ }); |
||||||
|
class WebSQLAdapterError extends Error { |
||||||
|
static from(e) { |
||||||
|
let message = ""; |
||||||
|
if (e && e.message) { |
||||||
|
message = e.message; |
||||||
|
} |
||||||
|
return new WebSQLAdapterError(message); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/***/ }) |
||||||
|
|
||||||
|
/******/ }); |
||||||
|
/************************************************************************/ |
||||||
|
/******/ // 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/websql_adapter.mjs ***! |
||||||
|
\********************************/ |
||||||
|
__webpack_require__.r(__webpack_exports__); |
||||||
|
/* harmony export */ __webpack_require__.d(__webpack_exports__, { |
||||||
|
/* harmony export */ "WebSQLAdapter": () => (/* binding */ WebSQLAdapter) |
||||||
|
/* harmony export */ }); |
||||||
|
/* harmony import */ var _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./websql_adapter_error.mjs */ "./src/websql_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 WebSQLAdapter = { |
||||||
|
open: async (dbName) => { |
||||||
|
const version = "1.0"; |
||||||
|
|
||||||
|
if (!databases.has(dbName)) { |
||||||
|
databases.set(dbName, new _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__.DatabaseWrapper(dbName, version)); |
||||||
|
} |
||||||
|
|
||||||
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success({ version }); |
||||||
|
}, |
||||||
|
|
||||||
|
close: async (dbName) => { |
||||||
|
databases.delete(dbName); |
||||||
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(); |
||||||
|
}, |
||||||
|
|
||||||
|
sql: async (dbName, sql, ...args) => { |
||||||
|
(0,_log_mjs__WEBPACK_IMPORTED_MODULE_3__.log)(sql); |
||||||
|
|
||||||
|
const db = databases.get(dbName); |
||||||
|
if (!db) { |
||||||
|
throw new _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.WebSQLAdapterError("Database not open"); |
||||||
|
} |
||||||
|
|
||||||
|
const result = await db.sql(sql, ...args); |
||||||
|
return _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__.ResponseWrapper.success(result); |
||||||
|
}, |
||||||
|
|
||||||
|
delete: async (dbName) => { |
||||||
|
throw new _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__.WebSQLAdapterError("Delete not implemented"); |
||||||
|
}, |
||||||
|
|
||||||
|
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_3__.enableDebug |
||||||
|
}; |
||||||
|
|
||||||
|
})(); |
||||||
|
|
||||||
|
module.exports["pawsqlite-websql-adapter"] = __webpack_exports__.default; |
||||||
|
/******/ })() |
||||||
|
; |
||||||
|
//# sourceMappingURL=pawsqlite-websql-adapter.js.map
|
File diff suppressed because one or more lines are too long
@ -0,0 +1,20 @@ |
|||||||
|
{ |
||||||
|
"name": "pawsqlite-websql-adapter", |
||||||
|
"version": "1.0.0", |
||||||
|
"description": "A WebSQL Adapter for PawSQLite", |
||||||
|
"main": "./lib/pawsqlite-websql-adapter.js", |
||||||
|
"scripts": { |
||||||
|
"test": "echo \"Error: no test specified\" && exit 1", |
||||||
|
"build": "webpack" |
||||||
|
}, |
||||||
|
"repository": { |
||||||
|
"type": "git", |
||||||
|
"url": "https://git.n0m.org/n0m/PawSQLite-WebSQL-Adapter.git" |
||||||
|
}, |
||||||
|
"author": "Ben Ashton", |
||||||
|
"license": "MIT", |
||||||
|
"devDependencies": { |
||||||
|
"webpack": "5.x", |
||||||
|
"webpack-cli": "4.x" |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,30 @@ |
|||||||
|
import { TransactionManager } from "./transaction_manager.mjs"; |
||||||
|
|
||||||
|
|
||||||
|
export class DatabaseWrapper { |
||||||
|
constructor(dbName, version) { |
||||||
|
this.name = dbName; |
||||||
|
this.db = openDatabase(this.name, version, "", 5 * 1024 * 1024); |
||||||
|
this.transManager = new TransactionManager(this.db); |
||||||
|
} |
||||||
|
|
||||||
|
sql(sql, ...args) { |
||||||
|
const reg = /^\s*(BEGIN|END|COMMIT|ROLLBACK)(?:[^A-Z]|$)/i; |
||||||
|
const match = reg.exec(sql); |
||||||
|
if (match) { |
||||||
|
const statement = match[1].toUpperCase(); |
||||||
|
|
||||||
|
switch(statement) { |
||||||
|
case "BEGIN": |
||||||
|
return this.transManager.begin(); |
||||||
|
case "END": |
||||||
|
case "COMMIT": |
||||||
|
return this.transManager.commit(); |
||||||
|
case "ROLLBACK": |
||||||
|
return this.transManager.rollback(); |
||||||
|
} |
||||||
|
} else { |
||||||
|
return this.transManager.sql(sql, args); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,14 @@ |
|||||||
|
let DEBUG = false; |
||||||
|
|
||||||
|
export function log(...args) { |
||||||
|
if (DEBUG) { |
||||||
|
console.log(...args); |
||||||
|
}
|
||||||
|
} |
||||||
|
|
||||||
|
export function enableDebug(active) { |
||||||
|
DEBUG = !!active; |
||||||
|
log("PawSQLite-WebSQL-Adapter: debugging " + ( |
||||||
|
DEBUG ? "enabled" : "disabled") |
||||||
|
); |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
export const ResponseWrapper = { |
||||||
|
success: (obj = {}) => ({ |
||||||
|
success: true, |
||||||
|
...obj |
||||||
|
}) |
||||||
|
}; |
||||||
|
|
||||||
|
|
@ -0,0 +1,22 @@ |
|||||||
|
export function mapResult(originalResult) { |
||||||
|
const newResult = {}; |
||||||
|
if (!originalResult) { |
||||||
|
return newResult; |
||||||
|
} |
||||||
|
|
||||||
|
try { |
||||||
|
newResult.insertId = originalResult.insertId; |
||||||
|
} catch (e) {} |
||||||
|
|
||||||
|
newResult.rowsAffected = originalResult.rowsAffected; |
||||||
|
|
||||||
|
if ("rows" in originalResult) { |
||||||
|
newResult.rows = []; |
||||||
|
|
||||||
|
for (let i = 0; i < originalResult.rows.length; i++) { |
||||||
|
newResult.rows.push(originalResult.rows.item(i)); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
return newResult; |
||||||
|
} |
@ -0,0 +1,156 @@ |
|||||||
|
import { WebSQLAdapterError } from "./websql_adapter_error.mjs"; |
||||||
|
import { mapResult } from "./result_mapper.mjs"; |
||||||
|
import { log } from "./log.mjs"; |
||||||
|
|
||||||
|
|
||||||
|
class Task { |
||||||
|
constructor(job, startsTransaction=false, endsTransaction=false) { |
||||||
|
this._job = job; |
||||||
|
|
||||||
|
if (startsTransaction && endsTransaction) { |
||||||
|
throw new Error("Task cannot start and end a transaction."); |
||||||
|
} |
||||||
|
|
||||||
|
this.startsTransaction = startsTransaction; |
||||||
|
this.endsTransaction = endsTransaction; |
||||||
|
|
||||||
|
this.result = new Promise ((resolve, reject) => { |
||||||
|
this._resolve = resolve; |
||||||
|
this._reject = reject; |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
run(tx) { |
||||||
|
return this._job(tx).then((result) => { |
||||||
|
this._resolve(result); |
||||||
|
return result; |
||||||
|
}, (e) => { |
||||||
|
this._reject(e); |
||||||
|
}); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
export class TransactionManager { |
||||||
|
constructor(db) { |
||||||
|
this.db = db; |
||||||
|
|
||||||
|
this._txCount = 0; |
||||||
|
this._tasks = []; |
||||||
|
this._processing = false; |
||||||
|
} |
||||||
|
|
||||||
|
begin() { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
if (tx) { |
||||||
|
reject(new WebSQLAdapterError("BEGIN called with an active " + |
||||||
|
"transaction. This should not happen")); |
||||||
|
return; |
||||||
|
} |
||||||
|
this.db.transaction((tx) => { |
||||||
|
resolve(tx); |
||||||
|
}); |
||||||
|
}); |
||||||
|
}, true)).then(() => mapResult()); |
||||||
|
} |
||||||
|
|
||||||
|
sql(sql, args = []) { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return this._executeSql(tx, sql, args); |
||||||
|
})); |
||||||
|
} |
||||||
|
|
||||||
|
commit() { |
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return Promise.resolve(mapResult()); |
||||||
|
}, false, true)); |
||||||
|
} |
||||||
|
|
||||||
|
rollback() { |
||||||
|
// Hack to manually cause rollback:
|
||||||
|
// Intentionally cause an error with rollbackOnError set to true
|
||||||
|
return this._addTask(new Task((tx) => { |
||||||
|
return this._executeSql(tx, "", [], true).catch(() => mapResult()); |
||||||
|
}, false, true)); |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
async _process() { |
||||||
|
if (this._processing) { |
||||||
|
return; |
||||||
|
} |
||||||
|
this._processing = true; |
||||||
|
|
||||||
|
let tx = null; |
||||||
|
let keepaliveCount = 0; |
||||||
|
|
||||||
|
while (true) { |
||||||
|
const tasks = this._tasks; |
||||||
|
this._tasks = []; |
||||||
|
|
||||||
|
|
||||||
|
if (tasks.length) { |
||||||
|
const promises = []; |
||||||
|
|
||||||
|
for (const task of tasks) { |
||||||
|
const promise = task.run(tx); |
||||||
|
|
||||||
|
if (task.startsTransaction) { |
||||||
|
tx = await promise; |
||||||
|
this._txCount++; |
||||||
|
} else { |
||||||
|
if (task.endsTransaction) { |
||||||
|
tx = null; |
||||||
|
keepaliveCount = 0; |
||||||
|
} |
||||||
|
|
||||||
|
promises.push(promise); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
await Promise.all(promises); |
||||||
|
} else { |
||||||
|
if (tx) { |
||||||
|
await this._nop(tx); |
||||||
|
keepaliveCount++; |
||||||
|
if (keepaliveCount % 5000 === 0) { |
||||||
|
log(`Transaction: ${ this._txCount } Keepalive: #${ keepaliveCount }`); |
||||||
|
} |
||||||
|
} else { |
||||||
|
break; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
this._processing = false; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
_addTask(task) { |
||||||
|
this._tasks.push(task); |
||||||
|
this._process(); |
||||||
|
return task.result; |
||||||
|
} |
||||||
|
|
||||||
|
|
||||||
|
_executeSql(tx, sql, sqlArgs = [], rollbackOnError = false) { |
||||||
|
return new Promise((resolve, reject) => { |
||||||
|
if (!tx) { |
||||||
|
reject(new WebSQLAdapterError("No transaction. This should not be " + |
||||||
|
" possible")); |
||||||
|
return; |
||||||
|
} |
||||||
|
tx.executeSql(sql, sqlArgs, (tx, result) => { |
||||||
|
resolve(mapResult(result)); |
||||||
|
}, (tx, e) => { |
||||||
|
reject(WebSQLAdapterError.from(e)); |
||||||
|
return rollbackOnError; |
||||||
|
}); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
_nop(tx) { |
||||||
|
return this._executeSql(tx, "SELECT 1"); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,41 @@ |
|||||||
|
import { WebSQLAdapterError } from "./websql_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(); |
||||||
|
|
||||||
|
export const WebSQLAdapter = { |
||||||
|
open: async (dbName) => { |
||||||
|
const version = "1.0"; |
||||||
|
|
||||||
|
if (!databases.has(dbName)) { |
||||||
|
databases.set(dbName, new DatabaseWrapper(dbName, version)); |
||||||
|
} |
||||||
|
|
||||||
|
return ResponseWrapper.success({ version }); |
||||||
|
}, |
||||||
|
|
||||||
|
close: async (dbName) => { |
||||||
|
databases.delete(dbName); |
||||||
|
return ResponseWrapper.success(); |
||||||
|
}, |
||||||
|
|
||||||
|
sql: async (dbName, sql, ...args) => { |
||||||
|
log(sql); |
||||||
|
|
||||||
|
const db = databases.get(dbName); |
||||||
|
if (!db) { |
||||||
|
throw new WebSQLAdapterError("Database not open"); |
||||||
|
} |
||||||
|
|
||||||
|
const result = await db.sql(sql, ...args); |
||||||
|
return ResponseWrapper.success(result); |
||||||
|
}, |
||||||
|
|
||||||
|
delete: async (dbName) => { |
||||||
|
throw new WebSQLAdapterError("Delete not implemented"); |
||||||
|
}, |
||||||
|
|
||||||
|
debug: enableDebug |
||||||
|
}; |
@ -0,0 +1,9 @@ |
|||||||
|
export class WebSQLAdapterError extends Error { |
||||||
|
static from(e) { |
||||||
|
let message = ""; |
||||||
|
if (e && e.message) { |
||||||
|
message = e.message; |
||||||
|
} |
||||||
|
return new WebSQLAdapterError(message); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,18 @@ |
|||||||
|
var webpack = require('webpack'); |
||||||
|
var libraryName = 'pawsqlite-websql-adapter'; |
||||||
|
var outputFile = libraryName + '.js'; |
||||||
|
|
||||||
|
var config = { |
||||||
|
mode: 'development', |
||||||
|
entry: __dirname + '/src/websql_adapter.mjs', |
||||||
|
devtool: 'source-map', |
||||||
|
output: { |
||||||
|
path: __dirname + '/lib', |
||||||
|
filename: outputFile, |
||||||
|
library: libraryName, |
||||||
|
libraryExport: 'default', |
||||||
|
libraryTarget: 'commonjs2', |
||||||
|
} |
||||||
|
}; |
||||||
|
|
||||||
|
module.exports = config; |
Loading…
Reference in new issue