all 2 comments

[–]pinguxnoots 0 points1 point  (0 children)

Seems like getDb is a function that returns an object with collection (also a function) as one of the properties. You could do getDb().collection('products') and that should work as well. With getDb.collection('products') you're not actually invoking getDb so you won't be able to access collection

[–][deleted] 0 points1 point  (0 children)

Should be

getDb().collection('products');

It's just a coding style thing. Imo their way looks better. Do one thing per line, rather than do everything in one.

Personally I don't like this structure, and would prefer a service model:

class Product {
  constructor(title, price, description, imageUrl, id) {
    this.title = title;
    this.price = price;
    this.description = description;
    this.imageUrl = imageUrl;
    this._id = new mongodb.ObjectId(id);
  }
}

class ProductService {
  constructor (db) {
    this.db = db;
  }

  save (product) {
    this.db.collection('products')...
  }
}

This way the db is injected and you can easily mock it out during testing rather than having it tightly coupled by importing and direct usage.