@rpcbase/client 0.93.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.
@@ -35,7 +35,7 @@ const SignUp = ({
35
35
  set_tenant_id(user_id.slice(0, 8))
36
36
  set_is_signed_in(true)
37
37
 
38
- // we must now reconnect on the websocket as we now have a new cookie
38
+ // we must now reconnect on the websocket as we have a new cookie
39
39
  rts_reconnect()
40
40
  onSuccess()
41
41
  page(onSuccessRedirect)
package/auth/index.js CHANGED
@@ -107,10 +107,17 @@ export const session_restrict = (ctx, next) => {
107
107
 
108
108
 
109
109
  export const get_uid = () => __user_id
110
- export const set_uid = (val) => __user_id = val
110
+ export const set_uid = (val) => {
111
+ __user_id = val
112
+ const tenant_id_prefix = val.slice(0, 8)
113
+ localStorage.setItem(uid_storage_key(tenant_id_prefix), val)
114
+ }
111
115
 
112
116
  export const get_tenant_id = () => __tenant_id
113
- export const set_tenant_id = (val) => __tenant_id = val
117
+ export const set_tenant_id = (val) => {
118
+ __tenant_id = val
119
+ localStorage.setItem(LAST_TENANT_KEY, val)
120
+ }
114
121
 
115
122
  export const set_is_signed_in = (val) => __is_authenticated = val
116
123
 
package/auth/sign_out.js CHANGED
@@ -1,9 +1,10 @@
1
1
  /* @flow */
2
2
 
3
- import {set_is_signed_in, set_uid} from "./index"
3
+ import {set_is_signed_in, set_uid, set_tenant_id} from "./index"
4
4
 
5
5
  export const sign_out = () => {
6
6
  set_uid(null)
7
+ set_tenant_id(null)
7
8
  set_is_signed_in(false)
8
9
  localStorage.clear()
9
10
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.93.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)
@@ -177,7 +176,6 @@ export const register_query = (model_name, query, _options, _callback) => {
177
176
 
178
177
  log("register_query", {model_name, query, options, callback})
179
178
 
180
-
181
179
  if (!_socket) {
182
180
  log("register_query: trying to use null socket", {model_name, query})
183
181
  return
@@ -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