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

@database-revisions/cli

v0.2.1

Published

A lightweight database migration tool for SQL and no-SQL databases

Downloads

9

Readme

@database-revisions/cli

Build Status

Written in JavaScript, database-revisions is a lightweight database migration tool for usage with SQL and no-SQL databases such as MongoDB and PostgreSQL.

Goals

  • Revision immutability for reproducible database migrations
  • Support for SQL and no-SQL databases using plugins
  • Compatibility with Continuous Delivery / Continuous Integrations

Candidates for database implementations require support for:

  • Database Transactions to guarantee the state of the database is predictable even when a revision upgrade or downgrade fails
  • Database Resource Locks to control concurrent access to the "migrations table" to prevent race conditions and unexpected behaviour

| Command | Description | | ------------------- | ------------------------------------------------ | | init | interactive prompt for project and config setup | | new [description] | create a new revision | | version | show the current persisted version | | list | list revision files | | up | upgrade database using all pending revisions | | down | downgrade database using current revision | | help | show help menu |

Installation

# Create new project folder
mkdir my-project

# Setup a node project
npm init

# Install packages
npm install @database-revisions/cli
npm install @database-revisions/postgres

# Configure CLI tool
npx db init

# Print out help
npx db help

How It Works

Each revision has:

  • previous version - describes its dependency to another revision
  • up function - provides a database client session that has been setup to be transactional, and it used to make changes to the database in an "up" direction
  • down function - similar to the up function in that it is provided a session but should be written for the "down" direction to revert changes that the "up" function performs

A version of a revision is computed as a Merkle Tree. That is the revision without a previous version (the first revision with base dependency) computes its version by hashing the contents of the revision file.

firstRevision = {
  previousVersion = undefined,
  version = hash('001_my-first.revision.js', 'sha1')
}

Whereas the second revision depends on the first revision, so its previous version will be that of the first revision.

secondRevision = {
  previousVersion = firstRevision.version,
  version = hash(
    hash('002_my-second.revision.js', 'sha1') + firstRevision.version,
    'sha1'
  )
}

Using this technique a retroactive change to file content of the

  • first revision means that the version of the second revision changes too
  • second revision means that the version of the second revision changes too

With these properties, it is easy to see how one can detect change, and support immutability. Given that a retroactive change is detected the migration tool can abort applying pending revisions until rectified. This way consistency can be established across one or more environments because database changes are reproducible. In conjunction with a CI/CD pipeline, this tool can ensure changes are performed consistently and avoid redundant actions performed against a database.

How-to Use

To get a better understanding of database migrations, please first review concepts DDL, DML and DCL.

  1. In source control, create a folder to store *.revision.js files
  2. Configure the tool environment variables, see db help
  3. Create a new revision, eg. db new 'my-first-revision', which automatically creates a template
  4. Edit the new revision file, adding a database query for up() and down()
  5. Apply the revision against a database db up
  6. Verify changes using db version and db list