How to create two Environments and test them in Embeddable

A question we often get is how to create two environments - for example “Production” and “Staging” and test them in Embeddable. To achieve this, you can define your .cube.yml files like so:

cubes:
  - name: customers
    title: My customers
    data_source: default # can omit, as `data_source` has value `default` by default
    sql: >
        ...

And use the Connections API to provide your db credentials like so:

body: JSON.stringify({
	name: 'production-postgres',
	type: 'postgres',
	credentials: {
		...

and

body: JSON.stringify({
	name: 'staging-postgres',
	type: 'postgres',
	credentials: {
		...

and then just create two environments like so:

body: JSON.stringify({
	name: 'production',
	datasources: [
		{ data_source: 'default', connection: 'production-postgres'}
	]
})

and

body: JSON.stringify({
	name: 'staging',
	datasources: [
		{ data_source: 'default', connection: 'staging-postgres'}
	]
})

and then when generating your security token, simply pass either “production” or “staging” to the environment field. When you pass “production” to the environment field it will dynamically map all the models whose data_source is “default” to the “production-postgres” database. Whereas for “staging” it will map them to the “staging-postgres” database.

To test this in the Embeddable no-code builder, just update your src/presets/security-contexts.sc.yml file to specify the environment, like so:

- name: Example customer 1
  securityContext:
    country: United States
  environment: production

- name: Example customer 2
  securityContext:
    country: Germany
  environment: staging

Now you can test against staging data as well as production data in the builder, before clicking “publish”.