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

node-pg-migrations

v0.0.7

Published

<h2 align="center"> Warning! This package is still in alpha</h2> <h1 align="center"> Node PostgreSQL Migrations (NPG)</h1> <p align="center"> <b >This is a node.js service for generating and maintaining PostgreSQL migrations</b> </p> <br>

Downloads

16

Readme

npm version License: MIT

Description :

NPG is setup to help devs setup and maintain PostgreSQL migrations

It gives you:

  • Auto generated migrations based on insert/update/delete of functions/triggers/stored-procedures
  • Support for manually added migrations in both js and sql

Installation

Stable version:

npm i node-pg-migrations --save

Usage :

Simple Example

// index.js
const migrations = require('node-pg-migrations');
const pg = require('pg');

const pool = new pg.Pool();
migrations.init();
migrations.run(pool).then(function(){
        console.log('done');
    }).catch(function(err){
        console.log('err: ', err);
    }).finally(function(){
        pool.end();
    });
#bash
$ npg init
$ npg run

Run the example above with --help to see the help for the application.

Documentation :

Env

This package uses the same env variables as the node-postgres program

PGHOST='localhost'
PGUSER=process.env.USER
PGDATABASE=process.env.USER
PGPASSWORD=null
PGPORT=5432

Bash

Usage: npg <command> [options]

Commands:
  npg run                                   Run pending migrations
  npg init                                  Initial migration generation based
                                            on added/modified/deleted functions
                                            and stored procedures
  npg generate                              Generate migrations based on
                                            added/modified/deleted functions and
                                            stored procedures
  npg add <fileName>                        Add new migration
  npg add_notify_trigger <tableName>        Add notify trigger
  [triggerName]

Options:
  --version             Show version number                            [boolean]
  -p, --proceduresPath  Procedures directory path        [default: "procedures"]
  -f, --functionsPath   Functions directory path          [default: "functions"]
  -t, --triggersPath    Triggers directory path            [default: "triggers"]
  -m, --migrationsPath  Migrations directory path        [default: "migrations"]
  -c, --clear           Clear migrations table before running          [boolean]
  -h, --help            Show help                                      [boolean]

Init

The init method runs through all functions/triggers/stored-procedures and creates insert migrations for them.

  • options(optional):
    • proceduresPath: Procedures directory path relative to project root, default 'procedures'
    • functionsPath: 'Functions directory path relative to project root, default 'functions'
    • triggersPath: 'Triggers directory path relative to project root, default 'triggers'
    • migrationsPath: Migrations directory path relative to project root, default 'migrations'

example:

// index.js
const migrations = require('node-pg-migrations');
migrations.init();
#bash
$ npg init

Run

The run method executes the migrations in the migrations folder. The method takes two params:

  • pool: A PostgreSQL pool object
  • options(optional):
    • migrationsPath: Migrations directory path relative to project root, default 'migrations'
    • clear: If set to true the method will run all migration regardless of if they've been run or not

example:

const migrations = require('node-pg-migrations');
const pg = require('pg');

const pool = new pg.Pool();
migrations.run(pool, {migrationsPath: 'migrations', clear: false}).then(function(){
        console.log('done');
    }).catch(function(err){
        console.log('err: ', err);
    }).finally(function(){
        pool.end();
    });
#run new migrations
$ npg run
$ npg run './migrations'
#run all migrations
$ npg run --clear

Generate

The generate method generates new migrations based on git staged changes to functions/triggers/stored-procedures. It takes the param.

  • options(optional):
    • proceduresPath: Procedures directory path relative to project root, default 'procedures'
    • functionsPath: 'Functions directory path relative to project root, default 'functions'
    • triggersPath: 'Triggers directory path relative to project root, default 'triggers'
    • migrationsPath: Migrations directory path relative to project root, default 'migrations'

example:

const migrations = require('node-pg-migrations');
migrations.generate().then(function(){
    console.log('done');
}).catch(function(err){
    console.log('err: ', err)
});

You can set up generate to run on every git commit by using the pre-commit service

package.json example

{
    "name": "node-pg-migrations",
    "version": "0.0.0",
    "scripts": {
        "generate": "node-pg-migrations generate"
    },
    "bin": {
        "node-pg-migrations": "bin/migration.js"
    },
    "pre-commit": [
        "generate"
    ]
}
$ npg generate

Add

The add method adds a new migration file. It takes the param.

  • options:
    • filename: If the filename has an extension of .sql it creates a blank sql file. If the filename has an extension of .js it creates a js file with the expected js format.
    • migrationsPath(optional): Migrations directory path relative to project root, default 'migrations'
module.exports = function(pool){
    const sql = '';
    const values = [];
    return pool.query(sql, values);
};

example:

const migrations = require('node-pg-migrations');
migrations.add('migration.sql').then(function(){
   console.log('done');
}).catch(function(err){
   console.log('err: ', err)
});
migrations.add('newMigration.js').then(function(){
    console.log('done');
 }).catch(function(err){
    console.log('err: ', err)
 });
$ npg add 'migration.sql'

AddNotifyTrigger

The addNotifyTrigger method generates a notify function and trigger for the provided table name. It takes two params: It takes the param.

  • options:
    • tableName: The name of the table to which the trigger will be added.
    • triggerName(optional): This will default to ${tableName}_after_insert_update_delete_trigger. But if the name is over 63 chars you will have to provide an alternate name.
    • triggersPath(optional): 'Triggers directory path relative to project root, default 'triggers'

example:

const migrations = require('node-pg-migrations');
migrations.addNotifyTrigger('users').then(function(){
    console.log('done');
}).catch(function(err){
    console.log('err: ', err)
});
$ npg addNotifyTrigger users users_insert_trigger