Reputation: 2216
What would be a proper way to handle global "settings" in my sailsjs application? The user will want to change those settings via the web front of my app.
I imagine I could use a new model "GlobalSettings" with only one item, but I don't really know if it's a good "MVC" practice.
Upvotes: 1
Views: 1095
Reputation: 1072
In my own opinion, I always find as a web app develops, you will start to realize there are more and more fields that you want the user to setup as their preference, as a good practice to relax the application.
For me I usually setup a meta_data model with name, value, criteria, and some other fields.
For example, when viewing your web page, 'Alice' may want a background color of black, 'Bob' may want a background color of green. Then you can let them modify or insert row into this meta_data collection. Then in your database, you will have
name value criteria
background_color black user_name='Alice'
background_color green user_name='Bob'
and it can be all kinds of values.
of course if you just have one value that can be changed by all of your users, it is probably a good idea to know who updated them. For this you would want to create a trigger, (if you are using a sql database)see trigger in mysql, so that every update on the table will trigger a function that stores what was changed and who changed it in another table
So yes, to answer your question, it is totally ok to have a model to store a value, and don't worry about only have one row, you will have more as you develop your app.
Upvotes: 1
Reputation: 1608
Since it is based on user input, it has to be stored in a database and therefore storing it in model seems like a right choice to me.
Having just 1 row/collection is completely ok in my opinion, especially in the no-SQL field. But for more reusability and scalability, you might want to consider to actually store each setting in invididual row, that might give you space to expand the usability of it in the future.
Upvotes: 3
Reputation: 81
The config/globals.js
file seems to be a good place to place a global configuration setting.
For convenience, Sails exposes a handful of global variables. By default, your app's models, services, and the global sails object are all available on the global scope; meaning you can refer to them by name anywhere in your backend code (as long as Sails has been loaded). Nothing in Sails core relies on these global variables - each and every global exposed in Sails may be disabled in sails.config.globals (conventionally configured in config/globals.js.)
Alternatively you can use the sails.config
object.
Custom Configuration
Sails recognizes many different settings, namespaced under different top level keys (e.g. sails.config.sockets and
sails.config.blueprints). However you can also use sails.config for your own custom configuration (e.g. sails.config.someProprietaryAPI.secret).
From the docs
There is also services which is global by default.
Overview Services can be thought of as libraries which contain functions that you might want to use in many places of your application. For example, you might have an EmailService which wraps some default email message boilerplate code that you would want to use in many parts of your application. The main benefit of using services in Sails is that they are globalized--you don't have to use require() to access them.
It really depends on what kind of global you are wanting.
Upvotes: 0