Skip to content

Instantly share code, notes, and snippets.

@ebectar
Last active October 23, 2018 17:13
Show Gist options
  • Save ebectar/e4fd10fb2b4ab2a52c7b4b7d454e408b to your computer and use it in GitHub Desktop.
Save ebectar/e4fd10fb2b4ab2a52c7b4b7d454e408b to your computer and use it in GitHub Desktop.

Knex CRUD commands

LIST

select * from allergies

knex('allergies')  
 .then(data => {
   console.log("SELECT", data)
})
 .catch(err => {
   console.log("BAD THING...", err)
})

CREATE

knex('allergies')
 .insert([{ allergen: 'grasses', pollen_count: 5},
    {allergen: 'more grasses', pollen_count: 10}])
 .returning('*')
 .then(data => {
    console.log("RETURN FROM INSERT", data)
 })

READ

let userId = '; drop table allergies;' 
let userId = 8;

knex('allergies')
  .select('id', 'allergen')
  .where('id', +(userId))
  .then(data => {
    console.log("SELECT WITH WHERE CLAUSE", data)
  })

UPDATE

knex('allergies')
 .update({ allergen: 'grasses'})
 .where('id', 8)
 .then(data => console.log("RETURN FROM UPDATE", data))

DELETE

knex('allergies')
 .delete()
 .where('id', 7)
 .returning('*')
 .then(data => console.log("RETURN FROM DELETE", data))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment