I would like to save some site settings to the Mongo db through an administrator panel, in order to always have them handy on both client and server. The basic format for settings would be key-value pairs, but this is not the format of collections and documents.
Should I make something like a “singleton” collection and just store my key-value pairs in a single document?
Or maybe divide the pairs into categories, each with its own document?
Or possibly store each pair as a document, like { name: "background", value: "starry pink" }?
It might not be a big impact, but these values need to be retrieved many times all over the site, so I’m thinking about performance as well as practicality. Considerations include whether to use a SimpleSchema and/or AutoForm, and ease of tracking updates.
What will be the easiest, most performant way for me to, at any time, retrieve a setting from Mongo given a key?
P.S. I’m using FastRender to get the settings to the client ASAP.
I have tried out a ‘settings’ collection with only one document and when I first start up the app after a db reset it checks if that single record exists and if not forces you to fill in a ‘default settings’ page that creates the first admin user and a bunch of the settings.
Then a null publication that always serves whatever is in public and the root of the document, and private is of course available on the server. This settings document is similar to the settings.json approach (with the private, public vars), so I figured that would make logical sense.
I have tested out using an always available app = {settings: {}} that follows a similar convention but gets extended with the settings collection document, that way I can mix in hard coded settings and user customisable settings reasonably easy.
Only frustrating this is that the subscription always has some delay so in the example, the appName would take a few hundred milliseconds to load in the nav-bar, bit cluncky but works.
I have not settled on a solid approach I am happy with, when I do I might make a package (or see if other packages exist that do this).
I tried out each different solution – not so much as a test, but because I wanted the merits of each in turn – and found that @lbee had the only solution that really works.
My app now uses a config collection with only one document. In the administration panel of the site, I use several autoforms each updating a part of the config doc. Everything works like a charm.
Using FastRender couldn’t be easier, just make sure your router files are not inside a client or server folder (I’m using lib for my routes) and add the fastrender property.
The only gotcha is forgetting to wrap client specific code inside routes in an if (Meteor.isClient) statement.