{"_id":"@cenkingunlugu/mysql-events","_rev":"2-c348baf10cbf324a11c92caf41850300","name":"@cenkingunlugu/mysql-events","dist-tags":{"latest":"0.6.1"},"versions":{"0.6.0":{"name":"@cenkingunlugu/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/cenkingunlugu/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/cenkingunlugu/mysql-events.git"},"publishConfig":{"access":"public"},"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":{"zongji":"^0.5.1","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"},"bugs":{"url":"https://github.com/cenkingunlugu/mysql-events/issues"},"directories":{"example":"examples","lib":"lib"},"author":{"name":"Cenk Ebret"},"contributors":[{"name":"Cenk Ebret","email":"cenkebret@gmail.com","url":"https://github.com/cenkingunlugu"}],"gitHead":"ff0a342aafab427a08ce48bc31bc872b4244d9b2","_id":"@cenkingunlugu/mysql-events@0.6.0","_nodeVersion":"12.16.1","_npmVersion":"6.13.4","dist":{"integrity":"sha512-EM/Yy+qE6RLAvVzMp3f4/smXT3aQKnU92AMPE8TC7++/1aJrQCM+lBnBKT/clR3FFmTAPOyeOjmF5x/8a9v8zw==","shasum":"20df3583a53087e48c410a7544cd33a617a86121","tarball":"https://registry.npmjs.org/@cenkingunlugu/mysql-events/-/mysql-events-0.6.0.tgz","fileCount":16,"unpackedSize":47899,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJelZHGCRA9TVsSAnZWagAAGV0P/01joBKxCbPh9K9sC44Q\nTgJndNenka2kRTtRi5ntGoth8vDfHCF+cyg/IhOugPecEIIFImzmBBeJJnZp\no9zo5KXNCiZLtmKRzHhiRaeG1mUpSefDHwPCX4ViDE8z+u3nIDKz+JKss0x+\nFmYXwKBt1Lxr7MCL2/2/SwtaGmoF9NPfbhy06UH0NX7zt99fdFDwQPcQ/vzv\n8fLtqxUftM7fdnuu5NLsZw4I4tL+ihThYTapZn4YwUB688DaZpXR2orsxZlS\nzYVs4kmVpt+j5DrlU4778cTfSWzqnnAmCRIc522+H77plbeBvnA5mZSzHsBY\nP5j+FdoVG4Xexr+Jf8kv/01amJtcjpL6envJ70HpxaNU8/JY+qP63zVq1P3O\nIrB13PF50+2gd+tI2etiEegcA8317VyaZyJc/8hZzG7hBvzzMl1Ce87KVpQW\n0qgDnrHWfeZfbWI8sxSesZTKyhWmQLfcnycec4H3eZUiifMs5srDF/JT/3km\nVH2ajHo2P3B5gpJ6PFyC1l+rRTVdZfUsW7gPdTHZhqwOUfO/3yY1zfrgKTKq\nfFvaGLBYHDZ2Gu+wTfFAn8Ix4NsDt2aI9C24EY+A1wRlvXeuCYYYkzAektiI\nsi8Ypino+aa4BfPTxc6Yjt5Bg/HE6NERphSlnz7FnWDIZAe8bqbQvvm2QW6x\nMtn3\r\n=Fsbd\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIQDeGhUqzlgBvZhhoNyMn+nfLoat1qQ475GZ9zDBfsUf4gIgYpp6rsCoYdLzb2iGsr3BMECI4YSOmZjZtoaMWdpx65k="}]},"maintainers":[{"name":"cenkingunlugu","email":"cenkebret@gmail.com"}],"_npmUser":{"name":"cenkingunlugu","email":"cenkebret@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mysql-events_0.6.0_1586860485792_0.05392031091442928"},"_hasShrinkwrap":false},"0.6.1":{"name":"@cenkingunlugu/mysql-events","version":"0.6.1","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/cenkingunlugu/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/cenkingunlugu/mysql-events.git"},"publishConfig":{"access":"public"},"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":{"zongji":"^0.5.1","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"},"bugs":{"url":"https://github.com/cenkingunlugu/mysql-events/issues"},"directories":{"example":"examples","lib":"lib"},"author":{"name":"Cenk Ebret"},"contributors":[{"name":"Cenk Ebret","email":"cenkebret@gmail.com","url":"https://github.com/cenkingunlugu"}],"gitHead":"a434880ce1ca3a93059b71f4f6c765d5994867fc","_id":"@cenkingunlugu/mysql-events@0.6.1","_nodeVersion":"12.16.1","_npmVersion":"6.13.4","dist":{"integrity":"sha512-RmCy64jIKPMFhvgS6W29znrYrrMeW6tdOwKlb4MK8BrRREzF0vQAY22HgI/jP0vEGygj44DoYxdrUVGsSOljmA==","shasum":"c54699e7dfdd5bc62777d7dabd9bf61a0022bc9c","tarball":"https://registry.npmjs.org/@cenkingunlugu/mysql-events/-/mysql-events-0.6.1.tgz","fileCount":16,"unpackedSize":47884,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJelZbWCRA9TVsSAnZWagAAvTMP/A65U66SMJjP7rUGVrGq\nEOMt6mVIAgXjBpEkMFwAc/kZk2XQcsWYKP98mLoMKB4f/hv+RmY8ZoN70Yp0\nE1ovIjql4Yuag7lQnKXyANxZSvERRWDlQRg6PHlAhWzzhh2Lu+siHAn4WDCu\njWm/tF7sRiRoXnI9UJekyFOKivZDoFEVVx+zGbe2W1XIKcvR/WMC53sIYNhC\nDjNrgV/QVzJjMpPHEos1HV8gLh+seRRtIGEsmUlq2LpEjcRTwO8J5BEFh280\nyVH5KnSLA692ZWKJ6OQhehF4VmHTGokVK+fjdDN2rlWyAEBb+q9054SewskE\nKe9iIIey7Y/EQOJIX4P5okXCiE64Di/r6qonoUWj4j+E5Ov0QXHNFf+6NDhO\n1LWIBFVRI2/V2VhYCMyzxmd8uVB/4xhwCkjMarEGrG3rQd5lkMLjR4nX69rT\noZR1L/M30fOjgZ0eFaFceMn5YbxW/WxScZsz6erimJ7LhaNj5wzUSlMbeTAi\nzTlfWm4nQauZ9LqR3mjHT0ejo0Y+yrAXxF5kDNBBYsx+22Ld5BbQ6cPrOMDi\nC8I1xcrbWBXLzYQw9vMqlSRb3v4waY1yB8d4efmKSMaiLZkPcp7s6O1cfTFe\nYp9Zgpf10FzH0fk3SvvI+nwP4+eDvAZJ3jDQkBGkzaujOotoH18b7qDZewqx\nUbYS\r\n=ReHf\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEYCIQC2zBIUPpOgbzfs8OzgkqYuNTBamJn9dwsskaSOOOJn0gIhAMlajB8hF0dvykX0SGTDzwV80kfDvtj7Fw5x4GvMOMz6"}]},"maintainers":[{"name":"cenkingunlugu","email":"cenkebret@gmail.com"}],"_npmUser":{"name":"cenkingunlugu","email":"cenkebret@gmail.com"},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/mysql-events_0.6.1_1586861781804_0.5389804512781495"},"_hasShrinkwrap":false}},"time":{"created":"2020-04-14T10:34:45.749Z","0.6.0":"2020-04-14T10:34:45.929Z","modified":"2022-04-04T22:19:07.364Z","0.6.1":"2020-04-14T10:56:21.941Z"},"maintainers":[{"name":"cenkingunlugu","email":"cenkebret@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/cenkingunlugu/mysql-events","keywords":["mysql","events","trigger","notify","watcher"],"repository":{"type":"git","url":"git+ssh://git@github.com/cenkingunlugu/mysql-events.git"},"contributors":[{"name":"Cenk Ebret","email":"cenkebret@gmail.com","url":"https://github.com/cenkingunlugu"}],"author":{"name":"Cenk Ebret"},"bugs":{"url":"https://github.com/cenkingunlugu/mysql-events/issues"},"license":"BSD-3-Clause","readme":"# mysql-events\n[![CircleCI](https://circleci.com/gh/cenkingunlugu/mysql-events.svg)](https://circleci.com/gh/cenkingunlugu/mysql-events)\n[![Code Climate](https://codeclimate.com/github/cenkingunlugu/mysql-events/badges/gpa.svg)](https://codeclimate.com/github/cenkingunlugu/mysql-events)\n[![Test Coverage](https://codeclimate.com/github/cenkingunlugu/mysql-events/badges/coverage.svg)](https://codeclimate.com/github/cenkingunlugu/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/cenkingunlugu/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 @cenkingunlugu/mysql-events\n```\n\n## Quick Start\n```javascript\nconst mysql = require('mysql');\nconst MySQLEvents = require('@cenkingunlugu/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/cenkingunlugu/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/cenkingunlugu/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/cenkingunlugu/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/cenkingunlugu/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/cenkingunlugu/mysql-events/blob/master/LICENSE) © Rodrigo Gomes da Silva\n","readmeFilename":"README.md"}