@sichangi/migrate
v0.0.1
Published
A database migration library for mongodb via mongoose
Downloads
4
Maintainers
Readme
Migrate
A database migration library for mongodb via mongoose
A personalized fork of migrate-mongoose
by Balmasi et al
Getting Started
Install from npm / yarn
yarn add @sichangi/migrate
Usage
migrate -h
Commands:
migrate list [options]
migrate create <migration_name> [options]
migrate up <migration_name> [options]
migrate down <migration_name> [options]
migrate prune [options]
Config File
The config file source path defaults to ./migrate.[json|js]
{
"autoSync": false,
"migrationsDir": "./migrations",
"collectionName": "migrations",
"dbConnectionUri": "mongodb+srv://username:[email protected]/database",
}
Migration Files
Here's an example of a migration created using migrate create some-migration-name
.
migrations/1562460744403-some-migration-name.js
/**
* Make any changes you need to make to the database here
*/
async function up () {
// Write migration here
}
/**
* Make any changes that UNDO the up function;s operations here
*/
async function down () {
// Write migration here
}
module.exports = { up, down };
Sample setup
Below is an example of a typical setup in a mongoose project
models/user.model.js
const {Schema} = require('mongoose');
const UserSchema = new Schema({
firstName: String,
lastName: String,
});
module.exports = mongoose.model('user', UserSchema);
models/index.js
const mongoose = require('mongoose');
const User = require('./user.model');
mongoose.connect('mongodb://localhost:27017/mydb', { useNewUrlParser: true })
module.exports = { User };
migrations/1459287720919-my-migration.js
import { User } from '../models'
async function up() {
// Then you can use it in the migration like so
await User.create({ firstName: 'Ada', lastName: 'Lovelace' });
}