# ERD syntax

## Entities

Entities correspond to database tables or similar. Entities contain attributes.

Entity definitions consist of a name followed by `{ }`. For example, `users` is the name of the below entity and it contains attributes `id` and `displayName`.

```eraser
users {
id string
displayName string
}
```

<Figure src="/assets/5f7c92b-image.png" alt="" width="225px" />

<br />

It is possible for entities to contain nothing like the below.

```eraser
users { }
```

<Figure src="/assets/2f9153e-image.png" alt="" width="225px" />

Entity names are required to be unique.

## Attributes

Attributes correspond to database table columns or similar.

Attribute definitions occur within an entity definition. They consist of a `name`, `type` (optional), and `metadata` (optional) delimited by the space character. Here is an example:

```eraser
users {
id string pk
}
```

<Figure src="/assets/167bdc2-image.png" alt="" width="225px" />

Outside of a definition (e.g. in a relationship statement), attributes are referred to following the entity that they belong to, separated by a `.`. Here is an example:

```eraser
users.teamId > teams.id
```

<br />

## Properties

Properties are key-value pairs enclosed in `[ ]` brackets that can be appended to entity definitions. Properties are optional.

It is possible to set multiple properties like shown below:

```eraser
users [icon: user, color: blue] {
 // ...rows
}
```

<Figure src="/assets/8e957c8-image.png" alt="" width="550px" />

<br />

Here are the properties that are allowed:

| Property    | Description                          | Value                                                                   | Default value |
| :---------- | :----------------------------------- | :---------------------------------------------------------------------- | :------------ |
| `icon`      | Attached icons                       | Icon names (e.g. `aws-ec2`). See [Icons](/icons) page for full list. |               |
| `color`     | Stroke and fill color, when possible | Color name (e.g. `blue`) or hex code (e.g. `#000000`)                   |               |
| `colorMode` | Fill color lightness                 | `pastel`, `bold`, `outline`                                             | `pastel`      |
| `styleMode` | Embellishments                       | `shadow`, `plain`, `watercolor`                                         | `shadow`      |
| `typeface`  | Text typeface                        | `rough`, `clean`, `mono`                                                | `rough`       |

Here are the lists of icon names:

* [General Icons](/icons#general-icons)
* [Tech Logos](/icons#tech-logos)
* [AWS Icons](/icons#aws-icons)
* [Google Cloud Icons](/icons#google-cloud-icons)
* [Azure Icons](/icons#azure-icons)

Refer to [Styling](/styling) for more details and examples on the `colorMode`, `styleMode`, and `typeface` properties.

## Relationships

Relationships show the attribute-level relations between entities.

Here is an example:

```eraser
users.teamId > teams.id
```

<Figure src="/assets/4d44cf6-image.png" alt="" width="550px" />

It is possible to show omit the attribute-level and simply show entity-level relations like this:

```eraser
users > teams
```

<Figure src="/assets/107892f-image.png" alt="" width="500px" />

The type of connecting line represents the cardinality between the two entities. Here are the types:

| Connector | Syntax | Description |
| --- | --- | --- |
| ![](/assets/07b00c8-one-to-many.svg) | `<` | One-to-many |
| ![](/assets/9fb1ca3-many-to-one.svg) | `>` | Many-to-one |
| ![](/assets/5468d1b-one-to-one.svg) | `-` | One-to-one |
| ![](/assets/965c3c4-many-to-many.svg) | `<>` | Many-to-many |

If a relationship statement contains a name that has not been previously defined as an entity or attribute, an entity or attribute with that name will be created.

Here are the properties that are allowed on relationships (lines):

| Property | Description | Example                                  |
| :------- | :---------- | :--------------------------------------- |
| `color`  | Line color  | `users.teamId > teams.id [color: green]` |

## Icons

Here's a [list of all the icons you can use with diagram-as-code](/icons).

## Escape string

Certain reserved characters are not allowed in entity or attribute names. However, you can still use these characters by wrapping the entire entity or attribute name in quotes `" "`.

```eraser
"CI/CD" [icon: gear] {
    id string pk
}
```

## Styling

Styles can be applied at the diagram level. Below is an overview of the options and syntax. Refer to [Styling](/styling) for more details and examples.

| Property    | Values                          | Default value | Syntax example        |
| :---------- | :------------------------------ | :------------ | :-------------------- |
| `colorMode` | `pastel`, `bold`, `outline`     | `pastel`      | `colorMode bold`      |
| `styleMode` | `shadow`, `plain`, `watercolor` | `shadow`      | `styleMode shadow`    |
| `typeface`  | `rough`, `clean`, `mono`        | `rough`       | `typeface clean`      |
| `notation`  | `chen`, `crows-feet`            | `chen`        | `notation crows-feet` |

## Legends

Legends map a swatch (connection, icon, shape, or color) to a label. Swatches are not tied to actual elements in the diagram.

```eraser
 legend {                                        
    [connection: -->, label: Async]            
    [color: red, label: Error]
    [icon: aws-lambda, label: Lambda]
    [shape: diamond, label: Decision]
  }
```

### Legend properties

| Property | Description                          | Value                                       | Default Value |
| :------- | :----------------------------------- | :------------------------------------------ | :------------ |
| position | Position of the legend on the canvas | Position names (e.g. `top-left` or `right`) | `top-right`   |

```eraser
  legend [position: bottom-left] {  
    [color: red, label: Error]  
  }
```

Here is the list of positions:

`top-left`, `top-right` (default), `bottom-left`, `bottom-right`, `top`, `bottom`, `left`, `right`

### Legend item properties

Each legend item is enclosed in `[ ]` and must include a `label` and at least one swatch property.

| Property   | Description       | Value                                                         |
| :--------- | :---------------- | :------------------------------------------------------------ |
| label      | Text label        | Any string. Enclose in double quotes if containing a space.   |
| connection | Connection swatch | Connection types (e.g. `-->` or `<>`). See Connections above. |
| color      | Color swatch      | Color name (e.g. `blue`) or hex code (e.g. `"#000000"`)       |
| icon       | Icon swatch       | Icon names (e.g. `aws-ec2`). See Icons page for full list.    |
| shape      | Shape swatch      | Shape names (e.g. `diamond` or `oval`).                       |

`color` can be combined with `connection` or `shape`. Other swatch types are mutually exclusive.

<br />

```eraser
legend {  
    [connection: -->, color: orange, label: Async backup]  
    [shape: rectangle, color: blue, label: Active]  
}
```

<br />
