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

db-migr

v0.1.2

Published

Javascript db migrations utility for SQLite, MySQL or other SQL Ansi databases

Downloads

8

Readme

db-migr

Installation

Since the library is a JS-based solution, to install the latest version of db-migr you only need to run:

$ npm install --save db-migr

or

$ yarn add db-migr

How it works

This tool implements a lightweight migrations API that works with a array of migrations as the follow example:

const migrations = [
  {
    name: "001-create-table",
    migration: `
-- Up
CREATE TABLE IF NOT EXISTS "people" (
  id   INTEGER PRIMARY KEY,
  name TEXT    NOT NULL
);

-- Down
DROP TABLE people;`,
  },
  {
    name: "002-create-index-name",
    migrationUp: `CREATE INDEX people_name ON people (name)`,
    migrationDown: `DROP INDEX people_name`,
  },
  {
    name: "003-create-field",
    migration: [
      "ALTER TABLE people ADD address VARCHAR(255)",
      "ALTER TABLE people ADD email VARCHAR(60)"
    ],
  },
  {
    name: "004-create-index-email",
    migration: `CREATE INDEX people_email ON people ('email')`,
  },
];

The name attribute it's required in each migration, so you can use this attribute as execution order and history. Every name needs to follow this format {n}-{description}. Migrations with names that didn't start with number will be ignored.

Each migration in your array could be based in a complex string, or using two attributes migrationUp and migrationDown. You could use in up operations some array of queries also.

It's very important to keep a history of migrations in your migrations array, if you remove some migration entry in futures updates/releases if you implements some migrationDown query this tool will automatically reverts your migrationUp.

Implement your query runner

To make this tool more versatile we didn't implement a default query runner method, so you can use this tool with your favorite database adapter just implementing some promisable/async query function.

Simple generic example:

import migr from "db-migr";

const myMigrations = [
  // a incremental list of migrations for your db...
];

const dbConn = ...implements your database connection

// run your migrations when you are sure that your db connection are already opened
migr({
  // implemention your query runner [required]
  // this method needs to return some promisable function
  query: async (query, params = []) => {
    return await dbConn.executeSql(query, params);
  },
  // pass your migrations
  migrations: myMigrations
}).then(() => {
  // if you got here, everything ran ok
}).catch(err => {
  // if you got here, something wrong happened
  // and you could look to `err` object
});

License

MIT