Todo application using Node.js, Express, postrges, Knex.js, and handlebars
- Install Node
-
Clone Repository:
$ git clone https://github.com/egarcia410/todo-app.git
-
Change Directory:
$ cd todo-app
-
Install Dependencies:
$ npm install
-
Enter PostgreSQL shell
$ psql
-
Inside PostgreSQL shell, create a database called
todos
=# CREATE DATABASE todos
-
Update your database
$ knex migrate:latest
A table will be created in your database called todos
-
Run Program:
$ nodemon server.js
-
Install Postgres
-
Install Postres.app
-
Initialize server in Postgres.app
-
Enter PostgreSQL shell
$ psql
-
Inside PostgreSQL shell, create a database
=# CREATE DATABASE <NAME_OF_DATABASE>
-
Install Knex.js and PostgreSQL driver inside working directory
$ npm install --save knex pg
-
Create a new knexfile
$ knex init
A knexfile.js file will be created
-
Configure knexfile.js with appropriate database client
Example using PostgreSQL configuration:
module.exports = { development: { client: 'pg', connection: 'postgres://localhost/<NAME_OF_DATABASE>' }, production: { client: 'postgresql', connection: process.env.DATABASE_URL } };
-
Create a new migration file
$ knex migrate:make <MIGRATION_NAME>
A migrations folder will be created
-
Configure migrations file with a
createTable
anddropTable
-
Update your database
$ knex migrate:latest
A table will be created in your database
-
Create
db
folder$ mkdir db
-
Create
knex.js
file insidedb
folder$ touch db/knex.js
-
Insert into
knex.js
:let environment = process.env.NODE_ENV || 'development'; let config = require('../knexfile.js')[environment]; module.exports = require('knex')(config);
acquires database client and connection
-
Require the exported module in file where queries will be made
Example:
const knex = require('./db/knex');