io-event-reactor-plugin-mysql

1.0.0-beta.6 • Public • Published

io-event-reactor-plugin-mysql

Mysql filesystem event reactor plugin for: io-event-reactor

NPM

Usage

To configure this ReactorPlugin in your application that uses io-event-reactor do the following

npm install io-event-reactor-plugin-mysql

Then in your io-event-reactor configuration object that you pass to the IoReactorService constructor, you can specify this plugin in the reactors block as so:

var ioReactorServiceConf = {

  ...

  ioReactors: [

          {
              id: "reactor1",

              monitor: {
                  ...
              },

              evaluators: [
                  {
                      evaluator: myEvaluatorFunction,
                      reactors: ['mysql1,...'] // binds mysql to this evaluator
                  }
              ],

              reactors: [

                  // the "id" is what you use to bind this reactor
                  // to an evaluator above
                  { id: "mysql1",
                    plugin: "io-event-reactor-plugin-mysql",

                    config: {

                          // a node-mysql connection pool configuration object:
                          // see: https://github.com/felixge/node-mysql#pool-options
                          //      https://github.com/felixge/node-mysql#connection-options,
                          poolConfig : {
                              host     : 'hostname',
                              user     : 'username',
                              password : 'pw',
                              database : 'dbname',
                              multipleStatements: true,
                              ....
                          },

                          /**
                          * 'sqlTemplates' - an array of mustache (https://github.com/janl/mustache.js) SQL template strings that will be executed
                          *                  in order using node-mysql when this plugin's react() is invoked.
                          *                  (all statements from here and sqlGenerator below exec in a single transaction)
                          *
                          *  Supported mustache template variables that will be made available to you: (a full IoEvent)
                          *    - see https://github.com/bitsofinfo/io-event-reactor-plugin-support for IoEvent definition
                          *    - ioEvent.uuid
                          *    - ioEvent.eventType: one of: 'add', 'addDir', 'unlink', 'unlinkDir', 'change'
                          *    - ioEvent.fullPath: string full path to file being reacted to (filename/dir inclusive)
                          *    - ioEvent.parentPath: full path to the directory containing the item manipulated
                          *    - ioEvent.parentName: parent directory name only
                          *    - ioEvent.filename: filename/dirname only (no path information)
                          *    - ioEvent.optionalFsStats: optional stats object -> https://nodejs.org/docs/latest/api/fs.html#fs_class_fs_stats
                          *    - ioEvent.optionalExtraInfo: optional object, see the MonitorPlugin you are using to see the spec and when/if its available
                          */
                          sqlTemplates: [
                              'INSERT INTO io_event (eventType,fullPath,stats)      VALUES("{{{ioEvent.eventType}}}","{{{ioEvent.fullPath}}}","{{{ioEvent.optionalFsStats}}}")'
                          ],

                          /**
                          *  - 'sqlGenerator' - callback function(ioEvent) that must return an array[] of sql
                          *                     statements literals that will be executed in order using
                          *                     node-mysql when this plugin's react() is invoked.
                          *                     (all statements from here and sqlTemplates above exec in a single transaction)
                          */
                          sqlGenerator: function(ioEvent) {
                              return [('INSERT INTO io_event2 (eventType,fullPath,stats) VALUES("'+ioEvent.eventType+'","'+ioEvent.fullPath+'","'+(ioEvent.optionalFsStats ? ioEvent.optionalFsStats.size : '?') +'")')];
                          },
                      }
                  },

                  ....
              ]
        },
        ....
    ]

    ...
};

Security

Be aware that this plugin takes raw input from events generated by a monitor plugin and allows you to use that data in custom SQL statements. This potentially could open you up to certain edge cases where SQL injection could occur. If you are concerned about this you may only want to use the sqlGenerator option to pre-santize all arguments before you create SQL statements to be executed.

Unit tests

To run the unit tests go to the root of the project and run the following. Note the username below should have access to drop/create tables in the target testDbName

export mysqlHost=[ip/host] mysqlUser=[username] mysqlPw=[pw] mysqlDb=[testDbName]; mocha test/all.js

Package Sidebar

Install

npm i io-event-reactor-plugin-mysql

Weekly Downloads

2

Version

1.0.0-beta.6

License

ISC

Last publish

Collaborators

  • bitsofinfo