Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-mini-migrations

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-mini-migrations

A very small, lightweight and flexible migrations library unconcerned with what database you use

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31
increased by6.9%
Maintainers
1
Weekly downloads
 
Created
Source

Mini Migrations for NodeJS

Build Status David DM GitHub code size in bytes GitHub package.json version GitHub

A really simple node migrations library that is completely independant of any database or file system.

Example Usage

You need to define a driver for example:

Installation

npm install node-mini-migrations

Setup

# migrations/driver.js
const fs = require('fs')

// `passedFunctions` are passed to each migration file and can container
// a database, like mysql, postgres, mongodb, or anything you want. 
// It just returns an object that gets passed to migrations.
const passedFunctions = {
  tableCreate: (table) => {
    console.log('would create a table', table)
  },

  tableDrop: (table) => {
    console.log('would drop a table', table)
  },

  insert: (row) => {
    console.log('would insert a row', row)
  },

  remove: (row) => {
    console.log('would remove a row', row)
  }
}

module.exports = {
  init: () => {
    if (!fs.existsSync('test_state.json')) {
      fs.writeFileSync('test_state.json', JSON.stringify({}))
    }
  },

  finish: () => {
    console.log('finished migrations')
  },

  getMigrationState: (id) => {
    const state = JSON.parse(
      fs.readFileSync('test_state.json', 'utf8')
    )
    return state[id]
  },

  setMigrationUp: (id) => {
    const state = JSON.parse(
      fs.readFileSync('test_state.json', 'utf8')
    )
    state[id] = true
    fs.writeFileSync('test_state.json', JSON.stringify(state))
  },

  setMigrationDown: (id) => {
    const state = JSON.parse(
      fs.readFileSync('test_state.json', 'utf8')
    )
    delete state[id]
    fs.writeFileSync('test_state.json', JSON.stringify(state))
  },

  getPassedFunctions: () => passedFunctions
}

Migration files

You can then create typical migrations files like:

# migrations/1-my-migration-example.js
module.exports = {
  up: db => {
    return db.tableCreate('test_table')
  },

  down: db => {
    return db.tableDrop('test_table')
  }
}

Usage

You run migrator up to bring up any migrations or migrator down to bring them down.

Or inside node app
const {up, prepareRun} = require('node-mini-migrations')
up(prepareRun('./migrations'))

License

This project is licensed under the terms of the GPLv3 license.

FAQs

Package last updated on 07 Nov 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc