How to use the Op function from sequelize
Find comprehensive JavaScript sequelize.Op code examples handpicked from public code repositorys.
sequelize.Op is an object in the Sequelize ORM library that contains a set of operators that can be used in database queries.
1 2 3 4 5 6 7 8 9 10
const extend = require('extend'); const fs = require('fs'); const uuid = require('uuid'); const Sequelize = require('sequelize'); const Op = Sequelize.Op; const defineQueueTable = (sequelize, name) => { return sequelize.define( name,
+ 13 other calls in file
How does sequelize.Op work?
sequelize.Op
is an object in the Sequelize ORM library that contains a set of operators that can be used in database queries.
When defining a query with Sequelize, the Op
object can be used to specify the operator to use for a particular comparison. For example, to find all users with a name containing the string 'John', we can use the Op.like
operator like this:
javascriptconst { Op } = require('sequelize');
const users = await User.findAll({
where: {
name: {
[Op.like]: '%John%'
}
}
});
In this example, we use Op.like
to specify a case-insensitive search for the string 'John' within the name
column of the User
model. The resulting query would be translated to SQL like this:
sqlSELECT * FROM users WHERE name LIKE '%John%';
The sequelize.Op
object also contains other comparison operators, such as Op.gt
(greater than), Op.lt
(less than), Op.between
(between two values), Op.in
(in an array of values), and many more.
For example, we could use Op.between
to find all users with a birthdate between two dates:
javascriptconst users = await User.findAll({
where: {
birthdate: {
[Op.between]: ['1990-01-01', '2000-01-01']
}
}
});
In this example, we use Op.between
to specify a range query for the birthdate
column of the User
model, where the value must be between '1990-01-01'
and '2000-01-01'
. The resulting query would be translated to SQL like this:
sqlSELECT * FROM users WHERE birthdate BETWEEN '1990-01-01' AND '2000-01-01';
By using the sequelize.Op
object in this way, we can create flexible and powerful database queries with Sequelize.
GitHub: pubpub/pubpub
2 3 4 5 6 7 8 9 10 11
const passportLocalSequelize = require('passport-local-sequelize'); // const operatorsAliases = { // $or: Sequelize.Op.or, // $and: Sequelize.Op.and, // $ilike: Sequelize.Op.iLike, // $in: Sequelize.Op.in, // $not: Sequelize.Op.not, // $eq: Sequelize.Op.eq, // $ne: Sequelize.Op.ne,
+ 17 other calls in file
9 10 11 12 13 14 15 16 17 18
* ******************************************************************************* * */ const Sequelize = require('sequelize') const Op = Sequelize.Op const AppHelper = require('../helpers/app-helper') const ChangeTrackingService = require('./change-tracking-service') const ErrorMessages = require('../helpers/error-messages')
Ai Example
1 2 3 4 5 6 7 8
const { Op } = require("sequelize"); const users = await User.findAll({ where: { name: { [Op.like]: "%John%", }, }, });
In this example, we use sequelize.Op to specify the Op.like operator for a case-insensitive search for the string 'John' within the name column of the User model. We pass the where option to the findAll method, which specifies the search criteria for the query. Within the where option, we use an object with a name property that contains another object with the Op.like operator and the search string '%John%'. When the findAll method is called, Sequelize will construct a SQL query that includes the LIKE operator for the specified column and search string. We could also use other operators in a similar way, such as Op.gt, Op.lt, Op.between, and many more. For example, we could use Op.between to find all users with a birthdate between two dates: javascript Copy code
13 14 15 16 17 18 19 20 21 22
Default: require('./modelGenerators/default') } var MIN_SERVER_VERSION = '1.10.0' var Op = Sequelize.Op class SqlStoreError extends Error { constructor (properties) { super()
GitHub: wejs/we-core
26 27 28 29 30 31 32 33 34 35
this.modelsConfigs = { t: getTranslationsModelConfigs(we) }; this.Sequelize = Sequelize; we.Op = Sequelize.Op; this.projectFolder = process.cwd(); this.defaultModelDefinitionConfigs = {
0 1 2 3 4 5 6 7 8 9 10
const express = require('express') const router = express.Router() const { requireAuth, orgCheck } = require('../../utils/auth'); const { Group, GroupImage, Membership, User, Venue, EventImage, Attendance, Event } = require('../../db/models'); const sequelize = require('sequelize') const Op = sequelize.Op router.get('/', async (req, res, next) => { //maybe refactor later, cannot think of any other way than N+1. Tried including. let groups = await Group.findAll()
+ 2 other calls in file
17 18 19 20 21 22 23 24 25 26 27
const svgCaptcha = require("svg-captcha") let whiteList = require(__basename + "/whiteList/whiteList.js"); const { BroadcastChannel } = require("broadcast-channel"); //获取操作符引用 let Op = Sequelize.Op; let token = ""; class RoutesController { //验证验证码 validCode(req, res, next) {
66 67 68 69 70 71 72 73 74 75
}); }); } async getPostsByMinDate(minDateStr) { return new Promise((resolve, reject) => { const { gte } = Sequelize.Op; Post.findAll({ where: { postDate: { [gte]: new Date(minDateStr)
sequelize.col is the most popular function in sequelize (1002 examples)