@olympeio/runtime-node 9.0.2 → 9.0.5

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.
package/types/cloud.d.ts CHANGED
@@ -7,60 +7,173 @@
7
7
  // @ts-ignore
8
8
  import Observable from 'rxjs';
9
9
  import {Context, CloudObject, Relation, Property, Tag, Class} from "./base";
10
- import { BrickContext } from './runtime';
10
+ import {BrickContext} from './runtime';
11
11
  import {Color} from "./utils";
12
12
 
13
13
  // --------------------------
14
14
  // -- Primitive data types --
15
15
  // --------------------------
16
+ /**
17
+ * A property model defines a property of a data type.
18
+ * A property is defined by its type and the model it defines a property for.
19
+ * */
16
20
  export class PropertyModel extends CloudObject {
21
+ /**
22
+ * Relation from the property model to the data type (model)
23
+ * to which that property is attached.
24
+ *
25
+ * Example:
26
+ *
27
+ * ` myNumberPropertyModel --definingModelRel-> myDataTypeObject`
28
+ */
17
29
  static definingModelRel: Relation<PropertyModel, CloudObject>;
30
+
31
+ /**
32
+ * Relation from the `PropertyModel` to its type.
33
+ *
34
+ * Example :
35
+ *
36
+ * `myNumberPropertyModel --typeRel-> Number`
37
+ */
18
38
  static typeRel: Relation<PropertyModel, CloudObject>;
19
39
  }
20
40
 
41
+ /**
42
+ * A relation model defines a relation between two data types.
43
+ * It defines what data types is the origin and the destination of the relation.
44
+ *
45
+ * It also defines follow rules properties on the relation.
46
+ * */
21
47
  export class RelationModel extends CloudObject {
48
+ /**
49
+ * Relation from the origin data type to the RelationModel
50
+ * */
22
51
  static originModelRel: Relation<CloudObject, RelationModel>;
52
+ /**
53
+ * Relation from a RelationModel to the destination data type
54
+ * */
23
55
  static destinationModelRel: Relation<RelationModel, CloudObject>;
56
+ /**
57
+ * Property for the *dump* follow rule
58
+ */
24
59
  static dumpFollowRuleProp: Property<number>;
60
+ /**
61
+ * Property for the *delete* follow rule
62
+ */
25
63
  static deleteFollowRuleProp: Property<number>;
64
+ /**
65
+ * Property for the *runtime* follow rule
66
+ */
26
67
  static runtimeFollowRuleProp: Property<number>;
27
68
  }
28
69
 
70
+ /**
71
+ * StringModel represents a {@link PropertyModel} for a string property of a data type.
72
+ */
29
73
  export class StringModel extends CloudObject {
30
74
  static valueProp: Property<string>;
31
75
  }
32
76
 
77
+ /**
78
+ * NumberModel represents a {@link PropertyModel} for a number property of a data type.
79
+ */
33
80
  export class NumberModel extends CloudObject {
34
81
  static valueProp: Property<number>;
35
82
  }
36
83
 
84
+ /**
85
+ * BooleanModel represents a {@link PropertyModel} for a boolean property of a data type.
86
+ */
37
87
  export class BooleanModel extends CloudObject {
38
88
  static valueProp: Property<boolean>;
39
89
  }
40
90
 
91
+ /**
92
+ * DatetimeModel represents a {@link PropertyModel} for a datetime property of a data type.
93
+ */
41
94
  export class DatetimeModel extends CloudObject {
42
95
  static valueProp: Property<Date>;
43
96
  }
44
97
 
98
+ /**
99
+ * ColorModel represents a {@link PropertyModel} for a color property of a data type.
100
+ */
45
101
  export class ColorModel extends CloudObject {
46
102
  static valueProp: Property<Color>;
47
103
  }
48
104
 
105
+ /**
106
+ * File with content as binary or string data
107
+ */
49
108
  export class File extends CloudObject {
109
+
50
110
  static nameProp: Property<string>;
51
111
  static creationDateProp: Property<Date>;
52
112
  static modificationDateProp: Property<Date>;
53
113
  static mimeTypeProp: Property<string>;
54
114
  static urlProp: Property<string>;
115
+
116
+ /**
117
+ * Create a `File` from its content
118
+ *
119
+ * @param transaction transaction in which to create the file
120
+ * @param name filename
121
+ * @param content byte content of the file
122
+ * @param mimeType optional mime type of the file
123
+ * @param source optional source where file will be stored ('server', 'self' or DBConnector tag)
124
+ * @param tag optional tag for the file
125
+ * @return tag string of the file
126
+ */
55
127
  static createFromContent(transaction: Transaction, name: string, content: ArrayBuffer, mimeType?: string, source?: string, tag?: string): string;
128
+
129
+ /**
130
+ * Create a `File` from a specified URL
131
+ *
132
+ * @param transaction transaction in which to create the file
133
+ * @param name filename
134
+ * @param url url to retrieve content from
135
+ * @param mimeType optional mime type of the file
136
+ * @param source optional source where file will be stored ('server', 'self' or DBConnector tag)
137
+ * @param tag optional tag for the file
138
+ * @return tag string of the file
139
+ */
56
140
  static createFromURL(transaction: Transaction, name: string, url: string, mimeType?: string, source?: string, tag?: string): string;
57
141
 
142
+ /**
143
+ * Retrieve content from this file asynchronously
144
+ *
145
+ * @param onSuccess callback to execute when byte content has been retrieved successfully
146
+ * @param onFailure callback to execute when content retrieval has failed
147
+ */
58
148
  getContentAsBinary(onSuccess: (content: ArrayBuffer) => void, onFailure?: (string) => void): void;
149
+
150
+ /**
151
+ * Retrieve string content from this file asynchronously
152
+ *
153
+ * @param onSuccess callback to execute when string content has been retrieved successfully
154
+ * @param onFailure callback to execute when content retrieval has failed
155
+ */
59
156
  getContentAsString(onSuccess: (content: string) => void, onFailure?: (string) => void): void;
157
+
158
+ /**
159
+ * Retrieve string content from URL asynchronously
160
+ *
161
+ * @param onSuccess callback to execute when content has been retrieved successfully
162
+ * @param onFailure callback to execute when content retrieval has failed
163
+ */
60
164
  getContentUrl(onSuccess: (content: string) => void, onFailure?: () => void): void;
165
+
166
+ /**
167
+ * Save this file with specified name
168
+ *
169
+ * @param name filename
170
+ */
61
171
  saveAs(name: string): void;
62
172
  }
63
173
 
174
+ /**
175
+ * User object for authentication purposes
176
+ */
64
177
  export class User extends CloudObject {
65
178
  static loginProp: Property<string>;
66
179
  static saltProp: Property<string>;
@@ -68,16 +181,56 @@ export class User extends CloudObject {
68
181
  static SAMLNameIdProp: Property<string>;
69
182
  }
70
183
 
184
+ /**
185
+ * An Enum is an ordered list of string key-values mappings
186
+ */
71
187
  export class Enum extends CloudObject {
188
+ /**
189
+ * Create an empty `Enum?` without any value
190
+ *
191
+ * See {@link EnumValue#createValue} to create values for an Enum.
192
+ *
193
+ * @param transaction transaction in which to create the Enum
194
+ * @param name optional name of the Enum
195
+ * @return string tag of the Enum object
196
+ */
72
197
  create(transaction: Transaction, name?: string): string;
198
+
199
+ /**
200
+ * Get the values defined for this `Enum`
201
+ */
73
202
  getValues(): QueryResult<EnumValue>;
74
203
  }
75
204
 
205
+ /**
206
+ * An `EnumValue` is a single value part of an `Enum`.
207
+ * It has a `string` name, a rank (its position in the Enum list).
208
+ *
209
+ * The values for `EnumValue` are always stringified.
210
+ */
76
211
  export class EnumValue extends CloudObject {
212
+ /**
213
+ * Add a `EnumValue` to a specified `Enum`
214
+ *
215
+ * @param transaction transaction in which to add the EnumValue to the Enum
216
+ * @param enumModel `EnumModel` containing the specified `EnumValue`
217
+ * @param value value of the `EnumValue`
218
+ * @param name name of the `EnumValue`
219
+ * @param rank position of the `EnumValue` in the `Enum`
220
+ */
77
221
  static createValue(transaction: Transaction, enumModel: Tag, value: string, name?: string, rank?: number): string;
78
222
 
223
+ /**
224
+ * Name property for an EnumValue
225
+ */
79
226
  static nameProp: Property<string>;
227
+ /**
228
+ * Value property for an EnumValue
229
+ */
80
230
  static valueProp: Property<string>;
231
+ /**
232
+ * Rank property for an EnumValue
233
+ */
81
234
  static rankProp: Property<number>;
82
235
  }
83
236
 
@@ -87,7 +240,10 @@ export class EnumValue extends CloudObject {
87
240
 
88
241
  /**
89
242
  * Transactions are the unique way to apply a list of operations to the datacloud.
90
- * A transaction can be merged into another transaction to be executed as a single set of operations.
243
+ * A transaction can be merged into another transaction so that both are executed as a single set of operations.
244
+ *
245
+ * All the operations for a transaction are executed atomically. Either all operations succeed, or none is applied.
246
+ *
91
247
  *
92
248
  * There are 5 types of basic operations :
93
249
  * - Create instance: create a node in the database with specified properties.
@@ -96,51 +252,226 @@ export class EnumValue extends CloudObject {
96
252
  * - Create relation: create a relation of a specific type between 2 existing node.
97
253
  * - Delete relation: remove the specified relation between 2 specified nodes.
98
254
  *
99
- * Classical transactions are executed using "execute()" method. The transaction size is limited but real-time but
100
- * all the changes applied on data "observed" by queries will be notified.
255
+ * In most cases, we apply the Transactions operations using the "execute()" method. The transaction size is limited but in real-time.
256
+ * It notifies observers of the data in real-time.
101
257
  *
102
- * Transactions with big set of operations (batch updates) can be executed using "executeAsLarge()" method. However no notification will be generated.
258
+ * Larger transactions with big set of operations (batch updates) must be executed using "executeAsLarge()" method. However no notification will be generated.
103
259
  *
104
260
  * Callbacks can be registered on transaction using "afterExecution()" method. They are executed once the transaction is applied.
105
261
  *
262
+ * Example of transaction:
263
+ *
264
+ * ```javascript
265
+ * const tx = new Transaction(true);
266
+ * const myNewInstanceTag = tx.create(<myModelTag>);
267
+ * tx.update(myNewInstanceTag, <myPropertyTag>, <myPropertyValue>)
268
+ * .execute()
269
+ * .then(() => {
270
+ * // success tx case
271
+ * })
272
+ * .catch((err) => {
273
+ * // error during tx case
274
+ * })
275
+ * ```
276
+ *
106
277
  * Concurrency transactions do not guarantee their execution order.
107
278
  */
108
279
  export class Transaction {
109
280
  constructor(persist?: boolean);
110
281
 
282
+ /**
283
+ * Get the transaction id attached to this transaction
284
+ * @return this transaction id
285
+ */
111
286
  getId(): string;
112
287
 
113
288
  // Operations
289
+ /**
290
+ * Create a new instance.
291
+ *
292
+ * A custom map can specify property values for the instance.
293
+ *
294
+ * A source can be the orchestrator ('server'), local ('self') or
295
+ * an external data source ('<DBConnectorTag>'). The source is where
296
+ * the object is persisted and its true value outside local scopes.
297
+ *
298
+ * @param model tag of the model of the instance to be created
299
+ * @param properties custom map from tag to their value for properties of the instance to be created
300
+ * @param source optional source of the instance
301
+ * @param tag optional tag of the instance to be created and must be unique
302
+ * @return the tag of the instance to be created
303
+ */
114
304
  create(model: Tag, properties?: Map<Tag, any>, source?: string, tag?: string): string;
305
+
306
+ /**
307
+ * Update the property of an instance
308
+ *
309
+ * @param instance tag of the instance to be updated
310
+ * @param property the property to be updated
311
+ * @param value the value of the property to be updated to
312
+ * @return this transaction
313
+ */
115
314
  update<T>(instance: Tag, property?: Property<T>, value?: T): this;
116
- multiUpdate(instance: Tag, properties: Map<string, any>): this;
315
+
316
+ /**
317
+ * Update multiple properties of a single instance
318
+ *
319
+ * @param instance tag of the instance to be updated
320
+ * @param properties (<propertyTag>, <propertyValue>) map to update properties
321
+ * @return this transaction
322
+ */
323
+ multiUpdate(instance: Tag, properties: Map<Tag, any>): this;
324
+
325
+ /**
326
+ * Delete an instance
327
+ *
328
+ * @param instance tag of the instance to be deleted
329
+ * @return this transaction
330
+ */
117
331
  delete(instance: Tag): this;
332
+
333
+ /**
334
+ * Create relation between two instances
335
+ *
336
+ * @param relation tag of the relation to be created
337
+ * @param from tag of the ORIGIN instance of the relation
338
+ * @param to tag ofo the DESTINATION instance of the relation
339
+ * @return this transaction with the create relation operation registered
340
+ */
118
341
  createRelation(relation: Tag, from: Tag, to: Tag): this;
342
+
343
+ /**
344
+ * Delete a relation between two specified instances.
345
+ *
346
+ * The relation is only deleted for the relation parameter direction.
347
+ *
348
+ * @param relation tag of the relation to be deleted
349
+ * @param from origin instance tag
350
+ * @param to destination instance tag
351
+ * @return this transaction
352
+ */
119
353
  deleteRelation(relation: Tag, from: Tag, to: Tag): this;
354
+
355
+ /**
356
+ * Delete any number of the relation starting from specified node.
357
+ *
358
+ * The specified node might be the origin or (exclusive) the destination of the relation.
359
+ *
360
+ * For the relations :
361
+ * ```
362
+ * - a - [rel1] -> b,
363
+ * - c - [inverseRel(rel1)] -> a,
364
+ * - a - [rel1] -> d,
365
+ * - a - [rel2] -> d
366
+ * ```
367
+ * `tx.deleteAllRelation(rel1, a)` will remove the first and third relations
368
+ *
369
+ * @param relation deleted relation, indicates relation tag *and* direction
370
+ * @param origin starting node
371
+ */
120
372
  deleteAllRelations(relation: Relation<any, any>, origin: Tag): this;
121
373
 
122
- // Change persistence of instances
374
+ /**
375
+ * Change the persistence mode of all instances in this transaction
376
+ *
377
+ * @param persist whether instances are persisted outside
378
+ * the local datacloud
379
+ * @return this transaction
380
+ */
123
381
  persist(persist?: boolean): this;
382
+
383
+ /**
384
+ * Change the persistence mode for a single instance in this transaction
385
+ *
386
+ * @param instance the instance to be persisted
387
+ * @param persist the persisting mode
388
+ * @return this transaction
389
+ */
124
390
  persistInstance(instance: Tag, persist?: boolean): this;
125
391
 
126
- // Set source of all instances created in this transaction
392
+ /**
393
+ * Change the source of all instances created in this transaction
394
+ *
395
+ * `source` can be specified as :
396
+ * 1. the orchestrator ('server'),
397
+ * 2. local ('self') or
398
+ * 3. an external data source ('<DBConnectorTag>')
399
+ *
400
+ * The source of a data object is where the object is persisted.
401
+ *
402
+ * By default, the `source` is the default source configured in the project.
403
+ *
404
+ * @param source the new source for instances
405
+ * @return this transaction
406
+ */
127
407
  setSource(source: string): this;
408
+
409
+ /**
410
+ * Retrieve the tag of the model of the instance tag
411
+ * from the created instances in this transaction
412
+ * or from the local database.
413
+ *
414
+ * @param tag the instance to find the model of
415
+ * @return the tag of the model of the given instance
416
+ */
128
417
  model(tag: Tag): string;
129
418
 
419
+ /**
420
+ * Add all operations of another transaction into this transaction
421
+ *
422
+ * @param otherTransaction the other transaction to add operations from
423
+ * @return this transaction
424
+ */
130
425
  merge(otherTransaction: Transaction): this;
131
426
 
132
427
  // Execution methods
428
+ /**
429
+ * Set a callback to be executed after all the operations
430
+ * in the transaction were executed successfully
431
+ *
432
+ * @param callback the callback to be executed
433
+ * @return this transaction
434
+ */
133
435
  afterExecution(callback: (success: boolean, message?: string) => void): this;
436
+
437
+ /**
438
+ * Execute atomically the transaction at the source
439
+ *
440
+ * @return promise resolving if the transaction succeeds,
441
+ * rejecting if the transaction fails
442
+ */
134
443
  execute(): Promise<void>;
444
+
445
+ /**
446
+ * Execute atomically a transaction at the source
447
+ * The `large` mode sends the transaction over HTTP
448
+ *
449
+ * @return promise resolving if the transaction succeeds,
450
+ * rejecting if the transaction fails.
451
+ */
135
452
  executeAsLarge(): Promise<void>;
136
453
 
137
- // Begin/End shared transaction helpers
454
+ /**
455
+ * Start a transaction using an existing transaction in the provided context
456
+ * or a new one if there is none.
457
+ *
458
+ * @param $ context in which the transaction executes
459
+ * @return transaction
460
+ */
138
461
  static from($: BrickContext): Transaction;
462
+
463
+ /**
464
+ * Execute a given transaction if there is no open transaction in the context.
465
+ *
466
+ * @param $ context
467
+ * @param transaction transaction to process
468
+ * @return boolean indicating if the argument transaction has been processed
469
+ */
139
470
  static process($: BrickContext, transaction: Transaction): Promise<boolean>;
140
471
  }
141
472
 
142
473
  /**
143
- * BurstTransactions are designed to apply updates of properties continuously with the matter of the order.
474
+ * BurstTransactions are designed to continuously apply high rates of updates on properties (FIFO ordering).
144
475
  * The only operation supported by BurstTransactions is to update properties of an instance.
145
476
  *
146
477
  * Unlike classical Transactions, a burst transaction preserves the order of update operations and
@@ -153,10 +484,19 @@ export class Transaction {
153
484
  export class BurstTransaction {
154
485
  constructor();
155
486
 
156
- // Push all the property values from the specified Observable to update the specified object.
487
+ /**
488
+ * Push all property values from specified Observable to update specified object
489
+ *
490
+ * @param object the object to update
491
+ * @param values observable of a `Map<Property<T>, T>` mapping properties to new values
492
+ */
157
493
  push<T>(object: CloudObject, values: Observable<Map<Property<T>, T>>): void;
158
494
 
159
- // Commit and close the burst transaction.
495
+ /**
496
+ * Commit and close the burst transaction
497
+ *
498
+ * @return Promise<void> completing with commit success/failure
499
+ */
160
500
  complete(): Promise<void>;
161
501
  }
162
502
 
@@ -166,65 +506,297 @@ export class BurstTransaction {
166
506
  type Empty = [];
167
507
 
168
508
  /**
169
- * A CloudArray (type: QueryResult) is a array of tuple of CloudObjects that has been generated by a Query. It contains the result of a query at a specific time.
509
+ * A QueryResult is a list of key-value pairs that has been generated as a result of a `Query`.
510
+ * It contains the result of a query at a specific time.
170
511
  *
171
- * When a query is subscribed, it generates multiple CloudArrays along time and got via an observable.
172
- * Difference between CloudArrays from a Query is accessible using methods getAdded() and getRemoved().
512
+ * The keys are tags (e.g. `myTag`) that correspond to a `CloudObject` (e.g. `myCloudObject`).
513
+ * When the {@link Query} contains more than one [Query.andReturn()]{@link Query#andReturn} clause,
514
+ * keys are composite tags (e.g. `'tag1.tag2.tag3'`) and values are tuples of `CloudObjects` (e.g. `[cloudObj1, cloudObj2, cloudObj3]`).
515
+ * Such a QueryResult can be obtained via:
516
+ * ```javascript
517
+ * Query.instancesOf(myModel1).andReturn.follow(myRelation1).andReturn.follow(myRelation2).andReturn().execute().
518
+ * ```
173
519
  *
174
- * A CloudArray can be easily manipulated and transformed to a standard Array.
520
+ * When a query is subscribed, the returned observable generates multiple QueryResults over time.
175
521
  *
176
- * Keys of CloudObject tuples follow the rule: 'tag1.tag2.tag3' : [cloudObj1, cloudObj2, cloudObj3]
522
+ * A QueryResult can be easily manipulated and transformed to a standard Array.
177
523
  */
178
524
  export class QueryResult<T> {
525
+ /**
526
+ *@return empty `QueryResult`
527
+ */
179
528
  static empty(): QueryResult<any>;
180
529
 
530
+ /**
531
+ * Symbol iterator for `for..in` syntactic sugar
532
+ *
533
+ * @return iterator over the key-values pairs of this `QueryResult`
534
+ */
181
535
  [Symbol.iterator](): IterableIterator<[string, T]>;
536
+
537
+ /**
538
+ * Get an iterable iterator over the key-value pairs of this `QueryResult`
539
+ *
540
+ * @return iterator
541
+ */
182
542
  entries(): IterableIterator<[string, T]>;
543
+
544
+ /**
545
+ * Get an iterable iterator over the values of this `QueryResult`
546
+ *
547
+ * @return iterator
548
+ */
183
549
  values(): IterableIterator<T>
550
+
551
+ /**
552
+ * Get an iterable iterator over the keys of this `QueryResult`
553
+ *
554
+ * @return iterator
555
+ */
184
556
  keys(): IterableIterator<string>
185
557
 
558
+ /**
559
+ * Get an array containing the values of this `QueryResult`
560
+ *
561
+ * @return array of this `QueryResult`'s values
562
+ */
186
563
  toArray(): T[];
564
+
565
+ /**
566
+ * Get the number of key-value pairs this `QueryResult` contains
567
+ *
568
+ * @return size of this `QueryResult`
569
+ */
187
570
  size(): number;
571
+
572
+ /**
573
+ * Check whether a key / value is contained in this `QueryResult`.
574
+ *
575
+ * Returns true if the cloud array contains an element with the specified key.
576
+ *
577
+ * If no corresponding key is found, returns true if the cloud array contains an element with the specified value.
578
+ *
579
+ * @param key the value to search for
580
+ * @return true if the value `key` is found
581
+ * @TODO why T, it's CloudObjects
582
+ */
188
583
  has(key: T | string): boolean;
584
+
585
+ /**
586
+ * Get index of a value in the keys or values of this `QueryResult`.
587
+ *
588
+ * If the key is a string, retrieve the index from the `string` keys of this `QueryResult`.
589
+ *
590
+ * If the key is not a string, get the index from the `CloudObject` tuples
591
+ *
592
+ * @param key the value to find the index of
593
+ * @return index of the argument value or -1 if the value is not found
594
+ * TODO why T, it's CloudObjects
595
+ */
189
596
  indexOf(key: T | string): number;
597
+
598
+ /**
599
+ * Get the value corresponding to a given key
600
+ *
601
+ * @param key tag or composite tag
602
+ * @return matching tuple or `null`
603
+ */
190
604
  get(key: string): T | null;
605
+
606
+ /**
607
+ * Get the first value
608
+ *
609
+ * @return first value or `null` if empty
610
+ */
191
611
  getFirst(): T | null;
612
+
613
+ /**
614
+ * Get the value at the specified index
615
+ *
616
+ * @param index
617
+ * @return value at position `index` or `null` if index is out of bounds
618
+ */
192
619
  getAt(index: number): T | null;
193
620
 
621
+ /**
622
+ * Returns an array equivalent to [this.toArray().push(...object)]{@link toArray}
623
+ *
624
+ * This operation returns a new array and does not modify this QueryResult.
625
+ *
626
+ * @param object new values to push in the values array.
627
+ * @return new values array with new elements appended
628
+ */
194
629
  push(...object: T[]): T[];
630
+
631
+ /**
632
+ * Returns the QueryResult values as an array whose last value has been removed.
633
+ *
634
+ * If [QueryResult.size()]{@link size} is 0 or 1, this method returns an empty array.
635
+ *
636
+ * This operation returns a new array and does not modify this QueryResult.
637
+ *
638
+ * @return new values array with last element removed
639
+ */
195
640
  pop(): T[];
641
+
642
+ /**
643
+ * Returns the QueryResult values as an array whose first value has been removed.
644
+ *
645
+ * If [QueryResult.size()]{@link size} is 0 or 1, this method returns an empty array.
646
+ *
647
+ * This operation returns a new array and does not modify this QueryResult.
648
+ *
649
+ * @return new values array with first element removed
650
+ */
196
651
  shift(): T[];
197
652
 
198
- // Gives the difference between last cloud array => what instances added, and what where removed.
653
+ /**
654
+ * Returns the new values added since the last `QueryResult` update.
655
+ * In other words, `getAdded` returns the new values that exists in this query result, but did not in the previous query result pushed by the Observable.
656
+ *
657
+ * This applies only when *observing* a [Query]{@link Query#observe}.
658
+ *
659
+ * @return values added
660
+ */
199
661
  getAdded(): T[];
662
+
663
+ /**
664
+ * Returns the keys of values removed since the last `QueryResult` update.
665
+ * In other words, `getRemoved` returns the old values that existed in the previous query result, but are no longer present in this query result pushed by the Observable.
666
+ *
667
+ * This applies only when *observing* a [Query]{@link Query#observe}.
668
+ *
669
+ * @return keys of the removed values
670
+ */
200
671
  getRemoved(): string[] | string[][];
201
672
 
673
+ /**
674
+ * Returns an array containing the values matching the provided filter predicate.
675
+ *
676
+ * @param predicate filter {@link Predicate}
677
+ * @return array of matching values
678
+ */
202
679
  filter(predicate: (entry: T) => boolean): T[];
680
+
681
+ /**
682
+ * Returns the first value matching the predicate, `null` otherwise.
683
+ *
684
+ * `find` calls `predicate` once for each element of the array until it returns true.
685
+ * If such an element is found, find immediately returns that element value.
686
+ *
687
+ * @param predicate
688
+ * @return the first value evaluating to true or `null`
689
+ */
203
690
  find(predicate: (entry: T) => boolean): T | null;
204
691
 
692
+ /**
693
+ * Execute the specified callback function for each value of this `QueryResult`
694
+ *
695
+ * @param callback
696
+ */
205
697
  forEach(callback: (entry: T) => void): void;
698
+
699
+ /**
700
+ * Execute the specified callback function for each value of this `QueryResult`.
701
+ * It returns a new array that contains the results of the callback execution.
702
+ *
703
+ * @param callback
704
+ * @return new array of transformed values
705
+ */
206
706
  map<S>(callback: (entry: T) => S): S[];
707
+
708
+ /**
709
+ * Return a sorted copy of this QueryResult.
710
+ *
711
+ * The specified `comparator` used to determine the order of the QueryResult key-value pairs.
712
+ * It compares two QueryResult values and should return a negative number if the first value shall be present before the second, zero if they're at the same position, and a positive
713
+ * number otherwise.
714
+ *
715
+ * @param comparator comparator returning a negative, zero or positive number
716
+ * @return sorted copy of this QueryResult
717
+ */
207
718
  sort(comparator: (b1: T, b2: T) => number): QueryResult<T>;
719
+
720
+ /**
721
+ * Return a reduced value for this QueryResult.
722
+ *
723
+ * The specified `reducer` callback reduces the QueryResult values as [reduce]{@link Array#reduce}
724
+ *
725
+ * @param reducer callback
726
+ * @param initial initial value of the accumulator
727
+ * @return accumulated result applying the `reducer` function iteratively
728
+ */
208
729
  reduce<S>(reducer: (accumulator: S, entry: T) => S, initial: S): S;
730
+
731
+ /**
732
+ * Return a new QueryResult that contains the concatenation of two or more QueryResults.
733
+ *
734
+ * Concatenating two QueryResults results in a new QueryResult with the following:
735
+ *
736
+ * - the keys of the QueryResults are concatenated
737
+ * - the values of the QueryResults are concatenated
738
+ * - the arrays returned by {@link getAdded} are concatenated
739
+ * - the arrays returned by {@link getRemoved} are concatenated
740
+ *
741
+ * Example : ```javascript
742
+ * const result1 = Query.fromInstances(myModel1).executeFromCache();
743
+ * // result1 is {tag1: cloudObj1, tag2: cloudObj2}
744
+ * const result2 = Query.fromInstances(myModel2).executeFromCache();
745
+ * // result2 is {tag3: cloudObj3}
746
+ * const concatenatedResult = result2.concat(result1);
747
+ * // concatenatedResult is {tag3: cloudObj3, tag1: cloudObj1, tag2: cloudObj2}
748
+ * ```
749
+ *
750
+ * @param others QueryResults to append to this `QueryResult`
751
+ * @return concatenated query results
752
+ */
209
753
  concat(...others: QueryResult<T>[]): QueryResult<T>;
210
754
  }
211
755
 
212
756
  /**
213
757
  * A Query is an immutable object used to build queries on the datacloud. It is a graph query builder.
214
- * Starting from an origin Tag (CloudObject or its unique id), it follows specified relations:
758
+ * It starts from an `origin` Tag (typically, the `Tag` of a data type or of an instance).
759
+ * From there, it can follow relations between data types and filter results.
215
760
  *
216
- * origin --a--> X --b--> Y --c--> Z
761
+ * Example:
762
+ *
763
+ * `origin --a--> X --b--> Y --c--> Z`
217
764
  *
218
- * At each level, "andReturn()" method can be called to add the current level to the result.
219
765
  * Example:
220
766
  *
767
+ * ```javascript
768
+ * Query.from(origin)
769
+ * .follow(a)
770
+ * .execute();
771
+ * ```
772
+ * This will return a Promise of a QueryResult whose values are of type X: `Promise<QueryResult<X>>`.
773
+ *
774
+ * A query defines a starting working set of nodes and operations that will mutate it.
775
+ * From our example,the working set is first the `origin` graph node. Then the `follow`
776
+ * operations create a new working set only containing the nodes related to the origin through relation *`a`*.
777
+ *
778
+ * Multiple relations can be followed at once to create the equivalent of JOIN operations in relational databases.
779
+ * ```javascript
221
780
  * Query.from(origin)
222
781
  * .follow(a).andReturn()
223
782
  * .follow(b)
224
783
  * .follow(c).andReturn()
225
784
  * .execute();
785
+ * ```
786
+ * The next operation defines a working set containing the nodes related
787
+ * through relation *`b`* to nodes that are related through relation *`a`* to the origin.
788
+ * A similar operation is defined for relation `c`.
789
+ * At each level, "andReturn()" method can be called to add the current level to the result.
790
+ * This will return a Promise of a QueryResult whose values are of type [X, Z]: `Promise<QueryResult<[X, Z]>>`.
791
+ *
792
+ * Out of the 4 working sets created, only the second and fourth are flagged to be returned and part of the result.
793
+ * But the operations in the middle impact this result: e.g.,
794
+ * all returned tuples (t1, t2) are object that have an intermediate `i` of type Y.
795
+ * such that `t1 --b-> i --c-> t2` but the `i` was not flagged to be returned.
796
+ *
797
+ * Queries define a path from a starting point following a specific list of relations.
798
+ * As explained with our example, only nodes part of a full path can be returned.
226
799
  *
227
- * This will return a Promise of a of tuple [X, Z]: Promise<CloudArray<[X, Z]>>.
228
800
  *
229
801
  * A query can be executed with 3 different ways:
230
802
  * - execute(): it runs the query on the datacloud and return the result as a QueryResult.
@@ -232,68 +804,346 @@ export class QueryResult<T> {
232
804
  * - executeFromCache(): it runs the query on the *local* datacloud which may be incomplete view of the datacloud (it is the local cache). It is executed synchronously.
233
805
  */
234
806
  export class Query<T extends CloudObject, R extends any[]> {
235
- static from<T extends CloudObject>(tag: Tag): Query<T, Empty>;
236
- static instancesOf<T extends CloudObject>(model: Class<T>, source?: string): Query<T, Empty>;
237
807
 
238
- follow<D extends CloudObject>(relation: Relation<T, D>): Query<D, R>; //
808
+ /**
809
+ * Create a query starting from the `CloudObject` specified tag
810
+ *
811
+ * @param tag tag of the `CloudObject` the query is starting from
812
+ * @param source optional source of the data to answer the query
813
+ * @return an empty Query whose starting point is defined by a single `CloudObject`
814
+ */
815
+ static from<T extends CloudObject>(tag: Tag, source?: string): Query<T, Empty>;
816
+
817
+ /**
818
+ * Create a query starting from the instances of the specified model
819
+ *
820
+ * @param model tag/class of the model to find the instances of
821
+ * @param source optional source of the data to answer the query
822
+ * A source can be the orchestrator ('server'), local ('self') or
823
+ * an external data source ('<DBConnectorTag>')
824
+ * @return a new query object starting from model instances
825
+ */
826
+ static instancesOf<T extends CloudObject>(model: Tag, source?: string): Query<T, Empty>;
827
+
828
+ /**
829
+ * Instruct the query to follow a specified relation. This does not add any key-value pair to the result.
830
+ * (see {@link andReturn}). This operation defines a new step in the path defined by our graph query.
831
+ *
832
+ * Example:
833
+ * Find the model of a single object.
834
+ * ```javascript
835
+ * Query.from(myTag).follow(CloudObject.modelRel).andReturn()
836
+ * ```
837
+ *
838
+ * @param relation relation to follow, specifies type and direction
839
+ * @return a new query object
840
+ */
841
+ follow<D extends CloudObject>(relation: Relation<T, D>): Query<D, R>;
842
+
843
+ /**
844
+ * Follow a relation recursively, see {@link follow}.
845
+ * The relation that is followed is automatically recursively followed.
846
+ * All nodes that are recursively followed are part of the same step in the path defined by this graph query.
847
+ *
848
+ * @param relation relation to follow recursively, specifies type and direction
849
+ * @param includeSelf if a starting node includes itself when the relation loops on itself
850
+ * @return a new query object
851
+ */
239
852
  followRecursively<D extends CloudObject>(relation: Relation<T, D>, includeSelf?: boolean): Query<D, R>;
240
853
 
241
- andReturn(): Query<T, [...R, T]>; // Add the current level of the query to the result. (Listdefs only support the last level to be returned).
854
+ /**
855
+ * Add the current working set of nodes to the result.
856
+ *
857
+ * Example:
858
+ * Query returning the instances of a model and the model itself
859
+ * ```javascript
860
+ * Query
861
+ * .from(myModelTag).andReturn()
862
+ * .follow(CloudObject.modelRel.getInverse()).andReturn()
863
+ * .execute();
864
+ * ```
865
+ * The result tuples are [modelCloudObject, instance1], [modelCloudObject, instance2], ...
866
+ *
867
+ * @return a new query object with the current level flagged to be returned.
868
+ */
869
+ andReturn(): Query<T, [...R, T]>;
242
870
 
871
+ /**
872
+ * Define a filter operation on the current working set of nodes.
873
+ * It filters out instances that don't match the given {@link Predicate}
874
+ *
875
+ * @param predicate
876
+ * @return a new query object with the new filter operation
877
+ */
243
878
  filter(predicate: Predicate): Query<T, R>;
879
+
880
+ /**
881
+ * Cast the type of the instances in the current working set of nodes
882
+ *
883
+ * @param type cast type for `CloudObjects`
884
+ * @return the
885
+ */
244
886
  cast<S extends CloudObject>(type: Class<S>): Query<S, R>;
245
887
 
246
- // Methods for which only the last call matters
888
+ /**
889
+ * Set the maximum number of tuples the executed query will return with an optional offset.
890
+ *
891
+ * Only the last call to limit matters.
892
+ *
893
+ * Example :
894
+ * ```javascript
895
+ * const queryLimited = query.limit(50).follow(myRel2).limit(100, 200).follow(myRel1);
896
+ * // is equivalent to
897
+ * const queryLimited2 = query.follow(myRel2).follow(myRel1).limit(100, 200);
898
+ * ```
899
+ * `queryLimited`, when executed, will return at most the 200th to the 299th (included) results.
900
+ *
901
+ * @param max maximum number of tuple to fetch from the final result
902
+ * @param offset number of skipped tuples for the final result
903
+ * @return new query with limit operation
904
+ */
247
905
  limit(max: number, offset?: number): Query<T, R>;
906
+
907
+ /**
908
+ * Sort the result tuples according to a property of a data type with a specified order.
909
+ **
910
+ * For the sortBy operator to be properly defined, the following must be true:
911
+ * - the property is defined on the instances that will be used for sorting.
912
+ * - the instances on which the sorting property is defined must be part of the result,
913
+ * i.e., `andReturn()` has been called before `sortBy()`.
914
+ *
915
+ * Only the last call to `sortBy` matters as it will be the last comparator set on the query.
916
+ * You should remove previous calls to `sortBy` for the same query.
917
+ *
918
+ * Example :
919
+ * ```javascript
920
+ * const sortQuery = Query
921
+ * .fromInstances(myModel1).andReturn()
922
+ * .sortBy(nameProperty)
923
+ * .follow(rel1).andReturn()
924
+ * .execute();
925
+ * ```
926
+ * This query fetches instances of a model, flags them as first element of result tuples.
927
+ * Then the query follows a relation to other instances and return them as second element of the result tuples.
928
+ * `sortBy` will sort the results according to the `nameProperty` of the first element in the result tuples in
929
+ * ascending order.
930
+ *
931
+ * @param property the property used for sorting
932
+ * @param order optional [Order.ASC]{@link Order#ASC} (default) or [Order.DESC]{@link Order#DESC} order
933
+ * @return new query with sort operation
934
+ */
248
935
  sortBy(property: Property<any>, order?: Order): Query<T, R>;
249
936
 
250
- execute(context: Context): Promise<QueryResult<R>>; // Execute the query on the datacloud (as a listdef) and deletes it afterwards.
251
- observe(context: Context): Observable<QueryResult<R>>; // Generate a subscription and get a new CloudArray for every update.
937
+ /**
938
+ * Execute the query asynchronously on the datacloud and deletes it afterwards.
939
+ * In other words, one cannot call execute twice on the same `Query`.
940
+ *
941
+ * @param context context in which the query must be executed. As this is an asynchronous operation,
942
+ * one has to provide a context that won't be destroyed before the query has a result.
943
+ * @return promise resolving to query result or failing otherwise.
944
+ */
945
+ execute(context: Context): Promise<QueryResult<R>>;
946
+
947
+ /**
948
+ * Get an observable to the current value of the QueryResult for this Query instance.
949
+ *
950
+ * The observable gets the new value each time data modification in the datacloud changes the result of the query.
951
+ *
952
+ * The observable gets completed automatically once the specified context is [cleared]{@link Context#onClear}.
953
+ *
954
+ * @param context [context]{@link Context} to which the Observable is attached
955
+ * @return Observable of QueryResult values
956
+ */
957
+ observe(context: Context): Observable<QueryResult<R>>;
252
958
 
253
- executeFromCache(): QueryResult<R>; // Execute the query locally, using the DBView.
959
+ /**
960
+ * Execute synchronously the query on the local datacloud cache.
961
+ * `executeFromCache` can only be called once on a given `Query`.
962
+ *
963
+ * @return query result of the execution on the local cache
964
+ */
965
+ executeFromCache(): QueryResult<R>;
254
966
  }
255
967
 
256
968
  /**
257
- * A QuerySingle is a Query which follows relations that should be unique between CloudObjects (0..1 and 1..1 cardinalities).
258
- * It returns the CloudObjects related to the origin, following the specified relation(s).
969
+ * A QuerySingle is a Query which follows relations that should be unique between data types (0..1 and 1..1 cardinalities).
970
+ * It returns the instance related to the origin, following the specified relation(s).
259
971
  *
260
- * It acts as a syntactic sugar to avoid having a CloudArray and get the first value.
972
+ * It acts as a syntactic sugar to avoid having a query result structure and get the first value out of it.
261
973
  *
262
974
  * A QuerySingle is immutable.
263
975
  */
264
976
  export class QuerySingle<T extends CloudObject> {
977
+ /**
978
+ * Create a `query single` from a single node.
979
+ * See [Query.from]{@link Query#from}
980
+ *
981
+ * @param object starting node for the graph query single
982
+ * @return new query single only accepting 0..1 relations
983
+ */
265
984
  static from<T extends CloudObject>(object: Tag): QuerySingle<T>;
266
985
 
986
+ /**
987
+ * Follow a 0..1 relation
988
+ * See [Query.follow()]{@link Query#follow}
989
+ *
990
+ * @param relation relation to next node
991
+ * @return new query single with follow operation
992
+ */
267
993
  follow<D extends CloudObject>(relation: Relation<T, D>): QuerySingle<D>;
994
+
995
+ /**
996
+ * See [Query.cast()]{@link Query#cast}
997
+ *
998
+ * @param type the new type
999
+ * @return query single on a node of the specified type
1000
+ */
268
1001
  cast<S extends CloudObject>(type: Class<S>): QuerySingle<S>;
269
1002
 
270
- execute(context: Context): Promise<T | null>; // Execute the query on the datacloud (as a listdef) and deletes it afterwards.
1003
+ /**
1004
+ * Execute the query single, see [Query.execute()]{@link Query#execute}
1005
+ *
1006
+ * @param context
1007
+ * @return promise containing the single result or `null`
1008
+ */
1009
+ execute(context: Context): Promise<T | null>;
271
1010
 
272
- executeFromCache(): T | null; // Execute the query locally, using the DBView.
1011
+ /**
1012
+ * Execute the query single on the local data cloud,
1013
+ * see [Query.executeFromCache()]{@link Query#executeFromCache}
1014
+ *
1015
+ * @return single result value or `null`
1016
+ */
1017
+ executeFromCache(): T | null;
273
1018
  }
274
1019
 
275
1020
  /**
276
1021
  * Predicates are used to filter queries.
1022
+ *
1023
+ * Example:
1024
+ * ```javascript
1025
+ * const predicate = Predicate.equals(myNumberProperty, 1);
1026
+ * const filteredQuery = Query.instancesOf(myModel).filter(predicate).andReturn();
1027
+ * ```
277
1028
  */
278
1029
  export class Predicate {
1030
+ /**
1031
+ * Create a predicate matching a specified property to a specified value.
1032
+ *
1033
+ * @param property the property to use for filtering
1034
+ * @param value the value the property must match
1035
+ * @return new Predicate with the equals operation
1036
+ */
279
1037
  static equals<T>(property: Property<T>, value: T): Predicate;
1038
+
1039
+ /**
1040
+ * Create a predicate matching a specified string property to a specified string.
1041
+ *
1042
+ * @param property the string property to use for filtering
1043
+ * @param value the value the string property must match
1044
+ * @param caseSensitive if the match must pay attention to case sensitivity
1045
+ * @return new Predicate with the contains string operation
1046
+ */
280
1047
  static contains(property: Property<string>, value: string, caseSensitive?: boolean): Predicate;
1048
+
1049
+ /**
1050
+ * Create a predicate matching a specified string property to a regular expression.
1051
+ *
1052
+ * @param property the string property to use for filtering
1053
+ * @param value the regex the string property must match
1054
+ * @param caseSensitive if the match must pay attention to case sensitivity
1055
+ * @return new Predicate with the match regex operation
1056
+ */
281
1057
  static regex(property: Property<string>, value: RegExp, caseSensitive?: boolean): Predicate;
282
1058
 
1059
+ /**
1060
+ * Create a predicate matching a specified number property to a numerical lower bound
1061
+ *
1062
+ * @param property the number property to user for filtering
1063
+ * @param value the lower bound
1064
+ * @param strict if the inequality is strict or not
1065
+ * @return new Predicate with the greaterThan mathematical operation
1066
+ */
283
1067
  static greaterThan(property: Property<number>, value: number, strict?: boolean): Predicate;
1068
+
1069
+ /**
1070
+ * Create a predicate matching a specified number property to a numerical upper bound
1071
+ *
1072
+ * @param property the number property to user for filtering
1073
+ * @param value the upper bound
1074
+ * @param strict if the inequality is strict or not
1075
+ * @return new Predicate with the smallerThan mathematical operation
1076
+ */
284
1077
  static smallerThan(property: Property<number>, value: number, strict?: boolean): Predicate;
285
1078
 
1079
+ /**
1080
+ * Create a predicate matching a specified number property to a number property lower bound
1081
+ *
1082
+ * @param property1 the number property to user for filtering
1083
+ * @param property2 the lower bound number property
1084
+ * @param strict if the inequality is strict or not
1085
+ * @return new Predicate with the greaterThan mathematical operation
1086
+ */
286
1087
  static greaterThan(property1: Property<number>, property2: Property<number>, strict?: boolean): Predicate;
1088
+
1089
+ /**
1090
+ * Create a predicate matching a specified number property to a number property upper bound
1091
+ *
1092
+ * @param property1 the number property to user for filtering
1093
+ * @param property2 the upper bound number property
1094
+ * @param strict if the inequality is strict or not
1095
+ * @return new Predicate with the smallerThan mathematical operation
1096
+ */
287
1097
  static smallerThan(property1: Property<number>, property2: Property<number>, strict?: boolean): Predicate;
288
1098
 
1099
+ /**
1100
+ * Create an AND composite predicate matching a specified set of predicates
1101
+ *
1102
+ * @param predicates list of predicates to match
1103
+ * @return new Predicate that should match all specified predicates
1104
+ */
289
1105
  static and(...predicates: Predicate[]): Predicate;
1106
+
1107
+ /**
1108
+ * Create an OR composite predicate matching a specified set of predicates
1109
+ *
1110
+ * @param predicates list of predicates to match
1111
+ * @return new Predicate that should match at least one specified predicate
1112
+ */
290
1113
  static or(...predicates: Predicate[]): Predicate;
1114
+
1115
+ /**
1116
+ * Create a NOT predicate matching the opposite of a specified predicate
1117
+ *
1118
+ * @param predicate the predicate to inverse
1119
+ * @return new Predicate that match when the specified predicate does not
1120
+ */
291
1121
  static not(predicate: Predicate): Predicate;
292
1122
  }
293
1123
 
1124
+ /**
1125
+ * Specific predicate class creator for filtering based on relations between objects
1126
+ *
1127
+ * A path is defined by a list of relations, and an object match the predicate if and only if the patch can be followed
1128
+ * to a destination (which might be specified)
1129
+ */
294
1130
  export class RelatedTo extends Predicate {
295
1131
  constructor(destination?: Tag);
1132
+
1133
+ /**
1134
+ * Adds a relation to the path the RelatedTo predicate must match
1135
+ *
1136
+ * @param relation relation to add to the path
1137
+ * @return new RelatedTo predicate with additional relation to match in the path
1138
+ */
296
1139
  follow(relation: Relation<any, any>): RelatedTo;
1140
+
1141
+ /**
1142
+ * Adds a relation that can be followed recursively in the path for the RelatedTo predicate
1143
+ *
1144
+ * @param relation relation to add to the path that can be followed recursively
1145
+ * @return new RelatedTo predicate with additional relation that can be matched recursively in the path
1146
+ */
297
1147
  followRecursive(relation: Relation<any, any>): RelatedTo;
298
1148
  }
299
1149