Advertisement
Guest User

Untitled

a guest
Jul 15th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.41 KB | None | 0 0
  1. const knex = require('knex')
  2.  
  3. const knexInstance = knex({
  4. client: 'pg',
  5. connection: 'postgresql://dunder-mifflin@localhost/knex-practice',
  6. })
  7.  
  8. function searchByName(searchTerm) {
  9. knexInstance
  10. .select('id', 'name', 'price', 'date_added', 'checked', 'category')
  11. .from('shopping_list')
  12. .where('name', 'ILIKE', `%${searchTerm}%`)
  13. .then(result => {
  14. console.log(result)
  15. })
  16. }
  17.  
  18. searchByName('pepper')
  19.  
  20. function paginateItems(pageNumber) {
  21. const resultsPerPage = 6
  22. const offset = resultsPerPage * (pageNumber - 1)
  23. knexInstance
  24. .select('id', 'name', 'price', 'date_added', 'checked', 'category')
  25. .from('shopping_list')
  26. .limit(resultsPerPage)
  27. .offset(offset)
  28. .then(result => {
  29. console.log(rult)
  30. })
  31. }
  32.  
  33. paginateItems(2)
  34.  
  35. function getItemByAge (daysAgo) {
  36. knexInstance
  37. .select('id', 'name', 'price', 'date_added', 'checked', 'category')
  38. .from('shopping_list')
  39. .where(
  40. 'date_added',
  41. '<',
  42. knexInstance.raw(`now() - '?? days'::INTERVAL`, daysAgo)
  43. )
  44. .then(result => {
  45. console.log(result)
  46. })
  47. }
  48.  
  49. getItemByAge(8)
  50.  
  51. function getCategoryPrice() {
  52. knexInstance
  53. .select('category')
  54. .from('shopping_list')
  55. .groupBy('category')
  56. .sum('price AS total')
  57. .then(result => {
  58. console.log(result)
  59. })
  60. }
  61.  
  62. getCategoryPrice();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement