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

postgres-mig

v1.0.0

Published

Simple PostgreSQL migrations for nodejs without an ORM

Downloads

9

Readme

postgres-mig

Build Status TypeScript

Simple PostgreSQL migrations for nodejs without an ORM.

npm i postgres-mig

Example

// NOTE: `client` is a `pg` client
module.exports = {
  /**
   * Run anything you need
   * to migrate the database `up`
   *
   * NOTE: this is wrapped in a transaction,
   * so the writes will either all succeed or all fail.
   */
  async up(client) {
    await client.query(`CREATE TYPE Greeting AS ENUM ('hi', 'hello')`);
    await client.query(`
      CREATE TABLE HelloWorld (
        kind Greeting,
        name TEXT
      )
    `);
  },
  /**
   * Run anything you need
   * to migrate the database `down` (i.e. undo the `up`)
   */
  async down(client) {
    await client.query("DROP TABLE HelloWorld");
    await client.query("DROP TYPE Greeting"); // NOTE: you'll want to drop in reverse order of your creates
  }
};

Migration scripts like this, live in your git repository in a folder called migration.

migration/2018-12-25-000000-add-christmas-table.js

It's important that the file names sort such that the older ones run before the newer ones. Hence why the convention of using ISO date timestamps to prefix the names. It also helps avoid name collisions with people you are working with.

pgmig keeps track of which migrations have been applied in a table called migration

CLI

This npm package ships with an executable script. pgmig

usage: pgmig [command]

sync    migrate up until up-to-date
up      migrate 1 up
down    migrate 1 down
st      status
new     make a new migration

If you want to configure the directory or connection wrap this in your own script.

#!/usr/bin/env node

// path to your migrations
var dir = __dirname + "/migration";

// connect to your db
var Client = require("pg").Client;
var client = new Client({..});

require("postgres-mig").runCli(dir, client);

API

You can programmatically run migrations. Useful for testing.

import { PgMig } from "postgres-mig";
// or
var PgMig = require("postgres-mig").PgMig;

Read the code, it's less than 100 lines. src/PgMig.ts

For example, an ava test script.

test.before("reset db", async function(t) {
  var ROOTCLIENT = new pg.Client({
    database: "invpaint_test" // hardcode to be sure we never touch prod
    // ...
  });
  await ROOTCLIENT.connect();
  await ROOTCLIENT.query("DROP SCHEMA public CASCADE");
  await ROOTCLIENT.query("CREATE SCHEMA public AUTHORIZATION invpaint_test");
  await ROOTCLIENT.query("CREATE EXTENSION pgcrypto");
  await ROOTCLIENT.end();

  var client = await env.pool.connect();

  var pgmig = new PgMig({
    client: client,
    dir: path.resolve(__dirname, "../migration")
  });
  await pgmig.setup();

  // Test `down` migrations
  await pgmig.up((await pgmig.status()).notApplied);
  await pgmig.down((await pgmig.status()).applied.reverse());

  // Get the database set up so we can continue testing
  await pgmig.up((await pgmig.status()).notApplied);

  await client.release();

  t.true(true, "got setup");
});

License

MIT