Skip to content

Commit

Permalink
feat steps 1 2 and 3
Browse files Browse the repository at this point in the history
  • Loading branch information
sourav-sm committed Mar 25, 2024
1 parent 13c8781 commit 6d6824f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 5 deletions.
4 changes: 3 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"description": "A minimal SQL based DB based on CSV files. For educational purposes only.",
"main": "./src/index.js",
"directories": {
"doc": "docs"
"doc": "docs",
"test": "tests"
},
"scripts": {
"test": "jest",
"test:1": "jest --testPathPattern=./tests/step-01",
"test:1": "jest --testPathPattern=./tests/step-01",
"test:2": "jest --testPathPattern=./tests/step-02",
"test:3": "jest --testPathPattern=./tests/step-03",
"test:4": "jest --testPathPattern=./tests/step-04",
Expand Down Expand Up @@ -38,11 +39,11 @@
"author": "Chakshu Gautam",
"license": "ISC",
"devDependencies": {
"csv-parser": "^3.0.0",
"jest": "^29.7.0"
},
"dependencies": {
"csv-parser": "^3.0.0",
"json2csv": "^6.0.0-alpha.2",
"xterm": "^5.3.0"
}
}
}
4 changes: 4 additions & 0 deletions sample.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id,name,age
1,John,30
2,Jane,25
3,Bob,22
20 changes: 20 additions & 0 deletions src/csvReader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const fs = require('fs');
const csv = require('csv-parser');

function readCSV(filePath) {
const results = [];

return new Promise((resolve, reject) => {
fs.createReadStream(filePath)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
resolve(results);
})
.on('error', (error) => {
reject(error);
});
});
}

module.exports = readCSV;
Empty file added src/index.js
Empty file.
18 changes: 18 additions & 0 deletions src/queryParser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function parseQuery(query){
const selectRegex = /SELECT (.+) FROM (.+)/i;
const match=query.match
(selectRegex);

if(match){
const [,fields,table]=match;
return{
fields:fields.split(',').
map(field=>field.trim()),
table:table.trim()
};
}else{
throw new Error('Invalid query format');
}
}

module.exports=parseQuery;

0 comments on commit 6d6824f

Please sign in to comment.