@keendev/db-migration
TypeScript icon, indicating that this package has built-in type declarations

1.0.5 • Public • Published

KeenDev / Database migration

Keep your database structure synchronized across different environments using native language on your database of choice.

NPM version NPM downloads

Installation

# NPM
npm i @keendev/db-migration --save

# Yarn
yarn install @keendev/db-migration

Usage

Javascript

const { resolve as resolvePath, join as joinPath } = require('fs');
const dbMigration = require('@keendev/db-migration');

// Postgres

// 1) Prepare a repository
const repository = dbMigration.repository.fileSystem.create({
    migrationsDirectory: resolvePath(joinPath(__dirname, 'migrations')),
    fileExtension: '.sql',
});

// 2) Create the template engine of your choice
const templateEngine = dbMigration.template.create();

// 3) Prepare migration configuration
const migrationConfig = {
    schemaName: 'migration',
    tableName: 'history',
    keepTrackOfMigration: true,
};

// 4) Prepare the database connection
const connectionPool = {
    connectionString: process.env.DATABASE_CONNECTION_URI,
};

// 5) Instantiate the database driver of your choice
const migrationEngine = dbMigration.migration.postgres.create(
    repository,
    templateEngine,
    migrationConfig,
    connectionPoolConfig,
);

// 6) Synchronize your database
migrationEngine
    .apply()
    .then(() => console.info('Database is migrated to the latest version'))
    .catch(err => console.error('Failed to migrate the database', err));

TypeScript

import { resolve as resolvePath, join as joinPath } from 'fs';
import * as dbMigration from '@keendev/db-migration';

// Postgres

// 1) Prepare a repository
const repository = dbMigration.repository.fileSystem.create({
    migrationsDirectory: resolvePath(joinPath(__dirname, 'migrations')),
    fileExtension: '.sql',
});

// 2) Create the template engine of your choice
const templateEngine = dbMigration.template.create();

// 3) Prepare migration configuration
const migrationConfig: MigrationConfig = {
    schemaName: 'migration',
    tableName: 'history',
    keepTrackOfMigration: true,
};

// 4) Prepare the database connection
const connectionPool: Pool = {
    connectionString: process.env.DATABASE_CONNECTION_URI,
};

// 5) Instantiate the database driver of your choice
const migrationEngine = dbMigration.migration.postgres.create(
    repository,
    templateEngine,
    migrationConfig,
    connectionPoolConfig,
);

// 6) Synchronize your database
migrationEngine
    .apply()
    .then(() => console.info('Database is migrated to the latest version'))
    .catch(err => console.error('Failed to migrate the database', err));

And you're good to go!

License

MIT

Package Sidebar

Install

npm i @keendev/db-migration

Weekly Downloads

8

Version

1.0.5

License

none

Unpacked Size

28.6 kB

Total Files

33

Last publish

Collaborators

  • dmanavi