Skip to content

Commit 9dd55e4

Browse files
chore(deps): update all deps and fix database schema (#37)
1 parent 2721cfc commit 9dd55e4

File tree

8 files changed

+143
-100
lines changed

8 files changed

+143
-100
lines changed

.babelrc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
{
2-
"presets": ["es2015", "stage-2"]
3-
}
2+
"presets": [
3+
[
4+
"@babel/preset-env",
5+
{
6+
"targets": {
7+
"browsers": ["edge >= 15", "safari >= 9", "last 2 versions"]
8+
}
9+
}
10+
]
11+
]
12+
}

.eslintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"extends": "unity"
3-
}
2+
"extends": "unity"
3+
}

.travis.yml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
language: node_js
22
node_js:
3-
- "6"
4-
- "5"
5-
- "4"
3+
- "11"
64
cache:
75
directories:
86
- node_modules

package.json

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "unity-cache",
3-
"version": "2.1.0",
4-
"description": "Cache abstraction around localforage.",
3+
"version": "2.2.0",
4+
"description": "Cache abstraction around Dexie.",
55
"main": "lib/index.js",
66
"scripts": {
77
"build": "npm run test && npm run clean && ./node_modules/.bin/babel src --out-dir lib",
@@ -10,7 +10,7 @@
1010
"commit": "./node_modules/.bin/git-cz",
1111
"coverage:report": "./node_modules/.bin/nyc report",
1212
"coverage:send": "./node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls",
13-
"lint": "./node_modules/.bin/eslint --ignore-path=.gitignore --fix ./src",
13+
"lint": "./node_modules/.bin/eslint --ignore-path=.gitignore --fix ./src ./test",
1414
"lint-prod": "NODE_ENV='production' npm run lint",
1515
"version": " ./node_modules/.bin/conventional-changelog -i CHANGELOG.md -s && git add CHANGELOG.md",
1616
"prepublish": "npm run build",
@@ -36,40 +36,37 @@
3636
},
3737
"homepage": "https://github.com/auru/unity-cache#readme",
3838
"keywords": [
39+
"unity",
3940
"cache",
40-
"indexeddb",
41-
"localforage",
42-
"localstorage",
41+
"dexie",
4342
"storage",
44-
"unity",
45-
"websql"
43+
"indexeddb"
4644
],
4745
"engines": {
48-
"node": ">=4",
49-
"npm": ">=3"
46+
"node": ">=11",
47+
"npm": ">=6"
5048
},
5149
"dependencies": {
52-
"dexie": "^2.0.1"
50+
"dexie": "^2.0.4"
5351
},
5452
"devDependencies": {
55-
"ava": "^0.18.1",
56-
"babel-cli": "^6.16.0",
57-
"babel-core": "^6.17.0",
58-
"babel-polyfill": "^6.16.0",
59-
"babel-preset-es2015": "^6.16.0",
60-
"babel-preset-stage-2": "^6.16.0",
61-
"babel-register": "^6.16.3",
62-
"browser-env": "^2.0.19",
63-
"commitizen": "^2.8.6",
64-
"conventional-changelog-cli": "^1.2.0",
65-
"coveralls": "^2.11.14",
66-
"cz-conventional-changelog": "^1.2.0",
53+
"@babel/cli": "^7.4.4",
54+
"@babel/core": "^7.4.5",
55+
"@babel/polyfill": "^7.4.4",
56+
"@babel/preset-env": "^7.4.5",
57+
"@babel/register": "^7.4.4",
58+
"ava": "^2.1.0",
59+
"browser-env": "^3.2.6",
60+
"commitizen": "^3.1.1",
61+
"conventional-changelog-cli": "^2.0.21",
62+
"coveralls": "^3.0.4",
63+
"cz-conventional-changelog": "^2.1.0",
6764
"eslint-config-unity": "^1.0.1",
68-
"fake-indexeddb": "^1.0.8",
69-
"husky": "^0.11.9",
70-
"nyc": "^10.0.0",
65+
"fake-indexeddb": "^2.1.1",
66+
"husky": "^2.5.0",
67+
"nyc": "^14.1.1",
7168
"rimraf": "^2.5.4",
72-
"sinon": "^2.0.0",
69+
"sinon": "^7.3.2",
7370
"validate-commit-msg": "^2.8.2"
7471
},
7572
"ava": {
@@ -79,15 +76,13 @@
7976
"source": [
8077
"src/**/*.js"
8178
],
82-
"concurrency": 4,
83-
"failFast": false,
84-
"tap": false,
8579
"require": [
8680
"./test/setup.js",
87-
"babel-register",
88-
"babel-polyfill"
81+
"@babel/register",
82+
"@babel/polyfill"
8983
],
90-
"babel": "inherit"
84+
"failFast": false,
85+
"concurrency": 4
9186
},
9287
"nyc": {
9388
"include": [

src/index.js

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,23 @@ import Dexie from 'dexie';
22
import UnityCacheError from './error';
33

44
const RE_BIN = /^\w+$/;
5-
const EXPIRE_BIN = '___expire___';
6-
const EXPIRE_GLUE = '::';
75
const DEFAULT_NAME = 'unity';
86
const DEFAULT_VERSION = 1;
97

108
const cacheInstance = {
11-
config: {},
12-
db: null
9+
db: null,
10+
config: {}
1311
};
1412

15-
function setCacheConfig(name, stores, version) {
16-
stores = [].concat(stores, EXPIRE_BIN);
17-
13+
function initCacheConfig(stores = [], name = DEFAULT_NAME, version = DEFAULT_VERSION) {
14+
stores = [].concat(stores);
1815
stores = stores.reduce((result, storeName) => {
1916
if (!RE_BIN.test(storeName)) {
2017
throw new UnityCacheError(`Store names can only be alphanumeric, '${storeName}' given`);
2118
}
2219

23-
result[storeName] = '&';
20+
result[storeName] = '&key, value, expire';
21+
2422
return result;
2523
}, {});
2624

@@ -34,16 +32,18 @@ function setCacheConfig(name, stores, version) {
3432
function initCacheStores() {
3533
const { name, stores, version } = cacheInstance.config;
3634

35+
if (cacheInstance.db) {
36+
closeDB();
37+
}
38+
3739
cacheInstance.db = new Dexie(name);
3840

3941
if (!cacheInstance.db) {
4042
/* istanbul ignore next: error new Dexie */
4143
throw new UnityCacheError('Database is undefined or null');
4244
}
4345

44-
cacheInstance.db
45-
.version(version)
46-
.stores(stores);
46+
cacheInstance.db.version(version).stores(stores);
4747
}
4848

4949
function errorHandlerWrapper(method) {
@@ -54,6 +54,8 @@ function errorHandlerWrapper(method) {
5454
switch (e.name) {
5555
case Dexie.errnames.Upgrade:
5656
case Dexie.errnames.Version:
57+
case Dexie.errnames.InvalidState:
58+
case Dexie.errnames.QuotaExceeded:
5759
await upgradeDB();
5860
return null;
5961

@@ -70,8 +72,20 @@ function errorHandlerWrapper(method) {
7072
};
7173
}
7274

75+
function closeDB() {
76+
if (!cacheInstance.db) {
77+
/* istanbul ignore next: db is not defined */
78+
throw new UnityCacheError('Database is undefined or null');
79+
}
80+
81+
if (cacheInstance.db.isOpen()) {
82+
cacheInstance.db.close();
83+
}
84+
}
85+
7386
async function openDB() {
7487
if (!cacheInstance.db) {
88+
/* istanbul ignore next: db is not defined */
7589
throw new UnityCacheError('Database is undefined or null');
7690
}
7791

@@ -88,6 +102,11 @@ async function openDB() {
88102
}
89103

90104
async function upgradeDB() {
105+
if (!cacheInstance.db) {
106+
/* istanbul ignore next: db is not defined */
107+
throw new UnityCacheError('Database is undefined or null');
108+
}
109+
91110
return await deleteDB()
92111
.then(() => {
93112
initCacheStores();
@@ -100,10 +119,11 @@ async function upgradeDB() {
100119

101120
async function deleteDB() {
102121
if (!cacheInstance.db) {
122+
/* istanbul ignore next: db is not defined */
103123
throw new UnityCacheError('Database is undefined or null');
104124
}
105125

106-
cacheInstance.db.close();
126+
closeDB();
107127

108128
return await cacheInstance.db
109129
.delete()
@@ -113,30 +133,23 @@ async function deleteDB() {
113133
});
114134
}
115135

116-
function getExpireKey(store, key) {
117-
return store + EXPIRE_GLUE + key;
118-
}
119-
120136
async function get(store, key, validate = true) {
121137
const { db } = cacheInstance;
122-
123-
const expired = await db[EXPIRE_BIN].get(getExpireKey(store, key));
124-
const isValid = validate && db[store] ? expired > Date.now() : true;
138+
const { value = null, expire = 0 } = await db[store].get(key) || {};
139+
const isValid = validate ? expire > Date.now() : true;
125140

126141
if (!isValid) {
127-
await db[EXPIRE_BIN].delete(getExpireKey(store, key));
142+
await db[store].delete(key);
128143
}
129144

130-
return isValid ? await db[store].get(key) : null;
145+
return isValid ? value : null;
131146
}
132147

133-
async function set(store, key, value, expire = Number.MAX_SAFE_INTEGER) {
148+
async function set(store, key, value, ttl = Number.MAX_SAFE_INTEGER) {
134149
const { db } = cacheInstance;
150+
const expire = Date.now() + Number(ttl);
135151

136-
return await Promise.all([
137-
db[EXPIRE_BIN].put(Date.now() + Number(expire), getExpireKey(store, key)),
138-
db[store].put(value, key)
139-
]);
152+
return await db[store].put({ key, value, expire });
140153
}
141154

142155
async function remove(store, key) {
@@ -146,28 +159,26 @@ async function remove(store, key) {
146159

147160
const { db } = cacheInstance;
148161

149-
return await Promise.all([
150-
db[EXPIRE_BIN].delete(getExpireKey(store, key)),
151-
db[store].delete(key)
152-
]);
162+
return await db[store].delete(key);
153163
}
154164

155165
async function drop(stores) {
156166
const { db } = cacheInstance;
157167

158168
stores = [].concat(stores);
169+
159170
return await Promise.all(stores.map(store => db[store].clear()));
160171
}
161172

162-
function createCache(stores, name = DEFAULT_NAME, version = DEFAULT_VERSION) {
163-
setCacheConfig(name, stores, version);
173+
function createCache(stores = [], name = DEFAULT_NAME, version = DEFAULT_VERSION) {
174+
initCacheConfig(stores, name, version);
164175
initCacheStores();
165176

166177
return {
167178
get: errorHandlerWrapper(get),
168179
set: errorHandlerWrapper(set),
169-
remove: errorHandlerWrapper(remove),
170-
drop: errorHandlerWrapper(drop)
180+
drop: errorHandlerWrapper(drop),
181+
remove: errorHandlerWrapper(remove)
171182
};
172183
}
173184

0 commit comments

Comments
 (0)