Compare commits

...

2 Commits

Author SHA1 Message Date
4e9f5b2512 New CommonJS Build 2021-03-01 22:58:18 -08:00
c7ee992ee7 Corrected ES6 Module and CommonJS entry points 2021-03-01 22:57:51 -08:00
3 changed files with 57 additions and 461 deletions

View File

@ -87,450 +87,6 @@ module.exports =
/************************************************************************/
/******/ ({
/***/ "./src/adapters/cordova_pawsqlite/psql_adapter.mjs":
/*!*********************************************************!*\
!*** ./src/adapters/cordova_pawsqlite/psql_adapter.mjs ***!
\*********************************************************/
/*! exports provided: PSQLAdapter */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PSQLAdapter", function() { return PSQLAdapter; });
/* harmony import */ var _psql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./psql_adapter_error.mjs */ "./src/adapters/cordova_pawsqlite/psql_adapter_error.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ../../log.mjs */ "./src/log.mjs");
const PSQLAdapter = {
open: (dbName) => new Promise((resolve, reject) => {
cordova.exec(resolve, (e) => {
reject(new _psql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PSQLAdapterError"](e));
}, "PawSQLite", "open", [dbName]);
}),
close: (dbName) => new Promise((resolve, reject) => {
cordova.exec(resolve, (e) => {
reject(new _psql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PSQLAdapterError"](e));
}, "PawSQLite", "close", [dbName]);
}),
sql: (dbName, sql, ...args) => new Promise((resolve, reject) => {
Object(_log_mjs__WEBPACK_IMPORTED_MODULE_1__["log"])(sql);
cordova.exec(resolve, (e) => {
reject(new _psql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PSQLAdapterError"](e));
}, "PawSQLite", "sql", [dbName, sql, ...args]);
}),
delete: (dbName) => new Promise((resolve, reject) => {
cordova.exec(resolve, (e) => {
reject(new _psql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PSQLAdapterError"](e));
}, "PawSQLite", "delete", [dbName]);
})
};
/***/ }),
/***/ "./src/adapters/cordova_pawsqlite/psql_adapter_error.mjs":
/*!***************************************************************!*\
!*** ./src/adapters/cordova_pawsqlite/psql_adapter_error.mjs ***!
\***************************************************************/
/*! exports provided: PSQLAdapterError */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PSQLAdapterError", function() { return PSQLAdapterError; });
/* harmony import */ var _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../pawsqlite_error.mjs */ "./src/pawsqlite_error.mjs");
class PSQLAdapterError extends _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PawSQLiteError"] {
constructor(response) {
if (response.hasOwnProperty("message")) {
super(response.message);
} else {
super();
}
if (response.hasOwnProperty("name")) {
this.name = response.name;
} else {
this.name = "PSQLAdapterError";
}
if (response.hasOwnProperty("trace")) {
this.trace = response.trace;
}
}
toString() {
let str = this.name;
if (this.hasOwnProperty("message")) {
str += ": " + this.message;
}
if (this.hasOwnProperty("trace")) {
str += "\n" + this.trace;
}
return str;
}
}
/***/ }),
/***/ "./src/adapters/websql/database_wrapper.mjs":
/*!**************************************************!*\
!*** ./src/adapters/websql/database_wrapper.mjs ***!
\**************************************************/
/*! exports provided: DatabaseWrapper */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "DatabaseWrapper", function() { return DatabaseWrapper; });
/* harmony import */ var _transaction_manager_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./transaction_manager.mjs */ "./src/adapters/websql/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/adapters/websql/response_wrapper.mjs":
/*!**************************************************!*\
!*** ./src/adapters/websql/response_wrapper.mjs ***!
\**************************************************/
/*! exports provided: ResponseWrapper */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "ResponseWrapper", function() { return ResponseWrapper; });
const ResponseWrapper = {
success: (obj = {}) => ({
success: true,
...obj
})
};
/***/ }),
/***/ "./src/adapters/websql/result_mapper.mjs":
/*!***********************************************!*\
!*** ./src/adapters/websql/result_mapper.mjs ***!
\***********************************************/
/*! exports provided: mapResult */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "mapResult", function() { return mapResult; });
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/adapters/websql/transaction_manager.mjs":
/*!*****************************************************!*\
!*** ./src/adapters/websql/transaction_manager.mjs ***!
\*****************************************************/
/*! exports provided: TransactionManager */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "TransactionManager", function() { return TransactionManager; });
/* harmony import */ var _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./websql_adapter_error.mjs */ "./src/adapters/websql/websql_adapter_error.mjs");
/* harmony import */ var _result_mapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./result_mapper.mjs */ "./src/adapters/websql/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(() => Object(_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(Object(_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(() => Object(_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) {
Object(_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(Object(_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/adapters/websql/websql_adapter.mjs":
/*!************************************************!*\
!*** ./src/adapters/websql/websql_adapter.mjs ***!
\************************************************/
/*! exports provided: WebSQLAdapter */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "WebSQLAdapter", function() { return WebSQLAdapter; });
/* harmony import */ var _websql_adapter_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./websql_adapter_error.mjs */ "./src/adapters/websql/websql_adapter_error.mjs");
/* harmony import */ var _database_wrapper_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./database_wrapper.mjs */ "./src/adapters/websql/database_wrapper.mjs");
/* harmony import */ var _response_wrapper_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./response_wrapper.mjs */ "./src/adapters/websql/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) => {
Object(_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");
}
};
/***/ }),
/***/ "./src/adapters/websql/websql_adapter_error.mjs":
/*!******************************************************!*\
!*** ./src/adapters/websql/websql_adapter_error.mjs ***!
\******************************************************/
/*! exports provided: WebSQLAdapterError */
/***/ (function(__webpack_module__, __webpack_exports__, __webpack_require__) {
"use strict";
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "WebSQLAdapterError", function() { return WebSQLAdapterError; });
/* harmony import */ var _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../../pawsqlite_error.mjs */ "./src/pawsqlite_error.mjs");
class WebSQLAdapterError extends _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_0__["PawSQLiteError"] {
static from(e) {
let message = "";
if (e && e.message) {
message = e.message;
}
return new WebSQLAdapterError(message);
}
}
/***/ }),
/***/ "./src/database.mjs":
/*!**************************!*\
!*** ./src/database.mjs ***!
@ -647,15 +203,12 @@ __webpack_require__.r(__webpack_exports__);
/* harmony import */ var _database_mjs__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./database.mjs */ "./src/database.mjs");
/* harmony import */ var _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./pawsqlite_error.mjs */ "./src/pawsqlite_error.mjs");
/* harmony import */ var _log_mjs__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! ./log.mjs */ "./src/log.mjs");
/* harmony import */ var _adapters_cordova_pawsqlite_psql_adapter_mjs__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(/*! ./adapters/cordova_pawsqlite/psql_adapter.mjs */ "./src/adapters/cordova_pawsqlite/psql_adapter.mjs");
/* harmony import */ var _adapters_websql_websql_adapter_mjs__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(/*! ./adapters/websql/websql_adapter.mjs */ "./src/adapters/websql/websql_adapter.mjs");
// Adapters
const adapters = [];
/* harmony default export */ __webpack_exports__["default"] = ({
@ -671,24 +224,60 @@ __webpack_require__.r(__webpack_exports__);
return adapter.delete(dbName);
},
registerAdapter: function(adapter) {
adapters.push(wrapAdapter(adapter));
},
debug: _log_mjs__WEBPACK_IMPORTED_MODULE_2__["enableDebug"],
Error: _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__["PawSQLiteError"]
});
function wrapAdapter() {
const wrapped = {};
["name", "open", "close", "delete", "sql"].forEach((prop) => {
if (!(prop in adapter)) {
throw new _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__["PawSQLiteError"](`Invalid adapter: missing property: ${ prop }`);
}
if (
prop === "name" && !(
typeof adapter[prop] === "string" ||
adapter[prop] instanceof String
) ||
prop !== "name" && (
typeof adapter[prop] !== "function"
)
) {
throw new _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__["PawSQLiteError"]("Invalid adapter: invalid type for property: " +
prop);
}
if (typeof adapter[prop] === "function") {
wrapped[prop] = async (...args) => {
try {
return await adapter[prop](...args);
} catch (err) {
throw _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__["PawSQLiteError"].from(err);
}
};
} else {
wrapped[prop] = adapter[prop];
}
});
return wrapped;
}
function getAdapter(adapterName) {
if (!adapterName) {
adapterName = "pawsqlite";
} else {
adapterName = adapterName.toLowerCase();
}
if (adapterName === "pawsqlite") {
return _adapters_cordova_pawsqlite_psql_adapter_mjs__WEBPACK_IMPORTED_MODULE_3__["PSQLAdapter"];
} else if (adapterName === "websql") {
return _adapters_websql_websql_adapter_mjs__WEBPACK_IMPORTED_MODULE_4__["WebSQLAdapter"];
} else {
const adapter = adapters.find((a) => a.name === adapterName);
if (!adapter) {
throw new _pawsqlite_error_mjs__WEBPACK_IMPORTED_MODULE_1__["PawSQLiteError"](`Unknown Adapter: ${adapterName}`);
}
return adapter;
}
/***/ }),
@ -704,6 +293,12 @@ function getAdapter(adapterName) {
__webpack_require__.r(__webpack_exports__);
/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "PawSQLiteError", function() { return PawSQLiteError; });
class PawSQLiteError extends Error {
static from(err) {
const p_err = new PawSQLiteError(err.toString());
p_err.cause = err;
return p_err;
}
constructor(message) {
super(message);
this.name = "PawSQLiteError";

File diff suppressed because one or more lines are too long

View File

@ -1,8 +1,9 @@
{
"name": "pawsqlite",
"name": "PawSQLite",
"version": "1.0.0",
"description": "An SQLite interface with adapters for Cordova and WebSQL",
"main": "./src/pawsqlite.mjs",
"main": "./lib/PawSQLite.js",
"module": "./src/pawsqlite.mjs",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "webpack"