Skip to content

Commit 075a4db

Browse files
authored
Merge pull request #2 from jkcs/feat/v0.1
🎉 feat v0.1
2 parents 29c845d + 309a3c9 commit 075a4db

20 files changed

+3549
-116
lines changed

.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+
}

__tests__/ddl.spec.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { getDBConfig, getSpace, getTable } from './utils';
2+
3+
const testSpace = 'ddltestspace';
4+
5+
describe('DDL', () => {
6+
let db: any;
7+
const dbConfig = getDBConfig();
8+
9+
beforeAll(async () => {
10+
db = await dbConfig.connect();
11+
});
12+
13+
afterAll(async () => {
14+
dbConfig.disconnect();
15+
});
16+
17+
it('CREATE SPACE', async () => {
18+
const spaceName = `${testSpace + Date.now()}`;
19+
try {
20+
expect(await db.query(`CREATE SPACE ${spaceName}`)).toBe(null);
21+
} finally {
22+
await db.query(`DROP SPACE ALLOW NOT EMPTY ${spaceName}`);
23+
}
24+
});
25+
26+
// FIXME need to fix. why need 'ALTER SPACE'?
27+
// it('ALTER SPACE', async () => {
28+
// const spaceName = `${testSpace + Date.now()}`;
29+
// try {
30+
// await db.query(`CREATE SPACE IF NOT EXISTS ${spaceName} WITH { property_name: ? }`, 123);
31+
32+
// expect(await db.query(`ALTER SPACE ${spaceName} WITH { property_name: ? }`, 456)).toBe(null);
33+
// } finally {
34+
// await db.query(`DROP SPACE ALLOW NOT EMPTY ${spaceName}`);
35+
// }
36+
// })
37+
38+
it('CREATE MODEL', async () => {
39+
const [space, drop] = await getSpace(db, `${testSpace + Date.now()}`);
40+
const tableName = `${space}.testTable${Date.now()}`;
41+
42+
try {
43+
await db.query(`CREATE MODEL ${tableName}(id: string, name: string)`);
44+
45+
expect(
46+
await db.query(
47+
`CREATE MODEL IF NOT EXISTS ${tableName}(id: string, name: string)`,
48+
),
49+
).toBe(false);
50+
} finally {
51+
await drop();
52+
}
53+
});
54+
55+
it('ALTER MODEL', async () => {
56+
const [tableName, drop] = await getTable(db);
57+
58+
try {
59+
await db.query(`CREATE MODEL ${tableName}(id: string, name: string)`);
60+
await db.query(`ALTER MODEL ${tableName} ADD field { type: uint8 }`);
61+
await db.query(
62+
`ALTER MODEL ${tableName} ADD ( first_field { type: string }, second_field { type: binary } )`,
63+
);
64+
65+
await db.query(`ALTER MODEL ${tableName} UPDATE field { type: uint64 }`);
66+
await db.query(
67+
`ALTER MODEL ${tableName} REMOVE (first_field, second_field)`,
68+
);
69+
} finally {
70+
await drop();
71+
}
72+
});
73+
});

__tests__/dml.spec.ts

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import type { Rows } from '../src';
2+
import { getDBConfig, getTable } from './utils';
3+
4+
describe('DML', () => {
5+
let db: any;
6+
const dbConfig = getDBConfig();
7+
8+
beforeAll(async () => {
9+
db = await dbConfig.connect();
10+
});
11+
12+
afterAll(async () => {
13+
dbConfig.disconnect();
14+
});
15+
16+
it('null type', async () => {
17+
const [tableName, drop] = await getTable(db);
18+
try {
19+
await db.query(
20+
`CREATE MODEL ${tableName}(username: string, null email_id: string)`,
21+
);
22+
23+
await db.query(`INSERT INTO ${tableName}(?, ?)`, 'test', null);
24+
25+
expect(
26+
await db.query(
27+
`SELECT username,email_id FROM ${tableName} WHERE username = ?`,
28+
'test',
29+
),
30+
).toEqual(['test', null]);
31+
} finally {
32+
await drop();
33+
}
34+
});
35+
36+
it('int number type', async () => {
37+
const [tableName, drop] = await getTable(db);
38+
39+
try {
40+
await db.query(
41+
`CREATE MODEL ${tableName}(u8: uint8, u16: uint16, u32: uint32, u64: uint64)`,
42+
);
43+
44+
await db.query(
45+
`INSERT INTO ${tableName}(?, ?, ?, ?)`,
46+
1,
47+
2,
48+
3312321,
49+
BigInt(478787872837218382),
50+
);
51+
52+
// TODO why is the uint8 in bigint
53+
expect(
54+
await db.query(`SELECT * FROM ${tableName} WHERE u8 = ?`, 1),
55+
).toEqual([BigInt(1), 2, 3312321, BigInt(478787872837218382)]);
56+
} finally {
57+
await drop();
58+
}
59+
});
60+
61+
it('list type', async () => {
62+
const [tableName, drop] = await getTable(db);
63+
64+
try {
65+
await db.query(
66+
`CREATE MODEL ${tableName}(id: uint64, list: list { type: string} )`,
67+
);
68+
69+
await db.query(`INSERT INTO ${tableName}(?, [?])`, 1, 'test');
70+
71+
expect(
72+
await db.query(`SELECT * FROM ${tableName} WHERE id = ?`, 1),
73+
).toEqual([BigInt(1), ['test']]);
74+
} finally {
75+
await drop();
76+
}
77+
});
78+
79+
it('binary type', async () => {
80+
const [tableName, drop] = await getTable(db);
81+
82+
try {
83+
await db.query(`CREATE MODEL ${tableName}(id: uint64, binary: binary )`);
84+
85+
await db.query(`INSERT INTO ${tableName}(?, ?)`, 1, Buffer.from('test'));
86+
87+
expect(
88+
await db.query(`SELECT * FROM ${tableName} WHERE id = ?`, 1),
89+
).toEqual([BigInt(1), Buffer.from('test')]);
90+
} finally {
91+
await drop();
92+
}
93+
});
94+
95+
it('Multirow', async () => {
96+
const [tableName, drop] = await getTable(db);
97+
98+
try {
99+
await db.query(`CREATE MODEL ${tableName}(id: string, name: string )`);
100+
for (let i = 0; i < 10; i++) {
101+
await db.query(`INSERT INTO ${tableName}(?, ?)`, String(i), 'test');
102+
}
103+
const rows = (await db.query(
104+
`SELECT ALL * FROM ${tableName} LIMIT ?`,
105+
10,
106+
)) as Rows;
107+
108+
expect(
109+
rows.sort((row1, row2) =>
110+
Number(row1[0] as string) > Number(row2[0] as string) ? 1 : -1,
111+
),
112+
).toEqual([
113+
...Array.from({ length: 10 }).map((_, index) => [
114+
String(index),
115+
'test',
116+
]),
117+
]);
118+
} finally {
119+
await drop();
120+
}
121+
});
122+
});

__tests__/utils.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import Config from '../src';
2+
3+
export function getDBConfig() {
4+
return new Config('root', 'admin123456123456', '127.0.0.1', 2003);
5+
}
6+
7+
export async function getSpace(
8+
db: any,
9+
space = 'testspace',
10+
): Promise<[string, Function]> {
11+
await db.query(`CREATE SPACE IF NOT EXISTS ${space}`);
12+
13+
await db.query(`USE ${space}`);
14+
15+
return [
16+
space,
17+
async () => await db.query(`DROP SPACE ALLOW NOT EMPTY ${space}`),
18+
];
19+
}
20+
21+
export async function getTable(db: any): Promise<[string, Function]> {
22+
const [space, drop] = await getSpace(db, `testTable${Date.now()}Space`);
23+
24+
return [`${space}.testTable${Date.now()}`, drop as Function];
25+
}

examples/simple.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import Config, { type Row } from '../src';
2+
3+
async function main() {
4+
const config = new Config('root', 'admin123456123456', '127.0.0.1', 2003);
5+
const spaceName = `testSpace`;
6+
const tableName = `${spaceName}.users`;
7+
const db = await config.connect();
8+
9+
try {
10+
await db.query(`create space IF NOT EXISTS ${spaceName}`);
11+
await db.query(`use ${spaceName}`);
12+
await db.query(
13+
`CREATE MODEL ${tableName}(username: string, password: string, null email_id: string)`,
14+
);
15+
await db.query(
16+
`INSERT INTO ${tableName}(?, ?, ?)`,
17+
'test',
18+
'password',
19+
null,
20+
);
21+
const row = await db.query(
22+
`SELECT * FROM ${tableName} WHERE username = ?`,
23+
'test',
24+
);
25+
const [username, password, email_id] = row as Row;
26+
console.assert(username === 'test');
27+
console.assert(password === 'password');
28+
console.assert(email_id == null);
29+
} catch (e) {
30+
console.error(e);
31+
} finally {
32+
config.disconnect();
33+
}
34+
}
35+
36+
main();

jest.config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
maxWorkers: 1,
4+
testEnvironment: 'node',
5+
globals: {
6+
'ts-jest': {
7+
tsconfig: 'tsconfig.json'
8+
}
9+
},
10+
testMatch: ['**/?*.(spec|test|e2e).(j|t)s?(x)'],
11+
testPathIgnorePatterns: ['/node_modules/', '/dist/'],
12+
}

package.json

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,57 @@
11
{
22
"name": "skytable",
33
"version": "1.0.0",
4-
"main": "dist/index.js",
4+
"main": "dist/cjs/index.js",
5+
"types": "dist/cjs/index.d.ts",
6+
"module": "dist/esm/index.mjs",
57
"description": "Offical NodeJS client driver for Skytable",
6-
"repository": "https://github.com/skytable/client-nodejs.git",
78
"author": "Sayan Nandan <nandansayan@outlook.com>",
89
"license": "Apache-2.0",
10+
"repository": {
11+
"url": "https://github.com/skytable/client-nodejs"
12+
},
13+
"bugs": {
14+
"url": "https://github.com/skytable/client-nodejs/issues"
15+
},
16+
"scripts": {
17+
"build": "yarn build:cjs && yarn build:esm",
18+
"build:cjs": "tsc",
19+
"build:esm": "rollup --config rollup.config.js",
20+
"test": "jest",
21+
"formatting": "prettier src examples __tests__ --check",
22+
"prettier:fix": "prettier src examples __tests__ --write"
23+
},
24+
"sideEffects": false,
25+
"exports": {
26+
"./package.json": "./package.json",
27+
".": {
28+
"types": "./dist/cjs/index.d.ts",
29+
"module": "./dist/esm/index.mjs",
30+
"import": "./dist/esm/index.mjs",
31+
"require": "./dist/cjs/index.js"
32+
}
33+
},
34+
"engines": {
35+
"node": ">=16.0.0"
36+
},
937
"devDependencies": {
38+
"@rollup/plugin-typescript": "^11.1.5",
39+
"@types/jest": "^29.5.11",
40+
"@types/node": "^20.10.4",
41+
"jest": "^29.7.0",
42+
"nodemon": "^3.0.2",
43+
"prettier": "^3.1.1",
44+
"rollup": "^4.9.1",
45+
"ts-jest": "^29.1.1",
46+
"tslib": "^2.6.2",
47+
"tsx": "^4.6.2",
1048
"typescript": "^5.3.3"
1149
},
12-
"scripts": {
13-
"build": "tsc"
50+
"publishConfig": {
51+
"access": "public"
1452
},
15-
"dependencies": {
16-
"@types/node": "^20.10.4"
17-
}
53+
"keywords": [
54+
"skytable",
55+
"skytable-client"
56+
]
1857
}

rollup.config.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// rollup.config.js
2+
const typescript = require('@rollup/plugin-typescript');
3+
4+
module.exports = {
5+
input: 'src/index.ts',
6+
output: [
7+
{
8+
file: 'dist/esm/index.mjs',
9+
format: 'es',
10+
sourcemap: false,
11+
},
12+
],
13+
plugins: [
14+
typescript({
15+
tsconfig: 'tsconfig-esm.json',
16+
sourceMap: false,
17+
outputToFilesystem: true,
18+
}),
19+
],
20+
}

0 commit comments

Comments
 (0)