7.19.134 • Published 1 year ago

@firanorg/non-praesentium-sint v7.19.134

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year 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

1 year ago

7.18.134

1 year ago

6.17.126

1 year ago

6.17.125

1 year ago

6.17.124

1 year ago

6.17.123

1 year ago

6.17.122

1 year ago

6.17.121

1 year ago

6.17.117

1 year ago

6.17.119

1 year ago

6.17.118

1 year ago

6.17.120

1 year ago

7.17.126

1 year ago

7.18.132

1 year ago

7.18.133

1 year ago

7.18.130

1 year ago

7.18.131

1 year ago

7.18.129

1 year ago

7.18.126

1 year ago

7.18.127

1 year ago

7.18.128

1 year ago

6.17.116

1 year ago

6.17.115

1 year ago

5.17.114

1 year ago

5.17.115

1 year ago

5.17.113

1 year ago

5.17.110

1 year ago

5.17.111

1 year ago

5.17.112

1 year ago

5.16.109

1 year ago

5.16.107

1 year ago

5.16.108

1 year ago

5.16.110

1 year ago

5.16.106

1 year ago

5.16.105

1 year ago

5.16.104

1 year ago

5.16.103

1 year ago

5.16.101

1 year ago

5.16.102

1 year ago

5.16.100

1 year ago

5.16.98

1 year ago

5.16.99

1 year ago

5.16.97

1 year ago

5.16.96

1 year ago

5.16.95

1 year ago

5.16.94

1 year ago

5.16.93

1 year ago

5.16.92

1 year ago

5.16.91

1 year ago

5.16.90

1 year ago

5.16.89

1 year 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

1 year ago

2.12.46

1 year ago

1.4.20

2 years ago

1.4.22

2 years ago

1.4.21

2 years ago

1.4.24

2 years ago

1.4.23

2 years ago

1.4.26

2 years ago

1.4.25

2 years ago

2.8.38

1 year ago

1.4.28

2 years ago

2.8.37

1 year ago

1.4.27

2 years ago

3.14.60

1 year ago

3.14.61

1 year ago

3.14.62

1 year ago

2.10.45

1 year ago

3.14.63

1 year ago

2.10.46

1 year ago

2.6.37

1 year ago

4.15.72

1 year ago

2.6.36

1 year ago

4.15.73

1 year ago

5.16.87

1 year 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

1 year ago

2.9.38

1 year ago

1.4.17

2 years ago

2.9.39

1 year ago

1.4.16

2 years ago

1.4.19

2 years ago

1.4.18

2 years 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

1 year ago

4.14.71

1 year ago

2.7.37

1 year ago

4.14.72

1 year ago

4.14.67

1 year ago

4.14.68

1 year ago

4.14.65

1 year ago

4.14.66

1 year ago

4.14.69

1 year ago

3.12.54

1 year ago

3.12.55

1 year ago

4.16.80

1 year ago

4.16.82

1 year ago

4.16.81

1 year ago

4.16.84

1 year ago

4.16.83

1 year ago

4.16.86

1 year ago

4.14.63

1 year ago

4.16.85

1 year ago

4.14.64

1 year ago

4.16.87

1 year ago

4.16.79

1 year ago

4.16.78

1 year ago

4.16.73

1 year ago

4.16.75

1 year ago

1.5.29

1 year ago

4.16.74

1 year ago

1.5.28

1 year ago

4.16.77

1 year ago

4.16.76

1 year ago

1.3.16

2 years ago

1.3.13

2 years ago

1.3.14

2 years ago

1.2.13

2 years ago

1.3.15

2 years ago

1.2.12

2 years ago

1.2.11

2 years ago

1.2.10

2 years ago

1.2.9

2 years ago

1.1.9

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.1.6

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.1.3

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago