Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions core-concepts/modules/ROOT/pages/typedb/crud.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
= CRUD
:page-aliases: {page-version}@manual::CRUD.adoc, {page-version}@manual::CRUD/inserting.adoc, {page-version}@manual::CRUD/reading.adoc
:test-typeql: linear

CRUD operations create, read, update, and delete data in the TypeDB database.

Expand All @@ -16,14 +17,25 @@ For these examples, consider the following schema:
[,typeql]
.Sample schema
----
#!test[schema]
define
entity person,
owns name @card(1),
plays friendship:friend;
relation friendship,
relates friend;
plays friendship:friend,
plays parentship:parent,
plays parentship:child;
attribute name,
value string;
relation friendship,
relates friend @card(2);
relation parentship,
relates parent,
relates child;

entity user sub person,
owns username @key;
attribute username,
value string;
----

If `insert` is the first stage in a pipeline, it runs just once and outputs a single row containing the newly created instances.
Expand All @@ -33,6 +45,7 @@ An example command to insert two people and a friendship between them would be:
[,typeql]
.Alice 🤝 Bob
----
#!test[write]
insert
$a-name isa name "Alice";
$b-name isa name "Bob";
Expand Down Expand Up @@ -61,6 +74,7 @@ In this example, Alice and Bob are already in the database, and we want to creat
[,typeql]
.Alice 🤝 Bob, v2
----
#!test[write]
match
$a isa person, has name "Alice";
$b isa person, has name "Bob";
Expand All @@ -76,6 +90,7 @@ The `put` stage represents an "insert if does not exist" operation and provides

[,typeql]
----
#!test[write]
put
$a isa person, has name "Alice";
$b isa person, has name "Bob";
Expand Down Expand Up @@ -118,10 +133,11 @@ For further explanation, see the drivers xref:{page-version}@core-concepts::driv
A concern when loading large amounts of data is duplication. In most cases, it can be avoided by using the `put` stage:

[,typeql]
.Insert a new user if there isn't already a user named `"john1"`
.Insert a new user if there isn't already a user with username `"john1"`
----
#!test[write, count = 1]
put
$x isa user, has username "john_1";
$x isa user, has username "john_1", has name "John";
----

An issue may arise during parallel loading, however, where concurrent workers ensure the same user exists, inadvertently creating two
Expand Down Expand Up @@ -160,6 +176,7 @@ This allows structuring the output of the query to map precisely to the structur

[,typeql]
----
#!test[read, documents]
match
$p isa person;
fetch {
Expand Down Expand Up @@ -206,6 +223,7 @@ Similar to ``put``, an `update` stage represents a "set if does not exist" opera
[,typeql]
.Simple attribute update
----
#!test[write]
match
$a isa person, has name "Alice";
update
Expand All @@ -217,6 +235,7 @@ This is the expected way to "change" the value of an attribute that is owned. At
[,typeql]
.Fixing a data entry error by switching roleplayers
----
#!test[write]
match
$parentship isa parentship, links (parent: $p, child: $c);
update
Expand All @@ -226,6 +245,7 @@ update
[,typeql]
.Multiple unrelated statements can be updated at a time
----
#!test[write]
match
$a isa person, has username "white-rabbit";
$b isa person, has username "mad-hatter";
Expand All @@ -243,6 +263,7 @@ The same roleplayer switch from the previous section can be expressed as follows
[,typeql]
.Fixing a data entry error by switching roleplayers, v2
----
#!test[write]
match
$parentship isa parentship, links (parent: $p, child: $c);
delete
Expand All @@ -257,6 +278,7 @@ Similarly, the common operation of replacing an owned attribute can be done like

[,typeql]
----
#!test[write]
match
$a isa person, has name $name;
$name == "Alice";
Expand All @@ -276,6 +298,7 @@ The most straightforward deletion is to remove an entire instance (an entity, re

[,typeql]
----
#!test[write]
match
$a isa person, has name "Alice";
delete
Expand All @@ -298,8 +321,10 @@ Connections between instances, that is ownerships and roleplayers, can be delete

[,typeql]
----
#!test[write]
match
$a isa person, has name $name == "Alice";
$a isa person, has name $name;
$name == "Alice";
$f isa friendship, links (friend: $a);
delete
has $name of $a;
Expand Down
2 changes: 2 additions & 0 deletions core-concepts/modules/ROOT/pages/typedb/databases.adoc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
= Databases
:page-aliases: {page-version}@manual::databases.adoc, {page-version}@manual::databases/creating.adoc
:test-typeql: linear

This section covers the management of databases and the definition of the schema, which provides the structural foundation for your data.

Expand Down Expand Up @@ -50,6 +51,7 @@ Here is an example of a simple schema definition:

[,typeql]
----
#!test[schema]
define
entity person,
owns name,
Expand Down