Modules (multi-file)
Entry point of a multi-file project: use * from a barrel that re-exports with reuse; selective use { ... } with element kinds and as aliases; importing a whole schema and a table group; Metadata blocks adding properties to imported elements.
Covers: Module system: use * from, selective use { table … tablepartial … }, as aliases to avoid name clashes, importing a whole schema, a tablegroup and a sticky note, a barrel file that re-exports with reuse, .dbml extension optional in specifiers, nested folder, front matter, Metadata blocks on imported elements, a DiagramView over imported tables
9 tables · 8 refs · 1 group · 1 sticky note · 1 view · 6 files, entry main.dbml
View: customer_360
A DiagramView shows a subset of the schema. Pass view="customer_360" (or pick it in the toolbar's view menu) to start there:
Source
6 files in examples/12-modules/
// The auth schema, imported selectively and as a whole schema by main.dbml.
Table auth.users {
id integer [pk]
email varchar
password_hash varchar [not null]
}
Table auth.roles {
id integer [pk]
name varchar [not null, unique]
}
Table auth.sessions {
id integer [pk]
user_id integer [not null, ref: > auth.users.id]
expires_at timestamp [not null]
}
TableGroup auth_core [color: #8e44ad] {
auth.users
auth.roles
}
Note auth_readme {
'auth.* is managed by the identity service; read-only for everyone else.'
}
// Defines its own `users` table, which would clash with common/users.dbml;
// main.dbml imports it under an alias.
Table users {
id integer [pk]
stripe_customer_id varchar(64) [unique]
balance "decimal(12,2)" [not null, default: 0]
}
Table invoices {
id integer [pk, increment]
billing_user_id integer [not null, ref: > users.id]
amount "decimal(12,2)" [not null]
}
// Barrel file: `reuse` re-exports so importers of common/index see these elements
// without knowing the folder layout. A plain `use` would keep them private here.
reuse * from './users'
reuse * from './orders'
// Orders depend on users: a sibling import inside the common/ folder.
// @dbml/parse 12.1.0 requires a table's partials to be imported alongside it.
use {
table users
tablepartial timestamps
} from './users'
Table orders {
id integer [pk, increment]
user_id integer [not null]
total "decimal(10,2)" [not null]
placed_at timestamp [not null, default: `now()`]
}
Ref orders_user: orders.user_id > users.id [delete: restrict]
// Shared user tables. Re-exported by common/index.dbml.
TablePartial timestamps {
created_at timestamp [not null, default: `now()`]
updated_at timestamp
}
Table users [owner: 'identity-team'] {
id integer [pk, increment]
email varchar(255) [not null, unique]
~timestamps
}
enum user_role {
member
admin
owner
}
---
title: Modular shop
owner: platform-team
---
// Entry point of a multi-file project.
// - `use *` from a barrel that re-exports with `reuse`
// - selective `use { ... }` with element kinds and `as` aliases
// - importing a whole schema and a table group
// - Metadata blocks adding properties to imported elements
use * from './common/index'
use {
table users as billing_users
table invoices
} from './billing'
use {
schema auth
tablegroup auth_core
note auth_readme
} from './auth.dbml'
Project modular_shop {
database_type: 'PostgreSQL'
Note: 'Example of the DBML module system (use / reuse).'
}
Table user_links {
user_id integer [pk, ref: - users.id]
billing_user_id integer [unique, ref: - billing_users.id]
auth_user_id integer [unique, ref: - auth.users.id]
}
Table order_invoices {
order_id integer [ref: > orders.id]
invoice_id integer [ref: > invoices.id]
indexes {
(order_id, invoice_id) [pk]
}
}
Metadata Table users {
owner: 'growth-team'
pii: true
}
Metadata Ref orders_user {
critical: true
}
DiagramView customer_360 {
Tables {
users
user_links
billing_users
orders
}
TableGroups { auth_core }
}