@rpcbase/client 0.46.0 → 0.48.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/get_object_id.js +35 -0
- package/package.json +1 -1
- package/rts/store/index.js +0 -5
package/get_object_id.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/* @flow */
|
|
2
|
+
import assert from "assert"
|
|
3
|
+
|
|
4
|
+
import isHexadecimal from "validator/lib/isHexadecimal"
|
|
5
|
+
|
|
6
|
+
import {TENANT_PREFIX} from "env"
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
const get_random_bytes = (n) => {
|
|
10
|
+
let array = new Uint8Array(n)
|
|
11
|
+
window.crypto.getRandomValues(array)
|
|
12
|
+
return array
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
// Validation
|
|
16
|
+
// is defined
|
|
17
|
+
assert(TENANT_PREFIX, "expected TENANT_PREFIX to be defined")
|
|
18
|
+
// is hexadecimal
|
|
19
|
+
assert(isHexadecimal(TENANT_PREFIX), "expected TENANT_PREFIX to be a hexadecimal")
|
|
20
|
+
// is 4 bytes
|
|
21
|
+
assert(TENANT_PREFIX.length === 8, "TENANT_PREFIX must be exactly bytes long ie: 8 hex chars")
|
|
22
|
+
// is lower than
|
|
23
|
+
const max_val = parseInt("6387427d", 16)
|
|
24
|
+
const tenant_prefix_int = parseInt(TENANT_PREFIX, 16)
|
|
25
|
+
assert(max_val - tenant_prefix_int > 0, "TENANT_PREFIX must be lower than 6387427d")
|
|
26
|
+
|
|
27
|
+
// generates a 12 bytes mongodb object id using the org id prefix or custom customer id
|
|
28
|
+
const get_object_id = () => {
|
|
29
|
+
const random_bytes = get_random_bytes(8)
|
|
30
|
+
const random_hex = Buffer.from(random_bytes).toString("hex")
|
|
31
|
+
const obj_id = `${TENANT_PREFIX}${random_hex}`
|
|
32
|
+
return obj_id
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export default get_object_id
|
package/package.json
CHANGED
package/rts/store/index.js
CHANGED
|
@@ -124,12 +124,9 @@ const log = debug("rb:store")
|
|
|
124
124
|
const run_query = async({model_name, query, query_key, options}, callback) => {
|
|
125
125
|
// console.log("ALAAARM")
|
|
126
126
|
// console.log("run_query", {model_name, query, query_key, options})
|
|
127
|
-
|
|
128
127
|
// console.time("store run_query")
|
|
129
|
-
|
|
130
128
|
const col = get_collection(model_name)
|
|
131
129
|
|
|
132
|
-
|
|
133
130
|
// https://github.com/pouchdb/pouchdb/tree/master/packages/node_modules/pouchdb-find#dbcreateindexindex--callback
|
|
134
131
|
const {docs} = await col.find({
|
|
135
132
|
// TODO: we should check if the selectors are compatible here
|
|
@@ -139,7 +136,6 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
|
|
|
139
136
|
// fields: [""]
|
|
140
137
|
})
|
|
141
138
|
|
|
142
|
-
|
|
143
139
|
const mapped_docs = docs.map(({_rev, ...doc}) => {
|
|
144
140
|
// TODO: handle projections here
|
|
145
141
|
const remapped_doc = Object.entries(doc).reduce((new_doc, [key, value]) => {
|
|
@@ -151,7 +147,6 @@ const run_query = async({model_name, query, query_key, options}, callback) => {
|
|
|
151
147
|
return remapped_doc
|
|
152
148
|
})
|
|
153
149
|
|
|
154
|
-
// console.log("FOUND INITIAL", mapped_docs)
|
|
155
150
|
// console.timeEnd("store run_query")
|
|
156
151
|
|
|
157
152
|
callback(null, mapped_docs, {source: "cache"})
|