Comments (4)
Thanks for re-open, wasn't sure if to close or not. This would be a great enhancement! Given time I still would like to implement this enhancement, but maybe someone else can help.
The advantage of my solution is flexibility in building the INSERTS. The disadvantage is the field datatypes are unknown thus require creating strings of all data and INSERTing into a tmp table - ultimately SQL needs to update the production tables while converting data from the tmp table to the proper datatypes.
alasql would handle the datatypes properly, so the tmp table would not be required, and could directly INSERT INTO the production tables - which would be awesome!
from node-red-contrib-alasql.
That sounds very reasonable - thank you again for contributing!
from node-red-contrib-alasql.
I started implementing alasql node to output INSERT INTO commands, but then it dawned on me; after using an alasql node to select the data - wire it to a standard function node with the following code. (Plus the code can be easily modified for special INSERT INTO edge cases).
// Build sql 'insert into' commands from data in payload
msg.db = msg.db || {table: 'node_red_tmp'};
msg.db.fieldnames = []; msg.db.sqlcmds=[];
function isPlainObject(input){
return input && !Array.isArray(input) && typeof input === 'object';
}
// Create the insert commands for each object in payload
if (Array.isArray(msg.payload) && msg.payload.length && isPlainObject(msg.payload[0])) {
msg.db.fieldnames = Object.keys(msg.payload[0]);
msg.payload.forEach((rec) => {
var values = msg.db.fieldnames.map(name => '"' + rec[name] + '"');
msg.db.sqlcmds.push('INSERT INTO ' + msg.db.table + ' ' + msg.db.fieldnames.join(',') +
' VALUES (' + values.join(',') + ');');
});
}
return msg;
The results are in msg.db.sqlcmds, which then are pumped into a SQL database.
Export of node:
[{"id":"d29dc847.4a6038","type":"function","z":"1919d1c3.b05b5e","name":"Create SQL Insert Into","func":"// Build sql insert into commands from data in payload\nmsg.db = msg.db || {table: 'node_red_tmp'};\nmsg.db.fieldnames = []; msg.db.sqlcmds=[];\n\nfunction isPlainObject(input){\n return input && !Array.isArray(input) && typeof input === 'object';\n}\n// Create the insert commands for each obj in payload\nif (Array.isArray(msg.payload) && msg.payload.length && isPlainObject(msg.payload[0])) {\n msg.db.fieldnames = Object.keys(msg.payload[0]);\n msg.payload.forEach((rec) => {\n var values = msg.db.fieldnames.map(name => '\"' + rec[name] + '\"');\n msg.db.sqlcmds.push('INSERT INTO ' + msg.db.table + ' ' + msg.db.fieldnames.join(',') + \n ' VALUES (' + values.join(',') + ');');\n });\n}\nreturn msg;","outputs":1,"noerr":0,"x":370,"y":420,"wires":[["2040a1d.e19675e"]]}]
from node-red-contrib-alasql.
Sure, this will solve the problem.
I will reopen the issue and indicate it as a feature request so others might feel inspired to implement it.
from node-red-contrib-alasql.
Related Issues (20)
- UNION ALL problem HOT 2
- Alafile out - overwrites target file HOT 4
- Action required: Greenkeeper could not be activated 🚨
- Node crashes node-red HOT 6
- Array parameters passed via msg.payload to alasql node not working HOT 2
- node-red-contrib-alasql: JOIN DOESN'T WORK
- msg.topic doesn't work as predefined sql queries HOT 2
- msg.payload contains a object (alarms) and each alarm is a array element HOT 3
- Thrown errors crashes Node-RED HOT 4
- Using Buffer HOT 1
- Update npm package HOT 6
- Could you execute "npm publish" as v2.0.0 ? HOT 2
- Access NR global context inside function
- NULL value is not returned in payload HOT 1
- "TypeError: Cannot read property 'toJS' of undefined" HOT 2
- Trying to query direct on a CSV file doesn't work if using payload as the filename HOT 1
- SELECT statement does not work properly HOT 3
- Dependency Dashboard
- Proposed feature changes HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-red-contrib-alasql.