Skip to content

Commit 505dd7c

Browse files
committed
added initial sql parser
1 parent e4c4a53 commit 505dd7c

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
},
1515
"author": "Arif Hossain",
1616
"license": "MIT",
17-
"type": "module",
1817
"devDependencies": {
1918
"typescript": "^5.2.2"
2019
},
2120
"dependencies": {
2221
"sql-to-nosql": "^0.0.3"
23-
}
22+
},
23+
"type": "module"
2424
}

src/index.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,16 @@
1-
export const greetings = (name: string) =>
2-
`Hello ${name}! I can run your sql queries!`;
1+
import { parseQuery } from "./utils/parser.js";
2+
3+
export const SqlToNoSql = (query: string) => {
4+
if (!query) {
5+
throw new Error("missing query!");
6+
}
7+
8+
if ([";", "(", ")"].some((char) => query.includes(char))) {
9+
throw new Error("Invalid query, your query contains invalid characters!");
10+
}
11+
12+
const q = parseQuery(query);
13+
return q;
14+
};
15+
16+
console.log(SqlToNoSql("SELECT * FROM users"));

src/utils/parser.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
export const parseQuery = (query: string) => {
2+
const [command, ...rest] = query.split(" ");
3+
4+
if (command !== "SELECT") {
5+
throw new Error("Only SELECT queries are supported");
6+
}
7+
8+
const fromIndex = rest.findIndex((word) => word === "FROM");
9+
if (fromIndex === -1) {
10+
throw new Error("Invalid query, missing FROM keyword");
11+
}
12+
13+
const columns = rest.slice(0, fromIndex);
14+
const table = rest[fromIndex + 1];
15+
16+
return { command, table, columns };
17+
};

tsconfig.json

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
11
{
22
"compilerOptions": {
3+
"strict": true,
4+
"module": "ES6",
35
"esModuleInterop": true,
4-
"target": "es6",
6+
"target": "es2016",
57
"moduleResolution": "node",
68
"sourceMap": true,
7-
"sourceRoot": "src",
8-
"outDir": "dist"
9+
"rootDir": "./src",
10+
"outDir": "./dist",
11+
"baseUrl": "./src",
12+
"paths": {
13+
"@utils/*": ["src/utils/*"]
14+
}
915
},
10-
"include": ["src/**/*"],
16+
"include": ["src"],
1117
"exclude": ["node_modules", "dist"]
1218
}

0 commit comments

Comments
 (0)