snowbase 4.1.0 → 5.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.upm/store.json +1 -0
- package/README.md +30 -38
- package/index.js +5 -4
- package/package.json +6 -11
- package/src/Local.js +45 -0
- package/src/Server.js +121 -0
- package/src/index.js +4 -0
- package/src/database.js +0 -67
- package/src/errors.js +0 -10
package/.upm/store.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":2,"languages":{"nodejs-npm":{"specfileHash":"5d5decf1c69e3a2b4980b0627502cc86","lockfileHash":"603978d5629b271eba01c863d286f4e7","guessedImports":["node-fetch"],"guessedImportsHash":"56c1dadb55ef9ad867462f97e0673488"}}}
|
package/README.md
CHANGED
|
@@ -1,39 +1,31 @@
|
|
|
1
|
-
#
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
const db = new
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
db.remove(
|
|
31
|
-
```
|
|
32
|
-
|
|
33
|
-
> COMO DIVIDIR ELEMENTOS EM PASTAS
|
|
34
|
-
|
|
35
|
-
```js
|
|
36
|
-
const snow = require('snowbase');
|
|
37
|
-
const db = new snow.database('./snowbase.json')
|
|
38
|
-
db.get('testes/teste1')// null
|
|
1
|
+
# Snowbase ❄️
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
- #### [Alpha Discord](https://www.npmjs.com/package/alpha-discord) - A library for interact with discord [API](https://discord.com/api) on js
|
|
5
|
+
- ## Server
|
|
6
|
+
```js
|
|
7
|
+
const snowbase = require('snowbase');
|
|
8
|
+
|
|
9
|
+
const db = new snowbase.Server({
|
|
10
|
+
login: 'Your account login of snowbase',
|
|
11
|
+
password: 'Your password'
|
|
12
|
+
});
|
|
13
|
+
db.save('value1', 150)//emits valueUpdate event: key, {old: old_value, new: new_value}
|
|
14
|
+
|
|
15
|
+
db.get('value1')//150
|
|
16
|
+
|
|
17
|
+
db.remove('value1')//emits valueDelete event: key, deleted_value
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
- ## Local
|
|
21
|
+
```js
|
|
22
|
+
const db = new snowbase.Local('./database.json')//or other file
|
|
23
|
+
|
|
24
|
+
db.save('value2', 100)
|
|
25
|
+
|
|
26
|
+
db.get('value2')//100
|
|
27
|
+
|
|
28
|
+
db.all(a => a == 100)//filter
|
|
29
|
+
|
|
30
|
+
db.remove('value2')
|
|
39
31
|
```
|
package/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
module.exports =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
/*module.exports*/const data = require('./src/index.js');
|
|
2
|
+
let db = new data.Server({
|
|
3
|
+
login: 'frozen_test',
|
|
4
|
+
password: 'PHSBP3012'
|
|
5
|
+
});
|
package/package.json
CHANGED
|
@@ -1,27 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "snowbase",
|
|
3
|
-
"version": "
|
|
4
|
-
"description": "
|
|
3
|
+
"version": "5.1.0",
|
|
4
|
+
"description": "Local and Global Server database using json",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"dependencies": {
|
|
7
|
-
"
|
|
7
|
+
"node-fetch": "^2.6.1"
|
|
8
8
|
},
|
|
9
9
|
"devDependencies": {},
|
|
10
10
|
"scripts": {
|
|
11
11
|
"test": "echo \"Error: no test specified\" && exit 1"
|
|
12
12
|
},
|
|
13
|
+
"keywords": [],
|
|
14
|
+
"author": "mr.frozenfire",
|
|
15
|
+
"license": "ISC",
|
|
13
16
|
"repository": {
|
|
14
17
|
"type": "git",
|
|
15
18
|
"url": "git+https://github.com/FrozenFireBR/snowbase.git"
|
|
16
19
|
},
|
|
17
|
-
"keywords": [
|
|
18
|
-
"json",
|
|
19
|
-
"JSON",
|
|
20
|
-
"database",
|
|
21
|
-
"local"
|
|
22
|
-
],
|
|
23
|
-
"author": "Mr. Frozen Fire",
|
|
24
|
-
"license": "ISC",
|
|
25
20
|
"bugs": {
|
|
26
21
|
"url": "https://github.com/FrozenFireBR/snowbase/issues"
|
|
27
22
|
},
|
package/src/Local.js
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
const {readFileSync, writeFileSync} = require('fs')
|
|
2
|
+
|
|
3
|
+
module.exports = class LocalDatabase {
|
|
4
|
+
constructor(path) {
|
|
5
|
+
if (!path) throw new Error('Invalid path')
|
|
6
|
+
this.path = path;
|
|
7
|
+
};
|
|
8
|
+
get(key) {
|
|
9
|
+
if (!key) throw new Error('Invalid key to get');
|
|
10
|
+
let to;
|
|
11
|
+
const fileToRead = readFileSync(this.path, 'utf8');
|
|
12
|
+
let data = JSON.parse(fileToRead);
|
|
13
|
+
return data[key];
|
|
14
|
+
};
|
|
15
|
+
remove(key) {
|
|
16
|
+
if (!key) throw new Error('Invalid key to delete');
|
|
17
|
+
const fileToRead = readFileSync(this.path, 'utf8');
|
|
18
|
+
let data = JSON.parse(fileToRead);
|
|
19
|
+
delete data[key];
|
|
20
|
+
data = JSON.stringify(data);
|
|
21
|
+
writeFileSync(this.path, data);
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
save(key, value) {
|
|
25
|
+
if (!value) throw new Error('Missing Params');
|
|
26
|
+
const fileToRead = readFileSync(this.path, 'utf8');
|
|
27
|
+
let data = JSON.parse(fileToRead);
|
|
28
|
+
data[key] = value;
|
|
29
|
+
data = JSON.stringify(data);
|
|
30
|
+
writeFileSync(this.path, data)
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
all(filter) {
|
|
34
|
+
const fileToRead = readFileSync(this.path, 'utf8');
|
|
35
|
+
let data = JSON.parse(fileToRead);
|
|
36
|
+
data = Object.entries(data).map(d => {
|
|
37
|
+
return {
|
|
38
|
+
key: d[0],
|
|
39
|
+
value: d[1]
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
if (filter) return data.filter(filter);
|
|
43
|
+
return data;
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/Server.js
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
const server = process.env.server;
|
|
2
|
+
const fetch = require('node-fetch');
|
|
3
|
+
const {EventEmitter} = require('events');
|
|
4
|
+
|
|
5
|
+
module.exports = class ServerDatabase extends EventEmitter {
|
|
6
|
+
constructor(options={}) {
|
|
7
|
+
super();
|
|
8
|
+
this.connected = false;
|
|
9
|
+
this.connectedAt = null;
|
|
10
|
+
if (options.type === 'alpha-discord') {
|
|
11
|
+
if (options.token) {
|
|
12
|
+
this.type = 'alpha-discord-bot';
|
|
13
|
+
this.token = options.token;
|
|
14
|
+
}
|
|
15
|
+
} else {
|
|
16
|
+
if (!options.login) throw new Error('Invalid key to connect');
|
|
17
|
+
if (!options.password) throw new Error('Invalid password to connect');
|
|
18
|
+
this.client = {
|
|
19
|
+
login: options.login,
|
|
20
|
+
password: options.password
|
|
21
|
+
};
|
|
22
|
+
this.type = 'personal'
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
this.connect();
|
|
26
|
+
};
|
|
27
|
+
async connect() {
|
|
28
|
+
let body = JSON.stringify(this);
|
|
29
|
+
const connection = await fetch(server + '/connect', {
|
|
30
|
+
method: 'POST',
|
|
31
|
+
headers: {
|
|
32
|
+
body
|
|
33
|
+
}
|
|
34
|
+
}).then(d => d.json());
|
|
35
|
+
if (connection.connected !== true) throw new Error('Cannot connect to the Server: '+ connection.message);
|
|
36
|
+
this.connected = true;
|
|
37
|
+
this.connectedAt = Date.now();
|
|
38
|
+
this.emit('connected');
|
|
39
|
+
return connection;
|
|
40
|
+
}
|
|
41
|
+
async get(key) {
|
|
42
|
+
if (!this.connected) throw new Error('Need to have a connection for database.');
|
|
43
|
+
if (typeof key != 'string') throw new Error('Key must be a string!');
|
|
44
|
+
|
|
45
|
+
const info = {
|
|
46
|
+
key,
|
|
47
|
+
connected: this.connected
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
if (this.type == 'alpha-discord-bot') {
|
|
51
|
+
info.type = this.type;
|
|
52
|
+
info.client = {
|
|
53
|
+
login: this.token
|
|
54
|
+
}
|
|
55
|
+
} else {
|
|
56
|
+
info.client = this.client;
|
|
57
|
+
}
|
|
58
|
+
const value = await fetch(server + '/get', {
|
|
59
|
+
method: 'POST',
|
|
60
|
+
headers: {
|
|
61
|
+
body: JSON.stringify(info)
|
|
62
|
+
}
|
|
63
|
+
}).then(a => a.json());
|
|
64
|
+
if (value.error) throw new Error('Cannot get response: ' + value.message)
|
|
65
|
+
return value.value;
|
|
66
|
+
};
|
|
67
|
+
async save(key, value) {
|
|
68
|
+
if (!this.connected) throw new Error('Need to have a connection for database.');
|
|
69
|
+
|
|
70
|
+
if (typeof value != 'string' || typeof key != 'string') throw new Error('Key and value must be a string');
|
|
71
|
+
|
|
72
|
+
const info = {
|
|
73
|
+
key,
|
|
74
|
+
value,
|
|
75
|
+
connected: this.connected
|
|
76
|
+
};
|
|
77
|
+
if (this.type == 'alpha-discord-bot') {
|
|
78
|
+
info.type = this.type;
|
|
79
|
+
info.client = {
|
|
80
|
+
login: this.token
|
|
81
|
+
}
|
|
82
|
+
} else {
|
|
83
|
+
info.client = this.client;
|
|
84
|
+
};
|
|
85
|
+
const save = await fetch(server + '/save', {
|
|
86
|
+
method: 'POST',
|
|
87
|
+
headers: {
|
|
88
|
+
body: JSON.stringify(info)
|
|
89
|
+
}
|
|
90
|
+
}).then(r => r.json());
|
|
91
|
+
if (save.error) throw new Error('Cannot save the value:' + save.message);
|
|
92
|
+
this.emit(save.eventName, save.eventContent[0], save.eventContent[1]);
|
|
93
|
+
return save;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
async remove(key) {
|
|
97
|
+
if (!this.connected) throw new Error('Need to have a connection for database.');
|
|
98
|
+
if (typeof key != 'string') throw new Error('Invalid key to remove.');
|
|
99
|
+
const info = {
|
|
100
|
+
key,
|
|
101
|
+
connected: this.connected
|
|
102
|
+
};
|
|
103
|
+
if (this.type == 'alpha-discord-bot') {
|
|
104
|
+
info.type = this.type;
|
|
105
|
+
info.client = {
|
|
106
|
+
login: this.token
|
|
107
|
+
}
|
|
108
|
+
} else {
|
|
109
|
+
info.client = this.client;
|
|
110
|
+
};
|
|
111
|
+
const remove = await fetch(server + '/remove', {
|
|
112
|
+
method: 'POST',
|
|
113
|
+
headers: {
|
|
114
|
+
body: JSON.stringify(info)
|
|
115
|
+
}
|
|
116
|
+
}).then(r => r.json());
|
|
117
|
+
if (remove.error) throw new Error('Cannot remove the value:' + remove.message);
|
|
118
|
+
this.emit(remove.eventName, remove.eventContent[0], remove.eventContent[1])
|
|
119
|
+
return remove;
|
|
120
|
+
}
|
|
121
|
+
}
|
package/src/index.js
ADDED
package/src/database.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
const fs = require('fs');
|
|
2
|
-
const error = require('./errors.js')
|
|
3
|
-
class Database {
|
|
4
|
-
constructor(file) {
|
|
5
|
-
this.database = file;
|
|
6
|
-
this.get = this.get;
|
|
7
|
-
this.save = this.save;
|
|
8
|
-
this.remove = this.remove;
|
|
9
|
-
}
|
|
10
|
-
get(str) {
|
|
11
|
-
if(!str) throw new error('You need to specify a database do get, if you need help go to discord.gg/AxcQf5Pf58');
|
|
12
|
-
|
|
13
|
-
const split = str.split("/");
|
|
14
|
-
|
|
15
|
-
const db = require(this.database);
|
|
16
|
-
|
|
17
|
-
let result = db;
|
|
18
|
-
|
|
19
|
-
for(let i = 0; i < split.length; i++) {
|
|
20
|
-
if(!result[split[i]]) break;
|
|
21
|
-
|
|
22
|
-
result = result[split[i]] || null
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
return result;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
save(get, set) {
|
|
29
|
-
if (!get)
|
|
30
|
-
throw new error(
|
|
31
|
-
`You need to specify a database to get, if you need help go to discord.gg/AxcQf5Pf58`
|
|
32
|
-
);
|
|
33
|
-
if (!set)
|
|
34
|
-
throw new error(
|
|
35
|
-
`You need to specify a value to set, if you need help go to discord.gg/AxcQf5Pf58`
|
|
36
|
-
);
|
|
37
|
-
const split = get.split("/");
|
|
38
|
-
|
|
39
|
-
let go = require(this.database);
|
|
40
|
-
|
|
41
|
-
for(let i = 0; i < split.length; i++) {
|
|
42
|
-
if(!go[split[i]]) break;
|
|
43
|
-
delete go[split[i]];
|
|
44
|
-
}
|
|
45
|
-
fs.writeFileSync(this.database, go);
|
|
46
|
-
}
|
|
47
|
-
remove(str) {
|
|
48
|
-
if (!str)
|
|
49
|
-
throw new error(
|
|
50
|
-
"You need to specify a database to delete, if you need help go to discord.gg/AxcQf5Pf58"
|
|
51
|
-
);
|
|
52
|
-
const split = str.split("/");
|
|
53
|
-
|
|
54
|
-
let go = require(this.database);
|
|
55
|
-
|
|
56
|
-
let result = go;
|
|
57
|
-
|
|
58
|
-
for(let i = 0; i < split.length; i++) {
|
|
59
|
-
if(!result[split[i]]) break;
|
|
60
|
-
|
|
61
|
-
delete go[split[i]];
|
|
62
|
-
}
|
|
63
|
-
fs.writeFileSync(this.database, go);
|
|
64
|
-
return true;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
module.exports = Database
|
package/src/errors.js
DELETED