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. $
  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. $
  1. mkdir quickstart
  1. $
  1. cd quickstart

Then initialize your EdgeDB project:

  1. $
  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. $
  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 project [quickstart]:
  5. > quickstart
  6. Checking EdgeDB versions...
  7. Specify the version of EdgeDB to use with this project [default: 2.0-rc.3]:
  8. > 2.0
  9. ┌─────────────────────┬───────────────────────────────────────────────┐
  10. Project directory ~/path/to/quickstart
  11. Project config ~/path/to/quickstart/edgedb.toml
  12. Schema dir (empty) ~/path/to/quickstart/dbschema
  13. Installation method portable package
  14. Version 2.0+c21decd
  15. Instance name quickstart
  16. └─────────────────────┴───────────────────────────────────────────────┘
  17. Downloading package...
  18. 00:00:01 [====================] 32.98MiB/32.98MiB 32.89MiB/s | ETA: 0s
  19. Successfully installed 2.0+c21decd
  20. Initializing EdgeDB instance...
  21. Applying migrations...
  22. Everything is up to date. Revision initial
  23. Project initialized.
  24. To connect to quickstart, run `edgedb`

This did a couple things.

  1. 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.

  2. 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 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. edgedb>
  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 a 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 release_year -> int64;
  9. multi link actors -> Person;
  10. link director -> Person;
  11. }
  12. };

A few things to note here.

  • Our types don’t contain an id property; EdgeDB automatically creates this property and assigned a unique UUID to every object inserted into the database.

  • 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 without JOINs.

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. $
  1. edgedb migration create
  1. Created ./dbschema/migrations/00001.edgeql, id: m1la5u4qi...

This creates an .edgeql migration file 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. $
  1. edgedb migrate
  1. Applied m1la5u4qi... (00001.edgeql)
  2. Note: adding first migration disables DDL.

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. $
  1. edgedb

Inserting objects

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

  1. edgedb>
  2. .......
  3. .......
  4. .......
  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. edgedb>
  2. .......
  3. .......
  4. .......
  5. .......
  6. .......
  7. .......
  8. .......
  9. .......
  10. .......
  11. .......
  12. .......
  13. .......
  14. .......
  15. .......
  16. .......
  17. .......
  18. .......
  19. .......
  1. with director_id := <uuid>$director_id
  2. insert Movie {
  3. title := 'Blade Runnr 2049', # typo is intentional 🙃
  4. release_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.

  1. edgedb>
  2. .......
  3. .......
  4. .......
  5. .......
  1. update Movie
  2. filter .title = 'Blade Runnr 2049'
  3. set {
  4. title := "Blade Runner 2049",
  5. };
  1. {default::Movie {id: 4d0c8ddc-54d4-11e9-8c54-7776f6130e05}}

Now for something a little more interesting; let’s add Ryan Gosling to the cast.

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

This query uses the += operator to assign an additional item to the actors link without overwriting the existing contents. By contrast, -= unlinks elements and := overwrites the link entirely.

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

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

5. Run some queries

Let’s write some basic queries:

  1. edgedb>
  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 specify which properties to select, add a shape:

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

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

Let’s retrieve some information about Blade Runner 2049.

  1. edgedb>
  2. .......
  3. .......
  4. .......
  5. .......
  6. .......
  7. .......
  8. .......
  9. .......
  1. select Movie {
  2. title,
  3. release_year,
  4. actors: {
  5. first_name,
  6. last_name
  7. }
  8. }
  9. filter .title = "Blade Runner 2049";
  1. {
  2. default::Movie {
  3. title: 'Blade Runner 2049',
  4. release_year: 2017,
  5. director: default::Person {first_name: 'Denis', last_name: 'Villeneuve'},
  6. actors: {
  7. default::Person {first_name: 'Harrison', last_name: 'Ford'},
  8. default::Person {first_name: 'Ana', last_name: 'de Armas'},
  9. default::Person {first_name: 'Ryan', last_name: 'Gosling'},
  10. },
  11. },
  12. }

Nice and easy! We’re able to fetch the movie and its related objects by nesting shapes (similar to GraphQL).

6. Migrate your schema

Let’s add some more information about “Dune”; for starters, we’ll insert Person objects for its cast members Jason Momoa, Oscar Isaac, and Zendaya.

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

Oh no! We can’t add Zendaya 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/default.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 release_year -> int64;
  10. link director -> Person;
  11. multi link actors -> Person;
  12. }
  13. };

Then create a new migration.

  1. $
  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...

EdgeDB detects the modifications to your schema, and prompts you to confirm each change. In this case there’s only one modification. Answer y to proceed, then apply the migration.

  1. $
  1. edgedb migrate
  1. Applied m1k62y4x... (00002.edgeql)

Now run edgedb to re-open the REPL and insert Zendaya. This time, the query works.

  1. edgeql>
  2. .......
  3. .......
  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. edgedb>
  2. .......
  3. .......
  4. .......
  5. .......
  6. .......
  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 release_year -> int64;
  13. link director -> Person;
  14. multi link actors -> Person;
  15. }
  16. };

Then create and run another migration:

  1. $
  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. $
  1. edgedb migrate
  1. Applied m1gd3vxwz3oopur6ljgg7kzrin3jh65xhhjbj6de2xaou6i7owyhaq
  2. (00003.edgeql)

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

  1. edgeql>
  2. .......
  3. .......
  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!