supabase-typed-query 0.13.0 → 1.1.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/index.js CHANGED
@@ -47,9 +47,6 @@ const log = {
47
47
  info: (msg) => process.env.NODE_ENV !== "test" && console.info(`[supabase-typed-query] ${msg}`)
48
48
  };
49
49
  const TABLES_WITHOUT_DELETED = /* @__PURE__ */ new Set([]);
50
- const wrapAsync$2 = (fn) => {
51
- return fn();
52
- };
53
50
  const QueryBuilder = (client, config) => {
54
51
  const buildSupabaseQuery = () => {
55
52
  const { table, conditions, order, limit, offset, schema } = config;
@@ -444,81 +441,52 @@ const QueryBuilder = (client, config) => {
444
441
  * Execute query expecting exactly one result
445
442
  */
446
443
  one: () => {
447
- return wrapAsync$2(async () => {
448
- try {
449
- const query2 = buildSupabaseQuery();
450
- const { data, error } = await query2.single();
451
- if (error) {
452
- log.error(`Error getting ${config.table} item: ${toError(error).toString()}`);
453
- return functype.Err(toError(error));
454
- }
455
- const result = data;
456
- const filteredResult = config.filterFn ? config.filterFn(result) : true;
457
- if (!filteredResult) {
458
- return functype.Ok(functype.Option.none());
459
- }
460
- return functype.Ok(functype.Option(result));
461
- } catch (error) {
462
- log.error(`Error executing single query on ${config.table}: ${toError(error).toString()}`);
463
- return functype.Err(toError(error));
464
- }
465
- });
444
+ return functype.IO.tryAsync(async () => {
445
+ const query2 = buildSupabaseQuery();
446
+ const { data, error } = await query2.single();
447
+ if (error) throw toError(error);
448
+ const result = data;
449
+ const passes = config.filterFn ? config.filterFn(result) : true;
450
+ return passes ? functype.Option(result) : functype.Option.none();
451
+ }, toError).tapError((err) => log.error(`Error getting ${config.table} item: ${err}`));
466
452
  },
467
453
  /**
468
454
  * Execute query expecting zero or more results
469
455
  */
470
456
  many: () => {
471
- return wrapAsync$2(async () => {
472
- try {
473
- const query2 = buildSupabaseQuery();
474
- const { data, error } = await query2;
475
- if (error) {
476
- log.error(`Error getting ${config.table} items: ${toError(error).toString()}`);
477
- return functype.Err(toError(error));
478
- }
479
- const rawResults = data;
480
- const results = config.filterFn ? rawResults.filter(config.filterFn) : rawResults;
481
- return functype.Ok(functype.List(results));
482
- } catch (error) {
483
- log.error(`Error executing multi query on ${config.table}: ${toError(error).toString()}`);
484
- return functype.Err(toError(error));
485
- }
486
- });
457
+ return functype.IO.tryAsync(async () => {
458
+ const query2 = buildSupabaseQuery();
459
+ const { data, error } = await query2;
460
+ if (error) throw toError(error);
461
+ const rawResults = data;
462
+ const results = config.filterFn ? rawResults.filter(config.filterFn) : rawResults;
463
+ return functype.List(results);
464
+ }, toError).tapError((err) => log.error(`Error getting ${config.table} items: ${err}`));
487
465
  },
488
466
  /**
489
467
  * Execute query expecting first result from potentially multiple
490
468
  */
491
469
  first: () => {
492
- return wrapAsync$2(async () => {
493
- const manyResult = await QueryBuilder(client, config).many();
494
- const list = manyResult.orThrow();
495
- if (list.isEmpty) {
496
- return functype.Ok(functype.Option.none());
497
- }
498
- return functype.Ok(functype.Option(list.head));
499
- });
470
+ return QueryBuilder(client, config).many().map((list) => list.isEmpty ? functype.Option.none() : functype.Option(list.head));
500
471
  },
501
472
  /**
502
473
  * Execute query expecting exactly one result, throw if error or not found
503
474
  */
504
475
  oneOrThrow: async () => {
505
- const result = await QueryBuilder(client, config).one();
506
- const option = result.orThrow();
476
+ const option = await QueryBuilder(client, config).one().runOrThrow();
507
477
  return option.orThrow(new Error(`No record found in ${config.table}`));
508
478
  },
509
479
  /**
510
480
  * Execute query expecting zero or more results, throw if error
511
481
  */
512
482
  manyOrThrow: async () => {
513
- const result = await QueryBuilder(client, config).many();
514
- return result.orThrow();
483
+ return QueryBuilder(client, config).many().runOrThrow();
515
484
  },
516
485
  /**
517
486
  * Execute query expecting first result, throw if error or empty
518
487
  */
519
488
  firstOrThrow: async () => {
520
- const result = await QueryBuilder(client, config).first();
521
- const option = result.orThrow();
489
+ const option = await QueryBuilder(client, config).first().runOrThrow();
522
490
  return option.orThrow(new Error(`No records found in ${config.table}`));
523
491
  }
524
492
  };
@@ -533,53 +501,42 @@ const createMappedQuery = (sourceQuery, mapFn) => {
533
501
  return createMappedQuery(filteredQuery, mapFn);
534
502
  },
535
503
  one: () => {
536
- return wrapAsync$2(async () => {
537
- const maybeItemResult = await sourceQuery.one();
538
- const maybeItem = maybeItemResult.orThrow();
539
- return maybeItem.fold(
540
- () => functype.Ok(functype.Option.none()),
541
- (item) => functype.Ok(functype.Option(mapFn(item)))
542
- );
543
- });
504
+ return sourceQuery.one().map(
505
+ (maybeItem) => maybeItem.fold(
506
+ () => functype.Option.none(),
507
+ (item) => functype.Option(mapFn(item))
508
+ )
509
+ );
544
510
  },
545
511
  many: () => {
546
- return wrapAsync$2(async () => {
547
- const itemsResult = await sourceQuery.many();
548
- const items = itemsResult.orThrow();
549
- return functype.Ok(items.map(mapFn));
550
- });
512
+ return sourceQuery.many().map((items) => items.map(mapFn));
551
513
  },
552
514
  first: () => {
553
- return wrapAsync$2(async () => {
554
- const maybeItemResult = await sourceQuery.first();
555
- const maybeItem = maybeItemResult.orThrow();
556
- return maybeItem.fold(
557
- () => functype.Ok(functype.Option.none()),
558
- (item) => functype.Ok(functype.Option(mapFn(item)))
559
- );
560
- });
515
+ return sourceQuery.first().map(
516
+ (maybeItem) => maybeItem.fold(
517
+ () => functype.Option.none(),
518
+ (item) => functype.Option(mapFn(item))
519
+ )
520
+ );
561
521
  },
562
522
  /**
563
523
  * Execute mapped query expecting exactly one result, throw if error or not found
564
524
  */
565
525
  oneOrThrow: async () => {
566
- const result = await createMappedQuery(sourceQuery, mapFn).one();
567
- const option = result.orThrow();
526
+ const option = await createMappedQuery(sourceQuery, mapFn).one().runOrThrow();
568
527
  return option.orThrow(new Error(`No record found`));
569
528
  },
570
529
  /**
571
530
  * Execute mapped query expecting zero or more results, throw if error
572
531
  */
573
532
  manyOrThrow: async () => {
574
- const result = await createMappedQuery(sourceQuery, mapFn).many();
575
- return result.orThrow();
533
+ return createMappedQuery(sourceQuery, mapFn).many().runOrThrow();
576
534
  },
577
535
  /**
578
536
  * Execute mapped query expecting first result, throw if error or empty
579
537
  */
580
538
  firstOrThrow: async () => {
581
- const result = await createMappedQuery(sourceQuery, mapFn).first();
582
- const option = result.orThrow();
539
+ const option = await createMappedQuery(sourceQuery, mapFn).first().runOrThrow();
583
540
  return option.orThrow(new Error(`No records found`));
584
541
  }
585
542
  };
@@ -601,38 +558,28 @@ const isQuery = (obj) => {
601
558
  const isMappedQuery = (obj) => {
602
559
  return typeof obj === "object" && obj !== null && "one" in obj && "many" in obj && "first" in obj && "map" in obj && "filter" in obj;
603
560
  };
604
- const wrapAsync$1 = (fn) => {
605
- return fn();
606
- };
607
561
  const rpc = (client, functionName, args, options) => {
608
562
  const executeRpc = () => {
609
563
  return client.rpc(functionName, args ?? {}, {
610
564
  count: options?.count
611
565
  });
612
566
  };
613
- const one = () => wrapAsync$1(async () => {
614
- try {
615
- const { data, error } = await executeRpc();
616
- if (error) {
617
- return functype.Err(toError(error));
618
- }
619
- if (data === null || data === void 0) {
620
- return functype.Ok(functype.Option.none());
621
- }
622
- if (Array.isArray(data)) {
623
- if (data.length === 0) {
624
- return functype.Ok(functype.Option.none());
625
- }
626
- return functype.Ok(functype.Option(data[0]));
567
+ const one = () => functype.IO.tryAsync(async () => {
568
+ const { data, error } = await executeRpc();
569
+ if (error) throw toError(error);
570
+ if (data === null || data === void 0) {
571
+ return functype.Option.none();
572
+ }
573
+ if (Array.isArray(data)) {
574
+ if (data.length === 0) {
575
+ return functype.Option.none();
627
576
  }
628
- return functype.Ok(functype.Option(data));
629
- } catch (error) {
630
- return functype.Err(toError(error));
577
+ return functype.Option(data[0]);
631
578
  }
632
- });
579
+ return functype.Option(data);
580
+ }, toError);
633
581
  const oneOrThrow = async () => {
634
- const result = await one();
635
- const option = result.orThrow();
582
+ const option = await one().runOrThrow();
636
583
  return option.fold(
637
584
  () => {
638
585
  throw new Error("RPC call returned no result");
@@ -640,26 +587,19 @@ const rpc = (client, functionName, args, options) => {
640
587
  (value) => value
641
588
  );
642
589
  };
643
- const many = () => wrapAsync$1(async () => {
644
- try {
645
- const { data, error } = await executeRpc();
646
- if (error) {
647
- return functype.Err(toError(error));
648
- }
649
- if (data === null || data === void 0) {
650
- return functype.Ok(functype.List([]));
651
- }
652
- if (Array.isArray(data)) {
653
- return functype.Ok(functype.List(data));
654
- }
655
- return functype.Ok(functype.List([data]));
656
- } catch (error) {
657
- return functype.Err(toError(error));
590
+ const many = () => functype.IO.tryAsync(async () => {
591
+ const { data, error } = await executeRpc();
592
+ if (error) throw toError(error);
593
+ if (data === null || data === void 0) {
594
+ return functype.List([]);
595
+ }
596
+ if (Array.isArray(data)) {
597
+ return functype.List(data);
658
598
  }
659
- });
599
+ return functype.List([data]);
600
+ }, toError);
660
601
  const manyOrThrow = async () => {
661
- const result = await many();
662
- return result.orThrow();
602
+ return many().runOrThrow();
663
603
  };
664
604
  return {
665
605
  one,
@@ -668,208 +608,145 @@ const rpc = (client, functionName, args, options) => {
668
608
  manyOrThrow
669
609
  };
670
610
  };
671
- const wrapAsync = (fn) => {
672
- return fn();
673
- };
674
- const getEntity = (client, table, where, is, schema) => wrapAsync(async () => {
675
- try {
676
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
677
- const baseQuery = tableQuery.select("*").match(where);
678
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(baseQuery)(
679
- (query2, [column, value]) => query2.is(column, value)
680
- ) : baseQuery;
681
- const { data, error } = await queryWithIs.single();
682
- if (error) {
683
- return functype.Err(toError(error));
684
- }
685
- return functype.Ok(data);
686
- } catch (error) {
687
- return functype.Err(toError(error));
688
- }
689
- });
611
+ const getEntity = (client, table, where, is, schema) => functype.IO.tryAsync(async () => {
612
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
613
+ const baseQuery = tableQuery.select("*").match(where);
614
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(baseQuery)(
615
+ (query2, [column, value]) => query2.is(column, value)
616
+ ) : baseQuery;
617
+ const { data, error } = await queryWithIs.single();
618
+ if (error) throw toError(error);
619
+ return data;
620
+ }, toError);
690
621
  const getEntities = (client, table, where = {}, is, wherein, order = [
691
622
  "id",
692
623
  { ascending: true }
693
- ], schema) => wrapAsync(async () => {
694
- try {
695
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
696
- const baseQuery = tableQuery.select("*").match(where);
697
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
698
- (query2, [column, values]) => query2.in(column, values)
699
- ) : baseQuery;
700
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
701
- (query2, [column, value]) => query2.is(column, value)
702
- ) : queryWithIn;
703
- const queryOrderBy = queryWithIs.order(order[0], order[1]);
704
- const { data, error } = await queryOrderBy;
705
- if (error) {
706
- return functype.Err(toError(error));
707
- }
708
- return functype.Ok(functype.List(data));
709
- } catch (error) {
710
- return functype.Err(toError(error));
711
- }
712
- });
713
- const addEntities = (client, table, entities, schema) => wrapAsync(async () => {
714
- try {
715
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
716
- const { data, error } = await tableQuery.insert(entities).select();
717
- if (error) {
718
- return functype.Err(toError(error));
719
- }
720
- return functype.Ok(functype.List(data));
721
- } catch (error) {
722
- return functype.Err(toError(error));
723
- }
724
- });
725
- const updateEntity = (client, table, entities, where, is, wherein, schema) => wrapAsync(async () => {
726
- try {
727
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
728
- const baseQuery = tableQuery.update(entities).match(where);
729
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
730
- (query2, [column, values]) => query2.in(column, values)
731
- ) : baseQuery;
732
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
733
- (query2, [column, value]) => query2.is(column, value)
734
- ) : queryWithIn;
735
- const { data, error } = await queryWithIs.select().single();
736
- if (error) {
737
- return functype.Err(toError(error));
738
- }
739
- return functype.Ok(data);
740
- } catch (error) {
741
- return functype.Err(toError(error));
742
- }
743
- });
744
- const upsertEntities = (client, table, entities, identity = "id", where, is, wherein, schema) => wrapAsync(async () => {
745
- try {
746
- const onConflict = Array.isArray(identity) ? identity.join(",") : identity;
747
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
748
- const baseQuery = tableQuery.upsert(entities, { onConflict }).match(where ?? {});
749
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
750
- (query2, [column, values]) => query2.in(column, values)
751
- ) : baseQuery;
752
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
753
- (query2, [column, value]) => query2.is(column, value)
754
- ) : queryWithIn;
755
- const { data, error } = await queryWithIs.select();
756
- if (error) {
757
- return functype.Err(toError(error));
758
- }
759
- return functype.Ok(functype.List(data));
760
- } catch (error) {
761
- return functype.Err(toError(error));
762
- }
763
- });
764
- const deleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
765
- try {
766
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
767
- const baseQuery = tableQuery.delete().match(where);
768
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
769
- (query2, [column, values]) => query2.in(column, values)
770
- ) : baseQuery;
771
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
772
- (query2, [column, value]) => query2.is(column, value)
773
- ) : queryWithIn;
774
- const { data, error } = await queryWithIs.select().single();
775
- if (error) {
776
- return functype.Err(toError(error));
777
- }
778
- return functype.Ok(data);
779
- } catch (error) {
780
- return functype.Err(toError(error));
781
- }
782
- });
783
- const deleteEntities = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
784
- try {
785
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
786
- const baseQuery = tableQuery.delete().match(where);
787
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
788
- (query2, [column, values]) => query2.in(column, values)
789
- ) : baseQuery;
790
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
791
- (query2, [column, value]) => query2.is(column, value)
792
- ) : queryWithIn;
793
- const { data, error } = await queryWithIs.select();
794
- if (error) {
795
- return functype.Err(toError(error));
796
- }
797
- return functype.Ok(functype.List(data));
798
- } catch (error) {
799
- return functype.Err(toError(error));
800
- }
801
- });
802
- const softDeleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
803
- try {
804
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
805
- const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
806
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
807
- (query2, [column, values]) => query2.in(column, values)
808
- ) : baseQuery;
809
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
810
- (query2, [column, value]) => query2.is(column, value)
811
- ) : queryWithIn;
812
- const { data, error } = await queryWithIs.select().single();
813
- if (error) {
814
- return functype.Err(toError(error));
815
- }
816
- return functype.Ok(data);
817
- } catch (error) {
818
- return functype.Err(toError(error));
819
- }
820
- });
821
- const softDeleteEntities = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
822
- try {
823
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
824
- const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
825
- const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
826
- (query2, [column, values]) => query2.in(column, values)
827
- ) : baseQuery;
828
- const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
829
- (query2, [column, value]) => query2.is(column, value)
830
- ) : queryWithIn;
831
- const { data, error } = await queryWithIs.select();
832
- if (error) {
833
- return functype.Err(toError(error));
834
- }
835
- return functype.Ok(functype.List(data));
836
- } catch (error) {
837
- return functype.Err(toError(error));
838
- }
839
- });
624
+ ], schema) => functype.IO.tryAsync(async () => {
625
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
626
+ const baseQuery = tableQuery.select("*").match(where);
627
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
628
+ (query2, [column, values]) => query2.in(column, values)
629
+ ) : baseQuery;
630
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
631
+ (query2, [column, value]) => query2.is(column, value)
632
+ ) : queryWithIn;
633
+ const queryOrderBy = queryWithIs.order(order[0], order[1]);
634
+ const { data, error } = await queryOrderBy;
635
+ if (error) throw toError(error);
636
+ return functype.List(data);
637
+ }, toError);
638
+ const addEntities = (client, table, entities, schema) => functype.IO.tryAsync(async () => {
639
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
640
+ const { data, error } = await tableQuery.insert(entities).select();
641
+ if (error) throw toError(error);
642
+ return functype.List(data);
643
+ }, toError);
644
+ const updateEntity = (client, table, entities, where, is, wherein, schema) => functype.IO.tryAsync(async () => {
645
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
646
+ const baseQuery = tableQuery.update(entities).match(where);
647
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
648
+ (query2, [column, values]) => query2.in(column, values)
649
+ ) : baseQuery;
650
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
651
+ (query2, [column, value]) => query2.is(column, value)
652
+ ) : queryWithIn;
653
+ const { data, error } = await queryWithIs.select().single();
654
+ if (error) throw toError(error);
655
+ return data;
656
+ }, toError);
657
+ const upsertEntities = (client, table, entities, identity = "id", where, is, wherein, schema) => functype.IO.tryAsync(async () => {
658
+ const onConflict = Array.isArray(identity) ? identity.join(",") : identity;
659
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
660
+ const baseQuery = tableQuery.upsert(entities, { onConflict }).match(where ?? {});
661
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
662
+ (query2, [column, values]) => query2.in(column, values)
663
+ ) : baseQuery;
664
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
665
+ (query2, [column, value]) => query2.is(column, value)
666
+ ) : queryWithIn;
667
+ const { data, error } = await queryWithIs.select();
668
+ if (error) throw toError(error);
669
+ return functype.List(data);
670
+ }, toError);
671
+ const deleteEntity = (client, table, where, is, wherein, schema) => functype.IO.tryAsync(async () => {
672
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
673
+ const baseQuery = tableQuery.delete().match(where);
674
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
675
+ (query2, [column, values]) => query2.in(column, values)
676
+ ) : baseQuery;
677
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
678
+ (query2, [column, value]) => query2.is(column, value)
679
+ ) : queryWithIn;
680
+ const { data, error } = await queryWithIs.select().single();
681
+ if (error) throw toError(error);
682
+ return data;
683
+ }, toError);
684
+ const deleteEntities = (client, table, where, is, wherein, schema) => functype.IO.tryAsync(async () => {
685
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
686
+ const baseQuery = tableQuery.delete().match(where);
687
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
688
+ (query2, [column, values]) => query2.in(column, values)
689
+ ) : baseQuery;
690
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
691
+ (query2, [column, value]) => query2.is(column, value)
692
+ ) : queryWithIn;
693
+ const { data, error } = await queryWithIs.select();
694
+ if (error) throw toError(error);
695
+ return functype.List(data);
696
+ }, toError);
697
+ const softDeleteEntity = (client, table, where, is, wherein, schema) => functype.IO.tryAsync(async () => {
698
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
699
+ const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
700
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
701
+ (query2, [column, values]) => query2.in(column, values)
702
+ ) : baseQuery;
703
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
704
+ (query2, [column, value]) => query2.is(column, value)
705
+ ) : queryWithIn;
706
+ const { data, error } = await queryWithIs.select().single();
707
+ if (error) throw toError(error);
708
+ return data;
709
+ }, toError);
710
+ const softDeleteEntities = (client, table, where, is, wherein, schema) => functype.IO.tryAsync(async () => {
711
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
712
+ const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
713
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
714
+ (query2, [column, values]) => query2.in(column, values)
715
+ ) : baseQuery;
716
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
717
+ (query2, [column, value]) => query2.is(column, value)
718
+ ) : queryWithIn;
719
+ const { data, error } = await queryWithIs.select();
720
+ if (error) throw toError(error);
721
+ return functype.List(data);
722
+ }, toError);
840
723
  const query = (client, table, where = {}, is, wherein, order, schema) => {
841
724
  return createQuery(client, table, where, is, wherein, order, void 0, schema);
842
725
  };
843
- function MultiMutationQuery(promise) {
844
- const result = Object.assign(promise, {
845
- many: () => promise,
726
+ function MultiMutationQuery(task) {
727
+ return {
728
+ many: () => task,
846
729
  manyOrThrow: async () => {
847
- const taskResult = await promise;
848
- return taskResult.orThrow();
730
+ return task.runOrThrow();
849
731
  },
850
- execute: () => promise,
732
+ execute: () => task,
851
733
  executeOrThrow: async () => {
852
- const taskResult = await promise;
853
- return taskResult.orThrow();
734
+ return task.runOrThrow();
854
735
  }
855
- });
856
- return result;
736
+ };
857
737
  }
858
- function SingleMutationQuery(promise) {
859
- const result = Object.assign(promise, {
860
- one: () => promise.then((outcome) => outcome.map((value) => functype.Option(value))),
738
+ function SingleMutationQuery(task) {
739
+ return {
740
+ one: () => task.map((value) => functype.Option(value)),
861
741
  oneOrThrow: async () => {
862
- const taskResult = await promise;
863
- return taskResult.orThrow();
742
+ return task.runOrThrow();
864
743
  },
865
- execute: () => promise.then((outcome) => outcome.map((value) => functype.Option(value))),
744
+ execute: () => task.map((value) => functype.Option(value)),
866
745
  executeOrThrow: async () => {
867
- const taskResult = await promise;
868
- const value = taskResult.orThrow();
746
+ const value = await task.runOrThrow();
869
747
  return functype.Option(value);
870
748
  }
871
- });
872
- return result;
749
+ };
873
750
  }
874
751
  function getSoftDeleteMode(softDelete) {
875
752
  return softDelete ? "exclude" : "include";
@@ -1187,6 +1064,97 @@ function makePartitionedDeleteItems(client, name, partitionField, softDelete, sc
1187
1064
  );
1188
1065
  };
1189
1066
  }
1067
+ function makeViewGetItem(client, name, schema) {
1068
+ return function getItem({ id, where, is }) {
1069
+ const whereConditions = { ...where, id };
1070
+ return createViewGetItemQuery(client, name, whereConditions, is, schema);
1071
+ };
1072
+ }
1073
+ function makeViewGetItems(client, name, schema) {
1074
+ return function getItems({
1075
+ where,
1076
+ is,
1077
+ wherein,
1078
+ order,
1079
+ gte,
1080
+ gt,
1081
+ lte,
1082
+ lt,
1083
+ neq,
1084
+ like,
1085
+ ilike,
1086
+ not
1087
+ } = {}) {
1088
+ return createViewGetItemsQuery(
1089
+ client,
1090
+ name,
1091
+ where,
1092
+ is,
1093
+ wherein,
1094
+ order,
1095
+ schema,
1096
+ { gte, gt, lte, lt, neq, like, ilike },
1097
+ not
1098
+ );
1099
+ };
1100
+ }
1101
+ function makePartitionedViewGetItem(client, name, partitionField, schema) {
1102
+ return function getItem(partitionKey, { id, where, is }) {
1103
+ const whereConditions = buildWhereWithPartitionAndId(partitionField, partitionKey, id, where);
1104
+ return createViewGetItemQuery(
1105
+ client,
1106
+ name,
1107
+ whereConditions,
1108
+ is,
1109
+ schema
1110
+ );
1111
+ };
1112
+ }
1113
+ function makePartitionedViewGetItems(client, name, partitionField, schema) {
1114
+ return function getItems(partitionKey, { where, is, wherein, order, gte, gt, lte, lt, neq, like, ilike, not } = {}) {
1115
+ const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
1116
+ return createViewGetItemsQuery(
1117
+ client,
1118
+ name,
1119
+ whereConditions,
1120
+ is,
1121
+ wherein,
1122
+ order,
1123
+ schema,
1124
+ { gte, gt, lte, lt, neq, like, ilike },
1125
+ not
1126
+ );
1127
+ };
1128
+ }
1129
+ function createViewGetItemQuery(client, name, where, is, schema) {
1130
+ const viewAsTable = name;
1131
+ const clientAny = client;
1132
+ const query2 = createQuery(clientAny, viewAsTable, where, is, void 0, void 0, void 0, schema);
1133
+ return query2;
1134
+ }
1135
+ function createViewGetItemsQuery(client, name, where, is, wherein, order, schema, comparison, not) {
1136
+ const viewAsTable = name;
1137
+ const clientAny = client;
1138
+ const query2 = createQuery(
1139
+ clientAny,
1140
+ viewAsTable,
1141
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1142
+ where,
1143
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1144
+ is,
1145
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1146
+ wherein,
1147
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1148
+ order,
1149
+ void 0,
1150
+ schema,
1151
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1152
+ comparison,
1153
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
1154
+ not
1155
+ );
1156
+ return query2;
1157
+ }
1190
1158
  const Entity = (client, name, config) => {
1191
1159
  const softDeleteMode = getSoftDeleteMode(config.softDelete);
1192
1160
  const { schema } = config;
@@ -1308,18 +1276,50 @@ const PartitionedEntity = (client, name, config) => {
1308
1276
  deleteItems: makePartitionedDeleteItems(client, name, partitionField, config.softDelete, schema)
1309
1277
  };
1310
1278
  };
1311
- Object.defineProperty(exports, "Err", {
1279
+ const ViewEntity = (client, name, config) => {
1280
+ const schema = config?.schema;
1281
+ return {
1282
+ /**
1283
+ * Retrieve a single item from the view by ID.
1284
+ * @param params Query parameters including id, where conditions, and is conditions
1285
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
1286
+ */
1287
+ getItem: makeViewGetItem(client, name, schema),
1288
+ /**
1289
+ * Get a list of items from the view filtered by conditions.
1290
+ * @param params Optional query parameters including where, is, wherein, and order
1291
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
1292
+ */
1293
+ getItems: makeViewGetItems(client, name, schema)
1294
+ };
1295
+ };
1296
+ const PartitionedViewEntity = (client, name, config) => {
1297
+ const { partitionField, schema } = config;
1298
+ return {
1299
+ /**
1300
+ * Retrieve a single item from the view by ID within a partition.
1301
+ * @param partitionKey The partition key value (e.g., tenantId)
1302
+ * @param params Query parameters including id, where conditions, and is conditions
1303
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
1304
+ */
1305
+ getItem: makePartitionedViewGetItem(client, name, partitionField, schema),
1306
+ /**
1307
+ * Get a list of items from the view within a partition.
1308
+ * @param partitionKey The partition key value (e.g., tenantId)
1309
+ * @param params Optional query parameters including where, is, wherein, and order
1310
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
1311
+ */
1312
+ getItems: makePartitionedViewGetItems(client, name, partitionField, schema)
1313
+ };
1314
+ };
1315
+ Object.defineProperty(exports, "IO", {
1312
1316
  enumerable: true,
1313
- get: () => functype.Err
1317
+ get: () => functype.IO
1314
1318
  });
1315
1319
  Object.defineProperty(exports, "List", {
1316
1320
  enumerable: true,
1317
1321
  get: () => functype.List
1318
1322
  });
1319
- Object.defineProperty(exports, "Ok", {
1320
- enumerable: true,
1321
- get: () => functype.Ok
1322
- });
1323
1323
  Object.defineProperty(exports, "Option", {
1324
1324
  enumerable: true,
1325
1325
  get: () => functype.Option
@@ -1327,8 +1327,10 @@ Object.defineProperty(exports, "Option", {
1327
1327
  exports.Entity = Entity;
1328
1328
  exports.MultiMutationQuery = MultiMutationQuery;
1329
1329
  exports.PartitionedEntity = PartitionedEntity;
1330
+ exports.PartitionedViewEntity = PartitionedViewEntity;
1330
1331
  exports.SingleMutationQuery = SingleMutationQuery;
1331
1332
  exports.SupabaseError = SupabaseError;
1333
+ exports.ViewEntity = ViewEntity;
1332
1334
  exports.addEntities = addEntities;
1333
1335
  exports.deleteEntities = deleteEntities;
1334
1336
  exports.deleteEntity = deleteEntity;