@live-change/db-store-observable-db 0.6.0 → 0.6.2
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 +15 -13
- package/package.json +2 -2
package/lib/Store.js
CHANGED
|
@@ -566,17 +566,19 @@ class DatabaseConnection {
|
|
|
566
566
|
packet.writeUInt32BE(requestId, 1)
|
|
567
567
|
this.websocket.send(packet)
|
|
568
568
|
}
|
|
569
|
-
|
|
570
569
|
}
|
|
571
570
|
|
|
572
|
-
|
|
573
|
-
|
|
574
571
|
class Store {
|
|
575
|
-
constructor(connection, databaseName, storeName) {
|
|
572
|
+
constructor(connection, databaseName, storeName, options = {}) {
|
|
576
573
|
this.connection = connection
|
|
577
574
|
this.databaseName = databaseName
|
|
578
575
|
this.storeName = storeName
|
|
579
576
|
|
|
577
|
+
const {
|
|
578
|
+
serialization = JSON
|
|
579
|
+
} = options
|
|
580
|
+
this.serialization = serialization
|
|
581
|
+
|
|
580
582
|
this.objectObservables = new Map()
|
|
581
583
|
this.rangeObservables = new Map()
|
|
582
584
|
this.countObservables = new Map()
|
|
@@ -585,7 +587,7 @@ class Store {
|
|
|
585
587
|
objectGet(id) {
|
|
586
588
|
if(!id) throw new Error("id is required")
|
|
587
589
|
return this.connection.get(this.databaseName, this.storeName, id)
|
|
588
|
-
.then(result => result && ({ id, ...
|
|
590
|
+
.then(result => result && ({ id, ...this.serialization.parse(result) }))
|
|
589
591
|
}
|
|
590
592
|
|
|
591
593
|
objectObservable(id) {
|
|
@@ -593,7 +595,7 @@ class Store {
|
|
|
593
595
|
const observableValue = new ReactiveDao.ObservableValue()
|
|
594
596
|
const onValue = (value) => {
|
|
595
597
|
if(debug) console.log("OBJ", id, "ON VALUE", value)
|
|
596
|
-
observableValue.set(value && { id, ...
|
|
598
|
+
observableValue.set(value && { id, ...this.serialization.parse(value) })
|
|
597
599
|
return !observableValue.isDisposed()
|
|
598
600
|
}
|
|
599
601
|
const onError = (error) => {
|
|
@@ -624,7 +626,7 @@ class Store {
|
|
|
624
626
|
async rangeGet(range) {
|
|
625
627
|
if(!range) throw new Error("range not defined")
|
|
626
628
|
const rawResults = await this.connection.getRange(this.databaseName, this.storeName, range)
|
|
627
|
-
return rawResults.map(({ key, value }) => ({ id: key, ...
|
|
629
|
+
return rawResults.map(({ key, value }) => ({ id: key, ...this.serialization.parse(value) }))
|
|
628
630
|
}
|
|
629
631
|
|
|
630
632
|
rangeObservable(range) {
|
|
@@ -640,16 +642,16 @@ class Store {
|
|
|
640
642
|
if(changesMode) {
|
|
641
643
|
if(found) {
|
|
642
644
|
if(last) {
|
|
643
|
-
observableList.push({ id: key, ...
|
|
645
|
+
observableList.push({ id: key, ...this.serialization.parse(value)})
|
|
644
646
|
} else {
|
|
645
|
-
const obj = { id: key, ...
|
|
647
|
+
const obj = { id: key, ...this.serialization.parse(value)}
|
|
646
648
|
observableList.putByField('id', key, obj, range.reverse)
|
|
647
649
|
}
|
|
648
650
|
} else {
|
|
649
651
|
observableList.removeByField('id', key)
|
|
650
652
|
}
|
|
651
653
|
} else if(found) {
|
|
652
|
-
results.push({ id: key, ...
|
|
654
|
+
results.push({ id: key, ...this.serialization.parse(value) })
|
|
653
655
|
} else {
|
|
654
656
|
console.error("non existing object in initial read!")
|
|
655
657
|
}
|
|
@@ -730,14 +732,14 @@ class Store {
|
|
|
730
732
|
if(debug) console.log("PUT!!!", object)
|
|
731
733
|
if(!id) throw new Error("ID must not be empty string!")
|
|
732
734
|
const putResult = await this.connection.put(this.databaseName, this.storeName, id,
|
|
733
|
-
|
|
734
|
-
return putResult ? { id, ...
|
|
735
|
+
this.serialization.stringify({ ...object, id: undefined }))
|
|
736
|
+
return putResult ? { id, ...this.serialization.parse(putResult) } : null
|
|
735
737
|
}
|
|
736
738
|
|
|
737
739
|
async delete(id) {
|
|
738
740
|
if(!id) throw new Error("ID must not be empty string!")
|
|
739
741
|
const deleteResult = await this.connection.delete(this.databaseName, this.storeName, id)
|
|
740
|
-
return deleteResult ? { id, ...
|
|
742
|
+
return deleteResult ? { id, ...this.serialization.parse(deleteResult) } : null
|
|
741
743
|
}
|
|
742
744
|
|
|
743
745
|
async close() {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-change/db-store-observable-db",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.2",
|
|
4
4
|
"description": "Database backend protocol",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -29,5 +29,5 @@
|
|
|
29
29
|
"utf-8-validate": "^5.0.4",
|
|
30
30
|
"ws": "^7.4.5"
|
|
31
31
|
},
|
|
32
|
-
"gitHead": "
|
|
32
|
+
"gitHead": "9a1b104864c08f3e35b009f191889e3308e3eeb0"
|
|
33
33
|
}
|