|
| 1 | +/* |
| 2 | +Copyright 2019-2020 vChain, Inc. |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +const dotenv = require('dotenv').config() |
| 15 | +const ImmudbClient = require('../lib/client') |
| 16 | + |
| 17 | +const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1' |
| 18 | +const IMMUDB_PORT = process.env.IMMUDB_PORT || 3322 |
| 19 | +const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb' |
| 20 | +const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb' |
| 21 | + |
| 22 | +ImmudbClient({ |
| 23 | + address: `${IMMUDB_HOST}:${IMMUDB_PORT}`, |
| 24 | + rootPath: 'rootfile' |
| 25 | + }, main) |
| 26 | + |
| 27 | +const rand = '1' |
| 28 | +const testDB = 'opsdb' |
| 29 | + |
| 30 | +async function main(err, cl) { |
| 31 | + if (err) { |
| 32 | + return console.log(err) |
| 33 | + } |
| 34 | + |
| 35 | + try { |
| 36 | + // login using the specified username and password |
| 37 | + let req = { username: IMMUDB_USER, password: IMMUDB_PWD } |
| 38 | + let res = await cl.login(req) |
| 39 | + console.log('success: login', res) |
| 40 | + |
| 41 | + // create database |
| 42 | + req = { database: testDB } |
| 43 | + res = await cl.createDatabase(req) |
| 44 | + console.log('success: createDatabase', res) |
| 45 | + |
| 46 | + // use database just created |
| 47 | + req = { database: testDB } |
| 48 | + res = await cl.useDatabase(req) |
| 49 | + console.log('success: useDatabase', res) |
| 50 | + |
| 51 | + // add new item having the specified key and value |
| 52 | + req = { key: rand, value: rand } |
| 53 | + res = await cl.set(req) |
| 54 | + console.log('success: set', res) |
| 55 | + const index = res.index |
| 56 | + |
| 57 | + // get item having the specified key |
| 58 | + req = { key: rand } |
| 59 | + res = await cl.get(req) |
| 60 | + console.log('success: get', res) |
| 61 | + |
| 62 | + // count keys having the specified value |
| 63 | + // in the database in use |
| 64 | + req = { keyPrefix: rand } |
| 65 | + res = await cl.count(req) |
| 66 | + console.log('success: count', res) |
| 67 | + |
| 68 | + // increase occurences of items having the |
| 69 | + // same key |
| 70 | + for (let i = 0; i < 10; i++) { |
| 71 | + req = { key: rand, value: rand } |
| 72 | + res = await cl.set(req) |
| 73 | + } |
| 74 | + |
| 75 | + // count again keys having the specified value |
| 76 | + // in the database in use (result will be +10) |
| 77 | + req = { keyPrefix: rand } |
| 78 | + res = await cl.count(req) |
| 79 | + console.log('success: count', res) |
| 80 | + |
| 81 | + // iterate over keys having the specified |
| 82 | + // prefix |
| 83 | + req = { |
| 84 | + keyPrefix: rand, |
| 85 | + offset: '10', |
| 86 | + limit: 1, |
| 87 | + reverse: false, |
| 88 | + deep: false, |
| 89 | + } |
| 90 | + res = await cl.scan(req) |
| 91 | + console.log('success: scan', res) |
| 92 | + |
| 93 | + // return an element by index |
| 94 | + req = { index: index } |
| 95 | + res = await cl.byIndex(req) |
| 96 | + console.log('success: byIndex', res) |
| 97 | + |
| 98 | + // fetch history for the item having the |
| 99 | + // specified key |
| 100 | + req = { key: rand } |
| 101 | + res = await cl.history(req) |
| 102 | + console.log('success: history', res) |
| 103 | + |
| 104 | + // fetch paginated history for the item having the |
| 105 | + // specified key |
| 106 | + req = { |
| 107 | + set: rand, |
| 108 | + offset: 10, |
| 109 | + limit: 5, |
| 110 | + reverse: false, |
| 111 | + } |
| 112 | + res = await cl.history(req) |
| 113 | + console.log('success: paginated history', res) |
| 114 | + |
| 115 | + // iterate over a sorted set |
| 116 | + req = { |
| 117 | + set: rand, |
| 118 | + offset: '10', |
| 119 | + limit: 5, |
| 120 | + reverse: false, |
| 121 | + } |
| 122 | + res = await cl.zScan(req) |
| 123 | + console.log('success: zScan', res) |
| 124 | + |
| 125 | + // iterate over all elements by insertion order |
| 126 | + req = { pageSize: 1, pageNumber: 1 } |
| 127 | + res = await cl.iScan(req) |
| 128 | + console.log('success: iScan', res) |
| 129 | + |
| 130 | + // execute a batch read |
| 131 | + req = { |
| 132 | + keys: [{ |
| 133 | + key: rand, |
| 134 | + }], |
| 135 | + } |
| 136 | + res = await cl.getBatch(req) |
| 137 | + console.log('success: getBatch', res) |
| 138 | + |
| 139 | + // check immudb health status |
| 140 | + res = await cl.health() |
| 141 | + console.log('success: health', res) |
| 142 | + |
| 143 | + // get current root info |
| 144 | + res = await cl.currentRoot() |
| 145 | + console.log('success: currentRoot', res) |
| 146 | + |
| 147 | + // safely add new item having the specified key and value |
| 148 | + req = { |
| 149 | + key: rand+10, |
| 150 | + value: rand+10, |
| 151 | + } |
| 152 | + res = await cl.safeSet(req) |
| 153 | + console.log('success: safeSet', res) |
| 154 | + |
| 155 | + // get current root info |
| 156 | + res = await cl.currentRoot() |
| 157 | + console.log('success: currentRoot', res) |
| 158 | + |
| 159 | + // safely add new item having the specified key and value |
| 160 | + req = { |
| 161 | + key: rand+11, |
| 162 | + value: rand+11, |
| 163 | + } |
| 164 | + res = await cl.safeSet(req) |
| 165 | + console.log('success: safeSet', res) |
| 166 | + |
| 167 | + // safely add new item having the specified key and value |
| 168 | + req = { |
| 169 | + key: rand+12, |
| 170 | + value: rand+12, |
| 171 | + } |
| 172 | + res = await cl.safeSet(req) |
| 173 | + console.log('success: safeSet', res) |
| 174 | + |
| 175 | + // safely get item by key |
| 176 | + req = { |
| 177 | + key: rand+12, |
| 178 | + } |
| 179 | + res = await cl.safeGet(req) |
| 180 | + console.log('success: safeGet', res) |
| 181 | + |
| 182 | + } catch (err) { |
| 183 | + console.log(err) |
| 184 | + } |
| 185 | +} |
0 commit comments