Skip to content

Commit 958de4c

Browse files
committed
pick commit
1 parent 29c845d commit 958de4c

13 files changed

Lines changed: 976 additions & 113 deletions

File tree

.prettierrc.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
bracketSpacing: true,
3+
bracketSameLine: false,
4+
singleQuote: true,
5+
jsxSingleQuote: true,
6+
trailingComma: 'all',
7+
semi: true,
8+
tabWidth: 2,
9+
useTabs: false,
10+
};

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"typescript.tsdk": "./node_modules/typescript/lib"
3+
}

examples/test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Config } from "../src"
2+
3+
async function test() {
4+
const config = new Config(
5+
'root',
6+
'admin123456123456',
7+
'127.0.0.1',
8+
2003
9+
)
10+
11+
const db = await config.connect()
12+
// await db.query('create space testmyspace')
13+
await db.query('USE testmyspace')
14+
// await db.query("create model testmyspace.user(username: string, password: string)")
15+
const res = await db.query(
16+
'SELECT all * FROM int limit ?',
17+
100
18+
)
19+
// await db.query(
20+
// 'insert into testmyspace.user(?, ?)',
21+
// 'test2',
22+
// `a123456`,
23+
// )
24+
// const res = await db.query(
25+
// 'SELECT * FROM mymodel where username = ?',
26+
// 'sayan6'
27+
// )
28+
29+
// const res = await db.query(
30+
// 'SELECT * FROM testmyspace.int where username = ?',
31+
// 10001n
32+
// )
33+
34+
console.log(res, 'result=========');
35+
36+
}
37+
38+
test()

package.json

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,34 @@
33
"version": "1.0.0",
44
"main": "dist/index.js",
55
"description": "Offical NodeJS client driver for Skytable",
6-
"repository": "https://github.com/skytable/client-nodejs.git",
76
"author": "Sayan Nandan <nandansayan@outlook.com>",
87
"license": "Apache-2.0",
9-
"devDependencies": {
10-
"typescript": "^5.3.3"
11-
},
8+
"repository": {
9+
"url": "https://github.com/skytable/client-nodejs"
10+
},
11+
"bugs": {
12+
"url": "https://github.com/skytable/client-nodejs/issues"
13+
},
1214
"scripts": {
13-
"build": "tsc"
15+
"build": "tsc",
16+
"test": "npx tsx examples/test.ts",
17+
"formatting": "prettier src --check",
18+
"prettier:fix": "prettier src --write"
1419
},
1520
"dependencies": {
1621
"@types/node": "^20.10.4"
17-
}
22+
},
23+
"devDependencies": {
24+
"nodemon": "^3.0.2",
25+
"prettier": "^3.1.1",
26+
"tsx": "^4.6.2",
27+
"typescript": "^5.3.3"
28+
},
29+
"publishConfig": {
30+
"access": "public"
31+
},
32+
"keywords": [
33+
"skytable",
34+
"skytable-client"
35+
]
1836
}

src/Config.ts

Lines changed: 0 additions & 56 deletions
This file was deleted.

src/Query.ts

Lines changed: 0 additions & 42 deletions
This file was deleted.

src/config.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
import {
2+
connectionWrite,
3+
createConnection,
4+
createConnectionTls,
5+
} from './connection';
6+
import { createSkytable } from './skytable';
7+
import { bufferToHandshakeResult, getClientHandshake } from './protocol';
8+
import type {
9+
ConnectionOptions as ConnectionTLSOptions,
10+
TLSSocket,
11+
} from 'node:tls';
12+
import { Socket } from 'node:net';
13+
14+
/**
15+
* Configuration for a client connection (single node)
16+
*/
17+
export class Config {
18+
private username: string;
19+
private password: string;
20+
private host: string;
21+
private port: number;
22+
private connection: Socket | TLSSocket | undefined;
23+
24+
/**
25+
* Create a new configuration
26+
*
27+
* @param username Set the username for authenticating with Skytable
28+
* @param password Set the password for authenticating with Skytable
29+
* @param host Set the host to connect to Skytable (defaults to `127.0.0.1`)
30+
* @param port Set the port to connect to Skytable (defaults to 2003)
31+
*/
32+
constructor(
33+
username: string,
34+
password: string,
35+
host: string = 'localhost',
36+
port: number = 2003,
37+
) {
38+
this.username = username;
39+
this.password = password;
40+
this.host = host;
41+
this.port = port;
42+
}
43+
44+
/**
45+
* Get the set username
46+
* @returns Username set in this configuration
47+
*/
48+
getUsername(): string {
49+
return this.username;
50+
}
51+
52+
/**
53+
* Get the set password
54+
* @returns Username set in this configuration
55+
*/
56+
getPassword(): string {
57+
return this.password;
58+
}
59+
60+
/**
61+
* Get the set host
62+
* @returns Username set in this configuration
63+
*/
64+
getHost(): string {
65+
return this.host;
66+
}
67+
68+
/**
69+
* Get the set port
70+
* @returns Username set in this configuration
71+
*/
72+
getPort(): number {
73+
return this.port;
74+
}
75+
76+
/**
77+
* Get the connection
78+
* @returns The current connection
79+
*/
80+
getConnection() {
81+
return this.connection;
82+
}
83+
84+
async connect() {
85+
const connect = await createConnection({
86+
port: this.port,
87+
host: this.host,
88+
});
89+
90+
const data = await connectionWrite(connect, getClientHandshake(this));
91+
92+
await bufferToHandshakeResult(data);
93+
94+
this.connection = connect;
95+
96+
return createSkytable(connect);
97+
}
98+
99+
async connectTSL(options: ConnectionTLSOptions) {
100+
const connect = await createConnectionTls({
101+
port: this.port,
102+
host: this.host,
103+
...options,
104+
});
105+
106+
const data = await connectionWrite(connect, getClientHandshake(this));
107+
108+
await bufferToHandshakeResult(data);
109+
110+
this.connection = connect;
111+
112+
return createSkytable(connect);
113+
}
114+
115+
async disconnect() {
116+
if (this.connection) {
117+
this.connection.destroySoon();
118+
}
119+
}
120+
}

src/connection.ts

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import { connect as connectTcp, Socket, NetConnectOpts } from 'node:net';
2+
import {
3+
connect as connectTcpTLS,
4+
TLSSocket,
5+
ConnectionOptions as ConnectionTLSOptions,
6+
} from 'node:tls';
7+
8+
export function createConnection(options: NetConnectOpts): Promise<Socket> {
9+
return new Promise((resolve, reject) => {
10+
const conn = connectTcp(options);
11+
conn.once('connect', () => {
12+
resolve(conn);
13+
});
14+
15+
conn.once('error', (error) => {
16+
console.error(`createConnection error: ${error.message}`);
17+
reject(error);
18+
});
19+
});
20+
}
21+
22+
export function createConnectionTls(
23+
options: ConnectionTLSOptions,
24+
): Promise<TLSSocket> {
25+
return new Promise((resolve, reject) => {
26+
const conn = connectTcpTLS(options);
27+
conn.once('connect', () => {
28+
resolve(conn);
29+
});
30+
31+
conn.once('error', (error) => {
32+
console.error(`createConnection error: ${error.message}`);
33+
reject(error);
34+
});
35+
});
36+
}
37+
38+
export function connectionWrite(
39+
connect: Socket | TLSSocket,
40+
buffer: Buffer | string,
41+
): Promise<Buffer> {
42+
return new Promise((resolve, reject) => {
43+
connect.write(buffer, (writeError) => {
44+
if (writeError) {
45+
reject(writeError);
46+
return;
47+
}
48+
49+
connect.once('data', (data) => {
50+
resolve(data);
51+
});
52+
connect.once('error', (err) => {
53+
reject(err);
54+
});
55+
});
56+
});
57+
}

src/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export {Config} from './Config';
2-
export {Query, SQParam} from './Query';
1+
export { Config } from './config';

0 commit comments

Comments
 (0)