You will need express-generator installed globally to have access to the 'express' cli command
npm install -g express-generator
- Create a GitHub repo
- Clone the repo and cd into it
Console:
express --view=hbs --git
npm install pg knex dotenv
knex init
createdb *database-name*
touch .env
In .gitignore file:
node_modules
.env
In knexfile.js
require('dotenv').config();
module.exports = {
development: {
client: 'pg',
connection: 'postgres://localhost/*database-name*'
},
production: {
client: 'pg',
connection: process.env.DATABASE_URL + '?ssl=true'
}
};
Console:
mkdir db
cd db
touch knex.js
In knex.js
const environment = process.env.NODE_ENV || 'development';
const config = require('../knexfile.js')[environment];
module.exports = require('knex')(config);
Console:
knex migrate:make 01_migration_name
Migration file should look something like:
exports.up = function(knex, Promise) {
return knex.schema.createTable('member', (table) =>{
table.increments();
table.text('username').notNullable().unique();
table.text('email').notNullable().unique();
table.text('password').notNullable();
table.date('dateCreated').notNullable().defaultTo(new Date);
table.boolean('isActive').notNullable().defaultTo(true);
table.text('bio');
})
};
// express knex 4 lyfe
exports.down = function(knex, Promise) {
return knex.schema.dropTableIfExists('member');
};
Foreign Key relationships are handled as such:
table.integer('memberID').references('member.id').unsigned().onDelete('cascade')
Run migration
knex migrate:latest
- Continue making migrations for all the tables in your database
Console:
knex seed:make 01_seed_name
Seed file should look something like:
var bcrypt = require('bcrypt');
exports.seed = function(knex, Promise) {
// Deletes ALL existing entries
return knex.raw('DELETE FROM "user"; ALTER SEQUENCE user_id_seq RESTART WITH 3;')
.then(function () {
var users = [{
id: 1,
name: 'sam',
email: 'sam@gmail.com',
password: bcrypt.hashSync('sammyg21', 10)
}, {
id: 2,
name: 'alex',
email: 'alex@gmail.com',
password: bcrypt.hashSync('alexmart05', 10)
}];
return knex('user').insert(users);
});
};
Run seed
knex seed:run
- Keep creating seeds and running them until your entire database is seeded
In your routes file make sure to require knex
const knex = require('../db/knex');
Do the same in your app.js file if you plan to write knex queries there:
const knex = require('../db/knex');
Install CORS module
npm install cors
In app.js
const cors = require('cors');
// Make sure the following line is above your routes
app.use(cors());
Fin.