@tinyshed/tinystore 0.8.0 → 0.9.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/README.md +1 -1
- package/dist/backup.d.ts +2 -0
- package/dist/backup.js +1 -0
- package/dist/blobs.d.ts +18 -1
- package/dist/blobs.js +52 -45
- package/dist/directory.d.ts +72 -0
- package/dist/directory.js +121 -0
- package/dist/index.d.ts +2 -1
- package/dist/jobs.d.ts +33 -9
- package/dist/jobs.js +137 -34
- package/dist/metrics.d.ts +5 -1
- package/dist/metrics.js +2 -0
- package/dist/query.d.ts +8 -1
- package/dist/query.js +25 -4
- package/dist/search.d.ts +6 -1
- package/dist/search.js +4 -2
- package/dist/sql.d.ts +26 -2
- package/dist/sql.js +18 -5
- package/dist/store.d.ts +9 -0
- package/dist/store.js +11 -0
- package/dist/wire/protocol.d.ts +200 -7
- package/dist/wire/protocol.js +602 -13
- package/package.json +8 -8
- package/src/backup.ts +3 -0
- package/src/blobs.ts +66 -45
- package/src/directory.ts +235 -0
- package/src/index.ts +9 -0
- package/src/jobs.ts +178 -41
- package/src/metrics.ts +7 -1
- package/src/query.ts +28 -6
- package/src/search.ts +10 -5
- package/src/sql.ts +35 -5
- package/src/store.ts +12 -0
- package/src/wire/protocol.ts +637 -13
package/src/jobs.ts
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
import { FailureLog, messageOf } from './background.ts'
|
|
9
9
|
import type { Connection, Idempotence, Link } from './connection.ts'
|
|
10
|
-
import { CancelledError, CorruptError, InvalidError, UnavailableError } from './errors.ts'
|
|
10
|
+
import { CancelledError, CorruptError, errorOf, InvalidError, UnavailableError } from './errors.ts'
|
|
11
11
|
import { checkName, type Home, handleOn, type Open, type Via, viaLink } from './handles.ts'
|
|
12
12
|
import { type Rate, rateOf } from './limits.ts'
|
|
13
13
|
import { sayOwn } from './logger.ts'
|
|
@@ -16,9 +16,11 @@ import { ConnectionLostError, type Stream } from './session.ts'
|
|
|
16
16
|
import { type Duration, ms, type Time, unixMs } from './time.ts'
|
|
17
17
|
import type { Read, Written } from './wire/codec.ts'
|
|
18
18
|
import {
|
|
19
|
+
Handle,
|
|
19
20
|
JobsAnswer,
|
|
20
21
|
JobsCall,
|
|
21
22
|
JobsChanged,
|
|
23
|
+
JobsCounts,
|
|
22
24
|
JobsHeld,
|
|
23
25
|
JobsId,
|
|
24
26
|
JobsJob,
|
|
@@ -124,6 +126,40 @@ export interface ListOptions {
|
|
|
124
126
|
limit?: number | undefined
|
|
125
127
|
}
|
|
126
128
|
|
|
129
|
+
/** How many jobs a queue holds in each state, from one snapshot; a done job has left it. */
|
|
130
|
+
export interface JobCounts {
|
|
131
|
+
/** due later */
|
|
132
|
+
scheduled: number
|
|
133
|
+
/** due, and no handler runs it yet */
|
|
134
|
+
waiting: number
|
|
135
|
+
running: number
|
|
136
|
+
/** kept for the queue's keep */
|
|
137
|
+
failed: number
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Where a job without an id lies, which a cancel names it by: kept on the job
|
|
142
|
+
* a page gave, out of sight, and not on a copy of it.
|
|
143
|
+
*/
|
|
144
|
+
const ROW = Symbol('tinystore.job.row')
|
|
145
|
+
|
|
146
|
+
/** What a cancel sends of a job: its id, or for one without, its row. */
|
|
147
|
+
function namedBy(job: Job<unknown> | string, describe: string): { id?: string; row?: string } {
|
|
148
|
+
if (typeof job === 'string') {
|
|
149
|
+
return { id: job }
|
|
150
|
+
}
|
|
151
|
+
if (job.id !== undefined) {
|
|
152
|
+
return { id: job.id }
|
|
153
|
+
}
|
|
154
|
+
const row = (job as { [ROW]?: string })[ROW]
|
|
155
|
+
if (row === undefined) {
|
|
156
|
+
throw new InvalidError(
|
|
157
|
+
`${describe}: a job without an id names no row: cancel the job a list gave, not a copy`,
|
|
158
|
+
)
|
|
159
|
+
}
|
|
160
|
+
return { row }
|
|
161
|
+
}
|
|
162
|
+
|
|
127
163
|
/** A page of a queue's jobs, and where the next starts when there is one. */
|
|
128
164
|
export interface JobPage<T> {
|
|
129
165
|
jobs: Job<T>[]
|
|
@@ -465,27 +501,115 @@ async function getJob<T>(source: Source<T>, id: string): Promise<Job<T> | undefi
|
|
|
465
501
|
return found.found === true ? jobOf(source, found) : undefined
|
|
466
502
|
}
|
|
467
503
|
|
|
468
|
-
async function cancelJob<T>(source: Source<T>,
|
|
504
|
+
async function cancelJob<T>(source: Source<T>, job: Job<T> | string): Promise<boolean> {
|
|
505
|
+
const named = namedBy(job as Job<unknown> | string, source.describe)
|
|
469
506
|
const body = await callOf(
|
|
470
507
|
source,
|
|
471
508
|
methods['jobs.cancel'],
|
|
472
|
-
handle => JobsId.encode({ handle,
|
|
509
|
+
handle => JobsId.encode({ handle, ...named }),
|
|
473
510
|
'write',
|
|
474
511
|
)
|
|
475
512
|
return JobsChanged.decode(body).changed === true
|
|
476
513
|
}
|
|
477
514
|
|
|
515
|
+
/**
|
|
516
|
+
* A page of the queue's jobs. A walk's page has a job whose value no longer
|
|
517
|
+
* reads as its error in its place; any other page fails on it.
|
|
518
|
+
*/
|
|
519
|
+
async function pageOf<T>(
|
|
520
|
+
source: Source<T>,
|
|
521
|
+
options: ListOptions,
|
|
522
|
+
walk: boolean,
|
|
523
|
+
): Promise<{ jobs: (Job<T> | Error)[]; next: string | undefined }> {
|
|
524
|
+
const body = await callOf(
|
|
525
|
+
source,
|
|
526
|
+
methods['jobs.list'],
|
|
527
|
+
handle => JobsList.encode({ handle, ...options, walk }),
|
|
528
|
+
'read',
|
|
529
|
+
)
|
|
530
|
+
const page = JobsPage.decode(body)
|
|
531
|
+
const jobs: (Job<T> | Error)[] = []
|
|
532
|
+
for (const found of page.jobs ?? []) {
|
|
533
|
+
if (found.failure !== undefined) {
|
|
534
|
+
const { code, message, what } = found.failure
|
|
535
|
+
jobs.push(errorOf(code ?? 'internal', message ?? '', what ?? {}))
|
|
536
|
+
continue
|
|
537
|
+
}
|
|
538
|
+
const job = jobOf(source, found)
|
|
539
|
+
jobs.push(walk ? await job.catch((err: Error) => err) : await job)
|
|
540
|
+
}
|
|
541
|
+
return { jobs, next: page.next }
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
/**
|
|
545
|
+
* Every job a filter names, a page at a time. A job whose value no longer
|
|
546
|
+
* reads is its error in its place: `next()` rejects with it, and the call
|
|
547
|
+
* after goes on past it, as a `for await` that catches it and iterates the
|
|
548
|
+
* same walk again does.
|
|
549
|
+
*/
|
|
550
|
+
class Walk<T> implements AsyncGenerator<Job<T>, void, unknown> {
|
|
551
|
+
readonly #source: Source<T>
|
|
552
|
+
readonly #options: Omit<ListOptions, 'after'>
|
|
553
|
+
#jobs: (Job<T> | Error)[] = []
|
|
554
|
+
#after: string | undefined
|
|
555
|
+
#ended = false
|
|
556
|
+
|
|
557
|
+
constructor(source: Source<T>, options: Omit<ListOptions, 'after'>) {
|
|
558
|
+
this.#source = source
|
|
559
|
+
this.#options = options
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
async next(): Promise<IteratorResult<Job<T>, void>> {
|
|
563
|
+
while (this.#jobs.length === 0 && !this.#ended) {
|
|
564
|
+
const page = await pageOf(this.#source, { ...this.#options, after: this.#after }, true)
|
|
565
|
+
this.#jobs = page.jobs
|
|
566
|
+
this.#after = page.next
|
|
567
|
+
this.#ended = page.next === undefined
|
|
568
|
+
}
|
|
569
|
+
const job = this.#jobs.shift()
|
|
570
|
+
if (job === undefined) {
|
|
571
|
+
return { done: true, value: undefined }
|
|
572
|
+
}
|
|
573
|
+
if (job instanceof Error) {
|
|
574
|
+
throw job
|
|
575
|
+
}
|
|
576
|
+
return { done: false, value: job }
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
async return(): Promise<IteratorResult<Job<T>, void>> {
|
|
580
|
+
this.#end()
|
|
581
|
+
return { done: true, value: undefined }
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
async throw(err: unknown): Promise<IteratorResult<Job<T>, void>> {
|
|
585
|
+
this.#end()
|
|
586
|
+
throw err
|
|
587
|
+
}
|
|
588
|
+
|
|
589
|
+
[Symbol.asyncIterator](): this {
|
|
590
|
+
return this
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
async [Symbol.asyncDispose](): Promise<void> {
|
|
594
|
+
this.#end()
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
#end(): void {
|
|
598
|
+
this.#jobs = []
|
|
599
|
+
this.#ended = true
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
478
603
|
/** A job as the server sent it, its value read as the queue reads one. */
|
|
479
604
|
async function jobOf<T>(source: Source<T>, found: Read<typeof JobsJob.fields>): Promise<Job<T>> {
|
|
480
605
|
let value: T
|
|
481
606
|
try {
|
|
482
607
|
value = await source.values.decode(found.value ?? 'null')
|
|
483
608
|
} catch (err) {
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
)
|
|
609
|
+
const named = found.id === undefined ? 'a job without an id' : `id ${JSON.stringify(found.id)}`
|
|
610
|
+
throw new CorruptError(`${source.describe}: ${named}: ${messageOf(err)}`)
|
|
487
611
|
}
|
|
488
|
-
|
|
612
|
+
const job: Job<T> = {
|
|
489
613
|
id: found.id,
|
|
490
614
|
value,
|
|
491
615
|
state: (found.state ?? 'waiting') as JobState,
|
|
@@ -504,6 +628,10 @@ async function jobOf<T>(source: Source<T>, found: Read<typeof JobsJob.fields>):
|
|
|
504
628
|
endedAt: new Date(found.endedAt ?? found.startedAt),
|
|
505
629
|
},
|
|
506
630
|
}
|
|
631
|
+
if (found.row !== undefined) {
|
|
632
|
+
Object.defineProperty(job, ROW, { value: found.row })
|
|
633
|
+
}
|
|
634
|
+
return job
|
|
507
635
|
}
|
|
508
636
|
|
|
509
637
|
/**
|
|
@@ -650,12 +778,34 @@ export class Queue<T> {
|
|
|
650
778
|
}
|
|
651
779
|
|
|
652
780
|
/**
|
|
653
|
-
* Takes
|
|
654
|
-
*
|
|
781
|
+
* Takes a job, whatever its state, and says whether it was there: the job
|
|
782
|
+
* under an id, or a job a list gave, by its id or, for one without, by the
|
|
783
|
+
* row it was listed from. A job retried, run again for its repeat or
|
|
784
|
+
* cancelled since is in no row, and its cancel takes nothing, never another
|
|
785
|
+
* job. A running one's handler sees its signal abort, and what it returns
|
|
655
786
|
* then settles nothing; a repeating one stops.
|
|
787
|
+
*
|
|
788
|
+
* for await (const job of emails.all({ state: 'scheduled' })) if (job.value.to === bounced) await emails.cancel(job)
|
|
656
789
|
*/
|
|
657
|
-
cancel(
|
|
658
|
-
return cancelJob(this.#source,
|
|
790
|
+
cancel(job: Job<T> | string): Promise<boolean> {
|
|
791
|
+
return cancelJob(this.#source, job)
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
/** How many jobs the queue holds in each state, from one snapshot; it reads no value. */
|
|
795
|
+
async counts(): Promise<JobCounts> {
|
|
796
|
+
const body = await callOf(
|
|
797
|
+
this.#source,
|
|
798
|
+
methods['jobs.counts'],
|
|
799
|
+
handle => Handle.encode({ handle }),
|
|
800
|
+
'read',
|
|
801
|
+
)
|
|
802
|
+
const { scheduled, waiting, running, failed } = JobsCounts.decode(body)
|
|
803
|
+
return {
|
|
804
|
+
scheduled: scheduled ?? 0,
|
|
805
|
+
waiting: waiting ?? 0,
|
|
806
|
+
running: running ?? 0,
|
|
807
|
+
failed: failed ?? 0,
|
|
808
|
+
}
|
|
659
809
|
}
|
|
660
810
|
|
|
661
811
|
/**
|
|
@@ -685,31 +835,17 @@ export class Queue<T> {
|
|
|
685
835
|
* then those without as they run, then those without that failed.
|
|
686
836
|
*/
|
|
687
837
|
async list(options: ListOptions = {}): Promise<JobPage<T>> {
|
|
688
|
-
const
|
|
689
|
-
|
|
690
|
-
methods['jobs.list'],
|
|
691
|
-
handle => JobsList.encode({ handle, ...options }),
|
|
692
|
-
'read',
|
|
693
|
-
)
|
|
694
|
-
const page = JobsPage.decode(body)
|
|
695
|
-
const jobs: Job<T>[] = []
|
|
696
|
-
for (const found of page.jobs ?? []) {
|
|
697
|
-
jobs.push(await jobOf(this.#source, found))
|
|
698
|
-
}
|
|
699
|
-
return { jobs, next: page.next }
|
|
838
|
+
const page = await pageOf(this.#source, options, false)
|
|
839
|
+
return { jobs: page.jobs as Job<T>[], next: page.next }
|
|
700
840
|
}
|
|
701
841
|
|
|
702
|
-
/**
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
return
|
|
710
|
-
}
|
|
711
|
-
after = page.next
|
|
712
|
-
}
|
|
842
|
+
/**
|
|
843
|
+
* Every job the options name, a page at a time, holding nothing between
|
|
844
|
+
* pages. A job whose value no longer reads is a CorruptError in its place,
|
|
845
|
+
* naming it, and iterating the same walk again goes on past it.
|
|
846
|
+
*/
|
|
847
|
+
all(options: Omit<ListOptions, 'after'> = {}): AsyncGenerator<Job<T>> {
|
|
848
|
+
return new Walk(this.#source, options)
|
|
713
849
|
}
|
|
714
850
|
|
|
715
851
|
/**
|
|
@@ -780,8 +916,8 @@ export class TxQueue<T> {
|
|
|
780
916
|
return this.#queue.update(id, value, options)
|
|
781
917
|
}
|
|
782
918
|
|
|
783
|
-
cancel(
|
|
784
|
-
return this.#queue.cancel(
|
|
919
|
+
cancel(job: Job<T> | string): Promise<boolean> {
|
|
920
|
+
return this.#queue.cancel(job)
|
|
785
921
|
}
|
|
786
922
|
|
|
787
923
|
/** The job under id as the transaction sees it, its own writes among it. */
|
|
@@ -836,12 +972,12 @@ export function addingIn(
|
|
|
836
972
|
}
|
|
837
973
|
}
|
|
838
974
|
|
|
839
|
-
/** A write the store's transaction keeps for its commit: its method, and its call or the
|
|
975
|
+
/** A write the store's transaction keeps for its commit: its method, and its call or the job it cancels. */
|
|
840
976
|
export interface JobOp {
|
|
841
977
|
source: Source<unknown>
|
|
842
978
|
method: number
|
|
843
979
|
call?: Omit<Written<typeof JobsCall.fields>, 'handle'>
|
|
844
|
-
id?: string
|
|
980
|
+
id?: { id?: string; row?: string }
|
|
845
981
|
}
|
|
846
982
|
|
|
847
983
|
/**
|
|
@@ -880,8 +1016,9 @@ export class StoreTxQueue<T> {
|
|
|
880
1016
|
this.#write('jobs.update', { ...jobFields(options), id }, value)
|
|
881
1017
|
}
|
|
882
1018
|
|
|
883
|
-
/** Takes the job under id, whatever its state, with the commit. */
|
|
884
|
-
async cancel(
|
|
1019
|
+
/** Takes the job under id, or a job a list gave, whatever its state, with the commit. */
|
|
1020
|
+
async cancel(job: Job<T> | string): Promise<void> {
|
|
1021
|
+
const id = namedBy(job as Job<unknown> | string, this.#source.describe)
|
|
885
1022
|
this.#keep({ source: this.#source as Source<unknown>, method: methods['jobs.cancel'], id })
|
|
886
1023
|
}
|
|
887
1024
|
|
|
@@ -904,7 +1041,7 @@ export async function commitJobs(link: Link, ops: readonly JobOp[]): Promise<voi
|
|
|
904
1041
|
writes.push({
|
|
905
1042
|
method: op.method,
|
|
906
1043
|
call: op.call === undefined ? undefined : { ...op.call, handle },
|
|
907
|
-
id: op.id === undefined ? undefined : { handle,
|
|
1044
|
+
id: op.id === undefined ? undefined : { handle, ...op.id },
|
|
908
1045
|
})
|
|
909
1046
|
}
|
|
910
1047
|
await connection.session.call(methods['jobs.tx'], JobsTx.encode({ writes }))
|
package/src/metrics.ts
CHANGED
|
@@ -185,6 +185,8 @@ export interface AggregateQuery<O extends Op = Op> extends Query {
|
|
|
185
185
|
without?: readonly string[] | undefined
|
|
186
186
|
/** a timer's percentile also gives a point between its bounds, interpolated: an estimate */
|
|
187
187
|
estimate?: boolean | undefined
|
|
188
|
+
/** a NaN sample counts in no bucket, which says how many it passed over in skipped; an infinity is still refused */
|
|
189
|
+
skipNaN?: boolean | undefined
|
|
188
190
|
}
|
|
189
191
|
|
|
190
192
|
/** What every bucket of an aggregate says of itself. */
|
|
@@ -194,6 +196,8 @@ interface Spanned {
|
|
|
194
196
|
/** how many samples it comes from; a timer's, how many durations */
|
|
195
197
|
samples: number
|
|
196
198
|
resets: number
|
|
199
|
+
/** the NaN samples it passed over, with skipNaN */
|
|
200
|
+
skipped: number
|
|
197
201
|
/** its value comes from less than its span: the range, the clock or retention cut it */
|
|
198
202
|
partial: boolean
|
|
199
203
|
/** its first step began before the bucket before it: a whole bucket between had no samples */
|
|
@@ -225,7 +229,7 @@ export interface Aggregate<O extends Op = Op> {
|
|
|
225
229
|
name: string
|
|
226
230
|
/** the labels by kept or without left */
|
|
227
231
|
labels: Labels
|
|
228
|
-
/** a bucket without samples is left out */
|
|
232
|
+
/** a bucket without samples is left out, and so is one of NaN alone */
|
|
229
233
|
buckets: Bucket<O>[]
|
|
230
234
|
/** a page cut the group here: the page its next reads begins with the rest of its buckets */
|
|
231
235
|
more: boolean
|
|
@@ -754,6 +758,7 @@ function aggregateOf(query: AggregateQuery): Parameters<typeof MetricsAggregateA
|
|
|
754
758
|
by: query.by === undefined ? undefined : [...query.by],
|
|
755
759
|
without: query.without === undefined ? undefined : [...query.without],
|
|
756
760
|
estimate: query.estimate === true,
|
|
761
|
+
skipNan: query.skipNaN === true,
|
|
757
762
|
}
|
|
758
763
|
}
|
|
759
764
|
|
|
@@ -771,6 +776,7 @@ function bucketOf(bucket: WireBucket): PercentileBucket {
|
|
|
771
776
|
at: new Date(bucket.at ?? 0),
|
|
772
777
|
samples: bucket.samples ?? 0,
|
|
773
778
|
resets: bucket.resets ?? 0,
|
|
779
|
+
skipped: bucket.skipped ?? 0,
|
|
774
780
|
partial: bucket.partial === true,
|
|
775
781
|
gap: bucket.gap === true,
|
|
776
782
|
}
|
package/src/query.ts
CHANGED
|
@@ -15,12 +15,14 @@ import {
|
|
|
15
15
|
grouped,
|
|
16
16
|
identOf,
|
|
17
17
|
joinedWith,
|
|
18
|
+
type Ordered,
|
|
18
19
|
placeholders,
|
|
19
20
|
type Row,
|
|
20
21
|
type Runner,
|
|
21
22
|
Sql,
|
|
22
23
|
type SqlArg,
|
|
23
24
|
type SqlValue,
|
|
25
|
+
walk,
|
|
24
26
|
} from './sql.ts'
|
|
25
27
|
|
|
26
28
|
/** How a column SQLite keeps otherwise reads in JavaScript. */
|
|
@@ -255,16 +257,21 @@ export class Query<T = Row> implements Embeddable {
|
|
|
255
257
|
return Number(answered.rows[0]?.[0] ?? 0)
|
|
256
258
|
}
|
|
257
259
|
|
|
258
|
-
/**
|
|
260
|
+
/**
|
|
261
|
+
* The rows one at a time, as their parts arrive: for more than fits in
|
|
262
|
+
* memory. The server reads them from one snapshot of five seconds at
|
|
263
|
+
* most; ordered by columns by their names, a unique one last, as `list`
|
|
264
|
+
* is, it reads on past that after the last row it sent, and any other
|
|
265
|
+
* walk fails `limit` there. Inside a transaction it reads the rows `all`
|
|
266
|
+
* does.
|
|
267
|
+
*/
|
|
259
268
|
async *each(): AsyncGenerator<T> {
|
|
260
|
-
const runner = this.#source.runner
|
|
261
|
-
|
|
262
|
-
}
|
|
263
|
-
if (runner.each === undefined) {
|
|
269
|
+
const runner = this.#source.runner
|
|
270
|
+
if (runner[walk] === undefined) {
|
|
264
271
|
yield* await this.all()
|
|
265
272
|
return
|
|
266
273
|
}
|
|
267
|
-
for await (const row of runner
|
|
274
|
+
for await (const row of runner[walk]<Row>(render(this.#parts), walkOrder(this.#parts.order))) {
|
|
268
275
|
yield (await this.#rows(Object.keys(row), [Object.values(row)]))[0]!
|
|
269
276
|
}
|
|
270
277
|
}
|
|
@@ -890,6 +897,21 @@ function render(parts: Parts): Sql {
|
|
|
890
897
|
return new Sql(text, values)
|
|
891
898
|
}
|
|
892
899
|
|
|
900
|
+
/**
|
|
901
|
+
* The columns a walk's rows are ordered by, as its rows name them, when each
|
|
902
|
+
* is a column by its name: the server reads on past one snapshot by them, as
|
|
903
|
+
* `list` pages by them.
|
|
904
|
+
*/
|
|
905
|
+
function walkOrder(order: Parts['order']): Ordered[] | undefined {
|
|
906
|
+
if (order.length === 0 || order.some(each => each.column === undefined)) {
|
|
907
|
+
return undefined
|
|
908
|
+
}
|
|
909
|
+
return order.map(each => ({
|
|
910
|
+
column: each.column!.split('.').pop()!,
|
|
911
|
+
descending: each.direction === 'desc',
|
|
912
|
+
}))
|
|
913
|
+
}
|
|
914
|
+
|
|
893
915
|
/**
|
|
894
916
|
* The condition that starts a page after a row's order values. SQLite orders
|
|
895
917
|
* a null before every value, first in ascending order and last in descending,
|
package/src/search.ts
CHANGED
|
@@ -156,10 +156,13 @@ export interface Found<T> {
|
|
|
156
156
|
corrected?: string | undefined
|
|
157
157
|
}
|
|
158
158
|
|
|
159
|
-
/**
|
|
159
|
+
/**
|
|
160
|
+
* Where an index stands: serving what it was opened with, read again beside it, or a rebuild that failed.
|
|
161
|
+
* `error` beside `ready` and `rebuilding` is the last maintenance pass's failure while the passes keep failing.
|
|
162
|
+
*/
|
|
160
163
|
export type IndexStatus =
|
|
161
|
-
| { state: 'ready' }
|
|
162
|
-
| { state: 'rebuilding'; done: number }
|
|
164
|
+
| { state: 'ready'; error?: string | undefined }
|
|
165
|
+
| { state: 'rebuilding'; done: number; error?: string | undefined }
|
|
163
166
|
| { state: 'failed'; error: string }
|
|
164
167
|
|
|
165
168
|
/** An index as `store.indexes()` lists it. */
|
|
@@ -463,13 +466,15 @@ function statusOf(status: {
|
|
|
463
466
|
done?: number | undefined
|
|
464
467
|
error?: string | undefined
|
|
465
468
|
}): IndexStatus {
|
|
469
|
+
// an empty error is none: the wire's field is a string
|
|
470
|
+
const error = status.error ? { error: status.error } : {}
|
|
466
471
|
switch (status.state) {
|
|
467
472
|
case 'rebuilding':
|
|
468
|
-
return { state: 'rebuilding', done: status.done ?? 0 }
|
|
473
|
+
return { state: 'rebuilding', done: status.done ?? 0, ...error }
|
|
469
474
|
case 'failed':
|
|
470
475
|
return { state: 'failed', error: status.error ?? '' }
|
|
471
476
|
default:
|
|
472
|
-
return { state: 'ready' }
|
|
477
|
+
return { state: 'ready', ...error }
|
|
473
478
|
}
|
|
474
479
|
}
|
|
475
480
|
|
package/src/sql.ts
CHANGED
|
@@ -91,6 +91,14 @@ export interface DatabaseOptions {
|
|
|
91
91
|
* with it. A database open already keeps what it was opened with.
|
|
92
92
|
*/
|
|
93
93
|
durability?: Durability
|
|
94
|
+
/**
|
|
95
|
+
* Whether backups take the database; `false` keeps its file and the
|
|
96
|
+
* bytes of its files in `local/` rather than `sql/`, where no backup
|
|
97
|
+
* looks, open or not, and no node copies it: each node that opens it
|
|
98
|
+
* writes its own. A name lives in one folder: one whose file lies in the
|
|
99
|
+
* other is InvalidError, naming both.
|
|
100
|
+
*/
|
|
101
|
+
backup?: boolean
|
|
94
102
|
}
|
|
95
103
|
|
|
96
104
|
/** What a write changed: the rows, and SQLite's rowid of the row it inserted, 0 when none. */
|
|
@@ -578,7 +586,8 @@ export async function openDatabase(
|
|
|
578
586
|
checkDurability(options.durability, `sql ${name}`)
|
|
579
587
|
const migrations =
|
|
580
588
|
options.migrations === undefined ? undefined : await migrationFiles(options.migrations)
|
|
581
|
-
const
|
|
589
|
+
const backup = options.backup === false ? false : undefined
|
|
590
|
+
const open = SqlOpen.encode({ name, migrations, durability: options.durability, backup })
|
|
582
591
|
const db = new Database(link, workers, name, open)
|
|
583
592
|
await link.run('read', connection => db.handle(connection))
|
|
584
593
|
return db
|
|
@@ -708,11 +717,21 @@ function doneOf(done: {
|
|
|
708
717
|
|
|
709
718
|
type Want = 'all' | 'one' | 'scalar'
|
|
710
719
|
|
|
720
|
+
/** A column a walk's rows are ordered by, as the rows name it. */
|
|
721
|
+
export interface Ordered {
|
|
722
|
+
column: string
|
|
723
|
+
descending: boolean
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
/** The database's walk of a statement's rows, which its table's `each` reads; a transaction has none. */
|
|
727
|
+
export const walk: unique symbol = Symbol('walk')
|
|
728
|
+
|
|
711
729
|
/** What reads and writes a database: the database itself, or a transaction of it. */
|
|
712
730
|
export interface Runner {
|
|
713
731
|
readonly describe: string
|
|
714
732
|
rows(statement: Sql, want: Want): Promise<Answered>
|
|
715
733
|
exec(statement: Sql): Promise<Done>
|
|
734
|
+
[walk]?<T = Row>(statement: Sql, order: readonly Ordered[] | undefined): AsyncGenerator<T>
|
|
716
735
|
}
|
|
717
736
|
|
|
718
737
|
/** The reads every runner shares: all, one and scalar over its rows. */
|
|
@@ -944,19 +963,30 @@ export class Database implements Runner, Home {
|
|
|
944
963
|
|
|
945
964
|
/**
|
|
946
965
|
* The rows a statement gives, one at a time as their parts arrive: an
|
|
947
|
-
* export that holds
|
|
948
|
-
* one snapshot,
|
|
966
|
+
* export that holds a part, not every row, however many there are. The
|
|
967
|
+
* server reads them a part at a time from one snapshot, which lasts five
|
|
968
|
+
* seconds at most: a walk that takes longer fails `limit`, and is read a
|
|
969
|
+
* page at a time with a table's `list`, or ordered by its key with a
|
|
970
|
+
* table's `each`, which reads on past it.
|
|
949
971
|
*/
|
|
950
972
|
async *each<T = Row>(...statement: Statement): AsyncGenerator<T> {
|
|
973
|
+
yield* this[walk]<T>(statementOf(statement), undefined)
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* A statement's rows as the server reads them, `want: each`; ordered by
|
|
978
|
+
* `order`, a unique column last, the server reads on past one snapshot.
|
|
979
|
+
*/
|
|
980
|
+
async *[walk]<T = Row>(piece: Sql, order: readonly Ordered[] | undefined): AsyncGenerator<T> {
|
|
951
981
|
this.#refuseAround('a read')
|
|
952
|
-
const piece = statementOf(statement)
|
|
953
982
|
const connection = await this.#link.connection()
|
|
954
983
|
const handle = await this.handle(connection)
|
|
955
984
|
const body = SqlQuery.encode({
|
|
956
985
|
handle,
|
|
957
986
|
text: piece.text,
|
|
958
987
|
values: [...piece.values],
|
|
959
|
-
want: '
|
|
988
|
+
want: 'each',
|
|
989
|
+
order: order === undefined ? undefined : [...order],
|
|
960
990
|
})
|
|
961
991
|
const stream = await connection.session.open(methods['sql.query'], body, true)
|
|
962
992
|
try {
|
package/src/store.ts
CHANGED
|
@@ -19,6 +19,7 @@ import {
|
|
|
19
19
|
type SelfMetrics,
|
|
20
20
|
sidecar,
|
|
21
21
|
} from './connection.ts'
|
|
22
|
+
import { type Directory, type DirectoryOptions, openDirectory } from './directory.ts'
|
|
22
23
|
import { ClosedError, InvalidError } from './errors.ts'
|
|
23
24
|
import { Instruments } from './instruments.ts'
|
|
24
25
|
import {
|
|
@@ -491,6 +492,17 @@ export class Store implements AsyncDisposable {
|
|
|
491
492
|
return openFiles(this.#link, name, options)
|
|
492
493
|
}
|
|
493
494
|
|
|
495
|
+
/**
|
|
496
|
+
* A folder of the store's machine under a name, which the store lists and
|
|
497
|
+
* reads and never writes; over a token, the server's own by its name:
|
|
498
|
+
*
|
|
499
|
+
* const movies = store.directory('movies', { path: 'D:/Movies' })
|
|
500
|
+
* const photos = store.directory('photos', { path: '/mnt/photos', every: '15m', ignore: ['*.part', '.*'] })
|
|
501
|
+
*/
|
|
502
|
+
directory(name: string, options?: DirectoryOptions): Directory {
|
|
503
|
+
return openDirectory(this.#link, name, options)
|
|
504
|
+
}
|
|
505
|
+
|
|
494
506
|
/**
|
|
495
507
|
* The application's database name, sql/<name>.db, once its migrations
|
|
496
508
|
* are applied and checked: a migration that fails, or one changed after
|