Skip to content

Commit 1a14fe0

Browse files
author
Snowflake107
committed
init
0 parents  commit 1a14fe0

File tree

14 files changed

+310
-0
lines changed

14 files changed

+310
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
node_modules/
2+
test/
3+
yarn.lock

.npmignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
test/
3+
.prettierignore
4+
.prettierrc
5+
.gitignore
6+
yarn.lock

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
test/

.prettierrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"tabWidth": 4,
3+
"useTabs": false,
4+
"semi": true,
5+
"singleQuote": true,
6+
"printWidth": 400
7+
}

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# JSQL
2+
Use **JSON** as **SQL**.
3+
4+
# Installing
5+
6+
```sh
7+
$ npm i --save jsql
8+
```
9+
10+
> Note: This library is very new and does not support most of the statements
11+
12+
# Example
13+
14+
```js
15+
const { Database } = require("jsql");
16+
const db = new Database("./database.json");
17+
18+
// creating a table
19+
db.prepare(`CREATE TABLE IF NOT EXISTS "DEMO" ("key" TEXT, "value" TEXT)`).run();
20+
21+
// inserting data
22+
db.prepare(`INSERT INTO "DEMO" ("key","value") VALUES ("test_key", "test_value")`).run();
23+
24+
// fetching data
25+
db.prepare(`SELECT * FROM "DEMO"`).run();
26+
27+
// fetching data in limit
28+
db.prepare(`SELECT * FROM "DEMO" LIMIT 3`).run(); // returns 3 items if available
29+
30+
// drop a table
31+
db.prepare(`DROP TABLE "DEMO"`).run();
32+
```

index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
Database: require("./src/jsql/Database"),
3+
SQLParser: require("./src/sql/parser"),
4+
version: require("./package.json").version
5+
};

package.json

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "jsql",
3+
"version": "1.0.0",
4+
"description": "Use JSON as SQL.",
5+
"main": "index.js",
6+
"types": "typings/index.d.ts",
7+
"scripts": {
8+
"test": "cd test && node index.js",
9+
"format": "prettier --write */**"
10+
},
11+
"author": "Snowflake107",
12+
"license": "MIT",
13+
"dependencies": {
14+
"node-sql-parser": "^3.2.0"
15+
},
16+
"repository": {
17+
"type": "git",
18+
"url": "git+https://github.com/DevSnowflake/JSQL.git"
19+
},
20+
"keywords": [
21+
"JSQL",
22+
"SQL",
23+
"Database",
24+
"JavaScript",
25+
"JSON",
26+
"JSONSQL"
27+
],
28+
"bugs": {
29+
"url": "https://github.com/DevSnowflake/JSQL/issues"
30+
},
31+
"homepage": "https://github.com/DevSnowflake/JSQL#readme",
32+
"devDependencies": {
33+
"prettier": "^2.2.1"
34+
}
35+
}

src/Util/create.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
*
3+
* @param {*} ast
4+
* @param {import("../jsql/Database")} db
5+
*/
6+
module.exports = (ast, db) => {
7+
if (ast.type.toLowerCase() !== 'create') throw new TypeError('invalid query type');
8+
if (ast.keyword !== 'table') throw new Error(`Unsupported ${ast.keyword}`);
9+
const ifne = ast.if_not_exists === 'if not exists';
10+
const table = ast.table[0].table;
11+
12+
const col = ast.create_definitions.map((m) => ({ column: m.column.column, type: m.definition.dataType }));
13+
const data = db.db;
14+
15+
const payload = {
16+
tableName: table,
17+
keys: col,
18+
data: [],
19+
};
20+
21+
if (ifne && data[payload.tableName]) return;
22+
23+
data[payload.tableName] = payload;
24+
db.write(data);
25+
};

src/Util/drop.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module.exports = (ast, db) => {
2+
if (ast.type.toLowerCase() !== 'drop') throw new TypeError('invalid query type');
3+
if (ast.keyword !== 'table') throw new Error(`Unsupported ${ast.keyword}`);
4+
5+
const table = ast.name[0].table;
6+
7+
const data = db.db;
8+
if (!data[table]) return false;
9+
10+
delete data[table];
11+
db.write(data);
12+
13+
return true;
14+
};

src/Util/insert.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
*
3+
* @param {*} ast
4+
* @param {import("../jsql/Database")} db
5+
*/
6+
module.exports = (ast, db) => {
7+
if (ast.type.toLowerCase() !== 'insert') throw new TypeError('invalid query type');
8+
const table = ast.table[0].table;
9+
const data = db.db;
10+
if (!data[table]) throw new Error(`Table "${table}" does not exist`);
11+
const values = ast.values.find((f) => f.type === 'expr_list');
12+
if (ast.columns.length !== values.value.length) throw new Error('values length mismatch');
13+
14+
const payload = ast.columns.map((m, i) => ({
15+
key: m,
16+
data: values.value[i].value,
17+
}));
18+
19+
payload.forEach((i) => {
20+
data[table].data.push(i);
21+
});
22+
23+
db.write(data);
24+
};

0 commit comments

Comments
 (0)