From 27f5b2c8c271b1796861f8d4751c1ef3c13fd887 Mon Sep 17 00:00:00 2001 From: ReadyHash Date: Thu, 16 Apr 2020 08:17:58 +0800 Subject: [PATCH 1/4] Created file with node module, added [ADD] functionality --- index.js | 68 ++++++++++++++++++-------- package-lock.json | 119 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++ 3 files changed, 190 insertions(+), 19 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/index.js b/index.js index 3907d3b5..57b279fb 100644 --- a/index.js +++ b/index.js @@ -1,36 +1,66 @@ -console.log("works!!", process.argv[2]); +console.log("/////////////////////////////////////"); +console.log("///////// TO DO LIST PROGRAM ////////"); +console.log("/////////////////////////////////////"); + const pg = require('pg'); const configs = { - user: 'akira', + user: 'shane', host: '127.0.0.1', database: 'todo', port: 5432, }; -const client = new pg.Client(configs); +const addCommand = process.argv[2]; -let queryDoneCallback = (err, result) => { - if (err) { - console.log("query error", err.message); - } else { - console.log("result", result.rows ); - } - client.end(); -}; +console.log("Checking for command") + +// A function that reads what command user inputted +checkCommand = (input) => { + if( addCommand == "add"){ + console.log("-----------------------"); + console.log("INITIALIZE : ADD") -let clientConnectionCallback = (err) => { + const client = new pg.Client(configs); - if( err ){ - console.log( "error", err.message ); - } + let queryDoneCallback = (err, result) => { + if (err) { + console.log("////////////////////////"); + console.log("query error", err.message); + console.log("////////////////////////"); + } else { + console.log("result", result.rows ); + } + console.log("-----------------------"); + console.log("ending connection..."); + client.end(); + }; - let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id"; + // only activates when connection is successful + let clientConnectionCallback = (err) => { + // checks if connection has errors + if( err ){ + console.log( "error", err.message ); + } + // new data directory + let text = "INSERT INTO todoitems (status, instructions) VALUES ($1, $2) RETURNING id"; + // retrieve input from process to fill data + const values = [process.argv[3], process.argv[4]]; - const values = ["hello"]; + client.query(text, values, queryDoneCallback); + console.log("-----------------------"); + console.log("finished query..."); + }; + console.log("-----------------------"); + console.log("making the connection to database [todo]..."); + client.connect(clientConnectionCallback); - client.query(text, values, queryDoneCallback); + }else{ + console.log("-----------------------"); + console.log("ERROR: COMMAND NOT FOUND"); + console.log("entered command: " + addCommand); + } }; -client.connect(clientConnectionCallback); +checkCommand(); \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..2afe3c42 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,119 @@ +{ + "name": "cli-todo-sql", + "version": "1.0.0", + "lockfileVersion": 1, + "requires": true, + "dependencies": { + "buffer-writer": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" + }, + "packet-reader": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-1.0.0.tgz", + "integrity": "sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==" + }, + "pg": { + "version": "8.0.2", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.0.2.tgz", + "integrity": "sha512-ngOUEDk69kLdH/k/YLT2NRIBcUiPFRcY4l51dviqn79P5qIa5jBIGIFTIGXh4OlT/6gpiCAza5a9uy08izpFQQ==", + "requires": { + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "0.1.3", + "pg-pool": "^3.1.0", + "pg-protocol": "^1.2.1", + "pg-types": "^2.1.0", + "pgpass": "1.x", + "semver": "4.3.2" + } + }, + "pg-connection-string": { + "version": "0.1.3", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-0.1.3.tgz", + "integrity": "sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=" + }, + "pg-int8": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/pg-int8/-/pg-int8-1.0.1.tgz", + "integrity": "sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==" + }, + "pg-pool": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.1.0.tgz", + "integrity": "sha512-CvxGctDwjZZad6Q7vvhFA4BsYdk26UFIZaFH0XXqHId5uBOc26vco/GFh/laUVIQUpD9IKe/f9/mr/OQHyQ2ZA==" + }, + "pg-protocol": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.2.1.tgz", + "integrity": "sha512-IqZ+VUOqg3yydxSt5NgNKLVK9JgPBuzq4ZbA9GmrmIkQjQAszPT9DLqTtID0mKsLEZB68PU0gjLla561WZ2QkQ==" + }, + "pg-types": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", + "requires": { + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", + "postgres-bytea": "~1.0.0", + "postgres-date": "~1.0.4", + "postgres-interval": "^1.1.0" + } + }, + "pgpass": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.2.tgz", + "integrity": "sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=", + "requires": { + "split": "^1.0.0" + } + }, + "postgres-array": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-2.0.0.tgz", + "integrity": "sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==" + }, + "postgres-bytea": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/postgres-bytea/-/postgres-bytea-1.0.0.tgz", + "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" + }, + "postgres-date": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.5.tgz", + "integrity": "sha512-pdau6GRPERdAYUQwkBnGKxEfPyhVZXG/JiS44iZWiNdSOWE09N2lUgN6yshuq6fVSon4Pm0VMXd1srUUkLe9iA==" + }, + "postgres-interval": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.2.0.tgz", + "integrity": "sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==", + "requires": { + "xtend": "^4.0.0" + } + }, + "semver": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", + "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" + }, + "split": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", + "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "requires": { + "through": "2" + } + }, + "through": { + "version": "2.3.8", + "resolved": "https://registry.npmjs.org/through/-/through-2.3.8.tgz", + "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + }, + "xtend": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.2.tgz", + "integrity": "sha512-LKYU1iAXJXUgAXn9URjiu+MWhyUXHsvfp7mcuYm9dSUKK0/CjtrUwFAxD82/mCWbtLsGjFIad0wIsod4zrTAEQ==" + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 00000000..b0e5ed0d --- /dev/null +++ b/package.json @@ -0,0 +1,22 @@ +{ + "name": "cli-todo-sql", + "version": "1.0.0", + "description": "![https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp](https://i.giphy.com/media/26ufnwz3wDUli7GU0/giphy.webp)", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/wdi-sg/cli-todo-sql.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/wdi-sg/cli-todo-sql/issues" + }, + "homepage": "https://github.com/wdi-sg/cli-todo-sql#readme", + "dependencies": { + "pg": "^8.0.2" + } +} From 11d9cb6042f3a8695a883439836faad339c86bdc Mon Sep 17 00:00:00 2001 From: ReadyHash Date: Thu, 16 Apr 2020 08:59:48 +0800 Subject: [PATCH 2/4] Added more commands and a global status to check them --- index.js | 82 +++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 60 insertions(+), 22 deletions(-) diff --git a/index.js b/index.js index 57b279fb..7380503c 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,12 @@ -console.log("/////////////////////////////////////"); -console.log("///////// TO DO LIST PROGRAM ////////"); -console.log("/////////////////////////////////////"); + + + + + + +console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); +console.log("~~~~~~~~~ TO DO LIST PROGRAM ~~~~~~~~"); +console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); const pg = require('pg'); @@ -12,36 +18,46 @@ const configs = { port: 5432, }; -const addCommand = process.argv[2]; - -console.log("Checking for command") +const consoleCommand = process.argv[2]; // A function that reads what command user inputted checkCommand = (input) => { - if( addCommand == "add"){ - console.log("-----------------------"); - console.log("INITIALIZE : ADD") + console.log("Checking for command") + const client = new pg.Client(configs); + let commandStatus = false - const client = new pg.Client(configs); + if( consoleCommand == "add"){ + commandStatus = true; + console.log("-----------------------"); + console.log("INITIALIZE COMMAND: ADD") let queryDoneCallback = (err, result) => { if (err) { - console.log("////////////////////////"); - console.log("query error", err.message); - console.log("////////////////////////"); + console.log("////////////////////////"); + console.log("ADD ERROR"); + console.log(err.message); + console.log("////////////////////////"); } else { - console.log("result", result.rows ); + // ??? + // how do I view what is inside result.rows object? + console.log("result", result.rows ); } + // ??? + // why put this code outside of else block? console.log("-----------------------"); - console.log("ending connection..."); + console.log("DISCONNECTING DATABASE") client.end(); + console.log("connection ended"); }; - // only activates when connection is successful + // Only activates when connection is successful let clientConnectionCallback = (err) => { // checks if connection has errors if( err ){ - console.log( "error", err.message ); + console.log("////////////////////////"); + console.log("CONNECTION ERROR :"); + console.log( err.message ); + console.log("////////////////////////"); } // new data directory let text = "INSERT INTO todoitems (status, instructions) VALUES ($1, $2) RETURNING id"; @@ -55,12 +71,34 @@ checkCommand = (input) => { console.log("-----------------------"); console.log("making the connection to database [todo]..."); client.connect(clientConnectionCallback); - - }else{ + } + // command to edit the list + if( consoleCommand == "edit"){ + commandStatus = true; console.log("-----------------------"); - console.log("ERROR: COMMAND NOT FOUND"); - console.log("entered command: " + addCommand); - } + console.log(" edit command found") + console.log(client); + } + // command to delete one list + if( consoleCommand == "delete"){ + commandStatus = true; + console.log("-----------------------"); + console.log(" delete command found") + } + // command to view the list + if( consoleCommand == "view"){ + commandStatus = true; + console.log("-----------------------"); + console.log(" view command found") + + } + // checks if command does not exist + if(commandStatus === false){ + console.log("////////////////////////"); + console.log("ERROR: COMMAND DOES NOT EXIST"); + console.log("entered command: " + consoleCommand); + console.log("////////////////////////"); + } }; checkCommand(); \ No newline at end of file From 09e2c24d664df073ae4e17d1fa782a3d03f6d893 Mon Sep 17 00:00:00 2001 From: ReadyHash Date: Thu, 16 Apr 2020 09:25:48 +0800 Subject: [PATCH 3/4] Added a way to view list table in console --- index.js | 79 ++++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 7380503c..676030e7 100644 --- a/index.js +++ b/index.js @@ -23,14 +23,18 @@ const consoleCommand = process.argv[2]; // A function that reads what command user inputted checkCommand = (input) => { console.log("Checking for command") + const client = new pg.Client(configs); + + // create a global status to make sure only correct commands are true let commandStatus = false if( consoleCommand == "add"){ commandStatus = true; console.log("-----------------------"); - console.log("INITIALIZE COMMAND: ADD") + console.log("INITIALIZE COMMAND: ADD"); + // This function runs if query returns a response let queryDoneCallback = (err, result) => { if (err) { console.log("////////////////////////"); @@ -47,11 +51,11 @@ checkCommand = (input) => { console.log("-----------------------"); console.log("DISCONNECTING DATABASE") client.end(); - console.log("connection ended"); }; + // Only activates when connection is successful - let clientConnectionCallback = (err) => { + let addnewList = (err) => { // checks if connection has errors if( err ){ console.log("////////////////////////"); @@ -59,39 +63,92 @@ checkCommand = (input) => { console.log( err.message ); console.log("////////////////////////"); } - // new data directory + // specifies where to add the data let text = "INSERT INTO todoitems (status, instructions) VALUES ($1, $2) RETURNING id"; // retrieve input from process to fill data - const values = [process.argv[3], process.argv[4]]; + const inputData = [process.argv[3], process.argv[4]]; - client.query(text, values, queryDoneCallback); + client.query(text, inputData, queryDoneCallback); console.log("-----------------------"); console.log("finished query..."); }; console.log("-----------------------"); console.log("making the connection to database [todo]..."); - client.connect(clientConnectionCallback); + client.connect(addnewList); } + + + + + // command to edit the list if( consoleCommand == "edit"){ commandStatus = true; console.log("-----------------------"); - console.log(" edit command found") - console.log(client); + console.log("INITIALIZE COMMAND: EDIT"); + console.log(); } + + + + + // command to delete one list if( consoleCommand == "delete"){ commandStatus = true; console.log("-----------------------"); - console.log(" delete command found") + console.log("INITIALIZE COMMAND: DELETE"); } + + + + + // command to view the list if( consoleCommand == "view"){ commandStatus = true; console.log("-----------------------"); - console.log(" view command found") + console.log("INITIALIZE COMMAND: VIEW"); + let queryDoneCallback = (err, result) => { + if (err) { + console.log("////////////////////////"); + console.log("ADD ERROR"); + console.log(err.message); + console.log("////////////////////////"); + } else { + // ??? + // how do I view what is inside result.rows object? + console.table( result.rows ); + } + // ??? + // why put this code outside of else block? + console.log("-----------------------"); + console.log("DISCONNECTING DATABASE") + client.end(); + }; + + viewList = (err) => { + if(err){ + console.log("////////////////////////"); + console.log("CONNECTION ERROR :"); + console.log( err.message ); + console.log("////////////////////////"); + } + let listTable = "SELECT * FROM todoitems"; + + client.query(listTable, queryDoneCallback); + + } + console.log("-----------------------"); + console.log("making the connection to database [todo]..."); + client.connect(viewList) } + + + + + // checks if command does not exist if(commandStatus === false){ console.log("////////////////////////"); From c3f9789e12a95d0c2d006db2079716b9b9d92b25 Mon Sep 17 00:00:00 2001 From: ReadyHash Date: Thu, 16 Apr 2020 09:39:26 +0800 Subject: [PATCH 4/4] added function to delete command --- index.js | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/index.js b/index.js index 676030e7..4db7b0b5 100644 --- a/index.js +++ b/index.js @@ -36,6 +36,7 @@ checkCommand = (input) => { // This function runs if query returns a response let queryDoneCallback = (err, result) => { + if (err) { console.log("////////////////////////"); console.log("ADD ERROR"); @@ -98,6 +99,26 @@ checkCommand = (input) => { commandStatus = true; console.log("-----------------------"); console.log("INITIALIZE COMMAND: DELETE"); + + queryDoneCallback = (err, remainder) =>{ + if(err){ + console.log("////////////////////////"); + console.log("CONNECTION ERROR :"); + console.log( err.message ); + console.log("////////////////////////"); + } else { + console.log("-----------------------"); + console.log("deleted") + } + client.end(); + } + + deleteList = () =>{ + let text = "DELETE FROM todoitems WHERE id = 10" + client.query(text,queryDoneCallback) + }; + + client.connect(deleteList); }