xeplr-database-helpers
v1.0.17
Published
Database Helpers with KnexJS
Downloads
116
Readme
HOW TO SET UP THE KNEX:
Knex's two things are important. Models, and migration.
I love its migration.
Here is how to set up.
Step 1. Always build the knex using Yalc. yarn run publish-local ==> it will store the dist/js files in the yalc folder Step 2. Include this in your project. run this command: yalc add x-database-helpers@ Step 3. One the DB is connected, next is to create a migration friendly project Step 4. For that, create a folder named db [better to keep it separate] Step 5. there, make two folders, models, and migrations. Migrations is the folder where we will create migration. Step 6. There is a file in knex, called knex.migrations. This makes and runs the migration files. Step 7. Make a migration file. Run this command: node ./node_modules/x-database-helpers/dist/dbHelper/knex/knex.migrations.js --command=create --server=localhost --db=ollari --username=ollari --pwd=ollari --port=5433 --mdirectory=/Users/vikasbhandari/Dropbox/ollari/ollari-be/db/migrations/
Step 8. Make sure you are creating migrations in your folder, because that is what you will be building Step 9. you can also add this entry in package.json, so that you don't have to run this yourself again "makemigration": "rm -R ./db/migrations/jsmigrations && yarn --cwd=./db/migrations/ run build && node ./node_modules/x-database-helpers/dist/dbHelper/knex/knex.migrations.js --command=create --server=localhost --db=ollari --username=ollari --pwd=ollari --port=5433 --mdirectory=/Users/vikasbhandari/Dropbox/ollari/ollari-be/db/migrations/jsmigrations", Step 10. When you create a migration, it will create a blank migration file, with up and down function. Step 11. If you are creating lot of tables in one go, you can use the columnInfo array which will create the tables using arrays. Example is added when the migration file is added
Step 12. If you don't want to create the columns manually, you can also use migration.helper file to create Json for columnInfos. Example: const columnInfoInString = [ 'tag-true-tagName-string-100-notnullable', 'tag-true-tagDescription-string-1000', 'badge-true-badgeName-string-100-notnullable', 'badge-true-badgeDescription-string-1000', 'productCategory-true-productCategoryName-string-100-notnullable', 'productCategory-true-productCategoryDescription-string-1000',]
Step 13. Once you have edited the migration, you will need to run the migration. As soon as you run the migration, it will update the DB