@rpcbase/client 0.115.0 → 0.116.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/rts/store/index.js +21 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/client",
3
- "version": "0.115.0",
3
+ "version": "0.116.0",
4
4
  "scripts": {
5
5
  "build-firebase": "webpack -c firebase/webpack.config.js",
6
6
  "build": "yarn build-firebase",
@@ -48,14 +48,16 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
48
48
 
49
49
  const replaced_query = replace_query_keys(query, (k) => (k.startsWith("_") && k !== "_id") ? `${UNDERSCORE_PREFIX}${k}` : k)
50
50
 
51
- // console.time(`query-${model_name}`)
52
- // https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-find#dbcreateindexindex--callback
53
- const {docs} = await collection.find({
51
+ const find_query = {
54
52
  selector: replaced_query,
55
53
  // DO NOT INCLUDE FIELDS HERE AS IT USES PICK AND WE WOULD GO through the list twice
56
54
  // https://github.com/pouchdb/pouchdb/blob/master/packages/node_modules/pouchdb-find/src/adapters/local/find/index.js
57
55
  // fields: [""]
58
- })
56
+ }
57
+
58
+ // console.time(`query-${model_name}`)
59
+ // https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-find#dbcreateindexindex--callback
60
+ const {docs} = await collection.find(find_query)
59
61
  // console.timeEnd(`query-${model_name}`)
60
62
 
61
63
  let mapped_docs = docs
@@ -76,6 +78,21 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
76
78
  })
77
79
  }
78
80
 
81
+ if (options.sort) {
82
+ mapped_docs = mapped_docs.sort((a, b) => {
83
+ for (const key in options.sort) {
84
+ // Check if property exists on both objects
85
+ if (a.hasOwnProperty(key) && b.hasOwnProperty(key)) {
86
+ const dir = options.sort[key] // Direction of sorting: 1 or -1
87
+
88
+ if (a[key] < b[key]) return -1 * dir
89
+ if (a[key] > b[key]) return 1 * dir
90
+ }
91
+ }
92
+ return 0
93
+ })
94
+ }
95
+
79
96
  callback(null, mapped_docs, {source: "cache"})
80
97
  }
81
98