GitHub Readme.md
This shopping cart is using Redis and RedisJSON module functionalities, allowing you to save JSON as keys using methods like json_get and json_set.
JSON.SET product:{productId} . '{ "id": "productId", "name": "Product Name", "price": "375.00", "stock": 10 }'
.
JSON.SET product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 . '{ "id": "e182115a-63d2-42ce-8fe0-5f696ecdfba6", "name": "Brilliant Watch", "price": "250.00", "stock": 2 }'
HSET cart:{cartId} product:{productId} {productQuantity}
, where cartId is random generated value and stored in user session.
HSET cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 1
JSON.SET product:{productId} . '{ "id": "productId", "name": "Product Name", "price": "375.00", "stock": {newStock} }'
.
JSON.SET product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 . '{ "id": "e182115a-63d2-42ce-8fe0-5f696ecdfba6", "name": "Brilliant Watch", "price": "250.00", "stock": 1 }'
HSET cart:{cartId} product:{productId} {newProductQuantity}
or HINCRBY cart:{cartId} product:{productId} {incrementBy}
.
HSET cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 2
HINCRBY cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 1
HINCRBY cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 -1
HDEL cart:{cartId} product:{productId}
HDEL cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
HGETALL cart:{cartId}
and then HDEL cart:{cartId} {productKey}
in loop.
HGETALL cart:77f7fc881edc2f558e683a230eac217d
=> product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
, product:f9a6d214-1c38-47ab-a61c-c99a59438b12
, product:1f1321bb-0542-45d0-9601-2a3d007d5842
=> HDEL cart:77f7fc881edc2f558e683a230eac217d product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
, HDEL cart:77f7fc881edc2f558e683a230eac217d product:f9a6d214-1c38-47ab-a61c-c99a59438b12
, HDEL cart:77f7fc881edc2f558e683a230eac217d product:1f1321bb-0542-45d0-9601-2a3d007d5842
SCAN {cursor} MATCH cart:*
and then DEL cart:{cartId}
in loop.
SCAN {cursor} MATCH cart:*
=> cart:77f7fc881edc2f558e683a230eac217d
, cart:217dedc2f558e683a230eac77f7fc881
, cart:1ede77f558683a230eac7fc88217dc2f
=> DEL cart:77f7fc881edc2f558e683a230eac217d
, DEL cart:217dedc2f558e683a230eac77f7fc881
, DEL cart:1ede77f558683a230eac7fc88217dc2f
SCAN {cursor} MATCH product:*
to get all product keys and then JSON.GET {productKey}
in loop.
SCAN {cursor} MATCH product:*
=> product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
, product:f9a6d214-1c38-47ab-a61c-c99a59438b12
, product:1f1321bb-0542-45d0-9601-2a3d007d5842
=> JSON.GET product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
, JSON.GET product:f9a6d214-1c38-47ab-a61c-c99a59438b1
, JSON.GET product:1f1321bb-0542-45d0-9601-2a3d007d5842
HGETALL cart:{cartId}
to get quantity of products and JSON.GET product:{productId}
to get products data in loop.
HGETALL cart:77f7fc881edc2f558e683a230eac217d
=> product:e182115a-63d2-42ce-8fe0-5f696ecdfba6 (quantity: 1)
, product:f9a6d214-1c38-47ab-a61c-c99a59438b12 (quantity: 0)
, product:1f1321bb-0542-45d0-9601-2a3d007d5842 (quantity: 2)
=> JSON.GET product:e182115a-63d2-42ce-8fe0-5f696ecdfba6
, JSON.GET product:f9a6d214-1c38-47ab-a61c-c99a59438b12
, JSON.GET product:1f1321bb-0542-45d0-9601-2a3d007d5842
Go to server folder (cd ./server
) and then:
# Environmental variables
Copy `.env.example` to `.env` file and fill environmental variables
REDIS_PORT: Redis port (default: 6379)
REDIS_HOST: Redis host (default: 127.0.0.1)
REDIS_PASSWORD: Redis password (default: demo)
cp .env.example .env
# Run docker compose or install redis with RedisJson module manually. You can also go to https://redislabs.com/try-free/ and obtain necessary environmental variables
docker network create global
docker-compose up -d --build
# Install dependencies
npm install
# Run dev server
npm run dev
Go to client folder (cd ./client
) and then:
# Environmental variables
Copy `.env.example` to `.env` file
cp .env.example .env
# Install dependencies
npm install
# Serve locally
npm run serve
To make deploys work, you need to create free account in https://redislabs.com/try-free/, create Redis instance with RedisJson
module and get informations - REDIS_ENDPOINT_URI and REDIS_PASSWORD. You must pass them as environmental variables.