{"_id":"@apagiaro/mysql-events","_rev":"2-ae54bf5b571a866e9e5d4ac6b186cdc4","name":"@apagiaro/mysql-events","dist-tags":{"latest":"0.7.0"},"versions":{"0.6.0":{"name":"@apagiaro/mysql-events","version":"0.6.0","license":"BSD-3-Clause","description":"A node.js package that watches a MySQL database and runs callbacks on matched events like updates on tables and/or specific columns.","homepage":"https://github.com/alessandro308/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/rodrigogs/mysql-events.git"},"main":"index.js","scripts":{"eslint":"eslint . --ext .js","test":"npm run test:55 && npm run test:56 && npm run test:57 && npm run test:80","test:55":"cross-env DATABASE_PORT=3355 jest --forceExit --runInBand","test:56":"cross-env DATABASE_PORT=3356 jest --forceExit --runInBand","test:57":"cross-env DATABASE_PORT=3357 jest --forceExit --runInBand","test:80":"cross-env DATABASE_PORT=3380 jest --forceExit --runInBand","test:local":"./scripts/test.sh","coverage":"nyc --reporter=lcov npm test"},"dependencies":{"@rodrigogs/zongji":"^0.4.14","debug":"^4.1.1","debuggler":"^1.0.0","mysql":"^2.17.1"},"devDependencies":{"chai":"^4.2.0","codeclimate-test-reporter":"^0.5.1","cross-env":"^5.2.0","dotenv-cli":"^2.0.0","eslint":"^5.16.0","eslint-config-airbnb-base":"^13.1.0","eslint-plugin-import":"^2.17.2","jest":"^24.8.0","nyc":"^14.1.1"},"engines":{"node":">=7.6.0"},"contributors":[{"name":"Rodrigo Gomes da Silva","email":"rodrigo.smscom@gmail.com","url":"https://github.com/rodrigogs"}],"gitHead":"316a8b2bf30760c8271d60adea8ed3e575c1f1c8","bugs":{"url":"https://github.com/rodrigogs/mysql-events/issues"},"_id":"@apagiaro/mysql-events@0.6.0","_nodeVersion":"12.18.2","_npmVersion":"6.11.3","dist":{"integrity":"sha512-DmZnNVencNn+J6qgF0jQIA+EZ3pm1aZGACDX/nx1KXfSoBbZFd5cPNwcvH2+tcE8Wj3JBK9+QmyYDWCrP1uBIw==","shasum":"1b658bea58626459bf907d321f63d35433328922","tarball":"https://registry.npmjs.org/@apagiaro/mysql-events/-/mysql-events-0.6.0.tgz","fileCount":16,"unpackedSize":236722,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfdtyGCRA9TVsSAnZWagAAHO4QAJeWWZdGAB5aUQGVJ4dQ\nkDkVdy1B4/lbwEjSzuyKKoI08V+SlLhL9K/WpT5wDbPA2BSrZ+eniOSyOAYZ\nY0pweklH8Wdu47jo2UhEcS9jPWT09FQlApaQyBhPxrlZjkgWFCYhZs7sUEOC\nrT6wD1izT1xbtT6FjKc2YIZDv5FekufTBYkmXWDVMCZMtdEv/uAptQuZ84yO\nLkU2noEaC8mUeRR2irjvMfgNMjL7dC8Rd7guni6v6P9E95vaPtBnPRj5qQ/m\n5z9zbwijUrvcGENWCKNocEjhqLmaA9pd+EAEBOjPq2d1grtKFDErfdsRNQEi\nSccSk9+h1eLqIbh8HNfEnmA/yYCS2EaTZJOjuqZ3wgIM/uc0xdQ7Fu9XBCXV\no95rJr81vg17DjehMcKzr5ps5pzoqxXCZI6Vil1vB75+5NVkb9xwpUrFCA3D\n2TNSPybjbH9xiU7OKZV/busBpXHTQuYBLyd/htP+gj6I/pVIGQAS3DiGnNg3\nWnZCd7Vrz9Z3BUXI55DRxB6Ay0FMVewzHsvZPEdRFgjBVKZF/Enshcti2pk4\nyEGqR4CVZDRrY5Zzd4t+MTrzaZ9YDfemPSoqTTXEGq4MF9OqDPgK2t1Hj7k1\n8Alv1fUpvnsS5asIj2msKbQ2HC57+4N+ykIeSqn8OM5dvgsg9ZlD9UPOvHO4\nIMBz\r\n=0KK6\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQDennWzWB4MpcCNdfzc6SssycVujSjtJIffTHPs8O5vewIhANqt6Erz77APuj2IUmmEuX6PixlQ4NbvtUbjm+mB+GFe"}]},"maintainers":[{"name":"apagiaro","email":"alessandropagiaro@gmail.com"}],"_npmUser":{"name":"apagiaro","email":"alessandropagiaro@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mysql-events_0.6.0_1601625222432_0.3279006722003768"},"_hasShrinkwrap":false},"0.7.0":{"name":"@apagiaro/mysql-events","version":"0.7.0","license":"BSD-3-Clause","description":"A node.js package that watches a MySQL database and runs callbacks on matched events like updates on tables and/or specific columns.","homepage":"https://github.com/alessandro308/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/rodrigogs/mysql-events.git"},"main":"index.js","scripts":{"eslint":"eslint . --ext .js","test":"npm run test:55 && npm run test:56 && npm run test:57 && npm run test:80","test:55":"cross-env DATABASE_PORT=3355 jest --forceExit --runInBand","test:56":"cross-env DATABASE_PORT=3356 jest --forceExit --runInBand","test:57":"cross-env DATABASE_PORT=3357 jest --forceExit --runInBand","test:80":"cross-env DATABASE_PORT=3380 jest --forceExit --runInBand","test:local":"./scripts/test.sh","coverage":"nyc --reporter=lcov npm test"},"dependencies":{"@rodrigogs/zongji":"^0.4.14","debug":"^4.1.1","debuggler":"^1.0.0","mysql":"^2.17.1"},"devDependencies":{"chai":"^4.2.0","codeclimate-test-reporter":"^0.5.1","cross-env":"^5.2.0","dotenv-cli":"^2.0.0","eslint":"^5.16.0","eslint-config-airbnb-base":"^13.1.0","eslint-plugin-import":"^2.17.2","jest":"^24.8.0","nyc":"^14.1.1"},"engines":{"node":">=7.6.0"},"contributors":[{"name":"Rodrigo Gomes da Silva","email":"rodrigo.smscom@gmail.com","url":"https://github.com/rodrigogs"}],"gitHead":"f9264e03e68114e5bf899d53c2e803b423765467","bugs":{"url":"https://github.com/rodrigogs/mysql-events/issues"},"_id":"@apagiaro/mysql-events@0.7.0","_nodeVersion":"12.18.2","_npmVersion":"6.11.3","dist":{"integrity":"sha512-dexVpdSY5SALBzvFRfLq64pEHAx6+U6TS7V+BQYbt3Ut7yjDioUqia5YlX2SgIwCYYJqK5h47gOu+TqIGRYEhA==","shasum":"de2159b13b66ba1e6fbad845781aad00748265fe","tarball":"https://registry.npmjs.org/@apagiaro/mysql-events/-/mysql-events-0.7.0.tgz","fileCount":16,"unpackedSize":236975,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.13\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJfdwZrCRA9TVsSAnZWagAAWhgP/3zlaCwiC0/k6f3WkbJQ\n4HUVillpFYnqs6yfSr9jm21a6ijsyIT198dFE2S2G2NhqzHzv0SLAS8qQgLt\n3ZWbAD7Ri/35i6T6rBEIF0dRbWQ3pzuCZHC/5H4X0GPbvPSue4E+wfuE9png\n3Coppawc61OSuueQmiZRytHNevzHi2HtjEyXi/0YsPrf+v8CQZsWmIEA6SdL\nTghdbXRDd5QLU2SwZdHFwdwsU8Oy2UXZNjzwE6BLvdszBUQvlEeWwA/5IK++\nrPcnoahnzhI9EnNrgcxoJsRpBVqNw95j1M4n3znZC177WVa3jfFaPGmli3RP\na+G9JpyUD7A4NldQVKgrb+/jXbiTuDu5BLadVFfiBDROJltSJvlXp8SlhbdV\nRHUlmMc9twOf3gfjAjRrHjYqAs188em/BOmIitgQNwGA29lDQgQL/7TmP7II\n89FX0BSCIhiJ2mSqNJc3rQQ1BrSQPKLFzDuTz8Fq7OcesljzyH6RgtoX9mRJ\nCsDnT9iV1dHRwBkSzmrHzrr6nNcHLOzSGtGA+5vj7R9E1fiUdZzU9h2xgW31\n3+j7qzwGFw9vRAHvmpGnwPXH8e/Fn+wrwFb0iuJmSieBHhc5IJVNWCz3bwhs\nZISsEULz2ttfjAUGyCYTqrpv8qIik07lk3LFsBsYXCkCkeEciPZq6j3BNl50\nyZzv\r\n=Ub5m\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQCfDyDyM88qOvrywHI2LpodD2acYRlHIfhzEjZVjbq+UgIgCYo7ZYJACN9qOByMDtMhokqisMDsjytxYqy8XTZYu4o="}]},"maintainers":[{"name":"apagiaro","email":"alessandropagiaro@gmail.com"}],"_npmUser":{"name":"apagiaro","email":"alessandropagiaro@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mysql-events_0.7.0_1601635947063_0.9166663630766021"},"_hasShrinkwrap":false}},"time":{"created":"2020-10-02T07:53:42.401Z","0.6.0":"2020-10-02T07:53:42.546Z","modified":"2022-04-04T14:30:42.577Z","0.7.0":"2020-10-02T10:52:27.374Z"},"maintainers":[{"name":"apagiaro","email":"alessandropagiaro@gmail.com"}],"description":"A node.js package that watches a MySQL database and runs callbacks on matched events like updates on tables and/or specific columns.","homepage":"https://github.com/alessandro308/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/rodrigogs/mysql-events.git"},"contributors":[{"name":"Rodrigo Gomes da Silva","email":"rodrigo.smscom@gmail.com","url":"https://github.com/rodrigogs"}],"bugs":{"url":"https://github.com/rodrigogs/mysql-events/issues"},"license":"BSD-3-Clause","readme":"# mysql-events\n[![CircleCI](https://circleci.com/gh/rodrigogs/mysql-events.svg)](https://circleci.com/gh/rodrigogs/mysql-events)\n[![Code Climate](https://codeclimate.com/github/rodrigogs/mysql-events/badges/gpa.svg)](https://codeclimate.com/github/rodrigogs/mysql-events)\n[![Test Coverage](https://codeclimate.com/github/rodrigogs/mysql-events/badges/coverage.svg)](https://codeclimate.com/github/rodrigogs/mysql-events/coverage)\n\nA [node.js](https://nodejs.org) package that watches a MySQL database and runs callbacks on matched events.\n\nThis package is based on the [original ZongJi](https://github.com/nevill/zongji) and the [original mysql-events](https://github.com/spencerlambert/mysql-events) modules. Please make sure that you meet the requirements described at [ZongJi](https://github.com/rodrigogs/zongji#installation), like MySQL binlog etc.\n\nCheck [@kuroski](https://github.com/kuroski)'s [mysql-events-ui](https://github.com/kuroski/mysql-events-ui) for a `mysql-events` UI implementation.\n\n## Install\n```sh\nnpm install @rodrigogs/mysql-events\n```\n\n## Quick Start\n```javascript\nconst mysql = require('mysql');\nconst MySQLEvents = require('@rodrigogs/mysql-events');\n\nconst program = async () => {\n  const connection = mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    password: 'root',\n  });\n\n  const instance = new MySQLEvents(connection, {\n    startAtEnd: true,\n    excludedSchemas: {\n      mysql: true,\n    },\n  });\n\n  await instance.start();\n\n  instance.addTrigger({\n    name: 'TEST',\n    expression: '*',\n    statement: MySQLEvents.STATEMENTS.ALL,\n    onEvent: (event) => { // You will receive the events here\n      console.log(event);\n    },\n  });\n  \n  instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);\n  instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);\n};\n\nprogram()\n  .then(() => console.log('Waiting for database events...'))\n  .catch(console.error);\n```\n[Check the examples](https://github.com/rodrigogs/mysql-events/examples)\n\n## Usage\n  ### #constructor(connection, options)\n  - Instantiate and create a database connection using a DSN\n    ```javascript\n    const dsn = {\n      host: 'localhost',\n      user: 'username',\n      password: 'password',\n    };\n\n    const myInstance = new MySQLEvents(dsn, { /* ZongJi options */ });\n    ```\n\n  - Instantiate and create a database connection using a preexisting connection\n    ```javascript\n    const connection = mysql.createConnection({\n      host: 'localhost',\n      user: 'username',\n      password: 'password',\n    });\n\n    const myInstance = new MySQLEvents(connection, { /* ZongJi options */ });\n    ```\n  - Options(the second argument) is for ZongJi options\n    ```javascript\n    const myInstance = new MySQLEvents({ /* connection */ }, {\n      serverId: 3,\n      startAtEnd: true,\n    });\n    ```\n    [See more about ZongJi options](https://github.com/rodrigogs/zongji#zongji-class)\n\n  ### #start()\n  - start function ensures that MySQL is connected and ZongJi is running before resolving its promise\n    ```javascript\n    myInstance.start()\n      .then(() => console.log('I\\'m running!'))\n      .catch(err => console.error('Something bad happened', err));\n    ```\n  ### #stop()\n  - stop function terminates MySQL connection and stops ZongJi before resolving its promise\n    ```javascript\n    myInstance.stop()\n      .then(() => console.log('I\\'m stopped!'))\n      .catch(err => console.error('Something bad happened', err));\n    ```\n  ### #pause()\n  - pause function pauses MySQL connection until `#resume()` is called, this it useful when you're receiving more data than you can handle at the time\n    ```javascript\n    myInstance.pause();\n    ```\n  ### #resume()\n  - resume function resumes a paused MySQL connection, so it starts to generate binlog events again\n    ```javascript\n    myInstance.resume();\n    ```\n  ### #addTrigger({ name, expression, statement, onEvent })\n  - Adds a trigger for the given expression/statement and calls the `onEvent` function when the event happens\n    ```javascript\n    instance.addTrigger({\n      name: 'MY_TRIGGER',\n      expression: 'MY_SCHEMA.MY_TABLE.MY_COLUMN',\n      statement: MySQLEvents.STATEMENTS.INSERT,\n      onEvent: async (event) => {\n        // Here you will get the events for the given expression/statement.\n        // This could be an async function.\n        await doSomething(event);\n      },\n    });\n    ```\n  - The `name` argument must be unique for each expression/statement, it will be user later if you want to remove a trigger\n    ```javascript\n    instance.addTrigger({\n      name: 'MY_TRIGGER',\n      expression: 'MY_SCHEMA.*',\n      statement: MySQLEvents.STATEMENTS.ALL,\n      ...\n    });\n\n    instance.removeTrigger({\n      name: 'MY_TRIGGER',\n      expression: 'MY_SCHEMA.*',\n      statement: MySQLEvents.STATEMENTS.ALL,\n    });\n    ```\n  - The `expression` argument is very dynamic, you can replace any step by `*` to make it wait for any schema, table or column events\n    ```javascript\n    instance.addTrigger({\n      name: 'Name updates from table USERS at SCHEMA2',\n      expression: 'SCHEMA2.USERS.name',\n      ...\n    });\n    ```\n    ```javascript\n    instance.addTrigger({\n      name: 'All database events',\n      expression: '*',\n      ...\n    });\n    ```\n    ```javascript\n    instance.addTrigger({\n      name: 'All events from SCHEMA2',\n      expression: 'SCHEMA2.*',\n      ...\n    });\n    ```\n    ```javascript\n    instance.addTrigger({\n      name: 'All database events for table USERS',\n      expression: '*.USERS',\n      ...\n    });\n    ```\n  - The `statement` argument indicates in which database operation an event should be triggered\n    ```javascript\n    instance.addTrigger({\n      ...\n      statement: MySQLEvents.STATEMENTS.ALL,\n      ...\n    });\n    ```\n    [Allowed statements](https://github.com/rodrigogs/mysql-events/blob/master/lib/STATEMENTS.enum.js)\n  - The `onEvent` argument is a function where the trigger events should be threated\n    ```javascript\n    instance.addTrigger({\n      ...\n      onEvent: (event) => {\n        console.log(event); // { type, schema, table, affectedRows: [], affectedColumns: [], timestamp, }\n      },\n      ...\n    });\n    ```\n  ### #removeTrigger({ name, expression, statement })\n  - Removes a trigger from the current instance\n    ```javascript\n    instance.removeTrigger({\n      name: 'My previous created trigger',\n      expression: '',\n      statement: MySQLEvents.STATEMENTS.INSERT,\n    });\n    ```\n  ### Instance events\n  - MySQLEvents class emits some events related to its MySQL connection and ZongJi instance\n    ```javascript\n    instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, (err) => console.log('Connection error', err));\n    instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, (err) => console.log('ZongJi error', err));\n    ```\n  [Available events](https://github.com/rodrigogs/mysql-events/blob/master/lib/EVENTS.enum.js)\n\n## Tigger event object\nIt has the following structure:\n```javascript\n{\n  type: 'INSERT | UPDATE | DELETE',\n  schema: 'SCHEMA_NAME',\n  table: 'TABLE_NAME',\n  affectedRows: [{\n    before: {\n      column1: 'A',\n      column2: 'B',\n      column3: 'C',\n      ...\n    },\n    after: {\n      column1: 'D',\n      column2: 'E',\n      column3: 'F',\n      ...\n    },\n  }],\n  affectedColumns: [\n    'column1',\n    'column2',\n    'column3',\n  ],\n  timestamp: 1530645380029,\n  nextPosition: 1343,\n  binlogName: 'bin.001',\n}\n```\n\n**Make sure the database user has the privilege to read the binlog on database that you want to watch on.**\n\n## LICENSE\n[BSD-3-Clause](https://github.com/rodrigogs/mysql-events/blob/master/LICENSE) © Rodrigo Gomes da Silva\n","readmeFilename":"README.md"}