Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save scootcho/38718b05492b6ff10ad114c5c4a2ab1b to your computer and use it in GitHub Desktop.
Save scootcho/38718b05492b6ff10ad114c5c4a2ab1b to your computer and use it in GitHub Desktop.

Revisions

  1. @laurenfazah laurenfazah revised this gist Dec 11, 2017. No changes.
  2. @laurenfazah laurenfazah created this gist Dec 11, 2017.
    148 changes: 148 additions & 0 deletions express_postgress_knex.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,148 @@
    # Express & Postgres via Knex

    > Note: `<example>` is meant to denote text replaced by you (including brackets).
    ## Setup

    ```js
    // global dependencies
    npm install -g knex

    ```

    ```js
    // project's dependencies
    npm install knex pg --save
    ```

    ```js
    // project's dev dependencies
    npm nodemon --save-dev
    ```

    ### DB Setup

    ```bash
    $ psql
    ```

    ```sql
    CREATE DATABASE <example>;
    CREATE DATABASE <example_test>;
    ```

    ## Knex

    ### Config

    ```bash
    $ knex init
    Created ./knexfile.js
    ```

    Replace contents of `./knexfile.js` with:

    ```js
    module.exports = {
    development: {
    client: 'pg',
    connection:'postgres://localhost/<examples>',
    migrations: {
    directory: './db/migrations'
    },
    seeds: {
    directory: './db/seeds/dev'
    },
    useNullAsDefault: true
    },

    test: {
    client: 'pg',
    connection:'postgres://localhost/<examples_test>',
    migrations: {
    directory: './db/migrations'
    },
    seeds: {
    directory: './db/seeds/test'
    },
    useNullAsDefault: true
    },

    production: {
    client: 'pg',
    connection: process.env.DATABASE_URL,
    migrations: {
    directory: './db/migrations'
    },
    seeds: {
    directory: './db/seeds/production'
    },
    useNullAsDefault: true
    }
    }
    ```

    ### Migrations

    #### Create Table

    ```bash
    $ knex migrate:make create-<example>-table
    ```
    #### `up` and `down` Functions

    Populate those functions in your `migrations/<example>.js` file.
    For example:

    ```js
    exports.up = function(knex, Promise) {
    let createQuery = `CREATE TABLE <examples>(
    id SERIAL PRIMARY KEY NOT NULL,
    message TEXT,
    created_at TIMESTAMP
    )`
    return knex.raw(createQuery)
    }

    exports.down = function(knex, Promise) {
    let dropQuery = `DROP TABLE <examples>`
    return knex.raw(dropQuery)
    }
    ```

    #### Run Migrations

    ```
    $ knex migrate:latest
    ```

    Add `--env=test` to migrate your test database.


    ### Seeds

    #### Create Seeds

    ```bash
    $ knex seed:make <examples>
    ```

    Replace the function in `seeds/dev/<examples>.js` with your own seeds.d

    #### Seed DB

    ```bash
    $ knex seed:run
    ```

    ## Requiring Necessary Modules into Express

    > The following lines may need their paths adjusted depending on where in the project they're used.
    ```js
    const environment = process.env.NODE_ENV || 'development'; // if something else isn't setting ENV, use development
    const configuration = require('../knexfile')[environment]; // require environment's settings from knexfile
    const database = require('knex')(configuration); // connect to DB via knex using env's settings
    ```

    You're ready to get devving!