@rpcbase/client 0.59.0 → 0.61.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/base_url.js CHANGED
@@ -11,6 +11,5 @@ const is_local = ["localhost", "127.0.0.1"].includes(APP_DOMAIN)
11
11
 
12
12
  const BASE_URL = is_local ? `${protocol}//${APP_DOMAIN}:${SERVER_PORT}` : `https://${APP_DOMAIN}`
13
13
 
14
- console.log("APP_DOMAIN", APP_DOMAIN, BASE_URL)
15
14
 
16
15
  export default BASE_URL
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.59.0",
3
+ "version": "0.61.0",
4
4
  "scripts": {
5
5
  "test": "echo \"Error: no test specified\" && exit 0"
6
6
  },
@@ -0,0 +1,23 @@
1
+ /* @flow */
2
+ import getUseQuery from "./getUseQuery"
3
+
4
+
5
+ const getUseDocument = (register_query) => (...args) => {
6
+
7
+ const useQuery = getUseQuery(register_query)
8
+
9
+ const res = useQuery(...args)
10
+
11
+ let data
12
+ // cache will always return [] when there are no matching documents, but we want null instead
13
+ if (Array.isArray(res.data) && res.data.length === 0 && res.source === "cache") {
14
+ data = null
15
+ } else if (Array.isArray(res.data) && res.data.length > 0) {
16
+ // TODO: should we throw if res.data.length > 1 ? ie: there are more than one matching document
17
+ data = res.data[0]
18
+ }
19
+
20
+ return {...res, data}
21
+ }
22
+
23
+ export default getUseDocument
@@ -11,7 +11,8 @@ import get_uid from "../auth/get_uid"
11
11
  import cacheStorage from "./cacheStorage"
12
12
 
13
13
 
14
- const log = debug("rb:useQuery")
14
+ const log = debug("rb:rts:useQuery")
15
+
15
16
 
16
17
  const getUseQuery = (register_query) => (
17
18
  model_name,
@@ -44,10 +45,8 @@ const getUseQuery = (register_query) => (
44
45
  sort = {},
45
46
  } = options
46
47
 
47
-
48
48
  const storageKey = `${uid}.${key}.${model_name}.${JSON.stringify(query)}.${JSON.stringify(projection)}.${JSON.stringify(sort)}`
49
49
 
50
-
51
50
  useEffect(() => {
52
51
  if (options.debug) {
53
52
  console.log("use query", model_name, query, options)
@@ -57,9 +56,10 @@ const getUseQuery = (register_query) => (
57
56
  useEffect(() => {
58
57
  const load = async() => {
59
58
  const val = await cacheStorage.get(storageKey)
60
- // console.log("got val from storage", val)
59
+ // TODO: rm this
61
60
  // always initially apply when first load here
62
61
  if (val) {
62
+ console.log("Will set val from cache storage")
63
63
  setData(val)
64
64
  setLoading(false)
65
65
  }
@@ -94,6 +94,7 @@ const getUseQuery = (register_query) => (
94
94
  }
95
95
 
96
96
  const start = Date.now()
97
+ log("will register query", model_name, query)
97
98
  const unsubscribe = register_query(model_name, query, {...options, key: queryKey}, (err, queryResult, context) => {
98
99
  log("callback answer with context", context, queryResult?.length)
99
100
 
@@ -170,6 +171,11 @@ const getUseQuery = (register_query) => (
170
171
 
171
172
  const result = useMemo(() => ({data, source, error, loading}), [data, source, error, loading])
172
173
 
174
+ // TODO: investigate this one
175
+ // if (Array.isArray(result.data) && !result.source) {
176
+ // console.warn("RESULT HAS NO SOURCE", {data, error, loading, source})
177
+ // }
178
+
173
179
  return result
174
180
  }
175
181
 
package/rts/index.js CHANGED
@@ -9,6 +9,7 @@ import BASE_URL from "../base_url"
9
9
  import {get_tenant_id} from "../auth"
10
10
 
11
11
  import store from "./store"
12
+ import getUseDocument from "./getUseDocument"
12
13
  import getUseQuery from "./getUseQuery"
13
14
 
14
15
  const log = debug("rb:socket")
@@ -219,3 +220,5 @@ export const register_query = (model_name, query, _options, _callback) => {
219
220
  }
220
221
 
221
222
  export const useQuery = getUseQuery(register_query)
223
+
224
+ export const useDocument = getUseDocument(register_query)
@@ -2,7 +2,7 @@
2
2
  import debug from "debug"
3
3
  import PouchDB from "pouchdb-core"
4
4
 
5
- if (debug.enabled("rb:store")) {
5
+ if (debug.enabled("rb:rts:store")) {
6
6
 
7
7
  const log = debug("rb:store:pouch")
8
8
 
@@ -6,7 +6,7 @@ import FindPlugin from "pouchdb-find"
6
6
 
7
7
  import {TENANT_PREFIX, DATABASE_NAME} from "env"
8
8
 
9
- const log = debug("rb:store")
9
+ const log = debug("rb:rts:store")
10
10
 
11
11
  let prefix = `rb/${TENANT_PREFIX}/`
12
12
 
@@ -6,7 +6,7 @@ import "./debug"
6
6
  import get_collection from "./get_collection"
7
7
  import update_docs from "./update_docs"
8
8
 
9
- const log = debug("rb:store")
9
+ const log = debug("rb:rts:store")
10
10
 
11
11
  // import updateDocument from "./updateDocument"
12
12
  // import {DATABASE_NAME} from "env"
@@ -122,9 +122,10 @@ const log = debug("rb:store")
122
122
  // TODO: should the store be in a worker or the main thread ?
123
123
 
124
124
  const run_query = async({model_name, query, query_key, options}, callback) => {
125
- // console.log("ALAAARM")
126
125
  // console.log("run_query", {model_name, query, query_key, options})
127
126
  // console.time("store run_query")
127
+
128
+ // TODO: we should prefix model_name with tenant_prefix + env_id
128
129
  const col = get_collection(model_name)
129
130
 
130
131
  // https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-find#dbcreateindexindex--callback
@@ -136,6 +137,8 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
136
137
  // fields: [""]
137
138
  })
138
139
 
140
+ // console.log("GOT QUERY RESULT", col, docs)
141
+
139
142
  const mapped_docs = docs.map(({_rev, ...doc}) => {
140
143
  // TODO: handle projections here
141
144
  const remapped_doc = Object.entries(doc).reduce((new_doc, [key, value]) => {