SHARE
TWEET

Untitled

a guest Oct 23rd, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict';
  2.  
  3. require('dotenv').config();
  4. const knex = require('knex');
  5.  
  6. const knexInstance = knex({
  7.   client:'pg',
  8.   connection: process.env.DB_URL
  9. });
  10.  
  11. const query = require('readline').createInterface({
  12.   input: process.stdin,
  13.   output: process.stdout
  14. });
  15.  
  16. query.question('Would you like to.. \n ( 1 ) Get All items that contain the following text:  \n ( 2 ) Get all items paginated \n ( 3 ) Get all items added after date \n ( 4 ) Get the total cost for each category \n Choice #:',
  17.   (choice) => {
  18.     const productsPerPage = 6;
  19.     let offset = 0;
  20.     switch(choice) {
  21.     case '1':
  22.       //query.close();
  23.       query.clearLine();
  24.       query.question('Please enter your search term:', (search) => {
  25.         console.log(search);
  26.         knexInstance
  27.           .select('id','name', 'price', 'category', 'checked', 'date_added')
  28.           .from('shopping_list')
  29.           .where('name', 'ILIKE', `%${search}%`)
  30.           .then(result => {
  31.             console.log(result);
  32.             query.close();
  33.           });
  34.       });
  35.       break;
  36.     case '2':
  37.       query.clearLine();
  38.       query.question('Please enter a page number:', (page) => {
  39.         offset = productsPerPage * (page - 1) ;
  40.         console.log(page);
  41.         knexInstance
  42.           .select('id','name', 'price', 'category', 'checked', 'date_added')
  43.           .from('shopping_list')
  44.           .limit(productsPerPage)
  45.           .offset(offset)
  46.           .then(result => {
  47.             console.log(result);
  48.             query.close();
  49.           });
  50.       });
  51.       break;
  52.     case '3':
  53.       query.clearLine();
  54.       query.question('Please enter a number representing "days ago" for item added after date:', (daysAgo) => {
  55.         console.log(daysAgo);
  56.         knexInstance
  57.           .select('id','name', 'price', 'category', 'checked', 'date_added')
  58.           .from('shopping_list')
  59.           .where('date_added', '>', knexInstance.raw(`now() - '?? days'::INTERVAL`, daysAgo))
  60.           .then(result => {
  61.             console.log(result);
  62.             query.close();
  63.           });
  64.       });
  65.       break;
  66.     case '4':
  67.       knexInstance
  68.         .select('category')
  69.         .sum('price AS total')
  70.         .from('shopping_list')
  71.         .groupBy('category')
  72.         .then(result => {
  73.           console.log('Cost Per Category');
  74.           console.log(result);
  75.           query.close();
  76.         });
  77.       break;
  78.     }
  79.  
  80.   });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top