Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. require('dotenv').config();
  2. const knex = require('knex');
  3.  
  4. const db = knex({
  5. client: 'pg',
  6. connection: process.env.DB_URL
  7. });
  8.  
  9. console.log('knex and driver installed correctly');
  10.  
  11. function getItemsWithTerm(searchTerm){
  12. db('shopping_list')
  13. .select('product_id', 'name', 'price', 'checked')
  14. .where('name', 'ilike', `%${searchTerm}%`)
  15. .then(result => { console.log(result); } );
  16. }
  17. getItemsWithTerm('tuna');
  18.  
  19. function paginateResults(page){
  20. const productsPerPage = 6;
  21. const offset = productsPerPage * (page -1);
  22. db('shopping_list')
  23. .select('product_id', 'name', 'price', 'checked')
  24. .where( 'price', '<', '5')
  25. .limit(productsPerPage)
  26. .offset(offset)
  27. .then(result => {
  28. console.log(result);
  29. console.log('Items less than $5, page' { page });
  30. } );
  31. }
  32. paginateResults(3);
  33.  
  34. function getItemsAddedAfterDate(daysAgo){
  35. db('shopping_list')
  36. .select('name', 'price', 'checked', 'date_added')
  37. .where('date_added', '>', db.raw(`now() - '?? days'::INTERVAL`, daysAgo) )
  38. .then(result => { console.log( result); } );
  39. }
  40. getItemsAddedAfterDate(2);
  41.  
  42. function categoriesGrouped() {
  43. db('shopping_list')
  44. .select('category')
  45. .where('checked', false)
  46. .sum('price as total')
  47. .groupBy('category')
  48. .then(result => {
  49. console.log('Totals per category for unchecked items');
  50. console.log(result);
  51. });
  52. }
  53. categoriesGrouped();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement