Skip to content

v2.1.0

Choose a tag to compare

@WesloTheWeb WesloTheWeb released this 09 Apr 03:06
· 27 commits to master since this release
6ccf68e

Significant refactoring and now the code is in MongoDb. This version exclusively uses MongoDb's driver. There is no ORM / ODM in place, and the queries exist interacting with the database.

Note: Any remnants of SQL or Sequelize have been replaced. To use this, you must create a MongoDb Atlas account and connect the database through there.

Features

  • Users can create products for the shop (admin tab), delete products (admin tab) and edit products.
  • Users can add products to the cart, as well as remove products from the cart.
  • Upon adding to cart, you can submit an order and the 'Orders' tab is now populated with a user id and items that have been purchased.

Set Up

  • In order to set up, create a mongodb Atlas account, have your database config file similar to this (your own key is required)
  • MongoDB Compass is optional, as it provides a GUI to see the data more clearly but you can see it in terminal. You do not need to worry about creating databases on the GUI or terminal because the code will create it as it sees fit right now with the existing queries. You will just need mainly a user with read / write access.
require('dotenv').config();

const mongodb = require('mongodb');
const MongoClient = mongodb.MongoClient;
const MongoDBPassword = process.env.DB_PASSWORD;

let _db;

const mongoConnect = (callback) => {
    MongoClient.connect(`mongodb+srv://<UserId>:${MongoDBPassword}@cluster0.k30d4tr.mongodb.net/shop?retryWrites=true&w=majority`)
        .then((client) => {
            console.log('Connection Successful!');
            _db = client.db();
            callback()
        })
        .catch(err => {
            console.log(err);
            throw err;
        });
};

const getDb = () => {
    if (_db) {
        return _db;
    };
    throw 'No database found!';
};

exports.mongoConnect = mongoConnect;
exports.getDb = getDb;

As well as setting up the right middleware in App.js similar to this snippet

const mongoConnect = require('./util/database').mongoConnect;

...

mongoConnect(() => {
    app.listen(3000);
});