D2 1. Getting Started - swkim0128/PARA GitHub Wiki


type: D2 archive: false

Install

Install script

Install from source

Hello World

Shape

Basics

Example

Connections

Basics

Connection labels

Connections must reference a shape's key, not its label.

Example

Repeated connections

Connection chaining

Cycles are okay

Arrowheads

Containers

Nested syntax

Container labels

  1. Shorthand container labels

  2. Reserved keyword label

Example

Reference parent

Install


Install script

The recommended way to install is to run our install script, which will figure out the best way to install based on your machine. E.g. if D2 is available through a package manager installed, it will use that package manager.

๊ถŒ์žฅํ•˜๋Š” ์„ค์น˜ ๋ฐฉ๋ฒ•์€ ์šฐ๋ฆฌ์˜ ์„ค์น˜ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ์Šคํฌ๋ฆฝํŠธ๋Š” ๋‹น์‹ ์˜ ์ปดํ“จํ„ฐ์— ๋”ฐ๋ผ ๊ฐ€์žฅ ์ ํ•ฉํ•œ ์„ค์น˜ ๋ฐฉ๋ฒ•์„ ์ฐพ์•„์ค๋‹ˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, D2๊ฐ€ ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €๋ฅผ ํ†ตํ•ด ์„ค์น˜ ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์—๋Š” ํ•ด๋‹น ํŒจํ‚ค์ง€ ๋งค๋‹ˆ์ €๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

# With --dry-run the install script will print the commands it will use
# to install without actually installing so you know what it's going to do.
curl -fsSL https://d2lang.com/install.sh | sh -s -- --dry-run
# If things look good, install for real.
curl -fsSL https://d2lang.com/install.sh | sh -s --

Follow the instructions, if any. Test your installation was successful by running d2 version.

If you want to uninstall:

curl -fsSL https://d2lang.com/install.sh | sh -s -- --uninstall

Install from source

Alternatively, you can install from source:

๋Œ€์‹  ์†Œ์Šค์—์„œ ์„ค์น˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

go install oss.terrastruct.com/d2

Hello World


x -> y: hello world

Shape


Basics

You can declare shapes like so:

imAShape
im_a_shape
im a shape
i'm a shape
# notice that one-hyphen is not a connection
# whereas, `a--shape` would be a connection
a-shape

You can also use semicolons to define multiple shapes on the same line:

ํ•˜๋‚˜์˜ ์ค„์— ์—ฌ๋Ÿฌ ๊ฐœ์˜ ๋„ํ˜•์„ ์ •์˜ํ•˜๋ ค๋ฉด ์„ธ๋ฏธ์ฝœ๋ก ์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค:

1SQLite; Cassandra

By default, a shape's label is the same as the shape's key. But if you want it to be different, assign a new label like so:

๊ธฐ๋ณธ์ ์œผ๋กœ ๋„ํ˜•์˜ ๋ผ๋ฒจ์€ ๋„ํ˜•์˜ ํ‚ค์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๋ผ๋ฒจ์„ ์›ํ•œ๋‹ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ƒˆ ๋ผ๋ฒจ์„ ํ• ๋‹นํ•˜์„ธ์š”:

1pg: PostgreSQL

By default, a shape's type is rectangle. To specify otherwise, provide the field shape:

๊ธฐ๋ณธ์ ์œผ๋กœ ๋„ํ˜•์˜ ์œ ํ˜•์€ ์‚ฌ๊ฐํ˜•์ž…๋‹ˆ๋‹ค. ๋‹ค๋ฅธ ์œ ํ˜•์„ ์ง€์ •ํ•˜๋ ค๋ฉด shape ํ•„๋“œ๋ฅผ ์ œ๊ณตํ•˜์‹ญ์‹œ์˜ค:

Cloud: my cloud
Cloud.shape: cloud

Example

pg: PostgreSQL
Cloud: my cloud
Cloud.shape: cloud
SQLite; Cassandra

Important

SHAPE CATALOGrectanglesquarepageparallelogramdocumentcylinderqueuepackagestepcalloutstored_datapersondiamondovalcirclehexagoncloudSpecial shape types (more on these next):textcodeclasssql_tableimagesequence_diagram

Connections


Connections define relationships between shapes.

์—ฐ๊ฒฐ์€ ๋„ํ˜• ๊ฐ„์˜ ๊ด€๊ณ„๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

Basics

Hyphens/arrows in between shapes define a connection.

๋„ํ˜• ์‚ฌ์ด์˜ ํ•˜์ดํ”ˆ/ํ™”์‚ดํ‘œ๋Š” ์—ฐ๊ฒฐ์„ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.

Write Replica Canada <-> Write Replica Australia

Read Replica <- Master
Write Replica -> Master

Read Replica 1 -- Read Replica 2

If you reference an undeclared shape in a connection, it's created (as shown in the hello world example).

์—ฐ๊ฒฐ์—์„œ ์„ ์–ธ๋˜์ง€ ์•Š์€ ๋„ํ˜•์„ ์ฐธ์กฐํ•˜๋ฉด, hello world ์˜ˆ์ œ์—์„œ ๋ณด์—ฌ์ง€๋“ฏ์ด, ๋„ํ˜•์ด ์ƒ์„ฑ๋ฉ๋‹ˆ๋‹ค.

Important

INFOThere are 4 valid ways to define a connection:---><-<->

Connection labels

Read Replica 1 -- Read Replica 2: Kept in sync

Connections must reference a shape's key, not its label.

์—ฐ๊ฒฐ์€ ๋ชจ์–‘์˜ ๋ผ๋ฒจ์ด ์•„๋‹Œ ํ‚ค๋ฅผ ์ฐธ์กฐํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

be: Backend
fe: Frontend

# This would create new shapes
Backend -> Frontend

# This would define a connection over existing labels
be -> fe

Example

Write Replica Canada <-> Write Replica Australia

Read Replica <- Master

x -- y

super long shape id here --\
  -> super long shape id even longer here

Repeated connections

Repeated connections do not override existing connections. They declare new ones.

๋ฐ˜๋ณต ์—ฐ๊ฒฐ์€ ๊ธฐ์กด ์—ฐ๊ฒฐ์„ ๋ฎ์–ด์“ฐ์ง€ ์•Š์Šต๋‹ˆ๋‹ค. ์ƒˆ๋กœ์šด ์—ฐ๊ฒฐ์„ ์„ ์–ธํ•ฉ๋‹ˆ๋‹ค.

Database -> S3: backup
Database -> S3
Database -> S3: backup

Connection chaining

For readability, it may look more natural to define multiple connection in a single line.

๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ์—ฌ๋Ÿฌ ์—ฐ๊ฒฐ์„ ํ•œ ์ค„์— ์ •์˜ํ•˜๋Š” ๊ฒƒ์ด ๋” ์ž์—ฐ์Šค๋Ÿฌ์šธ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

# The label applies to each connection in the chain.
High Mem Instance -> EC2 <- High CPU Instance: Hosted By

Cycles are okay

Stage One -> Stage Two -> Stage Three -> Stage Four
Stage Four -> Stage One: repeat

Arrowheads

To override the default arrowhead shape or give a label next to arrowheads, define a special shape on connections named source-arrowhead and/or target-arrowhead.

๊ธฐ๋ณธ ํ™”์‚ดํ‘œ ๋จธ๋ฆฌ ๋ชจ์–‘์„ ๋ฎ์–ด์“ฐ๊ฑฐ๋‚˜ ํ™”์‚ดํ‘œ ๋จธ๋ฆฌ ์˜†์— ๋ ˆ์ด๋ธ”์„ ์ง€์ •ํ•˜๋ ค๋ฉด, ์—ฐ๊ฒฐ์— ๋Œ€ํ•œ ํŠน์ˆ˜ํ•œ ๋ชจ์–‘์„ source-arrowhead ๋ฐ/๋˜๋Š” target-arrowhead๋กœ ์ •์˜ํ•˜์‹ญ์‹œ์˜ค.

a: The best way to avoid responsibility is to say, "I've got responsibilities"
b: Whether weary or unweary, O man, do not rest
c: I still maintain the point that designing a monolithic kernel in 1991 is a

a -> b: To err is human, to moo bovine {
  source-arrowhead: 1
  target-arrowhead: * {
    shape: diamond
  }
}

b <-> c: "Reality is just a crutch for people who can't handle science fiction" {
  source-arrowhead.label: 1
  target-arrowhead: * {
    shape: diamond
    style.filled: true
  }
}

d: A black cat crossing your path signifies that the animal is going somewhere

d -> a -> c

Important

ARROWHEAD OPTIONStriangle (default)arrow (like triangle but pointier)diamondCan be further styled as style.filled: true.circleCan be further styled as style.filled: true.cf-one, cf-one-required (cf stands for crows foot)cf-many, cf-many-required

Containers


server
# Declares a shape inside of another shape
server.process

# Can declare the container and child in same line
im a parent.im a child

# Since connections can also declare keys, this works too
apartment.Bedroom.Bathroom -> office.Spare Room.Bathroom: Portal

Nested syntax

You can avoid repeating containers by creating nested maps.

์ค‘์ฒฉ๋œ ๋งต์„ ๋งŒ๋“ค์–ด ๋ฐ˜๋ณต์ ์ธ ์ปจํ…Œ์ด๋„ˆ๋ฅผ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

clouds: {
  aws: {
    load_balancer -> api
    api -> db
  }
  gcloud: {
    auth -> db
  }

  gcloud -> aws
}

Container labels

There are two ways define container labels.

์ปจํ…Œ์ด๋„ˆ ๋ผ๋ฒจ์„ ์ •์˜ํ•˜๋Š” ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

1. Shorthand container labels

gcloud: Google Cloud {
  ...
}

2. Reserved keyword label

gcloud: {
  label: Google Cloud
  ...
}

Example

clouds: {
  aws: AWS {
    load_balancer -> api
    api -> db
  }
  gcloud: Google Cloud {
    auth -> db
  }

  gcloud -> aws
}

users -> clouds.aws.load_balancer
users -> clouds.gcloud.auth

ci.deploys -> clouds

Reference parent

Sometimes you want to reference something outside of the container from within. The underscore (_) refers to parent.

๊ฐ€๋”์€ ์ปจํ…Œ์ด๋„ˆ ์™ธ๋ถ€์˜ ๊ฒƒ์„ ์ฐธ์กฐํ•˜๊ณ  ์‹ถ์„ ๋•Œ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ‘์ค„(_)์€ ๋ถ€๋ชจ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

christmas: {
  presents
}
birthdays: {
  presents
  _.christmas.presents -> presents: regift
  _.christmas.style.fill: "\#ACE1AF"
}
โš ๏ธ **GitHub.com Fallback** โš ๏ธ