@based/client 3.3.0 → 4.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (211) hide show
  1. package/README.md +32 -336
  2. package/dist/Emitter.d.ts +7 -6
  3. package/dist/Emitter.js +31 -6
  4. package/dist/Emitter.js.map +1 -1
  5. package/dist/authState/parseAuthState.d.ts +3 -0
  6. package/dist/authState/parseAuthState.js +51 -0
  7. package/dist/authState/parseAuthState.js.map +1 -0
  8. package/dist/authState/updateAuthState.d.ts +3 -0
  9. package/dist/authState/updateAuthState.js +15 -0
  10. package/dist/authState/updateAuthState.js.map +1 -0
  11. package/dist/channel/cleanUp.d.ts +2 -0
  12. package/dist/channel/cleanUp.js +32 -0
  13. package/dist/channel/cleanUp.js.map +1 -0
  14. package/dist/channel/index.d.ts +12 -0
  15. package/dist/channel/index.js +67 -0
  16. package/dist/channel/index.js.map +1 -0
  17. package/dist/genObserveId.d.ts +1 -0
  18. package/dist/genObserveId.js +12 -0
  19. package/dist/genObserveId.js.map +1 -0
  20. package/dist/getTargetInfo.d.ts +6 -0
  21. package/dist/getTargetInfo.js +14 -0
  22. package/dist/getTargetInfo.js.map +1 -0
  23. package/dist/getUrlFromOpts.d.ts +3 -0
  24. package/dist/getUrlFromOpts.js +27 -0
  25. package/dist/getUrlFromOpts.js.map +1 -0
  26. package/dist/incoming/debug.d.ts +9 -0
  27. package/dist/incoming/debug.js +132 -0
  28. package/dist/incoming/debug.js.map +1 -0
  29. package/dist/incoming/index.d.ts +2 -0
  30. package/dist/incoming/index.js +341 -0
  31. package/dist/incoming/index.js.map +1 -0
  32. package/dist/incoming/protocol.d.ts +9 -0
  33. package/dist/incoming/protocol.js +62 -0
  34. package/dist/incoming/protocol.js.map +1 -0
  35. package/dist/index.d.ts +70 -127
  36. package/dist/index.js +225 -15
  37. package/dist/index.js.map +1 -1
  38. package/dist/outgoing/debug.d.ts +8 -0
  39. package/dist/outgoing/debug.js +70 -0
  40. package/dist/outgoing/debug.js.map +1 -0
  41. package/dist/outgoing/index.d.ts +14 -0
  42. package/dist/outgoing/index.js +227 -0
  43. package/dist/outgoing/index.js.map +1 -0
  44. package/dist/outgoing/protocol.d.ts +24 -0
  45. package/dist/outgoing/protocol.js +200 -0
  46. package/dist/outgoing/protocol.js.map +1 -0
  47. package/dist/persistentStorage/browser.d.ts +5 -0
  48. package/dist/persistentStorage/browser.js +150 -0
  49. package/dist/persistentStorage/browser.js.map +1 -0
  50. package/dist/persistentStorage/index.d.ts +6 -0
  51. package/dist/persistentStorage/index.js +56 -0
  52. package/dist/persistentStorage/index.js.map +1 -0
  53. package/dist/persistentStorage/node.d.ts +6 -0
  54. package/dist/persistentStorage/node.js +104 -0
  55. package/dist/persistentStorage/node.js.map +1 -0
  56. package/dist/query/index.d.ts +17 -0
  57. package/dist/query/index.js +112 -0
  58. package/dist/query/index.js.map +1 -0
  59. package/dist/stream/fetch.d.ts +4 -0
  60. package/dist/stream/fetch.js +38 -0
  61. package/dist/stream/fetch.js.map +1 -0
  62. package/dist/stream/index.d.ts +4 -0
  63. package/dist/stream/index.js +65 -0
  64. package/dist/stream/index.js.map +1 -0
  65. package/dist/stream/nodeStream.d.ts +6 -0
  66. package/dist/stream/nodeStream.js +114 -0
  67. package/dist/stream/nodeStream.js.map +1 -0
  68. package/dist/stream/types.d.ts +37 -0
  69. package/dist/stream/types.js +8 -0
  70. package/dist/stream/types.js.map +1 -0
  71. package/dist/stream/uploadFileBrowser.d.ts +4 -0
  72. package/dist/stream/uploadFileBrowser.js +106 -0
  73. package/dist/stream/uploadFileBrowser.js.map +1 -0
  74. package/dist/types/auth.d.ts +11 -0
  75. package/dist/{selvaTypes/get.js → types/auth.js} +1 -1
  76. package/dist/types/auth.js.map +1 -0
  77. package/dist/types/cache.d.ts +6 -0
  78. package/dist/{selvaTypes/set.js → types/cache.js} +1 -1
  79. package/dist/types/cache.js.map +1 -0
  80. package/dist/types/channel.d.ts +25 -0
  81. package/dist/types/channel.js +3 -0
  82. package/dist/types/channel.js.map +1 -0
  83. package/dist/types/error.d.ts +37 -0
  84. package/dist/types/error.js +41 -0
  85. package/dist/types/error.js.map +1 -0
  86. package/dist/types/events.d.ts +34 -0
  87. package/dist/{selvaTypes/schema.js → types/events.js} +1 -1
  88. package/dist/types/events.js.map +1 -0
  89. package/dist/types/functions.d.ts +8 -0
  90. package/dist/types/functions.js +3 -0
  91. package/dist/types/functions.js.map +1 -0
  92. package/dist/types/generic.d.ts +22 -0
  93. package/dist/types/generic.js +3 -0
  94. package/dist/types/generic.js.map +1 -0
  95. package/dist/types/index.d.ts +6 -0
  96. package/dist/{subscriptions → types}/index.js +6 -10
  97. package/dist/types/index.js.map +1 -0
  98. package/dist/types/observe.d.ts +46 -0
  99. package/dist/types/observe.js +3 -0
  100. package/dist/types/observe.js.map +1 -0
  101. package/dist/websocket/index.d.ts +1 -1
  102. package/dist/websocket/index.js +19 -5
  103. package/dist/websocket/index.js.map +1 -1
  104. package/dist/websocket/types.d.ts +0 -0
  105. package/dist/websocket/types.js +0 -0
  106. package/dist/websocket/types.js.map +0 -0
  107. package/dist/websocket/urlLoader.d.ts +0 -0
  108. package/dist/websocket/urlLoader.js +0 -0
  109. package/dist/websocket/urlLoader.js.map +0 -0
  110. package/package.json +18 -27
  111. package/dist/Client.d.ts +0 -80
  112. package/dist/Client.js +0 -271
  113. package/dist/Client.js.map +0 -1
  114. package/dist/auth.d.ts +0 -13
  115. package/dist/auth.js +0 -87
  116. package/dist/auth.js.map +0 -1
  117. package/dist/createError.d.ts +0 -3
  118. package/dist/createError.js +0 -67
  119. package/dist/createError.js.map +0 -1
  120. package/dist/debug.d.ts +0 -2
  121. package/dist/debug.js +0 -21
  122. package/dist/debug.js.map +0 -1
  123. package/dist/file/fetch.d.ts +0 -6
  124. package/dist/file/fetch.js +0 -27
  125. package/dist/file/fetch.js.map +0 -1
  126. package/dist/file/getUrl.d.ts +0 -4
  127. package/dist/file/getUrl.js +0 -42
  128. package/dist/file/getUrl.js.map +0 -1
  129. package/dist/file/index.d.ts +0 -6
  130. package/dist/file/index.js +0 -95
  131. package/dist/file/index.js.map +0 -1
  132. package/dist/file/stream/browser.d.ts +0 -7
  133. package/dist/file/stream/browser.js +0 -17
  134. package/dist/file/stream/browser.js.map +0 -1
  135. package/dist/file/stream/index.d.ts +0 -7
  136. package/dist/file/stream/index.js +0 -86
  137. package/dist/file/stream/index.js.map +0 -1
  138. package/dist/file/uploadFileBrowser.d.ts +0 -3
  139. package/dist/file/uploadFileBrowser.js +0 -72
  140. package/dist/file/uploadFileBrowser.js.map +0 -1
  141. package/dist/findPrefix.d.ts +0 -3
  142. package/dist/findPrefix.js +0 -20
  143. package/dist/findPrefix.js.map +0 -1
  144. package/dist/idleTimeout.d.ts +0 -3
  145. package/dist/idleTimeout.js +0 -17
  146. package/dist/idleTimeout.js.map +0 -1
  147. package/dist/observable/index.d.ts +0 -28
  148. package/dist/observable/index.js +0 -84
  149. package/dist/observable/index.js.map +0 -1
  150. package/dist/printBasedObject.d.ts +0 -4
  151. package/dist/printBasedObject.js +0 -76
  152. package/dist/printBasedObject.js.map +0 -1
  153. package/dist/queue.d.ts +0 -5
  154. package/dist/queue.js +0 -53
  155. package/dist/queue.js.map +0 -1
  156. package/dist/request.d.ts +0 -6
  157. package/dist/request.js +0 -67
  158. package/dist/request.js.map +0 -1
  159. package/dist/selvaTypes/get.d.ts +0 -97
  160. package/dist/selvaTypes/get.js.map +0 -1
  161. package/dist/selvaTypes/schema.d.ts +0 -89
  162. package/dist/selvaTypes/schema.js.map +0 -1
  163. package/dist/selvaTypes/set.d.ts +0 -73
  164. package/dist/selvaTypes/set.js.map +0 -1
  165. package/dist/subscriptions/addGetSubscriber.d.ts +0 -3
  166. package/dist/subscriptions/addGetSubscriber.js +0 -87
  167. package/dist/subscriptions/addGetSubscriber.js.map +0 -1
  168. package/dist/subscriptions/addSubscriber.d.ts +0 -2
  169. package/dist/subscriptions/addSubscriber.js +0 -136
  170. package/dist/subscriptions/addSubscriber.js.map +0 -1
  171. package/dist/subscriptions/generateId.d.ts +0 -1
  172. package/dist/subscriptions/generateId.js +0 -11
  173. package/dist/subscriptions/generateId.js.map +0 -1
  174. package/dist/subscriptions/incomingSubscription.d.ts +0 -3
  175. package/dist/subscriptions/incomingSubscription.js +0 -87
  176. package/dist/subscriptions/incomingSubscription.js.map +0 -1
  177. package/dist/subscriptions/incomingSubscriptionDiff.d.ts +0 -3
  178. package/dist/subscriptions/incomingSubscriptionDiff.js +0 -85
  179. package/dist/subscriptions/incomingSubscriptionDiff.js.map +0 -1
  180. package/dist/subscriptions/index.d.ts +0 -10
  181. package/dist/subscriptions/index.js.map +0 -1
  182. package/dist/subscriptions/logoutSubscriptions.d.ts +0 -3
  183. package/dist/subscriptions/logoutSubscriptions.js +0 -36
  184. package/dist/subscriptions/logoutSubscriptions.js.map +0 -1
  185. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.d.ts +0 -2
  186. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js +0 -14
  187. package/dist/subscriptions/removeSendSubscriptionDataFromQueue.js.map +0 -1
  188. package/dist/subscriptions/removeSubscriber.d.ts +0 -2
  189. package/dist/subscriptions/removeSubscriber.js +0 -44
  190. package/dist/subscriptions/removeSubscriber.js.map +0 -1
  191. package/dist/subscriptions/removeUnsubscribesFromQueue.d.ts +0 -2
  192. package/dist/subscriptions/removeUnsubscribesFromQueue.js +0 -14
  193. package/dist/subscriptions/removeUnsubscribesFromQueue.js.map +0 -1
  194. package/dist/subscriptions/sendAllSubscriptions.d.ts +0 -2
  195. package/dist/subscriptions/sendAllSubscriptions.js +0 -114
  196. package/dist/subscriptions/sendAllSubscriptions.js.map +0 -1
  197. package/dist/token.d.ts +0 -4
  198. package/dist/token.js +0 -35
  199. package/dist/token.js.map +0 -1
  200. package/dist/track.d.ts +0 -8
  201. package/dist/track.js +0 -71
  202. package/dist/track.js.map +0 -1
  203. package/docs/auth-based-ui-howto.md +0 -74
  204. package/docs/auth-howto.md +0 -166
  205. package/docs/auth.md +0 -93
  206. package/docs/authorize.md +0 -56
  207. package/docs/files.md +0 -66
  208. package/docs/get-started.md +0 -60
  209. package/docs/get.md +0 -759
  210. package/docs/schema.md +0 -244
  211. package/docs/set.md +0 -652
package/docs/get.md DELETED
@@ -1,759 +0,0 @@
1
- # _Get_ Method Query Reference
2
-
3
- ## Introduction
4
-
5
- The `based.get()` method allows the user to retrieve data from the database. The user can shape the resulting object using Based's special _$operators_ and syntax.
6
-
7
- The method takes as argument a single object formatted using the based-db query language, and returns an object containing the required data.
8
-
9
- > :exclamation: **_Note:_** While the `get()` method and `observe()` method share the same argument syntax, when using `get()` you will not subscribe to the query, thus you will not be notified when there's a change in the node.
10
-
11
- **Based-db** is a graph database, meaning that nodes are connected by reference in a tree-like structure. When executing a `get` operation, you can either extract data from a single node, or start from a single node and execute a _traversal_ of the tree, collecting data along the way. The starting point and direction of this traversal can be specified in the query.
12
-
13
- Let's look at an example:
14
-
15
- ```js
16
- /*
17
- Let's have the following node in the db
18
- {
19
- id: 'ch6d044a24',
20
- type: 'characters',
21
- name: 'anakin',
22
- address: 'a galaxy far far away',
23
- specialties: [ 'force', 'fatherly love', 'breathing' ]
24
- }
25
- */
26
- const data = await client.get({
27
- $id: 'ch6d044a24',
28
- name: true,
29
- address: true,
30
- })
31
- /*
32
- Will result in
33
- data = {
34
- name: 'anakin',
35
- address: 'a galaxy far far away',
36
- }
37
- */
38
- ```
39
-
40
- `$id` is an operator (since it is prefixed with **_$_**) that points Based to the node with the ID specified, and the rest of the query says to return only the fields `name` and `address` from that node.
41
- This will not run a traversal.
42
-
43
- ## Query structure
44
-
45
- Queries are objects that hold the data structure to be returned, as well as the operators that shape it.
46
-
47
- ```js
48
- const result = await get({
49
- $language: 'en', // operator that sets the language to return in text fields
50
- $id: 'muASxsd3', // operator that sets the id of the document to return
51
-
52
- title: true, // includes the document fields title and
53
- year: true, // year to the results
54
-
55
- technicalData: {
56
- // adds the field technicalData which is
57
- // an object data type with nested properties.
58
- runtime: true,
59
- camera: {
60
- lens: true,
61
- // Of these properties, only `runtime` and
62
- // `camera.lens` are included in the result
63
- },
64
- },
65
- })
66
-
67
- // returns:
68
- // {
69
- // title: '2001: A Space Odyssey',
70
- // year: 1968,
71
- // technicalData: {
72
- // runtime: 97
73
- // camera: {
74
- // lens: '5-perf 65mm'
75
- // }
76
- // }
77
- // }
78
- ```
79
-
80
- They can mirror the existing structure in the document to filter its contents or dynamically add new fields to include fields inherited from its ancestors or from a traversal. See [`$inherit`](#inherit-boolean) for more details.
81
-
82
- ```js
83
- const result = await get({
84
- $language: 'en',
85
- $id: 'muASxsd3',
86
- title: true,
87
- icon: { $inherit: true },
88
- // icon is not part of the movie type
89
- // but inherited from a parent node
90
- })
91
-
92
- // returns:
93
- // {
94
- // title: '2001: A Space Odyssey',
95
- // icon: 'http://example.com/an_icon.png'
96
- // }
97
- ```
98
-
99
- A query can also have other queries nested directly in the field value or as part of a `$field` operator.
100
-
101
- ```js
102
- const result = await get({
103
- $language: 'en',
104
- $id: 'muASxsd3',
105
- title: true,
106
- otherMovie: {
107
- $id: 'muFDedx2', // adds another query
108
- title: true, // as the value of `otherMovie` field
109
- },
110
- })
111
-
112
- // returns:
113
- // {
114
- // title: '2001: A Space Odyssey',
115
- // otherMovie: {
116
- // title: 'Soylent Green'
117
- // }
118
- // }
119
- ```
120
-
121
- Array syntax can also be used to concatenate different individual queries. The result of each query is concatenated in the returned array.
122
-
123
- ```js
124
- const result = await get({
125
- $language: 'en',
126
- $id: 'muASxsd3',
127
- title: true,
128
- type: true, // default field type that holds the documetn type
129
- extraFields: [
130
- {
131
- $id: 'muFDedx2',
132
- title: true,
133
- type: true,
134
- },
135
- {
136
- $id: 'geGhfr4D',
137
- title: true,
138
- type: true,
139
- },
140
- ],
141
- })
142
-
143
- // returns:
144
- // {
145
- // title: '2001: A Space Odyssey',
146
- // type: 'movie',
147
- // extraFields: [
148
- // {
149
- // title: 'Soylent Green',
150
- // type: 'movie',
151
- // },
152
- // {
153
- // title: 'Sci-fi',
154
- // type: 'genre',
155
- // }
156
- // ]
157
- // }
158
- ```
159
-
160
- ## Operators
161
-
162
- - [`$id`](#id-string--string-string-)
163
- - [`$alias`](#alias-string--string-string-)
164
- - [`$all`](#all-boolean)
165
- - [`<any field name>`](#any-field-name-boolean--object)
166
- - [`$value`](#value-any)
167
- - [`$default`](#default-any)
168
- - [`$inherit`](#inherit-boolean)
169
- - [`$type`](#type-string--string-string-)
170
- - [`$item`](#item-string--string-string-)
171
- - [`$required`](#required-string--string-string-)
172
- - [`$field`](#field-string--string-string-)
173
- - [`$language`](#language-string)
174
- - [`$find`](#find-object)
175
- - [`$traverse`](#traverse-string--id1-id2---object)
176
- - [`$recursive`](#recursive--boolean)
177
- - [`$filter`](#filter-object--array)
178
- - [`$list`](#list-boolean--object)
179
- - [`$aggregate`](#aggregate-object)
180
-
181
- ## `$id`: _string | [string, string, ...]_
182
-
183
- ID of the node to get. If it is an array, the first existing record is used for the query.
184
- If omited, the _root_ object ID is assumed. Can only be used at the top level of a query object.
185
-
186
- ```js
187
- const result = await get({
188
- $id: 'muASxsd3',
189
- })
190
- ```
191
-
192
- ## `$alias`: _string | [string, string, ...]_
193
-
194
- Alias of the object to get. If it is an array, the first existing record is used for the query.
195
- An ID can also be passed as `$alias`. If the specified alias does not exist, the value is tried as an ID lookup also before moving to the next entry if an array is specified.
196
-
197
- ```js
198
- const result = await get({
199
- $alias: ['/home', '/stuff'],
200
- })
201
- ```
202
-
203
- ## `$all`: _boolean_
204
-
205
- Includes all the fields in the document, except for `reference(s)` type fields.
206
-
207
- ```js
208
- const result = await get({
209
- $id: 'peASxsd3',
210
- $all: true,
211
- })
212
- ```
213
-
214
- Fields can be excluded if a false is set to specific fields.
215
-
216
- ```js
217
- const result = await get({
218
- $id: 'peASxsd3',
219
- $all: true,
220
- died: false,
221
- })
222
- ```
223
-
224
- ## `<any field name>`: _boolean | object_
225
-
226
- When truthy, includes the named field in the results object.
227
- If an object is passed and the object contains a query, the results of the query will fill that field.
228
- This means that it's possible to include a field name that doesn't actually exist in any node, but will be filled with a different query result.
229
-
230
- ```js
231
- const result = await get({
232
- $id: 'muASxsd3',
233
- title: true,
234
- director: true,
235
- technicalData: {
236
- runtime: true,
237
- color: true,
238
- aspectRatio: true,
239
- },
240
- sequel: {
241
- $id: 'muiK9sQ3',
242
- title: true,
243
- director: true,
244
- },
245
- })
246
- /*
247
- 'result' will then contain a nested field called sequel.title
248
- containing the title field of the node with id = 'muiK9sQ3'
249
- */
250
- ```
251
-
252
- ### `$value`: _any_
253
-
254
- Overrides the current value of the field. It can be of any type.
255
-
256
- ```js
257
- const result = await get({
258
- $id: 'moASxsd3',
259
- title: { $value: 'Amazing movie' },
260
- })
261
- ```
262
-
263
- ### `$default`: _any_
264
-
265
- Default value to be returned in case the field has no value set.
266
-
267
- ```js
268
- const result = await get({
269
- $id: 'moASxsd3',
270
- director: { $default: 'Unknown director' },
271
- })
272
- ```
273
-
274
- ### `$inherit`: _boolean_
275
-
276
- If the value for the field is not set in the node, `$inherit` searches for that field in the node's ancestors.
277
-
278
- ```js
279
- // `icon` is not a field in the 'moSoylentGreen' node
280
- // but exists in one of the nodes set as parents of it.
281
- const result = await client.get({
282
- $id: 'moSoylentGreen',
283
- icon: { $inherit: true },
284
- })
285
- ```
286
-
287
- #### `$type`: _string | [string, string, ...]_
288
-
289
- Limits inheritance to a specific node type or array of types in the ancestry. It goes in order through the array.
290
-
291
- ```js
292
- // `icon` is not set in the 'moSoylentGreen' document
293
- // but exists in a parent 'genre' type node
294
- const result = await client.get({
295
- $id: 'moSoylentGreen',
296
- icon: {
297
- $inherit: { $type: ['genre', 'collection'] },
298
- // Of all the ancestors, only search the ones with type
299
- // 'genre' and 'collection', in that order.
300
- },
301
- })
302
- ```
303
-
304
- #### `$item`: _string | [string, string, ...]_
305
-
306
- Once the inheritance traversal has found a hit, you can select which fields to inherit from that node. It's faster than doing multiple inherits since it's a single operation.
307
-
308
- ```js
309
- // Let's have these two nodes in the db
310
- // {
311
- // id: 'myleagueID'
312
- // type: 'league',
313
- // name: 'serie-a',
314
- // amount: 30,
315
- // }
316
- // and
317
- // {
318
- // id: 'mymatchID'
319
- // type: 'match',
320
- // name: 'roma-lazio',
321
- // parents: [myleagueID],
322
- // }
323
- const data = await client.get({
324
- $id: 'mymatchID,
325
- $all: true,
326
- league: {
327
- $inherit: { $item: 'league' },
328
- amount: true,
329
- name: true,
330
- },
331
- })
332
- // returns:
333
- // {
334
- // type: 'match',
335
- // name: 'roma-lazio',
336
- // id: 'mymatchID',
337
- // league: { amount: 30, name: 'serie-a' }
338
- // }
339
- ```
340
-
341
- #### `$required`: _string | [string, string, ...]_
342
-
343
- Used in conjucntion with `$item`, it requires one or more fields to be set in the ancestor.
344
-
345
- ```js
346
- // Let's have these two nodes in the db
347
- // {
348
- // id: 'myleagueID'
349
- // type: 'league',
350
- // name: 'serie-a',
351
- // amount: 30,
352
- // }
353
- // and
354
- // {
355
- // id: 'mymatchID'
356
- // type: 'match',
357
- // name: 'roma-lazio',
358
- // parents: [myleagueID],
359
- // }
360
- const data = await client.get({
361
- $id: 'mymatchID,
362
- $all: true,
363
- league: {
364
- $inherit: { $item: 'league', $required: 'flurp' },
365
- amount: true,
366
- name: true,
367
- },
368
- })
369
- // returns { type: 'match', name: 'roma-lazio', id: 'mymatchID' },
370
- // since there's no node in the ancestors of 'mymatchID'
371
- // with a field called 'flurp'
372
- const data = await client.get({
373
- $id: 'mymatchID,
374
- $all: true,
375
- league: {
376
- $inherit: { $item: 'league', $required: 'amount' },
377
- amount: true,
378
- name: true,
379
- },
380
- })
381
- // will return
382
- // {
383
- // type: 'match',
384
- // name: 'roma-lazio',
385
- // id: 'mymatchID',
386
- // league: { amount: 30, name: 'serie-a' }
387
- // }
388
- // since this time the required field is indeed set.
389
-
390
- ```
391
-
392
- ### `$field`: _string | [string, string, ...]_
393
-
394
- The `$field` operator is used to create a field that fetches its results from another field.
395
-
396
- ```js
397
- const result = await client.get({
398
- $id: 'mo2001ASpaceOdyssey',
399
- directedBy: { $field: 'director' },
400
- })
401
- ```
402
-
403
- Dot notation can be used to create a path to a nested field or even specific data inside a _`JSON`, `object`, `text`, `record`_ and _`reference`_ datatype.
404
-
405
- ```js
406
- const result = await client.get({
407
- $id: 'mo2001ASp',
408
- ratio: { $field: 'technicalData.aspectRatio' },
409
- })
410
- // with title : 'text' = {en: 'A space odissey', it: 'Odissea nello spazio'}
411
- const result = await client.get({
412
- $id: 'mo2001ASp',
413
- englishTitle: { $field: 'title.en' },
414
- })
415
- // with sequel : 'reference' = 'mo2001ASp'
416
- const result = await client.get({
417
- $id: 'mo2SD58dfd',
418
- englishTitleOfSequel: { $field: 'sequel.title.en' },
419
- })
420
- ```
421
-
422
- The `$field` operator can take an Array instead of a string. The array can have several field names or paths for the alias to point to. The first that is defined in the document will be returned.
423
-
424
- ```js
425
- const = await client.get({
426
- $id: 'mo2001ASpaceOdyssey',
427
- by: { $field: ['producer', 'director'] }
428
- })
429
- ```
430
-
431
- ## `$language`: _string_
432
-
433
- Filters node's data to a set language on fields that support it. Currently only the `text` type supports this.
434
-
435
- ```js
436
- const result = await client.get({
437
- $language: 'en',
438
- $id: 'mo2001ASpaceOdyssey',
439
- title: true, // value of `title` becomes the value of `title.en`
440
- })
441
- ```
442
-
443
- ## `$find`: _object_
444
-
445
- This operator is one of the most useful ones, and you'll probably be using it a lot when dealing with Based-db.
446
-
447
- It specifies that the `get()` operation (or `observe()`) should execute a **traversal of the database**. The operators used in conjunction with it allow the user to change **direction**, **starting point**, and **filter** of this traversal.
448
- It is especially useful when used in conjunction with the `$list` operator, turning the result into an array of nodes that match the query.
449
-
450
- It is used as a property of a field of the return object.
451
- When not used together with `$list`, it will return the first matched document.
452
-
453
- ```js
454
- const data = await client.get({
455
- $id: 'root',
456
- $all: true,
457
- $find: {
458
- $traverse: 'children',
459
- },
460
- })
461
- // Will return a single random child of root, the first one hit.
462
- ```
463
-
464
- ### `$traverse`: _string | [id1, id2, ...] | object_
465
-
466
- - Property of `$find`.
467
- - Allowed values: any field of type `references`, an array of IDs, an object
468
-
469
- This sets the direction of the traversal, which starts from the `$id` specified or, if omitted, from `root`.
470
-
471
- Based-db has four built-in fields of type `references`:
472
-
473
- | Value | Meaning |
474
- | ----------- | ---------------------------------------------------------------------------------------------------------------------------------------------------- |
475
- | parents | Relationship set by the user. |
476
- | ancestors | Recursively traverse the tree upwards, from parent to parent of parent and so on. |
477
- | children | Relationship set by the user. |
478
- | descendants | Recursively traverse the tree downwards, from child to child of child and so on. **If this is applied to `root`, it traverses the entire database.** |
479
-
480
- ```js
481
- const data = await client.get({
482
- $id: 'root',
483
- $all: true,
484
- $find: {
485
- $traverse: 'children',
486
- },
487
- })
488
- // Will return a single random child of root, the first one hit.
489
- ```
490
-
491
- When set to an object, `$traverse` can take special operators that change the traversal.
492
-
493
- These are `$any`, `$first`, `<node's type>`.
494
-
495
- ```js
496
- client.get({
497
- $id: 'root',
498
- id: true,
499
- items: {
500
- name: true,
501
- nonsense: { $default: 'yes' },
502
- $list: {
503
- $find: {
504
- $recursive: true,
505
- $traverse: {
506
- root: 'children',
507
- league: { $first: ['matches', 'children'] },
508
- team: { $all: ['parents', 'children'] },
509
- $any: false,
510
- },
511
- },
512
- },
513
- },
514
- })
515
- ```
516
-
517
- #### `<node's type name>` : _reference(s) field(s)_
518
-
519
- When setting for example `root: { 'children' }` in the traverse object, we're saying that when in a node of type `'root'`, the traversal should go through the field named `'children'`.
520
-
521
- ##### `$first`: _[string, string, ...]_
522
-
523
- When setting the traversal object field to `league: { $first: ['matches', 'children'] }` we're saying that when in a node of type `league`, the traversal should go through the **first existing field** in the list.
524
-
525
- ##### `$all`: _[string, string, ...]_
526
-
527
- Viceversa, when setting the traversal object to `team: { $all: ['parents', 'children'] }`, we're saying that when in a node of type `team`, the traversal should go through the **all the existing field** in the list.
528
-
529
- #### `$any` : _boolean | reference(s) field(s)_
530
-
531
- The previous example query ends with `$any: false` which act as a catch-all for the other node types, meaning that in any other node type there should be no traversal.
532
-
533
- ### `$recursive` : _boolean_
534
-
535
- If set to `true` within the `$find` operator, it recursevely traverses `references` nodes with the name specified in `$traverse`.
536
- For example, doing **a recursive traversal on the `children` field is equivalent to doing a traversal on `descendants`.**
537
-
538
- ```js
539
- const res = await client.get({
540
- $id: firstId,
541
- items: {
542
- id: true,
543
- title: true,
544
- $list: {
545
- $find: {
546
- $traverse: 'subclasses',
547
- $recursive: true,
548
- $filter: {
549
- $field: 'num',
550
- $operator: '=',
551
- $value: 180,
552
- },
553
- },
554
- },
555
- },
556
- })
557
- // This will recursively traverse all 'subclasses' references
558
- // starting from id firstId. (and turn 'items' into an array,
559
- // see later for $list operator)
560
- ```
561
-
562
- ### `$filter`: _object | array_
563
-
564
- Property of `$find`.
565
-
566
- Sets a search term for the `$find` operator.
567
- Has the following properties:
568
-
569
- - `$operator`: _string_ - Operator to use in the comparison.
570
- - `$field`: _string_ - Field name to compare the value to.
571
- - `$value`: _string_ - Value to compare the field to.
572
-
573
- The `$operator` has several possible types:
574
-
575
- | Operator | Field type | `value` format | Effect |
576
- | ------------- | ------------ | --------------- | ------------------------ |
577
- | `'='` | all | | is equal to |
578
- | `'!='` | all | | is not equal to |
579
- | `'>'` | number types | | is strictly greater than |
580
- | `'<'` | number types | | is strictly less than |
581
- | `'..'` | number types | [number,number] | is in range (inclusive) |
582
- | `'has'` | `set` | | value is in set |
583
- | `'exists'` | all | | field is set in node |
584
- | `'notExists'` | all | | field is not set in node |
585
-
586
- Search terms can be **composed with the `$or` and `$and` operators**, and nested to create complex logic.
587
- If an array of search terms is used, each term acts as an **AND**.
588
-
589
- ```js
590
- const result = await client.get({
591
- $id: 'mo2001ASpaceOdyssey',
592
- $language: 'en',
593
- title: true,
594
- genres: {
595
- name: true,
596
- $list: {
597
- $traverse: 'parents',
598
- $find: {
599
- $filter: {
600
- $field: 'type',
601
- $operator: '=',
602
- $value: 'genre',
603
- },
604
- },
605
- },
606
- },
607
- })
608
- ```
609
-
610
- #### `$or`: _object_
611
-
612
- Property of `$filter`.
613
- Adds a OR search term to the filter.
614
- Can be nested.
615
-
616
- #### `$and`: _object_
617
-
618
- - Property of `$filter`.
619
- - Adds a AND search term to the filter.
620
- - Can be nested.
621
-
622
- ```js
623
- const result = await client.get({
624
- $id: 'geScifi',
625
- $language: 'en',
626
- name: true,
627
- longMovies: {
628
- title: true,
629
- $list: {
630
- $traverse: 'children',
631
- $find: {
632
- $filter: {
633
- $field: 'type',
634
- $operator: '=',
635
- $value: 'movie',
636
- $and: {
637
- $field: 'technicalData.runtime',
638
- $operator: '>',
639
- $value: 100,
640
- },
641
- },
642
- },
643
- },
644
- },
645
- })
646
- ```
647
-
648
- ### `$list`: _boolean | object_
649
-
650
- - Sets the field to return **a collection of documents.**
651
-
652
- It can be set as a property of any field, turning it, if possible, into a list.
653
- It is possible to nest more operators in the list object to shape the collection of items.
654
-
655
- ```js
656
- const result = await client.get({
657
- $id: 'geScifi',
658
- $language: 'en',
659
- name: true,
660
- longMovies: {
661
- title: true,
662
- $list: {
663
- $traverse: 'children',
664
- },
665
- },
666
- },
667
- })
668
- // longMovies is now a list containing titles of the children of node 'geScifi'
669
- ```
670
-
671
- #### `$sort`: _object_
672
-
673
- Property of `$list` operator.
674
- Sorts the `$list` results according to the following properties:
675
-
676
- - `$field`: _string_ - Name of the field to sort by.
677
- - `$order`: _['asc', 'desc']_ - Sort in ascending or descending order.
678
-
679
- ```js
680
- const result = await client.get({
681
- $id: 'geScifi',
682
- $language: 'en',
683
- children: {
684
- title: true,
685
- year: true,
686
- $list: {
687
- $sort: { $field: 'year', $order: 'asc' },
688
- $find: {
689
- $traverse: 'children',
690
- },
691
- },
692
- },
693
- })
694
- // if any of the nodes doesnt have a 'year' field,
695
- // they get bumped to the end of the list, orderd by alphabetically by id.
696
- ```
697
-
698
- #### `$offset`: _integer_
699
-
700
- - Property of `$list` operator.
701
- - Shows results of a `$list` starting at the specified index.
702
-
703
- ```js
704
- const result = await client.get({
705
- $id: 'geScifi',
706
- $language: 'en',
707
- children: {
708
- title: true,
709
- year: true,
710
- $list: {
711
- $sort: { $field: 'year', $order: 'asc' },
712
- $offset: 0,
713
- $limit: 2,
714
- },
715
- },
716
- })
717
- ```
718
-
719
- #### `$limit`: _integer_
720
-
721
- - Property of `$list` operator.
722
- - Limits the `$list` amount of items returned in a `$list`.
723
-
724
- ## `$aggregate`: _object_
725
-
726
- The `$aggregate` operator takes the same arguments as `$find`, but instead of returning a node or node fields it returns a single number.
727
- The type of aggregation is defined by the `$function` operator, which takes one of `'count' | 'avg' | 'sum'`.
728
-
729
- | Func | Syntax | Return value |
730
- | ------- | --------------------------------------------- | -------------------------------------------------------------------------------------------------------------- |
731
- | `count` | - | How many nodes correspond to the query |
732
- | `sum` | `$function: {$name: 'sum', $args: ['value']}` | Sum together all `value` fields of every node found during the traversal. Can only be used on numerical types. |
733
- | `avg` | `$function: {$name: 'avg', $args: ['value']}` | Average of all `value` fields of every node found during the traversal. Can only be used on numerical types. |
734
-
735
- ```js
736
- await client.get({
737
- $id: 'root',
738
- id: true,
739
- valueSum: {
740
- $aggregate: {
741
- $function: { $name: 'sum', $args: ['value'] },
742
- $traverse: 'descendants',
743
- $filter: [
744
- {
745
- $field: 'type',
746
- $operator: '=',
747
- $value: 'match',
748
- },
749
- {
750
- $field: 'value',
751
- $operator: 'exists',
752
- },
753
- ],
754
- },
755
- },
756
- }),
757
- // Sums together all the `value` fields in the descendants of root
758
- // of type `match`, where the `value` field exists.
759
- ```