Sequelize : How to check if a substring exists in a table - mysql

Is there a way to check if a particular string exists in a column in a table?
For example, I have a table 'fruits' with two columns, primary key and fruit_name and following rows
1 apple
2 orange
3 pineapple
I have a sample string named apple_shake. I need to check if a substring of this apple_shake exists. The query should return row containing 'apple'
I know how this can be done in mysql query - SQL - Query to find if a string contains part of the value in Column
But through sequelize, following has problem
var sample_fruit_string = "apple_shake";
var gsp_config = await model.fruit.findOne({where: {
fruit_name: {
[Op.like]: sample_fruit_string + '%'
}
}});

Credit to #alx for the SQL I didn't know was possible - this is how you generate the appropriate SQL with Sequelize. Note that this may not be efficient with large datasets.
const sample_fruit_string = "apple_shake";
const gsp_config = await model.fruit.findOne({
attributes: ['id'],
where: Sequelize.where(
Sequelize.fn('LOCATE', Sequelize.col('fruit_name'), sample_fruit_string),
Sequelize.Op.ne,
0
),
logging: true,
});
Generates the following:
SELECT `id` FROM `fruit` AS `fruit` WHERE LOCATE(`fruit_name`, 'apple_shake') != 0 LIMIT 1;

Sequelize has a substring operator which you could use directly to solve this.
var sample_fruit_string = "apple_shake";
var gsp_config = await model.fruit.findOne({where: {
fruit_name: {
[Op.substring]: sample_fruit_string // LIKE '%sample_fruit_string%'
}
}});

var sample_fruit_string = "apple_shake";
var gsp_config = await model.fruit.findOne({where: {
fruit_name: {
[Op.like]: `%${sample_fruit_string}%` // LIKE '%sample_fruit_string%'
// [Op.ilike]: `%${sample_fruit_string}%` // For case sensitive searching
// [Op.substring]: sample_fruit_substring // Has been depreciated in future version of sequelize.
}
}});

Related

When inserting an array with TypeORM, the order of id that comes out as result?

TypeORM is working on inserting arrays using INSERT.
I'm going to do another task using the IDs that came out as a result of insert.
At this time, the IDs that come out as a result of insert come out in the order of array when insert?
// customRepository.ts
insertArr(nameArr : {name : string}[]){
reuturn this.createQueryBuilder()
.insert()
.into(customTable)
.values(nameArr)
.execute()
}
// service.ts
const connection = getConnection();
const repository = connection.getCustomRepository('customRepository')
const arr = [{name : 'first'},{name : 'second'}]
const result =
await repository.insertArr(
arr
);
console.log('result : ', result);
//Does this result come out in the order of insert?
Thank you!!!

how to dynamically update row in mysql on node express js

this is how i query to update row and it works:
const [rows, meta] = await db.query(
`
UPDATE
Portfolio
SET
title = ?,
infoText = ?,
devPeriod = ?,
tags = ?
WHERE
id = ?
`,
[title, infoText, Number(devPeriod), tags, Number(portfolioId)]
);
return rows;
but sometimes depending on what user wants i have to query to update only specific columns. For example user might want to edit only devPeriod or tags and infoText.
How do i achieve that?
I'd suggest creating an update object that specifies which fields to update and the relevant values. You can then create a query and parameters from this.
The update object can be populated based on user input.
For example:
async function updatePortfolio(db, portfolioId, update) {
const query = "Update Portfolio SET " + Object.keys(update).map(key => `${key} = ?`).join(", ") + " WHERE id = ?";
const parameters = [...Object.values(update), portfolioId];
console.log("updatePortfolio: Running query:", query);
const [rows, meta] = await db.query(query, parameters);
return rows;
}
// Add or remove fields as you require.
update = {
title: "Some Title",
infoText: "Infotext",
devPeriod: 10,
tags: "tags"
}
updatePortfolio(db, 1, update);
// Add or remove fields as you require.
update = {
title: "Some Title",
tags: "tags"
}
updatePortfolio(db, 2, update);
After asking my colleagues for help they introduced my to Knex.js to make dynamic query building easier. So I just went with it.
Have been using it for dynamic queries since then. Works pretty well for this type of task. Configuration is also easy:
import knex from 'knex';
const queryBuilder = knex({ client: 'mysql' });
export default queryBuilder;
Export it and use it anywhere in your project

sequelize between operator not working properly

I am trying to do query to get all rows between id 1 to 4.
I am using mySQL and sequelize.
query
`
main.test = function(req,res,next)
{
var param = req.params;
model.Time.find({
where:{
id:{
$between:[1,4]
}
},limit: 10
}).then(function (time) {
console.log("Time"+time);
});
}
`
and getting the result when execute
Need a way solve it?

Is it possible to use :bind (oracle style) in MYSQL query with Node?

I am migrating the database of my node.js/typescript project from Oracle to MYSQL.
My queries/dml in Oracle are all bind in this style
conn.execute('select date, name from table
where id = :ID and field = :VAR',
{ID: variable1, VAR: variable2});
When using MYSQL I found this:
connection.query('select date, name from table
where id = ? and field = ?',
[variable1, variable2]);
The second approach is worse for me because of following reasons:
i- I would to rewrite a lot of sql calls in my code
ii- I think the first approach is much more reliable, as you are not concerning of having unpredictable results due to changing in SQL
Although I found some mention to the first style here, it couldn't make it work
Any tips?
As I didn't find anything ready that could solve the issue, I tried to solve the problem. Maybe this could be helpful.
first, this code gets an Oracle bind interface type like {ID: 105, DEPT: 'MKT'} and a query like 'select * from emp where id = :ID and deptName = :DEPT' and translates them to [105,'MKT'] and 'select * from emp where id = ? and deptName = ?'
here is the code
const endBindCharacters: string = ' )=';
function prepareSQL(sql: string, binders: Object = null, valueArray: TBindArray): string {
let ich: number = 0;
let bindVariable: string;
if (! binders) {
if (sql.indexOf(':') > 0) {
throw new CustomError(errorCodes.connection.sqlBoundWithNoBinders,
'No binders {} in a bound SQL ' + sql);
};
return sql;
};
while ((ich = sql.indexOf(':')) > 0) {
bindVariable = '';
while (!endBindCharacters.includes(sql[++ich]) && ich < sql.length) {
bindVariable += sql[ich];
};
if (binders[bindVariable]) {
valueArray.push(binders[bindVariable]);
} else {
throw new CustomError(errorCodes.connection.bindVariableNotInBinders, ' Bind variable ' + bindVariable +
' não encontradada no binders {} da expressão:\n' + sql)
};
sql = sql.replace(':' + bindVariable, ' ? ');
};
return sql;
};
This is the wrapper. It will get a Promise from the callback.
export async function executeSQL (conn: TConnection, sql: string,
binders: Object = {}): Promise<TReturn> {
let bindArray: TBindArray = [];
sql = prepareSQL(sql, binders, bindArray);
console.log(sql, binders, bindArray);
return new Promise<TReturn>(function(resolve, reject) {
conn.query(sql, bindArray , function(err: db.IError, results: TReturn) {
if(err) {reject(err)}
else {resolve(results)};
});
});
};

how to find and rename field name in mongodb 3.2

how can I rename field name in MongoDB?
I want to replace all the field names that start with $ to &.
Thanks!
I saw some link and make a proper solution for you problem
First you need to get your all column, you could do this with MapReduce:
mr = db.runCommand({
"mapreduce" : "my_collection",
"map" : function() {
for (var key in this) { emit(key, null); }
},
"reduce" : function(key, stuff) { return null; },
"out": "my_collection" + "_keys"
});
Then run distinct on the resulting collection so as to find all the keys:
columns = db[mr.result].distinct("_id")
And rename the all matching columns
columns.forEach(function(columnName) {
if (columnName.indexOf('$') == 0) {
var newColumnName = columnName.replace('$', '&');
rename_query = { '$rename': {} };
rename_query['$rename'][columnName] = newColumnName;
db.my_collection.updateMany({}, rename_query)
}
})
Reference link are
MongoDB Get names of all keys in collection
MongoDB $rename javascript variable for key name