K’NEX provides creative building sets for kids to stimulate curiosity & imagination. To learn all about our innovative building toys for kids of all ages! To debug you can use.toSQL to debug your knex queries documentation. Also, nice cheatsheet As hot fix solution you can use.raw and paste your SQL code there. About WHERE conditions, you can just chain them in the end of your knex query. Something like this.
Project Structure⌗
Here’s the project structure for my application. Adjust to your needs.I used the Express application generator to scaffold the program.
- Knex Node Cheat Sheet in cli for local db -Install postgres (one time only for the psql cli tools) -Install pg in the working folder of code (postgres for node) npm install pg.
- Cheat Sheet: Setting up Express with Postgres via Knex Read other posts ← More Learning Resources During COVID-19 Outbreak Part 3 TIL About Streamlit's Magic →.
Dockerfile & docker-compose⌗
The Dockerfile must have the Postgres connection string as a build argument.
See Docker ARG, ENV and .env - a Complete Guide for more information on ARG, ENV and Docker.
Example excerpt from Dockerfile
:
Example docker-compose.yml
:
Install Knex.js & Postgres⌗
We also use docker-compose.yaml
to create a container for the Postgres database. Let’s call the service db
:
The database folder is located inside the node application: node_app/db
.
Here’s the Dockerfile for the Postgres database:
create.sql
spins up the databases for development, testing, production:
The Postgres connection string inside the docker-compose.yaml
reflects this:
Your Node application must have Knex.js and the postgres driver.
For example:
Initialization⌗
Initialize knex:
Now we can adjust the configuration to our needs:
Migrations⌗
up
and down
functions:You’ll find a new file inside the migrations
folder.
Run migrations:
Seeds⌗
For example:
This will create new folders called seeds/development
and seeds/test
which you can modify.
In my repository, the files are node_app/seeds/development/show-seed.js
and node_app/seeds/tet/show-seed.js
Then run docker-compose exec node_app seed:run --env development
(and --env test
).
Glue Everything Together⌗
Create knex.js
inside the database folder (node_app/db/knex.js
):
Example GitHub Repository⌗
Knex Cheat Sheet
See GitHub.