Skip to content

Commit ed819e9

Browse files
authored
Merge pull request #4758 from tomperr/feat/4746_add_er_alias
feat(er): add entity name alias
2 parents 979dcb0 + 9fa6dc2 commit ed819e9

File tree

8 files changed

+127
-6
lines changed

8 files changed

+127
-6
lines changed

cypress/integration/rendering/erDiagram.spec.js

+17
Original file line numberDiff line numberDiff line change
@@ -305,4 +305,21 @@ ORDER ||--|{ LINE-ITEM : contains
305305
{}
306306
);
307307
});
308+
309+
it('should render entities with entity name aliases', () => {
310+
imgSnapshotTest(
311+
`
312+
erDiagram
313+
p[Person] {
314+
varchar(64) firstName
315+
varchar(64) lastName
316+
}
317+
c["Customer Account"] {
318+
varchar(128) email
319+
}
320+
p ||--o| c : has
321+
`,
322+
{ logLevel: 1 }
323+
);
324+
});
308325
});

demos/er.html

+14
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@
110110
}
111111
MANUFACTURER only one to zero or more CAR : makes
112112
</pre>
113+
<hr />
114+
115+
<pre class="mermaid">
116+
erDiagram
117+
p[Person] {
118+
string firstName
119+
string lastName
120+
}
121+
a["Customer Account"] {
122+
string email
123+
}
124+
p ||--o| a : has
125+
</pre>
126+
<hr />
113127

114128
<script type="module">
115129
import mermaid from './mermaid.esm.mjs';

docs/syntax/entityRelationshipDiagram.md

+28
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,34 @@ erDiagram
198198

199199
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.
200200

201+
### Entity Name Aliases (v\<MERMAID_RELEASE_VERSION>+)
202+
203+
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.
204+
205+
```mermaid-example
206+
erDiagram
207+
p[Person] {
208+
string firstName
209+
string lastName
210+
}
211+
a["Customer Account"] {
212+
string email
213+
}
214+
p ||--o| a : has
215+
```
216+
217+
```mermaid
218+
erDiagram
219+
p[Person] {
220+
string firstName
221+
string lastName
222+
}
223+
a["Customer Account"] {
224+
string email
225+
}
226+
p ||--o| a : has
227+
```
228+
201229
#### Attribute Keys and Comments
202230

203231
Attributes may also have a `key` or comment defined. Keys can be `PK`, `FK` or `UK`, for Primary Key, Foreign Key or Unique Key. To specify multiple key constraints on a single attribute, separate them with a comma (e.g., `PK, FK`).. A `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.

packages/mermaid/src/diagrams/er/erDb.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,13 @@ export const parseDirective = function (statement, context, type) {
3232
mermaidAPI.parseDirective(this, statement, context, type);
3333
};
3434

35-
const addEntity = function (name) {
35+
const addEntity = function (name, alias = undefined) {
3636
if (entities[name] === undefined) {
37-
entities[name] = { attributes: [] };
37+
entities[name] = { attributes: [], alias: alias };
3838
log.info('Added new entity :', name);
39+
} else if (entities[name] && !entities[name].alias && alias) {
40+
entities[name].alias = alias;
41+
log.info(`Add alias '${alias}' to entity '${name}'`);
3942
}
4043

4144
return entities[name];

packages/mermaid/src/diagrams/er/erRenderer.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ const drawEntities = function (svgNode, entities, graph) {
326326
.style('text-anchor', 'middle')
327327
.style('font-family', getConfig().fontFamily)
328328
.style('font-size', conf.fontSize + 'px')
329-
.text(entityName);
329+
.text(entities[entityName].alias ?? entityName);
330330

331331
const { width: entityWidth, height: entityHeight } = drawAttributes(
332332
groupNode,

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

+9-3
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili
3535
<block>[\n]+ /* nothing */
3636
<block>"}" { this.popState(); return 'BLOCK_STOP'; }
3737
<block>. return yytext[0];
38+
"[" return 'SQS';
39+
"]" return 'SQE';
3840

3941
"one or zero" return 'ZERO_OR_ONE';
4042
"one or more" return 'ONE_OR_MORE';
@@ -102,17 +104,21 @@ statement
102104
yy.addEntity($1);
103105
yy.addEntity($3);
104106
yy.addRelationship($1, $5, $3, $2);
105-
/*console.log($1 + $2 + $3 + ':' + $5);*/
106107
}
107108
| entityName BLOCK_START attributes BLOCK_STOP
108109
{
109-
/* console.log('detected block'); */
110110
yy.addEntity($1);
111111
yy.addAttributes($1, $3);
112-
/* console.log('handled block'); */
113112
}
114113
| entityName BLOCK_START BLOCK_STOP { yy.addEntity($1); }
115114
| entityName { yy.addEntity($1); }
115+
| entityName SQS entityName SQE BLOCK_START attributes BLOCK_STOP
116+
{
117+
yy.addEntity($1, $3);
118+
yy.addAttributes($1, $6);
119+
}
120+
| entityName SQS entityName SQE BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); }
121+
| entityName SQS entityName SQE { yy.addEntity($1, $3); }
116122
| title title_value { $$=$2.trim();yy.setAccTitle($$); }
117123
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
118124
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }

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

+37
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,43 @@ describe('when parsing ER diagram it...', function () {
133133
const entities = erDb.getEntities();
134134
expect(entities.hasOwnProperty(hyphensUnderscore)).toBe(true);
135135
});
136+
137+
it('can have an alias', function () {
138+
const entity = 'foo';
139+
const alias = 'bar';
140+
erDiagram.parser.parse(`erDiagram\n${entity}["${alias}"]\n`);
141+
const entities = erDb.getEntities();
142+
expect(entities.hasOwnProperty(entity)).toBe(true);
143+
expect(entities[entity].alias).toBe(alias);
144+
});
145+
146+
it('can have an alias even if the relationship is defined before class', function () {
147+
const firstEntity = 'foo';
148+
const secondEntity = 'bar';
149+
const alias = 'batman';
150+
erDiagram.parser.parse(
151+
`erDiagram\n${firstEntity} ||--o| ${secondEntity} : rel\nclass ${firstEntity}["${alias}"]\n`
152+
);
153+
const entities = erDb.getEntities();
154+
expect(entities.hasOwnProperty(firstEntity)).toBe(true);
155+
expect(entities.hasOwnProperty(secondEntity)).toBe(true);
156+
expect(entities[firstEntity].alias).toBe(alias);
157+
expect(entities[secondEntity].alias).toBeUndefined();
158+
});
159+
160+
it('can have an alias even if the relationship is defined after class', function () {
161+
const firstEntity = 'foo';
162+
const secondEntity = 'bar';
163+
const alias = 'batman';
164+
erDiagram.parser.parse(
165+
`erDiagram\nclass ${firstEntity}["${alias}"]\n${firstEntity} ||--o| ${secondEntity} : rel\n`
166+
);
167+
const entities = erDb.getEntities();
168+
expect(entities.hasOwnProperty(firstEntity)).toBe(true);
169+
expect(entities.hasOwnProperty(secondEntity)).toBe(true);
170+
expect(entities[firstEntity].alias).toBe(alias);
171+
expect(entities[secondEntity].alias).toBeUndefined();
172+
});
136173
});
137174

138175
describe('attribute name', () => {

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

+16
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,22 @@ erDiagram
144144

145145
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.
146146

147+
### Entity Name Aliases (v<MERMAID_RELEASE_VERSION>+)
148+
149+
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.
150+
151+
```mermaid-example
152+
erDiagram
153+
p[Person] {
154+
string firstName
155+
string lastName
156+
}
157+
a["Customer Account"] {
158+
string email
159+
}
160+
p ||--o| a : has
161+
```
162+
147163
#### Attribute Keys and Comments
148164

149165
Attributes may also have a `key` or comment defined. Keys can be `PK`, `FK` or `UK`, for Primary Key, Foreign Key or Unique Key. To specify multiple key constraints on a single attribute, separate them with a comma (e.g., `PK, FK`).. A `comment` is defined by double quotes at the end of an attribute. Comments themselves cannot have double-quote characters in them.

0 commit comments

Comments
 (0)