From 6d6824f6a79c5207cc92f8d779d628a373bff4ac Mon Sep 17 00:00:00 2001 From: Sourav Date: Mon, 25 Mar 2024 19:13:31 +0530 Subject: [PATCH] feat steps 1 2 and 3 --- package-lock.json | 4 +++- package.json | 9 +++++---- sample.csv | 4 ++++ src/csvReader.js | 20 ++++++++++++++++++++ src/index.js | 0 src/queryParser.js | 18 ++++++++++++++++++ 6 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 sample.csv create mode 100644 src/index.js create mode 100644 src/queryParser.js diff --git a/package-lock.json b/package-lock.json index 3afaec37f..a6ba782df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9,7 +9,6 @@ "version": "0.1.6", "license": "ISC", "dependencies": { - "csv-parser": "^3.0.0", "json2csv": "^6.0.0-alpha.2", "xterm": "^5.3.0" }, @@ -17,6 +16,7 @@ "stylusdb-cli": "node ./src/cli.js" }, "devDependencies": { + "csv-parser": "^3.0.0", "jest": "^29.7.0" } }, @@ -1573,6 +1573,7 @@ "version": "3.0.0", "resolved": "https://registry.npmjs.org/csv-parser/-/csv-parser-3.0.0.tgz", "integrity": "sha512-s6OYSXAK3IdKqYO33y09jhypG/bSDHPuyCme/IdEHfWpLf/jKcpitVFyOC6UemgGk8v7Q5u2XE0vvwmanxhGlQ==", + "dev": true, "dependencies": { "minimist": "^1.2.0" }, @@ -2943,6 +2944,7 @@ "version": "1.2.8", "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", + "dev": true, "funding": { "url": "https://github.com/sponsors/ljharb" } diff --git a/package.json b/package.json index f52103d5c..ef81b73af 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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" } -} \ No newline at end of file +} diff --git a/sample.csv b/sample.csv new file mode 100644 index 000000000..9e7a9fa25 --- /dev/null +++ b/sample.csv @@ -0,0 +1,4 @@ +id,name,age +1,John,30 +2,Jane,25 +3,Bob,22 \ No newline at end of file diff --git a/src/csvReader.js b/src/csvReader.js index e69de29bb..b1d92429a 100644 --- a/src/csvReader.js +++ b/src/csvReader.js @@ -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; diff --git a/src/index.js b/src/index.js new file mode 100644 index 000000000..e69de29bb diff --git a/src/queryParser.js b/src/queryParser.js new file mode 100644 index 000000000..2f06cca41 --- /dev/null +++ b/src/queryParser.js @@ -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; \ No newline at end of file