Learn the basics of Commodo in our knowledge-sharing playlist below.
To simplify API development, Webiny heavily relies on a package called Commodo.
What is Commodo?
Commodo is a set of higher order functions (HOFs) that let you define and compose rich data model objects.
In other words, it's a package that out-of-the-box offers a couple of simple higher-order-functions, which enable you to define rich data models. Once you define a model, you can use it for example to do a simple data validation. And if needed, by utilizing additional HOFs, you can even store the data to the database.
Let's check out a couple of examples to get a better understanding of what all of this means.
Creating a simple model
Basic construct of every model is a field. The following example shows how to create a simple data model that consists of four fields:
A more complex model
Using other HOFs, you can create more complex models, that have a name, hooks, and even a storage layer, so that you can easily save the data to the database. For example:
To make it easier, we've created the
@webiny/commodopackage, which aggregates all of the relevant
commodo-*packages and lets you import any HOF (or any other construct) within a single import statement. The list of all included packages can be found here.
Using higher order functions (HOFs) is a very flexible approach for defining your data models, because you get to choose which functionality the model will posses.
Is Commodo an ORM/ODM?
Fundamentally, Commodo is not an ORM/ODM, but can very quickly become one, by utilizing additional HOFs that deal with storing to and reading from a database. The
withStorage HOF (seen in the above example) is what you'll need if you'll want to store the data into a database.