From dee637f4f769542581410de7822ad3e041c4b58b Mon Sep 17 00:00:00 2001 From: Arthur Clement Date: Wed, 22 May 2019 10:41:19 +1000 Subject: [PATCH 1/2] Ability to provide additional event emitter to emit on. Allows to have one separete emitter per migrate if needed (for logging purposes for example) --- lib/migrate.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/lib/migrate.js b/lib/migrate.js index 41c20d9..6b59b55 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -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) } @@ -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) => { @@ -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) } }) @@ -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) } @@ -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) } @@ -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) } @@ -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 } } From 85191a7aec473d14efca5316b29c2dc489a0ae40 Mon Sep 17 00:00:00 2001 From: Arthur Clement Date: Wed, 22 May 2019 11:59:15 +1000 Subject: [PATCH 2/2] remove semicolon --- lib/migrate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/migrate.js b/lib/migrate.js index 6b59b55..db55547 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -361,7 +361,7 @@ function emit (name, data, options) { return function (arg) { internals.emitter.emit(name, data) if (options && options.emitter) { - options.emitter.emit(name, data); + options.emitter.emit(name, data) } return arg }