NoSQL Database Design – Storing Global Variables Design Pattern

database-designdocument-databasesnosql

I'm wanting to create an admin dashboard for my app, and to show new events there. In order to know which events are new, I'll need to store at least one extra datapoint to the server: the last date at which the admin user (me) looked at the events and marked them all as read.

I'm trying to figure out where it makes sense to store that data.

I'm using Mongoose, and have a few different document types. Options I've considered include:

  • create an extra field on the user schema, that will be undefined for all non-admin users
  • create a new document type just to store admin data
  • use mongodb directly, rather than going through mongoose

Is one of these better than the others? Have I missed something that's obviously better than all of them?

Best Answer

I'm not familiar with Mongoose but we have started pushing configuration out of xml files and into the DB using ravendb. Our approach is to:

  • We create a configuration object or object graph with sensible defaults.
  • We write a configuration manager class that provides:
    • automatic creation of the object if it does not exist
    • singleton access to the class
    • manages caching, etc if required

On the database level it is just a special document with a fixed ID -- hence the singleton. We have had a few approaches where we could swap various configurations into the "active" singleton as well. From the code's perspective little has changed -- we typically injected configuration using IoC containers so the codebase has been pretty agnostic. Config is just manna from heaven.

Overall I'm pretty happy with it. The biggest downside is it takes config out of version control which is a feature I've really liked for the auditability aspect.

Related Topic