Skip to content

Commit f4a0c29

Browse files
GavinMendelGleasongitbook-bot
authored andcommitted
GitBook: [#57] No subject
1 parent 97a8528 commit f4a0c29

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

readme/terminusx-db/reference-guides/document-interface.md

+27-13
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ The documents to be submitted are given as post data. Multiple documents can be
7272
| message | | The commit message |
7373
| graph\_type | instance | either instance or schema. Used to switch between submitting to the instance or the schema graph. |
7474
| 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. |
7576

7677
#### Result
7778

@@ -91,12 +92,13 @@ The documents to be submitted are given as post data. Multiple documents can be
9192

9293
#### Parameters
9394

94-
| parameter | default | explanation |
95-
| ----------- | -------- | ------------------------------------------------------------------------------------------------- |
96-
| author | | The commit author |
97-
| message | | The commit message |
98-
| graph\_type | instance | either instance or schema. Used to switch between submitting to the instance or the schema graph. |
99-
| create | false | insert if the document was not already in the database. |
95+
| parameter | default | explanation |
96+
| ----------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
97+
| author | | The commit author |
98+
| message | | The commit message |
99+
| 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. |
100102

101103
### Deleting documents
102104

@@ -132,10 +134,13 @@ As shown above, deleting a single document can be done through query parameters
132134
In other words, a JSON list of document IDs.
133135

134136
### Capturing IDs while inserting or replacing documents
137+
135138
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.
136139

137140
#### Capturing an identifier into a variable
141+
138142
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+
139144
```jsx
140145
{ "@type": "Person",
141146
"@capture": "Id_Tom",
@@ -148,14 +153,17 @@ This will store the newly generated ID in a variable called `ID_Tom` for the dur
148153
It is allowed to capture an ID and then never actually refer to it.
149154

150155
It is an error to capture the same variable twice. Doing so will result in a `api:CaptureIdAlreadyBound` error with the following shape:
156+
151157
```jsx
152158
{"@type": "api:CaptureIdAlreadyBound",
153159
"api:capture": "..capture id..",
154160
"api:document": {..document where previously captured variable was captured again..}
155161
```
156162
157163
#### Referring to an identifier using a variable
164+
158165
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+
159167
```jsx
160168
{ "@type": "Person",
161169
"name": "Jerry",
@@ -164,14 +172,17 @@ When inserting or replacing a document that needs to refer to another document i
164172
```
165173
166174
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+
167176
```jsx
168177
{ "@type": "api:NotAllCapturesFound",
169178
"api:captures": [..list of capture ids that were referenced but not found..]
170179
}
171180
```
172181
173182
#### Ordering of documents
183+
174184
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+
175186
```jsx
176187
{ "@type": "Person",
177188
"@capture": "Id_Tom",
@@ -188,7 +199,9 @@ ID captures and ID references can be done in any order. That means that when you
188199
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.
189200
190201
### Self-reference
202+
191203
Using ID capture, it is possible to create a document that refers to itself:
204+
192205
```jsx
193206
{ "@type": "Person",
194207
"@capture": "Captured_Id",
@@ -200,6 +213,7 @@ Using ID capture, it is possible to create a document that refers to itself:
200213
This will make Elmo be his own friend.
201214
202215
#### ID capture only works within a single operation
216+
203217
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.
204218
205219
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
258272
259273
### Parameters
260274
261-
| parameter | default | explanation |
262-
| --------- | ------- | --------------------------------------------------------------------------------------------- |
263-
| before\_commit | | The first commit to compare in order to produce a diff |
264-
| after\_commit | | The last commit to compare in order to produce a diff |
265-
| commit\_info | | A JSON document with author and message |
266-
| match\_final\_state | true | Ignores conflicts if the final state would remain the same |
267-
| type | squash | What type of application to perform - currently can only be squash |
275+
| parameter | default | explanation |
276+
| ------------------- | ------- | ------------------------------------------------------------------ |
277+
| before\_commit | | The first commit to compare in order to produce a diff |
278+
| after\_commit | | The last commit to compare in order to produce a diff |
279+
| commit\_info | | A JSON document with author and message |
280+
| match\_final\_state | true | Ignores conflicts if the final state would remain the same |
281+
| type | squash | What type of application to perform - currently can only be squash |
268282
269283
### Result
270284

0 commit comments

Comments
 (0)