Created
June 10, 2019 19:34
-
-
Save jcharles22/6b2d26635f2c88e7bbe3d75ccc82561a to your computer and use it in GitHub Desktop.
Database with node drills
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require('dotenv').config(); | |
const knex = require('knex'); | |
const knexInstance = knex({ | |
client: 'pg', | |
connection: process.env.DB_URL | |
}) | |
function searchName(searchTerm) { | |
knexInstance | |
.select('id', 'name', 'price', 'category') | |
.from('shopping_list') | |
.where('name', 'ILIKE', `%${searchTerm}%`) | |
.then(result => { | |
console.log(result) | |
}) | |
} | |
searchName('fried') | |
function paginateProducts(page) { | |
const productsPerPage = 6; | |
const offset = productsPerPage * (page -1); | |
knexInstance | |
.select('id', 'name', 'price', 'category') | |
.from('shopping_list') | |
.limit(productsPerPage) | |
.offset(offset) | |
.then(result => { | |
console.log(result); | |
}) | |
} | |
paginateProducts(2); | |
function mostPopularVideosForDays(days) { | |
knexInstance | |
.select('name', 'date_added') | |
.where( | |
'date_added', | |
'>', | |
knexInstance.raw(`now() - '?? days'::INTERVAL`, days) | |
) | |
.from('shopping_list') | |
.orderBy([ | |
{ column: 'date_added', order: 'ASC' }, | |
]) | |
.then(result => { | |
console.log(result) | |
}) | |
} | |
mostPopularVideosForDays(30) | |
function costPerCategory() { | |
knexInstance | |
.select('category') | |
.sum('price as total') | |
.from('shopping_list') | |
.groupBy('category') | |
.then(result => { | |
console.log('COST PER CATEGORY') | |
console.log(result) | |
}) | |
} | |
costPerCategory() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment