Use case
We want to create a cube for a dataset which uses the Entity-Attribute-Value model (EAV). It stores entities in a table that can be joined to another table with numerous attribute-value pairs. Each entity is not guaranteed to have the same set of associated attributes, thus making the entity-attribute-value relation a sparse matrix. In the cube, we’d like every attribute to be modeled as a dimension.Data modeling
Let’s explore theusers cube that contains the entities:
users cube is joined with the orders cube to reflect that there might be
many orders associated with a single user. The orders remain in various
statuses, as reflected by the status dimension, and their creation dates are
available via the created_at dimension:
Let’s say that we’d like to know, for each user, the earliest creation date for
their orders in any of these statuses. In terms of the EAV model:
- the users serve as entities and they should be modeled with a cube
- order statuses serve as attributes and they should be modeled as dimensions
- the earliest creation dates for each status serve as attribute values and they will be modeled as dimension values
Static attributes
We already know that the following statuses are present in the dataset:completed, processing, and shipped. Let’s assume this set of statuses is
not going to change often.
Then, modeling the cube is as simple as defining a few joins (one join per
attribute):
The drawback is that when the set of statuses changes, we’ll need to amend the
cube definition in several places: update selected values and joins in SQL as
well as update the dimensions. Let’s see how to work around that.
Static attributes, DRY version
This approach uses JavaScript-specific features — programmatic code generation
with helper functions and
Object.assign. It is not available in YAML data
models.users_statuses_DRY cube is functionally identical to the
users_statuses_joins cube above. Querying this new cube would yield the same
data. However, there’s still a static list of statuses present in the cube’s
source code. Let’s work around that next.
Dynamic attributes
This approach uses JavaScript-specific features —
asyncModule, require, and
the Node.js pg package. It is not available in YAML data models.fetch.js file that defines the fetchStatuses function that would load the
statuses from the database. Note that it uses the pg package (Node.js client
for Postgres) and reuses the credentials from Cube.
fetchStatuses function to load the list of
statuses. We will also wrap the cube definition with the asyncModule built-in
function that allows the data model to be created
dynamically.
users_statuses_dynamic cube is functionally identical to the
previously created cubes. So, querying this new cube would yield the same data
too.