node.js looping database inserts - javascript

I have an application, with an address object, and a list of people associated with the application.
{
field1: value,
field2: value,
address: {
street: value,
apt: value,
city: value
},
owners: [
{name: value, etc.},
{name: value, etc.}
]
}
I am passing the owners off to a function that loops over the owners and passes the information for each query off to an async function:
async function insertAllOwners(appId, owners) {
for (var i = 0, len = owners.length; i < len; i++) {
console.log("insert owner", i);
await insertOwner(appId, owners[i]);
}
}
The function being called here turns the query into a promise:
function insertOwner(appId, owner) {
let sqlOwner = 'insert into ...';
return new Promise( ( resolve, reject ) => {
mySqlClient.query(sqlOwner, [appId, owner.memberType ...], (err, rows) => {
if ( err )
return reject( err );
console.log("rows:", rows);
resolve( rows );
} );
} );
}
The address insert is nested inside the application insert, then the code that calls the owner insert loop is nested inside the address insert. The looping owner insert function is called here:
if(app.owners) {
/* insert owners, then commit. else, just commit.*/
insertAllOwners(appId, app.owners).then( result => {
mySqlClient.commit(function(err, result) {
if (err) {
console.error("ERROR DURING COMMIT:", err);
mySqlClient.rollback(function() {
throw err;
});
}
else {
console.log("commit:",result);
}
});
});
}
The output looks perfect, but it doesn't appear to actually commit anything (the new rows are not showing up). Any suggestions? Output of logging is below:
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 0,
serverStatus: 1,
warningCount: 1,
message: '',
protocol41: true,
changedRows: 0
}
address result:
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 35,
serverStatus: 1,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
insert owner 0
rows:
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 70,
serverStatus: 1,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
...
insert owner 4
rows:
OkPacket {
fieldCount: 0,
affectedRows: 1,
insertId: 74,
serverStatus: 1,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
commit:
OkPacket {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 0,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}

The first thing is that , this is not a good practice to execute a query.
what you are doing to run a for loop and each loop execute each query. Bu this way you give load to mysql. And also it will take longer time execute your API.
I suggest this way.
In for loop :
for (var i = 0, len = owners.length; i < len; i++) {
inserData.push(owners[i]); // Add all data in to array ...
}
After that execute query once with all data.
connection.query("INSERT INTO TABLE (columns) VLAUES ? " , insertData)...

Related

How to Decode logs data of transaction?

I want decode logs data, I am using web3.utils.hexToAscii() method which return junk data like !3cÀ Token create101 insted of actual data 2000 Token create 101.
> {
transactionHash: '0x0d2a00b4d684efbdea26677468830d88b2145577a4b086775ebdb5189ed78d27',
transactionIndex: 0,
blockHash: '0x702e444fba974d17a9b8e881c8002e721c0517ea254991884d2ef745b7510126',
blockNumber: 2,
from: '0x21003381639c90b61ef0b1ad692078c4ee3d0a7f',
to: '0x42eb248f2dc51d6dbc13e92aed4be07291373b90',
gasUsed: 26567,
cumulativeGasUsed: 26567,
contractAddress: null,
logs: [
> {
logIndex: 0,
transactionIndex: 0,
transactionHash: '0x0d2a00b4d684efbdea26677468830d88b2145577a4b086775ebdb5189ed78d27',
blockHash: '0x702e444fba974d17a9b8e881c8002e721c0517ea254991884d2ef745b7510126',
blockNumber: 2,
address: '0x42EB248f2DC51D6dBc13e92Aed4bE07291373B90',
data: '0x00000000000000000000000021003381639c90b61ef0b1ad692078c4ee3d0a7f00000000000000000000000000000000000000000000000000000000000000c8000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000c546f6b656e20637265617465000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000033130310000000000000000000000000000000000000000000000000000000000',
topics: [Array],
type: 'mined',
id: 'log_985c3fd6'
> }
> ],
status: true,
logsBloom: '0x00000000000000000000000000000000000000800000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000'
> }
I have tried below code but its return junk data.
function checkHex(){
var block = web3.eth.getBlock('latest')
block.then(block => {
for(let txHash of block.transactions){
let tx = web3.eth.getTransaction(txHash);
tx.then(tx => {
let receipt = web3.eth.getTransactionReceipt(tx.hash);
receipt.then(receipt => {
for(let logs of receipt.logs){
const decoded = web3.utils.hexToAscii(logs.data);
console.log(decoded);
> }
> });
> });
> }
> });
> }
return
!3cÀ Token create101
expected
2000 Token create 101

Javascript Subtract between two arrays of objects

Hi can anybody help me to implement the right/clean way of subtracting between two arrays of object. My case (backend) is that I fetch Products data from mongodb, then I also have Trolley data that is fetched from MySql, what I'm trying to do is that if product stock is subtracted by quantity in trolley & the result is lower then 0 then I will throw error. Right know my Implementation as below:
const trolleyProducts = await Trolley.findAll({
where: {
userId,
isActive: true,
},
attributes: ["id", "productId", "quantity", "notes"],
});
const products = await ProductModel.find(
{
dbId: trolleyProductIds,
},
{
_id: 0,
sku: 0,
barcode: 0,
reservedStock: 0,
sold: 0,
tags: 0,
infos: 0,
photosURL: 0,
}
);
// ******* here is my implementation *******
products.map((product) => {
trolleyProducts.map((trolley) => {
if (product.dbId === trolley.productId) {
if (product.stock - trolley.quantity < 0) {
throw {
name: "Bad Request",
message: " Stock is less than desired quantity",
};
}
}
});
});
// **************
Please let me know if there are better & cleaner approach then mine (for performance matter). Thanks :)
You can convert trolleyProducts to an object whose keys are the product IDs. That way you won't need a nested loop to find the matching product.
Also, map() should be used when the callback function returns a value and you're making an array of those values. Use forEach() if the loop is for side effect only.
const trolleyProducts = await Trolley.findAll({
where: {
userId,
isActive: true,
},
attributes: ["id", "productId", "quantity", "notes"],
});
// convert array to object.
trolleyProducts = Object.fromEntries(trolleyProducts.map(obj => [obj.productId, obj]));
const products = await ProductModel.find({
dbId: trolleyProductIds,
}, {
_id: 0,
sku: 0,
barcode: 0,
reservedStock: 0,
sold: 0,
tags: 0,
infos: 0,
photosURL: 0,
});
products.forEach((product) => {
const trolley = trolleyProducts[product.dbId]
if (trolley && product.stock - trolley.quantity < 0) {
throw {
name: "Bad Request",
message: `Stock is less than desired quantity for ${product.dbId}`,
};
}
});

How to change the value of a field in an array mongodb nodejs

There are many array levels, that's why I can't choose the right field, I guess.
This is the Schema of the model:
{
season: 1,
finished: 0,
Games: [{
Game: [{
game: 1,
Players: [{
Id: 0,
name: "Peter",
jails: 3, //I want to change this value
pays: 1000,
gets: 2000,
points: 3
}]
}]
}]
}
I tried this query but it's not working:
Seasons.update({
season: 1,
game: 2,
Id: 0
}, {
jails: 4
},
(err, data) => {
if (err) {
console.log(err)
} else {
console.log(data)
}
}
)
I have used the $set propery but it didn't work as I want. It make changes all jails field of all players to same value in the same season. I mean, the query is selecting the most parent record, so it is the season record. But I want to change value of child element.

Generate a new array with count of property values

I have an array in my state :
projects: [
{ title: 'todo 1', person: 'Sam', status: 'ongoing'},
{ title: 'project', person: 'Jack', status: 'complete' },
{ title: 'Design video', person: 'Tim', status: 'complete' },
{ title: 'Create a forum', person: 'Jade', status: 'overdue' },
{ title: 'application', person: 'Jade', status: 'ongoing'},],
From this array (projects), I would like to generate a new array with Javascript and to get this result :
totalByPersonAndStatus : [
{person : 'Sam', complete: 0, ongoing: 1, overdue: 0 },
{person : 'Jack', complete: 1, ongoing: 0, overdue: 0 },
{person : 'Tim', complete: 1, ongoing: 0, overdue: 0 },
{person : 'Jade', complete: 0, ongoing: 1, overdue: 1 },]
I tried it
totalProjectsByPersonAndStatus: state => {
state.projects.forEach(name => {
state. totalByPersonAndStatus["name"] = name.person;
});
return state. totalByPersonAndStatus;
The problem, if a make a console.log(this.totalByPersonAndStatus) I have an object with only the data of projects.name [name: "Jade", __ob__: Observer]
Can you help me ?
Thank you
You can use reduce
let projects =[{title:'todo1',person:'Sam',status:'ongoing'},{title:'project',person:'Jack',status:'complete'},{title:'Designvideo',person:'Tim',status:'complete'},{title:'Createaforum',person:'Jade',status:'overdue'},{title:'application',person:'Jade',status:'ongoing'},]
let desired = projects.reduce((output,{person,status}) => {
if( output[person] ){
output[person][status]++
} else {
output[person] = {
person,
complete: Number(status==='complete'),
ongoing: Number(status==='ongoing'),
overdue: Number(status==='overdue')
}
}
return output;
},{})
console.log(Object.values(desired))
Create a new Set for people and statuses by iterating through the projects, a set has only unique values so sets are a convenience, iterate through your people set creating a new object with all the statuses initialized to 0, then iterate over the projects to increment the various statuses that apply. This method allows any number of new statuses to be added without changing the code - dynamic.
var people = new Set();
var status = new Set();
projects.forEach((p)=>{
people.add(p.person);
status.add(p.status);
});
var totalByPersonAndStatus = [];
people.forEach((person)=>{
let peeps = { "person": person };
status.forEach((stat)=>{
peeps[stat] = 0;
});
projects.forEach((project)=>{
if (project.person === person) { peeps[project.status]++; }
});
totalByPersonAndStatus.push(peeps);
});
You could use reduce and destructuring like this:
const projects=[{title:'todo 1',person:'Sam',status:'ongoing'},{title:'project',person:'Jack',status:'complete'},{title:'Design video',person:'Tim',status:'complete'},{title:'Create a forum',person:'Jade',status:'overdue'},{title:'application',person:'Jade',status:'ongoing'}]
const merged = projects.reduce((acc,{person,status})=>{
acc[person] = acc[person] || { person, ongoing:0, complete:0, overdue:0}
acc[person][status]++;
return acc;
},{})
console.log(Object.values(merged))
The goal is create an object merged with each person as key and then increment based on the statuses:
{
"Sam": {
"person": "Sam",
"ongoing": 1,
"complete": 0,
"overdue": 0
},
"Jack": {
}
...
}
Then use Object.values, to get the final array.
You could make it a one-liner:
const projects=[{title:'todo 1',person:'Sam',status:'ongoing'},{title:'project',person:'Jack',status:'complete'},{title:'Design video',person:'Tim',status:'complete'},{title:'Create a forum',person:'Jade',status:'overdue'},{title:'application',person:'Jade',status:'ongoing'}],
output = Object.values(projects.reduce((a,{person,status})=>
((a[person] = a[person] || {person,ongoing:0,complete:0,overdue:0})[status]++,a),{}))
console.log(output)

Ordering the JSON data with JavaScript

I'm looking way to order data wich is coming from my JSON Provider:
[{"Username":"Mehmet","UserID":2,"OkeyTablePlayerChairNumber":1},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":2},
{"Username":"Erçin","UserID":1,"OkeyTablePlayerChairNumber":3},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":4},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":5},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":6},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":7},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":8},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":9}]
There is two more important thing for ordering:
OkeyTablePlayerChairNumber
By UserID First; let me explain:
I want to order them firstly by OkeyTableChairNumber; this is done by Server Side already. Data coming ordered by OkeyTablePlayerChairNumber ASC...
Now the bull,
I would like to order them by UserID but; for example if I took UserID == 1 it should order them like: UserID == 1 field, will be come to first field. and upper objects will removed and added to end of the list...
For view:
UserID == 1
[{"Username":"Erçin","UserID":1,"OkeyTablePlayerChairNumber":3},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":4},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":5},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":6},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":7},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":8},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":9},
{"Username":"Mehmet","UserID":2,"OkeyTablePlayerChairNumber":1},
{"Username":null,"UserID":0,"OkeyTablePlayerChairNumber":2}]
C# Solution I made before; But I need JS solutions:
int yourSitPositionIndex = playersOnTheTableWithEmpytPositions.ToList().FindIndex(x => x.UserID == userID);
var beforePlayers = playersOnTheTableWithEmpytPositions.ToList().GetRange(0, yourSitPositionIndex);
IEnumerable<tbl_Okey_TablePlayer> afterPlayers = playersOnTheTableWithEmpytPositions.Except(beforePlayers);
IEnumerable<tbl_Okey_TablePlayer> newPositions = afterPlayers.Concat(beforePlayers);
Conversion of your C# solution:
var wantedId = 1;
var index = 0, result;
//int yourSitPositionIndex = playersOnTheTableWithEmpytPositions.ToList().FindIndex(x => x.UserID == userID);
while (index < data.length && data[index].UserID != wantedId) index += 1;
if (index < data.length) {
//var beforePlayers = playersOnTheTableWithEmpytPositions.ToList().GetRange(0, yourSitPositionIndex);
var beforePlayers = data.slice(0, index);
//IEnumerable<tbl_Okey_TablePlayer> afterPlayers = playersOnTheTableWithEmpytPositions.Except(beforePlayers);
var afterPlayers = data.slice(index);
//IEnumerable<tbl_Okey_TablePlayer> newPositions = afterPlayers.Concat(beforePlayers);
result = afterPlayers.concat(beforePlayers);
} else {
result = data;
}
There is no FindIndex in the core of javascript, so I did a while loop to find the index
I use .slice() to replace GetRange
Rest of the code should be clear enough. The all code could be simplified with libraries like underscore
Example fiddle
If the goal is to order by UserID DESC, OkeyTable... ASC
You can define a customer sort function:
In action: http://repl.it/XZm
var json = [/*all your data */]
function idSort(a,b){
if(a["UserID"] !== b["UserID"]){
return b["UserID"] - a["UserID"];
} else {
return a["OkeyTablePlayerChairNumber"] - b["OkeyTablePlayerChairNumber"];
}
}
var sorted = json.sort(idSort);
Results:
[ { Username: 'Mehmet', UserID: 2, OkeyTablePlayerChairNumber: 1 },
{ Username: 'Erçin', UserID: 1, OkeyTablePlayerChairNumber: 3 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 2 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 4 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 5 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 6 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 7 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 8 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 9 } ]
Here is UserID ASC, OkeyTable...ASC:
function idSort(a,b){
var notZero = (a["UserID"] > 0 && b["UserID"] > 0);
var notEqual = (a["UserID"] !== b["UserID"]);
if(notZero && notEqual){
return a["UserID"] - b["UserID"];
} else {
return a["OkeyTablePlayerChairNumber"] - b["OkeyTablePlayerChairNumber"];
}
}
var sorted = json.sort(idSort);
Results:
[ { Username: 'Erçin', UserID: 1, OkeyTablePlayerChairNumber: 3 },
{ Username: 'Mehmet', UserID: 2, OkeyTablePlayerChairNumber: 1 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 2 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 4 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 5 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 6 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 7 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 8 },
{ Username: null, UserID: 0, OkeyTablePlayerChairNumber: 9 } ]
In action: http://repl.it/XZm/1

Categories

Resources