Quickstart​

Welcome to EdgeDB!

This quickstart will walk you through the entire process of creating a simple EdgeDB-powered application: installation, defining your schema, adding some data, and writing your first query. Let’s jump in!

1. Installation​

First let’s install the EdgeDB CLI. Open a terminal and run the appropriate command below.

macOS/Linux

  1. curl https://sh.edgedb.com --proto '=https' -sSf1 | sh

Windows

  1. # in Powershell
  2. PS> iwr https://ps1.edgedb.com -useb | iex

This command downloads and executes a bash script that installs the edgedb CLI on your machine. You may be asked for your password. Once the installation completes, restart your terminal so the edgedb command becomes available.

Now let’s set up your EdgeDB project.

2. Initialize a project​

In a terminal, create a new directory and cd into it.

  1. mkdir quickstart
  1. cd quickstart

Then initialize your EdgeDB project:

  1. edgedb project init

This starts an interactive tool that walks you through the process of setting up your first EdgeDB instance. You should see something like this:

  1. edgedb project init
  1. No `edgedb.toml` found in `~/path/to/quickstart` or above.
  2. Do you want to initialize a new project? [Y/n]
  3. > Y
  4. Specify the name of EdgeDB instance to use with this
  5. project [default: quickstart]:
  6. > quickstart
  7. How would you like to run EdgeDB for this project?
  8. 1. Local (native package)
  9. 2. Docker
  10. Type a number to choose an option:
  11. > 1
  12. Checking EdgeDB versions...
  13. Specify the version of EdgeDB to use with this project
  14. [default: 1.0-rc.4]:
  15. > 1.0-rc.4
  16. ┌─────────────────────┬───────────────────────────────────────────────┐
  17. Project directory ~/path/to/quickstart
  18. Project config ~/path/to/quickstart/edgedb.toml
  19. Schema dir (empty) ~/path/to/quickstart/dbschema
  20. Installation method Native System Package
  21. Version 1.0-rc.4+c21decd
  22. Instance name quickstart
  23. └─────────────────────┴───────────────────────────────────────────────┘
  24. Downloading package...
  25. 00:00:01 [====================] 32.98MiB/32.98MiB 32.89MiB/s | ETA: 0s
  26. Successfully installed 1.0-rc.4+c21decd
  27. Initializing EdgeDB instance...
  28. Applying migrations...
  29. Everything is up to date. Revision initial
  30. Project initialized.
  31. To connect to quickstart, run `edgedb`

This did a couple things.

First, it scaffolded your project by creating an edgedb.toml config file and a schema file dbschema/default.esdl. In the next section, you’ll define your schema in default.esdl.

Second, it spun up an EdgeDB instance called quickstart (unless you overrode this with a different name). As long as you’re inside the project directory all edgedb CLI commands will be executed against this instance. For more details on how EdgeDB projects work, check out the Using projects guide.

Quick note! You can have several “instances” of EdgeDB running on your computer simultaneously. Each instance contains several “databases”. Each database may contain several “schema modules” (though commonly your schema will be entirely defined inside the default module).

Let’s give it a try! Run edgedb in your terminal. This will connect to your database and open a REPL. You’re now connected to a live EdgeDB instance running on your computer! Try executing a simple query:

  1. select 1 + 1;
  1. {2}

Run \q to exit the REPL. More interesting queries are coming soon, promise! But first we need to set up a schema.

3. Set up your schema​

Open the quickstart directory in your IDE or editor of choice. You should see the following file structure.

  1. /path/to/quickstart
  2. ├── edgedb.toml
  3. ├── dbschema
  4. ├── default.esdl
  5. ├── migrations

EdgeDB schemas are defined with a dedicated schema description language called (predictably) EdgeDB SDL (or just SDL for short). It’s an elegant, declarative way to define your data model. SDL lives inside .esdl files. Commonly, your entire schema will be declared in a file called default.esdl but you can split your schema across several .esdl files; the filenames don’t matter.

Syntax-highlighter packages/extensions for .esdl files are available for Visual Studio Code, Sublime Text, Atom, and Vim.

Let’s build a simple movie database. We’ll need to define two object types (equivalent to table in SQL): Movie and Person. Open dbschema/default.esdl in your editor of choice and paste the following:

  1. module default {
  2. type Person {
  3. required property first_name -> str;
  4. required property last_name -> str;
  5. }
  6. type Movie {
  7. required property title -> str;
  8. property year -> int64;
  9. link director -> Person;
  10. multi link actors -> Person;
  11. }
  12. };

Our Person schema just contains two string properties, first_name and last_name (both required). Our Movie schema contains a string property title and an optional integer property year. EdgeDB automatically assigns a unique id property to every object inserted into the database — no need to define it manually.

The Movie type also includes two links. In EdgeDB, links are used to represent relationships between object types. They entirely abstract away the concept of foreign keys. Later, you’ll see just how easy it is to write “deep” queries that include relations — no JOINs required!

Now we’re ready to run a migration to apply this schema to the database.

Generate the migration​

First, we generate a migration file with edgedb migration create. This starts an interactive tool that asks a series of questions. Pay attention to these questions to make sure you aren’t making any unintended changes.

  1. edgedb migration create
  1. did you create object type 'default::Person'? [y,n,l,c,b,s,q,?]
  2. > y
  3. did you create object type 'default::Movie'? [y,n,l,c,b,s,q,?]
  4. > y
  5. Created ./dbschema/migrations/00001.edgeql, id: m1la5u4qi...

For now, just type y to confirm each change. For a full breakdown of these options, refer to the dedicated Migrations guide. Once you’ve answered the prompts, a .edgeql migration file we be generated in the dbschema/migrations directory/

If you’re interested, open this migration file to see what’s inside! It’s a simple EdgeQL script consisting of DDL commands like create type, alter type, and create property. When you generate migrations, EdgeDB reads your declared .esdl schema and generates a migration path.

Execute the migration​

Let’s apply the migration:

  1. edgedb migrate
  1. Applied m1la5u4qi... (00001.edgeql)

Let’s make sure that worked. Run edgedb list types to view all currently-defined object types.

  1. $ edgedb list types
  2. ┌─────────────────┬──────────────────────────────┐
  3. Name Extending
  4. ├─────────────────┼──────────────────────────────┤
  5. default::Movie std::BaseObject, std::Object
  6. default::Person std::BaseObject, std::Object
  7. └─────────────────┴──────────────────────────────┘

Looking good! Now let’s add some data to the database!

4. Insert data​

For this tutorial we’ll just use the REPL tool to execute queries. In practice, you’ll probably be using one of EdgeDB’s client libraries for JavaScript/TypeScript, Go, or Python.

Open the REPL:

  1. edgedb

Inserting objects​

Now, let’s add Denis Villeneuve to the database with a simple EdgeQL query:

  1. insert Person {
  2. first_name := 'Denis',
  3. last_name := 'Villeneuve',
  4. };
  1. {default::Person {id: 86d0eb18-b7ff-11eb-ba80-7b8e9facf817}}

As you can see, EdgeQL differs from SQL in some important ways. It uses curly braces and the assignment operator (:=) to make queries explicit and intuitive for the people who write them: programmers. It’s also completely composable, so subqueries are easy; let’s try a nested insert.

The query below contains a query parameter $director_id. After executing the query in the REPL, we’ll be prompted to provide a value for it. Copy and paste the UUID for Denis Villeneuve from the previous query.

  1. with director_id := <uuid>$director_id
  2. insert Movie {
  3. title := 'Blade Runnr 2049', # typo is intentional 🙃
  4. year := 2017,
  5. director := (
  6. select Person
  7. filter .id = director_id
  8. ),
  9. actors := {
  10. (insert Person {
  11. first_name := 'Harrison',
  12. last_name := 'Ford',
  13. }),
  14. (insert Person {
  15. first_name := 'Ana',
  16. last_name := 'de Armas',
  17. }),
  18. }
  19. };
  1. Parameter <uuid>$director_id: 86d0eb18-b7ff-11eb-ba80-7b8e9facf817
  2. {default::Movie {id: 4d0c8ddc-54d4-11e9-8c54-7776f6130e05}}

Updating objects​

Oops, we misspelled “Runner”. Let’s fix that with an update query. While we’re at it, we’ll append Ryan Gosling to the cast with the += operator. This operator links additional objects to a multi link; by contrast, -= unlinks elements and := overwrites the link entirely.

  1. update Movie
  2. filter .title = 'Blade Runnr 2049'
  3. set {
  4. title := "Blade Runner 2049",
  5. actors += (
  6. insert Person {
  7. first_name := "Ryan",
  8. last_name := "Gosling"
  9. }
  10. )
  11. };
  1. {default::Movie {id: 4d0c8ddc-54d4-11e9-8c54-7776f6130e05}}

Our database is still a little sparse. Let’s quickly add a couple more movies.

  1. insert Movie { title := "Dune" };
  1. {default::Movie {id: 64d024dc-54d5-11e9-8c54-a3f59e1d995e}}
  1. insert Movie {
  2. title := "Arrival",
  3. year := 2016
  4. };
  1. {default::Movie {id: ca69776e-40df-11ec-b1b8-b7c909ac034a}}

5. Run some queries​

Let’s write some basic queries:

  1. select Movie;
  1. {
  2. default::Movie {id: 4d0c8ddc-54d4-11e9-8c54-7776f6130e05},
  3. default::Movie {id: 64d024dc-54d5-11e9-8c54-a3f59e1d995e},
  4. default::Movie {id: ca69776e-40df-11ec-b1b8-b7c909ac034a}
  5. }

This query simply returns all the Movie objects in the database. By default, only the id property is returned for each result. To select more properties, add a shape:

  1. select Movie {
  2. title,
  3. year
  4. };
  1. {
  2. default::Movie {title: 'Blade Runner 2049', year: 2017},
  3. default::Movie {title: 'Dune', year: {}},
  4. default::Movie {title: 'Arrival', year: 2016}
  5. }

This time, the results contain title and year as requested in the query shape. Note that the year for Dune is given as {} (the empty set). This is the equivalent of a null value in SQL.

Let’s fetch more information about Blade Runner 2049 specifically.

  1. select Movie {
  2. title,
  3. year
  4. }
  5. filter .title = "Blade Runner 2049";
  1. {default::Movie {title: 'Blade Runner 2049', year: 2017}}

Let’s get more details about the Movie:

  1. select Movie {
  2. title,
  3. year,
  4. director: {
  5. first_name,
  6. last_name
  7. },
  8. actors: {
  9. first_name,
  10. last_name
  11. }
  12. }
  13. filter .title = "Blade Runner 2049";
  1. {
  2. default::Movie {
  3. title: 'Blade Runner 2049',
  4. year: 2017,
  5. director: default::Person {
  6. first_name: 'Denis',
  7. last_name: 'Villeneuve'
  8. },
  9. actors: {
  10. default::Person {
  11. first_name: 'Harrison',
  12. last_name: 'Ford'
  13. },
  14. default::Person {
  15. first_name: 'Ryan',
  16. last_name: 'Gosling'
  17. },
  18. default::Person {
  19. first_name: 'Ana',
  20. last_name: 'de Armas',
  21. },
  22. },
  23. },
  24. }

6. Migrate your schema​

Let’s add some more information about “Dune”. For example, we can add some of the actors, like Jason Momoa, Zendaya, and Oscar Isaac:

  1. insert Person {
  2. first_name := 'Jason',
  3. last_name := 'Momoa'
  4. };
  1. default::Person {id: 618d4cd6-54db-11e9-8c54-67c38dbbba18}
  1. insert Person {
  2. first_name := 'Oscar',
  3. last_name := 'Isaac'
  4. };
  1. default::Person {id: 618d5a64-54db-11e9-8c54-9393cfcd9598}
  1. insert Person { first_name := 'Zendaya'};
  1. ERROR: MissingRequiredError: missing value for required property
  2. 'last_name' of object type 'default::Person'

Unfortunately, adding Zendaya isn’t possible with the current schema since both first_name and last_name are required. So let’s migrate our schema to make last_name optional.

If necessary, close the REPL with \q, then open dbschema/schema.esdl.

  1. module default {
  2. type Person {
  3. required property first_name -> str;
  4. required property last_name -> str;
  5. property last_name -> str;
  6. }
  7. type Movie {
  8. required property title -> str;
  9. property year -> int64; # the year of release
  10. link director -> Person;
  11. multi link actors -> Person;
  12. }
  13. };

Then create a new migration and apply it:

  1. edgedb migration create
  1. did you make property 'last_name' of object type
  2. 'default::Person' optional? [y,n,l,c,b,s,q,?]
  3. > y
  4. Created ./dbschema/migrations/00002.edgeql, id: m1k62y4x...
  1. edgedb migrate
  1. Applied m1k62y4x... (00002.edgeql)

Now re-open the REPL and add Zendaya:

  1. insert Person {
  2. first_name := 'Zendaya'
  3. };
  1. {default::Person {id: 65fce84c-54dd-11e9-8c54-5f000ca496c9}}

7. Computeds​

Now that last names are optional, we may want an easy way to retrieve the full name for a given Person. We’ll do this with a computed property:

  1. select Person {
  2. full_name :=
  3. .first_name ++ ' ' ++ .last_name
  4. if exists .last_name
  5. else .first_name
  6. };
  1. {
  2. default::Person {full_name: 'Zendaya'},
  3. default::Person {full_name: 'Harrison Ford'},
  4. default::Person {full_name: 'Ryan Gosling'},
  5. ...
  6. }

Let’s say we’re planning to use full_name a lot. Instead of re-defining it in each query, we can add it directly to the schema alongside the other properties of Person. Let’s update dbschema/default.esdl:

  1. module default {
  2. type Person {
  3. required property first_name -> str;
  4. property last_name -> str;
  5. property full_name :=
  6. .first_name ++ ' ' ++ .last_name
  7. if exists .last_name
  8. else .first_name;
  9. }
  10. type Movie {
  11. required property title -> str;
  12. property year -> int64; # the year of release
  13. link director -> Person;
  14. multi link actors -> Person;
  15. }
  16. };

Then create and run another migration:

  1. edgedb migration create
  1. did you create property 'full_name' of object type
  2. 'default::Person'? [y,n,l,c,b,s,q,?]
  3. > y
  4. Created ./dbschema/migrations/00003.edgeql, id:
  5. m1gd3vxwz3oopur6ljgg7kzrin3jh65xhhjbj6de2xaou6i7owyhaq
  1. edgedb migrate
  1. Applied m1gd3vxwz3oopur6ljgg7kzrin3jh65xhhjbj6de2xaou6i7owyhaq
  2. (00003.edgeql)

Now we can easily fetch full_name just like any other property!

  1. select Person {
  2. full_name
  3. };
  1. {
  2. default::Person {full_name: 'Denis Villeneuve'},
  3. default::Person {full_name: 'Harrison Ford'},
  4. default::Person {full_name: 'Ana de Armas'},
  5. default::Person {full_name: 'Ryan Gosling'},
  6. default::Person {full_name: 'Jason Momoa'},
  7. default::Person {full_name: 'Oscar Isaac'},
  8. default::Person {full_name: 'Zendaya'},
  9. }

8. Onwards and upwards​

You now know the basics of EdgeDB! You’ve installed the CLI and database, set up a local project, created an initial schema, added and queried data, and run a schema migration.

  • For guided tours of major concepts, check out the showcase pages for Data Modeling, EdgeQL, and Migrations.

  • For a deep dive into the EdgeQL query language, check out the Interactive Tutorial.

  • For an immersive, comprehensive walkthrough of EdgeDB concepts, check out our illustrated e-book Easy EdgeDB; it’s designed to walk a total beginner through EdgeDB, from the basics all the way through advanced concepts.

  • To start building an application using the language of your choice, check out our client libraries for JavaScript/TypeScript, Python, and Go.

  • Or just jump into the docs!