@tinyshed/tinystore 0.5.1 → 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/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/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
- /** One page, from one moment of the file: `{ events, next }`. */
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 new Events(link, stream, RecordsOpen.encode({ stream, keep }))
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: Uint8Array
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: Uint8Array) {
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
package/src/sql.ts CHANGED
@@ -35,6 +35,15 @@ import {
35
35
  } from './jobs.ts'
36
36
  import { Bucket, type BucketOptions, bucketOpen, partsOf, type Values, valuesOf } from './kv.ts'
37
37
  import { Table, type TableOptions } from './query.ts'
38
+ import {
39
+ Appending,
40
+ appendingIn,
41
+ Events,
42
+ type EventsOptions,
43
+ eventsOpen,
44
+ eventsParts,
45
+ TxEvents,
46
+ } from './records.ts'
38
47
  import type { StandardSchemaV1 } from './schema.ts'
39
48
  import { TableIndex, type TableIndexOptions, tableIndexOpen } from './search.ts'
40
49
  import { ConnectionLostError, type Stream } from './session.ts'
@@ -791,6 +800,25 @@ export class Database implements Runner, Home {
791
800
  return new Files(this.#link, name, open, [], this)
792
801
  }
793
802
 
803
+ /**
804
+ * The store's stream of events `stream`, opened through this database:
805
+ * the same calls and reads as `store.events(stream)` gives, the stream in
806
+ * records.db where every stream lives. Its appends inside `db.tx`,
807
+ * through `tx.with(audit)`, and in `db.batch`, through `audit.appending`,
808
+ * are kept in this database's file with the rows and reach the stream
809
+ * once they commit, before the call returns: an event exists exactly when
810
+ * the change it records does.
811
+ *
812
+ * const audit = db.events('audit')
813
+ */
814
+ events(stream: string, options?: EventsOptions): Events {
815
+ const bytes = eventsOpen(stream, options)
816
+ const open = this.keep(methods['records.open'], bytes, async connection =>
817
+ eventsOpen(stream, options, await this.handle(connection)),
818
+ )
819
+ return new Events(this.#link, stream, open, this)
820
+ }
821
+
794
822
  /**
795
823
  * An index of a table of this database, kept in its file: its rows found
796
824
  * again by their words, vectors or both, each hit's value its row as
@@ -911,12 +939,14 @@ export class Database implements Runner, Home {
911
939
  /**
912
940
  * Writes known before they run, written as one: all of them or none, in a
913
941
  * shared commit, and what each did, in their order. A write is a statement,
914
- * or a job's add of a queue opened from the database, `queue.adding`, which
915
- * commits with the rows and changed 1 job, or 0 when its id was taken.
942
+ * a job's add of a queue opened from the database, `queue.adding`, which
943
+ * commits with the rows and changed 1 job, or 0 when its id was taken, or
944
+ * an event's append of a stream opened from it, `audit.appending`, which
945
+ * reaches the stream before the batch returns.
916
946
  *
917
947
  * await db.batch([sql`update notes set body = ${body} where id = ${id}`, indexing.adding({ noteId: id })])
918
948
  */
919
- async batch(writes: readonly (Sql | Adding)[]): Promise<Done[]> {
949
+ async batch(writes: readonly (Sql | Adding | Appending)[]): Promise<Done[]> {
920
950
  const parts = writes.map(each => {
921
951
  if (each instanceof Sql) {
922
952
  return { text: each.text, values: [...each.values] }
@@ -924,7 +954,12 @@ export class Database implements Runner, Home {
924
954
  if (each instanceof Adding) {
925
955
  return addingIn(each, this)
926
956
  }
927
- throw new InvalidError("a batch takes pieces of SQL, sql`…`, and a queue's adding")
957
+ if (each instanceof Appending) {
958
+ return appendingIn(each, this)
959
+ }
960
+ throw new InvalidError(
961
+ "a batch takes pieces of SQL, sql`…`, a queue's adding and a stream's appending",
962
+ )
928
963
  })
929
964
  this.#refuseAround('a batch')
930
965
  const answered = await this.#link.run('write', async connection => {
@@ -1077,9 +1112,10 @@ export class SqlTx implements Runner {
1077
1112
  with<T>(queue: Queue<T>): TxQueue<T>
1078
1113
  with<T>(channel: Channel<T>): TxChannel<T>
1079
1114
  with(files: Files): Files
1115
+ with(events: Events): TxEvents
1080
1116
  with(
1081
- handle: Bucket<unknown> | Queue<unknown> | Channel<unknown> | Files,
1082
- ): Bucket<unknown> | TxQueue<unknown> | TxChannel<unknown> | Files {
1117
+ handle: Bucket<unknown> | Queue<unknown> | Channel<unknown> | Files | Events,
1118
+ ): Bucket<unknown> | TxQueue<unknown> | TxChannel<unknown> | Files | TxEvents {
1083
1119
  const channel = channelParts(handle)
1084
1120
  if (channel !== undefined) {
1085
1121
  // a message published here is handed over once the transaction commits, at the writer
@@ -1103,8 +1139,13 @@ export class SqlTx implements Runner {
1103
1139
  this.#takes(files.home, files.open, `files ${files.name}`)
1104
1140
  return files.inside(this.#via)
1105
1141
  }
1142
+ const events = eventsParts(handle)
1143
+ if (events !== undefined) {
1144
+ this.#takes(events.home, events.open, `events ${events.stream}`)
1145
+ return new TxEvents(events, this.#via)
1146
+ }
1106
1147
  throw new InvalidError(
1107
- `${this.describe}: a transaction takes in a bucket, a queue or files opened from its database`,
1148
+ `${this.describe}: a transaction takes in a bucket, a queue, files or events opened from its database`,
1108
1149
  )
1109
1150
  }
1110
1151