npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

agnostic-sql-migrator

v1.0.4

Published

Database agnostic migrator tool for SQL databases

Downloads

9

Readme

Agnostic SQL Migrator

A useful tool to carry out database migrations on Postgres, MySQL, or SQL Server databases. The tool was written in TypeScript and comes bundled with it's own types.


Configuration

This tool requires the SQL DB configuration information to be provided. This can be provided either via the environment, programmatically, or as command line arguments (if running locally).

Argument precedence is as follows:

A programmatic configuration variable takes highest precedence.

This is then followed by a command line configuration variable.

Then finally an environment variable.

Possible command line arguments/environment variables are:

  • ADAPTER - the type of SQL database you are targeting accepting values postgres, sqlserver, or mysql (defined by the type Adapters)
  • USER - the user on the database to login as
  • PASSWORD - user password
  • HOST - host server location
  • PORT - host server port for connection
  • DATABASE - database name
  • MIGRATIONS_PATH - the directory that the migrations are located at

There is also the optional VERSION argument which can be provided to specify which DB version to migrate to. If this remains unspecified then the migrator will migrate to the highest possible version of the database it can find.

A restriction that is placed on the migration files is that they must be named as follows: [version from]-[version to].sql with the lowest possible version being 1. The versions can skip e.g. 1-4.sql. This also applies to rollback versions e.g. 4-1.sql.

Installation

Install the tool via npm using:

npm install agnostic-sql-migrator

or if you're using yarn:

yarn add agnostic-sql-migrator

You will also need to install the relevant DB package:

MySQL: npm install mysql2 or yarn add mysql2

Postgres: npm install pg or yarn add pg

SQL Server: npm install mssql or yarn add mssql

Usage

Import and use the entrypoint using the following:

If your configuration variables are already defined in your environment

import {migrator} from 'agnostic-sql-migrator';

const migrateDb = async () => {
    await migrator();
};

Otherwise you can call:

import {migrator, ClientConfig, MigrationConfig} fromm 'agnostic-sql-migrator';
import {resolve} from 'path';

const migrationsPath = resolve(PATH_TO_MIGRATIONS);

const clientConfig: ClientConfig = {
  user: "postgres",
  password: "password",
  host: "localhost",
  port: 5432,
  database: "testdb"
};

const migrationConfig: MigrationConfig = {
  adapter: "postgres",
  migrationsPath,
  version: 10
};

const migrate = async () => {
    await migrator({ClientConfig: clientConfig, MigrationConfig: migrationConfig});
};

To call from command line you can use the following command:

node_modules/.bin/agnostic-sql-migrator --VERSION=YOUR_VERSION for example.

You can specify as many of the arguments described above as you like, but all the required arguments have to be specified either here in the command line, or as part of your environment variables, otherwise the command will fail.