Skip to content

Commit 9321e03

Browse files
committed
docs: Merge tenant data model recipes into single page
1 parent cfff973 commit 9321e03

10 files changed

+204
-200
lines changed

docs/pages/product/configuration.mdx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,19 @@ to; changing that list would require the deployment to restart.
1616

1717
</InfoBox>
1818

19+
<ReferenceBox>
20+
1921
See the [environment variables reference][link-env-vars] for all supported options.
2022

23+
</ReferenceBox>
24+
25+
<ReferenceBox>
26+
27+
See [this recipe][ref-env-var-recipe] if you'd like to reference environment variables
28+
in code.
29+
30+
</ReferenceBox>
31+
2132
### Cube Core
2233

2334
You can set environment variables in any way [supported by
@@ -47,8 +58,13 @@ processed by Cube.
4758
</InfoBox>
4859

4960
Configuration options take precedence over environment variables.
61+
62+
<ReferenceBox>
63+
5064
See the [configuration options reference][link-config] for all supported options.
5165

66+
</ReferenceBox>
67+
5268
### `cube.py` and `cube.js` files
5369

5470
Configuration options can be defined either using Python, in a `cube.py` file,
@@ -200,3 +216,4 @@ mode does the following:
200216
[ref-python]: /product/data-modeling/dynamic/jinja#python
201217
[ref-javascript]: /product/data-modeling/dynamic/javascript
202218
[ref-cube-package-config]: /product/data-modeling/reference/cube-package#config-object
219+
[ref-env-var-recipe]: /product/configuration/recipes/environment-variables

docs/pages/product/configuration/multitenancy.mdx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ Token][ref-security].
2929
There are several multitenancy setup scenarios that can be achieved by using
3030
combinations of these configuration options.
3131

32+
<ReferenceBox>
33+
34+
See the following recipes:
35+
- If you'd like to provide a [custom data source][ref-per-tenant-data-source-recipe] for each tenant.
36+
- If you'd like to provide a [custom data model][ref-per-tenant-data-model-recipe] for each tenant.
37+
38+
</ReferenceBox>
39+
3240
### Multitenancy vs Multiple Data Sources
3341

3442
In cases where your Cube data model is spread across multiple different data
@@ -391,3 +399,5 @@ input.
391399
[ref-security]: /product/auth
392400
[ref-cube-datasource]: /product/data-modeling/reference/cube#data_source
393401
[ref-cube-security-ctx]: /product/data-modeling/reference/context-variables#compile_context
402+
[ref-per-tenant-data-source-recipe]: /product/configuration/recipes/multiple-sources-same-schema
403+
[ref-per-tenant-data-model-recipe]: /product/configuration/recipes/custom-data-model-per-tenant
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
module.exports = {
22
"environment-variables": "Environment variables",
3-
"using-different-schemas-for-tenants": "Different data models for tenants",
4-
"custom-data-model-per-tenant": "Custom data model for each tenant",
5-
"multiple-sources-same-schema": "Multiple data sources",
6-
"using-ssl-connections-to-data-source": "SSL with data sources",
7-
"data-store-cost-saving-guide": "Data store cost saving guide"
3+
"using-ssl-connections-to-data-source": "SSL",
4+
"data-store-cost-saving-guide": "Data source usage",
5+
"multiple-sources-same-schema": "Per-tenant data sources",
6+
"custom-data-model-per-tenant": "Per-tenant data models"
87
}

docs/pages/product/configuration/recipes/custom-data-model-per-tenant.mdx

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,153 @@ module.exports = {
348348

349349
</CodeTabs>
350350

351+
#### Example
352+
353+
Here's an example of how to use this approach. Let's say we have a folder structure
354+
as follows:
355+
356+
```tree
357+
model/
358+
├── avocado/
359+
│ └── cubes
360+
│ └── Products.js
361+
└── mango/
362+
└── cubes
363+
└── Products.js
364+
```
365+
366+
Let's configure Cube to use a specific data model path for each tenant using the
367+
`repositoryFactory` function along with `contextToAppId` and `scheduledRefreshContexts`:
368+
369+
```javascript
370+
const { FileRepository } = require("@cubejs-backend/server-core");
371+
372+
module.exports = {
373+
contextToAppId: ({ securityContext }) =>
374+
`CUBE_APP_${securityContext.tenant}`,
375+
376+
repositoryFactory: ({ securityContext }) =>
377+
new FileRepository(`model/${securityContext.tenant}`),
378+
379+
scheduledRefreshContexts: () => [
380+
{ securityContext: { tenant: "avocado" } },
381+
{ securityContext: { tenant: "mango" } },
382+
],
383+
};
384+
```
385+
386+
In this example, we'll filter products differently for each tenant. For the `avocado`
387+
tenant, we'll show products with odd `id` values, and for the `mango` tenant, we'll show
388+
products with even `id` values.
389+
390+
This is the `products` cube for the `avocado` tenant:
391+
392+
<CodeTabs>
393+
394+
```yaml
395+
cubes:
396+
- name: products
397+
sql: >
398+
SELECT * FROM public.Products WHERE MOD (id, 2) = 1
399+
```
400+
401+
```javascript
402+
cube(`products`, {
403+
sql: `SELECT *
404+
FROM public.Products
405+
WHERE MOD (id, 2) = 1`,
406+
407+
// ...
408+
});
409+
```
410+
411+
</CodeTabs>
412+
413+
This is the `products` cube for the `mango` tenant:
414+
415+
<CodeTabs>
416+
417+
```yaml
418+
cubes:
419+
- name: products
420+
sql: >
421+
SELECT * FROM public.Products WHERE MOD (id, 2) = 0
422+
```
423+
424+
```javascript
425+
cube(`products`, {
426+
sql: `SELECT *
427+
FROM public.Products
428+
WHERE MOD (id, 2) = 0`,
429+
430+
// ...
431+
});
432+
```
433+
434+
</CodeTabs>
435+
436+
To fetch the products for different tenants, we send the same query but with different
437+
JWTs:
438+
439+
```json
440+
{
441+
"sub": "1234567890",
442+
"tenant": "Avocado",
443+
"iat": 1000000000,
444+
"exp": 5000000000
445+
}
446+
```
447+
448+
```json5
449+
{
450+
sub: "1234567890",
451+
tenant: "Mango",
452+
iat: 1000000000,
453+
exp: 5000000000,
454+
}
455+
```
456+
457+
This approach produces different results for each tenant as expected:
458+
459+
```json5
460+
// Avocado products
461+
[
462+
{
463+
"products.id": 1,
464+
"products.name": "Generic Fresh Keyboard",
465+
},
466+
{
467+
"products.id": 3,
468+
"products.name": "Practical Wooden Keyboard",
469+
},
470+
{
471+
"products.id": 5,
472+
"products.name": "Handcrafted Rubber Chicken",
473+
},
474+
]
475+
```
476+
477+
```json5
478+
// Mango products:
479+
[
480+
{
481+
"products.id": 2,
482+
"products.name": "Gorgeous Cotton Sausages",
483+
},
484+
{
485+
"products.id": 4,
486+
"products.name": "Handmade Wooden Soap",
487+
},
488+
{
489+
"products.id": 6,
490+
"products.name": "Handcrafted Plastic Chair",
491+
},
492+
]
493+
```
494+
495+
You can find a working example of this approach on [GitHub](https://github.com/cube-js/cube/tree/master/examples/recipes/using-different-schemas-for-tenants).
496+
Run it with the `docker-compose up` command to see the results in your console.
497+
351498
### Loading externally
352499

353500
Finally, you can maintain independent data models for each tenant that you would

docs/pages/product/configuration/recipes/data-store-cost-saving-guide.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Data Store Cost Saving Guide
1+
# Optimizing data source usage
22

33
As a semantic layer, Cube supports various [data
44
sources][ref-config-data-sources], including cloud-based data warehouses and
@@ -8,7 +8,7 @@ and business intelligence.
88
Depending on the upstream data source and the use case, Cube can be configured
99
in a way that provides the best economic effect and maximizes cost savings.
1010

11-
## Reducing Data Store Usage
11+
## Reducing data store usage
1212

1313
Cloud-based data stores commonly use two pricing models: _on-demand_, when you
1414
get charged for the storage and compute resources used to process each query, or

docs/pages/product/configuration/recipes/multiple-sources-same-schema.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Using multiple data sources
1+
# Providing a custom data source for each tenant
22

33
## Use case
44

0 commit comments

Comments
 (0)