-
Notifications
You must be signed in to change notification settings - Fork 1
Populating Database
It is now possible to populate the database with around 300 products from various categories which have been scraped from Argos. To do this:
- start
redis-server
from your CLI (you must have redis installed); -
cd backend
, and start up a servernpm run nodemon
; - Go to your browser and make a get request with http://localhost:4000/populateDB.
Products are stored as hashes, with UUIDs as their keys. They can be accessed through the dbHelpers.js functions, which must be imported into other files and invoked with a client parameter like so:
const client = require('./../redis.js')
const dbHelpers = require('./../dbHelpers.js')(client)
(Unless you are testing, you should import the client from redis.js
.) Please do not import the client at the top of server.js
or the tests will be hanging (and ultimately you will not be able to make a pull request).
Once you have imported the dbHelpers, they can be used to retrieve categories and individual products from the database. dbHelpers.getProductById('383a13e3-ad02-e854-fbb0-858a628a3809').then((x) => console.log(x))
will log something like:
{ id: '383a13e3-ad02-e854-fbb0-858a628a3809',
title: 'Dare 2b Women\'s Psychedelic Bodywarmer',
price: '49.99',
imageLink: 'http://www.argos.co.uk/wcsstore/argos/images/568-8077949IEUC1038839T.jpg',
rating: '4',
reviews: '[{"author":"nickname","text":"This Dare 2b Women\'s Psychedelic Bodywarmer was fantastic. I have never been so happy with a product before in my life as I was with the Dare 2b Women\'s Psychedelic Bodywarmer. ","rating":4.2,"date":1458316650693},{"author":"nickname","text":"This Dare 2b Women\'s Psychedelic Bodywarmer was fantastic. I have never been so happy with a product before in my life as I was with the Dare 2b Women\'s Psychedelic Bodywarmer. ","rating":4.9,"date":1458316650693},{"author":"nickname","text":"This Dare 2b Women\'s Psychedelic Bodywarmer. was fantastic. I have never been so happy with a product before in my life as I was with the Dare 2b Women\'s Psychedelic Bodywarmer. ","rating":3.6,"date":1458316650693}]',
description: 'Perfect for individuals OR families, this product , a Dare 2b Women\'s Psychedelic Bodywarmer. , is an absolute bargain at £49.99. It is extremely reliable, made with high quality materials, and comes with a 5-year quality guarantee.',
stock: 4,
categories: '["clothing","women","global"]' }
Likewise, dbHelpers.getProductsByCategories(['clothing', 'women']).then((x) => console.log(x))
will log:
['981eb5dc-7661-95b6-38c5-6bd6e9926666', '4dfdc9b6-85a8-95ae-0989-b80c5fc7c424', '39d133c4-e620-affd-0c05-b891f79c3fce', 'aa8e4a06-a180-795d-c064-245bb6087170' .....etc...]
Where the IDs in the array are products which have all the categories passed into the function, i.e. in this case, women AND clothing. If you want to access all products regardless of category, you can use the global
category which all products are a member of.