User Tools

Site Tools


sib:callers:mysqlcaller

mySQLCaller

Overview

The mySQL SIB Caller is a specific mySQL caller that performs operations on a mySQL database. The caller simply performs the [msg.sql] operation on the database and returns the result. It is recommended to generate the [msg.sql] field in the inputFormat, overwriting whatever value was received from the MQTT transport, for safety reasons. The [msg.sql] field can then be deleted in the outputFormat method if needed.

If the [msg.sql] contains ? placeholders the [msg.placeholder] must contain the array of values to substitute the placeholders with.

Details

Modulesib_mysqlcaller
Version1.3.6

Workflow

  • Subscribe: .custom.topic
    • on message:
      • custom.inputFormat
        • if msg.result is set
          • Respond with result from inputFormat
        • else
          • Perform operation on database
          • custom.outputFormat
          • Publish message on responseTopic with the result from the database operation

Custom configuration

mysqlcaller/custom.js
var mySQLCallerCustom = {
	_id: "mySQLSample:1",
	_name: "MySQLCallerSample",
	init: ( thisCaller )=>{},
	inputFormat: (topic, msg, cb, mysqlConnection)=>{
		msg.sql = "INSERT INTO test (test, num) VALUES ('a',1), ('b',2)";
		cb(msg);
	},
	loggerCfg: {
		dest: {
			file: false,
			console: true,
			mqtt: true
		}
	},
	mysql: {
		host: "localhost",
		user: "sibtest",
		password: "sibtest",
		database: "sibtest"
	},
	outputFormat: (topic, msg, cb, mysqlConnection)=>{
		delete( msg.sql );
		cb(msg);
	},
	mqtt: "mqtt://localhost",
	topic: "SIB/test/mySQLCaller"
};
 
module.exports = mySQLCallerCustom;
  • _id: The unique ID of this service
  • _name: A plaintext description of this service
  • init(thisCaller): If defined this method is executed when the service is starting up as the last step of initialization. thisCaller is a reference to the instance of the caller that does the method call.
  • inputFormat(topic, msg, callback, mysqlConnection): The method that is run on service activation. The original message is passed as the argument and the input value to the callback must be a JSON formatted message conforming to the SIB message standard. As of version 1.3.2 the mySQLCallers mysql connection is passed to the inputFormat. If msg.result is set in the returned message no further database actions will be taken by the caller.
  • loggerCfg: A JSON Object defining what logger actions should be active for this service (see Logger Configuration
  • mysql: Database configuration. The below fields are required but everything that can be passed to mysql.connect can be set here
    • host: The host of the database server
    • user: The user to log in as
    • password: The password for the user
    • database: The database to connect to
  • outputFormat(topic, msg, callback, mysqlConnection): This message is run on the response data before publishing to the response topic. The response message is passed as the argument and the input to the callback must be a JSON formatted message conforming to the SIB message standard.
  • mqtt: The MQTT connection string for the internal MQTT transport
  • topic: The MQTT topic to subscribe to on the internal MQTT transport
sib/callers/mysqlcaller.txt · Last modified: 2019/10/02 08:26 by hubbe