https://github.com/bitsofinfo/io-event-reactor-plugin-mysql
Mysql filesystem event reactor plugin for: io-event-reactor https://github.com/bitsofinfo/io-event-reactor
https://github.com/bitsofinfo/io-event-reactor-plugin-mysql
Last synced: 10 months ago
JSON representation
Mysql filesystem event reactor plugin for: io-event-reactor https://github.com/bitsofinfo/io-event-reactor
- Host: GitHub
- URL: https://github.com/bitsofinfo/io-event-reactor-plugin-mysql
- Owner: bitsofinfo
- Created: 2016-06-06T20:40:32.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2016-06-10T14:29:46.000Z (over 9 years ago)
- Last Synced: 2025-01-29T11:12:58.412Z (12 months ago)
- Language: JavaScript
- Size: 40 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# io-event-reactor-plugin-mysql
Mysql filesystem event reactor plugin for: [io-event-reactor](https://github.com/bitsofinfo/io-event-reactor)
[](https://nodei.co/npm/io-event-reactor-plugin-mysql/)
## Usage
To configure this ReactorPlugin in your application that uses [io-event-reactor](https://github.com/bitsofinfo/io-event-reactor) do the following
```
npm install io-event-reactor-plugin-mysql
```
Then in your [io-event-reactor](https://github.com/bitsofinfo/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
```