staab / js-allonge

Repository for collaboration on the project for the Javascript Allonge group 2014
MIT License
1 stars 3 forks source link

Design datastore #5

Open staab opened 9 years ago

staab commented 9 years ago

Mostly just dvd instance and rental history data.

staab commented 9 years ago

Also figuring out nosql/sql, and writing a reasonable db api that's simpler to use than Django's cursor.

cr-walker commented 9 years ago

Since we're using nosql (mongodb) as an academic exercise, we should take advantage of its (absolutely un-necessary) strengths: scalability and performance. Here's my first stab at it.

By the way, the documentation is brilliant. http://docs.mongodb.org/manual/

Two common nosql schema design patterns:

To design a nosql database, we should consider "What questions will asked of our data?" rather than just "How is our data structured?". Common questions:

Customers

Management

Let's try to structure the database so that each of these questions can be answered by either a simple search from a single collection of documents, or reading a single document. I think this can be accomplished with three collections:

TMDB collection

Here's the data format straight from TMDB. If we want to cache this, putting it in mongodb will be really simple. My only request is that we assign the _id field in mongodb to the TMDB id to make lookups simpler. Note the addition at the end showing which assets are available.

{
  "adult": false,
  "backdrop_path": "/8uO0gUM8aNqYLs1OsTBQiXu0fEv.jpg",
  "belongs_to_collection": null,
  "budget": 63000000,
  "genres": [
    {
      "id": 28,
      "name": "Action"
    },
    {
      "id": 18,
      "name": "Drama"
    },
    {
      "id": 53,
      "name": "Thriller"
    }
  ],
  "homepage": "",
  "id": 550,
  "imdb_id": "tt0137523",
  "original_title": "Fight Club",
  "overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.",
  "popularity": 61151.745,
  "poster_path": "/2lECpi35Hnbpa4y46JX0aY3AWTy.jpg",
  "production_companies": [
    {
      "name": "20th Century Fox",
      "id": 25
    }
  ],
  "production_countries": [
    {
      "iso_3166_1": "DE",
      "name": "Germany"
    },
    {
      "iso_3166_1": "US",
      "name": "United States of America"
    }
  ],
  "release_date": "1999-10-15",
  "revenue": 100853753,
  "runtime": 139,
  "spoken_languages": [
    {
      "iso_639_1": "en",
      "name": "English"
    }
  ],
  "status": "Released",
  "tagline": "How much can you know about yourself if you've never been in a fight?",
  "title": "Fight Club",
  "vote_average": 9.1,
  "vote_count": 174
  assets: [
    { asset_id : <ObjectId>,
      status : 'available', // options are 'unavailable' or 'available' 
      format : 'blu-ray'
    },
    { asset_id : <ObjectId>,
      status : 'unavailable',
      format : 'dvd'
    }
  ]
}    

Asset Collection

These are our local physical assets owned by the store.

{ _id : <ObjectId>, // this just needs to be universally unique, it's the 'HHID' 
  title : TMDB_id, // this will match both TMDB and our cached copy, if it exists
  format : 'blu-ray', // options presumably include 'vhs', 'dvd', 'blu-ray'
  status : 'available' // 'available' or 'unavailable'
}

Transactions

We'll store each transaction as a separate document. There are a lot of questions on business logic that we'll just gloss over, e.g.: Are items priced by 'new release' status? How do people pay? Are there late fees and so on?

{ _id : <ObjectId>,
  user_id : <ObjectId>,
  assets : [
    asset_id, 
    asset_id, 
    ... // probably can rent multiple items at once
  ]
  amount : 328 // note that price is stored as an integer representing cents (USD).
  metadata : [
    date: ISOdate('2014-11-1'),
    stripe_token: 'very long string representing transaction on stripe' 
  ]
}

Users

{ _id : <ObjectId>,
  email : 'someone@gmail.com',
  phone : '208-882-1234'
}

Implementation

We'll use the simple mongodb driver.

npm install mongodb

I'll write a wrapper around this to support the customer / management questions above. Note that since some data is stored in a couple of places we'll need to be careful to update asset status correctly. This is just a first draft, let me know what you think!

feus4177 commented 9 years ago

Under management questions, I think a good subquestion to add under optimizing is what are customers searching for that we don't have? I feel like one of the main reasons that people turn to local movie stores is for hard to find movies (indie, old, etc.).

staab commented 9 years ago

This looks cool. I'm interested in seeing how it turns out.