Skip to content

Commit 1608bb0

Browse files
committed
released 1.0.4
add storage supported check new cache only mode
1 parent f489bc3 commit 1608bb0

7 files changed

Lines changed: 39 additions & 6 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ Or add the script to your main HTML file
4949

5050
```js
5151
const db = new StorageDB({
52-
storage: window.localStorage, // storage object, default is window.localStorage
52+
storage: window.localStorage, // storage object, default is window.localStorage. If you want to store data in memory only, you can set it null
5353
database: 'testdb', // database name, default is 'db'
5454
primaryKey: 'id' // primary key of collection, default is '_id'
5555
})

lib/storagedb.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "storagedb2",
3-
"version": "1.0.2",
3+
"version": "1.0.4",
44
"description": "MongoDB-like API for HTML5 Storage (localStorage and sessionStorage)",
55
"main": "lib/index.js",
66
"scripts": {

src/collection.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import ID from './id'
2+
import MockStorage from './mock-storage'
23
import isObject from './utils/is-object'
34
import queryMatch from './utils/query-match'
45
import sortCompare from './utils/sort-compare'
@@ -8,11 +9,11 @@ class Collection {
89
opts = opts || {}
910

1011
this.name = name
11-
this.storage = db.storage
12+
this.storage = db.storage || new MockStorage()
1213
this.path = db.database + db.sep + name + db.sep
1314
this.primaryKey = opts.primaryKey || db.primaryKey
14-
this.cache = null
15-
this.cacheable = false
15+
this.cache = {}
16+
this.cacheable = !db.storage
1617
}
1718

1819

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import isSupported from './utils/is-supported'
12
import Collection from './collection'
23

34
class StorageDB {
@@ -8,6 +9,10 @@ class StorageDB {
89
this.database = opts.database || 'db'
910
this.primaryKey = opts.primaryKey || '_id'
1011
this.sep = opts.sep || ':'
12+
13+
if (!isSupported(this.storage)) {
14+
this.storage = null
15+
}
1116
}
1217

1318
get (name, opts) {

src/mock-storage.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class MockStorage {
2+
constructor () {
3+
this.length = 0
4+
}
5+
6+
key() {}
7+
setItem() {}
8+
getItem() {}
9+
removeItem() {}
10+
}
11+
12+
export default MockStorage

src/utils/is-supported.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const isSupported = (storage) => {
2+
if (!storage || !(storage instanceof Object)) {
3+
return false
4+
}
5+
6+
try {
7+
storage.setItem('_supported', '1')
8+
storage.removeItem('_supported')
9+
return true
10+
} catch (e) {
11+
return false
12+
}
13+
}
14+
15+
export default isSupported

0 commit comments

Comments
 (0)