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

rethinkdb-migration

v1.1.0

Published

RethinkDB migration tool allows you to handle database migration for your Node application.

Downloads

22

Readme

rethinkdb-migration

license Build Status

RethinkDB migration tool allows you to handle database migration for your Node application. Additionally you can define individual migration for stage, uat, etc. Rethinkdb-migration will take care of running proper migration script from core directory and indicated implementation.

IMPORTANT: You need Node >= 8.9.4 for this package.

Installation

Installation is done using the npm install command:

npm install rethinkdb-migration

Configuration

You may configure rethinkdb-migration in two ways: by environment variables or provide configuration file.

Environment variables.

Database configuration
TRM_DB_HOST,
TRM_DB_PORT,
TRM_DB_USER,
TRM_DB_PASS,
TRM_DB_NAME,
Tool configuration
TRM_DB_TABLE, specify table where rethinkdb-migration keeps migration history
TRM_DIR_MIGRATE, specify directory where rethinkdb-migration keeps migration scripts
TRM_DIR_IMPLEMENTATION, specify directory with additional implementation e.g STAGE, UAT

Configuration file

If you do not want to add environment variables to your system you may provide configuration file instead.

{
  "DB_HOST": "192.168.10.30",
  "DB_PORT": "28015",
  "DB_USER": "",
  "DB_PASS": "",
  "DB_NAME": "rethinkMigrateTest",
  "DB_TABLE": "toolRethingDBMigrate",
  "DIR_MIGRATE": "./migrate",
  "DIR_IMPLEMENTATION": "stage"
}

Example file with default data you can find at config/config-default.json

Usage

Let's create migration script with query which creates new table in database. Thus, we use config file to provide configuration for rethinkdb-migration.

Define following data in config-example.json
{
  "DB_HOST": "localhost",
  "DB_PORT": "2815",
  "DB_USER": "",
  "DB_PASS": "",
  "DB_NAME": "rethinkMigrateTest",
  "DB_TABLE": "toolRethingDBMigrate",
  "DIR_MIGRATE": "./migrate",
  "DIR_IMPLEMENTATION": ""
}
Generate migration script from template. Use following command:
node rethinkdb-migration -cs example-migration --config="./../config/config-example.json"

Above command creates file with name based on pattern yyyyMMdd_HHmm-example-migration.js in migrate directory.

Generated file should have following content:

async function up(rethink, migrate) {
    await migrate.finishMigrationScript();
}

module.exports = {
    up
};
Add rethinkdb query in order to create new table.
async function up(rethink, migrate) {
    await rethink.getReQL().tableCreate('exampleTable').run();
    await migrate.finishMigrationScript();
}

module.exports = {
    up
};

or you may use method createMissingTables()

async function up(rethink, migrate) {
    rethink.createMissingTables('exampleTable');
    await migrate.finishMigrationScript();
}

module.exports = {
    up
};

Running

Created migration can be launched by the command

node rethinkdb-migration --config="./../config/config-default.json"

When you run above command table exampleTable will be create in database which you defined in configuration file.

In table toolRethingDBMigrate you can see migration history.

{

    "date": Wed Jan 31 2018 11:47:31 GMT+00:00 ,
    "fileName": "20180131_1241-example-migration.js" ,
    "id": "89a98790-a51b-4eaa-a55e-d4906cde7341" ,
    "version": "20180131_1241"

}

Pre and Post scripts

You may setup some scripts that will be run before and after migrations. This is seldom helpful on AT environments when you need to clear database, insert test data, migrate, and dump data again.

Just put scripts that needs to be run before migrations in migrate/pre sorted alphabetically f.ex:

./migrate/pre/001_truncate_tables.js
./migrate/pre/002_insert_test_data.js

and scripts that needs to be run after migrations in folder migrate/post:

./migrate/post/001_dump_tables.js

IMPORTANT: scripts must expose function

/**
 * @param {Rethink} rethinkRef
 * @param {Migrate} migrate;
 */
async function up(rethinkRef, migrate)

Helpers

Method | Parameters type | Description ---|---|---| getReQl() | | Returns rethinkdbdash object. closeConnection() | | Closes connection to database. createDatabase(databaseName) | {String} databaseName | Creates database based on given name. createMissingTables(tablesName) | {Array} tablesName | Creates tables based on given table names. insertWhenTableIsEmpty(tableName, data) | {String} tableName, {String} data | Insert data to table.

License

MIT License