|
| 1 | +import _ from "lodash"; |
| 2 | + |
| 3 | +/** |
| 4 | + * @returns string `q` without params, `q_params` with. |
| 5 | + */ |
| 6 | +const getFn = (base, p) => { |
| 7 | + if (hasParams(p)) { |
| 8 | + return `${base}_params` |
| 9 | + } |
| 10 | + return base; |
| 11 | +}; |
| 12 | + |
| 13 | +const hasParams = (p) => { |
| 14 | + return _.isObject(p) || _.isArray(p) |
| 15 | +}; |
| 16 | + |
| 17 | +const getParamKey = (p) => { |
| 18 | + if (_.isPlainObject(p)) { |
| 19 | + return "key_based"; |
| 20 | + } |
| 21 | + if (_.isArray(p)) { |
| 22 | + return "index_based"; |
| 23 | + } |
| 24 | + |
| 25 | + // This function should only be called when `params` is valid. |
| 26 | + throw Error("Invalid params"); |
| 27 | +}; |
| 28 | + |
| 29 | +const getRSet = async (rr, tx_id, base, q, p) => { |
| 30 | + if (p !== null && !hasParams(p)) { |
| 31 | + throw Error("Invalid type provided for params."); |
| 32 | + } |
| 33 | + |
| 34 | + const fn = `tx/${getFn(base, p)}`; |
| 35 | + |
| 36 | + const args = { |
| 37 | + tx_id, |
| 38 | + q |
| 39 | + }; |
| 40 | + |
| 41 | + if (hasParams(p)) { |
| 42 | + args[getParamKey(p)] = p; |
| 43 | + } |
| 44 | + |
| 45 | + const rset = await rr.send({ |
| 46 | + fn, |
| 47 | + args |
| 48 | + }); |
| 49 | + |
| 50 | + return new RSet(rset); |
| 51 | +}; |
| 52 | + |
| 53 | +class ReadTx { |
| 54 | + rr = null; |
| 55 | + txId = null; |
| 56 | + |
| 57 | + constructor(rr, txId) { |
| 58 | + this.rr = rr; |
| 59 | + this.txId = txId; |
| 60 | + } |
| 61 | + |
| 62 | + |
| 63 | + async q(q, p = null) { |
| 64 | + const {rr, txId: tx_id} = this; |
| 65 | + return getRSet(rr, tx_id, "q", q, p); |
| 66 | + } |
| 67 | + |
| 68 | + async read(q, p = null) { |
| 69 | + const {rr, txId: tx_id} = this; |
| 70 | + return getRSet(rr, tx_id, "read", q, p); |
| 71 | + } |
| 72 | + |
| 73 | + async commit() { |
| 74 | + const {rr, txId: tx_id} = this; |
| 75 | + |
| 76 | + const rset = await rr.send({ |
| 77 | + fn: "tx/commit", |
| 78 | + args: { |
| 79 | + tx_id |
| 80 | + } |
| 81 | + }); |
| 82 | + |
| 83 | + return new RSet(rset); |
| 84 | + } |
| 85 | + |
| 86 | + async rollback() { |
| 87 | + const {rr, txId: tx_id} = this; |
| 88 | + |
| 89 | + const rset = await rr.send({ |
| 90 | + fn: "tx/rollback", |
| 91 | + args: { |
| 92 | + tx_id |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + return new RSet(rset); |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | + |
| 101 | +class WriteTx { |
| 102 | + rr = null; |
| 103 | + txId = null; |
| 104 | + |
| 105 | + constructor(rr, txId) { |
| 106 | + this.rr = rr; |
| 107 | + this.txId = txId; |
| 108 | + } |
| 109 | + |
| 110 | + async q(q, p = null) { |
| 111 | + const {rr, txId: tx_id} = this; |
| 112 | + return getRSet(rr, tx_id, "q", q, p); |
| 113 | + } |
| 114 | + |
| 115 | + async read(q, p = null) { |
| 116 | + const {rr, txId: tx_id} = this; |
| 117 | + return getRSet(rr, tx_id, "read", q, p); |
| 118 | + } |
| 119 | + |
| 120 | + async write(q, p = null) { |
| 121 | + const {rr, txId: tx_id} = this; |
| 122 | + return getRSet(rr, tx_id, "write", q, p); |
| 123 | + } |
| 124 | + |
| 125 | + async commit() { |
| 126 | + const {rr, txId: tx_id} = this; |
| 127 | + |
| 128 | + const rset = await rr.send({ |
| 129 | + fn: "tx/commit", |
| 130 | + args: { |
| 131 | + tx_id |
| 132 | + } |
| 133 | + }); |
| 134 | + |
| 135 | + return new RSet(rset); |
| 136 | + } |
| 137 | + |
| 138 | + async rollback() { |
| 139 | + const {rr, txId: tx_id} = this; |
| 140 | + |
| 141 | + const rset = await rr.send({ |
| 142 | + fn: "tx/rollback", |
| 143 | + args: { |
| 144 | + tx_id |
| 145 | + } |
| 146 | + }); |
| 147 | + |
| 148 | + return new RSet(rset); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | + |
| 153 | +class RSet { |
| 154 | + res = null; |
| 155 | + rows_cache = null; |
| 156 | + |
| 157 | + constructor(res) { |
| 158 | + // @todo/low deep freeze object. |
| 159 | + this.res = res; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Allow JS destructuring syntax by using a getter. |
| 164 | + * - E.g. `const {rows} = rset;` |
| 165 | + * Only compute rows when requested. |
| 166 | + */ |
| 167 | + get rows() { |
| 168 | + if (this.rows_cache === null) { |
| 169 | + // Convert `[[1,2,3]]` into `[{x: 1, y: 2, z: 3}]`. |
| 170 | + const {col_names, rows: {data}} = this.res; |
| 171 | + const keys = col_names.map(({name}) => name); |
| 172 | + const o = []; |
| 173 | + |
| 174 | + for (const r of data) { |
| 175 | + const one = {}; |
| 176 | + |
| 177 | + for (const [i, v] of r.entries()) { |
| 178 | + one[keys[i]] = v; |
| 179 | + } |
| 180 | + o.push(one); |
| 181 | + } |
| 182 | + this.rows_cache = o; |
| 183 | + } |
| 184 | + return this.rows_cache; |
| 185 | + } |
| 186 | + |
| 187 | + get rowsAsArray() { |
| 188 | + return this.res.rows.data; |
| 189 | + } |
| 190 | + |
| 191 | + get cols() { |
| 192 | + return this.res.col_names.map(({name}) => name); |
| 193 | + } |
| 194 | + |
| 195 | + get colsOrigin() { |
| 196 | + return this.res.col_names.map(({name_origin}) => name_origin); |
| 197 | + } |
| 198 | + |
| 199 | + get rowsChanged() { |
| 200 | + const c = this.res.rows_changed; |
| 201 | + |
| 202 | + // `null` when the query is not an insert, update or delete. |
| 203 | + return c === null ? 0 : c; |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +export { |
| 208 | + ReadTx, |
| 209 | + WriteTx |
| 210 | +} |
| 211 | + |
0 commit comments