mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-11-15 10:14:21 +01:00
feat(er): add entity name alias
This commit is contained in:
@@ -108,6 +108,20 @@
|
|||||||
}
|
}
|
||||||
MANUFACTURER only one to zero or more CAR : makes
|
MANUFACTURER only one to zero or more CAR : makes
|
||||||
</pre>
|
</pre>
|
||||||
|
<hr />
|
||||||
|
|
||||||
|
<pre class="mermaid">
|
||||||
|
erDiagram
|
||||||
|
p as Person {
|
||||||
|
string firstName
|
||||||
|
string lastName
|
||||||
|
}
|
||||||
|
a as "Customer Account" {
|
||||||
|
string email
|
||||||
|
}
|
||||||
|
p ||--o| a : has
|
||||||
|
</pre>
|
||||||
|
<hr />
|
||||||
|
|
||||||
<script type="module">
|
<script type="module">
|
||||||
import mermaid from './mermaid.esm.mjs';
|
import mermaid from './mermaid.esm.mjs';
|
||||||
|
|||||||
@@ -198,6 +198,34 @@ erDiagram
|
|||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
### Entity Name Aliases
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
```mermaid-example
|
||||||
|
erDiagram
|
||||||
|
p as Person {
|
||||||
|
string firstName
|
||||||
|
string lastName
|
||||||
|
}
|
||||||
|
a as "Customer Account" {
|
||||||
|
string email
|
||||||
|
}
|
||||||
|
p ||--o| a : has
|
||||||
|
```
|
||||||
|
|
||||||
|
```mermaid
|
||||||
|
erDiagram
|
||||||
|
p as Person {
|
||||||
|
string firstName
|
||||||
|
string lastName
|
||||||
|
}
|
||||||
|
a as "Customer Account" {
|
||||||
|
string email
|
||||||
|
}
|
||||||
|
p ||--o| a : has
|
||||||
|
```
|
||||||
|
|
||||||
#### Attribute Keys and Comments
|
#### Attribute Keys and Comments
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|||||||
@@ -32,9 +32,9 @@ export const parseDirective = function (statement, context, type) {
|
|||||||
mermaidAPI.parseDirective(this, statement, context, type);
|
mermaidAPI.parseDirective(this, statement, context, type);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addEntity = function (name) {
|
const addEntity = function (name, alias = undefined) {
|
||||||
if (entities[name] === undefined) {
|
if (entities[name] === undefined) {
|
||||||
entities[name] = { attributes: [] };
|
entities[name] = { attributes: [], alias: alias };
|
||||||
log.info('Added new entity :', name);
|
log.info('Added new entity :', name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -326,7 +326,7 @@ const drawEntities = function (svgNode, entities, graph) {
|
|||||||
.style('text-anchor', 'middle')
|
.style('text-anchor', 'middle')
|
||||||
.style('font-family', getConfig().fontFamily)
|
.style('font-family', getConfig().fontFamily)
|
||||||
.style('font-size', conf.fontSize + 'px')
|
.style('font-size', conf.fontSize + 'px')
|
||||||
.text(entityName);
|
.text(entities[entityName].alias || entityName);
|
||||||
|
|
||||||
const { width: entityWidth, height: entityHeight } = drawAttributes(
|
const { width: entityWidth, height: entityHeight } = drawAttributes(
|
||||||
groupNode,
|
groupNode,
|
||||||
|
|||||||
@@ -62,6 +62,7 @@ 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';
|
||||||
@@ -113,6 +114,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
|
||||||
|
{
|
||||||
|
/* console.log('detected block'); */
|
||||||
|
yy.addEntity($1, $3);
|
||||||
|
yy.addAttributes($1, $5);
|
||||||
|
/* console.log('handled block'); */
|
||||||
|
}
|
||||||
|
| entityName ALIAS entityName BLOCK_START BLOCK_STOP { yy.addEntity($1, $3); }
|
||||||
|
| entityName ALIAS entityName { 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($$); }
|
||||||
|
|||||||
@@ -133,6 +133,15 @@ describe('when parsing ER diagram it...', function () {
|
|||||||
const entities = erDb.getEntities();
|
const entities = erDb.getEntities();
|
||||||
expect(entities.hasOwnProperty(hyphensUnderscore)).toBe(true);
|
expect(entities.hasOwnProperty(hyphensUnderscore)).toBe(true);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can have an alias', function () {
|
||||||
|
const entity = 'foo';
|
||||||
|
const alias = 'bar';
|
||||||
|
erDiagram.parser.parse(`erDiagram\n${entity} as "${alias}"\n`);
|
||||||
|
const entities = erDb.getEntities();
|
||||||
|
expect(entities.hasOwnProperty(entity)).toBe(true);
|
||||||
|
expect(entities[entity].alias).toBe(alias);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('attribute name', () => {
|
describe('attribute name', () => {
|
||||||
|
|||||||
@@ -144,6 +144,22 @@ erDiagram
|
|||||||
|
|
||||||
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.
|
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.
|
||||||
|
|
||||||
|
### Entity Name Aliases
|
||||||
|
|
||||||
|
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.
|
||||||
|
|
||||||
|
```mermaid-example
|
||||||
|
erDiagram
|
||||||
|
p as Person {
|
||||||
|
string firstName
|
||||||
|
string lastName
|
||||||
|
}
|
||||||
|
a as "Customer Account" {
|
||||||
|
string email
|
||||||
|
}
|
||||||
|
p ||--o| a : has
|
||||||
|
```
|
||||||
|
|
||||||
#### Attribute Keys and Comments
|
#### Attribute Keys and Comments
|
||||||
|
|
||||||
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.
|
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.
|
||||||
|
|||||||
Reference in New Issue
Block a user