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 🙏

© 2026 – Pkg Stats / Ryan Hefner

cassandra-shift

v0.2.8

Published

Cassandra migration utility as Node.js module

Readme

cassandra-shift

npm version CI License

cassandra-shift is a Node.js library for managing forward-only database migrations in Apache Cassandra.

It provides a structured way to define, apply, validate, and inspect migrations, following Cassandra best practices and avoiding rollback-based workflows.

Notice: starting with version 0.2, this module uses ES6. JS migrations are also expected to be ES6 modules.

Overview

Schema evolution in Cassandra is inherently forward-only and does not lend itself well to traditional rollback mechanisms. cassandra-shift embraces this approach by offering:

  • Deterministic, ordered migrations
  • Support for both CQL and JavaScript migrations
  • Migration state tracking in a dedicated table
  • Validation and inspection utilities
  • Support for multiple Cassandra clients

Features

  • Forward-only migrations
  • CQL and JavaScript-based migrations
  • Multiple Cassandra clients support
  • Automatic migration state tracking
  • Migration validation and inspection
  • Minimal and explicit API

Installation

Install the package using npm:

npm install cassandra-shift

or yarn:

yarn add cassandra-shift

Project Structure

A typical project using cassandra-shift:

.
├── migrations/
│   ├── 001_create_users_table.cql
│   ├── 002_add_age_column.js
│   └── 003_update_posts_table.cql
└── migrate.js

Migration files are executed in lexicographical order.

Writing Migrations

CQL Migrations

CQL migrations are plain .cql files containing valid Cassandra Query Language statements.

Example:

migrations/001_create_users_table.cql

CREATE TABLE IF NOT EXISTS users (
  id UUID PRIMARY KEY,
  name text,
  email text,
  created_at timestamp
);

JavaScript Migrations

JavaScript migrations must export a function.

Example:

migrations/002_add_age_column.js

export default async function (clients) {
  const writeClient = clients[0];

  await writeClient.execute(`
    ALTER TABLE users ADD age int;
  `);
};

The clients parameter is the same array of Cassandra clients passed to the shift instance.

Usage

Creating the instance

The library exports a class.

The constructor accepts:

  • An array of Cassandra clients
  • An options object
import { Client } from "cassandra-driver";
import CassandraShift from "cassandra-shift";

const readClient = new Client({
  contactPoints: ["127.0.0.1"],
  localDataCenter: "datacenter1",
});

const writeClient = new Client({
  contactPoints: ["127.0.0.1"],
  localDataCenter: "datacenter1",
});

await readClient.connect();
await writeClient.connect();

const shift = new CassandraShift(
  [readClient, writeClient],
  {
    keyspace: "my_keyspace",
    migrationTable: "schema_migrations",
    ensureKeyspace: true,
    useKeyspace: true,
    dir: "./migrations",
  }
);

Options

The options object supports the following properties:

| Name | Type | Description | |------|------|-------------| | keyspace | string | Keyspace used for migrations | | migrationTable string Table used to store migration state | ensureKeyspace | boolean | Create the keyspace if it does not exist | | useKeyspace | boolean | Execute USE <keyspace> if the client is not bound to a default keyspace | | dir | string | Directory containing migration files |

Methods

migrate()

Applies all pending migrations in order.

await shift.migrate();

Only migrations that have not yet been applied will be executed.

clean()

Drops the keyspace and recreates it.

await shift.clean();

This is a destructive operation and should only be used in development or testing environments.

info()

Returns information about the current migration state.

const info = await shift.info();

This typically includes applied migrations and pending ones.

validate(rethrowError = false)

Validates that the defined migrations match the migrations already applied in the database.

const isValid = await shift.validate();

If rethrowError is set to true, validation errors will be thrown instead of returning false:

await shift.validate(true);

Migration Strategy

cassandra-shift does not support rollback operations.

Once a migration has been applied, it is considered permanent. This design encourages:

  • Additive and backward-compatible schema changes
  • Immutable migration files
  • Thorough testing before production deployment

Migration Integrity

cassandra-shift stores a hash of each applied migration in the migration table.

When migrations are executed, their content is hashed and persisted together with the migration identifier.
On subsequent runs, the library compares the stored hash with the hash of the migration file currently on disk.

If a migration file has been modified after being applied, the hash mismatch is detected and the migration state is considered inconsistent.

This mechanism ensures:

  • Applied migrations are immutable
  • Accidental or manual changes to migration files are detected
  • Safer schema evolution in distributed environments

Hash validation is performed automatically during migration execution and can also be explicitly checked using the validate() method.

Best Practices

  • Never modify migrations that have already been applied
  • Keep migrations small and focused
  • Prefer additive schema changes
  • Use validate() in CI pipelines or when apps relying on migrations are started
  • Use clean() only in non-production environments

Contributing

Contributions are welcome.

  • Fork the repository
  • Create a new branch
  • Commit your changes
  • Ensure coding style is respected
  • Open a pull request

License

MIT