Skip to content

Commit 5f8d45e

Browse files
committed
doc: update
1 parent 230fc15 commit 5f8d45e

1 file changed

Lines changed: 111 additions & 124 deletions

File tree

dbml-homepage/docs/syntax/module-system.md

Lines changed: 111 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,23 @@ A single DBML file can grow very large, making it difficult to navigate, maintai
88

99
- [Overview](#overview)
1010
- [Selective Import](#selective-import)
11-
- [Supported Import Kinds](#supported-import-kinds)
12-
- [Importing a Schema](#importing-a-schema)
13-
- [Importing a TableGroup](#importing-a-tablegroup)
14-
- [Wildcard Import](#wildcard-import)
15-
- [Import Aliases](#import-aliases)
16-
- [Schema-Qualified Import](#schema-qualified-import)
11+
- [Supported Import Types](#supported-import-types)
12+
- [Import Aliases](#import-aliases)
13+
- [Import All](#import-all)
1714
- [Re-Exporting with `reuse`](#re-exporting-with-reuse)
18-
- [Circular Imports](#circular-imports)
15+
- [Notes](#notes)
1916

2017
## Overview
2118

2219
Use `use` to import elements from another file:
2320

2421
```text
2522
use {
26-
kind name
23+
type name
2724
} from './path-to-file'
2825
```
2926

30-
- **`kind`** — the element kind: `table`, `enum`, `tablepartial`, `schema`, or `tablegroup`. See [Supported Import Kinds](#supported-import-kinds).
27+
- **`type`** — the element type: `table`, `enum`, `tablepartial`, `note`, `schema`, or `tablegroup`. See [Supported Import Types](#supported-import-types).
3128
- **`name`** — the element name as declared in the source file
3229
- **`./path-to-file`** — a relative path to the source file; the `.dbml` extension is optional (`'./types'` and `'./types.dbml'` both work)
3330

@@ -36,7 +33,9 @@ use {
3633
Enum job_status {
3734
pending running done
3835
}
36+
```
3937

38+
```text
4039
// jobs.dbml
4140
use {
4241
enum job_status
@@ -48,47 +47,37 @@ Table jobs {
4847
}
4948
```
5049

51-
Each file is isolated by default — nothing is visible across files unless explicitly imported. Imports are also not transitive: if `a.dbml` imports `b.dbml` and `b.dbml` imports `c.dbml`, elements from `c.dbml` are not available in `a.dbml`.
50+
Each file is isolated by default — nothing is visible across files unless explicitly imported.
5251

5352
## Selective Import
5453

55-
When you only need a few specific elements from another file, list them by kind and name.
54+
You can selectively pick some elements from another file to import into the current file.
5655

5756
```text
58-
// types.dbml
59-
Enum job_status {
60-
pending
61-
running
62-
done
57+
// shared.dbml
58+
Table users {
59+
id int [pk]
6360
}
6461
65-
// jobs.dbml
66-
use {
67-
enum job_status
68-
} from './types'
62+
Enum role {
63+
admin member
64+
}
6965
70-
Table jobs {
66+
Table products {
7167
id int [pk]
72-
status job_status
68+
user_id int [ref: > users.id]
7369
}
7470
```
7571

76-
Multiple entries can appear in one block or across separate statements:
77-
7872
```text
73+
// products won't be imported here
7974
use {
8075
table users
8176
enum role
8277
} from './shared'
83-
84-
use {
85-
table orders
86-
} from './orders'
8778
```
8879

89-
Only the named elements become available in the importing file. Everything else in the source file stays out of scope.
90-
91-
### Supported Import Kinds
80+
### Supported Import Types
9281

9382
| Keyword | What is imported |
9483
|----------------|-----------------------------------------------|
@@ -99,70 +88,83 @@ Only the named elements become available in the importing file. Everything else
9988
| `schema` | All elements under that schema |
10089
| `tablegroup` | TableGroup (all tables in the group) |
10190

102-
Element kind keywords are case-insensitive (`Table`, `TABLE`, and `table` are all valid).
103-
104-
### Importing a Schema
105-
106-
When a source file defines multiple tables under the same schema, importing the schema brings all of them in at once instead of listing each table individually.
91+
Element type keywords are case-insensitive (`Table`, `TABLE`, and `table` are all valid).
10792

10893
```text
10994
// auth.dbml
11095
Table auth.users {
11196
id int [pk]
97+
email varchar
11298
}
11399
114100
Table auth.roles {
115101
id int [pk]
102+
name varchar
116103
}
117104
118105
Table auth.sessions {
119106
id int [pk]
120107
}
121108
122-
// main.dbml
109+
TableGroup auth_core {
110+
auth.users
111+
auth.roles
112+
}
113+
```
114+
115+
```text
116+
// u is available as a table here
117+
use {
118+
table auth.users as u
119+
} from './auth'
120+
121+
// auth.users, auth.roles, auth.sessions are available here
123122
use {
124123
schema auth
125124
} from './auth'
126125
127-
Table orders {
128-
id int [pk]
129-
user_id int [ref: > auth.users.id]
130-
}
126+
// auth_core, auth.user, auth.roles are available here
127+
use {
128+
tablegroup auth_core
129+
} from './auth'
131130
```
132131

133-
All tables, enums, and other elements declared under the `auth` schema become available in `main.dbml` under their original schema-qualified names.
134-
135-
### Importing a TableGroup
132+
### Import Aliases
136133

137-
Importing a `tablegroup` brings the group definition itself into scope, along with all the tables it references.
134+
When two files define elements with the same name, use `as` to rename imports and avoid conflicts.
138135

139136
```text
140-
// base.dbml
137+
// auth.dbml
141138
Table users {
142139
id int [pk]
140+
email varchar
143141
}
142+
```
144143

145-
Table posts {
144+
```text
145+
// billing.dbml
146+
Table users {
146147
id int [pk]
147-
user_id int
148+
amount decimal
148149
}
150+
```
149151

150-
TableGroup blog {
151-
users
152-
posts
153-
}
152+
```text
153+
// Alias the tables to avoid name conflicts
154+
use {
155+
table users as auth_users
156+
} from './auth'
154157
155-
// main.dbml
156158
use {
157-
tablegroup blog
158-
} from './base'
159+
table users as billing_users
160+
} from './billing'
159161
```
160162

161-
The `blog` tablegroup and its member tables (`users`, `posts`) are all available in `main.dbml`.
163+
Once aliased, only the alias name is accessible — the original name is not.
162164

163-
## Wildcard Import
165+
## Import All
164166

165-
When you want everything a file has to offer, use a wildcard instead of listing each element individually.
167+
When you want everything a file exports, use `*` instead of listing each element.
166168

167169
```text
168170
// base.dbml
@@ -173,121 +175,106 @@ Table users {
173175
Table orders {
174176
id int [pk]
175177
}
178+
```
176179

177-
// main.dbml
180+
```text
181+
// Everything from ./base.dbml will be imported
178182
use * from './base'
179183
180184
Ref: orders.user_id > users.id
181185
```
182186

183-
Wildcard and selective imports from the same file can coexist; any duplicate names are deduplicated automatically.
187+
`use *` and selective imports from the same file can coexist; any duplicate names are deduplicated automatically.
184188

185-
## Import Aliases
189+
## Re-Exporting with `reuse`
186190

187-
When two files define elements with the same name, or when you want a shorter name locally, use `as` to rename an import.
191+
`use` makes imported elements available only in the current file. If another file imports the current file, it will **not** see elements brought in via `use`:
188192

189193
```text
190-
// auth.dbml
191-
Table auth.users {
192-
id int [pk]
193-
email varchar
194-
}
194+
// common/index.dbml
195+
use * from './users'
196+
use * from './orders'
197+
```
195198

199+
```text
196200
// main.dbml
197-
use {
198-
table auth.users as u
199-
} from './auth'
200-
201-
Table orders {
202-
id int [pk]
203-
user_id int [ref: > u.id]
204-
}
201+
// users and orders are NOT available here
202+
use * from './common/index'
205203
```
206204

207-
Once aliased, only the alias name (`u`) is accessible — the original name is not. Aliases also strip any schema prefix, so `auth.users as u` is visible as plain `u` in the default schema.
208-
209-
The same element can be imported multiple times under different aliases. Giving two different elements the same alias name is an error.
210-
211-
## Schema-Qualified Import
212-
213-
To import elements that live inside a named schema, qualify the name with the schema.
205+
`reuse` goes one step further — it also makes them visible to any file that imports the current file.
214206

215207
```text
216-
// auth.dbml
217-
Table auth.users {
218-
id int [pk]
219-
}
220-
221-
Table auth.roles {
222-
id int [pk]
223-
}
208+
// common/index.dbml
209+
reuse * from './users'
210+
reuse * from './orders'
211+
```
224212

213+
```text
225214
// main.dbml
226-
use {
227-
table auth.users
228-
} from './auth'
215+
// users and orders are available here
216+
use * from './common/index'
229217
```
230218

231-
Without an alias the element keeps its schema-qualified name (`auth.users`). Combined with `as`, it is placed in the default schema under the alias:
219+
`reuse` is best for cases where you want to expose some schema elements to other consumers, without forcing the consumers to be aware of the internal folder structure of your project.
220+
221+
### The Barrel File Pattern With `reuse`
222+
223+
To illustrate a use case of `reuse`, suppose you have a project named `management` that is split into 3 files for easier personal management: `management/users.dbml`, `management/products.dbml`, `management/orders.dbml`.
224+
225+
With `use`, all consumers of `management` must be aware of your project structure to import everything:
232226

233227
```text
234-
use {
235-
table auth.users as u
236-
} from './auth'
237-
// visible as: u (no schema prefix)
228+
// Consumer's file
229+
use * from './management/users'
230+
use * from './management/products'
231+
use * from './management/orders'
238232
```
239233

240-
## Re-Exporting with `reuse`
234+
This is fragile, as when you decide to reorganize your project (e.g. merge `products` and `orders` into one file), all consumers' schemas will be broken.
241235

242-
When you do want a file to pass elements through to its own importers — for example, a barrel file that composes several sub-files — use `reuse` instead of `use`.
236+
With `reuse`, you can do this instead:
243237

244238
```text
245-
// common/index.dbml
239+
// management/index.dbml
240+
// You totally control this file
246241
reuse * from './users'
247-
reuse * from './orders'
248242
reuse * from './products'
249-
250-
// main.dbml
251-
use * from './common/index'
243+
reuse * from './orders'
252244
```
253245

254-
Consumers of `common/index.dbml` never need to know how the files are organized internally. If you later restructure the sub-files, only the barrel changes — not every consumer.
255-
256-
`reuse` works with selective imports and aliases too:
246+
Now, the consumers only have to import this barrel file:
257247

258248
```text
259-
reuse {
260-
table users
261-
} from './base'
262-
263-
reuse {
264-
table auth.users as u
265-
} from './auth'
249+
// Consumer's file
250+
use * from './management/index'
266251
```
267252

268-
## Circular Imports
253+
This way, you can reorganize your project as you wish, only needing to modify your own barrel file. Your consumers never need to be aware of your changes.
254+
255+
## Notes
269256

270-
Because DBML is declarative, files can freely reference each other without any issues.
257+
**`use` is not transitive** — If `a.dbml` imports `b.dbml` and `b.dbml` imports `c.dbml` via `use`, elements from `c.dbml` would not be available in `a.dbml`. Use [`reuse`](#re-exporting-with-reuse) if you need to pass elements through.
258+
259+
**Circular imports** — Because DBML is declarative, files can reference each other without any issues. For example, `users.dbml` can import from `orders.dbml` and vice versa.
271260

272261
```text
273262
// users.dbml
274-
use {
275-
table orders
276-
} from './orders'
263+
use { table orders } from './orders'
277264
278265
Table users {
279266
id int [pk]
280267
}
281268
282269
Ref: users.id < orders.user_id
270+
```
283271

272+
```text
284273
// orders.dbml
285-
use {
286-
table users
287-
} from './users'
274+
use { table users } from './users'
288275
289276
Table orders {
290277
id int [pk]
291-
user_id int
278+
user_id int [ref: > users.id]
292279
}
293280
```

0 commit comments

Comments
 (0)