Skip to content

Commit

Permalink
change repl language from elixir to iex (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
nsidnev authored Aug 2, 2023
1 parent f56a2df commit 9244572
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 74 deletions.
20 changes: 10 additions & 10 deletions lib/edgedb.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule EdgeDB do
A simple example of how to use it:
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> EdgeDB.query!(client, "\"\"
...(2)> select Person{
Expand Down Expand Up @@ -181,15 +181,15 @@ defmodule EdgeDB do
If the first argument is a string, it will be assumed to be the DSN or instance name
and passed as `[dsn: dsn]` keyword list to connect.
```elixir
```iex
iex(1)> {:ok, _client} = EdgeDB.start_link("edgedb://edgedb:edgedb@localhost:5656/edgedb")
```
Otherwise, if the first argument is a list, it will be used as is to connect.
See `t:EdgeDB.start_option/0` for supported connection options.
```elixir
```iex
iex(1)> {:ok, _client} = EdgeDB.start_link(instance: "edgedb_elixir")
```
Expand Down Expand Up @@ -223,7 +223,7 @@ defmodule EdgeDB do
`[dsn: dsn]` keyword list along with other options to connect.
See `t:EdgeDB.start_option/0` for supported connection options.
```elixir
```iex
iex(1)> {:ok, _client} = EdgeDB.start_link("edgedb://edgedb:edgedb@localhost:5656/edgedb", tls_security: :insecure)
```
Expand Down Expand Up @@ -260,7 +260,7 @@ defmodule EdgeDB do
Execute the query on the client and return the results as a `{:ok, set}` tuple
if successful, where `set` is `EdgeDB.Set`.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, %EdgeDB.Set{} = set} = EdgeDB.query(client, "select 42")
iex(3)> set
Expand All @@ -270,7 +270,7 @@ defmodule EdgeDB do
If an error occurs, it will be returned as a `{:error, exception}` tuple
where `exception` is `EdgeDB.Error`.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:error, %EdgeDB.Error{} = error} = EdgeDB.query(client, "select UndefinedType")
iex(2)> raise error
Expand All @@ -280,14 +280,14 @@ defmodule EdgeDB do
If a query has arguments, they can be passed as a list for a query with positional arguments
or as a list of keywords for a query with named arguments.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, %EdgeDB.Set{} = set} = EdgeDB.query(client, "select <int64>$0", [42])
iex(3)> set
#EdgeDB.Set<{42}>
```
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, %EdgeDB.Set{} = set} = EdgeDB.query(client, "select <int64>$arg", arg: 42)
iex(3)> set
Expand Down Expand Up @@ -538,7 +538,7 @@ defmodule EdgeDB do
`EdgeDB.transaction/3` calls **cannot** be nested more than once.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, tickets} = EdgeDB.transaction(client, fn client ->
...(2)> EdgeDB.query!(client, "insert Ticket{ number := 2}")
Expand Down Expand Up @@ -581,7 +581,7 @@ defmodule EdgeDB do
See `t:EdgeDB.rollback_option/0` for supported options.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:error, :tx_rollback} =
...(2)> EdgeDB.transaction(client, fn tx_conn ->
Expand Down
2 changes: 1 addition & 1 deletion lib/edgedb/types/config_memory.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule EdgeDB.ConfigMemory do
@moduledoc """
An immutable value represeting an EdgeDB `cfg::memory` value as a quantity of memory storage.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> mem = EdgeDB.query_required_single!(client, "select <cfg::memory>'5KiB'")
#EdgeDB.ConfigMemory<"5KiB">
Expand Down
2 changes: 1 addition & 1 deletion lib/edgedb/types/date_duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule EdgeDB.DateDuration do
@moduledoc """
An immutable value represeting an EdgeDB `cal::date_duration` value.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> EdgeDB.query_required_single!(client, "select <cal::date_duration>'1 year 2 days'")
#EdgeDB.DateDuration<"P1Y2D">
Expand Down
8 changes: 4 additions & 4 deletions lib/edgedb/types/named_tuple.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule EdgeDB.NamedTuple do
`EdgeDB.NamedTuple` implements `Access` behavior to access fields
by index or key and `Enumerable` protocol for iterating over tuple values.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> nt = EdgeDB.query_required_single!(client, "select (a := 1, b := 'a', c := [3])")
#EdgeDB.NamedTuple<a: 1, b: "a", c: [3]>
Expand Down Expand Up @@ -33,7 +33,7 @@ defmodule EdgeDB.NamedTuple do
@doc """
Convert a named tuple to a regular erlang tuple.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> nt = EdgeDB.query_required_single!(client, "select (a := 1, b := 'a', c := [3])")
iex(3)> EdgeDB.NamedTuple.to_tuple(nt)
Expand All @@ -51,7 +51,7 @@ defmodule EdgeDB.NamedTuple do
@doc """
Convert a named tuple into a regular map.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> nt = EdgeDB.query_required_single!(client, "select (a := 1, b := 'a', c := [3])")
iex(3)> EdgeDB.NamedTuple.to_map(nt)
Expand All @@ -66,7 +66,7 @@ defmodule EdgeDB.NamedTuple do
@doc """
Get named tuple keys.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> nt = EdgeDB.query_required_single!(client, "select (a := 1, b := 'a', c := [3])")
iex(3)> EdgeDB.NamedTuple.keys(nt)
Expand Down
6 changes: 3 additions & 3 deletions lib/edgedb/types/object.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ defmodule EdgeDB.Object do
`EdgeDB.Object` implements `Access` behavior to access properties by key.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> %EdgeDB.Object{} = object =
iex(2)> EdgeDB.query_required_single!(client, "\"\"
Expand All @@ -28,7 +28,7 @@ defmodule EdgeDB.Object do
Links can also have their own properties (denoted as `@<link_prop_name>` in EdgeQL syntax).
You can use the same property name as in the query to access them from the links.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> %EdgeDB.Object{} = object =
iex(2)> EdgeDB.query_required_single!(client, "\"\"
Expand Down Expand Up @@ -214,7 +214,7 @@ defmodule EdgeDB.Object do
@doc """
Convert an object into a regular map.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> object =
iex(2)> EdgeDB.query_required_single!(client, "\"\"
Expand Down
6 changes: 3 additions & 3 deletions lib/edgedb/types/range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule EdgeDB.Range do
@moduledoc """
A value representing some interval of values.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> EdgeDB.query_required_single!(client, "select range(1, 10)")
#EdgeDB.Range<[1, 10)>
Expand Down Expand Up @@ -71,7 +71,7 @@ defmodule EdgeDB.Range do
@doc """
Create an empty range.
```elixir
```iex
iex(1)> EdgeDB.Range.empty()
#EdgeDB.Range<empty>
```
Expand All @@ -84,7 +84,7 @@ defmodule EdgeDB.Range do
@doc """
Create new range.
```elixir
```iex
iex(1)> EdgeDB.Range.new(1.1, 3.3, inc_upper: true)
#EdgeDB.Range<[1.1, 3.3]>
```
Expand Down
2 changes: 1 addition & 1 deletion lib/edgedb/types/relative_duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule EdgeDB.RelativeDuration do
@moduledoc """
An immutable value represeting an EdgeDB `cal::relative_duration` value.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> EdgeDB.query_required_single!(client, "select <cal::relative_duration>'45.6 seconds'")
#EdgeDB.RelativeDuration<"PT45.6S">
Expand Down
4 changes: 2 additions & 2 deletions lib/edgedb/types/set.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ defmodule EdgeDB.Set do
`EdgeDB.Set` implements `Enumerable` protocol for iterating over set values.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> %EdgeDB.Set{} =
iex(2)> EdgeDB.query!(client, "\"\"
Expand All @@ -31,7 +31,7 @@ defmodule EdgeDB.Set do
@doc """
Check if set is empty.
```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> %EdgeDB.Set{} = set = EdgeDB.query!(client, "select Ticket")
iex(3)> EdgeDB.Set.empty?(set)
Expand Down
4 changes: 2 additions & 2 deletions pages/md/custom-codecs.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,11 @@ end

Now let's test this codec:

```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link(codecs: [MyApp.EdgeDB.Codecs.JSONPayload])
iex(2)> payload = %MyApp.Users.Payload{public_id: 1, first_name: "Harry", last_name: "Potter"}
iex(3)> EdgeDB.query!(client, "insert User { name := <str>$username, payload := <JSONPayload>$payload }", username: "user", payload: payload)
iex(4) EdgeDB.Object{} = EdgeDB.query_required_single!(client, "select User {name, payload} filter .name = 'user' limit 1")
iex(4)> EdgeDB.Object{} = EdgeDB.query_required_single!(client, "select User {name, payload} filter .name = 'user' limit 1")
#EdgeDB.Object<name := "user", payload := %MyApp.Users.Payload{
first_name: "Harry",
last_name: "Potter",
Expand Down
20 changes: 10 additions & 10 deletions pages/md/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ module default {

Let's fill the database with some data, which will be used in further examples:

```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> EdgeDB.query!(client, """
...(2)> WITH
Expand Down Expand Up @@ -105,7 +105,7 @@ If you want to receive an `EdgeDB.Set` from your query, just use the `EdgeDB.que

Let's query all existing posts with their bodies:

```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, posts} = EdgeDB.query(client, "select Post { body }")
{:ok,
Expand All @@ -116,7 +116,7 @@ iex(2)> {:ok, posts} = EdgeDB.query(client, "select Post { body }")

We can iterate over `EdgeDB.Set` and inspect each object separately:

```elixir
```iex
iex(3)> Enum.each(posts, fn %EdgeDB.Object{} = post ->
...(3)> IO.inspect(post[:body], label: "post (#{inspect(post.id)})")
...(3)> end)
Expand All @@ -135,7 +135,7 @@ If you know that the query will return only one element or none, you can use `Ed

Let's query a post with a link to the Elixir client for EdgeDB:

```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> %EdgeDB.Object{} = post = EdgeDB.query_single!(client, "select Post filter contains(.body, 'https://hex.pm/packages/edgedb') limit 1")
iex(3)> post.id
Expand All @@ -144,7 +144,7 @@ iex(3)> post.id

If we try to select a `Post` that does not exist, `nil` will be returned:

```elixir
```iex
iex(4)> EdgeDB.query_single!(client, "select Post filter .body = 'lol' limit 1")
nil
```
Expand All @@ -154,7 +154,7 @@ nil
In case we want to ensure that the requested element must exist, we can use the functions `EdgeDB.query_required_single/4` and
`EdgeDB.query_required_single!/4`. Instead of returning `nil` they will return `EdgeDB.Error` in case of a missing element:

```elixir
```iex
iex(5)> EdgeDB.query_required_single!(client, "select Post filter .body = 'lol' limit 1")
** (EdgeDB.Error) NoDataError: expected result, but query did not return any data
```
Expand All @@ -167,7 +167,7 @@ iex(5)> EdgeDB.query_required_single!(client, "select Post filter .body = 'lol'
The API for transactions is provided by the `EdgeDB.transaction/3` function:

```elixir
```iex
iex(1)> {:ok, client} = EdgeDB.start_link()
iex(2)> {:ok, user} =
...(2)> EdgeDB.transaction(client, fn conn ->
Expand All @@ -178,7 +178,7 @@ iex(2)> {:ok, user} =
Transactions can be rollbacked using the `EdgeDB.rollback/2` function or automatically
if an error has occurred inside a transaction block:

```elixir
```iex
iex(3)> {:error, :rollback} =
...(3)> EdgeDB.transaction(client, fn conn ->
...(3)> %EdgeDB.Object{} = EdgeDB.query_required_single!(conn, "insert User { name := <str>$username }", username: "wrong_username")
Expand All @@ -198,7 +198,7 @@ The following types of errors can be retried retried:

As an example, let's create a transaction conflict to show how this works. In the first example, we will disable retries:

```elixir
```iex
iex(5)> callback = fn conn, body ->
...(5)> Process.sleep(500)
...(5)> EdgeDB.query!(conn, "update Post filter .author.id = <uuid>$user_id set { body := <str>$new_body }", user_id: user.id, new_body: body)
Expand All @@ -214,7 +214,7 @@ iex(7)> EdgeDB.transaction(client, &callback.(&1, "new_body_2"), retry: [transac

Now let's execute the same thing but with enabled retries:

```elixir
```iex
iex(8)> spawn(fn ->
...(8)> {:ok, client} = EdgeDB.start_link()
...(8)> EdgeDB.transaction(client, &callback.(&1, "new_body_1"))
Expand Down
Loading

0 comments on commit 9244572

Please sign in to comment.