herodb-sdk 1.0.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/index.js +56 -0
- package/package.json +16 -0
package/index.js
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const net = require('net');
|
|
2
|
+
|
|
3
|
+
class HeroDBClient {
|
|
4
|
+
constructor(host, port) {
|
|
5
|
+
this.host = host;
|
|
6
|
+
this.port = port;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
async connect() {
|
|
10
|
+
return new Promise((resolve, reject) => {
|
|
11
|
+
this.client = net.createConnection({ host: this.host, port: this.port }, () => {
|
|
12
|
+
resolve();
|
|
13
|
+
});
|
|
14
|
+
this.client.on('error', (err) => {
|
|
15
|
+
reject(err);
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async sendRequest(request) {
|
|
21
|
+
return new Promise((resolve, reject) => {
|
|
22
|
+
const payload = JSON.stringify(request);
|
|
23
|
+
const buffer = Buffer.alloc(4 + payload.length);
|
|
24
|
+
buffer.writeUInt32BE(payload.length, 0);
|
|
25
|
+
buffer.write(payload, 4);
|
|
26
|
+
|
|
27
|
+
this.client.write(buffer);
|
|
28
|
+
|
|
29
|
+
this.client.once('data', (data) => {
|
|
30
|
+
const len = data.readUInt32BE(0);
|
|
31
|
+
const response = JSON.parse(data.slice(4, 4 + len).toString());
|
|
32
|
+
resolve(response);
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async insert(doc) {
|
|
38
|
+
return this.sendRequest({ op: 'insert', doc });
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async find(id) {
|
|
42
|
+
const req = { op: 'find' };
|
|
43
|
+
if (id) req._id = id;
|
|
44
|
+
return this.sendRequest(req);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async delete(id) {
|
|
48
|
+
return this.sendRequest({ op: 'delete', _id: id });
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
close() {
|
|
52
|
+
this.client.end();
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
module.exports = HeroDBClient;
|
package/package.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "herodb-sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official JavaScript SDK for HeroDB - The high-performance NoSQL database by Death Legion Team",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"herodb",
|
|
8
|
+
"nosql",
|
|
9
|
+
"database",
|
|
10
|
+
"distributed",
|
|
11
|
+
"death-legion",
|
|
12
|
+
"document-store"
|
|
13
|
+
],
|
|
14
|
+
"author": "Death Legion Team",
|
|
15
|
+
"license": "MIT"
|
|
16
|
+
}
|