week5-node-express-knex-pg/README.md

122 lines
1.9 KiB
Markdown
Raw Normal View History

2024-10-22 10:06:50 +01:00
# use knex.js with ES6 on Node and work with .env
2024-10-21 17:09:40 +01:00
2024-10-22 10:06:50 +01:00
## The below instructions are intended for command line execution while working with node server
2024-10-21 17:09:40 +01:00
install dotenv and knex.js
2024-10-22 10:06:50 +01:00
```
npm install dotenv --save
2024-10-21 17:09:40 +01:00
npm install knex --save
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
In the project root directory, create a folder called 'db' and navigate to this 'db' folder:
execute the following command:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
npx knex init
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
A 'knexfile.js' will be created in this directory.
Change the file extension to .cjs
you will need to add the below line in your package.json file:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
"type": "module",
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
create 2 subfolders inside 'db' folder, called "migrations" and "seeds"
# data in .env file
This file should be placed in the project root directory
DEV_USER=
DEV_DB=
DEV_PASSWORD=
DEV_HOST=localhost
DEV_PORT=5432
# knexfile.cjs configuration
2024-10-22 10:06:50 +01:00
> check current working directory
2024-10-21 17:09:40 +01:00
const dir = process.cwd();
2024-10-22 10:06:50 +01:00
> check current running environment
2024-10-21 17:09:40 +01:00
const env = process.env.ENVIRONMENT || "";
2024-10-22 10:06:50 +01:00
> the database details here
2024-10-21 17:09:40 +01:00
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
exports.development = {}
exports.production = {}
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
# below instructions are used on a terminal window navigate to 'db' folder
create a migration file type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
knex migrate:make create_XYZ_table --migrations-directory migrations
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
make sure to rename the file extensions to .cjs
execute migration type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
knex migrate:latest --knexfile knexfile.cjs --migrations-directory migrations
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
execute rollback type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
knex migrate:rollback --knexfile knexfile.cjs --migrations-directory migrations
2024-10-22 10:06:50 +01:00
```
### Check out the knex documentation
2024-10-21 17:09:40 +01:00
knex URL: https://knexjs.org/guide/migrations.html#migration-cli
2024-10-22 10:06:50 +01:00
2024-10-21 17:09:40 +01:00
create a seed file type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
knex seed:make 01_XYZ_test_data --knexfile knexfile.cjs
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
make sure to rename the file extensions to .cjs
execute the seed file type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
knex seed:run --knexfile knexfile.cjs
2024-10-22 10:06:50 +01:00
```
## to run the node server
2024-10-21 17:09:40 +01:00
navigate back to the project root directory and type:
2024-10-22 10:06:50 +01:00
```
2024-10-21 17:09:40 +01:00
ENVIRONMENT=development npm run dev
2024-10-22 10:06:50 +01:00
```