Skip to content

Commit

Permalink
Merge af31115 into 2721cfc
Browse files Browse the repository at this point in the history
  • Loading branch information
Александр Вахитов authored Jun 24, 2019
2 parents 2721cfc + af31115 commit e71b0de
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 100 deletions.
13 changes: 11 additions & 2 deletions .babelrc
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"]
}
}
]
]
}
4 changes: 2 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"extends": "unity"
}
"extends": "unity"
}
4 changes: 1 addition & 3 deletions .travis.yml
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
Expand Down
61 changes: 28 additions & 33 deletions package.json
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.",
"main": "lib/index.js",
"scripts": {
"build": "npm run test && npm run clean && ./node_modules/.bin/babel src --out-dir lib",
Expand All @@ -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",
Expand All @@ -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": {
Expand All @@ -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": [
Expand Down
79 changes: 45 additions & 34 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {}
};

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';

return result;
}, {});

Expand All @@ -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) {
Expand All @@ -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;

Expand All @@ -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');
}

Expand All @@ -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();
Expand All @@ -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()
Expand All @@ -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);

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) {
Expand All @@ -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)
};
}

Expand Down
Loading

0 comments on commit e71b0de

Please sign in to comment.