-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(deps): update all deps and fix database schema #37
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,12 @@ | ||
{ | ||
"presets": ["es2015", "stage-2"] | ||
} | ||
"presets": [ | ||
[ | ||
"@babel/preset-env", | ||
{ | ||
"targets": { | ||
"browsers": ["edge >= 15", "safari >= 9", "last 2 versions"] | ||
} | ||
} | ||
] | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": "unity" | ||
} | ||
"extends": "unity" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,6 @@ | ||
language: node_js | ||
node_js: | ||
- "6" | ||
- "5" | ||
- "4" | ||
- "11" | ||
cache: | ||
directories: | ||
- node_modules | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
{ | ||
"name": "unity-cache", | ||
"version": "2.1.0", | ||
"description": "Cache abstraction around localforage.", | ||
"version": "2.2.0", | ||
"description": "Cache abstraction around Dexie.", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
"main": "lib/index.js", | ||
"scripts": { | ||
"build": "npm run test && npm run clean && ./node_modules/.bin/babel src --out-dir lib", | ||
|
@@ -10,7 +10,7 @@ | |
"commit": "./node_modules/.bin/git-cz", | ||
"coverage:report": "./node_modules/.bin/nyc report", | ||
"coverage:send": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls", | ||
"lint": "./node_modules/.bin/eslint --ignore-path=.gitignore --fix ./src", | ||
"lint": "./node_modules/.bin/eslint --ignore-path=.gitignore --fix ./src ./test", | ||
"lint-prod": "NODE_ENV='production' npm run lint", | ||
"version": " ./node_modules/.bin/conventional-changelog -i CHANGELOG.md -s && git add CHANGELOG.md", | ||
"prepublish": "npm run build", | ||
|
@@ -36,40 +36,37 @@ | |
}, | ||
"homepage": "https://github.com/auru/unity-cache#readme", | ||
"keywords": [ | ||
"unity", | ||
"cache", | ||
"indexeddb", | ||
"localforage", | ||
"localstorage", | ||
"dexie", | ||
"storage", | ||
"unity", | ||
"websql" | ||
"indexeddb" | ||
], | ||
"engines": { | ||
"node": ">=4", | ||
"npm": ">=3" | ||
"node": ">=11", | ||
"npm": ">=6" | ||
}, | ||
"dependencies": { | ||
"dexie": "^2.0.1" | ||
"dexie": "^2.0.4" | ||
}, | ||
"devDependencies": { | ||
"ava": "^0.18.1", | ||
"babel-cli": "^6.16.0", | ||
"babel-core": "^6.17.0", | ||
"babel-polyfill": "^6.16.0", | ||
"babel-preset-es2015": "^6.16.0", | ||
"babel-preset-stage-2": "^6.16.0", | ||
"babel-register": "^6.16.3", | ||
"browser-env": "^2.0.19", | ||
"commitizen": "^2.8.6", | ||
"conventional-changelog-cli": "^1.2.0", | ||
"coveralls": "^2.11.14", | ||
"cz-conventional-changelog": "^1.2.0", | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/polyfill": "^7.4.4", | ||
"@babel/preset-env": "^7.4.5", | ||
"@babel/register": "^7.4.4", | ||
"ava": "^2.1.0", | ||
"browser-env": "^3.2.6", | ||
"commitizen": "^3.1.1", | ||
"conventional-changelog-cli": "^2.0.21", | ||
"coveralls": "^3.0.4", | ||
"cz-conventional-changelog": "^2.1.0", | ||
"eslint-config-unity": "^1.0.1", | ||
"fake-indexeddb": "^1.0.8", | ||
"husky": "^0.11.9", | ||
"nyc": "^10.0.0", | ||
"fake-indexeddb": "^2.1.1", | ||
"husky": "^2.5.0", | ||
"nyc": "^14.1.1", | ||
"rimraf": "^2.5.4", | ||
"sinon": "^2.0.0", | ||
"sinon": "^7.3.2", | ||
"validate-commit-msg": "^2.8.2" | ||
}, | ||
"ava": { | ||
|
@@ -79,15 +76,13 @@ | |
"source": [ | ||
"src/**/*.js" | ||
], | ||
"concurrency": 4, | ||
"failFast": false, | ||
"tap": false, | ||
"require": [ | ||
"./test/setup.js", | ||
"babel-register", | ||
"babel-polyfill" | ||
"@babel/register", | ||
"@babel/polyfill" | ||
], | ||
"babel": "inherit" | ||
"failFast": false, | ||
"concurrency": 4 | ||
}, | ||
"nyc": { | ||
"include": [ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,25 +2,23 @@ import Dexie from 'dexie'; | |
import UnityCacheError from './error'; | ||
|
||
const RE_BIN = /^\w+$/; | ||
const EXPIRE_BIN = '___expire___'; | ||
const EXPIRE_GLUE = '::'; | ||
const DEFAULT_NAME = 'unity'; | ||
const DEFAULT_VERSION = 1; | ||
|
||
const cacheInstance = { | ||
config: {}, | ||
db: null | ||
db: null, | ||
config: {} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Useless change There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's like a tree 🌲 |
||
}; | ||
|
||
function setCacheConfig(name, stores, version) { | ||
stores = [].concat(stores, EXPIRE_BIN); | ||
|
||
function initCacheConfig(stores = [], name = DEFAULT_NAME, version = DEFAULT_VERSION) { | ||
stores = [].concat(stores); | ||
stores = stores.reduce((result, storeName) => { | ||
if (!RE_BIN.test(storeName)) { | ||
throw new UnityCacheError(`Store names can only be alphanumeric, '${storeName}' given`); | ||
} | ||
|
||
result[storeName] = '&'; | ||
result[storeName] = '&key, value, expire'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To remove |
||
|
||
return result; | ||
}, {}); | ||
|
||
|
@@ -34,16 +32,18 @@ function setCacheConfig(name, stores, version) { | |
function initCacheStores() { | ||
const { name, stores, version } = cacheInstance.config; | ||
|
||
if (cacheInstance.db) { | ||
closeDB(); | ||
} | ||
|
||
cacheInstance.db = new Dexie(name); | ||
|
||
if (!cacheInstance.db) { | ||
/* istanbul ignore next: error new Dexie */ | ||
throw new UnityCacheError('Database is undefined or null'); | ||
} | ||
|
||
cacheInstance.db | ||
.version(version) | ||
.stores(stores); | ||
cacheInstance.db.version(version).stores(stores); | ||
} | ||
|
||
function errorHandlerWrapper(method) { | ||
|
@@ -54,6 +54,8 @@ function errorHandlerWrapper(method) { | |
switch (e.name) { | ||
case Dexie.errnames.Upgrade: | ||
case Dexie.errnames.Version: | ||
case Dexie.errnames.InvalidState: | ||
case Dexie.errnames.QuotaExceeded: | ||
await upgradeDB(); | ||
return null; | ||
|
||
|
@@ -70,8 +72,20 @@ function errorHandlerWrapper(method) { | |
}; | ||
} | ||
|
||
function closeDB() { | ||
if (!cacheInstance.db) { | ||
/* istanbul ignore next: db is not defined */ | ||
throw new UnityCacheError('Database is undefined or null'); | ||
} | ||
|
||
if (cacheInstance.db.isOpen()) { | ||
cacheInstance.db.close(); | ||
} | ||
} | ||
|
||
async function openDB() { | ||
if (!cacheInstance.db) { | ||
/* istanbul ignore next: db is not defined */ | ||
throw new UnityCacheError('Database is undefined or null'); | ||
} | ||
|
||
|
@@ -88,6 +102,11 @@ async function openDB() { | |
} | ||
|
||
async function upgradeDB() { | ||
if (!cacheInstance.db) { | ||
/* istanbul ignore next: db is not defined */ | ||
throw new UnityCacheError('Database is undefined or null'); | ||
} | ||
|
||
return await deleteDB() | ||
.then(() => { | ||
initCacheStores(); | ||
|
@@ -100,10 +119,11 @@ async function upgradeDB() { | |
|
||
async function deleteDB() { | ||
if (!cacheInstance.db) { | ||
/* istanbul ignore next: db is not defined */ | ||
throw new UnityCacheError('Database is undefined or null'); | ||
} | ||
|
||
cacheInstance.db.close(); | ||
closeDB(); | ||
|
||
return await cacheInstance.db | ||
.delete() | ||
|
@@ -113,30 +133,23 @@ async function deleteDB() { | |
}); | ||
} | ||
|
||
function getExpireKey(store, key) { | ||
return store + EXPIRE_GLUE + key; | ||
} | ||
|
||
async function get(store, key, validate = true) { | ||
const { db } = cacheInstance; | ||
|
||
const expired = await db[EXPIRE_BIN].get(getExpireKey(store, key)); | ||
const isValid = validate && db[store] ? expired > Date.now() : true; | ||
const { value = null, expire = 0 } = await db[store].get(key) || {}; | ||
const isValid = validate ? expire > Date.now() : true; | ||
|
||
if (!isValid) { | ||
await db[EXPIRE_BIN].delete(getExpireKey(store, key)); | ||
await db[store].delete(key); | ||
} | ||
|
||
return isValid ? await db[store].get(key) : null; | ||
return isValid ? value : null; | ||
} | ||
|
||
async function set(store, key, value, expire = Number.MAX_SAFE_INTEGER) { | ||
async function set(store, key, value, ttl = Number.MAX_SAFE_INTEGER) { | ||
const { db } = cacheInstance; | ||
const expire = Date.now() + Number(ttl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Number('fdfff') // NaN I think it would be better to check its type before There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to rewrite it to typescript. The check doesn't help at all. |
||
|
||
return await Promise.all([ | ||
db[EXPIRE_BIN].put(Date.now() + Number(expire), getExpireKey(store, key)), | ||
db[store].put(value, key) | ||
]); | ||
return await db[store].put({ key, value, expire }); | ||
} | ||
|
||
async function remove(store, key) { | ||
|
@@ -146,28 +159,26 @@ async function remove(store, key) { | |
|
||
const { db } = cacheInstance; | ||
|
||
return await Promise.all([ | ||
db[EXPIRE_BIN].delete(getExpireKey(store, key)), | ||
db[store].delete(key) | ||
]); | ||
return await db[store].delete(key); | ||
} | ||
|
||
async function drop(stores) { | ||
const { db } = cacheInstance; | ||
|
||
stores = [].concat(stores); | ||
|
||
return await Promise.all(stores.map(store => db[store].clear())); | ||
} | ||
|
||
function createCache(stores, name = DEFAULT_NAME, version = DEFAULT_VERSION) { | ||
setCacheConfig(name, stores, version); | ||
function createCache(stores = [], name = DEFAULT_NAME, version = DEFAULT_VERSION) { | ||
initCacheConfig(stores, name, version); | ||
initCacheStores(); | ||
|
||
return { | ||
get: errorHandlerWrapper(get), | ||
set: errorHandlerWrapper(set), | ||
remove: errorHandlerWrapper(remove), | ||
drop: errorHandlerWrapper(drop) | ||
drop: errorHandlerWrapper(drop), | ||
remove: errorHandlerWrapper(remove) | ||
}; | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
require('browser-env')(['window', 'document', 'navigator']); | ||
require('@babel/polyfill'); | ||
require('browser-env')([ 'window', 'document', 'navigator' ]); | ||
|
||
const indexedDB = require('fake-indexeddb'); | ||
const FDBKeyRange = require('fake-indexeddb/lib/FDBKeyRange'); | ||
|
||
window.Promise = global.Promise; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cause Dexie uses Promise from window. |
||
window.indexedDB = indexedDB; | ||
window.IDBKeyRange = FDBKeyRange; | ||
window.IDBKeyRange = FDBKeyRange; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You updated node to major version. I think it should been updated to major.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Node is used only in testing environment. I'm not sure about the major version.