promised-db
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -1,2 +0,2 @@ | ||
!function(t){if("object"==typeof module&&"object"==typeof module.exports){var n=t(require,exports);void 0!==n&&(module.exports=n)}else"function"==typeof define&&define.amd&&define(["require","exports"],t)}(function(t,n){"use strict";var e=function(){function t(t,n,e){this.db_=this._request(indexedDB.open(t,n),function(t){t.onupgradeneeded=function(r){var o=t.result;e(o,r.oldVersion,r.newVersion||n)}}).catch(function(n){console.warn("PromisedDB: failed to open / upgrade database '"+t+"'",n)}),this.tctx_={request:this._request.bind(this),cursor:this._cursor.bind(this),keyCursor:this._keyCursor.bind(this),getAll:this._getAll.bind(this),getAllKeys:this._getAllKeys.bind(this)}}return t.prototype.close=function(){this.db_.then(function(t){t.close()})},t.prototype.transaction=function(t,n,e){var r=this;return this.db_.then(function(o){return new Promise(function(i,u){var c=o.transaction(t,n);c.onerror=function(){l(),u(c.error||"transaction failed")},c.onabort=function(){l(),u("aborted")};var s=null,l=function(){null!==s&&(clearTimeout(s),s=null)},f=Object.create(r.tctx_,{timeout:{value:function(t){s=setTimeout(function(){s=null,c.abort()},t)}}}),a=e(c,f);c.oncomplete=function(){l(),i(void 0===a?void 0:a)}})})},t.prototype._request=function(t,n){var e=new Promise(function(e,r){t.onerror=function(){r(t.error||"request failed")},t.onsuccess=function(){e(t.result)},n&&n(t)});return this.db_?this.db_.then(function(){return e}):e},t.prototype._cursorImpl=function(t){var n={next:function(t){return this.callbackFn_=t,this},complete:function(t){return this.completeFn_=t,this},catch:function(t){return this.errorFn_=t,this}};return t.onerror=function(){n.errorFn_&&n.errorFn_(t.error)},t.onsuccess=function(){var e=t.result;e?n.callbackFn_&&n.callbackFn_(e):n.completeFn_&&n.completeFn_()},n},t.prototype._cursor=function(t,n,e){var r=t.openCursor(n,e);return this._cursorImpl(r)},t.prototype._keyCursor=function(t,n,e){var r=t.openKeyCursor(n,e);return this._cursorImpl(r)},t.prototype._getAll=function(t,n,e,r){var o=this;return new Promise(function(i,u){var c=[];o._cursor(t,n,e).next(function(t){c.push(t.value),r&&c.length===r?i(c):t.continue()}).complete(function(){i(c)}).catch(function(t){u(t)})})},t.prototype._getAllKeys=function(t,n,e,r){var o=this;return new Promise(function(i,u){var c=[];o._keyCursor(t,n,e).next(function(t){c.push(t.primaryKey),r&&c.length===r?i(c):t.continue()}).complete(function(){i(c)}).catch(function(t){u(t)})})},t}();n.PromisedDB=e}); | ||
define("promised-db",["require","exports"],function(t,n){"use strict";Object.defineProperty(n,"__esModule",{value:!0});var e=function(){function t(t,n,e){this.db_=this._request(indexedDB.open(t,n),function(t){t.onupgradeneeded=function(r){e(t.result,r.oldVersion,r.newVersion||n)}}).catch(function(n){console.warn("PromisedDB: failed to open / upgrade database '"+t+"'",n)}),this.tctx_={request:this._request.bind(this),cursor:this._cursor.bind(this),keyCursor:this._keyCursor.bind(this),getAll:this._getAll.bind(this),getAllKeys:this._getAllKeys.bind(this)}}return t.prototype.close=function(){this.db_.then(function(t){t.close()})},t.prototype.transaction=function(t,n,e){var r=this;return this.db_.then(function(o){return new Promise(function(i,u){var c=o.transaction(t,n);c.onerror=function(){l(),u(c.error||"transaction failed")},c.onabort=function(){l(),u("aborted")};var s=null,l=function(){null!==s&&(clearTimeout(s),s=null)},a=Object.create(r.tctx_,{timeout:{value:function(t){s=setTimeout(function(){s=null,c.abort()},t)}}}),f=e(c,a);c.oncomplete=function(){l(),i(f)}})})},t.prototype._request=function(t,n){var e=new Promise(function(e,r){t.onerror=function(){r(t.error||"request failed")},t.onsuccess=function(){e(t.result)},n&&n(t)});return this.db_?this.db_.then(function(){return e}):e},t.prototype._cursorImpl=function(t){var n={next:function(t){return this.callbackFn_=t,this},complete:function(t){return this.completeFn_=t,this},catch:function(t){return this.errorFn_=t,this}};return t.onerror=function(){n.errorFn_&&n.errorFn_(t.error)},t.onsuccess=function(){var e=t.result;e?n.callbackFn_&&n.callbackFn_(e):n.completeFn_&&n.completeFn_()},n},t.prototype._cursor=function(t,n,e){var r=t.openCursor(n,e);return this._cursorImpl(r)},t.prototype._keyCursor=function(t,n,e){var r=t.openKeyCursor(n,e);return this._cursorImpl(r)},t.prototype._getAll=function(t,n,e,r){var o=this;return new Promise(function(i,u){var c=[];o._cursor(t,n,e).next(function(t){c.push(t.value),r&&c.length===r?i(c):t.continue()}).complete(function(){i(c)}).catch(function(t){u(t)})})},t.prototype._getAllKeys=function(t,n,e,r){var o=this;return new Promise(function(i,u){var c=[];o._keyCursor(t,n,e).next(function(t){c.push(t.primaryKey),r&&c.length===r?i(c):t.continue()}).complete(function(){i(c)}).catch(function(t){u(t)})})},t}();n.PromisedDB=e}); | ||
//# sourceMappingURL=dist/promised-db.min.js.map |
{ | ||
"name": "promised-db", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "IndexedDB with a better API using contextual interfaces and timeout support.", | ||
"scripts": { | ||
"build": "tslint --project tsconfig.json && tsc && uglifyjs promised-db.js --in-source-map promised-db.js.map --source-map dist/promised-db.min.js.map --screw-ie8 -mc --output dist/promised-db.min.js" | ||
"build": "tslint --project tsconfig-es6.json && tsc -p tsconfig-es5.json && tsc -p tsconfig-es6.json && uglifyjs promised-db.es5.js --in-source-map promised-db.es5.js.map --source-map dist/promised-db.min.js.map --screw-ie8 -mc --output dist/promised-db.min.js" | ||
}, | ||
"main": "promised-db.js", | ||
"main": "promised-db.es5.js", | ||
"module": "promised-db.js", | ||
"typings": "promised-db.d.ts", | ||
@@ -26,7 +27,7 @@ "homepage": "https://github.com/zenmumbler/promised-db", | ||
"devDependencies": { | ||
"@types/node": "^7.0.5", | ||
"tslint": "^4.4.2", | ||
"typescript": "^2.1.6", | ||
"uglify-js": "^2.7.5" | ||
"@types/node": "^7.0.8", | ||
"tslint": "^4.5.1", | ||
"typescript": "^2.2.1", | ||
"uglify-js": "^2.8.12" | ||
} | ||
} |
// promised-db - IndexedDB wrapped in a promise-based API with contextual methods and timeout support. (https://github.com/zenmumbler/promised-db) | ||
// (c) 2016-Present by Arthur Langereis (@zenmumbler) | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
export class PromisedDB { | ||
constructor(name, version, upgrade) { | ||
this.db_ = this._request(indexedDB.open(name, version), openReq => { | ||
openReq.onupgradeneeded = upgradeEvt => { | ||
const db = openReq.result; | ||
upgrade(db, upgradeEvt.oldVersion, upgradeEvt.newVersion || version); | ||
}; | ||
}) | ||
.catch(error => { | ||
console.warn(`PromisedDB: failed to open / upgrade database '${name}'`, error); | ||
}); | ||
// the TransactionContext is implemented as the private methods in PDB | ||
// bound to this and exposed as loose functions. | ||
this.tctx_ = { | ||
request: this._request.bind(this), | ||
cursor: this._cursor.bind(this), | ||
keyCursor: this._keyCursor.bind(this), | ||
getAll: this._getAll.bind(this), | ||
getAllKeys: this._getAllKeys.bind(this) | ||
}; | ||
} | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
close() { | ||
this.db_.then(db => { | ||
db.close(); | ||
}); | ||
} | ||
})(function (require, exports) { | ||
"use strict"; | ||
var PromisedDB = (function () { | ||
function PromisedDB(name, version, upgrade) { | ||
this.db_ = this._request(indexedDB.open(name, version), function (openReq) { | ||
openReq.onupgradeneeded = function (upgradeEvt) { | ||
var db = openReq.result; | ||
upgrade(db, upgradeEvt.oldVersion, upgradeEvt.newVersion || version); | ||
transaction(storeNames, mode, fn) { | ||
return this.db_.then(db => { | ||
return new Promise((resolve, reject) => { | ||
const tr = db.transaction(storeNames, mode); | ||
tr.onerror = () => { | ||
cancelTimeout(); | ||
reject(tr.error || "transaction failed"); | ||
}; | ||
}) | ||
.catch(function (error) { | ||
console.warn("PromisedDB: failed to open / upgrade database '" + name + "'", error); | ||
}); | ||
// the TransactionContext is implemented as the private methods in PDB | ||
// bound to this and exposed as loose functions. | ||
this.tctx_ = { | ||
request: this._request.bind(this), | ||
cursor: this._cursor.bind(this), | ||
keyCursor: this._keyCursor.bind(this), | ||
getAll: this._getAll.bind(this), | ||
getAllKeys: this._getAllKeys.bind(this) | ||
}; | ||
} | ||
PromisedDB.prototype.close = function () { | ||
this.db_.then(function (db) { | ||
db.close(); | ||
}); | ||
}; | ||
PromisedDB.prototype.transaction = function (storeNames, mode, fn) { | ||
var _this = this; | ||
return this.db_.then(function (db) { | ||
return new Promise(function (resolve, reject) { | ||
var tr = db.transaction(storeNames, mode); | ||
tr.onerror = function () { | ||
cancelTimeout(); | ||
reject(tr.error || "transaction failed"); | ||
}; | ||
tr.onabort = function () { | ||
cancelTimeout(); | ||
reject("aborted"); | ||
}; | ||
var timeoutID = null; | ||
var cancelTimeout = function () { | ||
if (timeoutID !== null) { | ||
clearTimeout(timeoutID); // make timeouts work for both web and node contexts | ||
timeoutID = null; | ||
tr.onabort = () => { | ||
cancelTimeout(); | ||
reject("aborted"); | ||
}; | ||
let timeoutID = null; | ||
const cancelTimeout = function () { | ||
if (timeoutID !== null) { | ||
clearTimeout(timeoutID); // make timeouts work for both web and node contexts | ||
timeoutID = null; | ||
} | ||
}; | ||
const tc = Object.create(this.tctx_, { | ||
timeout: { | ||
value: function (ms) { | ||
timeoutID = setTimeout(function () { | ||
timeoutID = null; | ||
tr.abort(); | ||
}, ms); | ||
} | ||
}; | ||
var tc = Object.create(_this.tctx_, { | ||
timeout: { | ||
value: function (ms) { | ||
timeoutID = setTimeout(function () { | ||
timeoutID = null; | ||
tr.abort(); | ||
}, ms); | ||
} | ||
} | ||
}); | ||
var result = fn(tr, tc); | ||
tr.oncomplete = function () { | ||
cancelTimeout(); | ||
resolve((result === undefined) ? undefined : result); | ||
}; | ||
} | ||
}); | ||
const result = fn(tr, tc); | ||
tr.oncomplete = () => { | ||
cancelTimeout(); | ||
resolve(result); | ||
}; | ||
}); | ||
}); | ||
} | ||
_request(req, fn) { | ||
const reqProm = new Promise(function (resolve, reject) { | ||
req.onerror = () => { reject(req.error || "request failed"); }; | ||
req.onsuccess = () => { resolve(req.result); }; | ||
if (fn) { | ||
fn(req); | ||
} | ||
}); | ||
return this.db_ ? this.db_.then(() => reqProm) : reqProm; | ||
} | ||
_cursorImpl(cursorReq) { | ||
const result = { | ||
next: function (callback) { | ||
this.callbackFn_ = callback; | ||
return this; | ||
}, | ||
complete: function (callback) { | ||
this.completeFn_ = callback; | ||
return this; | ||
}, | ||
catch: function (callback) { | ||
this.errorFn_ = callback; | ||
return this; | ||
} | ||
}; | ||
PromisedDB.prototype._request = function (req, fn) { | ||
var reqProm = new Promise(function (resolve, reject) { | ||
req.onerror = function () { reject(req.error || "request failed"); }; | ||
req.onsuccess = function () { resolve(req.result); }; | ||
if (fn) { | ||
fn(req); | ||
} | ||
}); | ||
return this.db_ ? this.db_.then(function () { return reqProm; }) : reqProm; | ||
cursorReq.onerror = function () { | ||
if (result.errorFn_) { | ||
result.errorFn_(cursorReq.error); | ||
} | ||
}; | ||
PromisedDB.prototype._cursorImpl = function (cursorReq) { | ||
var result = { | ||
next: function (callback) { | ||
this.callbackFn_ = callback; | ||
return this; | ||
}, | ||
complete: function (callback) { | ||
this.completeFn_ = callback; | ||
return this; | ||
}, | ||
catch: function (callback) { | ||
this.errorFn_ = callback; | ||
return this; | ||
cursorReq.onsuccess = function () { | ||
const cursor = cursorReq.result; | ||
if (cursor) { | ||
if (result.callbackFn_) { | ||
result.callbackFn_(cursor); | ||
} | ||
}; | ||
cursorReq.onerror = function () { | ||
if (result.errorFn_) { | ||
result.errorFn_(cursorReq.error); | ||
} | ||
else { | ||
if (result.completeFn_) { | ||
result.completeFn_(); | ||
} | ||
}; | ||
cursorReq.onsuccess = function () { | ||
var cursor = cursorReq.result; | ||
if (cursor) { | ||
if (result.callbackFn_) { | ||
result.callbackFn_(cursor); | ||
} | ||
} | ||
}; | ||
return result; | ||
} | ||
_cursor(container, range, direction) { | ||
const cursorReq = container.openCursor(range, direction); | ||
return this._cursorImpl(cursorReq); | ||
} | ||
// IDB 2 has IDBObjectStore.openKeyCursor, but 1 does not | ||
_keyCursor(index, range, direction) { | ||
const cursorReq = index.openKeyCursor(range, direction); | ||
return this._cursorImpl(cursorReq); | ||
} | ||
_getAll(container, range, direction, limit) { | ||
return new Promise((resolve, reject) => { | ||
const result = []; | ||
this._cursor(container, range, direction) | ||
.next(cur => { | ||
result.push(cur.value); | ||
if (limit && (result.length === limit)) { | ||
resolve(result); | ||
} | ||
else { | ||
if (result.completeFn_) { | ||
result.completeFn_(); | ||
} | ||
cur.continue(); | ||
} | ||
}; | ||
return result; | ||
}; | ||
PromisedDB.prototype._cursor = function (container, range, direction) { | ||
var cursorReq = container.openCursor(range, direction); | ||
return this._cursorImpl(cursorReq); | ||
}; | ||
// IDB 2 has IDBObjectStore.openKeyCursor, but 1 does not | ||
PromisedDB.prototype._keyCursor = function (index, range, direction) { | ||
var cursorReq = index.openKeyCursor(range, direction); | ||
return this._cursorImpl(cursorReq); | ||
}; | ||
PromisedDB.prototype._getAll = function (container, range, direction, limit) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var result = []; | ||
_this._cursor(container, range, direction) | ||
.next(function (cur) { | ||
result.push(cur.value); | ||
if (limit && (result.length === limit)) { | ||
resolve(result); | ||
} | ||
else { | ||
cur.continue(); | ||
} | ||
}) | ||
.complete(function () { | ||
resolve(result); | ||
}) | ||
.catch(function (error) { | ||
reject(error); | ||
}); | ||
}) | ||
.complete(() => { | ||
resolve(result); | ||
}) | ||
.catch(error => { | ||
reject(error); | ||
}); | ||
}; | ||
PromisedDB.prototype._getAllKeys = function (container, range, direction, limit) { | ||
var _this = this; | ||
return new Promise(function (resolve, reject) { | ||
var result = []; | ||
_this._keyCursor(container, range, direction) | ||
.next(function (cur) { | ||
result.push(cur.primaryKey); | ||
if (limit && (result.length === limit)) { | ||
resolve(result); | ||
} | ||
else { | ||
cur.continue(); | ||
} | ||
}) | ||
.complete(function () { | ||
}); | ||
} | ||
_getAllKeys(container, range, direction, limit) { | ||
return new Promise((resolve, reject) => { | ||
const result = []; | ||
this._keyCursor(container, range, direction) | ||
.next(cur => { | ||
result.push(cur.primaryKey); | ||
if (limit && (result.length === limit)) { | ||
resolve(result); | ||
}) | ||
.catch(function (error) { | ||
reject(error); | ||
}); | ||
} | ||
else { | ||
cur.continue(); | ||
} | ||
}) | ||
.complete(() => { | ||
resolve(result); | ||
}) | ||
.catch(error => { | ||
reject(error); | ||
}); | ||
}; | ||
return PromisedDB; | ||
}()); | ||
exports.PromisedDB = PromisedDB; | ||
}); | ||
}); | ||
} | ||
} | ||
//# sourceMappingURL=promised-db.js.map |
@@ -16,3 +16,3 @@ PromisedDB | ||
```typescript | ||
const pdb = new PromiseDB("mydb", 1, | ||
const pdb = new PromisedDB("mydb", 1, | ||
(db, onDiskVersion, newVersion) => { | ||
@@ -19,0 +19,0 @@ // This callback is called when there is no DB on disk or if the version |
{ | ||
"compilerOptions": { | ||
"module": "umd", | ||
"target": "es5", | ||
"rootDir": "src", | ||
"outDir": ".", | ||
"declaration": false, | ||
@@ -24,3 +20,3 @@ "sourceMap": true, | ||
"lib": ["es5", "dom", "es2015.core", "es2015.iterable", "es2015.collection", "es2015.promise"], | ||
"lib": ["es5", "dom", "es2015"], | ||
"skipLibCheck": true, | ||
@@ -27,0 +23,0 @@ "skipDefaultLibCheck": false |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
71303
18
709