I have a array of data something like
var records = [
{Name: '', Id: 1},
{Name: '', Id: 2},
{Name: '', Id: 3},
{Name: '', Id: 4},
{Name: '', Id: 5},
{Name: '', Id: 6}
];
there could be thousands of items inside records array...
Ques1: Can we create a stored procedure which will accept an array of objects in mysql?
Ques2: Is there a way to bulk insert this data into mysql with Node JS?
You can bulk insert the array of records ,but before that you might need to convert it into array of arrays
I use array reduce to get an array something like this
let j=[
{Name: '', Id: 1},
{Name: '', Id: 2},
{Name: '', Id: 3},
{Name: '', Id: 4},
{Name: '', Id: 5},
{Name: '', Id: 6}
];
let values=j.reduce((o,a)=>{
let ini=[];
ini.push(a.Name);
ini.push(a.Id);
o.push(ini);
return o
},[])
console.log(values);
This will output
[["",1],["",2],["",3],["",4],["",5],["",6]]
Now inserting into the mysql database
1-Using normal callback
const con=require('./mysql.js'); //mysql connectionin mysql.js
var sql = "INSERT INTO customers (name, id) VALUES ?";
con.query(sql, [values], function (err, result) { //pass values array (from above) directly here
if (err) throw err;
console.log("Number of records inserted: " + result.affectedRows);
});
});
so the format of multiple data insert should be like [[[a,b],[b,c],[d,k]]]
2-Using promises
var Promise = require("bluebird");//for promises
const promisecon=Promise.promisifyAll(require('./mysql.js'));//
var sql = "INSERT INTO customers (name, id) VALUES ?";
promisecon.queryAsync(sql,[values]).then((result)=>{//bluebird identifies with Async
console.log(result);
}).catch(function(err){
console.log(err);
})
3-Using async await
var sql = "INSERT INTO customers (name, id) VALUES ?";
async function build() {
try {
const result =await con.queryAsync(sql,[values]);
console.log(result);
} catch (err) {
// do something
}
}
build();
Related
I've been trying for hours and I can't get it to work 100% correctly.
I've using RTK Query with a backend connection to a mysql server.
The issue is that I get all result and not from a specific query, no matter what param I use.
Let me explain:
in the backend (connection is working perfectly) part I have:
app.get("/test", (req, res) => {
const q = 'select * from portfolios'
db.query(q, (err, data) => {
if(err) {return res.json(err)}
return res.json(data)
})
})
in the API-part, I have:
const testerApi = createApi({
reducerPath: 'tester',
baseQuery: fetchBaseQuery({
baseUrl: "http://localhost:8800"
}),
endpoints(builder) {
return {
fetchTesting: builder.query({
query: (name) => {
return {
url: '/test',
params: {
user_id: name.id
},
method: 'GET',
};
},
}),
};
},
});
To call this, I use:
const name = {id:6, na:'iets'};
const results = useFetchTestingQuery(name);
but as a result, I get all content from the mysql test-table, it doesnt filter out the user-id.
So, the result always is something like this:
0
:
{id: 1, user_id: 1, name: 'aaaaaa'}
1
:
{id: 2, user_id: 1, name: 'bbbb'}
2
:
{id: 3, user_id: 2, name: 'cccc'}
3
:
{id: 9, user_id: 3, name: 'dddddd'}
...
So, it is partially working, but how can I get a filtered result?
This might sound a stupid question, but I am a beginner and I've been searching for hours to find a working solution.
I hope someone can help me a little bit ...
Thx,
Peter
tried many things, such as changing the bacnekd side to
app.get("/test/:user_id")
const q = 'select * from portfolios where user_id = ?'
db.query(q, [user_id], (err, data) => {...
and many more but nothing seems to work.
hello I have a nodejs project, I am using sequelize and mysql2 to access the database, in one of its routes I show an ejs view and I want to send a variable to it so that different products/cards are dynamically loaded from a form.
now when I use the findAll() method to call the different mysql records I try to bring an array of objects but it returns the loose objects so in the ejs view I cannot iterate with a foreach() for example
I execute this from a home route
i try this
products_dataBase.findAll()
.then(products => {
products.forEach(element => {
let result = []
result.push( element.dataValues)
res.render('home.ejs', { result });
console.log(result)
});
})
.catch(err => {
console.log(err)
})
[
{
id: 14,
title: 'asdasd',
price: 33434,
description: 'sdasd',
createdAt: 2022-11-08T02:06:12.000Z,
updatedAt: 2022-11-08T02:06:12.000Z
}
]
[
{
id: 15,
title: 'asodfsd',
price: 232,
description: 'asdasd',
createdAt: 2022-11-08T03:17:20.000Z,
updatedAt: 2022-11-08T03:17:20.000Z
}
]
[
{
id: 16,
title: 'ldfsksdf',
price: 343434,
description: 'efsddsff',
createdAt: 2022-11-08T03:17:44.000Z,
updatedAt: 2022-11-08T03:17:44.000Z
}
]
res.render must be executed only once per request, but you invoke it repeatedly, in a forEach loop. Try this:
products_dataBase.findAll()
.then(products => {
let result = [];
products.forEach(element => {
result.push(element.dataValues);
});
res.render('home.ejs', { result });
console.log(result);
})
.catch(err => {
console.log(err);
});
I have a problem where I have an array of objects like this:
[
{
department_id: '6256f8ae6f749617e8167416',
employee_id: '6253ca0c6f749618a8d022af',
employee_number: '1234'
},
{
department_id: '6256f8ae6f749617e8167416',
employee_id_id: '6253ca0c6f749618a8d022af',
employee_number: '1503'
}
]
and would like to use node js and mysql to insert it into a database so I have got this script
Department.assignEmployeetoDepartment = (employees, result) => {
let employees_array = Object.values(rooms);
db.query(
`INSERT INTO department_employee (department_id, employee_id, employee_number) VALUES ?`,
[employees_array],
(err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
console.log("success: ", res);
result(null, res);
}
);
};
when I use the code above, I get
INSERT INTO department_employee (department_id, employee_id, employee_number) VALUES '[object Object]', '[object Object]'
which does not work for obvious reasons.
I tried to stringify the object
and I also tried to use a for loop to iterate over the employees array and it did not work as it says the headers were already sent.
How could I store the array (which can vary in length into the database?
Thanks in advance
The solution I'm suggesting for you will serve you safe if you make your data an array of arrays;
const employeesData = [
[
'6256f8ae6f749617e8167416',
'6253ca0c6f749618a8d022af',
'1234'
],
[
'6256f8ae6f749617e8167416',
'6253ca0c6f749618a8d022af',
'1503'
],
];
Next, prepare your query like this;
const sql = `INSERT INTO images (department_id, employee_id, employee_number) VALUES?`;
You're now ready to run your query liike below;
db
.query(sql, [employeesData])
.then(rows => {
res.status(200).json({
status: "success",
message:"Data inserted successfully"
});
})
.catch(err => {
res.status(500).json({
status: "error",
error: err.message
});
});
You can generate array of values to insert using array's map method like shown below,
employeesData = dataSourceArray.map ( (data) => {
return [
department_id,
employee_id,
employee_number
];
});
As per mysql- Escaping query values
Nested arrays are turned into grouped lists (for bulk inserts), e.g. [['a', 'b'], ['c', 'd']] turns into ('a', 'b'), ('c', 'd')
You probably want to map the values to array, you can't pass array of JSON object to query
db.query(
`INSERT INTO department_employee (department_id, employee_id, employee_number) VALUES ?`,
[employees_array.map(employee => [employee.department_id, employee.employee_id, employee.employee_number])],
(err, res) => {
if (err) {
console.log("error: ", err);
result(err, null);
return;
}
console.log("success: ", res);
result(null, res);
}
);
};
I have a Typescript class in which I collect a JSON Object with some empty parameters, what I want is to filter that object to obtain another Object but without the empty data of the first one.
This is my JSON object:
{ Country: 'Colombia',
Ser: 'ok',
Ins: '',
BBDD: 'ok',
Mid: '',
Ata: '',
'Branch Ser': 'ok',
Service: '' }
This is what I want to achieve:
{ Country: 'Colombia',
Ser: 'ok',
BBDD: 'ok',
'Branch Ser': 'ok' }
const allData = {
Country: 'Colombia',
Ser: 'ok',
Ins: '',
BBDD: 'ok',
Mid: '',
Ata: '',
'Branch Ser': 'ok',
Service: ''
};
const filteredData = Object.entries(allData).reduce((x, [k, v]) => {
if (v) { // not ( null, undefined, empty string)
x[k] = v;
}
return x;
}, {} as any);
console.log(filteredData);
You can use Object.entries function to list object as key value pairs and then use the Array.reduce to filter out the falsy values.
const allData = {
Country: 'Colombia',
Ser: 'ok',
Ins: '',
BBDD: 'ok',
Mid: '',
Ata: '',
'Branch Ser': 'ok',
Service: ''
};
const filteredData = Object.fromEntries(Object.entries(allData).filter(([_, value]) => value));
console.log(filteredData);
Above is a simple one-line solution to do it. Using JS' Object.fromEntries() really helps.
I understood how to make a dynamic insert based on what properties has an object:
let job_seeker = {
username: 'j.smith',
user_type: 'job_seeker',
name: 'john smith',
email: 'j.smith#example.com',
resume: true,
resume_date_time: '2109-01-01',
salary_expectation: 100000
};
let employer = {
username: 'b.burke',
user_type: 'employer',
name: 'betty burke',
email: 'b.burke#example.com',
company_name: 'Acme Inc.',
company_profile: 'http://acme.example.com/about_us/'
};
connection.query('INSERT INTO users SET ?', job_seeker, function(error, results, fields) {
if (error) throw error;
console.log(results.insertId);
});
connection.query('INSERT INTO users SET ?', employer, function(error, results, fields) {
if (error) throw error;
console.log(results.insertId);
});
But, what if I have an object like:
user_employers = [
{
username: 'b.burke',
user_type: 'employer',
name: 'betty burke',
company_profile: 'http://acme.example.com/about_us/'
},
{
username: 'b.burke',
user_type: 'job_seeker',
name: 'john smith',
email: 'j.smith#example.com',
resume: true,
resume_date_time: '2109-01-01',
salary_expectation: 100000
},
// and so on, with dynamic properties
]
And i have to make a multiple insert or update with the object user_employers? I have to do a loop of INSERT/UPDATE query, or is there a better way?