Synchronous SQL queries in AngularJS - sql

I have a 1.x AngularJS application, and need to perform 2 SQL queries synchronously (one after the other). The first does an update to a table, the second does a select against the same table to get the updated values. The problem I am encountering is that the queries are executed asynchronously, and occassionally the select is performed before the update completes, and I get back the "before" data rather than the "after" data. I put in a 500ms delay, and that works fine for me, but offshore folks with slower connections still encounter the problem intermittently. My code is below (simplified to make my point). Any thoughts as to how I can get the second query to wait until the first is complete?
$scope.saveUser = function(origuid, uid, name) {
$http.get('/ccrdb/updateAdmin?origuid=' + origuid + '&uid=' + uid + '&name=' + name);
setTimeout(function() {
$http.get('/ccrdb/getAdmins').then(function(response) {
$scope.users = response.data.detail;
});
return true;
}, 500);
};

$http returns promises that resolve when the request is done, so do:
$scope.saveUser = function(origuid, uid, name) {
$http.get('/ccrdb/updateAdmin?origuid=' + origuid + '&uid=' + uid + '&name=' + name)
.then(function() {
return $http.get('/ccrdb/getAdmins');
})
.then(function(response) {
$scope.users = response.data.detail;
});
};

Related

How to ignore DUPLICATE ENTRY error when updating multiple records at once using TypeORM

I am trying to update hundreds of database records using the TypeORM library. Problem is that sometimes DUPLICATE ERR is returned from SQL when the bulk upload is performed and stops the whole operation. Is possible to set up TypeORM in a way so duplicate entries are ignored and the insert is performed?
The table is using two primary keys:
This is my insert command (TypeORM + Nestjs):
public async saveBulk(historicalPrices: IHistoricalPrice[]) {
if (!historicalPrices.length) {
return;
}
const repoPrices = historicalPrices.map((p) => this.historicalPricesRepository.create(p));
await this.historicalPricesRepository.save(repoPrices, { chunk: 200 });
}
Thanks in advance
You will have to use InsertQueryBuilder to save the entities instead of repository.save method. InsertQueryBuilder will allow you to call an additional method orIgnore() which will add IGNORE literal into your mysql INSERT statement. From mysql official doc:
When INSERT IGNORE is used, the insert operation fails silently for rows containing the unmatched value, but inserts rows that are matched.
One demerit is obviously that you'll have to now chunk the rows on your own. InsertQueryBuilder doesn't provide any options to chunk the entities. Your code should look like this:
for (let i = 0; i < historicalPrices.length; i += 200) {
const chunk = historicalPrices.slice(i, i + 200);
const targetEntity = this.historicalPricesRepository.target;
await this.historicalPricesRepository
.createQueryBuilder()
.insert()
.into(targetEntity)
.values(chunk)
.orIgnore()
.execute();
}

API returns array of JSON for only one result

Currently working on an API which given an address returns information about is. Some of the rows in our tables are duplicates, however being as there is over 15 million I cant go and find the duplicates. Instead I have opted to use
var query = `SELECT TOP 1 * from my_TABLE where..conditions`;
This ensures that only one row of the duplicates are returned.
The problem is when this is sent back as a JSON it comes as an array with one object.
In the Server.js file
// create Request object
var request = new sql.Request();
// query to the database
request.query(query, function (err, result) {
if (err) {
console.log("Error while querying database :- " + err);
res.send(err);
}
else {
res.send(result)
}
Returns this:
[{
Address:'our info'
}]
is there a way to have it respond with
{
Address:'our info'
}
Because from DB you've get list of object anyway, even there is only 1 item.
It works as you expected when you try to return json with the first element of your array.

Nodejs localhost keeps loading my function

Good morning to everyone,
Please I would be so grateful if you could provide a little help for me. I am already stuck for long time with this issue.
I have a function which does not stop loading my localhost after this function is triggered. I did not figure out so far how to fix it.
Any help would be perfect.
This is my code:
// Copy scene
router.post('/copy', function(req,res,call) {
if( req.param('scene') !== undefined ){
db.serialize(function () {
db.run("CREATE TABLE temp_table as SELECT * FROM scene where id=?", req.param('scene'));
db.run("UPDATE temp_table SET id = NULL, user_id = (SELECT id FROM users WHERE email =?)",GLOBAL.email);
db.run("INSERT INTO scene SELECT * FROM temp_table");
db.run("DROP TABLE temp_table");
if(error) {
console.log(error);
}
});
db.close();
}
});
Thank you so much in advance
Whenever browser sends any request to a server it expects a response. if it doesn't get any response it will be stuck with the timeout.
You need to send the response to terminate the request if you are not doing next execution with callback call or if you are expecting next manipulation then replace res.send() to call(error parameter,success parameter).
router.post('/copy', function(req, res, call) {
if (req.param('scene') !== undefined) {
db.serialize(function() {
db.run("CREATE TABLE temp_table as SELECT * FROM scene where id=?", req.param('scene'));
db.run("UPDATE temp_table SET id = NULL, user_id = (SELECT id FROM users WHERE email =?)", GLOBAL.email);
db.run("INSERT INTO scene SELECT * FROM temp_table");
db.run("DROP TABLE temp_table");
if (error) {
console.log(error);
res.send(error);//send response if error
//or call(error);
}
res.send({message:'success'});//send response if success
//or call(null,whatever you want to pass)
});
db.close();
}
});
You must either call the callback call() to pass on control to next middleware, or render and end the response using res.end() once your middleware logic is done.
Please see: https://expressjs.com/en/guide/writing-middleware.html

Rally Analytics set the startindex for a query

I have a query for the Rally Analytics which returns a data set larger than the pagesize. So I want to do another query to return the remainder data set. I tried setting a startindex value but that does not work, StartIndex stays at 0.
this.query = {
find:Ext.encode(requestedQuery.find),
StartIndex:20000,
pagesize:20000 //MAX_PAGESIZE
};
_queryAnalyticsApi:function () {
Ext.Ajax.request({
url:"https://rally1.rallydev.com/analytics/1.27/" + this.workspace + "/artifact/snapshot/query.js?" + Ext.Object.toQueryString(this.query) +
"&fields=" + JSON.stringify(this.requestedFields) + "&sort={_ValidFrom:1}",
method:"GET",
//need to change this to a POST
success:function (response) {
this._afterQueryReturned(JSON.parse(response.responseText));
},
scope:this
});
},
that works, it was confusing because the attribute of the result set is called StartIndex. It would be nice if the granularity (i.e. day, week) could be defined and handled on the server first, so it wouldn't have to return such a large dataset.
The parameter you'll want to use is called start. Also, on subsequent pages it is important to include a filter using the ETLDate returned from the first page of data so your results are consistent in time. We have created a SnapshotStore in the AppSDK 2.0 that handles all this complexity for you. Look for it soon!

websql use select in to get rows from an array

in websql we can request a certain row like this:
tx.executeSql('SELECT * FROM tblSettings where id = ?', [id], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
however, I know regular SQL and figured i should be able to request
var arr = [1, 2, 3];
tx.executeSql('SELECT * FROM tblSettings where id in (?)', [arr], function(tx, rs){
// do stuff with the resultset.
},
function errorHandler(tx, e){
// do something upon error.
console.warn('SQL Error: ', e);
});
but that gives us no results, the result is always empty. if i would remove the [arr] into arr, then the sql would get a variable amount of parameters, so i figured it should be [arr]. otherwise it would require us to add a dynamic amount of question marks (as many as there are id's in the array).
so can anyone see what i'm doing wrong?
aparently, there is no other solution, than to manually add a question mark for every item in your array.
this is actually in the specs on w3.org
var q = "";
for each (var i in labels)
q += (q == "" ? "" : ", ") + "?";
// later to be used as such:
t.executeSql('SELECT id FROM docs WHERE label IN (' + q + ')', labels, function (t, d) {
// do stuff with result...
});
more info here: http://www.w3.org/TR/webdatabase/#introduction (at the end of the introduction)
however, at the moment i created a helper function that creates such a string for me
might be better than the above, might not, i haven't done any performance testing.
this is what i use now
var createParamString = function(arr){
return _(arr).map(function(){ return "?"; }).join(',');
}
// when called like this:
createparamString([1,2,3,4,5]); // >> returns ?,?,?,?,?
this however makes use of the underscore.js library we have in our project.
Good answer. It was interesting to read an explanation in the official documentation.
I see this question was answered in 2012. I tried it in Google 37 exactly as it is recommened and this is what I got.
Data on input: (I outlined them with the black pencil)
Chrome complains:
So it accepts as many question signs as many input parameters are given. (Let us pay attention that although array is passed it's treated as one parameter)
Eventually I came up to this solution:
var activeItemIds = [1,2,3];
var q = "";
for (var i=0; i< activeItemIds.length; i++) {
q += '"' + activeItemIds[i] + '", ';
}
q= q.substring(0, q.length - 2);
var query = 'SELECT "id" FROM "products" WHERE "id" IN (' + q + ')';
_db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results1) {
console.log(results1);
debugger;
}, function (a, b) {
console.warn(a);
console.warn(b);
})
})