1
1
= CRUD
2
2
:page-aliases: {page-version}@manual::CRUD.adoc, {page-version}@manual::CRUD/inserting.adoc, {page-version}@manual::CRUD/reading.adoc
3
+ :test-typeql: linear
3
4
4
5
CRUD operations create, read, update, and delete data in the TypeDB database.
5
6
@@ -16,14 +17,25 @@ For these examples, consider the following schema:
16
17
[,typeql]
17
18
.Sample schema
18
19
----
20
+ #!test[schema]
19
21
define
20
22
entity person,
21
23
owns name @card(1),
22
- plays friendship:friend;
23
- relation friendship ,
24
- relates friend ;
24
+ plays friendship:friend,
25
+ plays parentship:parent ,
26
+ plays parentship:child ;
25
27
attribute name,
26
28
value string;
29
+ relation friendship,
30
+ relates friend @card(2);
31
+ relation parentship,
32
+ relates parent,
33
+ relates child;
34
+
35
+ entity user sub person,
36
+ owns username @key;
37
+ attribute username,
38
+ value string;
27
39
----
28
40
29
41
If `insert` is the first stage in a pipeline, it runs just once and outputs a single row containing the newly created instances.
@@ -33,6 +45,7 @@ An example command to insert two people and a friendship between them would be:
33
45
[,typeql]
34
46
.Alice 🤝 Bob
35
47
----
48
+ #!test[write]
36
49
insert
37
50
$a-name isa name "Alice";
38
51
$b-name isa name "Bob";
@@ -61,6 +74,7 @@ In this example, Alice and Bob are already in the database, and we want to creat
61
74
[,typeql]
62
75
.Alice 🤝 Bob, v2
63
76
----
77
+ #!test[write]
64
78
match
65
79
$a isa person, has name "Alice";
66
80
$b isa person, has name "Bob";
@@ -76,6 +90,7 @@ The `put` stage represents an "insert if does not exist" operation and provides
76
90
77
91
[,typeql]
78
92
----
93
+ #!test[write]
79
94
put
80
95
$a isa person, has name "Alice";
81
96
$b isa person, has name "Bob";
@@ -118,10 +133,11 @@ For further explanation, see the drivers xref:{page-version}@core-concepts::driv
118
133
A concern when loading large amounts of data is duplication. In most cases, it can be avoided by using the `put` stage:
119
134
120
135
[,typeql]
121
- .Insert a new user if there isn't already a user named `"john1"`
136
+ .Insert a new user if there isn't already a user with username `"john1"`
122
137
----
138
+ #!test[write, count = 1]
123
139
put
124
- $x isa user, has username "john_1";
140
+ $x isa user, has username "john_1", has name "John" ;
125
141
----
126
142
127
143
An issue may arise during parallel loading, however, where concurrent workers ensure the same user exists, inadvertently creating two
@@ -160,6 +176,7 @@ This allows structuring the output of the query to map precisely to the structur
160
176
161
177
[,typeql]
162
178
----
179
+ #!test[read, documents]
163
180
match
164
181
$p isa person;
165
182
fetch {
@@ -206,6 +223,7 @@ Similar to ``put``, an `update` stage represents a "set if does not exist" opera
206
223
[,typeql]
207
224
.Simple attribute update
208
225
----
226
+ #!test[write]
209
227
match
210
228
$a isa person, has name "Alice";
211
229
update
@@ -217,6 +235,7 @@ This is the expected way to "change" the value of an attribute that is owned. At
217
235
[,typeql]
218
236
.Fixing a data entry error by switching roleplayers
219
237
----
238
+ #!test[write]
220
239
match
221
240
$parentship isa parentship, links (parent: $p, child: $c);
222
241
update
@@ -226,6 +245,7 @@ update
226
245
[,typeql]
227
246
.Multiple unrelated statements can be updated at a time
228
247
----
248
+ #!test[write]
229
249
match
230
250
$a isa person, has username "white-rabbit";
231
251
$b isa person, has username "mad-hatter";
@@ -243,6 +263,7 @@ The same roleplayer switch from the previous section can be expressed as follows
243
263
[,typeql]
244
264
.Fixing a data entry error by switching roleplayers, v2
245
265
----
266
+ #!test[write]
246
267
match
247
268
$parentship isa parentship, links (parent: $p, child: $c);
248
269
delete
@@ -257,6 +278,7 @@ Similarly, the common operation of replacing an owned attribute can be done like
257
278
258
279
[,typeql]
259
280
----
281
+ #!test[write]
260
282
match
261
283
$a isa person, has name $name;
262
284
$name == "Alice";
@@ -276,6 +298,7 @@ The most straightforward deletion is to remove an entire instance (an entity, re
276
298
277
299
[,typeql]
278
300
----
301
+ #!test[write]
279
302
match
280
303
$a isa person, has name "Alice";
281
304
delete
@@ -298,8 +321,10 @@ Connections between instances, that is ownerships and roleplayers, can be delete
298
321
299
322
[,typeql]
300
323
----
324
+ #!test[write]
301
325
match
302
- $a isa person, has name $name == "Alice";
326
+ $a isa person, has name $name;
327
+ $name == "Alice";
303
328
$f isa friendship, links (friend: $a);
304
329
delete
305
330
has $name of $a;
0 commit comments