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);
Related
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.
I want to run more than one query .. how to do it?
eg, I have below two queries -
FOR doc IN users
RETURN doc
FOR doc IN users
RETURN { user: doc, newAttribute: true }
If I have to run both queries I have to run them separately, is there a way to execute a script or I need to put a semicolon at the end like SQL and run it.
Can I use arangosh?
You can use LET to execute multiple sub-queries in a single queries:
LET firstUserResult = (
FOR doc IN users
RETURN doc
)
LET secondUserResult = (
FOR doc IN users
RETURN { user: doc, newAttribute: true }
)
RETURN { first: firstUserResult, second: secondUserResult }
Some notes here - you will need to add an additional RETURN statement at the end of the query. This will definitely work for reads but you may run into issues when trying to write in multiple queries.
I've inherited a project at work that uses Azure Cosmos DB. It's completely new to me. In the CosmosDB, we have a bunch of user preferences that are saved. I've discovered a typo in the settings that I need to fix. However, I cannot figure out how to modify the value.
So far I've found the query explorer and I want to run this query:
Update c
set c.Setting = REPLACE(c.Setting, 'N*m', 'N-m')
but query explorer only supports select, not update.
I tried to use Azure Storage Explorer, but when I try to access the document I get nothing except a modal saying "Hold on! We are still working on this." Seriously Microsoft?
My current thinking is to upload a stored procedure and run that. But I'm not sure where to start. My other thinking is to write a small c# application that iterates through each user document and updates them individually. Something like this:
currId = 0;
databaseId = ...;
collectionId = ...;
collectionLink = ...;
while (currId < maxUserId) {
var response = await client.ReadDocumentAsync(UriFactory.CreateDocumentUri(databaseId, collectionId, currId.ToString()));
if (response.Resource != null) {
var upserted = response.Resource;
upserted.SetPropertyValue("Setting", "N-m");
response = await client.UpsertDocumentAsync(collectionLink, upserted);
}
currId++;
}
But boy if that doesn't seem like a dumb idea...
What's the best way to update a single value in a CosmosDB Document?
Does ToFuture works with nhibernate linq? If so, how do you use it?
Be carefull ToFuture only works if the database driver supports MulitpleQueries. This is only the case in some drivers (eg. MySql, SqlServer) but not in all (eg. Oracle)
Yes it does. Here's a simple example:
var blogs = _session.Query<Blog>()
.Take(30)
.ToFuture();
var blogCount= _session.Query<Blog>()
.ToFutureValue(x => x.Count());
Console.WriteLine(blogCount.Value); // DB is queried here.
Here's an example of where I've used it for a customer search form that displayed paged search results and and a total count of search results. Notice you can reuse an IQueryable to create two futures. The Filter methods built an IQueryable based on what fields the user searched by.
int resultsPerPage = 50;
var query = _session.Query<CustomerSearch>()
.FilterById(model)
.FilterByFirstName(model)
.FilterByLastName(model)
.FilterBySocialSecurityNumber(model)
.FilterByPrimaryPhoneNumber(model);
var futureResults = query
.OrderBy(x => x.Id)
.Skip(model.Page * resultsPerPage)
.Take(resultsPerPage)
.ToFuture();
var futureCount = query.ToFutureValue(x => x.Count());
Why is no SQL being generated when I run my Nhibernate 3 query?
public IQueryable<Chapter> FindAllChapters()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var chapters = session.QueryOver<Chapter>().List();
return chapters.AsQueryable();
}
}
If I run the query below I can see that the SQL that gets created.
public IQueryable<Chapter> FindAllChapters()
{
using (ISession session = NHibernateHelper.OpenSession())
{
var resultDTOs = session.CreateSQLQuery("SELECT Title FROM Chapter")
.AddScalar("Title", NHibernateUtil.String)
.List();
// Convert resultDTOs into IQueryable<Chapter>
}
}
Linq to NHibernate (like Linq to entities) uses delayed execution. You are returning IQueryable<Chapter> which means that you might add further filtering before using the data, so no query is executed.
If you called .ToList() or .List() (i forget which is in the API), then it would actually produce data and execute the query.
In other words, right now you have an unexecuted query.
Added: Also use Query() not QueryOver(). QueryOver is like detached criteria.
For more info, google "delayed execution linq" for articles like this