skip to Main Content

/I’ve already established a connection in knex with mysql and i am trying to do a loginForm connected to mysql db and right now i am doing a login check.I was wondering if there is any difference between this: knex().select("id").from("users)
and this: knex("users").select("id")
/

function checkLogIn(email, username, password) {
  knex("users").select("id").where({
      email: "[email protected]",
      username: "username",
      password: "password",
    })
  
}

2

Answers


  1. In cases where you are undecided, you can print the pure sql statement to the console as in the example below:

    const query = knex("users").select("id").where("email", userEmail).first()
    console.info(query.toString())
    
    // then you can run the query
    const result = await query
    
    const otherQuery = knex.select("id").from("users").where("email", userEmail).first()
    console.info(otherQuery.toString())
    
    const otherResult = await otherQuery
    
    
    Login or Signup to reply.
  2. No, There is no difference between using
    knex().select("id").from("users) or knex("users").select("id").

    knex().select("id").from("users) => This is simply method chaining where select and from methods are used. Query at execution time will be select id from users

    knex("users").select("id") => Here, knex knows that the tableName can be passed in param and at execution time will convert the query to select id from users

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search