Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to provide additional event emitter to emit on #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions lib/migrate.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ const Path = require('path')
const internals = {}

const Migrate = function (opt) {
emit('info', 'Validating options')()
emit('info', 'Validating options', opt)()
return validateOptions(opt)
.then(emit('info', 'Connecting to RethinkDB'))
.then(emit('info', 'Connecting to RethinkDB', opt))
.then(connectToRethink)
.then(createDbIfInexistent)
.then(emit('info', 'Executing Migrations'))
.then(emit('info', 'Executing Migrations', opt))
.then(executeMigration)
.then(emit('info', 'Closing connection'))
.then(emit('info', 'Closing connection', opt))
.then(closeConnection)
}

Expand Down Expand Up @@ -69,7 +69,8 @@ function validateOptions (options) {
.description('The port to connect on')
}))
}),
ssl: Joi.alternatives().try(Joi.object(), Joi.boolean()).default(false).description('Rethinkdb SSL/TLS support')
ssl: Joi.alternatives().try(Joi.object(), Joi.boolean()).default(false).description('Rethinkdb SSL/TLS support'),
emitter: Joi.any().description('An alternate event emitter on which to emit event')
}).without('user', 'username').without('password', 'authKey').required()

return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -134,7 +135,7 @@ function createDbIfInexistent (options) {
.then(toArray)
.then(list => {
if (list.indexOf(db) < 0) {
emit('info', 'Creating db', db)()
emit('info', 'Creating db', db, options)()
return r.dbCreate(db).run(conn)
}
})
Expand Down Expand Up @@ -174,13 +175,13 @@ function migrateUp (options) {
return migrationSteps
})
.then(migrationSteps => runMigrations('up', migrationSteps, options))
.then(emit('info', 'Saving metadata'))
.then(emit('info', 'Saving metadata', options))
.then(executedMigrations => saveExecutedMigrationsMetadata(executedMigrations, options))
.then(() => {
const migrationMessage = steps
? `Executed ${steps} migration${steps > 1 ? 's' : ''}.`
: `No migrations executed.`
emit('info', migrationMessage)()
emit('info', migrationMessage, options)()
})
.then(() => options)
}
Expand All @@ -200,7 +201,7 @@ function migrateDown (options) {
const migrationMessage = steps
? `Cleared ${steps} migration${steps > 1 ? 's' : ''} from table.`
: 'Migrations table already clear.'
emit('info', migrationMessage)()
emit('info', migrationMessage, options)()
})
.then(() => options)
}
Expand Down Expand Up @@ -315,7 +316,7 @@ function runMigrations (direction, migrations, options) {
const { r, conn } = options
return migrations.reduce(
(chain, migration) => chain.then(() => migration.code[direction](r, conn)
.then(emit('info', `Executed migration ${migration.name} ${options.op}`))),
.then(emit('info', `Executed migration ${migration.name} ${options.op}`, options))),
Promise.resolve()
).then(() => migrations)
}
Expand Down Expand Up @@ -356,9 +357,12 @@ function closeConnection (options) {
return conn.close()
}

function emit (name, data) {
function emit (name, data, options) {
return function (arg) {
internals.emitter.emit(name, data)
if (options && options.emitter) {
options.emitter.emit(name, data)
}
return arg
}
}