What is the use of "useMaster: true" in sequelize(nodejs)? - node.js

So I was going through a project codebase(nodejs 10) and they are using sequelize for executing queries in Mysql and at some places they are using useMaster: true.
I am just wondering what is it there for? Anyone having any idea?
Example code:
[error , coupons ] = await to(#wagner.get('sequelize').query( query , { useMaster: true, replacements : [coupon_offset,parseInt(limit)] ,type: #wagner.get('sequelize').QueryTypes.SELECT } ))

Didi you check the official documentation?
It says about useMaster in the Params section of query instance method:
Force the query to use the write pool, regardless of the query type.
See query method
I'm not sure if it's applicable to MySQL but still I'd recommend to find out more about write pools.

Related

Can we log a SQL query having bind parameters in node-oracledb?

const query = `INSERT INTO countries VALUES (:country_id, :country_name)`;
try {
const result = await connection.execute(query, { country_id: 90, country_name: "Tonga" });
} catch (error) {
console.error(`error while executing: ${query}`);
}
Is there any way to log the query along with the bind parameters data
so that I can log INSERT INTO countries VALUES (90, "Tonga")
I think there's currently no builtin option to do that, but according to the docs you could create a wrapper around the execute function and log the actual query there. From the docs:
Sometimes it is useful to trace the bind data values that have been
used when executing statements. Several methods are available.
In the Oracle Database, the view V$SQL_BIND_CAPTURE can capture bind
information. Tracing with Oracle Database’s
DBMS_MONITOR.SESSION_TRACE_ENABLE() may also be useful.
You can also write your own wrapper around execute() and log any
parameters.
Eventually, I found a package known as bind-sql-string which has queryBindToString method that solves my problem. 🎉

How to chain express-validator based on query values?

I am trying to find a solution to chain conditions based on the query values passed to the route.
Task1:
// if value for query A = noIdNeeeded, then i do not need to search for a second queryB
/endpoint?queryA=noIdNeeded
Task2:
// if value for query A = idNeeded, then i need to ensure second queryB exists
/endpoint?queryA=idNeeded&queryB=SomeId
I am having trouble with the writing parameter for Task 2.
For task 1 i have use this logic and works fine [query('page').exists().notEmpty().isIn(seoPageTypes),]
So far I have seen there is an if clause that we can use probably (link) however implementing this has been a challenge due to lack of examples and no prior experience.
If anyone can guide on how to do this correctly or any hint is much appreciated.
Make sure you have a new version of express-validator installed.
The following should do your job.
query('queryA').exists().notEmpty().isIn(seoPageTypes),
query('queryB')
.if(query('queryA').equals('idNeeded'))
.exists().notEmpty().withMessage('queryB must exist'),
Another approach is to use a custom validator
query('queryA').exists().notEmpty().isIn(seoPageTypes)
.custom((value, {req}) => {
if (value === "idNeeded" && !req.query.queryB) {
throw new Error('queryB must exist');
}
return true;
}),
Use what suits you more :)

Alias of an object key using COSMOSDB sql query

I am working with Cosmos DB and I want to write a SQL query that returns different name of an key in document object.
To elaborate, imagine you have the following document in one container having "makeName" key in "make" object.
{
"vehicleDetailId":"38CBEAF7-5858-4EED-8978-E220D2BA745E",
"type":"Vehicle",
"vehicleDetail":{
"make":{
"Id":"B57ADAAD-C16E-44F9-A05B-AAB3BF7068B9",
"makeName":"BMW"
}
}
}
I want to write a query to display "vehicleMake" key in place of "makeName".
How to give alias name in the nested object property.
Output should be like below
{
"vehicleDetailId":"38CBEAF7-5858-4EED-8978-E220D2BA745E",
"type":"Vehicle",
"vehicleDetail":{
"make":{
"Id":"B57ADAAD-C16E-44F9-A05B-AAB3BF7068B9",
"vehicleMake":"BMW"
}
}
}
I have no idea how to query in Cosmosdb to get the above result.
Aliases for properties are similar to the way you'd create a column alias in SQL Server, with the as keyword. In your example, it would be:
SELECT c.vehicleDetail.make.makeName as vehicleMake
FROM c
This would return:
[
{
"vehicleMake": "BMW"
}
]
Try this:
SELECT c.vehicleDetailId, c.type,
{"make":{"Id":c.vehicleDetail.make.Id, "vehicleMake":c.vehicleDetail.make.makeName}} as vehicleDetail
FROM c
It uses the aliasing described in the following documentation. All of the aliasing examples I could find in the documentation or blog posts only show a single level of json output, but it happens that you can nest an object (make) within an object (vehichleDetail) to get the behavior you want.
https://learn.microsoft.com/en-us/azure/cosmos-db/sql-query-aliasing

Sequelize.js - build SQL query without executing it

Is there a way to get Sequelize to build a query with the replacements (so I'll be able to use their SQL injection cleanup) and just get the raw SQL query, without executing it?
You can just call the QueryGenerator with the type of query you want to generate.
For example a selectQuery:
const sql = MyModel.QueryGenerator.selectQuery(
MyModel.getTableName(), {
where: {
someAttribute: "value"
},
attributes: ["other", "attributes"]
},
MyModel);
There's also insertQuery, updateQuery, deleteQuery too.
It looks like this feature isn't implemented yet, but there are some users trying to push the issue forward.
See github issue.
it's undocumented, but I use next way (Sequelize ver. 5.2.13):
let tableName = myModel.getTableName(options);
let qi = myModel.QueryInterface;
options.type = 'SELECT';
options.model = myModel;
var sql = qi.QueryGenerator.selectQuery(tableName, options, myModel);

How can i pass input argument when writing loopback-testing

I am writing a test driven development for my strongloop API code with the help of loopback-testing .
Here they do not have any detailed document on this, so i am stuck with case of argument passing with the API call
Example i have a below case,
Method : PUT
URL : /api/admin/vineyard/<vineyard_id>
i need to pass the below arguments with this URL
1. 'vineyard_id' is a id of vine, it should be an integer .
2. in header = 'token'
3. in body = '{'name':'tastyWine','price':200}'
How can i pass these three arguments with this API ?
I can easily handle ,if there is only two types of arguments
Example :
Method : POST
`/api/user/members/<test_username>/auth'`
arguments : test_username and password
I can handle this like this ,
lt.describe.whenCalledRemotely('POST',
'/api/user/members/'+test_username+'/auth', {
'password': test_passwords
},
But how can i handle the above case , Many thanks for your answers for this example.
I'm not entirely sure what your specific problem is, but I will attempt to walk through everything you should need.
I am assuming you are using the predefined prototype.updateAttributes() method for your model as described here.
Next assumption is that you want to use the built-in authentication and authorization to allow the user to call this method. Given that assumption, you need something like this in your test code:
var vineyard_id = 123; //the id of the test item you want to change
var testUser = {email: 'test#test.com',password: 'test'};
lt.describe.whenCalledByUser(testUser, 'PUT', '/api/admin/vineyard/'+vineyard_id,
{
'name':'tastyWine',
'price':200
},
function () {
it('should update the record and return ok', function() {
assert.equal(this.res.statusCode, 200);
});
}
);
If you are using the out-of-the-box user model, you should be fine, but if you extended the model as is commonly done, you may need something like this early on in your test file:
lt.beforeEach.withUserModel('user');
Also, be aware of a few (currently incomplete) updates to will allow for better handling of built-in model extensions: Suggestions #56, Add support for non-default models #57, and givenLoggedInUser() function throws error #59.

Resources