Cube style guide
This style guide includes best practices on data modeling in Cube. While Cube allows for certain flexibility with regards to data modeling, following this fairly opinionated style guide helps create maintainable semantic layers and reduce effort to support them in the long run. This style guide is intended to be used by:- All users of Cube Cloud and self-hosted Cube Core deployments.
- Solution integrators from the Cube Partner Network.
- Cube team (for demo projects, documentation, and customer engagements).
Syntax
- Default to YAML syntax for data modeling.
- Use snake case when using either YAML or JavaScript syntax.
- Follow the recommendations on YAML syntax and SQL syntax below.
Folder structure
- Put cube and view files in
model/cubesandmodel/viewsfolders. - Within these folders, create subfolders to reflect your business units structure.
Cubes
- Cubes should remain private; set
public: falsefor all cubes. Only views can be exposed to visualization tools. - A cube’s name should represent a business entity and be plural. If a cube’s
name may clash with a view’s name, add the
base_prefix to the cube’s name, e.g.,base_opportunities.yml. - If possible, use
sql_tableinstead ofsql, i.e., usesql_table: schema.tableinstead ofsql: SELECT * FROM schema.table. - Use
many_to_one,one_to_many,one_to_onejoin relationship types instead ofbelongs_to,has_many,has_one. - Applicable cube parameters should be ordered as follows:
namesql_aliasextendsdata_sourcesqlsql_tabletitledescriptionpublicrefresh_keymetapre_aggregationsjoinsdimensionshierarchiessegmentsmeasuresaccess_policy
Dimensions & measures
- Primary key of the cube should be the first dimension listed.
- Applicable dimension and measure
parameters should be ordered as follows:
nametitledescriptionsqltypeprimary_keysub_querypublicformatfiltersdrill_members
- Use
titleanddescriptionif the name is not intuitive.
Example cube
Views
- Views should be designed for data consumers and optimized for consumption in visualization tools.
- Applicable view parameters should be ordered as follows:
namedescriptionpubliccubesfoldersaccess_policy
Example view
SQL style guide
- Indent with 2 spaces.
- Use trailing commas.
- Use upper case for SQL keywords and function names.
- Use
!=instead of<>. - Always use the
ASkeyword when aliasing columns, expressions, and tables. - Unless SQL query is a trivial one-liner, start SQL query from the new line.
- Use new lines, optimize for readability and maintainability.
- Use common table expressions (CTE) rather than subqueries.
- When joining multiple tables, always prefix the column names with the table name/alias.
- Use single quotes for strings.
- Avoid initialisms and unnecessary table aliases.
- If there’s only one thing in the list (e.g., projection expressions in
SELECT), put it on the same line as the opening keyword. - If there are multiple things in the list, put each one on its own line (including the first one), indented one level more than the opening keyword.
Example SQL
YAML style guide
- Use
.ymlextension instead of.yaml. - Indent with 2 spaces.
- Indent list items.
- Use a new line to separate list items that are dictionaries, where appropriate.
- Make sure lines are no longer than 80 characters.
- Prefer literal style (
|) for multi-line strings over folded style (>). - If quotes are needed around a string, use double quotes.
Example YAML
JavaScript style guide
- Indent with 2 spaces.
- Don’t use trailing semicolons.
- Don’t use trailing commas after last elements of arrays and objects.
- Use a new line to separate list items that are objects, where appropriate.
- Make sure lines are no longer than 80 characters.
- If quotes are needed around a string, use backticks.