Add fastify-postgres with example connection

This commit is contained in:
Robbie Antenesse 2019-09-17 15:10:46 -06:00
parent 4a5dbd4091
commit 1cb78f5db2
6 changed files with 145 additions and 5 deletions

View File

@ -23,6 +23,17 @@ An attempt at a viable alternative to Goodreads (currently lacking a name—idea
## Development ## Development
### Requirements
- [Git](https://git-scm.com/)
- [NodeJS 10.16.x](https://nodejs.org/)
- [PostgreSQL 11](https://www.postgresql.org/download/)
- See the following articles for guidance on how to install it:
- Windows: http://www.postgresqltutorial.com/install-postgresql/
- Ubuntu: https://www.digitalocean.com/community/tutorials/how-to-install-and-use-postgresql-on-ubuntu-18-04
- Be sure you set up an account for a readlebee database and [set a password for the account](https://stackoverflow.com/a/12721095)
- I plan to write up a tutorial on how to get this set up in the wiki at some point soon by combining these links
### Installation ### Installation
To develop, you'll need to know how to use a terminal or shell on your computer. To develop, you'll need to know how to use a terminal or shell on your computer.

View File

@ -7,7 +7,7 @@
"author": "Robbie Antenesse <dev@alamantus.com>", "author": "Robbie Antenesse <dev@alamantus.com>",
"license": "MIT", "license": "MIT",
"scripts": { "scripts": {
"dev": "concurrently \"npm run watch-js\" \"npm run serve\"", "dev": "concurrently --kill-others \"npm run watch-js\" \"npm run serve\"",
"start": "npm run build && cross-env NODE_ENV=production npm run serve", "start": "npm run build && cross-env NODE_ENV=production npm run serve",
"watch-js": "parcel watch app/index.html --out-dir public --no-hmr --no-cache", "watch-js": "parcel watch app/index.html --out-dir public --no-hmr --no-cache",
"serve": "node server/index.js", "serve": "node server/index.js",
@ -38,11 +38,13 @@
"fastify-cookie": "^3.1.0", "fastify-cookie": "^3.1.0",
"fastify-helmet": "^3.0.1", "fastify-helmet": "^3.0.1",
"fastify-jwt": "^1.0.0", "fastify-jwt": "^1.0.0",
"fastify-postgres": "^2.0.0",
"fastify-static": "^2.5.0", "fastify-static": "^2.5.0",
"handlebars": "^4.2.0", "handlebars": "^4.2.0",
"html-minifier": "^4.0.0", "html-minifier": "^4.0.0",
"make-promises-safe": "^5.0.0", "make-promises-safe": "^5.0.0",
"node-fetch": "^2.6.0", "node-fetch": "^2.6.0",
"pg": "^7.12.1",
"picnic": "^6.5.1" "picnic": "^6.5.1"
} }
} }

View File

@ -1,5 +1,10 @@
{ {
"port": 3000, "port": 3000,
"pgsql_host": "localhost",
"pgsql_port": 5432,
"pgsql_database": "readlebee",
"pgsql_username": "postgres",
"pgsql_password": "password",
"jwtSecretKey": "SomethingAtLeast32CharactersLong!", "jwtSecretKey": "SomethingAtLeast32CharactersLong!",
"tokenExpireDays": 7, "tokenExpireDays": 7,
"inventaireDomain": "https://inventaire.io" "inventaireDomain": "https://inventaire.io"

View File

@ -24,6 +24,13 @@ fastify.register(require('fastify-cookie')); // Enable reading and setting http
fastify.register(require('fastify-jwt'), { // Enable creating, parsing, and verifying JSON Web Tokens from the global fastify object fastify.register(require('fastify-jwt'), { // Enable creating, parsing, and verifying JSON Web Tokens from the global fastify object
secret: fastify.siteConfig.jwtSecretKey, // The secret key used to generate JWTs. Make it big and random! secret: fastify.siteConfig.jwtSecretKey, // The secret key used to generate JWTs. Make it big and random!
}); });
fastify.register(require('fastify-postgres'), {
host: fastify.siteConfig.pgsql_host,
port: fastify.siteConfig.pgsql_port,
database: fastify.siteConfig.pgsql_database,
user: fastify.siteConfig.pgsql_username,
password: fastify.siteConfig.pgsql_password,
});
// Every request, check to see if a valid token exists // Every request, check to see if a valid token exists
fastify.addHook('onRequest', (request, reply, done) => { fastify.addHook('onRequest', (request, reply, done) => {
@ -35,7 +42,7 @@ fastify.addHook('onRequest', (request, reply, done) => {
// Routes // Routes
fastify.register(require('./routes/public')); fastify.register(require('./routes/public'));
fastify.register(require('./routes/books')); fastify.register(require('./routes/books'));
// fastify.register(require('./routes/account')); fastify.register(require('./routes/account'));
fastify.register(require('./routes/search')); fastify.register(require('./routes/search'));
// Start the server // Start the server

View File

@ -1,5 +1,18 @@
async function routes(fastify, options) { async function routes(fastify, options) {
fastify.get('/login', async (request, reply) => { fastify.get('/api/test-db-connect', async (request, reply) => {
fastify.pg.connect((err, client, release) => {
if (err) return reply.send(err);
client.query(
'SELECT * FROM test', [], (err, result) => {
release();
return reply.send(err || result);
}
);
})
});
/*fastify.get('/login', async (request, reply) => {
reply.view('login.hbs', { text: request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in' }); reply.view('login.hbs', { text: request.isLoggedInUser ? JSON.stringify(fastify.jwt.decode(request.cookies.token)) : 'you are NOT logged in' });
}); });
@ -23,7 +36,7 @@ async function routes(fastify, options) {
fastify.get('/logout', async (request, reply) => { fastify.get('/logout', async (request, reply) => {
reply.clearCookie('token', { path: '/' }).redirect('/?loggedout'); reply.clearCookie('token', { path: '/' }).redirect('/?loggedout');
}); });*/
} }
module.exports = routes module.exports = routes;

102
yarn.lock
View File

@ -1316,6 +1316,11 @@ buffer-indexof-polyfill@~1.0.0:
resolved "https://registry.yarnpkg.com/buffer-indexof-polyfill/-/buffer-indexof-polyfill-1.0.1.tgz#a9fb806ce8145d5428510ce72f278bb363a638bf" resolved "https://registry.yarnpkg.com/buffer-indexof-polyfill/-/buffer-indexof-polyfill-1.0.1.tgz#a9fb806ce8145d5428510ce72f278bb363a638bf"
integrity sha1-qfuAbOgUXVQoUQznLyeLs2OmOL8= integrity sha1-qfuAbOgUXVQoUQznLyeLs2OmOL8=
buffer-writer@2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/buffer-writer/-/buffer-writer-2.0.0.tgz#ce7eb81a38f7829db09c873f2fbb792c0c98ec04"
integrity sha512-a7ZpuTZU1TRtnwyCNW3I5dc0wWNC3VR9S++Ewyk2HHZdrO3CQJqSpd+95Us590V6AL7JqUAH2IwZ/398PmNFgw==
buffer-xor@^1.0.3: buffer-xor@^1.0.3:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9" resolved "https://registry.yarnpkg.com/buffer-xor/-/buffer-xor-1.0.3.tgz#26e61ed1422fb70dd42e6e36729ed51d855fe8d9"
@ -2758,6 +2763,13 @@ fastify-plugin@^1.0.0, fastify-plugin@^1.2.0, fastify-plugin@^1.2.1, fastify-plu
dependencies: dependencies:
semver "^6.0.0" semver "^6.0.0"
fastify-postgres@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/fastify-postgres/-/fastify-postgres-2.0.0.tgz#5a7f7aabb8121bff1427f4f5f6a37c6e7eabe71f"
integrity sha512-JQPHhoDLPCzXtDBhemwOEXHPQ7zW+uCo0TIWO5LipbSKYbf684dyKUSlHixVfTw2mU5optG75XPm9Jb/GMQ8fg==
dependencies:
fastify-plugin "^1.4.0"
fastify-static@^2.5.0: fastify-static@^2.5.0:
version "2.5.0" version "2.5.0"
resolved "https://registry.yarnpkg.com/fastify-static/-/fastify-static-2.5.0.tgz#7b36b83250e1a3e008043c5ab7ba4295ba895017" resolved "https://registry.yarnpkg.com/fastify-static/-/fastify-static-2.5.0.tgz#7b36b83250e1a3e008043c5ab7ba4295ba895017"
@ -4886,6 +4898,11 @@ p-try@^2.0.0:
resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6" resolved "https://registry.yarnpkg.com/p-try/-/p-try-2.2.0.tgz#cb2868540e313d61de58fafbe35ce9004d5540e6"
integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ== integrity sha512-R4nPAVTAU0B9D35/Gk3uJf/7XYbQcyohSKdvAxIRSNghFl4e71hVoGnBNQz9cWaXxO2I10KTC+3jMdvvoKw6dQ==
packet-reader@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/packet-reader/-/packet-reader-1.0.0.tgz#9238e5480dedabacfe1fe3f2771063f164157d74"
integrity sha512-HAKu/fG3HpHFO0AA8WE8q2g+gBJaZ9MG7fcKk+IJPLTGAD6Psw4443l+9DGRbOIh3/aXr7Phy0TjilYivJo5XQ==
pako@^0.2.5: pako@^0.2.5:
version "0.2.9" version "0.2.9"
resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75" resolved "https://registry.yarnpkg.com/pako/-/pako-0.2.9.tgz#f3f7522f4ef782348da8161bad9ecfd51bf83a75"
@ -5077,6 +5094,52 @@ performance-now@^2.1.0:
resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b" resolved "https://registry.yarnpkg.com/performance-now/-/performance-now-2.1.0.tgz#6309f4e0e5fa913ec1c69307ae364b4b377c9e7b"
integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns= integrity sha1-Ywn04OX6kT7BxpMHrjZLSzd8nns=
pg-connection-string@0.1.3:
version "0.1.3"
resolved "https://registry.yarnpkg.com/pg-connection-string/-/pg-connection-string-0.1.3.tgz#da1847b20940e42ee1492beaf65d49d91b245df7"
integrity sha1-2hhHsglA5C7hSSvq9l1J2RskXfc=
pg-int8@1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/pg-int8/-/pg-int8-1.0.1.tgz#943bd463bf5b71b4170115f80f8efc9a0c0eb78c"
integrity sha512-WCtabS6t3c8SkpDBUlb1kjOs7l66xsGdKpIPZsg4wR+B3+u9UAum2odSsF9tnvxg80h4ZxLWMy4pRjOsFIqQpw==
pg-pool@^2.0.4:
version "2.0.7"
resolved "https://registry.yarnpkg.com/pg-pool/-/pg-pool-2.0.7.tgz#f14ecab83507941062c313df23f6adcd9fd0ce54"
integrity sha512-UiJyO5B9zZpu32GSlP0tXy8J2NsJ9EFGFfz5v6PSbdz/1hBLX1rNiiy5+mAm5iJJYwfCv4A0EBcQLGWwjbpzZw==
pg-types@^2.1.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/pg-types/-/pg-types-2.2.0.tgz#2d0250d636454f7cfa3b6ae0382fdfa8063254a3"
integrity sha512-qTAAlrEsl8s4OiEQY69wDvcMIdQN6wdz5ojQiOy6YRMuynxenON0O5oCpJI6lshc6scgAY8qvJ2On/p+CXY0GA==
dependencies:
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"
pg@^7.12.1:
version "7.12.1"
resolved "https://registry.yarnpkg.com/pg/-/pg-7.12.1.tgz#880636d46d2efbe0968e64e9fe0eeece8ef72a7e"
integrity sha512-l1UuyfEvoswYfcUe6k+JaxiN+5vkOgYcVSbSuw3FvdLqDbaoa2RJo1zfJKfPsSYPFVERd4GHvX3s2PjG1asSDA==
dependencies:
buffer-writer "2.0.0"
packet-reader "1.0.0"
pg-connection-string "0.1.3"
pg-pool "^2.0.4"
pg-types "^2.1.0"
pgpass "1.x"
semver "4.3.2"
pgpass@1.x:
version "1.0.2"
resolved "https://registry.yarnpkg.com/pgpass/-/pgpass-1.0.2.tgz#2a7bb41b6065b67907e91da1b07c1847c877b306"
integrity sha1-Knu0G2BltnkH6R2hsHwYR8h3swY=
dependencies:
split "^1.0.0"
physical-cpu-count@^2.0.0: physical-cpu-count@^2.0.0:
version "2.0.0" version "2.0.0"
resolved "https://registry.yarnpkg.com/physical-cpu-count/-/physical-cpu-count-2.0.0.tgz#18de2f97e4bf7a9551ad7511942b5496f7aba660" resolved "https://registry.yarnpkg.com/physical-cpu-count/-/physical-cpu-count-2.0.0.tgz#18de2f97e4bf7a9551ad7511942b5496f7aba660"
@ -5481,6 +5544,28 @@ postcss@^7.0.11:
source-map "^0.6.1" source-map "^0.6.1"
supports-color "^6.1.0" supports-color "^6.1.0"
postgres-array@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/postgres-array/-/postgres-array-2.0.0.tgz#48f8fce054fbc69671999329b8834b772652d82e"
integrity sha512-VpZrUqU5A69eQyW2c5CA1jtLecCsN2U/bD6VilrFDWq5+5UIEVO7nazS3TEcHf1zuPYO/sqGvUvW62g86RXZuA==
postgres-bytea@~1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/postgres-bytea/-/postgres-bytea-1.0.0.tgz#027b533c0aa890e26d172d47cf9ccecc521acd35"
integrity sha1-AntTPAqokOJtFy1Hz5zOzFIazTU=
postgres-date@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/postgres-date/-/postgres-date-1.0.4.tgz#1c2728d62ef1bff49abdd35c1f86d4bdf118a728"
integrity sha512-bESRvKVuTrjoBluEcpv2346+6kgB7UlnqWZsnbnCccTNq/pqfj1j6oBaN5+b/NrDXepYUT/HKadqv3iS9lJuVA==
postgres-interval@^1.1.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/postgres-interval/-/postgres-interval-1.2.0.tgz#b460c82cb1587507788819a06aa0fffdb3544695"
integrity sha512-9ZhXKM/rw350N1ovuWHbGxnGh/SNJ4cnxHiM0rxE4VN41wsg8P8zWn9hv/buK00RP4WvlOyr/RBDiptyxVbkZQ==
dependencies:
xtend "^4.0.0"
posthtml-parser@^0.4.0, posthtml-parser@^0.4.1: posthtml-parser@^0.4.0, posthtml-parser@^0.4.1:
version "0.4.1" version "0.4.1"
resolved "https://registry.yarnpkg.com/posthtml-parser/-/posthtml-parser-0.4.1.tgz#95b78fef766fbbe0a6f861b6e95582bc3d1ff933" resolved "https://registry.yarnpkg.com/posthtml-parser/-/posthtml-parser-0.4.1.tgz#95b78fef766fbbe0a6f861b6e95582bc3d1ff933"
@ -6069,6 +6154,11 @@ semver-store@^0.3.0:
resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7" resolved "https://registry.yarnpkg.com/semver/-/semver-5.7.1.tgz#a954f931aeba508d307bbf069eff0c01c96116f7"
integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ== integrity sha512-sauaDf/PZdVgrLTNYHRtpXa1iRiKcaebiKQ1BJdpQlWH2lCvexQdX55snPFyK7QzpudqbCI0qXFfOasHdyNDGQ==
semver@4.3.2:
version "4.3.2"
resolved "https://registry.yarnpkg.com/semver/-/semver-4.3.2.tgz#c7a07158a80bedd052355b770d82d6640f803be7"
integrity sha1-x6BxWKgL7dBSNVt3DYLWZA+AO+c=
semver@^6.0.0, semver@^6.3.0: semver@^6.0.0, semver@^6.3.0:
version "6.3.0" version "6.3.0"
resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d" resolved "https://registry.yarnpkg.com/semver/-/semver-6.3.0.tgz#ee0a64c8af5e8ceea67687b133761e1becbd1d3d"
@ -6345,6 +6435,13 @@ split-string@^3.0.1, split-string@^3.0.2:
dependencies: dependencies:
extend-shallow "^3.0.0" extend-shallow "^3.0.0"
split@^1.0.0:
version "1.0.1"
resolved "https://registry.yarnpkg.com/split/-/split-1.0.1.tgz#605bd9be303aa59fb35f9229fbea0ddec9ea07d9"
integrity sha512-mTyOoPbrivtXnwnIxZRFYRrPNtEFKlpB2fvjSnCQUiAA6qAZzqwna5envK4uk6OIeP17CsdF3rSBGYVBsU0Tkg==
dependencies:
through "2"
sprintf-js@~1.0.2: sprintf-js@~1.0.2:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c" resolved "https://registry.yarnpkg.com/sprintf-js/-/sprintf-js-1.0.3.tgz#04e6926f662895354f3dd015203633b857297e2c"
@ -6668,6 +6765,11 @@ through2@^2.0.0, through2@^2.0.3, through2@~2.0.3:
readable-stream "~2.3.6" readable-stream "~2.3.6"
xtend "~4.0.1" xtend "~4.0.1"
through@2:
version "2.3.8"
resolved "https://registry.yarnpkg.com/through/-/through-2.3.8.tgz#0dd4c9ffaabc357960b1b724115d7e0e86a2e1f5"
integrity sha1-DdTJ/6q8NXlgsbckEV1+Doai4fU=
timers-browserify@^2.0.4: timers-browserify@^2.0.4:
version "2.0.11" version "2.0.11"
resolved "https://registry.yarnpkg.com/timers-browserify/-/timers-browserify-2.0.11.tgz#800b1f3eee272e5bc53ee465a04d0e804c31211f" resolved "https://registry.yarnpkg.com/timers-browserify/-/timers-browserify-2.0.11.tgz#800b1f3eee272e5bc53ee465a04d0e804c31211f"