@rpcbase/client 0.94.0 → 0.95.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.94.0",
3
+ "version": "0.95.0",
4
4
  "scripts": {
5
5
  "build-firebase": "webpack -c firebase/webpack.config.js",
6
6
  "build": "yarn build-firebase",
package/rts/index.js CHANGED
@@ -27,7 +27,6 @@ const _local_txn = []
27
27
  // TODO: when server disconnects / crashes and loses all server side stored queries
28
28
  // the clients must reconnect and re-register those queries, or the page will need to be hard refreshed
29
29
 
30
-
31
30
  // add_local_txn
32
31
  // when a request is made to the server, we generate (or send if provided) the txn_id
33
32
  // if the array becomes longer than the default buffer length we shift the array
@@ -63,7 +62,7 @@ const dispatch_query_payload = (payload) => {
63
62
 
64
63
  let data
65
64
  try {
66
- // TODO: zstd / brotli decompression here
65
+ // TODO: zstd decompression here
67
66
  data = JSON.parse(payload.data_buf)
68
67
  } catch (err) {
69
68
  console.log("Error", err)
@@ -25,7 +25,8 @@ const get_collection = (col_name) => {
25
25
  if (_cols_store[col_name]) {
26
26
  return _cols_store[col_name]
27
27
  } else {
28
- const col = new PouchDB(col_name, { adapter: "indexeddb" });
28
+ // https://pouchdb.com/api.html#create_database
29
+ const col = new PouchDB(col_name, { adapter: "indexeddb", revs_limit: 1 })
29
30
  _cols_store[col_name] = col
30
31
 
31
32
  return col
@@ -29,8 +29,6 @@ const log = debug("rb:rts:store")
29
29
  //
30
30
  // fn.cancel()
31
31
  //
32
- // console.log("GOT FN", fn)
33
- //
34
32
  // console.log("got res", res)
35
33
  // _cols_store[col_name].getIndexes().then(function (result) {
36
34
  // console.log("got indexes", result)
@@ -70,7 +68,9 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
70
68
  })
71
69
 
72
70
  if (options.projection) {
73
- mapped_docs = mapped_docs.filter((doc) => satisfies_projection(doc, options.projection))
71
+ mapped_docs = mapped_docs.filter((doc) => {
72
+ return satisfies_projection(doc, options.projection)
73
+ })
74
74
  }
75
75
 
76
76
  callback(null, mapped_docs, {source: "cache"})
@@ -48,3 +48,11 @@ test("missing nested field", () => {
48
48
  test("empty", () => {
49
49
  expect(satisfies_projection(doc, {field1: 1})).toBe(true)
50
50
  })
51
+
52
+ test("no fields, _id only, empty projection", () => {
53
+ expect(satisfies_projection({_id: "1012"}, {})).toBe(true)
54
+ })
55
+
56
+ test("no fields, _id only, mismatching projection", () => {
57
+ expect(satisfies_projection({_id: "1012"}, {doesntexist: 1})).toBe(false)
58
+ })
@@ -7,38 +7,38 @@ const update_docs = async(model_name, data) => {
7
7
 
8
8
  const all_ids = data.map((doc) => doc._id)
9
9
 
10
- // console.log("will update cache", model_name, data, all_ids)
11
-
12
- // console.time("find")
13
-
14
- // TODO:
15
- // there is also a bulk get which could have different performance than find, try both
16
- // https://pouchdb.com/api.html#bulk_get
17
-
18
10
  // https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-find#dbcreateindexindex--callback
19
11
  const {docs: current_docs} = await collection.find({
20
12
  selector: {_id: {$in: all_ids}},
21
13
  fields: ["_id", "_rev"],
22
14
  })
23
15
 
24
- // console.log("current_docs", current_docs)
16
+ const current_docs_by_id = current_docs.reduce((acc, val) => {
17
+ acc[val._id] = val
18
+ return acc
19
+ }, {})
20
+
21
+ // WARNING: tmp
22
+ // we don't need to remove docs that aren't present in data because queries are refined with find
25
23
 
26
24
  const revs_map = {}
27
25
  current_docs.forEach((doc) => revs_map[doc._id] = doc._rev)
28
26
 
29
- const write_docs = data.map((mongo_doc) => {
30
- const op = Object.entries(mongo_doc).reduce((new_doc, [key, value]) => {
31
- let new_key = key !== "_id" && key.startsWith('_') ? `$_${key}` : key
32
- new_doc[new_key] = value
33
- return new_doc
34
- }, {})
27
+ const new_docs = data.map((mongo_doc) => {
28
+ const current_doc = current_docs_by_id[mongo_doc._id] || {}
29
+
30
+ const op = Object.entries(mongo_doc)
31
+ .reduce((new_doc, [key, value]) => {
32
+ let new_key = key !== "_id" && key.startsWith('_') ? `$_${key}` : key
33
+ new_doc[new_key] = value
34
+ return new_doc
35
+ }, current_doc)
35
36
 
36
37
  op._rev = revs_map[mongo_doc._id]
37
38
  return op
38
39
  })
39
40
 
40
- await collection.bulkDocs(write_docs)
41
- // console.timeEnd("find")
41
+ await collection.bulkDocs(new_docs)
42
42
  }
43
43
 
44
44
  export default update_docs