This tutorial demonstrates how to connect to a Postgres database with Objection.js.
It also demonstrates how to create the database schema and how to create seed data with the knex.js migration tool.
This repository is contains the code for the Objection.js tutorial on Youtube
Please also check out my website at jangoebel.com
For updates, please follow @_jgoebel on Twitter.
This application needs a local Postgres instance.
On macOS you can install Postgres with Homebrew.
brew install postgresql
and start it with
brew services start postgres
You can stop Postgres with:
brew services stop postgres
For other operating systems, please check the Postgres website.
Make sure to update the configuration app with your database credentials in the ../db/knexfile.js
file.
There you need to specify the user
, password
(per default no password
required) and the database
you want to connect to.
The default database we try to connect to is called objection_tutorial
. Please make sure it exists in your local Postgres instance. You can either use a UI like PgAdmin to create the database or you can do so with psql
psql -h localhost -U <your-username> postgres
When installing locally, Postgres creates a database user with your operation system's username. You can run
whoami
on any Unix-like
operating system to find out your user
Then you can create a new database with:
create database objection_tutorial;
Once you have installed Postgres and updated the credentials in the ./db/knexfile.js
, you can
npm run dev
This will:
- execute all existing database migrations located in
./db/migrations
- execute all seed files located in
./db/seed
so you have some initial data - start your server on port
8080
withnodemon
to provide hot reload functionality
npm run down
: undo last migration. Since we only have one migration at the moment this is equivalent to deleting all tablesnpm run migrate
: executes all migrations in./db/migrations
npm run make-seed
: create a new seed file