From 22b1acc3098d7c772407d4d9a468cc4d7c2767bf Mon Sep 17 00:00:00 2001 From: Ling Qing Meng Date: Sun, 26 Apr 2020 15:31:35 -0700 Subject: [PATCH 01/15] Update README.md --- README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 980d2de..e8cd43c 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,18 @@ node index.js ## Commands +- GET: `curl http://localhost:3000/settings/locale` +- PUT: `curl -X PUT -d "val=Georgia" http://localhost:3000/settings/locale` + + - GET: `curl http://localhost:3000/users` - POST: `curl --data "name=Jerry&email=jerry@example.com" http://localhost:3000/users` - PUT: `curl -X PUT -d "name=George" -d "email=george@example.com" http://localhost:3000/users/1` - DELETE: `curl -X "DELETE" http://localhost:3000/users/1` -## Author - +## Original Repository + +Credit goes to: - [Tania Rascia](https://www.taniarascia.com) ## License From c60f45574dd0e323991e68a8a5b4720c1a66bf92 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Wed, 15 Dec 2021 19:42:43 -0500 Subject: [PATCH 02/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index e8cd43c..2c1306f 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ CREATE TABLE users ( ); INSERT INTO users (name, email) - VALUES ('Jerry', 'jerry@example.com'), ('George', 'george@example.com'); + VALUES ('Alice', 'alice@ondecentral.com'), ('Bob', 'bob@ondecentral.com'); ``` ## Installation From e42221d26e249cca5bccf426f9c51c4247420c05 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Wed, 15 Dec 2021 20:33:09 -0500 Subject: [PATCH 03/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c1306f..bf3ef04 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ INSERT INTO users (name, email) ## Installation ```bash -git clone git@github.com:taniarascia/node-api-postgres +git clone git@github.com:lingqingmeng/node-api-postgres cd node-api-postgres npm install node index.js From e60d6981c8e1e36af4b09915a7f816056370662e Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Wed, 15 Dec 2021 20:34:01 -0500 Subject: [PATCH 04/15] Update README.md --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bf3ef04..9e2273d 100644 --- a/README.md +++ b/README.md @@ -50,8 +50,8 @@ node index.js - GET: `curl http://localhost:3000/users` -- POST: `curl --data "name=Jerry&email=jerry@example.com" http://localhost:3000/users` -- PUT: `curl -X PUT -d "name=George" -d "email=george@example.com" http://localhost:3000/users/1` +- POST: `curl --data "name=Alice&email=alice@ondecentral.com" http://localhost:3000/users` +- PUT: `curl -X PUT -d "name=Bob" -d "email=bob@ondecentral.com" http://localhost:3000/users/1` - DELETE: `curl -X "DELETE" http://localhost:3000/users/1` ## Original Repository From f004e65eeb1f50a896e5e47fff58dd4004df9275 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Wed, 15 Dec 2021 21:45:41 -0500 Subject: [PATCH 05/15] Update package.json --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 92a504e..ad6cc39 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,6 @@ "license": "MIT", "dependencies": { "express": "^4.16.3", - "pg": "^7.4.3" + "pg": "8.7.1" } } From 95ba85f9346a1155e444ddc7340531dc78dacdf2 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Wed, 15 Dec 2021 21:46:51 -0500 Subject: [PATCH 06/15] Update README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 9e2273d..b617c18 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,8 @@ INSERT INTO users (name, email) ## Installation +> Don't forget to edit `pg_hba.conf` in order to allow server connectiosn to postgres if on the same machine! + ```bash git clone git@github.com:lingqingmeng/node-api-postgres cd node-api-postgres From b1eaad237d4ac2a313d5a8147e78e8d6baa89075 Mon Sep 17 00:00:00 2001 From: Ling Qing Meng Date: Thu, 16 Dec 2021 13:24:25 -0500 Subject: [PATCH 07/15] configuration management so no need to expose credentials in git --- .gitignore | 3 +- README.md | 10 +++- index.js | 17 +++--- package-lock.json | 147 ++++++++++++++++++++++++++++------------------ package.json | 2 +- parser.js | 35 +++++++++++ queries.js | 23 +++++--- 7 files changed, 160 insertions(+), 77 deletions(-) create mode 100644 parser.js diff --git a/.gitignore b/.gitignore index 30bc162..da6ffab 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -/node_modules \ No newline at end of file +/node_modules +.cache.md diff --git a/README.md b/README.md index e8cd43c..76522ab 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ brew services start postgresql psql postgres ``` + ```sql CREATE ROLE me WITH LOGIN PASSWORD 'password'; ALTER ROLE me CREATEDB; @@ -37,11 +38,18 @@ INSERT INTO users (name, email) ## Installation ```bash -git clone git@github.com:taniarascia/node-api-postgres +git clone git@github.com:lingqingmeng/node-api-postgres cd node-api-postgres npm install node index.js ``` +## Usage + +```bash +node index.js -U 'my_pg_user' -p 'my_pg_password' +``` + + ## Commands diff --git a/index.js b/index.js index ac9b842..d9e55d5 100644 --- a/index.js +++ b/index.js @@ -1,15 +1,18 @@ -const express = require('express') -const bodyParser = require('body-parser') -const app = express() -const db = require('./queries') -const port = 3000 +"use strict"; -app.use(bodyParser.json()) +const express = require('express'); +const bodyParser = require('body-parser'); +const app = express(); +const db = require('./queries'); +const port = 3000; +const fs = require('fs'); + +app.use(bodyParser.json()); app.use( bodyParser.urlencoded({ extended: true, }) -) +); app.get('/', (request, response) => { response.json({ info: 'Node.js, Express, and Postgres API' }) diff --git a/package-lock.json b/package-lock.json index de63095..e8d2c4e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "pgapp", + "name": "node-api-postgres", "version": "1.0.0", "lockfileVersion": 1, "requires": true, @@ -36,9 +36,9 @@ } }, "buffer-writer": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-1.0.1.tgz", - "integrity": "sha1-Iqk2kB4wKa/NdUfrRIfOtpejvwg=" + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-2.0.0.tgz", + "integrity": "sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==" }, "bytes": { "version": "3.0.0", @@ -242,9 +242,9 @@ } }, "packet-reader": { - "version": "0.3.1", - "resolved": "https://registry.npmjs.org/packet-reader/-/packet-reader-0.3.1.tgz", - "integrity": "sha1-zWLmCvjX/qinBexP+ZCHHEaHHyc=" + "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==" }, "parseurl": { "version": "1.3.2", @@ -257,52 +257,63 @@ "integrity": "sha1-32BBeABfUi8V60SQ5yR6G/qmf4w=" }, "pg": { - "version": "7.4.3", - "resolved": "https://registry.npmjs.org/pg/-/pg-7.4.3.tgz", - "integrity": "sha1-97b5P1NA7MJZavu5ShPj1rYJg0s=", + "version": "8.7.1", + "resolved": "https://registry.npmjs.org/pg/-/pg-8.7.1.tgz", + "integrity": "sha512-7bdYcv7V6U3KAtWjpQJJBww0UEsWuh4yQ/EjNf2HeO/NnvKjpvhEIe/A/TleP6wtmSKnUnghs5A9jUoK6iDdkA==", "requires": { - "buffer-writer": "1.0.1", - "packet-reader": "0.3.1", - "pg-connection-string": "0.1.3", - "pg-pool": "~2.0.3", - "pg-types": "~1.12.1", - "pgpass": "1.x", - "semver": "4.3.2" + "buffer-writer": "2.0.0", + "packet-reader": "1.0.0", + "pg-connection-string": "^2.5.0", + "pg-pool": "^3.4.1", + "pg-protocol": "^1.5.0", + "pg-types": "^2.1.0", + "pgpass": "1.x" } }, "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=" + "version": "2.5.0", + "resolved": "https://registry.npmjs.org/pg-connection-string/-/pg-connection-string-2.5.0.tgz", + "integrity": "sha512-r5o/V/ORTA6TmUnyWZR9nCj1klXCO2CEKNRlVuJptZe85QuhFayC7WeMic7ndayT5IRIR0S0xFxFi2ousartlQ==" + }, + "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": "2.0.3", - "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-2.0.3.tgz", - "integrity": "sha1-wCIDLIlJ8xKk+R+2QJzgQHa+Mlc=" + "version": "3.4.1", + "resolved": "https://registry.npmjs.org/pg-pool/-/pg-pool-3.4.1.tgz", + "integrity": "sha512-TVHxR/gf3MeJRvchgNHxsYsTCHQ+4wm3VIHSS19z8NC0+gioEhq1okDY1sm/TYbfoP6JLFx01s0ShvZ3puP/iQ==" + }, + "pg-protocol": { + "version": "1.5.0", + "resolved": "https://registry.npmjs.org/pg-protocol/-/pg-protocol-1.5.0.tgz", + "integrity": "sha512-muRttij7H8TqRNu/DxrAJQITO4Ac7RmX3Klyr/9mJEOBeIpgnF8f9jAfRz5d3XwQZl5qBjF9gLsUtMPJE0vezQ==" }, "pg-types": { - "version": "1.12.1", - "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-1.12.1.tgz", - "integrity": "sha1-1kCH45A7WP+q0nnnWVxSIIoUw9I=", + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/pg-types/-/pg-types-2.2.0.tgz", + "integrity": "sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==", "requires": { - "postgres-array": "~1.0.0", + "pg-int8": "1.0.1", + "postgres-array": "~2.0.0", "postgres-bytea": "~1.0.0", - "postgres-date": "~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=", + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/pgpass/-/pgpass-1.0.4.tgz", + "integrity": "sha512-YmuA56alyBq7M59vxVBfPJrGSozru8QAdoNlWuW3cz8l+UX3cWge0vTvjKhsSHSJpo3Bom8/Mm6hf0TR5GY0+w==", "requires": { - "split": "^1.0.0" + "split2": "^3.1.1" } }, "postgres-array": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/postgres-array/-/postgres-array-1.0.3.tgz", - "integrity": "sha512-5wClXrAP0+78mcsNX3/ithQ5exKvCyK5lr5NEEEeGwwM6NJdQgzIJBVxLvRW+huFpX92F2QnZ5CcokH0VhK2qQ==" + "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", @@ -310,14 +321,14 @@ "integrity": "sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=" }, "postgres-date": { - "version": "1.0.3", - "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.3.tgz", - "integrity": "sha1-4tiXAu/bJY/52c7g/pG9BpdSV6g=" + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/postgres-date/-/postgres-date-1.0.7.tgz", + "integrity": "sha512-suDmjLVQg78nMK2UZ454hAG+OAW+HQPZ6n++TNDUX+L0+uUlLywnoxJKDou51Zm+zTCjrCl0Nq6J9C5hP9vK/Q==" }, "postgres-interval": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/postgres-interval/-/postgres-interval-1.1.2.tgz", - "integrity": "sha512-fC3xNHeTskCxL1dC8KOtxXt7YeFmlbTYtn7ul8MkVERuTmf7pI4DrkAxcw3kh1fQ9uz4wQmd03a1mRiXUZChfQ==", + "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" } @@ -375,16 +386,21 @@ } } }, + "readable-stream": { + "version": "3.6.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", + "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "requires": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + } + }, "safe-buffer": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" }, - "semver": { - "version": "4.3.2", - "resolved": "https://registry.npmjs.org/semver/-/semver-4.3.2.tgz", - "integrity": "sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=" - }, "send": { "version": "0.16.2", "resolved": "https://registry.npmjs.org/send/-/send-0.16.2.tgz", @@ -421,12 +437,12 @@ "resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.0.tgz", "integrity": "sha512-BvE/TwpZX4FXExxOxZyRGQQv651MSwmWKZGqvmPcRIjDqWub67kTKuIMx43cZZrS/cBBzwBcNDWoFxt2XEFIpQ==" }, - "split": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/split/-/split-1.0.1.tgz", - "integrity": "sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==", + "split2": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/split2/-/split2-3.2.2.tgz", + "integrity": "sha512-9NThjpgZnifTkJpzTZ7Eue85S49QwpNhZTq6GRJwObb6jnLFNGB7Qm73V5HewTROPyxD0C29xqmaI68bQtV+hg==", "requires": { - "through": "2" + "readable-stream": "^3.0.0" } }, "statuses": { @@ -434,10 +450,20 @@ "resolved": "https://registry.npmjs.org/statuses/-/statuses-1.4.0.tgz", "integrity": "sha512-zhSCtt8v2NDrRlPQpCNtw/heZLtfUDqxBM1udqikb/Hbk52LK4nQSwr10u77iopCW5LsyHpuXS0GnEc48mLeew==" }, - "through": { - "version": "2.3.8", - "resolved": "http://registry.npmjs.org/through/-/through-2.3.8.tgz", - "integrity": "sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=" + "string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "requires": { + "safe-buffer": "~5.2.0" + }, + "dependencies": { + "safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==" + } + } }, "type-is": { "version": "1.6.16", @@ -453,6 +479,11 @@ "resolved": "https://registry.npmjs.org/unpipe/-/unpipe-1.0.0.tgz", "integrity": "sha1-sr9O6FFKrmFltIF4KdIbLvSZBOw=" }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=" + }, "utils-merge": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/utils-merge/-/utils-merge-1.0.1.tgz", @@ -464,9 +495,9 @@ "integrity": "sha1-IpnwLG3tMNSllhsLn3RSShj2NPw=" }, "xtend": { - "version": "4.0.1", - "resolved": "https://registry.npmjs.org/xtend/-/xtend-4.0.1.tgz", - "integrity": "sha1-pcbVMr5lbiPbgg77lDofBJmNY68=" + "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 index 92a504e..ad6cc39 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,6 @@ "license": "MIT", "dependencies": { "express": "^4.16.3", - "pg": "^7.4.3" + "pg": "8.7.1" } } diff --git a/parser.js b/parser.js new file mode 100644 index 0000000..f4b6927 --- /dev/null +++ b/parser.js @@ -0,0 +1,35 @@ +"use strict"; + +module.exports = (function () { + function Parser(argsList){ + this.argsList = argsList.map(function (a) { + return a; + }) + } + + /** + * @description If cli has hyphen for -U return the element ahead of it + * @return {number} Default user is postgres + */ + Parser.prototype.getPgUser = function (credentials) { + if (this.argsList.indexOf("-U") !== -1) { + return this.argsList[this.argsList.indexOf("-U") + 1] + } + + return 'postgres'; + }; + + /** + * @description If cli has hyphen for -p retrieve password + * @return {number} Default pw is password + */ + Parser.prototype.getPgPassword = function (credentials) { + if (this.argsList.indexOf("-p") !== -1) { + return this.argsList[this.argsList.indexOf("-p") + 1] + } + + return 'password' + }; + + return Parser; +})() \ No newline at end of file diff --git a/queries.js b/queries.js index 9b95fb5..005e582 100644 --- a/queries.js +++ b/queries.js @@ -1,14 +1,19 @@ -const Pool = require('pg').Pool +"use strict"; + +const Parser = require("./parser"); +let parser = new Parser(process.argv); + +const Pool = require('pg').Pool; const pool = new Pool({ - user: 'me', + user: parser.getPgUser(), host: 'localhost', database: 'api', - password: 'password', + password: parser.getPgPassword(), port: 5432, -}) +}); const getUsers = (request, response) => { - pool.query('SELECT * FROM users ORDER BY id ASC', (error, results) => { + pool.query('SELECT * FROM users ORDER BY id ASC;', (error, results) => { if (error) { throw error } @@ -19,7 +24,7 @@ const getUsers = (request, response) => { const getUserById = (request, response) => { const id = parseInt(request.params.id) - pool.query('SELECT * FROM users WHERE id = $1', [id], (error, results) => { + pool.query('SELECT * FROM users WHERE id = $1;', [id], (error, results) => { if (error) { throw error } @@ -30,7 +35,7 @@ const getUserById = (request, response) => { const createUser = (request, response) => { const { name, email } = request.body - pool.query('INSERT INTO users (name, email) VALUES ($1, $2)', [name, email], (error, results) => { + pool.query('INSERT INTO users (name, email) VALUES ($1, $2);', [name, email], (error, results) => { if (error) { throw error } @@ -43,7 +48,7 @@ const updateUser = (request, response) => { const { name, email } = request.body pool.query( - 'UPDATE users SET name = $1, email = $2 WHERE id = $3', + 'UPDATE users SET name = $1, email = $2 WHERE id = $3;', [name, email, id], (error, results) => { if (error) { @@ -57,7 +62,7 @@ const updateUser = (request, response) => { const deleteUser = (request, response) => { const id = parseInt(request.params.id) - pool.query('DELETE FROM users WHERE id = $1', [id], (error, results) => { + pool.query('DELETE FROM users WHERE id = $1;', [id], (error, results) => { if (error) { throw error } From 727c2d0e2ae9257856689c310df6e6e7ef83da77 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Thu, 16 Dec 2021 13:26:35 -0500 Subject: [PATCH 08/15] Update parser.js --- parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parser.js b/parser.js index f4b6927..972f517 100644 --- a/parser.js +++ b/parser.js @@ -16,7 +16,7 @@ module.exports = (function () { return this.argsList[this.argsList.indexOf("-U") + 1] } - return 'postgres'; + return 'api'; }; /** @@ -32,4 +32,4 @@ module.exports = (function () { }; return Parser; -})() \ No newline at end of file +})() From 60c6a13163c7ca484808f456f963ee8f2ad7f126 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Thu, 16 Dec 2021 13:27:30 -0500 Subject: [PATCH 09/15] Update parser.js --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 972f517..7a266c0 100644 --- a/parser.js +++ b/parser.js @@ -16,7 +16,7 @@ module.exports = (function () { return this.argsList[this.argsList.indexOf("-U") + 1] } - return 'api'; + return 'me'; }; /** From 08f5a16efb6c1998ddfff82076679dd198b60ea0 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Thu, 16 Dec 2021 13:27:58 -0500 Subject: [PATCH 10/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 758ac5f..0c81eda 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ node index.js ## Usage ```bash -node index.js -U 'my_pg_user' -p 'my_pg_password' +node index.js -U 'me' -p 'password' ``` From 99c8990ca0ee98ab4d209d761a0ac70b259e3ed8 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Sat, 25 Dec 2021 11:13:28 -0800 Subject: [PATCH 11/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 0c81eda..ce214d4 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,7 @@ INSERT INTO users (name, email) ## Installation -> Don't forget to edit `pg_hba.conf` in order to allow server connectiosn to postgres if on the same machine! +> Don't forget to edit `pg_hba.conf` in order to allow server connections to postgres if on the same machine! ```bash git clone git@github.com:lingqingmeng/node-api-postgres From 40fe7e362be42f5f65bcbc64d5246aaa9a19ae3f Mon Sep 17 00:00:00 2001 From: GM Date: Sat, 1 Jan 2022 20:06:38 -0800 Subject: [PATCH 12/15] cleanup --- .gitignore | 1 + README.md | 26 ++++++++++++++++---------- queries.js | 6 ++++-- 3 files changed, 21 insertions(+), 12 deletions(-) diff --git a/.gitignore b/.gitignore index da6ffab..7511d17 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /node_modules .cache.md +SECURITY.md \ No newline at end of file diff --git a/README.md b/README.md index ce214d4..9c3caeb 100644 --- a/README.md +++ b/README.md @@ -32,10 +32,16 @@ CREATE TABLE users ( ); INSERT INTO users (name, email) - VALUES ('Alice', 'alice@ondecentral.com'), ('Bob', 'bob@ondecentral.com'); + VALUES ('Jerry', 'jerry@example.com'), ('George', 'george@example.com'); ``` -## Installation +## Installation for Mac + +> Currently tested with [Postgres.app](https://postgresapp.com/downloads.html) + +Applications are ran with group `wheel` in Mac. Applications and Users in that group have elevated `su` permissions. + +## Installation for Windows > Don't forget to edit `pg_hba.conf` in order to allow server connections to postgres if on the same machine! @@ -45,6 +51,7 @@ cd node-api-postgres npm install node index.js ``` + ## Usage ```bash @@ -53,22 +60,21 @@ node index.js -U 'me' -p 'password' -## Commands -- GET: `curl http://localhost:3000/settings/locale` -- PUT: `curl -X PUT -d "val=Georgia" http://localhost:3000/settings/locale` +## Commands + - GET: `curl http://localhost:3000/users` -- POST: `curl --data "name=Alice&email=alice@ondecentral.com" http://localhost:3000/users` -- PUT: `curl -X PUT -d "name=Bob" -d "email=bob@ondecentral.com" http://localhost:3000/users/1` +- POST: `curl --data "name=Jerry&email=jerry@example.com" http://localhost:3000/users` +- PUT: `curl -X PUT -d "name=George" -d "email=george@example.com" http://localhost:3000/users/1` - DELETE: `curl -X "DELETE" http://localhost:3000/users/1` -## Original Repository - -Credit goes to: +## Author + - [Tania Rascia](https://www.taniarascia.com) ## License This project is open source and available under the [MIT License](LICENSE). + diff --git a/queries.js b/queries.js index 005e582..45b3b59 100644 --- a/queries.js +++ b/queries.js @@ -35,11 +35,13 @@ const getUserById = (request, response) => { const createUser = (request, response) => { const { name, email } = request.body - pool.query('INSERT INTO users (name, email) VALUES ($1, $2);', [name, email], (error, results) => { + pool.query('INSERT INTO users (name, email) VALUES ($1, $2) RETURNING id;', [name, email], (error, results) => { if (error) { throw error } - response.status(201).send(`User added with ID: ${results.insertId}`) + let { rows } = results; + let idCreated = rows.pop().id; + response.status(201).send(`User added with ID: ${idCreated}`) }) } From 17d9edec908a6d70b5414949df1f6b1b19e58884 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Sat, 1 Jan 2022 20:14:23 -0800 Subject: [PATCH 13/15] Update parser.js --- parser.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/parser.js b/parser.js index 7a266c0..b5282f0 100644 --- a/parser.js +++ b/parser.js @@ -9,7 +9,7 @@ module.exports = (function () { /** * @description If cli has hyphen for -U return the element ahead of it - * @return {number} Default user is postgres + * @return {number} Default user is me */ Parser.prototype.getPgUser = function (credentials) { if (this.argsList.indexOf("-U") !== -1) { From f3168963f4a12fbacfc5a149e4cb505b6e225eb7 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Sat, 1 Jan 2022 20:14:52 -0800 Subject: [PATCH 14/15] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9c3caeb..88eac2d 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ Applications are ran with group `wheel` in Mac. Applications and Users in that g > Don't forget to edit `pg_hba.conf` in order to allow server connections to postgres if on the same machine! ```bash -git clone git@github.com:lingqingmeng/node-api-postgres +git clone git@github.com:taniarascia/node-api-postgres cd node-api-postgres npm install node index.js From 0cc27efca0e9fda78e4f45bffdaddd955e36b020 Mon Sep 17 00:00:00 2001 From: Ling Q Meng Date: Sat, 1 Jan 2022 20:18:56 -0800 Subject: [PATCH 15/15] Update parser.js --- parser.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/parser.js b/parser.js index b5282f0..91059a6 100644 --- a/parser.js +++ b/parser.js @@ -8,7 +8,7 @@ module.exports = (function () { } /** - * @description If cli has hyphen for -U return the element ahead of it + * @description If cli detects hyphen for -U return the element ahead of it * @return {number} Default user is me */ Parser.prototype.getPgUser = function (credentials) { @@ -20,7 +20,7 @@ module.exports = (function () { }; /** - * @description If cli has hyphen for -p retrieve password + * @description If cli detects hyphen for -p retrieve password in the element ahead * @return {number} Default pw is password */ Parser.prototype.getPgPassword = function (credentials) {