Skip to content

Support Array of values for bulk inserts #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,19 @@ msg.topic = 'SELECT * FROM `users` WHERE `name` = :name AND `age` > :age;';
msg.payload = { name: 'Adrien', age: 30 };
return msg;
```
If you have multiple arguments, put them in an array of objects in the `payload` variable.
You will get the result in the `payload` output variable.

Example:

```javascript
msg.topic = 'INSERT INTO `users` (`name`, `age`) VALUES( :name, :age) ;';
msg.payload = [
{ name: 'Adrien', age: 30 },
{ name: 'Maria', age: 62 }
]
return msg;
```
> Avoid SQL injections!!
>
> Do not NEVER EVER put variables content in `topic` directly!
Expand All @@ -54,4 +66,4 @@ You have to connect using TLS encryption. Simply check the box in this node conf

You are probably using another node than `node-red-contrib-stackhero-mysql` and tryin to connect to a MySQL >= 8 server using "Caching SHA2 password" authentication method.

To resolve that issue, simply use this node `node-red-contrib-stackhero-mysql`.
To resolve that issue, simply use this node `node-red-contrib-stackhero-mysql`.
76 changes: 59 additions & 17 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,68 @@ module.exports = (RED) => {
return;
}

const isAnObject = value => value === Object(value) && !Array.isArray(value);
const isAnObject = (value) => value === Object(value);
if (msg.payload !== undefined && !isAnObject(msg.payload)) {
this.error('msg.payload should be an object containing the query arguments.');
this.error(
"msg.payload should be an object or an array containing the query arguments."
);
return;
}

try {
const [ result ] = await this.serverConfig
.query(
msg.topic,
msg.payload
);
this.setState('queryDone');
msg.payload = result;
this.send(msg);
}
catch (error) {
this.error(error, msg);
this.setState('error', error.toString());
}
let values = [];
if (!(msg.payload != undefined && Array.isArray(msg.payload)))
values = [msg.payload];
else values = msg.payload;
let thisNode = this;
this.serverConfig.pool.getConnection(function (err, connection) {
if (err) {
if(connection)
connection.rollback(function () {
connection.release();
});
return
}
connection.beginTransaction(function (err) {
let promisses = [];
msg.payload = values.forEach((value) => {
promisses.push(
new Promise(function (resolve, reject) {
connection.query(
msg.topic,
value,

function (error, results, _fields) {
if (error)
reject( error)
else resolve(results);
}
);
})
);
});
Promise.all(promisses).then((results) => {
connection
.commit(function (error) {
if (error) {
connection.rollback(function () {
connection.release();
//Failure
thisNode.error(error);
thisNode.setState("error", error.toString());
});
} else {
connection.release();
msg.payload = results;
thisNode.setState("queryDone");
thisNode.send(msg);
}
})

}).catch((error) => {
thisNode.error(error);
thisNode.setState("error", error.toString());
});;
});
});
});

this.on('close', async () => {
Expand Down