7.19.134 • Published 9 months ago

@firanorg/non-praesentium-sint v7.19.134

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

mysql-events

CircleCI Code Climate Test Coverage

A node.js package that watches a MySQL database and runs callbacks on matched events.

This package is based on the original ZongJi and the original mysql-events modules. Please make sure that you meet the requirements described at ZongJi, like MySQL binlog etc.

Check @kuroski's mysql-events-ui for a mysql-events UI implementation.

Install

npm install @firanorg/non-praesentium-sint

Quick Start

const mysql = require('mysql');
const MySQLEvents = require('@firanorg/non-praesentium-sint');

const program = async () => {
  const connection = mysql.createConnection({
    host: 'localhost',
    user: 'root',
    password: 'root',
  });

  const instance = new MySQLEvents(connection, {
    startAtEnd: true,
    excludedSchemas: {
      mysql: true,
    },
  });

  await instance.start();

  instance.addTrigger({
    name: 'TEST',
    expression: '*',
    statement: MySQLEvents.STATEMENTS.ALL,
    onEvent: (event) => { // You will receive the events here
      console.log(event);
    },
  });
  
  instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, console.error);
  instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, console.error);
};

program()
  .then(() => console.log('Waiting for database events...'))
  .catch(console.error);

Check the examples

Usage

#constructor(connection, options)

  • Instantiate and create a database connection using a DSN

    const dsn = {
      host: 'localhost',
      user: 'username',
      password: 'password',
    };
    
    const myInstance = new MySQLEvents(dsn, { /* ZongJi options */ });
  • Instantiate and create a database connection using a preexisting connection

    const connection = mysql.createConnection({
      host: 'localhost',
      user: 'username',
      password: 'password',
    });
    
    const myInstance = new MySQLEvents(connection, { /* ZongJi options */ });
  • Options(the second argument) is for ZongJi options

    const myInstance = new MySQLEvents({ /* connection */ }, {
      serverId: 3,
      startAtEnd: true,
    });

    See more about ZongJi options

    #start()

  • start function ensures that MySQL is connected and ZongJi is running before resolving its promise

    myInstance.start()
      .then(() => console.log('I\'m running!'))
      .catch(err => console.error('Something bad happened', err));

    #stop()

  • stop function terminates MySQL connection and stops ZongJi before resolving its promise
    myInstance.stop()
      .then(() => console.log('I\'m stopped!'))
      .catch(err => console.error('Something bad happened', err));

    #pause()

  • 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
    myInstance.pause();

    #resume()

  • resume function resumes a paused MySQL connection, so it starts to generate binlog events again
    myInstance.resume();

    #addTrigger({ name, expression, statement, onEvent })

  • Adds a trigger for the given expression/statement and calls the onEvent function when the event happens
    instance.addTrigger({
      name: 'MY_TRIGGER',
      expression: 'MY_SCHEMA.MY_TABLE.MY_COLUMN',
      statement: MySQLEvents.STATEMENTS.INSERT,
      onEvent: async (event) => {
        // Here you will get the events for the given expression/statement.
        // This could be an async function.
        await doSomething(event);
      },
    });
  • The name argument must be unique for each expression/statement, it will be user later if you want to remove a trigger

    instance.addTrigger({
      name: 'MY_TRIGGER',
      expression: 'MY_SCHEMA.*',
      statement: MySQLEvents.STATEMENTS.ALL,
      ...
    });
    
    instance.removeTrigger({
      name: 'MY_TRIGGER',
      expression: 'MY_SCHEMA.*',
      statement: MySQLEvents.STATEMENTS.ALL,
    });
  • The expression argument is very dynamic, you can replace any step by * to make it wait for any schema, table or column events

    instance.addTrigger({
      name: 'Name updates from table USERS at SCHEMA2',
      expression: 'SCHEMA2.USERS.name',
      ...
    });
    instance.addTrigger({
      name: 'All database events',
      expression: '*',
      ...
    });
    instance.addTrigger({
      name: 'All events from SCHEMA2',
      expression: 'SCHEMA2.*',
      ...
    });
    instance.addTrigger({
      name: 'All database events for table USERS',
      expression: '*.USERS',
      ...
    });
  • The statement argument indicates in which database operation an event should be triggered
    instance.addTrigger({
      ...
      statement: MySQLEvents.STATEMENTS.ALL,
      ...
    });
    Allowed statements
  • The onEvent argument is a function where the trigger events should be threated
    instance.addTrigger({
      ...
      onEvent: (event) => {
        console.log(event); // { type, schema, table, affectedRows: [], affectedColumns: [], timestamp, }
      },
      ...
    });

    #removeTrigger({ name, expression, statement })

  • Removes a trigger from the current instance
    instance.removeTrigger({
      name: 'My previous created trigger',
      expression: '',
      statement: MySQLEvents.STATEMENTS.INSERT,
    });

    Instance events

  • MySQLEvents class emits some events related to its MySQL connection and ZongJi instance
    instance.on(MySQLEvents.EVENTS.CONNECTION_ERROR, (err) => console.log('Connection error', err));
    instance.on(MySQLEvents.EVENTS.ZONGJI_ERROR, (err) => console.log('ZongJi error', err));
    Available events

Tigger event object

It has the following structure:

{
  type: 'INSERT | UPDATE | DELETE',
  schema: 'SCHEMA_NAME',
  table: 'TABLE_NAME',
  affectedRows: [{
    before: {
      column1: 'A',
      column2: 'B',
      column3: 'C',
      ...
    },
    after: {
      column1: 'D',
      column2: 'E',
      column3: 'F',
      ...
    },
  }],
  affectedColumns: [
    'column1',
    'column2',
    'column3',
  ],
  timestamp: 1530645380029,
  nextPosition: 1343,
  binlogName: 'bin.001',
}

Make sure the database user has the privilege to read the binlog on database that you want to watch on.

LICENSE

BSD-3-Clause © Rodrigo Gomes da Silva

avaarrayswatchinglanguagesignalstouch$.extendhandlersECMAScript 2018valuesdependency managerlinewrapgetajaxgradients css3webenventriesprocessnameprotocol-bufferscloudtrailfastifyguidwordbreakconcatgetoptES5css nestingtypesutilitiesratereducerassertES7regular expressionargscharacterdescriptorcachetapmkdirmkdirscallboundasyncvestpipemetadatamime-dbl10nobjectoperating-systemsqses8phoneecmascripteveryconfiges5ECMAScript 6package.jsonArray.prototype.containsprettysharedarraybuffersetterawaitswftextconsolebatchpackage managerformattingtypedaccessibilityreducedirfpsJSON-SchemaArrayBuffer.prototype.sliceieECMAScript 2015hookformwhatwgvariablesWeakMapdiffenvironmentregularpreprocessorless.jsESnextqscodesflataccessorclasseslengthsorthttpspluginreadablestablefindquoteRegExp#flagsArray.prototype.includesbyteLengthcompile lesssinatraSymbol.toStringTaguuidtypedarraysPushprivate dataprogresspositiveStyleSheetairbnbparsingpropertiesdom-testing-libraryperformantrfc4122loggerlooktaskjQueryclassnamescolourcolumnsauthenticationoptimizergroupByform-validationpackageReactiveXlogtraverseemrbrowsereslintconfigboundspinnerperformancenegative zerolastrouteryamlinterruptsconfigurableextensioncss variablegetOwnPropertyDescriptorcloudformationcode pointsiamstyleswalkingmatchtestingtoolspredictablestringttyes2015lesscssexecReactiveExtensionslimitedyupHyBidefineworkspace:*watchFiledependencieszerohashchannelfinduptapecopypreserve-symlinksautoprefixerserializationshebangObject.entriessymbolcall-boundES2016deepclonevalidationsortedxtermajvhigher-orderes-shim APIWeakSetspeeddynamodbcryptstyled-componentsstructuredClonerequiredategrouprmmatchAllreact-hookselasticacheglobbannerbrowserslistequalsequencestyle@@toStringTageventEmitterdeep-cloneargumentreduxlintponyfillwindowsrestgenericsBigUint64Arraysyntaxerrornumberjasminereactnegativepushlivebufferslook-upES2023internalroutebootstrap lessidleUint32ArrayPromiseurljapanesetoArraysetImmediatedeepcopyeslintframeworkCSSefficientupinspectreal-timecss-in-jscontainspersistentwidthqueueMicrotaskvalidatepolyfillansivariables in cssextrarapidcomparepnpm90fast-copygettermochaYAMLwriteendertyped arrayjsonpathtoStringTagruntimepicomatchschemaclassnametrimRightflagsmimefile systemformati18ndataerrorES2020Object.isvisualtrimLeftio-tsshamratelimitawesomesauceoffsetES2017es-shimsdeletehasOwnPropertytermpromisesencryptionproxyObject.fromEntrieseast-asian-widthrandomrmdirECMAScript 2022filemkdirpflagcorskinesisJSONcommandinstallarraybufferpackageshooksdropsnsestreefastclonedayjsfseventsObservableloadbalancingprunerdsstreams2npmformsless cssfunctionswaitpromisecomputed-typescallinputpatchwgetterminalwatchwalkvalides7typesafeglacierArrayBuffertoobjectcolumnmiddlewarekeymoduleES2022scheme-validationsideArray.prototype.findLastarrayInt16ArrayECMAScript 2023Int32ArraytddsesredactenumerableconsumeparentsserializedotenvRxECMAScript 2020safejson.envforEachObject.valuesfind-upmodulesmulti-packagekeyses2018linkcensorsignalObservablesvaluethroatjsmimetypesbundlingbootstrap cssamazonbufferdatastructureroutingauthbluebirdmapreducenodeUnderscorefast-deep-copyprotobufindicatordeep-copyRFC-6455Microsofthelpersslotreusewrapes-abstractutil.inspectfigletelbcircularhardlinksxhragentreadablestreamcjkes2017restfulsliceschemeapi_.extendMaptypanionECMAScript 2019inmakecloudsearchlinuxsyntaxArraytsimportstdlibomitassertionreact-testing-libraryStreamsESmruArray.prototype.flattenelmescapesigtermwhichcompilerprivatelockfilearktypeInt8Arrayfixed-widthsymbolspathbyteOffsetmergeregular expressionsObject.definePropertyfull-widthnamesArrayBuffer#sliceiteratechaiuninstallsameValueZerocore-jshttpstatefast-deep-clonerequestArray.prototype.filterBigInt64ArrayObject.getPrototypeOfchromeiteratorformtc39karmaminimalstringifierreplayFunction.prototype.namemixinsnativeshellmobilesuperstructec2telephonesettingsIteratorcollectionworkerappjsxdescriptorsnested cssiterationsearchmake dirsymlinkCSSStyleDeclarationString.prototype.matchAllesUint8ClampedArrayES2021fscallbindwatcherECMAScript 7postcssrm -frhotObject.keysparserES8isConcatSpreadableeventsinvariantgetPrototypeOfendpointopenstarteroptimistequalitytrimchromiumflattenstreamcloudfronttypescriptwafserializeres2016statusobjchineseECMAScript 2016busyFloat64ArraypyyamlES2015WebSocketscallbackweakmapunicodefunctionshrinkwraptypeofebstypedarrayArray.prototype.flatMapfindLastIndexbcryptredux-toolkitcurlfoldertslibrangeerrorES6spinnersvpcES2019expressionshimless compilerECMAScript 2017Seteslint-pluginURLargvUint16Arrayjestmapstylingreact-hook-formObject.assigndescriptionStreameslintpluginastAsyncIteratoridutilitydebugsetPrototypeOfprotoarttestertakeemojiURLSearchParamsasciidom-0queuematchesexit-codeincludesbddpasswordtrimEndimmutablewordwrapbyteReflect.getPrototypeOfhas-ownjavascriptcss lessnopeirqhasreaddirectorycheckArray.prototype.findLastIndexFloat32ArrayconcatMapdataviewES3propautoscalingless mixinsargparse__proto__collection.es6colorssomeassignassertsroute53colorUint8Arrayconcurrencycligetintrinsics3dataViewstatelesslistenersES2018loggingtrimStartECMAScript 5sigintTypedArraybrowserlistthrottleviewqueryinternal slotweaksetjsdomspecstoragegatewayWebSocketextendtimecloneonceduplexcreatesimpledbeventDispatcherpostcss-pluginprefixstyleguidemoveownfetchSymbolnodejsbreakmacosa11yconnecthasOwnfluxutilregexelectronArray.prototype.flatwritablesymlinksTypeScriptlibphonenumberbundlerfastlazyinstallerimportexportlimitcryptoresolveloadingECMAScript 3typeerrorfast-clonefullwidthremoveexitdebuggerTypeBoxwarningjoicommand-linecharactersObjectRxJS256definePropertysharedrobustgdprtypetoSortedcommanderoptionECMAScript 2021awsmomentflatMapfastcopydeterministiccloudwatchcoerciblerecursiveRegExp.prototype.flagsprototypeString.prototype.trimoutputbindrm -rfclass-validatorquerystringes6superagentregexpintrinsicfromisemitbeanstalktostringtagcoreworkflowparentfull
7.19.134

9 months ago

7.18.134

9 months ago

6.17.126

10 months ago

6.17.125

10 months ago

6.17.124

10 months ago

6.17.123

10 months ago

6.17.122

10 months ago

6.17.121

10 months ago

6.17.117

10 months ago

6.17.119

10 months ago

6.17.118

10 months ago

6.17.120

10 months ago

7.17.126

9 months ago

7.18.132

9 months ago

7.18.133

9 months ago

7.18.130

9 months ago

7.18.131

9 months ago

7.18.129

9 months ago

7.18.126

9 months ago

7.18.127

9 months ago

7.18.128

9 months ago

6.17.116

10 months ago

6.17.115

10 months ago

5.17.114

10 months ago

5.17.115

10 months ago

5.17.113

10 months ago

5.17.110

10 months ago

5.17.111

10 months ago

5.17.112

10 months ago

5.16.109

10 months ago

5.16.107

10 months ago

5.16.108

10 months ago

5.16.110

10 months ago

5.16.106

10 months ago

5.16.105

10 months ago

5.16.104

10 months ago

5.16.103

10 months ago

5.16.101

10 months ago

5.16.102

10 months ago

5.16.100

10 months ago

5.16.98

11 months ago

5.16.99

10 months ago

5.16.97

11 months ago

5.16.96

11 months ago

5.16.95

11 months ago

5.16.94

11 months ago

5.16.93

11 months ago

5.16.92

11 months ago

5.16.91

11 months ago

5.16.90

11 months ago

5.16.89

11 months ago

2.12.49

1 year ago

2.12.48

1 year ago

3.14.58

1 year ago

2.12.47

1 year ago

3.14.59

12 months ago

2.12.46

1 year ago

1.4.20

1 year ago

1.4.22

1 year ago

1.4.21

1 year ago

1.4.24

1 year ago

1.4.23

1 year ago

1.4.26

1 year ago

1.4.25

1 year ago

2.8.38

1 year ago

1.4.28

1 year ago

2.8.37

1 year ago

1.4.27

1 year ago

3.14.60

12 months ago

3.14.61

12 months ago

3.14.62

12 months ago

2.10.45

1 year ago

3.14.63

12 months ago

2.10.46

1 year ago

2.6.37

1 year ago

4.15.72

12 months ago

2.6.36

1 year ago

4.15.73

12 months ago

5.16.87

11 months ago

2.12.54

1 year ago

2.12.53

1 year ago

2.12.52

1 year ago

2.12.51

1 year ago

2.12.50

1 year ago

1.6.35

1 year ago

1.6.36

1 year ago

5.16.88

11 months ago

2.9.38

1 year ago

1.4.17

1 year ago

2.9.39

1 year ago

1.4.16

1 year ago

1.4.19

1 year ago

1.4.18

1 year ago

2.9.41

1 year ago

2.9.42

1 year ago

2.9.40

1 year ago

1.5.30

1 year ago

2.11.46

1 year ago

1.5.32

1 year ago

1.5.31

1 year ago

1.5.34

1 year ago

1.5.33

1 year ago

2.9.45

1 year ago

1.5.35

1 year ago

2.9.43

1 year ago

2.9.44

1 year ago

3.13.55

1 year ago

3.13.56

1 year ago

3.13.57

1 year ago

3.13.58

1 year ago

4.14.70

12 months ago

4.14.71

12 months ago

2.7.37

1 year ago

4.14.72

12 months ago

4.14.67

12 months ago

4.14.68

12 months ago

4.14.65

12 months ago

4.14.66

12 months ago

4.14.69

12 months ago

3.12.54

1 year ago

3.12.55

1 year ago

4.16.80

11 months ago

4.16.82

11 months ago

4.16.81

11 months ago

4.16.84

11 months ago

4.16.83

11 months ago

4.16.86

11 months ago

4.14.63

12 months ago

4.16.85

11 months ago

4.14.64

12 months ago

4.16.87

11 months ago

4.16.79

11 months ago

4.16.78

11 months ago

4.16.73

11 months ago

4.16.75

11 months ago

1.5.29

1 year ago

4.16.74

11 months ago

1.5.28

1 year ago

4.16.77

11 months ago

4.16.76

11 months ago

1.3.16

1 year ago

1.3.13

1 year ago

1.3.14

1 year ago

1.2.13

1 year ago

1.3.15

1 year ago

1.2.12

1 year ago

1.2.11

1 year ago

1.2.10

1 year ago

1.2.9

1 year ago

1.1.9

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago