How can I create a single-use global data model?

I want to create a centralized place for global options that will only be set once, e.g. website name or default social image. Is there a way to set up a data model that can only ever have a single entry?

1 Like

Hi @JonathanLand,

Yes, you can set this out using builder data models. Please check Data Models - Builder.io and let us know if you’ve further questions!

Hey, thanks for the reply. However, every example I see is for models where I would want multiple entries (e.g. blog authors). I’m looking for a way to explicitly limit the model to a single entry, i.e. a piece of data there can be only one of (e.g. website name). How can I achieve this?

@JonathanLand, To put a limit on a model to have a single entry. You can use Using model validation hooks - Builder.io.

To centralize like website name, default image, etc. you can use Custom fields - Builder.io in the Data model and use it anywhere in the page or section model type content.

Hey @garima,
A. This is not a solution, since the goal is a better experience for our editors. Why show a list of entries when it is only possible to have one?
B. Recommending features without mentioning that they are enterprise-only is a little underhanded IMO, and also a bit silly. I want to hope that enterprise customers get better customer service than the public forum.
The options for a global singleton options model is something every other CMS I’ve used has, seems like a serious omission.
Sorry if I sound impatient, but for what your service costs we were expecting a serious solution for this common use case.

1 Like

Did you find a solution? I have the same problem…

I need something like globals as in payload CMS: Global Configs | Documentation | Payload CMS

Please vote for this if you are interested in this feature: Add a singleton global data model | Builder.io Ideas

1 Like

Create a table to store your global options. Ensure that this table has a column for each option you want to store globally. Additionally, add a unique constraint to a column that identifies the global options entry uniquely.
CREATE TABLE GlobalOptions (
id SERIAL PRIMARY KEY,
option_name VARCHAR(255) UNIQUE,
option_value TEXT
);

Implement a sales data enrichment access layer in your application that enforces a singleton pattern when accessing or modifying these global options. This ensures that only one instance of the global options exists at any given time.

Here’s a simplified example in Python using a singleton pattern:
class GlobalOptions:
_instance = None

def __new__(cls, *args, **kwargs):
    if not cls._instance:
        cls._instance = super().__new__(cls, *args, **kwargs)
        # Fetch global options from the database and store them as instance attributes
        cls._instance._options = cls._fetch_options_from_database()
    return cls._instance

@staticmethod
def _fetch_options_from_database():
    # Fetch global options from the database and return them
    # You may use ORM or raw SQL queries here
    return {}

def get_option(self, option_name):
    return self._options.get(option_name)

def set_option(self, option_name, option_value):
    # Update the database with the new option value
    # You may use ORM or raw SQL queries here
    self._options[option_name] = option_value

Example usage:

global_options = GlobalOptions()
website_name = global_options.get_option(‘website_name’)
if not website_name:
global_options.set_option(‘website_name’, ‘My Website’)