Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

mysql-live-select

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mysql-live-select

Live updating MySQL SELECT statements

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
increased by50%
Maintainers
1
Weekly downloads
 
Created
Source

mysql-live-select Build Status

NPM Package to provide events when a MySQL select statement result set changes.

Built using the zongji Binlog Tailer and node-mysql projects.

This package has been tested to work in MySQL 5.1, 5.5, 5.6, and 5.7. Expected support is all MySQL server version >= 5.1.15.

Installation

  • Add the package to your project:

    $ npm install mysql-live-select
    
  • Enable MySQL binlog in my.cnf, restart MySQL server after making the changes.

    # Must be unique integer from 1-2^32
    server-id        = 1
    # Row format required for ZongJi
    binlog_format    = row
    # Directory must exist. This path works for Linux. Other OS may require
    #   different path.
    log_bin          = /var/log/mysql/mysql-bin.log
    
    binlog_do_db     = employees   # Optional, limit which databases to log
    expire_logs_days = 10          # Optional, purge old logs
    max_binlog_size  = 100M        # Optional, limit log size
    
  • Create an account, then grant replication privileges:

    GRANT REPLICATION SLAVE, REPLICATION CLIENT, SELECT ON *.* TO 'user'@'localhost'
    

LiveMysql Constructor

The LiveMysql constructor makes 3 connections to your MySQL database:

  • Connection for executing SELECT queries (exposed node-mysql instance as db property)
  • Replication slave connection
  • information_schema connection for column information
Arguments
ArgumentTypeDescription
settingsobjectAn object defining the settings. In addition to the node-mysql connection settings, the additional settings below are available.
callbackfunctionDeprecated: callback on connection success/failure. Accepts one argument, error. See information below about events emitted.
Additional Settings
SettingTypeDescription
serverIdintegerUnique number (1 - 232) to identify this replication slave instance. Must be specified if running more than one instance.
Default: 1
minIntervalintegerPass a number of milliseconds to use as the minimum between result set updates. Omit to refresh results on every update. May be changed at runtime.
checkConditionWhenQueuedbooleanSet to true to call the condition function of a query on every binlog row change event. By default (when undefined or false), the condition function will not be called again when a query is already queued to be refreshed. Enabling this can be useful if external caching of row changes.
Events Emitted

Use .on(...) to handle the following event types.

Event NameArgumentsDescription
errorErrorAn error has occurred.
readyNoneThe database connection is ready.
Quick Start
// Example:
var liveConnection = new LiveMysql(settings);
var table = 'players';
var id = 11;

liveConnection.select(function(esc, escId){
  return (
    'select * from ' + escId(table) +
    'where `id`=' + esc(id)
  );
}, [ {
  table: table,
  condition: function(row, newRow){
    // Only refresh the results when the row matching the specified id is
    // changed.
    return row.id === id
      // On UPDATE queries, newRow must be checked as well
      || (newRow && newRow.id === id);
  }
} ]).on('update', function(diff, data){
  // diff contains an object describing the difference since the previous update
  // data contains an array of rows of the new result set
  console.log(data);
});

See example.js for full source...

LiveMysql.prototype.select(query, triggers)

ArgumentTypeDescription
querystring or functionSELECT SQL statement. See note below about passing function.
triggers[object]Array of objects defining which row changes to update result set

Returns LiveMysqlSelect object

Function as query

A function may be passed as the query argument that accepts two arguments.

  • The first argument, esc is a function that escapes values in the query.
  • The second argument, escId is a function that escapes identifiers in the query.
Trigger options
NameTypeDescription
tablestringName of table (required)
databasestringName of database (optional)
Default: database setting specified on connection
conditionfunctionEvaluate row values (optional)
Condition Function

A condition function accepts up to three arguments:

Argument NameDescription
rowTable row data
newRowNew row data (only available on UPDATE queries, null for others)
rowDeletedExtra argument for aid in external caching: true on DELETE queries, false on INSERT queries, null on UPDATE queries.

Return true when the row data meets the condition to update the result set.

LiveMysql.prototype.pause()

Temporarily skip processing of updates from the binary log.

LiveMysql.prototype.resume()

Begin processing updates after pause(). All active live select instances will be refreshed upon resume.

LiveMysql.prototype.end()

Close connections and stop checking for updates.

LiveMysql.applyDiff(data, diff)

Exposed statically on the LiveMysql object is a function for applying a diff given in an update event to an array of rows given in the data argument.

LiveMysqlSelect object

Each call to the select() method on a LiveMysql object, returns a LiveMysqlSelect object with the following methods:

Method NameArgumentsDescription
on, addListenerevent, handlerAdd an event handler to the result set. See the following section for a list of the available event names.
updatecallbackUpdate the result set. Callback function accepts error, rows arguments. Events will be emitted.
stopNoneStop receiving updates
activeNoneReturn true if ready to recieve updates, false if stop() method has been called.

As well as all of the other methods available on EventEmitter...

Available Events

Event NameArgumentsDescription
updatediff, dataFirst argument contains an object describing the difference since the previous update event with added, removed, moved, and copied rows. Second argument contains complete result set array.
errorerrorUnhandled errors will be thrown

Running Tests

Tests must be run with a properly configured MySQL server. Configure test settings in test/settings/mysql.js.

Execute Nodeunit using the npm test command.

License

MIT

Keywords

FAQs

Package last updated on 28 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc