You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: readme/terminusx-db/reference-guides/document-interface.md
+27-13
Original file line number
Diff line number
Diff line change
@@ -72,6 +72,7 @@ The documents to be submitted are given as post data. Multiple documents can be
72
72
| message || The commit message |
73
73
| graph\_type | instance | either instance or schema. Used to switch between submitting to the instance or the schema graph. |
74
74
| full\_replace | false | If true, all existing documents are deleted before inserting the posted documents. This allows the full replacement of the contents of a database. This is especially useful for replacing the schema. |
75
+
| raw\_json | false | If true, the input documents are treated as raw JSON , inserted as type `sys:JSONDocument` and are not subject to schema restrictions. |
75
76
76
77
#### Result
77
78
@@ -91,12 +92,13 @@ The documents to be submitted are given as post data. Multiple documents can be
| graph\_type | instance | either instance or schema. Used to switch between submitting to the instance or the schema graph. |
100
+
| create | false | insert if the document was not already in the database. |
101
+
| raw\_json | false | If true, the replaced documents are treated as raw JSON , they must be replacing a document of type `sys:JSONDocument` and they are not subject to schema restrictions. |
100
102
101
103
### Deleting documents
102
104
@@ -132,10 +134,13 @@ As shown above, deleting a single document can be done through query parameters
132
134
In other words, a JSON list of document IDs.
133
135
134
136
### Capturing IDs while inserting or replacing documents
137
+
135
138
When inserting or replacing several documents at once, it may occur that some of these documents need to refer to each other. However, at insertion time, you may not know what the IDs of the new documents are going to be. This is especially the case for document types that generate their identifier randomly, but even for non-random key types, it may be convenient to rely on the server's ID generation algorithm, rather than trying to predict what IDs will get generated. Therefore, in order to support cross-references between newly inserted documents, the document interface allows you to capture newly generated document IDs in a variable, and then refer to that variable later in other documents.
136
139
137
140
#### Capturing an identifier into a variable
141
+
138
142
When inserting or replacing a document that we want to refer to in another document inserted in the same operation, you can use a `@capture` key in the document to associate the newly generated identifier with a variable. For example,
143
+
139
144
```jsx
140
145
{ "@type":"Person",
141
146
"@capture":"Id_Tom",
@@ -148,14 +153,17 @@ This will store the newly generated ID in a variable called `ID_Tom` for the dur
148
153
It is allowed to capture an ID and then never actually refer to it.
149
154
150
155
It is an error to capture the same variable twice. Doing so will result in a `api:CaptureIdAlreadyBound` error with the following shape:
156
+
151
157
```jsx
152
158
{"@type":"api:CaptureIdAlreadyBound",
153
159
"api:capture":"..capture id..",
154
160
"api:document": {..document where previously captured variable was captured again..}
155
161
```
156
162
157
163
#### Referring to an identifier using a variable
164
+
158
165
When inserting or replacing a document that needs to refer to another document inserted in the same operation, you can use a json dictionary of the form `{"@ref":"..id.."}` in place of an ordinary id. For example,
166
+
159
167
```jsx
160
168
{ "@type":"Person",
161
169
"name":"Jerry",
@@ -164,14 +172,17 @@ When inserting or replacing a document that needs to refer to another document i
164
172
```
165
173
166
174
It is an error to refer to a variable which is never captured. Doing so will result in a `api:NotAllCapturesFound` error of the following shape:
175
+
167
176
```jsx
168
177
{ "@type":"api:NotAllCapturesFound",
169
178
"api:captures": [..listof capture ids that were referenced but not found..]
170
179
}
171
180
```
172
181
173
182
#### Ordering of documents
183
+
174
184
ID captures and ID references can be done in any order. That means that when you are submitting several documents, you're allowed to refer to a captured ID in an earlier document. This also allows you to do cross-references, where two documents refer to each other:
185
+
175
186
```jsx
176
187
{ "@type":"Person",
177
188
"@capture":"Id_Tom",
@@ -188,7 +199,9 @@ ID captures and ID references can be done in any order. That means that when you
188
199
In this example, Tom refers to Jerry, even though at that point in the submitted document stream, Jerry has not yet been processed. This is not a problem - both Tom and Jerry will get inserted referring to each other.
189
200
190
201
### Self-reference
202
+
191
203
Using ID capture, it is possible to create a document that refers to itself:
204
+
192
205
```jsx
193
206
{ "@type":"Person",
194
207
"@capture":"Captured_Id",
@@ -200,6 +213,7 @@ Using ID capture, it is possible to create a document that refers to itself:
200
213
This will make Elmo be his own friend.
201
214
202
215
#### ID capture only works within a single operation
216
+
203
217
It is important to keep in mind that the ID capture mechanism only works within a single call to the document api. It is not possible to capture an ID in one operation, and then refer to it in a second operation. The `@capture` and `@ref` instructions do not get saved into the database. They are processed immediately and are then forgotten.
204
218
205
219
If you need to refer to a document already in the database, the only way to do so is by referring to its ID.
@@ -258,13 +272,13 @@ The purpose of this endpoint is to take the difference between any two commits a
0 commit comments