weavedb-node-client 0.5.6 → 0.5.8

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/index.js +52 -6
  2. package/package.json +2 -2
package/index.js CHANGED
@@ -1,4 +1,4 @@
1
- const { all, complement, init, is, last, isNil } = require("ramda")
1
+ const { init, all, complement, isNil, is, last, includes } = require("ramda")
2
2
  const Base = require("weavedb-base")
3
3
  const PROTO_PATH = __dirname + "/weavedb.proto"
4
4
  const grpc = require("@grpc/grpc-js")
@@ -11,10 +11,38 @@ const packageDefinition = protoLoader.loadSync(PROTO_PATH, {
11
11
  oneofs: true,
12
12
  })
13
13
  const weavedb_proto = grpc.loadPackageDefinition(packageDefinition).weavedb
14
+ let Arweave = require("arweave")
15
+
16
+ const reads = [
17
+ "get",
18
+ "cget",
19
+ "getIndexes",
20
+ "getCrons",
21
+ "getSchema",
22
+ "getRules",
23
+ "getIds",
24
+ "getOwner",
25
+ "getAddressLink",
26
+ "getAlgorithms",
27
+ "getLinkedContract",
28
+ "getEvolve",
29
+ "getVersion",
30
+ ]
14
31
 
15
32
  class SDK extends Base {
16
- constructor({ rpc, contractTxId, wallet, name, version, EthWallet, web3 }) {
33
+ constructor({
34
+ rpc,
35
+ contractTxId,
36
+ wallet,
37
+ name,
38
+ version,
39
+ EthWallet,
40
+ web3,
41
+ arweave,
42
+ }) {
17
43
  super()
44
+ this.contractTxId = contractTxId
45
+ this.arweave = Arweave.init(arweave)
18
46
  this.client = new weavedb_proto.DB(rpc, grpc.credentials.createInsecure())
19
47
  if (typeof window === "object") {
20
48
  require("@metamask/legacy-web3")
@@ -30,10 +58,22 @@ class SDK extends Base {
30
58
  if (!isNil(EthWallet)) this.setEthWallet(EthWallet)
31
59
  }
32
60
 
33
- async _request(func, query) {
61
+ parseQuery(func, query) {
62
+ let nocache = false
63
+ if (includes(func)(reads) && is(Boolean, last(query))) {
64
+ nocache = last(query)
65
+ query = init(query)
66
+ }
67
+
68
+ return { nocache, query }
69
+ }
70
+
71
+ async _request(func, query, nocache = false) {
72
+ if (!includes(func)(reads)) nocache = false
34
73
  const request = {
35
- method: func,
74
+ method: `${func}@${this.contractTxId}`,
36
75
  query: JSON.stringify(query),
76
+ nocache,
37
77
  }
38
78
  const _query = () =>
39
79
  new Promise(ret => {
@@ -51,11 +91,17 @@ class SDK extends Base {
51
91
  })
52
92
  })
53
93
  let q = await _query()
54
- return q.result
94
+ if (isNil(q.err)) {
95
+ return q.result
96
+ } else {
97
+ throw new Error(q.err)
98
+ }
55
99
  }
56
100
 
57
101
  async request(func, ...query) {
58
- return await this._request(func, query)
102
+ let nocache = false
103
+ ;({ nocache, query } = this.parseQuery(func, query))
104
+ return await this._request(func, query, nocache)
59
105
  }
60
106
 
61
107
  async getNonce(addr) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "weavedb-node-client",
3
- "version": "0.5.6",
3
+ "version": "0.5.8",
4
4
  "main": "index.js",
5
5
  "license": "MIT",
6
6
  "engines": {
@@ -10,6 +10,6 @@
10
10
  "@grpc/grpc-js": "^1.1.0",
11
11
  "@grpc/proto-loader": "^0.5.0",
12
12
  "ramda": "^0.28.0",
13
- "weavedb-base": "^0.5.11"
13
+ "weavedb-base": "^0.7.3"
14
14
  }
15
15
  }