Support attribute definitions for entities in ERDs

This commit is contained in:
Adrian Hall
2020-11-02 09:41:46 +00:00
parent 8870ba3b65
commit ae1880311e
6 changed files with 197 additions and 10 deletions

View File

@@ -108,7 +108,7 @@ const drawAttributes = (groupNode, entityTextNode, attributes) => {
// Add rectangular boxes for the attribute types/names
let heightOffset = labelBBox.height + heightPadding * 2; // Start at the bottom of the entity label
let attribStyle = 'attributeBox1'; // We will flip the style on alternate rows to achieve a banded effect
let attribStyle = 'attributeBoxOdd'; // We will flip the style on alternate rows to achieve a banded effect
attributeNodes.forEach(nodePair => {
// Calculate the alignment y co-ordinate for the type/name of the attribute
@@ -157,7 +157,7 @@ const drawAttributes = (groupNode, entityTextNode, attributes) => {
heightPadding * 2;
// Flip the attribute style for row banding
attribStyle = attribStyle == 'attributeBox1' ? 'attributeBox2' : 'attributeBox1';
attribStyle = attribStyle == 'attributeBoxOdd' ? 'attributeBoxEven' : 'attributeBoxOdd';
});
} else {
// Ensure the entity box is a decent size without any attributes

View File

@@ -80,6 +80,7 @@ statement
yy.addAttributes($1, $3);
/* console.log('handled block'); */
}
| entityName BLOCK_START BLOCK_STOP { yy.addEntity($1); }
| entityName { yy.addEntity($1); }
;

View File

@@ -33,6 +33,85 @@ describe('when parsing ER diagram it...', function() {
expect(entities.hasOwnProperty('CHARACTER_SET')).toBe(true);
});
it('should allow an entity with a single attribute to be defined', function() {
const entity = 'BOOK';
const attribute = 'string title';
erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute}\n}`);
const entities = erDb.getEntities();
expect(Object.keys(entities).length).toBe(1);
expect(entities[entity].attributes.length).toBe(1);
});
it('should allow an entity with multiple attributes to be defined', function() {
const entity = 'BOOK';
const attribute1 = 'string title';
const attribute2 = 'string author';
const attribute3 = 'float price';
erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute1}\n${attribute2}\n${attribute3}\n}`);
const entities = erDb.getEntities();
expect(entities[entity].attributes.length).toBe(3);
});
it('should allow attribute definitions to be split into multiple blocks', function() {
const entity = 'BOOK';
const attribute1 = 'string title';
const attribute2 = 'string author';
const attribute3 = 'float price';
erDiagram.parser.parse(`erDiagram\n${entity} {\n${attribute1}\n}\n${entity} {\n${attribute2}\n${attribute3}\n}`);
const entities = erDb.getEntities();
expect(entities[entity].attributes.length).toBe(3);
});
it('should allow an empty attribute block', function() {
const entity = 'BOOK';
erDiagram.parser.parse(`erDiagram\n${entity} {}`);
const entities = erDb.getEntities();
expect(entities.hasOwnProperty('BOOK')).toBe(true);
expect(entities[entity].attributes.length).toBe(0);
});
it('should allow an attribute block to start immediately after the entity name', function() {
const entity = 'BOOK';
erDiagram.parser.parse(`erDiagram\n${entity}{}`);
const entities = erDb.getEntities();
expect(entities.hasOwnProperty('BOOK')).toBe(true);
expect(entities[entity].attributes.length).toBe(0);
});
it('should allow an attribute block to be separated from the entity name by spaces', function() {
const entity = 'BOOK';
erDiagram.parser.parse(`erDiagram\n${entity} {}`);
const entities = erDb.getEntities();
expect(entities.hasOwnProperty('BOOK')).toBe(true);
expect(entities[entity].attributes.length).toBe(0);
});
it('should allow whitespace before and after attribute definitions', function() {
const entity = 'BOOK';
const attribute = 'string title';
erDiagram.parser.parse(`erDiagram\n${entity} {\n \n\n ${attribute}\n\n \n}`);
const entities = erDb.getEntities();
expect(Object.keys(entities).length).toBe(1);
expect(entities[entity].attributes.length).toBe(1);
});
it('should allow no whitespace before and after attribute definitions', function() {
const entity = 'BOOK';
const attribute = 'string title';
erDiagram.parser.parse(`erDiagram\n${entity}{${attribute}}`);
const entities = erDb.getEntities();
expect(Object.keys(entities).length).toBe(1);
expect(entities[entity].attributes.length).toBe(1);
});
it('should associate two entities correctly', function() {
erDiagram.parser.parse('erDiagram\nCAR ||--o{ DRIVER : "insured for"');
const entities = erDb.getEntities();

View File

@@ -5,12 +5,12 @@ const getStyles = options =>
stroke: ${options.nodeBorder};
}
.attributeBox1 {
.attributeBoxOdd {
fill: #ffffff;
stroke: ${options.nodeBorder};
}
.attributeBox2 {
.attributeBoxEven {
fill: #f2f2f2;
stroke: ${options.nodeBorder};
}