Giter Site home page Giter Site logo

minigrate's Introduction

Minigrate

Abstract migration library.

Build Status

JavaScript Style Guide

How do I use it?

There is no CLI. Instead, use the Minigrate API and create migrations by hand. Could there be a CLI someday? Sure! I just haven't had the need to make one yet.

1. Define some migrations

Make a file for your migration. The name should follow the format ${timestamp}-${name}.

./migrations/1516226643067-create-foo-table.js

'use strict'

const { DIRECTION_UP, DIRECTION_DOWN } = require('minigrate')

module.exports = {
  name: '1516226643067-create-foo-table',
  async [DIRECTION_UP] (foo, bar) {
    // Create the foo table. I'm not your database, I don't know how.
    await mkTable('foo')
    console.log('Oh yeah, this is how args work', foo, bar)
  },
  async [DIRECTION_DOWN] (foo, bar) {
    // Remove the foo table.
    await rmTable('foo')
    console.log('Oh yeah, this is how args work', foo, bar)
  }
}

Create an array of all your migrations. The order isn't important, except for readability. Minigrate will sort the migrations before running them.

./migrations/index.js

'use strict'

module.exports = [
  require('./1516226643067-create-foo-table')
]

Use the Minigrate API to run migrations.

./migrate.js

'use strict'

const { up, down } = require('minigrate')
const args = [ 'foo', 'bar' ]

migrate()

async function migrate () {
  await up({
    getCurrentMigration,
    setCurrentMigration,
    migrations,
    args
  })

  console.log('Migrated')

  await down({
    getCurrentMigration,
    setCurrentMigration,
    migrations,
    args
  })

  console.log('Reverted')
}

function getCurrentMigration () {
  // Get the name of the current migration. I dunno how, you figure that out ๐Ÿคทโ€.
  // You can return a promise; Minigrate will await it.
  // Return undefined if no migration has yet been run.
}

function setCurrentMigration (name) {
  // Set the name of the current migration. This is called after each successful
  // migration. You should write it to your database, or a file, or something.
  // You can return a promise; Minigrate will await it.
}

Why did you make this?

I didn't really want to. First I tried migrate, but it lacked a feature I needed; the ability to pass arguments to migration functions. I forked migrate, but I made a bit of a mess of implementing my changes.

I wrote my own because:

  1. I needed to pass the database instance into migration functions via arguments.
  2. I wanted a library that could work in node and on the web, which ideally meant avoiding reading migrations from the file system.
  3. Making it for the web means keeping it small.
  4. How difficult can it be? ๐Ÿ‘€

The web?

Yes. This should work on the web, however it is not currently packaged for the web. It needs to be bundled.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.