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

postgresql-migrate

v0.1.0

Published

Simple CLI migration tool for PostgreSQL and Node.js with migration files written in raw SQL. TypeScript types files for all tables and columns in the database are automatically created.

Downloads

14

Readme

Postgresql Migrate

Simple CLI migration tool for PostgreSQL and Node.js with migration files written in raw SQL. TypeScript types files for all tables and columns in the database are automatically created.

How it works

Create a JSON config file, e.g. config.json with the connection to the database. You can either store connection information directly as a string but it is recommended to use environment variables from your .env file. To do so, simple add env:VARIABLE_NAME as the value and the package will automatiaclly replace it with the value of the environment variable.

Note: the db user used for the migration must have the required privileges to CREATE TABLES AND DROP TABLES.

Install package

npm install postgresql-migrate

Setup migration directory

Run the following command with the path to the directory that will contain the directory with the SQL migration files as well as the config.json.

postgresql-migrate setup -d "path-to-migration-dir"

Configure your settings

Open the newly created config.json and specify your settings. For the migration to work, the connection (database) part is required. All other settings are optional.

{
    "connection": {
        "host": "env:DB_HOST",
        "port": "env:DB_PORT",
        "user": "env:DB_USER",
        "password": "env:DB_PASSWORD",
        "database": "env:DB_NAME"
    }
}

Now you can run various CLI commands, such as creating migration files or applying up or down migration steps. You just need to reference the config.json.

Run command to create migration file

postgresql-migrate -d "path-to-migration-dir" create "name-of-migration-file"

Provide the migration step in raw SQL

For a successful migration, you should provide both up and down scripts and these must be placed after the /* UP */ comment for the [UP] migration script and after /* DOWN */ for the [DOWN] migration script.

/* UP */
CREATE TABLE "Users" (
    id SERIAL PRIMARY KEY,
    username VARCHAR NOT NULL,
    email VARCHAR NOT NULL
);

/* DOWN */
DROP TABLE IF EXISTS "Users" CASCADE;

Run migrations with the following CLI command

postgresql-migrate -d "path-to-migration-dir" up

Add migration command to package.json

To simplify running commands, it is recommended to add the following command to your package.json.

{
    "scripts": {
        "migrate": "postgresql-migrate -d 'path-to-migration-dir'"
    }
}

Run commands

Now you can use any command simply with

npm run migrate [up|down|create|redo|reset|setup|status]