@elaraai/e3-types 1.0.97 → 1.0.99

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/src/record.ts CHANGED
@@ -19,8 +19,8 @@
19
19
  */
20
20
 
21
21
  import {
22
- ArrayType, BooleanType, DateTimeType, DictType, EastTypeType, FunctionType, NullType,
23
- OptionType, PatchType, StringType, StructType, VariantType,
22
+ ArrayType, BooleanType, DateTimeType, DictType, EastTypeType, FloatType, FunctionType, IntegerType, NullType,
23
+ OptionType, PatchType, RecursiveType, StringType, StructType, VariantType,
24
24
  decodeBeast2For, dictPatchOpsType, setPatchOpsType, toEastTypeValue,
25
25
  type EastType, type EastTypeValue, type PatchTypeOf, type ValueTypeOf,
26
26
  } from '@elaraai/east';
@@ -163,6 +163,242 @@ export function decodeMutationObject(data: Uint8Array): MutationObject {
163
163
  }
164
164
  }
165
165
 
166
+ /**
167
+ * What a summary index holds for one of its keys (#1039): how many rows touch
168
+ * the key's period, and the milliseconds of the period they cover between
169
+ * them.
170
+ *
171
+ * @remarks
172
+ * Both are Integers, so a total kept write by write is exactly the total a
173
+ * rebuild sums: Integer addition is associative, where a Float sum depends on
174
+ * the order of its additions. A key is held only while its count is positive,
175
+ * so an emptied period leaves the summary as a rebuild would, and the count
176
+ * is what tells e3 a period has emptied.
177
+ *
178
+ * @property count - How many rows touch the period
179
+ * @property ms - The milliseconds of the period they cover, summed over them
180
+ */
181
+ export const RecordSummaryTotalsType = StructType({
182
+ count: IntegerType,
183
+ ms: IntegerType,
184
+ });
185
+ export type RecordSummaryTotalsType = typeof RecordSummaryTotalsType;
186
+ export type RecordSummaryTotals = ValueTypeOf<typeof RecordSummaryTotalsType>;
187
+
188
+ /**
189
+ * The level a series index files an event at (#1433): the finest, from the
190
+ * index's tile up, whose shortest tile holds the event's length, or the year
191
+ * for an event longer than a year. It names a level with the tiles' own
192
+ * words, as east-ui's `RecordSliceTileType` does.
193
+ *
194
+ * @property day - Filed under the UTC day holding its start
195
+ * @property week - Under the ISO week from Monday holding its start
196
+ * @property month - Under the UTC month holding its start
197
+ * @property quarter - Under the UTC quarter holding its start
198
+ * @property year - Under the UTC year holding its start, or every year it touches
199
+ */
200
+ export const RecordLevelType = VariantType({
201
+ day: NullType, week: NullType, month: NullType, quarter: NullType, year: NullType,
202
+ });
203
+ export type RecordLevelType = typeof RecordLevelType;
204
+ export type RecordLevel = ValueTypeOf<typeof RecordLevelType>;
205
+
206
+ /**
207
+ * A rollup, as a nested series index keeps it (#1434): how a node's own values
208
+ * in a tile fold, and how a parent aggregates what its children draw.
209
+ *
210
+ * @property fold - Its own values in a tile, in key order: their sum, mean,
211
+ * least, greatest, the value at the latest instant, or how many
212
+ * @property aggregate - What its children draw in a tile, in key order: their
213
+ * sum, mean, least or greatest; `count`, the leaf rows beneath that draw a
214
+ * value, a child drawing its own counting one
215
+ */
216
+ export const RecordRollupType = StructType({
217
+ fold: VariantType({ sum: NullType, mean: NullType, min: NullType, max: NullType, last: NullType, count: NullType }),
218
+ aggregate: VariantType({ sum: NullType, mean: NullType, min: NullType, max: NullType, count: NullType }),
219
+ });
220
+ export type RecordRollupType = typeof RecordRollupType;
221
+ export type RecordRollup = ValueTypeOf<typeof RecordRollupType>;
222
+
223
+ /**
224
+ * Which run of a node's entries a nested series index files an entry in
225
+ * (#1434). Each run is contiguous in the key, so a read of one never reads
226
+ * another.
227
+ *
228
+ * @property items - The node's own items or values, each at the level it is filed at
229
+ * @property totals - The node's own totals, a summary tile at a time
230
+ * @property subtotals - A parent's subtotals by one of the collection's rollups, at each level from the index's tile up
231
+ * @property subtree - The items of the node's descendants, filed under it too (`items(…, { rollup: true })`)
232
+ * @property subtreeTotals - Its descendants' items' totals, a summary tile at a time
233
+ */
234
+ export const RecordNestedRunType = VariantType({
235
+ items: NullType,
236
+ totals: NullType,
237
+ subtotals: RecordRollupType,
238
+ subtree: NullType,
239
+ subtreeTotals: NullType,
240
+ });
241
+ export type RecordNestedRunType = typeof RecordNestedRunType;
242
+ export type RecordNestedRun = ValueTypeOf<typeof RecordNestedRunType>;
243
+
244
+ /**
245
+ * A path in a nested series index's key (#1434): field names, or Dict keys,
246
+ * from a row down — `end` for none, else one `step` and the rest. A key holds
247
+ * no Array, which East may change in place, so a path is this list: it is
248
+ * immutable, and it sorts as an Array of its names would, a path before every
249
+ * path it leads (`[] < ["a"] < ["a", "b"] < ["b"]`).
250
+ *
251
+ * @property end - No more names
252
+ * @property step - One name, and the path after it
253
+ */
254
+ export const RecordNestedPathType = RecursiveType((self) => VariantType({
255
+ end: NullType,
256
+ step: StructType({ key: StringType, rest: self }),
257
+ }));
258
+ export type RecordNestedPathType = typeof RecordNestedPathType;
259
+ export type RecordNestedPath = ValueTypeOf<typeof RecordNestedPathType>;
260
+
261
+ /**
262
+ * Which item a nested series index's entry is (#1434).
263
+ *
264
+ * @property id - An item, by its id
265
+ * @property at - A value, by its instant
266
+ * @property under - A descendant's item filed under an ancestor: the Dict keys
267
+ * from the ancestor down to the node holding it, and its id
268
+ * @property none - A total or a subtotal, of which a node has one a tile a run
269
+ */
270
+ export const RecordNestedItemType = VariantType({
271
+ id: StringType,
272
+ at: DateTimeType,
273
+ under: StructType({ path: RecordNestedPathType, id: StringType }),
274
+ none: NullType,
275
+ });
276
+ export type RecordNestedItemType = typeof RecordNestedItemType;
277
+ export type RecordNestedItem = ValueTypeOf<typeof RecordNestedItemType>;
278
+
279
+ /**
280
+ * The key a nested series index files an entry under (#1434): its
281
+ * collection, its row, the node it is about, its run, its level and tile, and
282
+ * the item — in that order, so a node's run at one level is one contiguous
283
+ * range across tiles.
284
+ *
285
+ * @param rowKey - The record's key type
286
+ * @returns The entry key's struct type
287
+ * @property collection - The collection's place: the field names from the row down to it, a recursive step adding none
288
+ * @property row - The record row's key
289
+ * @property path - The Dict keys from the row down to the node the entry is about: `end` for the row itself
290
+ * @property run - Which of the node's runs ({@link RecordNestedRunType})
291
+ * @property level - The level the entry is filed at; a total's, the summary tile's
292
+ * @property tile - The start of the level's tile
293
+ * @property item - Which item ({@link RecordNestedItemType})
294
+ */
295
+ export function NestedEntryKeyType<K extends EastType>(rowKey: K) {
296
+ return StructType({
297
+ collection: RecordNestedPathType, row: rowKey, path: RecordNestedPathType,
298
+ run: RecordNestedRunType, level: RecordLevelType, tile: DateTimeType, item: RecordNestedItemType,
299
+ });
300
+ }
301
+
302
+ /**
303
+ * The key a nested series index's span keeps its totals under (#1438): a
304
+ * collection's place, as an entry's key names it, and a month its items or
305
+ * values touch. Each holds how many of them touch it and the milliseconds of it
306
+ * its items cover ({@link RecordSummaryTotalsType}), summed across the record's
307
+ * rows as a summary index's totals are, so a collection's first and last month
308
+ * held are its span across the rows, and its months' counts how much it holds
309
+ * in each.
310
+ *
311
+ * @property collection - The collection's place: the field names from the row down to it, a recursive step adding none
312
+ * @property tile - The start of a month its items or values touch
313
+ */
314
+ export const NestedSpanKeyType = StructType({ collection: RecordNestedPathType, tile: DateTimeType });
315
+ export type NestedSpanKeyType = typeof NestedSpanKeyType;
316
+
317
+ /**
318
+ * The name of the index a nested series index's span is kept in, beside it
319
+ * (#1438): an index of its record's, built with it and never declared.
320
+ *
321
+ * @param index - The nested series index's name
322
+ * @returns The span's
323
+ */
324
+ export function nestedSpanName(index: string): string {
325
+ return `${index}_span`;
326
+ }
327
+
328
+ /**
329
+ * One collection's totals in one summary tile (#1434), for one node, or for
330
+ * its descendants' items under `subtreeTotals`.
331
+ *
332
+ * @property count - How many items touch the tile
333
+ * @property ms - The milliseconds of the tile they cover; an instant covers none
334
+ * @property values - For a collection of Float or Integer values: their sum,
335
+ * the least, the greatest, and the value at the latest instant in the tile,
336
+ * as Floats, taken in key order; `none` for any other collection
337
+ */
338
+ export const RecordNestedTotalsType = StructType({
339
+ count: IntegerType,
340
+ ms: IntegerType,
341
+ values: OptionType(StructType({ sum: FloatType, min: FloatType, max: FloatType, last: FloatType })),
342
+ });
343
+ export type RecordNestedTotalsType = typeof RecordNestedTotalsType;
344
+ export type RecordNestedTotals = ValueTypeOf<typeof RecordNestedTotalsType>;
345
+
346
+ /**
347
+ * What a nested series index's entry carries (#1434): an item or a value as
348
+ * its collection holds it, its variant nesting as the declaration nests
349
+ * (`.item .members .load 0.82`); a node's totals in a tile; or a parent's
350
+ * subtotal.
351
+ *
352
+ * @param places - The items' variant: `VariantType({ jobs: Job, members: VariantType({ load: FloatType }) })`
353
+ * @returns The value's variant type
354
+ */
355
+ export function NestedEntryType<P extends EastType>(places: P) {
356
+ return VariantType({ item: places, totals: RecordNestedTotalsType, subtotal: FloatType });
357
+ }
358
+
359
+ /**
360
+ * A nested series index's declaration, as its definition holds it (#1434):
361
+ * each collection a row nests by its field — things people drag, with the
362
+ * fields their times are read from and whether each is filed under its
363
+ * ancestors too; values, with the rollups a parent's subtotals are kept by;
364
+ * child rows, their own collections declared the same way; or more rows of
365
+ * the row's own type. The field names are data, as an index's `on` is, and
366
+ * the binding reads them (#1435).
367
+ */
368
+ export const RecordNestedDeclType = RecursiveType((self) => DictType(StringType, VariantType({
369
+ items: StructType({
370
+ times: VariantType({ span: StructType({ from: StringType, to: StringType }), at: StringType }),
371
+ rollup: BooleanType,
372
+ }),
373
+ values: StructType({ rollups: ArrayType(RecordRollupType) }),
374
+ rows: self,
375
+ recursive: NullType,
376
+ })));
377
+ export type RecordNestedDeclType = typeof RecordNestedDeclType;
378
+ export type RecordNestedDecl = ValueTypeOf<typeof RecordNestedDeclType>;
379
+
380
+ /**
381
+ * Which collection an index builds (#1039).
382
+ *
383
+ * - `entries` — an entry per index key a row has, `Dict<{ik, k}, P>`: the
384
+ * index every constructor but the summary's declares.
385
+ * - `summary` — one entry per index key, standing for every row that has it,
386
+ * `Dict<{ik, k: Null}, Totals>` ({@link RecordSummaryTotalsType}): the
387
+ * rows' totals per period, which a write changes by adding to them. Its
388
+ * build's pieces hold the same keys, so it names the merge that sums them,
389
+ * `mergeIr`, a `({ik, k}, Totals, Totals) -> Totals` IR bundle.
390
+ *
391
+ * @remarks
392
+ * A variant, so an index object decodes only with one of these: a collection
393
+ * this e3 does not know is refused where the object is read.
394
+ */
395
+ export const RecordIndexFormType = VariantType({
396
+ entries: NullType,
397
+ summary: StructType({ mergeIr: StringType }),
398
+ });
399
+ export type RecordIndexFormType = typeof RecordIndexFormType;
400
+ export type RecordIndexForm = ValueTypeOf<typeof RecordIndexFormType>;
401
+
166
402
  /**
167
403
  * A secondary index over a record: an East function of an entry, and the
168
404
  * collection it builds.
@@ -178,7 +414,8 @@ export function decodeMutationObject(data: Uint8Array): MutationObject {
178
414
  */
179
415
  export const RecordIndexObjectType = StructType({
180
416
  /** Hash of the encoded EastIR bundle: `(K, V) -> IK`, or `-> Set<IK>` when
181
- * {@link multi}. */
417
+ * {@link multi}; a summary's or a nested series index's `(K, V) ->
418
+ * Dict<IK, P>`, a row's entries with their values (#1039, #1434). */
182
419
  keyIr: StringType,
183
420
  /** Declared with `keys` (a Set return) rather than `key`: one entry per
184
421
  * element, so a row that names five resources appears under five keys. */
@@ -196,6 +433,11 @@ export const RecordIndexObjectType = StructType({
196
433
  buildIr: StringType,
197
434
  /** Author-chosen runtime the index's functions run on. */
198
435
  runner: RunnerType,
436
+ /** Which collection it builds: an entry per key a row has, or a summary's
437
+ * totals per key ({@link RecordIndexFormType}). A summary's `keyIr` is
438
+ * `(K, V) -> Dict<IK, Totals>`, a row's contribution to each of its keys,
439
+ * and its `valueType` is {@link RecordSummaryTotalsType}. */
440
+ form: RecordIndexFormType,
199
441
  });
200
442
  export type RecordIndexObjectType = typeof RecordIndexObjectType;
201
443
  export type RecordIndexObject = ValueTypeOf<typeof RecordIndexObjectType>;
@@ -424,7 +666,9 @@ export function isRecordStateType(typeValue: EastTypeValue, value?: unknown): bo
424
666
  * @remarks
425
667
  * `ik` first, so every entry with one index key is a contiguous run — ordered
426
668
  * by primary key inside it — and a RANGE of index keys is one contiguous run
427
- * too. `k` makes the entry unique and gives the run its inner order.
669
+ * too. `k` makes the entry unique and gives the run its inner order. A
670
+ * summary's entry stands for every row of its key, so its `k` is Null and its
671
+ * value its totals: `indexCollectionType(NullType, IK, RecordSummaryTotalsType)`.
428
672
  *
429
673
  * @param keyType - the record's primary key type, `K`
430
674
  * @param indexKeyType - the index key the function computes, `IK`
@@ -451,23 +695,43 @@ export function indexCollectionType(keyType: EastType, indexKeyType: EastType, v
451
695
  * patches are sparse and addressed by key.
452
696
  */
453
697
  export function patchOpsType(collectionType: EastType): EastType {
454
- const collection = collectionType as unknown as { type: string; key: EastType; value: EastType };
455
- if (collection.type === 'Dict') return dictPatchOpsType(collection.value);
456
- if (collection.type === 'Set') return setPatchOpsType(collection.key);
698
+ if (collectionType.type === 'Dict') return dictPatchOpsType(collectionType.value);
699
+ if (collectionType.type === 'Set') return setPatchOpsType(collectionType.key);
457
700
  throw new Error(
458
- `a mutation delta addresses Dict and Set targets by key; this one holds ${collection.type}`,
701
+ `a mutation delta addresses Dict and Set targets by key; this one holds ${collectionType.type}`,
459
702
  );
460
703
  }
461
704
 
705
+ /**
706
+ * The op a summary index's arm of a mutation delta carries for one of its keys
707
+ * (#1039): `add`, the change to the key's totals — what the write's rows took
708
+ * away from it and gave it, summed.
709
+ *
710
+ * @remarks
711
+ * A write's program reads the record, never its indexes, so it cannot say
712
+ * what a summary's total becomes; it says what changed, and the apply adds it
713
+ * to the total the summary holds. Its parts may be negative. A key the summary
714
+ * does not hold is inserted with it, and a total whose count it brings to zero
715
+ * is deleted (see `e3-core`'s `record-apply.ts`).
716
+ *
717
+ * @property add - The change to the key's totals
718
+ */
719
+ export const RecordSummaryOpType = VariantType({ add: RecordSummaryTotalsType });
720
+ export type RecordSummaryOpType = typeof RecordSummaryOpType;
721
+ export type RecordSummaryOp = ValueTypeOf<typeof RecordSummaryOpType>;
722
+
462
723
  /** One target of a mutation delta: its name, its key and its op type. */
463
724
  export interface DeltaTarget {
464
725
  /** `primary`, or an index name. */
465
726
  name: string;
466
727
  /** The key the target is addressed by — `K` for the primary, `{ik, k}` for
467
- * an index. */
728
+ * an index, `{ik, k: Null}` for a summary. */
468
729
  keyType: EastType;
469
730
  /** The target's own collection type, whose patch ops the arm carries. */
470
731
  collectionType: EastType;
732
+ /** Whether the target is a summary index, whose arm carries
733
+ * {@link RecordSummaryOpType}'s `add` rather than its collection's patch ops. */
734
+ summary?: boolean;
471
735
  }
472
736
 
473
737
  /**
@@ -506,12 +770,12 @@ export const DELTA_CONFLICT = '$conflict';
506
770
  * @param targets - the primary and each index, in canonical case order
507
771
  * @returns `Dict<DeltaKey, DeltaOp>`
508
772
  */
509
- export function mutationDeltaType(targets: readonly DeltaTarget[]): EastType {
773
+ export function mutationDeltaType(targets: readonly DeltaTarget[]): DictType<VariantType<Record<string, EastType>>, VariantType<Record<string, EastType>>> {
510
774
  const keys: Record<string, EastType> = { [DELTA_CONFLICT]: StringType };
511
775
  const ops: Record<string, EastType> = { [DELTA_CONFLICT]: NullType };
512
776
  for (const target of targets) {
513
777
  keys[target.name] = target.keyType;
514
- ops[target.name] = patchOpsType(target.collectionType);
778
+ ops[target.name] = target.summary === true ? RecordSummaryOpType : patchOpsType(target.collectionType);
515
779
  }
516
780
  return DictType(VariantType(keys), VariantType(ops));
517
781
  }
@@ -546,14 +810,15 @@ export function editTypeOf<K extends EastType, V extends EastType>(recordType: D
546
810
  }>;
547
811
  export function editTypeOf(recordType: EastType): EastType;
548
812
  export function editTypeOf(recordType: EastType): EastType {
549
- const dict = recordType as unknown as { type: string; key: EastType; value: EastType };
550
- if (dict.type !== 'Dict') {
551
- throw new Error(`an edit mutation writes a Dict record; this one holds ${dict.type}`);
813
+ if (recordType.type !== 'Dict') {
814
+ throw new Error(`an edit mutation writes a Dict record; this one holds ${recordType.type}`);
552
815
  }
816
+ const keyType: EastType = recordType.key;
817
+ const valueType: EastType = recordType.value;
553
818
  return StructType({
554
- set: FunctionType([dict.key, dict.value], NullType),
555
- delete: FunctionType([dict.key], NullType),
556
- update: FunctionType([dict.key, PatchType(dict.value)], NullType),
819
+ set: FunctionType([keyType, valueType], NullType),
820
+ delete: FunctionType([keyType], NullType),
821
+ update: FunctionType([keyType, PatchType(valueType)], NullType),
557
822
  });
558
823
  }
559
824
 
@@ -585,3 +850,31 @@ export function indexWindowType(keyType: EastType, indexKeyType: EastType, value
585
850
  row: OptionType(rowType),
586
851
  }));
587
852
  }
853
+
854
+ /**
855
+ * What a read of a Dict's rows by key answers (#1351): one entry per key
856
+ * asked, in the order asked — an ORDERED array, as an index window is.
857
+ *
858
+ * @remarks
859
+ * Each entry says whether the Dict holds the key — its row, or none — and
860
+ * where the key sits in the Dict's canonical order: the row's position, the
861
+ * same row space a page's element windows address, or, for a key it does not
862
+ * hold, the position the key would take. So a client holds each row at its
863
+ * position, and answers a later search by the key, and a one-row page there,
864
+ * from what it holds.
865
+ *
866
+ * @param keyType - The Dict's key type, `K`
867
+ * @param rowType - Its value type, `V`
868
+ * @returns The answer's East type
869
+ */
870
+ export function datasetRowsType(keyType: EastType, rowType: EastType): EastType {
871
+ return ArrayType(StructType({
872
+ /** The key asked: the stored key when the Dict holds it. */
873
+ key: keyType,
874
+ /** The row the Dict holds under it, or none. */
875
+ row: OptionType(rowType),
876
+ /** Its position in the Dict's canonical order: the row's, or the one the
877
+ * key would take. */
878
+ position: IntegerType,
879
+ }));
880
+ }
package/src/release.ts CHANGED
@@ -17,7 +17,7 @@
17
17
  */
18
18
 
19
19
  /** The release of e3 this build is. */
20
- export const E3_RELEASE = '1.0.97';
20
+ export const E3_RELEASE = '1.0.99';
21
21
 
22
22
  /** A semantic version: its core, a pre-release, and build metadata. */
23
23
  const RELEASE = /^(0|[1-9]\d*)\.(0|[1-9]\d*)\.(0|[1-9]\d*)(?:-([0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*))?(?:\+[0-9A-Za-z-]+(?:\.[0-9A-Za-z-]+)*)?$/;
package/src/structure.ts CHANGED
@@ -129,13 +129,7 @@ export type TreePath = ValueTypeOf<typeof TreePathType>;
129
129
  * ```
130
130
  */
131
131
  export function pathToString(path: TreePath): string {
132
- return path.map(segment => {
133
- if (segment.type === 'field') {
134
- return '.' + printIdentifier(segment.value);
135
- } else {
136
- throw new Error(`pathToString: unsupported path segment type: ${segment.type}`);
137
- }
138
- }).join('');
132
+ return path.map(segment => '.' + printIdentifier(segment.value)).join('');
139
133
  }
140
134
 
141
135
  /**