Skip to content

Commit b89b0a1

Browse files
committed
Subdocuments handling documentation
1 parent a288ac5 commit b89b0a1

File tree

1 file changed

+64
-1
lines changed
  • src/app/docs/add-documents-with-woql

1 file changed

+64
-1
lines changed

src/app/docs/add-documents-with-woql/page.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,67 @@ let v = Vars("person", "name");
2828
and(isa(v.person, "People"),
2929
triple(v.person,"label",v.name),
3030
insert_document(doc({'@type' : 'Planet', label: v.name})))
31-
```
31+
```
32+
33+
## Create and link a subdocument
34+
35+
Subdocuments have two components linking them to their parent, one triple, and one subdocument, which are required by the schema checker to accept the document. The triple links the parent with the subdocument, and the subdocument is linked to the parent using the special property `@linked-by`.
36+
37+
Simple example for adding a subdocument:
38+
39+
```javascript
40+
and(
41+
insert_document(new doc({"@type": "PersonRole", "@linked-by": {"@id": "Person/John", "@property": "role"}}), "v:SubdocumentId"),
42+
add_triple("Person/John", "role", "v:SubdocumentId"),
43+
)
44+
```
45+
46+
## Delete a subdocument
47+
48+
Subdocuments can be deleted using the `delete_document` keyword, but it's important to also delete the triple that links the subdocument from the parent document. Here we resolve the parent document in the variable `v:parentdoc`.
49+
50+
```javascript
51+
and(
52+
eq("v:subdoc", "Person/John/role/PersonRole/cxW1Egirxm8-QYrq"),
53+
triple("v:parentdoc", "role", "v:subdoc"),
54+
delete_document("v:subdoc"),
55+
delete_triple("v:parentdoc", "role", "v:subdoc"),
56+
)
57+
```
58+
59+
## Update a subdocument
60+
61+
Let's combine creating and deleting a document into a single query to update the subdocument.
62+
63+
Subdocuments can't be updated in place using the `update_document` keyword which exists only for top level documents. You can update a subdocument by deleting the old subdocument and creating a new one with the updated content, including the `@linked-by` property, and updating the triple that links the subdocument.
64+
65+
This in place operation is only possible using random keyed identifiers on the subdocument. The alternative is to update the top level document.
66+
67+
```javascript
68+
select("v:oldsubdoc", "v:newsubdoc").
69+
and(
70+
eq("v:subdoc", "Person/John/role/PersonRole/cxW1Egirxm8-QYrq"),
71+
and(
72+
triple("v:parentdoc", "role", "v:oldsubdoc"),
73+
74+
delete_document("v:oldsubdoc"),
75+
insert_document(new doc({"@type": "PersonRole", "@linked-by": {"@id": "v:parentdoc", "@property": "role"}}),"v:newsubdoc"),
76+
77+
update_triple("v:parentdoc", "role", "v:newsubdoc", "v:oldsubdoc"),
78+
)
79+
)
80+
```
81+
82+
The parent document is resolved for the specific predicate, the old document gets deleted and a new document is created, linked to the parent document and the triple that links from the parent document is updated.
83+
84+
The old and new subdocuments are provided in the returned bindings via the selections of variables to return.
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+

0 commit comments

Comments
 (0)