How to use native sequelize statement

Preface:

Recently wrote an interface need to use sub-queries, and can result in sub-query is not an option as directly in another query, the district can not sequelize official website looked at how to use the direct native statements

sequelize document content translation

Look at the code examples for some official website, setting them

//1
sequelize.query('SELECT 1', {
  logging: console.log,
  
  plain: false,
 
  raw: false,

  type: Sequelize.QueryTypes.SELECT
})

//2
sequelize
  .query('SELECT * FROM projects', { raw: true })
  .then(projects => {
    console.log(projects)
  })
//3
sequelize.query('SELECT * FROM projects WHERE status = ?',
  { replacements: ['active'], type: sequelize.QueryTypes.SELECT }
).then(projects => {
  console.log(projects)
})
//4
sequelize.query('SELECT * FROM projects WHERE status = :status ',
  { replacements: { status: 'active' }, type: sequelize.QueryTypes.SELECT }
).then(projects => {
  console.log(projects)
})
复制代码
  • sequelize query function provides for direct manipulation statement native

  • This function returns two parameters - the result array and object contains metadata for mysql will return a reference to two objects.

  • The second parameter query function, is an object, the object several parameters which will be described below.

    1. pain: If plain is true, then sequelize returns only the first result set a record. If false, all the records are returned.
    2. type: the type of query being executed (can also update is hi oh, the official website to see what specific api). Query format type affects the way return to the previous results.
    3. raw: Query whether a definition of the type of model, if your query is not defined model, set this to true.
    4. logging: recording function console.log queries whether to call each SQL query sent to the server.
  • For conditions where to find the back of the field

    1. If you pass an array? Will be replaced in the order they appear in the array.
    2. If an object is passed,: key in the object key will be replaced. If the object contains key query is not found, the query will throw an exception.
  • For the latter alternative where the variables may be used in an array matching the keyword may be used like% wildcard character code such as:

//in关键字使用官网例子
sequelize.query('SELECT * FROM projects WHERE status IN(:status) ',
  { replacements: { status: ['active', 'inactive'] }, type: sequelize.QueryTypes.SELECT }
).then(projects => {
  console.log(projects)
})

//like通配符关键字使用官网例子
sequelize.query('SELECT * FROM users WHERE name LIKE :search_name ',
  { replacements: { search_name: 'ben%'  }, type: sequelize.QueryTypes.SELECT }
).then(projects => {
  console.log(projects)
})
复制代码
  • When the query may also be passed directly Model, if transfer model, the returned data is an example of this model, the above may also be used in other fields that
sequelize
  .query('SELECT * FROM projects', {
    model: Projects,
    mapToModel: true // 如果有任何映射字段,则在这里传递true
  })
  .then(projects => {
    // Each record will now be an instance of Project
  })
复制代码

Specific statements sequelize native Example

Native statement for a query code to see how I use in the development process as follows:

let sqlRank = `SELECT userspk.avatar AS user_avatar, 
        userspk.gender AS user_gender, 
        userspk.nickname AS user_nickname,
        a.id AS pk_record_id,
        a.user_id, 
        a.answer_record, 
        a.pk_type, 
         MAX(score) AS score, 
        a.create_time
        FROM (select * from pkrecord  order by score desc,create_time asc) as a 
        INNER JOIN userspk AS userspk 
        ON a.user_id = userspk.user_id
        WHERE a.status = 1 
        AND a.pk_type = 'noreal' 
        AND a.subject_id = :subject_id
        GROUP BY user_id
        ORDER BY a.score DESC 
        LIMIT 3;`
let pkRankResult= await ctx.main.query(sqlRank,  {
    replacements: {
        subject_id: subject_id,
    },
    type: Sequelize.QueryTypes.SELECT }
);
复制代码

Note that the replacements subject_id variable parameter value is obtained from the front end of the request, is a variable oh

annex:

The official website statement queries natively Address: docs.sequelizejs.com/manual/raw-...

I think this paper help you? Please share with more people

Welcome to my public concern number - programmers growth refers to the North. Your own micro-letter search - "Programmer growth refers to the North."

Guess you like

Origin blog.csdn.net/weixin_33883178/article/details/91367157