From 4dadf8a72da15f2236c26989c1a9f55b917f8025 Mon Sep 17 00:00:00 2001 From: Maho Pacheco Date: Tue, 6 Dec 2022 10:38:21 -0800 Subject: [PATCH 1/3] Adding support for parentheses --- cypress/integration/rendering/erDiagram.spec.js | 14 ++++++++++++++ docs/syntax/entityRelationshipDiagram.md | 8 +++++--- .../mermaid/src/diagrams/er/parser/erDiagram.jison | 2 +- .../src/docs/syntax/entityRelationshipDiagram.md | 5 +++-- 4 files changed, 23 insertions(+), 6 deletions(-) diff --git a/cypress/integration/rendering/erDiagram.spec.js b/cypress/integration/rendering/erDiagram.spec.js index 8e8946170..c72df49b6 100644 --- a/cypress/integration/rendering/erDiagram.spec.js +++ b/cypress/integration/rendering/erDiagram.spec.js @@ -182,6 +182,20 @@ describe('Entity Relationship Diagram', () => { cy.get('svg'); }); + it('should render entities with length in attributes type', () => { + renderGraph( + ` + erDiagram + CLUSTER { + varchar(99) name + string(255) description + } + `, + { logLevel: 1 } + ); + cy.get('svg'); + }); + it('should render entities and attributes with big and small entity names', () => { renderGraph( ` diff --git a/docs/syntax/entityRelationshipDiagram.md b/docs/syntax/entityRelationshipDiagram.md index 9b938bc36..4f33b1edc 100644 --- a/docs/syntax/entityRelationshipDiagram.md +++ b/docs/syntax/entityRelationshipDiagram.md @@ -230,7 +230,7 @@ erDiagram } ``` -The `type` and `name` values must begin with an alphabetic character and may contain digits, hyphens or underscores. Other than that, there are no restrictions, and there is no implicit set of valid data types. +The `type` and `name` values must begin with an alphabetic character and may contain digits, hyphens, underscores, parentheses and square brackets. Other than that, there are no restrictions, and there is no implicit set of valid data types. #### Attribute Keys and Comments @@ -244,11 +244,12 @@ erDiagram string registrationNumber string make string model + string[] parts } PERSON ||--o{ NAMED-DRIVER : is PERSON { string driversLicense PK "The license #" - string firstName + string(99) firstName "Only 99 characters are allowed" string lastName int age } @@ -263,11 +264,12 @@ erDiagram string registrationNumber string make string model + string[] parts } PERSON ||--o{ NAMED-DRIVER : is PERSON { string driversLicense PK "The license #" - string firstName + string(99) firstName "Only 99 characters are allowed" string lastName int age } diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison index f0411fd72..bd8dc20f0 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.jison +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.jison @@ -31,7 +31,7 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multili \s+ /* skip whitespace in block */ \b((?:PK)|(?:FK))\b return 'ATTRIBUTE_KEY' (.*?)[~](.*?)*[~] return 'ATTRIBUTE_WORD'; -[A-Za-z][A-Za-z0-9\-_\[\]]* return 'ATTRIBUTE_WORD' +[A-Za-z][A-Za-z0-9\-_\[\]\(\)]* return 'ATTRIBUTE_WORD' \"[^"]*\" return 'COMMENT'; [\n]+ /* nothing */ "}" { this.popState(); return 'BLOCK_STOP'; } diff --git a/packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md b/packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md index c666877c5..d83fd4b09 100644 --- a/packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md +++ b/packages/mermaid/src/docs/syntax/entityRelationshipDiagram.md @@ -160,7 +160,7 @@ erDiagram } ``` -The `type` and `name` values must begin with an alphabetic character and may contain digits, hyphens or underscores. Other than that, there are no restrictions, and there is no implicit set of valid data types. +The `type` and `name` values must begin with an alphabetic character and may contain digits, hyphens, underscores, parentheses and square brackets. Other than that, there are no restrictions, and there is no implicit set of valid data types. #### Attribute Keys and Comments @@ -174,11 +174,12 @@ erDiagram string registrationNumber string make string model + string[] parts } PERSON ||--o{ NAMED-DRIVER : is PERSON { string driversLicense PK "The license #" - string firstName + string(99) firstName "Only 99 characters are allowed" string lastName int age } From 07d8684fc76c6ffdbe77e8652d3de026bfa7b24c Mon Sep 17 00:00:00 2001 From: Maho Pacheco Date: Sun, 11 Dec 2022 19:34:25 -0800 Subject: [PATCH 2/3] Adding size to postal code --- demos/er.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/demos/er.html b/demos/er.html index 06fbf020e..124311a03 100644 --- a/demos/er.html +++ b/demos/er.html @@ -40,7 +40,7 @@ erDiagram string city string region string state - string postal_code + string(5) postal_code string country } From 6fb9b3b35367719e75ca61ade50991ab2fd9eb6a Mon Sep 17 00:00:00 2001 From: Maho Pacheco Date: Sun, 11 Dec 2022 19:45:05 -0800 Subject: [PATCH 3/3] Adding UTs for attributes with variable length in er diagram --- .../src/diagrams/er/parser/erDiagram.spec.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js index eb738fe4b..06e2d7993 100644 --- a/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js +++ b/packages/mermaid/src/diagrams/er/parser/erDiagram.spec.js @@ -214,6 +214,19 @@ describe('when parsing ER diagram it...', function () { expect(entities[entity].attributes.length).toBe(2); }); + it('should allow an entity with attribute that is a limited length string', function () { + const entity = 'BOOK'; + const attribute1 = 'character(10) isbn FK'; + const attribute2 = 'varchar(5) postal_code "Five digits"'; + + erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute1}\n${attribute2}\n}`); + const entities = erDb.getEntities(); + expect(Object.keys(entities).length).toBe(1); + expect(entities[entity].attributes.length).toBe(2); + expect(entities[entity].attributes[0].attributeType).toBe('character(10)'); + expect(entities[entity].attributes[1].attributeType).toBe('varchar(5)'); + }); + it('should allow an entity with multiple attributes to be defined', function () { const entity = 'BOOK'; const attribute1 = 'string title';