@tinyshed/tinystore 0.5.0 → 0.6.0
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/dist/config.d.ts +46 -1
- package/dist/config.js +94 -7
- package/dist/index.d.ts +3 -3
- package/dist/index.js +2 -2
- package/dist/kv.js +98 -90
- package/dist/logger.d.ts +10 -0
- package/dist/logger.js +16 -6
- package/dist/metrics.d.ts +17 -0
- package/dist/metrics.js +22 -1
- package/dist/records.d.ts +90 -5
- package/dist/records.js +114 -6
- package/dist/runtime/addon.js +51 -41
- package/dist/runtime/pipe.js +116 -87
- package/dist/sql.d.ts +19 -3
- package/dist/sql.js +31 -4
- package/dist/wire/protocol.d.ts +105 -9
- package/dist/wire/protocol.js +296 -8
- package/package.json +8 -8
- package/src/config.ts +147 -17
- package/src/index.ts +12 -1
- package/src/kv.ts +108 -98
- package/src/logger.ts +17 -7
- package/src/metrics.ts +25 -0
- package/src/records.ts +178 -7
- package/src/runtime/addon.ts +52 -41
- package/src/runtime/pipe.ts +136 -104
- package/src/sql.ts +48 -7
- package/src/wire/protocol.ts +321 -8
package/src/index.ts
CHANGED
|
@@ -26,16 +26,21 @@ export type { Clock } from './clock.ts'
|
|
|
26
26
|
export {
|
|
27
27
|
type Bounds,
|
|
28
28
|
type Changes,
|
|
29
|
+
type Chosen,
|
|
29
30
|
type Config,
|
|
30
31
|
type ConfigOptions,
|
|
32
|
+
chosenBy,
|
|
31
33
|
type DurationBounds,
|
|
32
34
|
duration,
|
|
35
|
+
type Entries,
|
|
36
|
+
type EntriesOptions,
|
|
33
37
|
type EnvLayer,
|
|
34
38
|
type EnvOptions,
|
|
35
39
|
type Explained,
|
|
36
40
|
type ExplainGiven,
|
|
37
41
|
type ExplainLine,
|
|
38
42
|
type ExplainOrigin,
|
|
43
|
+
entries,
|
|
39
44
|
env,
|
|
40
45
|
type FileLayer,
|
|
41
46
|
file,
|
|
@@ -101,7 +106,7 @@ export type {
|
|
|
101
106
|
SemaphoreOptions,
|
|
102
107
|
Tried,
|
|
103
108
|
} from './locks.ts'
|
|
104
|
-
export { type ConsoleOut, type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
109
|
+
export { type ConsoleOut, colors, type Logger, type LoggerOptions, logger } from './logger.ts'
|
|
105
110
|
export {
|
|
106
111
|
type Aggregate,
|
|
107
112
|
type AggregatePage,
|
|
@@ -148,6 +153,7 @@ export type { ColumnType, Query, RowPage, Table, TableOptions, UpsertOptions } f
|
|
|
148
153
|
export type {
|
|
149
154
|
Aggregate as EventsAggregate,
|
|
150
155
|
AllEvents,
|
|
156
|
+
Appending,
|
|
151
157
|
AppendOptions,
|
|
152
158
|
Counted,
|
|
153
159
|
Damage,
|
|
@@ -155,10 +161,12 @@ export type {
|
|
|
155
161
|
EventLevel,
|
|
156
162
|
Events,
|
|
157
163
|
EventsOptions,
|
|
164
|
+
FieldInfo,
|
|
158
165
|
Fields,
|
|
159
166
|
Level,
|
|
160
167
|
Levels,
|
|
161
168
|
Lines,
|
|
169
|
+
NameCount,
|
|
162
170
|
NewEvent,
|
|
163
171
|
Page as EventsPage,
|
|
164
172
|
PollBatch,
|
|
@@ -166,10 +174,13 @@ export type {
|
|
|
166
174
|
Row as EventsRow,
|
|
167
175
|
Stats,
|
|
168
176
|
StoreLoggerOptions,
|
|
177
|
+
StreamFields,
|
|
169
178
|
StreamInfo,
|
|
170
179
|
TailBatch,
|
|
171
180
|
TailQuery,
|
|
172
181
|
Top,
|
|
182
|
+
TxEvents,
|
|
183
|
+
ValueKind,
|
|
173
184
|
} from './records.ts'
|
|
174
185
|
export type { StandardSchemaV1 } from './schema.ts'
|
|
175
186
|
export {
|
package/src/kv.ts
CHANGED
|
@@ -631,114 +631,124 @@ export const jsonValues: Values<unknown> = {
|
|
|
631
631
|
},
|
|
632
632
|
}
|
|
633
633
|
|
|
634
|
+
/** A row of nothing reads as the empty string. */
|
|
635
|
+
const stringValues: Values<unknown> = {
|
|
636
|
+
encode: v => {
|
|
637
|
+
if (typeof v !== 'string') {
|
|
638
|
+
throw new InvalidError(`a ${typeof v} in a bucket of strings`)
|
|
639
|
+
}
|
|
640
|
+
return tagged(kinds.text, utf8.encode(v))
|
|
641
|
+
},
|
|
642
|
+
decode: raw => {
|
|
643
|
+
if (raw === null) {
|
|
644
|
+
return ''
|
|
645
|
+
}
|
|
646
|
+
const text = textOf(bodyOf(raw, kinds.text, 'text'))
|
|
647
|
+
if (text === undefined) {
|
|
648
|
+
throw new CorruptError('a stored value is text that is not UTF-8')
|
|
649
|
+
}
|
|
650
|
+
return text
|
|
651
|
+
},
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
/** A row of nothing reads as no bytes. */
|
|
655
|
+
const bytesValues: Values<unknown> = {
|
|
656
|
+
keeps: true,
|
|
657
|
+
encode: v => {
|
|
658
|
+
if (!(v instanceof Uint8Array)) {
|
|
659
|
+
throw new InvalidError(`a ${typeof v} in a bucket of bytes`)
|
|
660
|
+
}
|
|
661
|
+
return tagged(kinds.bytes, v)
|
|
662
|
+
},
|
|
663
|
+
decode: raw => (raw === null ? new Uint8Array(0) : bodyOf(raw, kinds.bytes, 'bytes')),
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const intValues: Values<unknown> = {
|
|
667
|
+
encode: v => {
|
|
668
|
+
if (typeof v !== 'number' || !Number.isSafeInteger(v)) {
|
|
669
|
+
throw new InvalidError(
|
|
670
|
+
`${String(v)} is not an integer a number holds exactly; open the bucket with type 'bigint'`,
|
|
671
|
+
)
|
|
672
|
+
}
|
|
673
|
+
return BigInt(v)
|
|
674
|
+
},
|
|
675
|
+
decode: raw => {
|
|
676
|
+
if (typeof raw !== 'bigint') {
|
|
677
|
+
throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`)
|
|
678
|
+
}
|
|
679
|
+
if (raw > BigInt(Number.MAX_SAFE_INTEGER) || raw < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
680
|
+
throw new CorruptError(
|
|
681
|
+
`${raw} does not fit a number exactly; open the bucket with type 'bigint'`,
|
|
682
|
+
)
|
|
683
|
+
}
|
|
684
|
+
return Number(raw)
|
|
685
|
+
},
|
|
686
|
+
}
|
|
687
|
+
|
|
688
|
+
const bigintValues: Values<unknown> = {
|
|
689
|
+
encode: v => {
|
|
690
|
+
if (typeof v !== 'bigint' && !(typeof v === 'number' && Number.isSafeInteger(v))) {
|
|
691
|
+
throw new InvalidError(`${String(v)} is not an integer`)
|
|
692
|
+
}
|
|
693
|
+
return BigInt(v)
|
|
694
|
+
},
|
|
695
|
+
decode: raw => {
|
|
696
|
+
if (typeof raw !== 'bigint') {
|
|
697
|
+
throw new CorruptError(`a stored value is ${describe(raw)} where an integer was written`)
|
|
698
|
+
}
|
|
699
|
+
return raw
|
|
700
|
+
},
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
const floatValues: Values<unknown> = {
|
|
704
|
+
encode: v => {
|
|
705
|
+
if (typeof v !== 'number') {
|
|
706
|
+
throw new InvalidError(`a ${typeof v} in a bucket of floats`)
|
|
707
|
+
}
|
|
708
|
+
const bytes = new Uint8Array(8)
|
|
709
|
+
new DataView(bytes.buffer).setFloat64(0, v)
|
|
710
|
+
return tagged(kinds.float, bytes)
|
|
711
|
+
},
|
|
712
|
+
decode: raw => {
|
|
713
|
+
const bits = bodyOf(raw, kinds.float, 'a float')
|
|
714
|
+
if (bits.length !== 8) {
|
|
715
|
+
throw new CorruptError(`a float from ${bits.length} bytes rather than 8`)
|
|
716
|
+
}
|
|
717
|
+
return new DataView(bits.buffer, bits.byteOffset, 8).getFloat64(0)
|
|
718
|
+
},
|
|
719
|
+
}
|
|
720
|
+
|
|
721
|
+
const boolValues: Values<unknown> = {
|
|
722
|
+
encode: v => {
|
|
723
|
+
if (typeof v !== 'boolean') {
|
|
724
|
+
throw new InvalidError(`a ${typeof v} in a bucket of booleans`)
|
|
725
|
+
}
|
|
726
|
+
return v ? 1n : 0n
|
|
727
|
+
},
|
|
728
|
+
decode: raw => {
|
|
729
|
+
if (typeof raw !== 'bigint') {
|
|
730
|
+
throw new CorruptError(`a stored value is ${describe(raw)} where a boolean was written`)
|
|
731
|
+
}
|
|
732
|
+
return raw !== 0n
|
|
733
|
+
},
|
|
734
|
+
}
|
|
735
|
+
|
|
634
736
|
export function valuesOf(type: ValueType | undefined): Values<unknown> {
|
|
635
737
|
switch (type) {
|
|
636
738
|
case undefined:
|
|
637
739
|
return jsonValues
|
|
638
740
|
case 'string':
|
|
639
|
-
return
|
|
640
|
-
encode: v => {
|
|
641
|
-
if (typeof v !== 'string') {
|
|
642
|
-
throw new InvalidError(`a ${typeof v} in a bucket of strings`)
|
|
643
|
-
}
|
|
644
|
-
return tagged(kinds.text, utf8.encode(v))
|
|
645
|
-
},
|
|
646
|
-
decode: raw => {
|
|
647
|
-
if (raw === null) {
|
|
648
|
-
return ''
|
|
649
|
-
}
|
|
650
|
-
const text = textOf(bodyOf(raw, kinds.text, 'text'))
|
|
651
|
-
if (text === undefined) {
|
|
652
|
-
throw new CorruptError('a stored value is text that is not UTF-8')
|
|
653
|
-
}
|
|
654
|
-
return text
|
|
655
|
-
},
|
|
656
|
-
}
|
|
741
|
+
return stringValues
|
|
657
742
|
case 'bytes':
|
|
658
|
-
return
|
|
659
|
-
keeps: true,
|
|
660
|
-
encode: v => {
|
|
661
|
-
if (!(v instanceof Uint8Array)) {
|
|
662
|
-
throw new InvalidError(`a ${typeof v} in a bucket of bytes`)
|
|
663
|
-
}
|
|
664
|
-
return tagged(kinds.bytes, v)
|
|
665
|
-
},
|
|
666
|
-
decode: raw => (raw === null ? new Uint8Array(0) : bodyOf(raw, kinds.bytes, 'bytes')),
|
|
667
|
-
}
|
|
743
|
+
return bytesValues
|
|
668
744
|
case 'int':
|
|
669
|
-
return
|
|
670
|
-
encode: v => {
|
|
671
|
-
if (typeof v !== 'number' || !Number.isSafeInteger(v)) {
|
|
672
|
-
throw new InvalidError(
|
|
673
|
-
`${String(v)} is not an integer a number holds exactly; open the bucket with type 'bigint'`,
|
|
674
|
-
)
|
|
675
|
-
}
|
|
676
|
-
return BigInt(v)
|
|
677
|
-
},
|
|
678
|
-
decode: raw => {
|
|
679
|
-
if (typeof raw !== 'bigint') {
|
|
680
|
-
throw new CorruptError(
|
|
681
|
-
`a stored value is ${describe(raw)} where an integer was written`,
|
|
682
|
-
)
|
|
683
|
-
}
|
|
684
|
-
if (raw > BigInt(Number.MAX_SAFE_INTEGER) || raw < BigInt(Number.MIN_SAFE_INTEGER)) {
|
|
685
|
-
throw new CorruptError(
|
|
686
|
-
`${raw} does not fit a number exactly; open the bucket with type 'bigint'`,
|
|
687
|
-
)
|
|
688
|
-
}
|
|
689
|
-
return Number(raw)
|
|
690
|
-
},
|
|
691
|
-
}
|
|
745
|
+
return intValues
|
|
692
746
|
case 'bigint':
|
|
693
|
-
return
|
|
694
|
-
encode: v => {
|
|
695
|
-
if (typeof v !== 'bigint' && !(typeof v === 'number' && Number.isSafeInteger(v))) {
|
|
696
|
-
throw new InvalidError(`${String(v)} is not an integer`)
|
|
697
|
-
}
|
|
698
|
-
return BigInt(v)
|
|
699
|
-
},
|
|
700
|
-
decode: raw => {
|
|
701
|
-
if (typeof raw !== 'bigint') {
|
|
702
|
-
throw new CorruptError(
|
|
703
|
-
`a stored value is ${describe(raw)} where an integer was written`,
|
|
704
|
-
)
|
|
705
|
-
}
|
|
706
|
-
return raw
|
|
707
|
-
},
|
|
708
|
-
}
|
|
747
|
+
return bigintValues
|
|
709
748
|
case 'float':
|
|
710
|
-
return
|
|
711
|
-
encode: v => {
|
|
712
|
-
if (typeof v !== 'number') {
|
|
713
|
-
throw new InvalidError(`a ${typeof v} in a bucket of floats`)
|
|
714
|
-
}
|
|
715
|
-
const bytes = new Uint8Array(8)
|
|
716
|
-
new DataView(bytes.buffer).setFloat64(0, v)
|
|
717
|
-
return tagged(kinds.float, bytes)
|
|
718
|
-
},
|
|
719
|
-
decode: raw => {
|
|
720
|
-
const bits = bodyOf(raw, kinds.float, 'a float')
|
|
721
|
-
if (bits.length !== 8) {
|
|
722
|
-
throw new CorruptError(`a float from ${bits.length} bytes rather than 8`)
|
|
723
|
-
}
|
|
724
|
-
return new DataView(bits.buffer, bits.byteOffset, 8).getFloat64(0)
|
|
725
|
-
},
|
|
726
|
-
}
|
|
749
|
+
return floatValues
|
|
727
750
|
case 'bool':
|
|
728
|
-
return
|
|
729
|
-
encode: v => {
|
|
730
|
-
if (typeof v !== 'boolean') {
|
|
731
|
-
throw new InvalidError(`a ${typeof v} in a bucket of booleans`)
|
|
732
|
-
}
|
|
733
|
-
return v ? 1n : 0n
|
|
734
|
-
},
|
|
735
|
-
decode: raw => {
|
|
736
|
-
if (typeof raw !== 'bigint') {
|
|
737
|
-
throw new CorruptError(`a stored value is ${describe(raw)} where a boolean was written`)
|
|
738
|
-
}
|
|
739
|
-
return raw !== 0n
|
|
740
|
-
},
|
|
741
|
-
}
|
|
751
|
+
return boolValues
|
|
742
752
|
}
|
|
743
753
|
throw new InvalidError(
|
|
744
754
|
`a bucket's type is string, bytes, int, bigint, float or bool, not ${String(type)}`,
|
package/src/logger.ts
CHANGED
|
@@ -368,6 +368,22 @@ function forceColor(): boolean {
|
|
|
368
368
|
return force !== undefined && force !== '' && force !== '0'
|
|
369
369
|
}
|
|
370
370
|
|
|
371
|
+
/**
|
|
372
|
+
* Whether a stream shows colours, by the rule every logger keeps, in Bun,
|
|
373
|
+
* Python, Go and Rust, and the command line too, so that a program's own
|
|
374
|
+
* words and the log lines printed beside them are coloured alike: off under
|
|
375
|
+
* NO_COLOR or a TERM of dumb, on where FORCE_COLOR is other than 0, a pipe or
|
|
376
|
+
* a file too, and otherwise on a terminal alone.
|
|
377
|
+
*
|
|
378
|
+
* `const painted = colors(process.stdout)`
|
|
379
|
+
*/
|
|
380
|
+
export function colors(stream: Pick<ConsoleOut, 'isTTY'>): boolean {
|
|
381
|
+
if (process.env.NO_COLOR || process.env.TERM === 'dumb') {
|
|
382
|
+
return false
|
|
383
|
+
}
|
|
384
|
+
return forceColor() || stream.isTTY === true
|
|
385
|
+
}
|
|
386
|
+
|
|
371
387
|
/** Whether a console told no format prints pretty lines: on a terminal, or where FORCE_COLOR asks. */
|
|
372
388
|
function prettyUntold(options: Pick<LoggerOptions, 'to'>, out: ConsoleOut | undefined): boolean {
|
|
373
389
|
const stream: ConsoleOut = out ?? options.to ?? process.stderr
|
|
@@ -384,19 +400,13 @@ function printer(
|
|
|
384
400
|
return undefined
|
|
385
401
|
}
|
|
386
402
|
const stream: ConsoleOut = out ?? options.to ?? process.stderr
|
|
387
|
-
const forced = out === undefined && forceColor()
|
|
388
|
-
const terminal = stream.isTTY === true
|
|
389
403
|
const pretty =
|
|
390
404
|
options.console === 'pretty' || (options.console === undefined && prettyUntold(options, out))
|
|
391
405
|
if (!pretty) {
|
|
392
406
|
return line => void stream.write(jsonLine(line))
|
|
393
407
|
}
|
|
394
|
-
const color =
|
|
395
|
-
(forced || (terminal && out === undefined)) &&
|
|
396
|
-
!process.env.NO_COLOR &&
|
|
397
|
-
process.env.TERM !== 'dumb'
|
|
398
408
|
const look: Look = {
|
|
399
|
-
color,
|
|
409
|
+
color: out === undefined && colors(stream),
|
|
400
410
|
utc: out?.utc === true,
|
|
401
411
|
time: options.time,
|
|
402
412
|
hideStream: options.hideStream,
|
package/src/metrics.ts
CHANGED
|
@@ -39,6 +39,7 @@ import {
|
|
|
39
39
|
MetricsPlan,
|
|
40
40
|
MetricsQuery,
|
|
41
41
|
MetricsRead,
|
|
42
|
+
MetricsReplace,
|
|
42
43
|
MetricsSeriesNamed,
|
|
43
44
|
MetricsTextPage,
|
|
44
45
|
MetricsUpload,
|
|
@@ -339,6 +340,30 @@ export class Metrics {
|
|
|
339
340
|
)
|
|
340
341
|
}
|
|
341
342
|
|
|
343
|
+
/**
|
|
344
|
+
* Replaces a gauge's samples from `from` to `to`, `to` excluded, with the series' own,
|
|
345
|
+
* whether the store had packed them or not: a time given takes its value, a time not there
|
|
346
|
+
* before is added, and a time the series does not give goes. One write: a read sees the
|
|
347
|
+
* range as it was or as it is now, never half, and the same samples sent again write
|
|
348
|
+
* nothing. A counter's or a histogram's is an InvalidError.
|
|
349
|
+
*
|
|
350
|
+
* // the last three days read again from the shop's database, a refund taken off yesterday
|
|
351
|
+
* await store.metrics.replace(
|
|
352
|
+
* { name: 'revenue', kind: 'gauge', labels: { shop: 'msk' }, samples: [[day(3), 1000], [day(2), 1000], [day(1), 900]] },
|
|
353
|
+
* { from: day(3), to: day(0) },
|
|
354
|
+
* )
|
|
355
|
+
*/
|
|
356
|
+
async replace(series: SeriesInput, range: { from: Time; to: Time }): Promise<void> {
|
|
357
|
+
const body = MetricsReplace.encode({
|
|
358
|
+
series: newSeries(series),
|
|
359
|
+
from: unixMs(range.from),
|
|
360
|
+
to: unixMs(range.to),
|
|
361
|
+
})
|
|
362
|
+
await this.#link.run('write', connection =>
|
|
363
|
+
connection.session.call(methods['metrics.replace'], body),
|
|
364
|
+
)
|
|
365
|
+
}
|
|
366
|
+
|
|
342
367
|
/**
|
|
343
368
|
* Reads the text an exporter serves, in Prometheus' format or OpenMetrics', and writes its
|
|
344
369
|
* series, every one or none: a counter as a counter, a histogram's _bucket series as
|
package/src/records.ts
CHANGED
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
import { type Connection, type Link, pieceOf } from './connection.ts'
|
|
9
9
|
import { type ConsoleLine, jsonOf } from './console.ts'
|
|
10
10
|
import { ClosedError, InvalidError } from './errors.ts'
|
|
11
|
-
import { checkName, handleOn, type Via, viaLink } from './handles.ts'
|
|
11
|
+
import { checkName, type Home, handleOn, type Open, type Via, viaLink } from './handles.ts'
|
|
12
12
|
import { parseJson } from './json.ts'
|
|
13
13
|
import {
|
|
14
14
|
encodeFields,
|
|
@@ -34,6 +34,7 @@ import {
|
|
|
34
34
|
RecordsDropped,
|
|
35
35
|
type RecordsEvent,
|
|
36
36
|
RecordsFieldNamesAsked,
|
|
37
|
+
RecordsFieldsAsked,
|
|
37
38
|
RecordsNames,
|
|
38
39
|
type RecordsNewEvent,
|
|
39
40
|
RecordsOpen,
|
|
@@ -42,6 +43,7 @@ import {
|
|
|
42
43
|
RecordsPollAsked,
|
|
43
44
|
RecordsQuery,
|
|
44
45
|
RecordsRows,
|
|
46
|
+
RecordsStreamFields,
|
|
45
47
|
RecordsStreams,
|
|
46
48
|
RecordsTailBatch,
|
|
47
49
|
RecordsTopAsked,
|
|
@@ -185,6 +187,31 @@ export interface StreamInfo {
|
|
|
185
187
|
newest: Date
|
|
186
188
|
}
|
|
187
189
|
|
|
190
|
+
/** What a stream's events carry, each by name. */
|
|
191
|
+
export interface StreamFields {
|
|
192
|
+
eventNames: NameCount[]
|
|
193
|
+
fields: FieldInfo[]
|
|
194
|
+
context: FieldInfo[]
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A name a stream's events have, and how many have it. */
|
|
198
|
+
export interface NameCount {
|
|
199
|
+
name: string
|
|
200
|
+
count: number
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/** A key of a stream's events' fields or contexts. */
|
|
204
|
+
export interface FieldInfo {
|
|
205
|
+
name: string
|
|
206
|
+
/** the kinds of value it holds, in the order `ValueKind` lists them */
|
|
207
|
+
kinds: ValueKind[]
|
|
208
|
+
/** the events that carry it: one that writes it twice, once */
|
|
209
|
+
count: number
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/** What a value is, as its JSON is spelled: `float` has a fraction or an exponent, `integer` is whole. */
|
|
213
|
+
export type ValueKind = 'float' | 'integer' | 'text' | 'bool' | 'object' | 'list' | 'null'
|
|
214
|
+
|
|
188
215
|
export interface PollBatch {
|
|
189
216
|
events: Event[]
|
|
190
217
|
/** where the next poll begins, kept once these events are safe */
|
|
@@ -253,7 +280,10 @@ abstract class Reads {
|
|
|
253
280
|
/** Keeps a stream that follows the store for its close to cancel; it returns what lets it go. */
|
|
254
281
|
protected abstract follow(stream: Stream): () => void
|
|
255
282
|
|
|
256
|
-
/**
|
|
283
|
+
/**
|
|
284
|
+
* One page: `{ events, next }`, its `limit` of events that match, fewer
|
|
285
|
+
* only on the last, which has no `next`.
|
|
286
|
+
*/
|
|
257
287
|
async list(query?: Query): Promise<Page> {
|
|
258
288
|
const asked = queryOf(query)
|
|
259
289
|
const answer = await this.ask('records.list', handle =>
|
|
@@ -375,25 +405,57 @@ abstract class Reads {
|
|
|
375
405
|
|
|
376
406
|
/** Opens a stream of events: lazily, at its first call on a connection. */
|
|
377
407
|
export function openEvents(link: Link, stream: string, options?: EventsOptions): Events {
|
|
408
|
+
return new Events(link, stream, eventsOpen(stream, options))
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
/** A stream's open, through the database of `database`, a handle on the connection, when given. */
|
|
412
|
+
export function eventsOpen(stream: string, options?: EventsOptions, database?: number): Uint8Array {
|
|
378
413
|
checkName(stream, 'stream')
|
|
379
414
|
const keep = options?.keep === undefined ? undefined : ms(options.keep)
|
|
380
|
-
return
|
|
415
|
+
return RecordsOpen.encode({ stream, keep, database })
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
/** What a stream's appends in a database's transaction and batch need of it. */
|
|
419
|
+
interface EventsParts {
|
|
420
|
+
stream: string
|
|
421
|
+
open: Open
|
|
422
|
+
home: Home | undefined
|
|
381
423
|
}
|
|
382
424
|
|
|
425
|
+
let eventsPartsOf: (events: Events) => EventsParts
|
|
426
|
+
|
|
383
427
|
/** A stream of events: written with `append`, which waits for the file, and read back. */
|
|
384
428
|
export class Events extends Reads {
|
|
385
429
|
readonly stream: string
|
|
386
430
|
readonly #link: Link
|
|
387
431
|
readonly #via: Via
|
|
388
|
-
readonly #open:
|
|
432
|
+
readonly #open: Open
|
|
433
|
+
readonly #home: Home | undefined
|
|
389
434
|
|
|
390
|
-
/** Events come from `store.events(stream)
|
|
391
|
-
constructor(link: Link, stream: string, open:
|
|
435
|
+
/** Events come from `store.events(stream)`, or `db.events(stream)` through a database. */
|
|
436
|
+
constructor(link: Link, stream: string, open: Open, home?: Home) {
|
|
392
437
|
super()
|
|
393
438
|
this.stream = stream
|
|
394
439
|
this.#link = link
|
|
395
|
-
this.#via = viaLink(link)
|
|
440
|
+
this.#via = home === undefined ? viaLink(link) : home.via(`events ${stream}`)
|
|
396
441
|
this.#open = open
|
|
442
|
+
this.#home = home
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
static {
|
|
446
|
+
eventsPartsOf = events => ({ stream: events.stream, open: events.#open, home: events.#home })
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* An event's append for a batch of the database the stream was opened
|
|
451
|
+
* from, `db.events`: it is kept with the batch's rows and reaches the
|
|
452
|
+
* stream once they commit, before the batch returns, or never.
|
|
453
|
+
*
|
|
454
|
+
* await db.batch([sql`update roles set role = ${role} where user_id = ${id}`, audit.appending('role.changed', { userId: id, role })])
|
|
455
|
+
*/
|
|
456
|
+
appending(name: string, fields?: Fields, options?: AppendOptions): Appending {
|
|
457
|
+
const event = newEventOf(oneEvent(name, fields, options), 0)
|
|
458
|
+
return new Appending({ ...eventsPartsOf(this), event })
|
|
397
459
|
}
|
|
398
460
|
|
|
399
461
|
/**
|
|
@@ -488,6 +550,92 @@ export class Events extends Reads {
|
|
|
488
550
|
}
|
|
489
551
|
}
|
|
490
552
|
|
|
553
|
+
/** The parts of a stream, when `handle` is one. */
|
|
554
|
+
export function eventsParts(handle: unknown): EventsParts | undefined {
|
|
555
|
+
return handle instanceof Events ? eventsPartsOf(handle) : undefined
|
|
556
|
+
}
|
|
557
|
+
|
|
558
|
+
/**
|
|
559
|
+
* A stream's appends inside a transaction of the database it was opened
|
|
560
|
+
* from, `tx.with(audit)`: each is kept with the transaction's rows and
|
|
561
|
+
* reaches the stream once it commits, before `db.tx` returns. A read goes
|
|
562
|
+
* through the stream itself, and finds what committed.
|
|
563
|
+
*/
|
|
564
|
+
export class TxEvents {
|
|
565
|
+
readonly stream: string
|
|
566
|
+
readonly #open: Open
|
|
567
|
+
readonly #via: Via
|
|
568
|
+
|
|
569
|
+
/** TxEvents come from `tx.with(events)`. */
|
|
570
|
+
constructor(parts: EventsParts, via: Via) {
|
|
571
|
+
this.stream = parts.stream
|
|
572
|
+
this.#open = parts.open
|
|
573
|
+
this.#via = via
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
/**
|
|
577
|
+
* Appends an event once the transaction commits, and says whether it
|
|
578
|
+
* will be written: false when its stream, or this transaction, holds its id.
|
|
579
|
+
*/
|
|
580
|
+
async append(name: string, fields?: Fields, options?: AppendOptions): Promise<boolean> {
|
|
581
|
+
const event = newEventOf(oneEvent(name, fields, options), 0)
|
|
582
|
+
const answer = await this.#via.call(
|
|
583
|
+
methods['records.open'],
|
|
584
|
+
this.#open,
|
|
585
|
+
methods['records.append'],
|
|
586
|
+
handle => RecordsAppend.encode({ handle, events: [event] }),
|
|
587
|
+
'write',
|
|
588
|
+
)
|
|
589
|
+
return (RecordsAppended.decode(answer).held ?? 0) === 0
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
interface AppendingParts extends EventsParts {
|
|
594
|
+
event: Parameters<typeof RecordsNewEvent.encode>[0]
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
let appendingPartsOf: (appending: Appending) => AppendingParts
|
|
598
|
+
|
|
599
|
+
/** An event's append for a database's batch, from `events.appending`. */
|
|
600
|
+
export class Appending {
|
|
601
|
+
readonly #parts: AppendingParts
|
|
602
|
+
|
|
603
|
+
/** Comes from `events.appending`. */
|
|
604
|
+
constructor(parts: AppendingParts) {
|
|
605
|
+
this.#parts = parts
|
|
606
|
+
}
|
|
607
|
+
|
|
608
|
+
static {
|
|
609
|
+
appendingPartsOf = appending => appending.#parts
|
|
610
|
+
}
|
|
611
|
+
}
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* What a batch of `home` sends of an append, on the connection it goes on:
|
|
615
|
+
* the append's call, its stream's handle opened there first. A stream opened
|
|
616
|
+
* from the store or another database is refused, since its event would not
|
|
617
|
+
* commit with the rows.
|
|
618
|
+
*/
|
|
619
|
+
export function appendingIn(
|
|
620
|
+
appending: Appending,
|
|
621
|
+
home: Home,
|
|
622
|
+
): (connection: Connection) => Promise<{ method: number; body: Uint8Array }> {
|
|
623
|
+
const { stream, open, home: kept, event } = appendingPartsOf(appending)
|
|
624
|
+
if (kept !== home) {
|
|
625
|
+
const where = kept === undefined ? 'records.db' : kept.describe
|
|
626
|
+
throw new InvalidError(
|
|
627
|
+
`${home.describe}: events ${stream}: kept in ${where}, outside this batch of ${home.describe}`,
|
|
628
|
+
)
|
|
629
|
+
}
|
|
630
|
+
return async connection => {
|
|
631
|
+
const handle = await handleOn(connection, methods['records.open'], open)
|
|
632
|
+
return {
|
|
633
|
+
method: methods['records.append'],
|
|
634
|
+
body: RecordsAppend.encode({ handle, events: [event] }),
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
491
639
|
/** Every stream's events, to read: `store.events()`. */
|
|
492
640
|
export class AllEvents extends Reads {
|
|
493
641
|
readonly #link: Link
|
|
@@ -509,6 +657,29 @@ export class AllEvents extends Reads {
|
|
|
509
657
|
}))
|
|
510
658
|
}
|
|
511
659
|
|
|
660
|
+
/**
|
|
661
|
+
* What the events of `stream` carry, none of it read from a packed event: each name with how
|
|
662
|
+
* many events have it, and each key of their fields and contexts with the kinds of value it
|
|
663
|
+
* holds and how many events carry it. A stream with no events carries nothing.
|
|
664
|
+
*/
|
|
665
|
+
async fields(stream: string): Promise<StreamFields> {
|
|
666
|
+
const answer = await this.ask('records.fields', () => RecordsFieldsAsked.encode({ stream }))
|
|
667
|
+
const carried = RecordsStreamFields.decode(answer)
|
|
668
|
+
const info = (field: (typeof carried.fields & {})[number]): FieldInfo => ({
|
|
669
|
+
name: field.name ?? '',
|
|
670
|
+
kinds: (field.kinds ?? []) as ValueKind[],
|
|
671
|
+
count: field.count ?? 0,
|
|
672
|
+
})
|
|
673
|
+
return {
|
|
674
|
+
eventNames: (carried.eventNames ?? []).map(name => ({
|
|
675
|
+
name: name.name ?? '',
|
|
676
|
+
count: name.count ?? 0,
|
|
677
|
+
})),
|
|
678
|
+
fields: (carried.fields ?? []).map(info),
|
|
679
|
+
context: (carried.context ?? []).map(info),
|
|
680
|
+
}
|
|
681
|
+
}
|
|
682
|
+
|
|
512
683
|
/**
|
|
513
684
|
* Every event once, a batch a call, from a cursor the program keeps, the
|
|
514
685
|
* oldest event kept without one: `{ events, cursor, expired }`. With no
|