RethinkDB - Run query one after another - javascript

I am having trouble running multiple queries inside a single connection with RethinkDB. I have tried the r.do as seen in this question, however no success. I have also tried working with the conditional update queries. What I am looking to do is:
Open the connection.
Query to see if my field is there and if it is, perform some tasks.
Query to see if a counts field is there, subtract it by one.
What would be the best way to go about this? It seems I might be missing something?
r.connect(config.rethinkdb, function(err, conn) {
if (err) {
throw err;
}
else {
console.log('Connected.');
app.set('rethinkdb.conn', conn);
}
r.table('upcs').filter({AcceptedUPC:data}).run(conn, (err, cursor) => {
if (err) throw err;
console.log(data);
cursor.toArray((err,resu) => {
if (err) throw err;
//make a csv with some information
})
})
And in the same connection run
r.table('upcs').filter({AcceptedUPC:data}).filter(r.row.hasFields("UPCCount")).update({UPCCount: r.row("UPCCount").sub(1)}).run(conn, (err,cursor) => {
if (err) throw err;
});
Running this in NodeJS

I'm going to assume you are using this library for node.
You can that they actually allow you to do either callbacks or promises. I would recommend promises to avoid brackets of hell.
For promises you can use the bluebird library to make life easy.
You can do it by doing the following.
r.connect(config.rethinkdb).then(() => {
console.log("Connected");
app.set("rethinkdb.conn", conn);
return r.table('upcs').filter({AcceptedUPC:data}).run(conn);
}).then((cursor) => {
console.log(data); //maybe this should be cursor but I don't use rethinkDB
return cursor.toArray();
}).then((resu) => {
//make a csv with some information
}).catch((err) => {
throw err;
});

Related

How do I get the information from the database before sending a message

I'm currently making a discord bot with discord.js v13. Right now I have a database where the guild.id and the standard prefix of a server are stored in. Now I want to rewrite a command which gets triggered by the prefix and the name of the command, like this '!somecommand'. Currently my prefix is defined in the file with a const variable, but I want the bot to check the database for the prefix the server has, and use this one instead. I'm checking the prefix in the database with this part of code:
pool.getConnection(function(err, connection) {
if (err) throw err;
let sql = `SELECT * FROM custom_prefix WHERE guild_id = '${message.guild.id}'`;
connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(result[0].prefix)
connection.release();
});
});
The output is the current prefix of the server where the command was triggered, so far everything works fine.
But as I said I want the output of the code above to be the prefix with which the bot gets triggered.
I already tried to do it, but I'm always making a mistake.
Most of the time the bot is checking the database too slow and the result will be 'undefined'.
I dont know how I make the bot to wait for the result from the database, check if this result is really the prefix and then execute the command.
I am happy about any answer :-)
If you need any more information please let me know.
I'm guessing you did put the code that uses the result outside of the callback.
pool.getConnection(function(err, connection) {
if (err) throw err;
let sql = `SELECT * FROM custom_prefix WHERE guild_id = '${message.guild.id}'`;
connection.query(sql, async function (err, result) {
if (err) throw err;
console.log(result[0].prefix)
connection.release();
//
// Put your code that uses result[0].prefix here
//
});
});
//
// Do not put your code that uses result[0].prefix here
//

Working with Express and SQLite (connection and disconnection) Node.js

I have a question about the integration of SQLite (but in general of the DB) with Node.js with and Express framework.
From what I know, it is always good to open and close a connection to the db. But in Node.js, the processes are asynchronous (some times).
router.get("/", (req, res) => {
let db = new sqlLite.Database(path.resolve("data.db"), (err) => {
if (err) {
console.error(err.message);
}
console.log("Connected to the chinook database.");
});
db.get(`SELECT * FROM data WHERE key = '${req.query.data}'`, (err, row) => {
res.json(row);
});
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log("Close the database connection.");
});
});
in this example, I open and close the connection directly within the route, so that it only handles the event in that case. Is this approach correct? what do you recommend?
In most cases, the better approach is to use some kind of connection pool, but depending on your application that may or may not matter.
That said, two things that will matter. First, you really need to close the connection during the callback, not subsequent to the query call:
router.get("/", (req, res) => {
let db = new sqlLite.Database(path.resolve("data.db"), (err) => {
if (err) {
console.error(err.message);
}
console.log("Connected to the chinook database.");
});
db.get(`SELECT * FROM data WHERE key = '${req.query.data}'`, (err, row) => {
db.close((err) => {
if (err) {
console.error(err.message);
}
console.log("Close the database connection.");
});
res.json(row);
});
});
Second, though not related to your question, is that template strings are not safe from SQL injection attacks. So you really should not take direct user input from req.body or req.query or similar and concatenate it with your query, unless you're comfortable with a malicious user destroying your DB.
To avoid the injection problem, it's a good idea to both validate that the user input falls into an acceptable range of values but also use prepared statements to properly escape potentially malicious values.

Proper error handling in node using try catch

Im new to javascript programming and i am required to make a web app. Node.js will be used as the js runtime environment. In order to minimize the amount of time needed for debugging as the app develops I would like to implement a robust error handling scheme. However, due to my limited background I am not sure if what I am implementing is best practise or if it is even adequate. So any feedback will be accepted.
The function is asynchronous and will use catch to detect if any errors occurred while operating. A try-catch statement will be used to catch any errors. This was done in order to allow for individual error identification from the functions. My aim is to propagate the errors up to the calling function that will handle it in the highest level catch statement (in my case where it is logged *this will change eventually). Any feedback?
create: async function(req, res) {
let data = JSON.parse(req.body.data);
let result;
let request_body;
let sub_networks_lora;
try {
request_body = sub_network_api_request_data(data, 1);
result = await lora_app_server.create_applications(request_body)
.catch(err => {
//Error updating application on lora app server
throw err;
});
await db_sub_networks.create_sub_network(result.data.id, data.sub_network_name, data.network_id)
.catch(err => {
throw err;
//Error creating sub network in database
});
sub_networks_lora = await get_sub_networks()
.catch(err => {
throw err;
//Error getting networks from lora app server
})
sub_networks_lora = JSON.stringify(sub_networks_lora);
res.status(200).send({
sub_networks_lora
});
} catch (err) {
console.log(err)
} finally {}
}

nodejs/pg, callback parameters

I am fairly new to node.js, and haven't done much of javascripts. Tried to search my problem, but couldn't find specific answer related to it.
So, while I was working on attaching the PostgreSQL to my app, I followed a snippet from some example on web, and it seems like working pretty well.
Anyways I wanted to understand how it works, I had a problem understanding specific part of the following code:
module.exports = {
query: function(text, values, cb) {
pool.connect(function(err, client, done) {
if(err) {
return console.error('error fetching client from pool', err);
}
console.log(client);
client.query(text, values, function(err, result) {
done();
cb(err, result);
})
});
}
}
and the specific part is:
pool.connect(function(err, client, done) { ... }
What I understood is connect function takes callback function with err, client, and done as parameter, however I couldn't understand from where the function(err, client, done) is passed to connect function as parameter. By where, I mean an object or a caller that call connect function.
I had suspected that it would be handled internally, but I wanted to know clearly.
Bydefault all callback function, the first parameter must be an error and second will be a result of ur callback function.
Done is similar to callback keyword, which says, your task is over and give response back from where the function has called, its just like return statement in normal function
example:
function callbackDemo(arg1, arg2, callback) {
if (condition)
callback(null, "Success");
else
callback("error");
}
callbackDemo(1, 2, function(err, result){
if(!err)
console(result);
else
console.log(err);
});

How do I call MySql procedures from Node JS

I want to call a stored procedure from MySql node:
How do I call it? The documentation says:
You can call stored procedures from your queries as with any other mysql driver. If the stored procedure produces several result sets, they are exposed to you the same way as the results for multiple statement queries
I tried searching for it on internet but got very old results which do not work anymore.
I tried:
connection.query('procedure_name()', {84,Bhuwan}, function(err, result) {
connection.destroy();
if (err)
throw err;
callback(err, result);
});
But I am getting error.
Can anyone provide a proper syntax for it??
You have to use 'call' command and if you have parameters to pass to the query, you need to add '?' marks. Check the code.
connection.query("call procedure_name(?,?)", [param1, param2], function (err, result) {
if (err) {
console.log("err:", err);
} else {
console.log("results:", result);
}
});

Categories

Resources