From 4c874d23cd68d59f09fb4469e37cfff9645c3efa Mon Sep 17 00:00:00 2001 From: dannst Date: Thu, 16 Apr 2020 10:27:41 +0800 Subject: [PATCH 1/3] add and show functionalities, done without ORDER --- index.js | 40 ++++++++++++---- package-lock.json | 119 ++++++++++++++++++++++++++++++++++++++++++++++ package.json | 22 +++++++++ 3 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/index.js b/index.js index 3907d3b5..71c34ade 100644 --- a/index.js +++ b/index.js @@ -1,9 +1,8 @@ -console.log("works!!", process.argv[2]); const pg = require('pg'); const configs = { - user: 'akira', + user: 'Mac', host: '127.0.0.1', database: 'todo', port: 5432, @@ -11,26 +10,51 @@ const configs = { const client = new pg.Client(configs); -let queryDoneCallback = (err, result) => { +/////////////////////////////////////////////// + +const user_command = process.argv[2]; +const input_index = process.argv[3]; + + +const renderList = function(res){ + const list = res.rows; + list.forEach(obj => console.log(`${obj.id}. ${obj.done} - ${obj.thing}`)); +} + +//callback function after sending query +const queryDoneCallback = (err, res) => { if (err) { console.log("query error", err.message); } else { - console.log("result", result.rows ); + // console.log(res.rows); + renderList(res); } client.end(); }; -let clientConnectionCallback = (err) => { + +//callback function after successful connection +const clientConnectionCallback = (err) => { + let queryText = undefined; + let values = undefined; if( err ){ console.log( "error", err.message ); } - let text = "INSERT INTO todo (name) VALUES ($1) RETURNING id"; + if (user_command === 'add'){ + queryText = 'INSERT INTO todolist (thing, done) VALUES ($1, $2) RETURNING'; + values = [process.argv[3], '[ ]']; - const values = ["hello"]; + } else if (user_command === 'show'){ + queryText = 'SELECT * FROM todolist;'; + } else if (user_command === 'done'){ + queryText = `UPDATE todolist SET done = '[X]' WHERE id = ${input_index}`; + } - client.query(text, values, queryDoneCallback); + //send query after connection + client.query(queryText, values, queryDoneCallback); }; +//connects to pg with a callback function client.connect(clientConnectionCallback); 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..c82c42d2 --- /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/dannst/cli-todo-sql.git" + }, + "author": "", + "license": "ISC", + "bugs": { + "url": "https://github.com/dannst/cli-todo-sql/issues" + }, + "homepage": "https://github.com/dannst/cli-todo-sql#readme", + "dependencies": { + "pg": "^8.0.2" + } +} From c8aa278722f26e0f3acf11d63655b2aa9d3ea2ae Mon Sep 17 00:00:00 2001 From: dannst Date: Thu, 16 Apr 2020 13:37:55 +0800 Subject: [PATCH 2/3] updated callbacks --- index.js | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 71c34ade..5ac41ba9 100644 --- a/index.js +++ b/index.js @@ -16,22 +16,30 @@ const user_command = process.argv[2]; const input_index = process.argv[3]; -const renderList = function(res){ - const list = res.rows; +const renderList = function(result){ + const list = result.rows; list.forEach(obj => console.log(`${obj.id}. ${obj.done} - ${obj.thing}`)); } //callback function after sending query -const queryDoneCallback = (err, res) => { +const queryDoneCallback = (err, result) => { if (err) { console.log("query error", err.message); } else { // console.log(res.rows); - renderList(res); + renderList(result); } client.end(); }; +const callbackShow = (err) => { + if (err) { + console.log("query error", err.message); + } else { + queryText = 'SELECT * FROM todolist ORDER BY id ASC'; + client.query(queryText, queryDoneCallback); + } +} //callback function after successful connection const clientConnectionCallback = (err) => { @@ -43,17 +51,21 @@ const clientConnectionCallback = (err) => { } if (user_command === 'add'){ - queryText = 'INSERT INTO todolist (thing, done) VALUES ($1, $2) RETURNING'; + queryText = 'INSERT INTO todolist (thing, done) VALUES ($1, $2) RETURNING *'; values = [process.argv[3], '[ ]']; + client.query(queryText, values, callbackShow); + } else if (user_command === 'show'){ queryText = 'SELECT * FROM todolist;'; + client.query(queryText, values, queryDoneCallback); + + } else if (user_command === 'done'){ queryText = `UPDATE todolist SET done = '[X]' WHERE id = ${input_index}`; - } + client.query(queryText, values, callbackShow); - //send query after connection - client.query(queryText, values, queryDoneCallback); + } }; //connects to pg with a callback function From 04050fb3cbb725e46f42426b1054fb3d60e2b89f Mon Sep 17 00:00:00 2001 From: dannst Date: Thu, 16 Apr 2020 14:21:23 +0800 Subject: [PATCH 3/3] minor change --- index.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/index.js b/index.js index 5ac41ba9..e54b7fc7 100644 --- a/index.js +++ b/index.js @@ -48,9 +48,7 @@ const clientConnectionCallback = (err) => { if( err ){ console.log( "error", err.message ); - } - - if (user_command === 'add'){ + } else if (user_command === 'add'){ queryText = 'INSERT INTO todolist (thing, done) VALUES ($1, $2) RETURNING *'; values = [process.argv[3], '[ ]']; client.query(queryText, values, callbackShow);