File tree Expand file tree Collapse file tree 4 files changed +45
-8
lines changed
Expand file tree Collapse file tree 4 files changed +45
-8
lines changed Original file line number Diff line number Diff line change 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}
Original file line number Diff line number Diff line change 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" ) ) ;
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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}
You can’t perform that action at this time.
0 commit comments