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

@benningfield-group/trek

v0.2.1

Published

Database migration tool for MSSQL.

Downloads

35

Readme

trek

A database migration tool for MSSQL.

Install

Node 6.10.0 or higher is required to use this tool. Install trek globally by issuing the following command.

npm install -g @benningfield-group/trek

This will add a global trek command. Running trek without any arguments will describe the program's usage.

Database Connection Configuration

Database configuration is stored in a connections.json file, which should contain a single object. (The connections.json file name and location can be overridden using the --connections-file option.) Each key in the object should correspond to an environment (dev, uat, prod, etc.). The value associated with each key should be an array of connection configuration objects suitable for use with node-mssql as described, here.

For example:

{
  "dev": [
    {
      "user":"sa",
      "password":"password",
      "server":"localhost",
      "database":"db1"
    },
    {
      "user":"sa",
      "password":"password",
      "server":"localhost",
      "database":"db2"
    }
  ],
  "prod": [
    {
      "user":"sa",
      "password":"password",
      "server":"prod.example.com",
      "database":"db1"
    },
    {
      "user":"sa",
      "password":"password",
      "server":"prod.example.com",
      "database":"db2"
    }
  ]
}

The above example contains two databases in the dev environment, and two prod databases.

Environment

trek uses the NODE_ENV environmental variable for determining what connection(s) to run migrations against. By default, migrations are run against the dev environment. Other environments must be specifically set in a NODE_ENV environmental variable like so: export NODE_ENV=prod under *nix; set NODE_ENV=prod under Windows. Or, alternatively, the environment can prefix the trek command: NODE_ENV=prod trek <command>.

Creating a Migration

To create a migration, run trek create <name>, where <name> is a descriptive name for the migration. Migrations are by default created in a migrations folder, but the folder can be overridden using the --migrations-dir option.

Running: trek create create_table_widgets produces the output:

Creating migration: /some/path/migrations/2017-06-07__09-23-35-450__create_table_widgets.js

Note that the migration is prefixed with a timestamp, which is important to ensure that migration order is preserved.

The newly-created file contains a migration scaffolding with two methods: up and down. The former is used to run a migration, and the latter is used to undo it. As an example, the following could be used to create a widgets table and subsequently drop it should the need arise:

'use strict';

module.exports = {
  /**
   * Run the migration.
   * @param {Object} mssql - An mssql instance from the node-mssql package
   * (https://github.com/patriksimek/node-mssql).
   * @param {ConnectionPool} conn - A connected ConnectionPool instance
   * (https://github.com/patriksimek/node-mssql#connections-1).
   */
  up(mssql, conn) {
    const sql = `
      CREATE TABLE widgets (
        widgetID INT NOT NULL PRIMARY KEY IDENTITY,
        widgetName NVARCHAR(255) NOT NULL)`;
    const req = new mssql.Request(conn);

    console.log(sql);

    return req.query(sql);
  },

  /**
   * Bring down a migration.
   * @param {Object} mssql - See up().
   * @param {ConnectionPool} conn - See up(). 
   */
  down(mssql, conn) {
    const sql = `DROP TABLE widgets`;
    const req = new mssql.Request(conn);

    console.log(sql);

    return req.query(sql);
  }
};

Running Migrations

trek up will run all migrations that have not yet been run. The migrations are run in order, one after another (sequentially), against each database defined for the environment. Should a failure occur, the operation is aborted and the error is logged to the console (to STDERR). After each successful migration, the migration is logged in the trek_migrations (this table will be created automatically if it does not exist).

Rolling Back

trek down will bring down the most recent migration. Like the up command, if an exception is raised the operation aborts and the error is printed. After a successful down operation, the most recent migration entry is deleted from the trek_migrations table.

Run a script

trek run <script-file will run a JavaScript manually. The script should export an up function that accepts mssql and conn as arguments, the same method signature as up and down.