feat(er): use square brackets to add aliases

This commit is contained in:
Tom PERRILLAT-COLLOMB
2023-08-22 11:05:09 +02:00
parent cc8b4572d5
commit a7ae1b6890
5 changed files with 20 additions and 19 deletions

View File

@@ -112,11 +112,11 @@
<pre class="mermaid"> <pre class="mermaid">
erDiagram erDiagram
p as Person { p[Person] {
string firstName string firstName
string lastName string lastName
} }
a as "Customer Account" { a["Customer Account"] {
string email string email
} }
p ||--o| a : has p ||--o| a : has

View File

@@ -200,15 +200,15 @@ The `type` values must begin with an alphabetic character and may contain digits
### Entity Name Aliases (v\<MERMAID_RELEASE_VERSION>+) ### Entity Name Aliases (v\<MERMAID_RELEASE_VERSION>+)
An alias can be added to an entity using `as` keyword. If provided, the alias will be showed in the diagram instead of the entity name. 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.
```mermaid-example ```mermaid-example
erDiagram erDiagram
p as Person { p[Person] {
string firstName string firstName
string lastName string lastName
} }
a as "Customer Account" { a["Customer Account"] {
string email string email
} }
p ||--o| a : has p ||--o| a : has
@@ -216,11 +216,11 @@ erDiagram
```mermaid ```mermaid
erDiagram erDiagram
p as Person { p[Person] {
string firstName string firstName
string lastName string lastName
} }
a as "Customer Account" { a["Customer Account"] {
string email string email
} }
p ||--o| a : has p ||--o| a : has

View File

@@ -35,6 +35,8 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili
<block>[\n]+ /* nothing */ <block>[\n]+ /* nothing */
<block>"}" { this.popState(); return 'BLOCK_STOP'; } <block>"}" { this.popState(); return 'BLOCK_STOP'; }
<block>. return yytext[0]; <block>. return yytext[0];
"[" return 'SQS';
"]" return 'SQE';
"one or zero" return 'ZERO_OR_ONE'; "one or zero" return 'ZERO_OR_ONE';
"one or more" return 'ONE_OR_MORE'; "one or more" return 'ONE_OR_MORE';
@@ -62,7 +64,6 @@ o\{ return 'ZERO_OR_MORE';
\-\- return 'IDENTIFYING'; \-\- return 'IDENTIFYING';
"to" return 'IDENTIFYING'; "to" return 'IDENTIFYING';
"optionally to" return 'NON_IDENTIFYING'; "optionally to" return 'NON_IDENTIFYING';
"as" return 'ALIAS';
\.\- return 'NON_IDENTIFYING'; \.\- return 'NON_IDENTIFYING';
\-\. return 'NON_IDENTIFYING'; \-\. return 'NON_IDENTIFYING';
[A-Za-z][A-Za-z0-9\-_]* return 'ALPHANUM'; [A-Za-z][A-Za-z0-9\-_]* return 'ALPHANUM';
@@ -114,15 +115,15 @@ statement
} }
| entityName BLOCK_START BLOCK_STOP { yy.addEntity($1); } | entityName BLOCK_START BLOCK_STOP { yy.addEntity($1); }
| entityName { yy.addEntity($1); } | entityName { yy.addEntity($1); }
| entityName ALIAS entityName BLOCK_START attributes BLOCK_STOP | entityName SQS entityName SQE BLOCK_START attributes BLOCK_STOP
{ {
/* console.log('detected block'); */ /* console.log('detected block'); */
yy.addEntity($1, $3); yy.addEntity($1, $3);
yy.addAttributes($1, $5); yy.addAttributes($1, $6);
/* console.log('handled block'); */ /* console.log('handled block'); */
} }
| entityName ALIAS entityName BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); } | entityName SQS entityName SQE BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); }
| entityName ALIAS entityName { yy.addEntity($1, $3); } | entityName SQS entityName SQE { yy.addEntity($1, $3); }
| title title_value { $$=$2.trim();yy.setAccTitle($$); } | title title_value { $$=$2.trim();yy.setAccTitle($$); }
| acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); } | acc_title acc_title_value { $$=$2.trim();yy.setAccTitle($$); }
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); } | acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }

View File

@@ -137,7 +137,7 @@ describe('when parsing ER diagram it...', function () {
it('can have an alias', function () { it('can have an alias', function () {
const entity = 'foo'; const entity = 'foo';
const alias = 'bar'; const alias = 'bar';
erDiagram.parser.parse(`erDiagram\n${entity} as "${alias}"\n`); erDiagram.parser.parse(`erDiagram\n${entity}["${alias}"]\n`);
const entities = erDb.getEntities(); const entities = erDb.getEntities();
expect(entities.hasOwnProperty(entity)).toBe(true); expect(entities.hasOwnProperty(entity)).toBe(true);
expect(entities[entity].alias).toBe(alias); expect(entities[entity].alias).toBe(alias);

View File

@@ -146,15 +146,15 @@ The `type` values must begin with an alphabetic character and may contain digits
### Entity Name Aliases (v<MERMAID_RELEASE_VERSION>+) ### Entity Name Aliases (v<MERMAID_RELEASE_VERSION>+)
An alias can be added to an entity using `as` keyword. If provided, the alias will be showed in the diagram instead of the entity name. 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.
```mermaid-example ```mermaid-example
erDiagram erDiagram
p as Person { p[Person] {
string firstName string firstName
string lastName string lastName
} }
a as "Customer Account" { a["Customer Account"] {
string email string email
} }
p ||--o| a : has p ||--o| a : has