# better-migration

> This is a simple tool to migrate data.

Latest version **1.0.1** (published 2016-12-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install better-migration
pnpm add better-migration
yarn add better-migration
bun add better-migration
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.1 |
| Published | 2016-12-12 |
| First published | 2016-12-12 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Diamond Inc. |
| Maintainers | diam |
| Keywords | migrate, migration, data, semver |

## Links

- npm: https://www.npmjs.com/package/better-migration
- Repository: https://github.com/diamondio/better-migration
- Issues: https://github.com/diamondio/better-migration/issues
- npm.io page: https://npm.io/package/better-migration

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^2.1.4
- [semver](https://npm.io/package/semver.md) ^5.3.0
- [lockfile](https://npm.io/package/lockfile.md) ^1.0.3
- [semver-sort](https://npm.io/package/semver-sort.md) 0.0.4

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 1.0.1 (latest) — 2016-12-12
- 1.0.0 — 2016-12-12

## README

Better Migration - Make migrations easy!
---------------------

[![npm package](https://nodei.co/npm/better-migration.png?downloads=true&downloadRank=true&stars=true)](https://nodei.co/npm/better-migration/)
[![Build status](https://img.shields.io/travis/diamondio/better-migration.svg?style=flat-square)](https://travis-ci.org/diamondio/better-migration)
[![Dependency Status](https://img.shields.io/david/diamondio/better-migration.svg?style=flat-square)](https://david-dm.org/diamondio/better-migration)
[![Known Vulnerabilities](https://snyk.io/test/npm/better-migration/badge.svg?style=flat-square)](https://snyk.io/test/npm/better-migration)
[![Gitter](https://img.shields.io/badge/gitter-join_chat-blue.svg?style=flat-square)](https://gitter.im/diamondio/better-migration?utm_source=badge)

This is a simple tool to migrate data. It features some key features:

 - Easy to write migrations: just implement the function
 - Out of the box auto-save current version (into a simple text file)
 - Out of the box locking mechanism to ensure only one migration is running
 - Customizable version saving and locking mechanism


```bash
npm install --save better-migration
```

## Usage

```js
var migration = require('better-migration');
```

The idea behind this tool is simple:

#### 1. Write a migration script
```js
migration.version('1.0.0', function (cb) {
	// Your migration script, call cb() when done.
	// cb(err) will abort the migration.
	cb();
})
```

#### 2. Start the migration

This will run your migration scripts one at a time until it reaches the latest version.

```js
migration.start(function (err) {
	// If the error is set, the script failed, and you should probably not continue.
	// Your data is updated to the latest semver version (in this case is 1.0.0)
})
```

When you need to migrate again, just call migration.set with the new version number, and restart.


## Advanced usage

By default, we save the current version into a file, `./version`. You may change this:

```js
var migration = require('better-migration');
```

Optionally, you may set your own functions for setting the version/lock:

```js
// Set the function to get the version
migration.get = function (cb) {
	cb(null, '1.0.0');
};

// Set the function to set the version
migration.set = function (v, cb) {
	cb(); // Call when saved
}

// Set the function to obtain a lock for migration.
migration.lock = function (cb) {
	// After you acquire the lock, you must return a release function
	// so that we can release the lock after the migration is complete.
	cb(err, release)
}
```


Contributions welcome!

### Credits
This library was initially made by the awesome team of engineers at [Diamond](https://diamond.io).

If you haven't already, make sure you install Diamond!

---
_Source: https://npm.io/package/better-migration · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
