@effect/platform-browser 4.0.0-beta.42 → 4.0.0-beta.43

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.
@@ -0,0 +1,862 @@
1
+ import * as Data from "effect/Data";
2
+ import * as Effect from "effect/Effect";
3
+ import { BaseProto } from "effect/Inspectable";
4
+ import * as Pipeable from "effect/Pipeable";
5
+ import * as Schema from "effect/Schema";
6
+ import * as SchemaIssue from "effect/SchemaIssue";
7
+ import * as SchemaParser from "effect/SchemaParser";
8
+ import * as Utils from "effect/Utils";
9
+ const ErrorTypeId = "~@effect/platform-browser/IndexedDbQueryBuilder/IndexedDbQueryError";
10
+ const CommonProto = {
11
+ [Symbol.iterator]() {
12
+ return new Utils.SingleShotGen(this);
13
+ },
14
+ ...Pipeable.Prototype,
15
+ ...BaseProto,
16
+ toJSON() {
17
+ return {
18
+ _id: "IndexedDbQueryBuilder"
19
+ };
20
+ }
21
+ };
22
+ /**
23
+ * @since 4.0.0
24
+ * @category errors
25
+ */
26
+ export class IndexedDbQueryError extends /*#__PURE__*/Data.TaggedError("IndexedDbQueryError") {
27
+ /**
28
+ * @since 4.0.0
29
+ */
30
+ [ErrorTypeId] = ErrorTypeId;
31
+ message = this.reason;
32
+ }
33
+ const applyDelete = query => Effect.callback(resume => {
34
+ const database = query.delete.from.database;
35
+ const IDBKeyRange = query.delete.from.IDBKeyRange;
36
+ let transaction = query.delete.from.transaction;
37
+ transaction ??= database.transaction([query.delete.from.table.tableName], "readwrite");
38
+ const objectStore = transaction.objectStore(query.delete.from.table.tableName);
39
+ const predicate = query.predicate;
40
+ let keyRange = undefined;
41
+ if (query.only !== undefined) {
42
+ keyRange = IDBKeyRange.only(query.only);
43
+ } else if (query.lowerBound !== undefined && query.upperBound !== undefined) {
44
+ keyRange = IDBKeyRange.bound(query.lowerBound, query.upperBound, query.excludeLowerBound, query.excludeUpperBound);
45
+ } else if (query.lowerBound !== undefined) {
46
+ keyRange = IDBKeyRange.lowerBound(query.lowerBound, query.excludeLowerBound);
47
+ } else if (query.upperBound !== undefined) {
48
+ keyRange = IDBKeyRange.upperBound(query.upperBound, query.excludeUpperBound);
49
+ }
50
+ let request;
51
+ if (query.limitValue !== undefined || predicate) {
52
+ const cursorRequest = objectStore.openCursor();
53
+ let count = 0;
54
+ cursorRequest.onerror = () => {
55
+ resume(Effect.fail(new IndexedDbQueryError({
56
+ reason: "TransactionError",
57
+ cause: cursorRequest.error
58
+ })));
59
+ };
60
+ cursorRequest.onsuccess = () => {
61
+ const cursor = cursorRequest.result;
62
+ if (cursor === null) {
63
+ return resume(Effect.void);
64
+ }
65
+ if (predicate === undefined || predicate(cursor.value)) {
66
+ const deleteRequest = cursor.delete();
67
+ deleteRequest.onerror = () => {
68
+ resume(Effect.fail(new IndexedDbQueryError({
69
+ reason: "TransactionError",
70
+ cause: deleteRequest.error
71
+ })));
72
+ };
73
+ count += 1;
74
+ }
75
+ if (query.limitValue === undefined || count < query.limitValue) {
76
+ return cursor.continue();
77
+ }
78
+ resume(Effect.void);
79
+ };
80
+ } else if (keyRange !== undefined) {
81
+ request = objectStore.delete(keyRange);
82
+ request.onerror = event => {
83
+ resume(Effect.fail(new IndexedDbQueryError({
84
+ reason: "TransactionError",
85
+ cause: event
86
+ })));
87
+ };
88
+ request.onsuccess = () => {
89
+ resume(Effect.succeed(request.result));
90
+ };
91
+ } else {
92
+ resume(Effect.die(new Error("No key range provided for delete operation")));
93
+ }
94
+ });
95
+ const getReadonlyObjectStore = query => {
96
+ const database = query.from.database;
97
+ const IDBKeyRange = query.from.IDBKeyRange;
98
+ const transaction = query.from.transaction ?? database.transaction([query.from.table.tableName], "readonly");
99
+ const objectStore = transaction.objectStore(query.from.table.tableName);
100
+ let keyRange = undefined;
101
+ let store;
102
+ if (query.only !== undefined) {
103
+ keyRange = IDBKeyRange.only(query.only);
104
+ } else if (query.lowerBound !== undefined && query.upperBound !== undefined) {
105
+ keyRange = IDBKeyRange.bound(query.lowerBound, query.upperBound, query.excludeLowerBound, query.excludeUpperBound);
106
+ } else if (query.lowerBound !== undefined) {
107
+ keyRange = IDBKeyRange.lowerBound(query.lowerBound, query.excludeLowerBound);
108
+ } else if (query.upperBound !== undefined) {
109
+ keyRange = IDBKeyRange.upperBound(query.upperBound, query.excludeUpperBound);
110
+ }
111
+ if (query.index !== undefined) {
112
+ store = objectStore.index(query.index);
113
+ } else {
114
+ store = objectStore;
115
+ }
116
+ return {
117
+ store,
118
+ keyRange
119
+ };
120
+ };
121
+ const getSelect = /*#__PURE__*/Effect.fnUntraced(function* (query) {
122
+ const keyPath = query.from.table.keyPath;
123
+ const predicate = query.predicate;
124
+ const data = predicate || keyPath === undefined ? yield* Effect.callback(resume => {
125
+ const {
126
+ keyRange,
127
+ store
128
+ } = getReadonlyObjectStore(query);
129
+ const cursorRequest = store.openCursor(keyRange);
130
+ const results = [];
131
+ let count = 0;
132
+ cursorRequest.onerror = () => {
133
+ resume(Effect.fail(new IndexedDbQueryError({
134
+ reason: "TransactionError",
135
+ cause: cursorRequest.error
136
+ })));
137
+ };
138
+ cursorRequest.onsuccess = () => {
139
+ const cursor = cursorRequest.result;
140
+ if (cursor === null) {
141
+ return resume(Effect.succeed(results));
142
+ }
143
+ if (predicate === undefined || predicate(cursor.value)) {
144
+ results.push(keyPath === undefined ? {
145
+ ...cursor.value,
146
+ key: cursor.key
147
+ } : cursor.value);
148
+ count += 1;
149
+ }
150
+ if (query.limitValue === undefined || count < query.limitValue) {
151
+ return cursor.continue();
152
+ }
153
+ resume(Effect.succeed(results));
154
+ };
155
+ }) : yield* Effect.callback(resume => {
156
+ const {
157
+ keyRange,
158
+ store
159
+ } = getReadonlyObjectStore(query);
160
+ const request = store.getAll(keyRange, query.limitValue);
161
+ request.onerror = event => {
162
+ resume(Effect.fail(new IndexedDbQueryError({
163
+ reason: "TransactionError",
164
+ cause: event
165
+ })));
166
+ };
167
+ request.onsuccess = () => {
168
+ resume(Effect.succeed(request.result));
169
+ };
170
+ });
171
+ const tableSchema = query.from.table.arraySchema;
172
+ return yield* Schema.decodeUnknownEffect(tableSchema)(data).pipe(Effect.mapError(error => new IndexedDbQueryError({
173
+ reason: "DecodeError",
174
+ cause: error
175
+ })));
176
+ });
177
+ const getFirst = /*#__PURE__*/Effect.fnUntraced(function* (query) {
178
+ const keyPath = query.select.from.table.keyPath;
179
+ const data = yield* Effect.callback(resume => {
180
+ const {
181
+ keyRange,
182
+ store
183
+ } = getReadonlyObjectStore(query.select);
184
+ if (keyRange !== undefined) {
185
+ const request = store.get(keyRange);
186
+ request.onerror = event => {
187
+ resume(Effect.fail(new IndexedDbQueryError({
188
+ reason: "TransactionError",
189
+ cause: event
190
+ })));
191
+ };
192
+ request.onsuccess = () => {
193
+ resume(Effect.succeed(request.result));
194
+ };
195
+ } else {
196
+ const request = store.openCursor();
197
+ request.onerror = event => {
198
+ resume(Effect.fail(new IndexedDbQueryError({
199
+ reason: "TransactionError",
200
+ cause: event
201
+ })));
202
+ };
203
+ request.onsuccess = () => {
204
+ const value = request.result?.value;
205
+ const key = request.result?.key;
206
+ if (value === undefined) {
207
+ resume(Effect.fail(new IndexedDbQueryError({
208
+ reason: "NotFoundError",
209
+ cause: request.error
210
+ })));
211
+ } else {
212
+ resume(Effect.succeed(keyPath === undefined ? {
213
+ ...value,
214
+ key
215
+ } : value));
216
+ }
217
+ };
218
+ }
219
+ });
220
+ return yield* Schema.decodeUnknownEffect(query.select.from.table.readSchema)(data).pipe(Effect.mapError(error => new IndexedDbQueryError({
221
+ reason: "DecodeError",
222
+ cause: error
223
+ })));
224
+ });
225
+ const applyModify = /*#__PURE__*/Effect.fnUntraced(function* ({
226
+ query,
227
+ value
228
+ }) {
229
+ const autoIncrement = query.from.table.autoIncrement;
230
+ const keyPath = query.from.table.keyPath;
231
+ const table = query.from.table;
232
+ const schema = autoIncrement && value[keyPath] === undefined ? table.autoincrementSchema : table.tableSchema;
233
+ const encodedValue = yield* SchemaParser.makeEffect(autoIncrement && value[keyPath] === undefined ? table.autoincrementSchema : table.tableSchema)(value).pipe(Effect.flatMap(Schema.encodeUnknownEffect(schema)), Effect.mapError(error => new IndexedDbQueryError({
234
+ reason: "EncodeError",
235
+ cause: error
236
+ })));
237
+ return yield* Effect.callback(resume => {
238
+ const database = query.from.database;
239
+ const transaction = query.from.transaction ?? database.transaction([query.from.table.tableName], "readwrite");
240
+ const objectStore = transaction.objectStore(query.from.table.tableName);
241
+ let request;
242
+ if (query.operation === "add") {
243
+ request = objectStore.add(encodedValue, keyPath === undefined ? value["key"] : undefined);
244
+ } else if (query.operation === "put") {
245
+ request = objectStore.put(encodedValue, keyPath === undefined ? value["key"] : undefined);
246
+ } else {
247
+ return resume(Effect.die(new Error("Invalid modify operation")));
248
+ }
249
+ request.onerror = event => {
250
+ resume(Effect.fail(new IndexedDbQueryError({
251
+ reason: "TransactionError",
252
+ cause: event
253
+ })));
254
+ };
255
+ request.onsuccess = () => {
256
+ resume(Effect.succeed(request.result));
257
+ };
258
+ });
259
+ });
260
+ const applyModifyAll = /*#__PURE__*/Effect.fnUntraced(function* ({
261
+ query,
262
+ values
263
+ }) {
264
+ const autoIncrement = query.from.table.autoIncrement;
265
+ const keyPath = query.from.table.keyPath;
266
+ const schema = query.from.table.tableSchema;
267
+ const encodedValues = new Array(values.length);
268
+ const makeValue = SchemaParser.makeEffect(schema);
269
+ const encodeValue = SchemaParser.encodeUnknownEffect(schema);
270
+ const makeValueAutoincrement = SchemaParser.makeEffect(query.from.table.autoincrementSchema);
271
+ const encodeValueAutoincrement = SchemaParser.encodeUnknownEffect(query.from.table.autoincrementSchema);
272
+ for (let i = 0; i < values.length; i++) {
273
+ const value = values[i];
274
+ if (autoIncrement && value[keyPath] === undefined) {
275
+ encodedValues[i] = yield* encodeValueAutoincrement(yield* makeValueAutoincrement(value));
276
+ } else {
277
+ encodedValues[i] = yield* encodeValue(yield* makeValue(value));
278
+ }
279
+ }
280
+ return yield* Effect.callback(resume => {
281
+ const database = query.from.database;
282
+ const transaction = query.from.transaction;
283
+ const objectStore = (transaction ?? database.transaction([query.from.table.tableName], "readwrite")).objectStore(query.from.table.tableName);
284
+ const results = [];
285
+ if (query.operation === "add") {
286
+ for (let i = 0; i < encodedValues.length; i++) {
287
+ const request = objectStore.add(encodedValues[i], keyPath === undefined ? values[i]["key"] : undefined);
288
+ request.onerror = () => {
289
+ resume(Effect.fail(new IndexedDbQueryError({
290
+ reason: "TransactionError",
291
+ cause: request.error
292
+ })));
293
+ };
294
+ request.onsuccess = () => {
295
+ results.push(request.result);
296
+ };
297
+ }
298
+ } else if (query.operation === "put") {
299
+ for (let i = 0; i < encodedValues.length; i++) {
300
+ const request = objectStore.put(encodedValues[i], keyPath === undefined ? values[i]["key"] : undefined);
301
+ request.onerror = () => {
302
+ resume(Effect.fail(new IndexedDbQueryError({
303
+ reason: "TransactionError",
304
+ cause: request.error
305
+ })));
306
+ };
307
+ request.onsuccess = () => {
308
+ results.push(request.result);
309
+ };
310
+ }
311
+ } else {
312
+ return resume(Effect.die(new Error("Invalid modify all operation")));
313
+ }
314
+ objectStore.transaction.onerror = () => {
315
+ resume(Effect.fail(new IndexedDbQueryError({
316
+ reason: "TransactionError",
317
+ cause: objectStore.transaction.error
318
+ })));
319
+ };
320
+ objectStore.transaction.oncomplete = () => {
321
+ resume(Effect.succeed(results));
322
+ };
323
+ });
324
+ }, /*#__PURE__*/Effect.catchIf(SchemaIssue.isIssue, issue => Effect.fail(new IndexedDbQueryError({
325
+ reason: "EncodeError",
326
+ cause: new Schema.SchemaError(issue)
327
+ }))));
328
+ const applyClear = options => Effect.callback(resume => {
329
+ const database = options.database;
330
+ const transaction = options.transaction ?? database.transaction([options.table], "readwrite");
331
+ const objectStore = transaction.objectStore(options.table);
332
+ const request = objectStore.clear();
333
+ request.onerror = event => {
334
+ resume(Effect.fail(new IndexedDbQueryError({
335
+ reason: "TransactionError",
336
+ cause: event
337
+ })));
338
+ };
339
+ request.onsuccess = () => {
340
+ resume(Effect.void);
341
+ };
342
+ });
343
+ const applyClearAll = options => Effect.callback(resume => {
344
+ const database = options.database;
345
+ const tables = database.objectStoreNames;
346
+ const transaction = options.transaction ?? database.transaction([...tables], "readwrite");
347
+ for (let t = 0; t < tables.length; t++) {
348
+ const objectStore = transaction.objectStore(tables[t]);
349
+ const request = objectStore.clear();
350
+ request.onerror = () => {
351
+ resume(Effect.fail(new IndexedDbQueryError({
352
+ reason: "TransactionError",
353
+ cause: request.error
354
+ })));
355
+ };
356
+ }
357
+ transaction.onerror = () => {
358
+ resume(Effect.fail(new IndexedDbQueryError({
359
+ reason: "TransactionError",
360
+ cause: transaction.error
361
+ })));
362
+ };
363
+ transaction.oncomplete = () => {
364
+ resume(Effect.void);
365
+ };
366
+ });
367
+ const getCount = query => Effect.callback(resume => {
368
+ const {
369
+ keyRange,
370
+ store
371
+ } = getReadonlyObjectStore(query);
372
+ const request = store.count(keyRange);
373
+ request.onerror = event => {
374
+ resume(Effect.fail(new IndexedDbQueryError({
375
+ reason: "TransactionError",
376
+ cause: event
377
+ })));
378
+ };
379
+ request.onsuccess = () => {
380
+ resume(Effect.succeed(request.result));
381
+ };
382
+ });
383
+ const FromProto = {
384
+ ...CommonProto,
385
+ select(index) {
386
+ return makeSelect({
387
+ from: this,
388
+ index
389
+ });
390
+ },
391
+ count(index) {
392
+ return makeCount({
393
+ from: this,
394
+ index
395
+ });
396
+ },
397
+ delete(index) {
398
+ return makeDeletePartial({
399
+ from: this,
400
+ index
401
+ });
402
+ },
403
+ insert(value) {
404
+ return makeModify({
405
+ from: this,
406
+ value,
407
+ operation: "add"
408
+ });
409
+ },
410
+ upsert(value) {
411
+ return makeModify({
412
+ from: this,
413
+ value,
414
+ operation: "put"
415
+ });
416
+ },
417
+ insertAll(values) {
418
+ return makeModifyAll({
419
+ from: this,
420
+ values,
421
+ operation: "add"
422
+ });
423
+ },
424
+ upsertAll(values) {
425
+ return makeModifyAll({
426
+ from: this,
427
+ values,
428
+ operation: "put"
429
+ });
430
+ },
431
+ get clear() {
432
+ const self = this;
433
+ return applyClear({
434
+ database: self.database,
435
+ transaction: self.transaction,
436
+ table: self.table.tableName
437
+ });
438
+ }
439
+ };
440
+ const makeFrom = options => {
441
+ const self = Object.create(FromProto);
442
+ self.table = options.table;
443
+ self.database = options.database;
444
+ self.IDBKeyRange = options.IDBKeyRange;
445
+ self.transaction = options.transaction;
446
+ self.reactivity = options.reactivity;
447
+ return self;
448
+ };
449
+ const DeletePartialProto = {
450
+ ...CommonProto,
451
+ limit(limit) {
452
+ return makeDelete({
453
+ delete: this,
454
+ limitValue: limit
455
+ });
456
+ },
457
+ equals(value) {
458
+ return makeDelete({
459
+ delete: this,
460
+ only: value
461
+ });
462
+ },
463
+ gte(value) {
464
+ return makeDelete({
465
+ delete: this,
466
+ lowerBound: value,
467
+ excludeLowerBound: false
468
+ });
469
+ },
470
+ lte(value) {
471
+ return makeDelete({
472
+ delete: this,
473
+ upperBound: value,
474
+ excludeUpperBound: false
475
+ });
476
+ },
477
+ gt(value) {
478
+ return makeDelete({
479
+ delete: this,
480
+ lowerBound: value,
481
+ excludeLowerBound: true
482
+ });
483
+ },
484
+ lt(value) {
485
+ return makeDelete({
486
+ delete: this,
487
+ upperBound: value,
488
+ excludeUpperBound: true
489
+ });
490
+ },
491
+ between(lowerBound, upperBound, queryOptions) {
492
+ return makeDelete({
493
+ delete: this,
494
+ lowerBound,
495
+ upperBound,
496
+ excludeLowerBound: queryOptions?.excludeLowerBound ?? false,
497
+ excludeUpperBound: queryOptions?.excludeUpperBound ?? false
498
+ });
499
+ }
500
+ };
501
+ const makeDeletePartial = options => {
502
+ const self = Object.create(DeletePartialProto);
503
+ self.from = options.from;
504
+ self.index = options.index;
505
+ return self;
506
+ };
507
+ const DeleteProto = {
508
+ ...CommonProto,
509
+ asEffect() {
510
+ return applyDelete(this);
511
+ },
512
+ limit(limit) {
513
+ return makeDelete({
514
+ delete: this.delete,
515
+ only: this.only,
516
+ lowerBound: this.lowerBound,
517
+ upperBound: this.upperBound,
518
+ excludeLowerBound: this.excludeLowerBound ?? false,
519
+ excludeUpperBound: this.excludeUpperBound ?? false,
520
+ limitValue: limit
521
+ });
522
+ },
523
+ equals(value) {
524
+ return makeDelete({
525
+ delete: this.delete,
526
+ only: value,
527
+ limitValue: this.limitValue
528
+ });
529
+ },
530
+ gte(value) {
531
+ return makeDelete({
532
+ delete: this.delete,
533
+ lowerBound: value,
534
+ excludeLowerBound: false,
535
+ limitValue: this.limitValue
536
+ });
537
+ },
538
+ lte(value) {
539
+ return makeDelete({
540
+ delete: this.delete,
541
+ upperBound: value,
542
+ excludeUpperBound: false,
543
+ limitValue: this.limitValue
544
+ });
545
+ },
546
+ gt(value) {
547
+ return makeDelete({
548
+ delete: this.delete,
549
+ lowerBound: value,
550
+ excludeLowerBound: true,
551
+ limitValue: this.limitValue
552
+ });
553
+ },
554
+ lt(value) {
555
+ return makeDelete({
556
+ delete: this.delete,
557
+ upperBound: value,
558
+ excludeUpperBound: true,
559
+ limitValue: this.limitValue
560
+ });
561
+ },
562
+ between(lowerBound, upperBound, queryOptions) {
563
+ return makeDelete({
564
+ delete: this.delete,
565
+ lowerBound,
566
+ upperBound,
567
+ excludeLowerBound: queryOptions?.excludeLowerBound ?? false,
568
+ excludeUpperBound: queryOptions?.excludeUpperBound ?? false,
569
+ limitValue: this.limitValue
570
+ });
571
+ },
572
+ filter(filter) {
573
+ const prev = this.predicate;
574
+ return makeDelete({
575
+ delete: this.delete,
576
+ predicate: prev ? item => prev(item) && filter(item) : filter
577
+ });
578
+ },
579
+ invalidate(keys) {
580
+ return this.delete.from.reactivity.mutation(keys, this.asEffect());
581
+ }
582
+ };
583
+ const makeDelete = options => {
584
+ const self = Object.create(DeleteProto);
585
+ self.delete = options.delete;
586
+ self.limitValue = options.limitValue;
587
+ self.only = options.only;
588
+ self.lowerBound = options.lowerBound;
589
+ self.upperBound = options.upperBound;
590
+ self.excludeLowerBound = options.excludeLowerBound;
591
+ self.excludeUpperBound = options.excludeUpperBound;
592
+ self.predicate = options.predicate;
593
+ return self;
594
+ };
595
+ const CountProto = {
596
+ ...CommonProto,
597
+ asEffect() {
598
+ return getCount(this);
599
+ },
600
+ equals(value) {
601
+ return makeCount({
602
+ from: this.from,
603
+ index: this.index,
604
+ only: value,
605
+ limitValue: this.limitValue
606
+ });
607
+ },
608
+ gte(value) {
609
+ return makeCount({
610
+ from: this.from,
611
+ index: this.index,
612
+ lowerBound: value,
613
+ excludeLowerBound: false,
614
+ limitValue: this.limitValue
615
+ });
616
+ },
617
+ lte(value) {
618
+ return makeCount({
619
+ from: this.from,
620
+ index: this.index,
621
+ upperBound: value,
622
+ excludeUpperBound: false,
623
+ limitValue: this.limitValue
624
+ });
625
+ },
626
+ gt(value) {
627
+ return makeCount({
628
+ from: this.from,
629
+ index: this.index,
630
+ lowerBound: value,
631
+ excludeLowerBound: true,
632
+ limitValue: this.limitValue
633
+ });
634
+ },
635
+ lt(value) {
636
+ return makeCount({
637
+ from: this.from,
638
+ index: this.index,
639
+ upperBound: value,
640
+ excludeUpperBound: true,
641
+ limitValue: this.limitValue
642
+ });
643
+ },
644
+ between(lowerBound, upperBound, queryOptions) {
645
+ return makeCount({
646
+ from: this.from,
647
+ index: this.index,
648
+ lowerBound,
649
+ upperBound,
650
+ excludeLowerBound: queryOptions?.excludeLowerBound ?? false,
651
+ excludeUpperBound: queryOptions?.excludeUpperBound ?? false,
652
+ limitValue: this.limitValue
653
+ });
654
+ }
655
+ };
656
+ const makeCount = options => {
657
+ const self = Object.create(CountProto);
658
+ self.from = options.from;
659
+ self.index = options.index;
660
+ self.only = options.only;
661
+ self.limitValue = options.limitValue;
662
+ self.lowerBound = options.lowerBound;
663
+ self.upperBound = options.upperBound;
664
+ self.excludeLowerBound = options.excludeLowerBound;
665
+ self.excludeUpperBound = options.excludeUpperBound;
666
+ return self;
667
+ };
668
+ const SelectProto = {
669
+ ...CommonProto,
670
+ limit(limit) {
671
+ return makeSelect({
672
+ ...this,
673
+ limitValue: limit
674
+ });
675
+ },
676
+ equals(value) {
677
+ return makeSelect({
678
+ ...this,
679
+ only: value
680
+ });
681
+ },
682
+ gte(value) {
683
+ return makeSelect({
684
+ ...this,
685
+ lowerBound: value,
686
+ excludeLowerBound: false
687
+ });
688
+ },
689
+ lte(value) {
690
+ return makeSelect({
691
+ ...this,
692
+ upperBound: value,
693
+ excludeUpperBound: false
694
+ });
695
+ },
696
+ gt(value) {
697
+ return makeSelect({
698
+ ...this,
699
+ lowerBound: value,
700
+ excludeLowerBound: true
701
+ });
702
+ },
703
+ lt(value) {
704
+ return makeSelect({
705
+ ...this,
706
+ upperBound: value,
707
+ excludeUpperBound: true
708
+ });
709
+ },
710
+ between(lowerBound, upperBound, queryOptions) {
711
+ return makeSelect({
712
+ ...this,
713
+ lowerBound,
714
+ upperBound,
715
+ excludeLowerBound: queryOptions?.excludeLowerBound ?? false,
716
+ excludeUpperBound: queryOptions?.excludeUpperBound ?? false
717
+ });
718
+ },
719
+ first() {
720
+ return makeFirst({
721
+ select: this
722
+ });
723
+ },
724
+ filter(filter) {
725
+ const prev = this.predicate;
726
+ return makeSelect({
727
+ ...this,
728
+ predicate: prev ? item => prev(item) && filter(item) : filter
729
+ });
730
+ },
731
+ asEffect() {
732
+ return getSelect(this);
733
+ },
734
+ reactive(keys) {
735
+ return this.from.reactivity.stream(keys, this.asEffect());
736
+ },
737
+ reactiveQueue(keys) {
738
+ return this.from.reactivity.query(keys, this.asEffect());
739
+ }
740
+ };
741
+ const makeSelect = options => {
742
+ const self = Object.create(SelectProto);
743
+ self.from = options.from;
744
+ self.index = options.index;
745
+ self.only = options.only;
746
+ self.limitValue = options.limitValue;
747
+ self.lowerBound = options.lowerBound;
748
+ self.upperBound = options.upperBound;
749
+ self.excludeLowerBound = options.excludeLowerBound;
750
+ self.excludeUpperBound = options.excludeUpperBound;
751
+ self.predicate = options.predicate;
752
+ return self;
753
+ };
754
+ const FirstProto = {
755
+ ...CommonProto,
756
+ asEffect() {
757
+ return getFirst(this);
758
+ }
759
+ };
760
+ const makeFirst = options => {
761
+ const self = Object.create(FirstProto);
762
+ self.select = options.select;
763
+ return self;
764
+ };
765
+ const ModifyProto = {
766
+ ...CommonProto,
767
+ asEffect() {
768
+ return applyModify({
769
+ query: this,
770
+ value: this.value
771
+ });
772
+ },
773
+ invalidate(keys) {
774
+ return this.from.reactivity.mutation(keys, this.asEffect());
775
+ }
776
+ };
777
+ const makeModify = options => {
778
+ const self = Object.create(ModifyProto);
779
+ self.from = options.from;
780
+ self.value = options.value;
781
+ self.operation = options.operation;
782
+ return self;
783
+ };
784
+ const ModifyAllProto = {
785
+ ...CommonProto,
786
+ asEffect() {
787
+ return applyModifyAll({
788
+ query: this,
789
+ values: this.values
790
+ });
791
+ },
792
+ invalidate(keys) {
793
+ return this.from.reactivity.mutation(keys, this.asEffect());
794
+ }
795
+ };
796
+ const makeModifyAll = options => {
797
+ const self = Object.create(ModifyAllProto);
798
+ self.from = options.from;
799
+ self.values = options.values;
800
+ self.operation = options.operation;
801
+ return self;
802
+ };
803
+ const QueryBuilderProto = {
804
+ ...CommonProto,
805
+ use(f) {
806
+ return Effect.tryPromise({
807
+ try: () => f(this.database),
808
+ catch: error => new IndexedDbQueryError({
809
+ reason: "UnknownError",
810
+ cause: error
811
+ })
812
+ });
813
+ },
814
+ from(table) {
815
+ return makeFrom({
816
+ database: this.database,
817
+ IDBKeyRange: this.IDBKeyRange,
818
+ table: this.tables.get(table),
819
+ transaction: this.IDBTransaction,
820
+ reactivity: this.reactivity
821
+ });
822
+ },
823
+ get clearAll() {
824
+ const self = this;
825
+ return applyClearAll({
826
+ database: self.database,
827
+ transaction: self.IDBTransaction
828
+ });
829
+ },
830
+ transaction: /*#__PURE__*/Effect.fnUntraced(function* (transactionTables, mode, callback, options) {
831
+ const transaction = this.database.transaction(transactionTables, mode, options);
832
+ return yield* callback({
833
+ from: table => makeFrom({
834
+ database: this.database,
835
+ IDBKeyRange: this.IDBKeyRange,
836
+ table: this.tables.get(table),
837
+ transaction,
838
+ reactivity: this.reactivity
839
+ })
840
+ });
841
+ })
842
+ };
843
+ /**
844
+ * @since 4.0.0
845
+ * @category constructors
846
+ */
847
+ export const make = ({
848
+ IDBKeyRange,
849
+ database,
850
+ tables,
851
+ transaction,
852
+ reactivity
853
+ }) => {
854
+ const self = Object.create(QueryBuilderProto);
855
+ self.tables = tables;
856
+ self.database = database;
857
+ self.reactivity = reactivity;
858
+ self.IDBKeyRange = IDBKeyRange;
859
+ self.IDBTransaction = transaction;
860
+ return self;
861
+ };
862
+ //# sourceMappingURL=IndexedDbQueryBuilder.js.map