Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. require('dotenv').config();
  2. const knex = require('knex');
  3.  
  4. const knexInstance = knex({
  5. client: 'pg',
  6. connection: process.env.DB_URL
  7. });
  8.  
  9. function searchByTerm(searchTerm) {
  10. knexInstance
  11. .from('shopping_list')
  12. .select('*')
  13. .where('name', 'ILIKE', `%${searchTerm}%`)
  14. .then(result => {
  15. console.log(result);
  16. });
  17. }
  18.  
  19. function searchByPage(pageNumber) {
  20. const productsPerPage = 6;
  21. const offset = productsPerPage * (pageNumber - 1);
  22. knexInstance
  23. .from('shopping_list')
  24. .select('*')
  25. .limit(productsPerPage)
  26. .offset(offset)
  27. .then(result => {
  28. console.log(result);
  29. });
  30. }
  31.  
  32. function itemsAfterDate(daysAgo) {
  33. knexInstance
  34. .from('shopping_list')
  35. .select('*')
  36. .where(
  37. 'date_added',
  38. '>',
  39. knexInstance.raw(`now() - '?? days'::INTERVAL`, daysAgo)
  40. )
  41. .then(result => {
  42. console.log(result);
  43. });
  44. }
  45.  
  46. knexInstance
  47. .from('shopping_list')
  48. .select('category')
  49. .sum('price as total')
  50. .groupBy('category')
  51. .then(result => {
  52. console.log(result);
  53. });
  54.  
  55. knexInstance.from('shopping_list');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement