Skip to content

Commit 42c2636

Browse files
committed
chore: updated examples
1 parent 28f342c commit 42c2636

File tree

6 files changed

+414
-73
lines changed

6 files changed

+414
-73
lines changed

examples/auth-management.js

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
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()
115
const ImmudbClient = require("../lib/client")
216
const types = require('../lib/types')
317

18+
const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1'
19+
const IMMUDB_PORT = process.env.IMMUDB_PORT || 3322
20+
const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb'
21+
const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb'
22+
423
ImmudbClient({
5-
address: '127.0.0.1:3322',
24+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
625
}, main)
726

827
const rand = '' + Math.floor(Math.random()
@@ -14,20 +33,17 @@ async function main(err, cl) {
1433
}
1534

1635
try {
17-
let req = { username: 'immudb', password: 'immudb' }
36+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD }
1837
let res = await cl.login(req)
1938

2039
res = await cl.useDatabase({ database: 'defaultdb' })
21-
22-
console.log('useDatabase successfull, token:', res && res.token)
23-
24-
await cl.updateAuthConfig({ auth: types.auth.enabled })
25-
26-
console.log('updateAuthConfig')
40+
console.log('success: useDatabase:', res)
2741

28-
await cl.updateMTLSConfig({ enabled: false })
42+
res = await cl.updateAuthConfig({ auth: types.auth.enabled })
43+
console.log('success: updateAuthConfig')
2944

30-
console.log('updateMTLSConfig')
45+
res = await cl.updateMTLSConfig({ enabled: false })
46+
console.log('success: updateMTLSConfig')
3147

3248
} catch (err) {
3349
console.log(err)

examples/batch-operations.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,54 @@
11
const ImmudbClient = require('../lib/client')
2-
const root = require('../lib/root')
2+
3+
const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1'
4+
const IMMUDB_PORT = process.env.IMMUDB_PORT || 3322
5+
const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb'
6+
const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb'
37

48
ImmudbClient({
5-
address: '127.0.0.1:3322',
6-
}, main)
9+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
10+
}, main)
711

812
const rand = '' + Math.floor(Math.random()
913
* Math.floor(100000))
10-
let unix = Math.floor(Date.now()/1000)
1114

1215
async function main(err, cl) {
1316
if (err) {
1417
return console.log(err)
1518
}
1619

1720
try {
18-
let req = { username: 'immudb', password: 'immudb' }
21+
// login using the specified username and password
22+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD }
1923
let res = await cl.login(req)
24+
console.log('success: login', res)
2025

21-
res = await cl.useDatabase({ database: 'defaultdb' })
26+
// create database
27+
req = { database: rand }
28+
res = await cl.createDatabase(req)
29+
console.log('success: createDatabase', res)
2230

23-
// set batch ops
31+
// use database just created
32+
req = { database: rand }
33+
res = await cl.useDatabase(req)
34+
console.log('success: useDatabase', res)
35+
36+
// execute a batch insert
37+
req = { keys: [] }
2438
for (let i = 0; i < 20; i++) {
25-
req = {
26-
kvList : [
27-
{ key: i, value: i },
28-
]
29-
}
30-
res = await cl.setBatch(req)
31-
console.log(res)
39+
req.keys.push({ key: i, value: i })
3240
}
41+
res = await cl.setBatch(req)
42+
console.log(`success: setBatch`, res)
3343

34-
// get batch ops
44+
// execute a batch read
45+
req = { keys: [] }
3546
for (let i = 0; i < 20; i++) {
36-
req = {
37-
kvList : [
38-
{ key: i },
39-
]
40-
}
41-
res = await cl.getBatch(req)
42-
console.log(res)
43-
}
47+
req.keys.push({ key: i })
48+
}
49+
res = await cl.getBatch(req)
50+
console.log(`success: getBatch`, res)
51+
4452
} catch (err) {
4553
console.log(err)
4654
}

examples/database-management.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
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()
115
const util = require('util')
2-
316
const ImmudbClient = require('../lib/client')
417

18+
const IMMUDB_HOST = process.env.IMMUDB_HOST || '127.0.0.1'
19+
const IMMUDB_PORT = process.env.IMMUDB_PORT || 3322
20+
const IMMUDB_USER = process.env.IMMUDB_USER || 'immudb'
21+
const IMMUDB_PWD = process.env.IMMUDB_PWD || 'immudb'
22+
523
ImmudbClient({
6-
address: '127.0.0.1:3322',
7-
}, main)
24+
address: `${IMMUDB_HOST}:${IMMUDB_PORT}`,
25+
}, main)
826

927
const rand = '' + Math.floor(Math.random()
1028
* Math.floor(100000))
@@ -15,21 +33,27 @@ async function main(err, cl) {
1533
}
1634

1735
try {
18-
let req = { username: 'immudb', password: 'immudb' }
36+
let req = { username: IMMUDB_USER, password: IMMUDB_PWD }
1937
let res = await cl.login(req)
2038

2139
await cl.createDatabase({ database: rand })
40+
console.log('success: createDatabase', res);
2241

2342
res = await cl.useDatabase({ database: rand })
43+
console.log('success: useDatabase', res);
2444

2545
res = await cl.set({ key: rand, value: rand })
2646

2747
res = await cl.listDatabases()
48+
console.log('success: listDatabases', res);
49+
2850
console.log(util.inspect(res, false, 8, true))
2951

3052
res = await cl.printTree()
53+
console.log('success: printTree', res);
3154

3255
res = await cl.health()
56+
console.log('success: health', res);
3357

3458
} catch (err) {
3559
console.log(err)
Lines changed: 185 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,185 @@
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

Comments
 (0)