Skip to content

Commit 903bdbb

Browse files
author
Alex Gaetano Padula
committed
CursusDB v1.0.0 update, tested and working great.
1 parent aa6c3c0 commit 903bdbb

File tree

3 files changed

+43
-53
lines changed

3 files changed

+43
-53
lines changed

main.js

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -17,65 +17,51 @@
1717
* You should have received a copy of the GNU General Public License
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
20-
import net from 'net'
20+
import net from 'node:net'
2121
import tls from 'tls'
2222

23+
// CursusDB Cluster Client class
24+
class Client {
25+
constructor(host, port, username, password, tls) {
26+
this.host = host;
27+
this.port = port;
28+
this.username = username;
29+
this.password = password;
30+
this.tls = tls;
31+
}
2332

24-
const cluster = {
25-
tls: false,
26-
socket: undefined,
27-
connect: connect,
28-
query: query,
29-
close: close,
30-
}
31-
/* Connect
32-
** host - cluster host
33-
** port - cluster port
34-
** username - database user username
35-
** password - database user password
36-
** tls bool
37-
*/
38-
async function connect(host, port, username, password, tlsIn) {
39-
cluster.tls = tlsIn
40-
cluster.socket = tlsIn ? new tls.TLSSocket() : new net.Socket()
33+
Connect() {
4134
return new Promise((resolve, reject) => {
42-
cluster.socket.connect(port, host, function() {
43-
cluster.socket.write("Authentication: " + Buffer.from(username + "\\0" + password).toString('base64') +"\r\n");
35+
this.socket = (this.tls ? tls : net).createConnection({ host: this.host, port: this.port }, () => {
36+
this.socket.write("Authentication: " + Buffer.from( this.username + "\\0" + this.password).toString('base64') +"\r\n");
4437

45-
cluster.socket.on('data', function (data) {
38+
this.socket.on('data', function (data) {
4639
if (data.toString().startsWith("0")) {
47-
resolve(cluster)
40+
resolve("Connected to CursusDB cluster successfully.")
4841
} else {
4942
reject(data.toString())
5043
}
5144
});
52-
53-
});
54-
55-
})
45+
});
46+
})
47+
}
5648

57-
58-
59-
60-
}
61-
62-
async function query(queryString) {
49+
Query(queryString) {
6350
return new Promise((resolve, reject) => {
64-
cluster.socket.write(queryString +"\r\n");
51+
this.socket.write(queryString +"\r\n");
6552

66-
cluster.socket.on('data', function (data) {
53+
this.socket.on('data', function (data) {
6754
resolve(data.toString())
6855
});
6956

7057
})
71-
72-
}
58+
}
7359

74-
async function close() {
75-
cluster.socket.end()
76-
77-
}
60+
Close() {
61+
this.socket.end()
62+
}
7863

64+
}
7965

8066

81-
export default cluster
67+
export default Client

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "cursusdb-node",
3-
"version": "0.1.0",
3+
"version": "1.0.0",
44
"description": "CursusDB NodeJS native client.",
55
"main": "main.js",
66
"type": "module",

readme.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
## CursusDB Node.JS Native Client Module
22

33
## How to use
4-
```
5-
import { cluster } from 'cursusdb-node'
4+
The Client class takes a cluster fqdn or ip, port, db user username, db user password, and whether you want TLS true or false(cluster must have tls enabled).
65

7-
// cluster host, cluster port, db user username, db user password, tls enabled
8-
cluster.connect("0.0.0.0", "7681", "username", "password", false).then(async (cluster) => {
9-
const results = await cluster.query("select * from users;")
6+
```
7+
import Client from 'cursusdb-node'
108
11-
console.log(results)
9+
(async function() {
10+
const client = new Client("0.0.0.0", "7681", "username", "password", false)
1211
13-
// Always close up shop
14-
cluster.close()
12+
client.Connect().then((res) => {
13+
console.log(res)
1514
16-
}).catch((err) => {
17-
console.log(err)
18-
})
15+
client.Query(`ping;`).then((res) => {
16+
console.log(res)
17+
client.Close()
18+
})
19+
}).catch((err) => {
20+
console.error(err)
21+
})
22+
})()
1923
2024
```

0 commit comments

Comments
 (0)