Awesome
next-postgres-with-typescript
This is a fork of the wonderful next-postgres repo by @jimmylee with additional support for Typescript and Jest. This should compile successfully as is, but it still relies on many 'any' types. To use, refer to the Setup instructions below.
Changes
- Most files in
~/common
,~/components
,~/higher-order
, and~/pages
have been converted to .tsx with provisional types. - A
~/data-models
file has been created to keep all common types/interfaces. - Jest and Enzyme are setup to allow for tests (currently only for unconnected, pure components. See issues.).
Remaining issues
IUser
,IPost
, andIComment
need to be defined (currently all 'any')dispatch
needs to be correctly typed (currently all 'any')~/higher-order/withData.js
needs to converted to Typescript~/pages/_document.js
needs to converted to Typescript~/api
files need to be converted to Typescript files- ? Redux and Next Router need to be configured for Jest/Enzyme? Currently fails, likely bc components not connected to Provider/Router.
Contributions
Any help is welcome to make this repo more type safe. Feel free to submit PRs!
next-postgres
This is an example of a full stack web application with...
- posts
- comments
- server side rendering.
It is configured to be deployed to Zeit but I also provide Heroku deployment steps.
It is code you can use without attribution, please enjoy. 🙏
Production Examples:
- Maurice Kenji Clarke used the setup to create: https://indvstry.io/
- I used some of the ideas here for a serious project: Reading Supply
Preview:
Stack
- NextJS + Custom Express
- Emotion CSS-in-JS
- Postgres
- Sequelize: PostgresSQL ORM
- Passport for local authentication
- Redux
- Babel
Why is this useful? Why should I care?
- The UX and UI are garbage, that means everything you do after will be better!
- Client and server are written in JavaScript.
- This is a production ready codebase you can use to test a concept you have.
- Server side rendering has been made simple.
- Maybe you want to get a head start at a hackathon.
Setup: Prerequisites
I use Homebrew to manage dependencies on a new laptop... You're welcome to use something else.
- Install Postgres:
brew install postgres
. - Install Node 10.7.0+:
brew install node
. (Or update your node)
Setup: Quick newbies guide to Postgres
- On OSX, to run Postgres in a tab on the default port.
postgres -D /usr/local/var/postgres -p 5432
- Postgres config is stored in
./config.js
. - Local database:
sampledb
. - Username and password as
test
.
First time Postgres instructions.
# Enter Postgres console
psql postgres
# Create a new user for yourself
CREATE ROLE yourname WITH LOGIN PASSWORD 'yourname';
# Allow yourself to create databases
ALTER ROLE yourname CREATEDB;
# Exit Postgres console
\q
# Log in as your new user.
psql postgres -U yourname
# Create a database named: sampledb.
# If you change this, update config.js
CREATE DATABASE sampledb;
# Give your self privileges
GRANT ALL PRIVILEGES ON DATABASE sampledb TO yourname;
# List all of your databases
\list
# Connect to your newly created DB as a test
\connect sampledb
# Exit Postgres console
\q
I also use a GUI called TablePlus if you don't like command line.
Setup: Run locally
In the root directory run these commands:
npm install
npm install -g babel-cli
npm install -g sequelize-cli
sequelize db:migrate
npm run dev
Visit localhost:8000
in a browser to start development locally. You will need postgres running.
Deploy Heroku (Alternative choice)
To deploy with Heroku, please follow the instructions here.
There are very specific details you must pay attention to.
Deploy Zeit
Deploying with now-cli
is as simple as
now
Do you have a custom domain? You can use an alias
# after the deploy, alias to your domain, add "alias" to now.json first
now alias
Make sure you configure your alias for zeit.world. Also make sure you add the secrets you need or delete the ones you aren't using from now-secrets.json
.
Database + Secrets
You can use a service like https://compose.io to get a hosted Postgres database.
Then update production secrets using now secrets
. You must do this or else your website can't connect to the production database.
now secrets add production-username xxxxxxxxxxxxxxx
now secrets add production-password xxxxxxxxxxxxxxx
now secrets add production-database xxxxxxxxxxxxxxx
now secrets add production-host xxxxxxxxxxxxxxx
now secrets add production-port xxxxxxxxxxxxxxx
now secrets add production-secret xxxxxxxxxxxxxxx
Questions?
Feel free to slang any feels to @wwwjim.