1.0.2 • Published 1 year ago

@pesalink/pesalink-database-package v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

Pesalink Database Package

Steps to integrate and utilize the DB migration package

make directory for keeping migration & seed files

Create migrations & seeds folder in src

Install migration package

Install the migration package
npm i @pesalink/pesalink-database-package 

Install dependencies

npm install knex mysql2 dotenv

Create a file with name knexfile.js in root directory

Pass connection details like host, user, password, database name, and client type
Provide a table name to store migrations file records
Give directory path for migrations and seeds

To create migrations file

npx knex migrate:make your_migration_file modify the migration file as per you requirement

Follow below steps to run migration files

Create a file in root directory with name runMigrations.js
Import the @pesalink/pesalink-database-package 
Import the knexfile.js of the project to pass env variable to package


import { runMigrations } from '@pesalink/pesalink-database-package';  // Import migration function
import knexConfig from './knexfile.js';  // Correct the import for knexfile.js

// Run migration
runMigrations(knexConfig.development)
  .then(() => {
    console.log('Service Migrations ran successfully!');
  })
  .catch((error) => {
    console.error('Error running migrations:', error);
  });

To run specific migration file

npx knex migrate:up your_migration_file.js

To run all migration files

node runMigrations.js

To rollback a specific migration file

npx knex migrate:down your_migration_file.js

To check migrations file status in service

npx knex migrate:status --knexfile knexfile.js (path of knexfile.js)

To rollback all migrations files

npx knex migrate:rollback or npx knex migrate:rollback --all

To create seed file

npx knex seed:make you_seed_file modify the seed file as per you requirement

Follow below steps to run seed files

Create a file in root directory with name runSeeds.js
Import the @pesalink/pesalink-database-package
Import the knexfile.js of the project to pass env variable to package


import { runSeeds } from '@pesalink/pesalink-database-package';  // Import runSeeds from shared package
import knexConfig from './knexfile.js';  // Import knex configuration

const runUserManagementSeeds = async () => {
  try {
    // Pass the knexConfig to runSeeds function
    await runSeeds(knexConfig.development);  // Use the correct config (like development)
    console.log('Service Seeds ran successfully!');
  } catch (error) {
    console.error('Error running seeds:', error);
  }
};

runUserManagementSeeds();  // Run the seeds

To run specific seed

npx knex seed:run --specific=you_seed_file.js

To run all seeds

node run runSeeds.js

To insert fake record use following steps

install facker package by npm install @faker-js/faker
Import this package into seed file

import { faker } from '@faker-js/faker';
/**
 * @param { import("knex").Knex } knex
 * @returns { Promise<void> } 
 */

export async function seed(knex) {
  await knex('company').del(); // Clear existing data

  const companies = Array.from({ length: 5 }).map(() => ({
    company_name: faker.company.name(),  // Generating fake company names
  }));

  await knex('company').insert(companies);
  console.log('Fake companies have been added!');

}

Structure of project

/ServiceDirectory
│── node_modules/
│── src/
│   │── migrations/        # Contains all migration files
│   │── seeds/             # Contains all seed files
│   │── services/          # Other services in the project
│── package.json
│── knexfile.js            # Knex configuration file
│── runSeeds.js            # Script to run specific seed files
│── runMigrations.js       # Script to run migrations
1.0.2

1 year ago

1.0.1

1 year ago