Skip to content

Commit a6f097d

Browse files
committed
feat: support optional ER attribute types
1 parent 9bae92c commit a6f097d

5 files changed

Lines changed: 67 additions & 1 deletion

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'mermaid': minor
3+
---
4+
5+
feat(er): support optional ER attribute types with a `?` suffix

‎docs/syntax/entityRelationshipDiagram.md‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,28 @@ erDiagram
247247

248248
The `type` values must begin with an alphabetic character and may contain digits, hyphens, underscores, parentheses and square brackets. The `name` values follow a similar format to `type`, but may start with an asterisk as another option to indicate an attribute is a primary key. Other than that, there are no restrictions, and there is no implicit set of valid data types.
249249

250+
#### Optional attribute types (v\<MERMAID_RELEASE_VERSION>+)
251+
252+
Attribute `type` values may end with `?` to indicate an optional or nullable type.
253+
254+
```mermaid-example
255+
erDiagram
256+
PERSON {
257+
string firstName
258+
string? middleName
259+
string lastName
260+
}
261+
```
262+
263+
```mermaid
264+
erDiagram
265+
PERSON {
266+
string firstName
267+
string? middleName
268+
string lastName
269+
}
270+
```
271+
250272
### Entity Name Aliases
251273

252274
An alias can be added to an entity using square brackets. If provided, the alias will be showed in the diagram instead of the entity name. Alias names follow all of the same rules as entity names.

‎packages/mermaid/src/diagrams/er/parser/erDiagram.jison‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ attribute
259259

260260
attributeType
261261
: ATTRIBUTE_WORD { $$=$1; }
262+
| ATTRIBUTE_WORD '?' { $$=$1 + $2; }
262263
;
263264

264265
attributeName
@@ -305,4 +306,4 @@ role
305306
| 'UNICODE_TEXT' { $$ = $1; }
306307
;
307308

308-
%%
309+
%%

‎packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,31 @@ describe('when parsing ER diagram it...', function () {
266266
expect(entities.get(entity).attributes[0].type).toBe('my.type');
267267
expect(entities.get(entity).attributes[0].name).toBe('address');
268268
});
269+
270+
it('should allow a question mark suffix on an attribute type', function () {
271+
const entity = 'PERSON';
272+
const attribute = 'string? alias';
273+
274+
erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute}\n}`);
275+
const entities = erDb.getEntities();
276+
expect(entities.size).toBe(1);
277+
expect(entities.get(entity).attributes.length).toBe(1);
278+
expect(entities.get(entity).attributes[0].type).toBe('string?');
279+
expect(entities.get(entity).attributes[0].name).toBe('alias');
280+
});
281+
282+
it('should allow a question mark suffix on an attribute type with a key and comment', function () {
283+
const entity = 'PERSON';
284+
const attribute = 'string? alias UK "optional display name"';
285+
286+
erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute}\n}`);
287+
const entities = erDb.getEntities();
288+
expect(entities.size).toBe(1);
289+
expect(entities.get(entity).attributes.length).toBe(1);
290+
expect(entities.get(entity).attributes[0].type).toBe('string?');
291+
expect(entities.get(entity).attributes[0].keys).toEqual(['UK']);
292+
expect(entities.get(entity).attributes[0].comment).toBe('optional display name');
293+
});
269294
});
270295

271296
it('should allow an entity with a single attribute to be defined', function () {

‎packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,19 @@ erDiagram
173173

174174
The `type` values must begin with an alphabetic character and may contain digits, hyphens, underscores, parentheses and square brackets. The `name` values follow a similar format to `type`, but may start with an asterisk as another option to indicate an attribute is a primary key. Other than that, there are no restrictions, and there is no implicit set of valid data types.
175175

176+
#### Optional attribute types (v<MERMAID_RELEASE_VERSION>+)
177+
178+
Attribute `type` values may end with `?` to indicate an optional or nullable type.
179+
180+
```mermaid-example
181+
erDiagram
182+
PERSON {
183+
string firstName
184+
string? middleName
185+
string lastName
186+
}
187+
```
188+
176189
### Entity Name Aliases
177190

178191
An alias can be added to an entity using square brackets. If provided, the alias will be showed in the diagram instead of the entity name. Alias names follow all of the same rules as entity names.

0 commit comments

Comments
 (0)