Coder Social home page Coder Social logo

russellafisher / express-knex-postgres-heroku-setup Goto Github PK

View Code? Open in Web Editor NEW

This project forked from evan-mcclaugherty/express-knex-postgres-heroku-setup

0.0 2.0 0.0 14 KB

Knex CRUD app boilerplate using express and handlebars.

JavaScript 90.67% CSS 2.56% HTML 6.77%

express-knex-postgres-heroku-setup's Introduction

Boilerplate for a knex CRUD app

important: The commands for all these steps are at the bottom if you don't know them.

  1. cd into clone && run npm install.
  2. Create a local db.
  3. Change knexfile.js to reflect your local db name.
  4. Create a heroku app && postgresql addon.
  5. put your heroku-postgresql url in the .env_example and rename file right away to .env.
  6. Run knex commands to make migrations && seeds.
  7. Migrate latest!
  8. Don't forget to migrate to heroku.
  9. Enjoy!

Steps on how I setup this CRUD project

Create Express App
  • express --hbs name_of_app creates new express app in a directory called name_of_app
    • --hbs adds handlebars
    • assumes you have express-generator package installed globally (npm install -g express-generator)
  • cd name_of_app && npm i
    • npm i installs all dependencies denoted by package.json
Set Up Knex
  • npm i -S pg knex installs pg & knex, & adds both to the dependencies in package.json
    • in 'index.js' file, add var pg = require('pg');
    • pg is a module that lets you connect to psql; must be installed on a per project basis
    • assumes you have knex package installed globally (npm i knex -g)
  • knex init creates 'knexfile.js' in root directory
  • In root directory, add a folder called 'db' & create 'knex.js' inside it
    • in index.js file, add var knex = require('../db/knex');
    • refer to 'db/knex.js' in this repo for how to define your environment configurations
Initialize Git & Set Up .env File
  • git init initializes git repo
  • echo node_modules > .gitignore to add node modules to a .gitignore file OR if you have gitignore installed globally, use command gitignore node
  • npm i -S dotenv installs dotenv module & adds to dependencies in package.json, which loads environment variables from a .env file into process.env
  • touch .env in root directory to create empty .env file
    • add 1 key-value pair per line; when assigning multiple values to one key, separate with :
    • this is where we define DATABASE_URL=yourURL later
    • add require('dotenv').config(); to top of 'knexfile.js' (so that we can access the DATABASE_URL variable through process.env when we define our production environment connection). also add it to 'app.js' (if you're adding other variables that your app will refer to, so it will be available to all routes).
  • echo .env >> .gitignore adds .env file to .gitignore so git doesn't track it
    • Why? Because we are going to set the DATABASE_URL variable equal to our heroku URL, which contains a password that we don't want others to see
Set Up Heroku App & Database
  • createdb name_of_app creates psql database locally
  • heroku apps:create name-of-app creates a heroku app (Name must start with a letter and can only contain lowercase letters, numbers, and dashes.)
  • heroku addons:create heroku-postgresql --app name_of_app adds postgresql database to the app
  • heroku config returns the URL
    • add URL to .env file DATABASE_URL=yourURL?ssl=true
  • create a repo on github & git remote add origin ssh_of_repo to add the remote
  • git add/commit/push to github
  • if you want to open a psql shell ๐Ÿš to the heroku database & run sql commands in the terminal: heroku pg:psql --app name_of_app
Migrating & Seeding
  • knex migrate:make table_name to create a new migration
    • this creates a folder called 'migrations' with a new 'timestamp_table_name.js' file
    • Each time you change the table you will need to rerun this command & change the contents of the js file
  • knex migrate:latest runs knex migration locally (whatever development environment is set to)
  • knex seed:make seed_name to create a new seed ๐ŸŒฑ
    • this creates a folder called 'seeds' with a new 'timestamp_seed_name.js' file
  • knex seed:run to run seed the local database

If deploying to heroku,

  • knex migrate:latest --env production runs knex migration locally against heroku database in the cloud
  • knex seed:run --env production runs knex locally & puts seed data in heroku database
  • git push heroku master to push to heroku ๐ŸŽ‰๐ŸŽˆ๐ŸŽŠ

express-knex-postgres-heroku-setup's People

Contributors

evan-mcclaugherty avatar evnmck avatar laneysmith avatar russellafisher avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.