|
| 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 ImmudbClient = require('../lib/client') |
| 15 | + |
| 16 | +const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1' |
| 17 | +const IMMUDB_PORT = process.env.IMMUDB_PORT || 3322 |
| 18 | +const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb' |
| 19 | +const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb' |
| 20 | + |
| 21 | +ImmudbClient({ |
| 22 | + address: `${IMMUDB_HOST}:${IMMUDB_PORT}`, |
| 23 | + }, main) |
| 24 | + |
| 25 | +const rand = '' + Math.floor(Math.random() |
| 26 | + * Math.floor(100000)) |
| 27 | + |
| 28 | +async function main(err, cl) { |
| 29 | + if (err) { |
| 30 | + return console.log(err) |
| 31 | + } |
| 32 | + |
| 33 | + try { |
| 34 | + // login using the specified username and password |
| 35 | + let req = { username: IMMUDB_USER, password: IMMUDB_PWD } |
| 36 | + let res = await cl.login(req) |
| 37 | + console.log('success: login', res) |
| 38 | + |
| 39 | + // create database |
| 40 | + req = { database: rand } |
| 41 | + res = await cl.createDatabase(req) |
| 42 | + console.log('success: createDatabase', res) |
| 43 | + |
| 44 | + // use database just created |
| 45 | + req = { database: rand } |
| 46 | + res = await cl.useDatabase(req) |
| 47 | + console.log('success: useDatabase', res) |
| 48 | + |
| 49 | + // execute a batch insert |
| 50 | + req = { keys: [] } |
| 51 | + for (let i = 0; i < 20; i++) { |
| 52 | + req.keys.push({ key: i, value: i }) |
| 53 | + } |
| 54 | + res = await cl.setBatch(req) |
| 55 | + console.log(`success: setBatch`, res) |
| 56 | + |
| 57 | + // execute a batch read |
| 58 | + req = { keys: [] } |
| 59 | + for (let i = 0; i < 20; i++) { |
| 60 | + req.keys.push({ key: i }) |
| 61 | + } |
| 62 | + res = await cl.getBatch(req) |
| 63 | + console.log(`success: getBatch`, res) |
| 64 | + |
| 65 | + } catch (err) { |
| 66 | + console.log(err) |
| 67 | + } |
| 68 | +} |
0 commit comments