@live-change/db-store-localstorage 0.6.1 → 0.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Store.js +11 -6
- package/package.json +3 -3
package/lib/Store.js
CHANGED
|
@@ -221,7 +221,7 @@ class RangeObservable extends ReactiveDao.ObservableList {
|
|
|
221
221
|
}
|
|
222
222
|
|
|
223
223
|
class Store {
|
|
224
|
-
constructor(dbName, storeName, type) {
|
|
224
|
+
constructor(dbName, storeName, type, options = {}) {
|
|
225
225
|
if(!dbName) throw new Error("dbName argument is required")
|
|
226
226
|
if(!storeName) throw new Error("storeName argument is required")
|
|
227
227
|
if(!type) throw new Error("type argument is required")
|
|
@@ -229,6 +229,11 @@ class Store {
|
|
|
229
229
|
this.dbName = dbName
|
|
230
230
|
this.storeName = storeName
|
|
231
231
|
|
|
232
|
+
const {
|
|
233
|
+
serialization = JSON
|
|
234
|
+
} = options
|
|
235
|
+
this.serialization = serialization
|
|
236
|
+
|
|
232
237
|
this.prefix = `lcdb/${dbName}/${storeName}/`
|
|
233
238
|
|
|
234
239
|
this.finished = false
|
|
@@ -303,7 +308,7 @@ class Store {
|
|
|
303
308
|
async objectGet(id) {
|
|
304
309
|
if(!id) throw new Error("key is required")
|
|
305
310
|
if(typeof id != 'string') throw new Error(`ID is not string: ${JSON.stringify(id)}`)
|
|
306
|
-
return
|
|
311
|
+
return this.serialization.parse(await this.storage.getItem(this.prefix + id) || 'null')
|
|
307
312
|
}
|
|
308
313
|
|
|
309
314
|
objectObservable(key) {
|
|
@@ -329,7 +334,7 @@ class Store {
|
|
|
329
334
|
return true
|
|
330
335
|
}).slice(0, limit)
|
|
331
336
|
const objects = (await this.storage.getValues(keys))
|
|
332
|
-
.map(json =>
|
|
337
|
+
.map(json => this.serialization.parse(json))
|
|
333
338
|
.sort((a, b) => (a.id > b.id) ? 1 : ((b.id > a.id) ? -1 : 0))
|
|
334
339
|
if(range.reverse) objects.reverse()
|
|
335
340
|
return objects
|
|
@@ -385,8 +390,8 @@ class Store {
|
|
|
385
390
|
async put(object) {
|
|
386
391
|
const id = object.id
|
|
387
392
|
if(typeof id != 'string') throw new Error(`ID is not string: ${JSON.stringify(id)}`)
|
|
388
|
-
const oldObject =
|
|
389
|
-
await this.storage.setItem(this.prefix + id,
|
|
393
|
+
const oldObject = this.serialization.parse(await this.storage.getItem(this.prefix + id) || 'null')
|
|
394
|
+
await this.storage.setItem(this.prefix + id, this.serialization.stringify(object))
|
|
390
395
|
const objectObservable = this.objectObservables.get(id)
|
|
391
396
|
if(objectObservable) objectObservable.set(object, oldObject)
|
|
392
397
|
const rangeObservables = this.rangeObservablesTree.search([id, id])
|
|
@@ -399,7 +404,7 @@ class Store {
|
|
|
399
404
|
|
|
400
405
|
async delete(id) {
|
|
401
406
|
if(typeof id != 'string') throw new Error(`ID is not string: ${JSON.stringify(id)}`)
|
|
402
|
-
const object =
|
|
407
|
+
const object = this.serialization.parse(await this.storage.getItem(this.prefix + id))
|
|
403
408
|
await this.storage.removeItem(this.prefix + id)
|
|
404
409
|
const objectObservable = this.objectObservables.get(id)
|
|
405
410
|
if(objectObservable) objectObservable.set(null)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/db-store-localstorage",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.3",
|
|
4
4
|
"description": "Database with observable data for live queries",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -26,9 +26,9 @@
|
|
|
26
26
|
"tape": "^5.3.2"
|
|
27
27
|
},
|
|
28
28
|
"dependencies": {
|
|
29
|
-
"@live-change/dao": "0.5.
|
|
29
|
+
"@live-change/dao": "0.5.15",
|
|
30
30
|
"@live-change/interval-tree": "^1.0.12",
|
|
31
31
|
"broadcast-channel": "^4.2.0"
|
|
32
32
|
},
|
|
33
|
-
"gitHead": "
|
|
33
|
+
"gitHead": "8dc4ac726243970c9f1431bf67b4390f7845ce76"
|
|
34
34
|
}
|