Update README.md

This commit is contained in:
jc user 2024-10-22 10:06:50 +01:00
parent 58f45f8947
commit 77ee8b1d66

View File

@ -1,25 +1,36 @@
# use knex.js as ES6 and work with .env # use knex.js with ES6 on Node and work with .env
# The below instructions are intended for command line execution while working with node server ## The below instructions are intended for command line execution while working with node server
install dotenv and knex.js install dotenv and knex.js
```
npm install dotenv --save
npm install knex --save npm install knex --save
```
In the project root directory, create a folder called 'db' and navigate to this 'db' folder: In the project root directory, create a folder called 'db' and navigate to this 'db' folder:
execute the following command: execute the following command:
```
npx knex init npx knex init
```
A 'knexfile.js' will be created in this directory. A 'knexfile.js' will be created in this directory.
Change the file extension to .cjs Change the file extension to .cjs
you will need to add the below line in your package.json file: you will need to add the below line in your package.json file:
```
"type": "module", "type": "module",
```
create 2 subfolders inside 'db' folder, called "migrations" and "seeds" create 2 subfolders inside 'db' folder, called "migrations" and "seeds"
# data in .env file # data in .env file
@ -34,52 +45,77 @@ DEV_PORT=5432
# knexfile.cjs configuration # knexfile.cjs configuration
// check current working directory > check current working directory
const dir = process.cwd(); const dir = process.cwd();
// check current running environment
> check current running environment
const env = process.env.ENVIRONMENT || ""; const env = process.env.ENVIRONMENT || "";
// current settings do not work for production mode > the database details here
// TODO production mode checking
const config =
env === "development"
? require("dotenv").config({ path: dir + "/.env" })
: require("dotenv").config({ path: "../.env" });
// the database details here // ```
exports.development = {} exports.development = {}
exports.production = {} exports.production = {}
```
# below instructions are used on a terminal window navigate to 'db' folder # below instructions are used on a terminal window navigate to 'db' folder
create a migration file type: create a migration file type:
```
knex migrate:make create_XYZ_table --migrations-directory migrations knex migrate:make create_XYZ_table --migrations-directory migrations
```
make sure to rename the file extensions to .cjs make sure to rename the file extensions to .cjs
execute migration type: execute migration type:
```
knex migrate:latest --knexfile knexfile.cjs --migrations-directory migrations knex migrate:latest --knexfile knexfile.cjs --migrations-directory migrations
```
execute rollback type: execute rollback type:
```
knex migrate:rollback --knexfile knexfile.cjs --migrations-directory migrations knex migrate:rollback --knexfile knexfile.cjs --migrations-directory migrations
```
### Check out the knex documentation
knex URL: https://knexjs.org/guide/migrations.html#migration-cli knex URL: https://knexjs.org/guide/migrations.html#migration-cli
create a seed file type: create a seed file type:
```
knex seed:make 01_XYZ_test_data --knexfile knexfile.cjs knex seed:make 01_XYZ_test_data --knexfile knexfile.cjs
```
make sure to rename the file extensions to .cjs make sure to rename the file extensions to .cjs
execute the seed file type: execute the seed file type:
```
knex seed:run --knexfile knexfile.cjs knex seed:run --knexfile knexfile.cjs
# to run the node server ```
## to run the node server
navigate back to the project root directory and type: navigate back to the project root directory and type:
```
ENVIRONMENT=development npm run dev ENVIRONMENT=development npm run dev
```