-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket-to-from-json.js
136 lines (115 loc) · 5.1 KB
/
bucket-to-from-json.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const SECRETS_FILE = '/srv/secret.env';
const fs = require('fs');
const dotenv = require('dotenv'); //for storing secrets in an env file
dotenv.config({ path: SECRETS_FILE }) //bot API key and other info
// const subdir = 'prod';
const subdir = 'qa';
// const subdir = process.env.BUCKET_FOLDER;
// const JSON_DB_FILE = '/srv/prod-data.json';
const JSON_DB_FILE = '/srv/qa-data.json';
const { JsonDB, Config } = require('node-json-db');
const BucketDB = require('./bucket-db');
// The first argument is the database filename. If no extension is used, '.json' is assumed and automatically added.
// The second argument is used to tell the DB to save after each push
// If you set the second argument to false, you'll have to call the save() method.
// The third argument is used to ask JsonDB to save the database in a human readable format. (default false)
// The last argument is the separator. By default it's slash (/)
const jdb = new JsonDB(new Config(JSON_DB_FILE, false, true, '/'));
const bdb = new BucketDB(process.env.BUCKET_ACCESS_KEY, process.env.BUCKET_SECRET_KEY, process.env.BUCKET_ENDPOINT, process.env.BUCKET_NAME, subdir);
async function bucketToJson() {
await jdb.load();
const all = await bdb.find_with_values();
console.log(all);
for (const key of Object.keys(all)) {
// Object.keys(all).forEach(async key => {
const value = all[key];
// try {
// value = JSON.parse(value)
// } catch (err) {
// console.error('error, falling back to raw string value:', err);
// }
console.log('PUSH', '/' + key, value);
await jdb.push('/' + key, value);
}
await jdb.save();
// await jdb.load();
// const keys = await bdb.find();
// console.log(keys);
// for (const key of keys) {
// const value = await bdb.get(key);
// console.log('PUSH', '/' + key, value);
// await jdb.push('/' + key, value);
// }
// await jdb.save();
}
async function jsonToBucket() {
const data = await jdb.getData('/');
jsonToBucketAux('', data);
}
async function jsonToBucketAux(path, data) {
console.log('path:', path);
Object.keys(data).forEach(key => {
const value = data[key];
console.log(' ', key, better_typeof(value));
if (better_typeof(value) === 'Object') {
jsonToBucketAux(path + '/' + key, value);
} else {
console.log(' ', 'SET', path + '/' + key, value);
bdb.set(path + '/' + key, value);
}
});
}
function better_typeof(data) {
if (data?.constructor === Array) return 'Array';
if (data?.constructor === Object) return 'Object';
return typeof (data); //for example, 'string', 'number', 'undefined', etc.
}
async function clearBucket() {
const all = await bdb.find();
console.log('old bucket data:', all);
for (const item of all) {
await bdb.delete(item);
}
}
async function clearJson() {
fs.writeFileSync(JSON_DB_FILE, JSON.stringify({}));
}
(async () => {
const start = + new Date();
// const all = await bdb.find();
// console.log('find', all);
// // all.forEach(item => bdb.delete(item));
// await bdb.set('test/123.txt', 'abcd');
// console.log(await bdb.get('test/1234.txt'));
// await bdb.delete('test/123.txt');
// console.log(await bdb.get('test/123.txt', 'geese'));
// console.log('find', await bdb.find());
// console.log('list', await bdb.list('channels', false));
// console.log('list', await bdb.list('channels/jjvanvan', false));
// console.log('list', await bdb.list('channels/jjvanvan', true));
// console.log('list', await bdb.list('channels/jjvanvan/nickname', true));
// console.log('find', await bdb.find('channels/jjvanvan'));
// console.log('list', await bdb.list('channels/jjvanvan'));
// console.log('list', await bdb.list('channels/jjvanvan/nickname'));
// console.log(await bdb.get('channels/jjvanvan/enabled'));
// console.log(await bdb.get('channels/jjvanvan/nickname/JJvanTheMan'));
// // NOTE: find_with_values is very slow and gets slower with more data in the bucket
// console.log(await bdb.find_with_values());
// // NOTE: list_with_values is somewhat slow and gets slower with more data in the folder being listed
// console.log(await bdb.list_with_values());
// console.log(await bdb.list_with_values('channels'));
// console.log(await bdb.list_with_values('channels/jjvanvan'));
// console.log(await bdb.list_with_values('channels/jjvanvan/nickname'));
// let val = [1,2,3];
// await bdb.set('test/123.txt', val);
// console.log(await bdb.get('test/123.txt'));
// await bdb.delete('test/123.txt');
// console.log(val);
// await clearBucket(); //warning: will delete everything in the bucket
// console.log(+ new Date() - start);
// await jsonToBucket(); //warning: will overwrite what is already in the bucket
await clearJson(); //warning: will delete everything in the json file
console.log(+ new Date() - start);
await bucketToJson(); //warning: will overwrite what is already in the json file
console.log(+ new Date() - start);
})();