supabase-typed-query 0.2.3 → 0.3.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
@@ -664,13 +664,11 @@ const query = (client, table, where = {}, is, wherein, order) => {
664
664
  };
665
665
  function MultiMutationQuery(promise) {
666
666
  const result = Object.assign(promise, {
667
- // Standard MultiExecution interface
668
667
  many: () => promise,
669
668
  manyOrThrow: async () => {
670
669
  const taskResult = await promise;
671
670
  return taskResult.orThrow();
672
671
  },
673
- // Standard ExecutableQuery interface
674
672
  execute: () => promise,
675
673
  executeOrThrow: async () => {
676
674
  const taskResult = await promise;
@@ -681,13 +679,11 @@ function MultiMutationQuery(promise) {
681
679
  }
682
680
  function SingleMutationQuery(promise) {
683
681
  const result = Object.assign(promise, {
684
- // Standard SingleExecution interface
685
682
  one: () => promise.then((outcome) => outcome.map((value) => functype.Option(value))),
686
683
  oneOrThrow: async () => {
687
684
  const taskResult = await promise;
688
685
  return taskResult.orThrow();
689
686
  },
690
- // Standard ExecutableQuery interface
691
687
  execute: () => promise.then((outcome) => outcome.map((value) => functype.Option(value))),
692
688
  executeOrThrow: async () => {
693
689
  const taskResult = await promise;
@@ -697,50 +693,189 @@ function SingleMutationQuery(promise) {
697
693
  });
698
694
  return result;
699
695
  }
700
- const Entity = (client, name, config) => {
701
- const softDeleteMode = config.softDelete ? "exclude" : "include";
702
- function getItem({ id, where, is }) {
703
- const whereWithPartition = config.partitionKey ? { ...config.partitionKey, ...where, id } : { ...where, id };
704
- return createQuery(
696
+ function getSoftDeleteMode(softDelete) {
697
+ return softDelete ? "exclude" : "include";
698
+ }
699
+ function buildWhereWithPartition(partitionField, partitionKey, where) {
700
+ const partitionCondition = { [partitionField]: partitionKey };
701
+ return { ...partitionCondition, ...where };
702
+ }
703
+ function buildWhereWithPartitionAndId(partitionField, partitionKey, id, where) {
704
+ const partitionCondition = { [partitionField]: partitionKey };
705
+ return { ...partitionCondition, ...where, id };
706
+ }
707
+ function createGetItemQuery(client, name, whereConditions, is, softDeleteMode) {
708
+ return createQuery(client, name, whereConditions, is, void 0, void 0, {
709
+ mode: softDeleteMode,
710
+ appliedByDefault: true
711
+ });
712
+ }
713
+ function createGetItemsQuery(client, name, whereConditions, is, wherein, order, softDeleteMode) {
714
+ return createQuery(client, name, whereConditions, is, wherein, order, {
715
+ mode: softDeleteMode,
716
+ appliedByDefault: true
717
+ });
718
+ }
719
+ function createAddItemsMutation(client, name, items) {
720
+ return MultiMutationQuery(addEntities(client, name, items));
721
+ }
722
+ function createUpdateItemMutation(client, name, item, whereConditions, is, wherein) {
723
+ return SingleMutationQuery(
724
+ updateEntity(client, name, item, whereConditions, is, wherein)
725
+ );
726
+ }
727
+ function createUpdateItemsMutation(client, name, items, identity, where, is, wherein) {
728
+ return MultiMutationQuery(
729
+ updateEntities(client, name, items, identity, where, is, wherein)
730
+ );
731
+ }
732
+ function makeGetItem(client, name, softDeleteMode) {
733
+ return function getItem({ id, where, is }) {
734
+ const whereConditions = { ...where, id };
735
+ return createGetItemQuery(client, name, whereConditions, is, softDeleteMode);
736
+ };
737
+ }
738
+ function makeGetItems(client, name, softDeleteMode) {
739
+ return function getItems({ where, is, wherein, order } = {}) {
740
+ return createGetItemsQuery(client, name, where, is, wherein, order, softDeleteMode);
741
+ };
742
+ }
743
+ function makePartitionedGetItem(client, name, partitionField, softDeleteMode) {
744
+ return function getItem(partitionKey, { id, where, is }) {
745
+ const whereConditions = buildWhereWithPartitionAndId(partitionField, partitionKey, id, where);
746
+ return createGetItemQuery(client, name, whereConditions, is, softDeleteMode);
747
+ };
748
+ }
749
+ function makePartitionedGetItems(client, name, partitionField, softDeleteMode) {
750
+ return function getItems(partitionKey, { where, is, wherein, order } = {}) {
751
+ const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
752
+ return createGetItemsQuery(
705
753
  client,
706
754
  name,
707
- whereWithPartition,
755
+ whereConditions,
708
756
  is,
709
- void 0,
710
- void 0,
711
- { mode: softDeleteMode, appliedByDefault: true }
712
- );
713
- }
714
- function getItems({ where, is, wherein, order } = {}) {
715
- const whereWithPartition = config.partitionKey ? { ...config.partitionKey, ...where } : where;
716
- return createQuery(client, name, whereWithPartition, is, wherein, order, {
717
- mode: softDeleteMode,
718
- appliedByDefault: true
719
- });
720
- }
721
- function addItems({ items }) {
722
- return MultiMutationQuery(addEntities(client, name, items));
723
- }
724
- function updateItem({ id, item, where, is, wherein }) {
725
- return SingleMutationQuery(
726
- updateEntity(client, name, item, { ...where, id }, is, wherein)
757
+ wherein,
758
+ order,
759
+ softDeleteMode
727
760
  );
728
- }
729
- function updateItems({
761
+ };
762
+ }
763
+ function makeUpdateItem(client, name) {
764
+ return function updateItem({ id, item, where, is, wherein }) {
765
+ const whereConditions = { ...where, id };
766
+ return createUpdateItemMutation(client, name, item, whereConditions, is, wherein);
767
+ };
768
+ }
769
+ function makePartitionedUpdateItem(client, name, partitionField) {
770
+ return function updateItem(partitionKey, { id, item, where, is, wherein }) {
771
+ const whereConditions = buildWhereWithPartitionAndId(partitionField, partitionKey, id, where);
772
+ return createUpdateItemMutation(client, name, item, whereConditions, is, wherein);
773
+ };
774
+ }
775
+ function makeUpdateItems(client, name) {
776
+ return function updateItems({
730
777
  items,
731
778
  identity = "id",
732
779
  where,
733
780
  is,
734
781
  wherein
735
782
  }) {
736
- return MultiMutationQuery(updateEntities(client, name, items, identity, where, is, wherein));
737
- }
783
+ return createUpdateItemsMutation(client, name, items, identity, where, is, wherein);
784
+ };
785
+ }
786
+ function makePartitionedUpdateItems(client, name, partitionField) {
787
+ return function updateItems(partitionKey, { items, identity = "id", where, is, wherein }) {
788
+ const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
789
+ return createUpdateItemsMutation(
790
+ client,
791
+ name,
792
+ items,
793
+ identity,
794
+ whereConditions,
795
+ is,
796
+ wherein
797
+ );
798
+ };
799
+ }
800
+ function makeAddItems(client, name) {
801
+ return function addItems({ items }) {
802
+ return createAddItemsMutation(client, name, items);
803
+ };
804
+ }
805
+ const Entity = (client, name, config) => {
806
+ const softDeleteMode = getSoftDeleteMode(config.softDelete);
738
807
  return {
739
- getItem,
740
- getItems,
741
- addItems,
742
- updateItem,
743
- updateItems
808
+ /**
809
+ * Retrieve a single item from the table by ID.
810
+ * @param params Query parameters including id, where conditions, and is conditions
811
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
812
+ */
813
+ getItem: makeGetItem(client, name, softDeleteMode),
814
+ /**
815
+ * Get a list of items from the table filtered by conditions.
816
+ * @param params Optional query parameters including where, is, wherein, and order
817
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
818
+ */
819
+ getItems: makeGetItems(client, name, softDeleteMode),
820
+ /**
821
+ * Adds multiple items to the table.
822
+ * @param params Parameters including items array
823
+ * @returns A mutation query with OrThrow methods
824
+ */
825
+ addItems: makeAddItems(client, name),
826
+ /**
827
+ * Update a single item in the table.
828
+ * @param params Update parameters including id, item data, and optional filters
829
+ * @returns A mutation query with OrThrow methods
830
+ */
831
+ updateItem: makeUpdateItem(client, name),
832
+ /**
833
+ * Update multiple items in the table.
834
+ * @param params Update parameters including items array, identity, and optional filters
835
+ * @returns A mutation query with OrThrow methods
836
+ */
837
+ updateItems: makeUpdateItems(client, name)
838
+ };
839
+ };
840
+ const PartitionedEntity = (client, name, config) => {
841
+ const softDeleteMode = getSoftDeleteMode(config.softDelete);
842
+ const { partitionField } = config;
843
+ return {
844
+ /**
845
+ * Retrieve a single item from the table by ID within a partition.
846
+ * @param partitionKey The partition key value (e.g., tenantId)
847
+ * @param params Query parameters including id, where conditions, and is conditions
848
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
849
+ */
850
+ getItem: makePartitionedGetItem(client, name, partitionField, softDeleteMode),
851
+ /**
852
+ * Get a list of items from the table within a partition.
853
+ * @param partitionKey The partition key value (e.g., tenantId)
854
+ * @param params Optional query parameters including where, is, wherein, and order
855
+ * @returns A chainable query that can be executed with .one(), .many(), or .first()
856
+ */
857
+ getItems: makePartitionedGetItems(client, name, partitionField, softDeleteMode),
858
+ /**
859
+ * Adds multiple items to the table.
860
+ * Note: Items should include the partition key value in their data.
861
+ * @param params Parameters including items array
862
+ * @returns A mutation query with OrThrow methods
863
+ */
864
+ addItems: makeAddItems(client, name),
865
+ /**
866
+ * Update a single item in the table within a partition.
867
+ * @param partitionKey The partition key value (e.g., tenantId)
868
+ * @param params Update parameters including id, item data, and optional filters
869
+ * @returns A mutation query with OrThrow methods
870
+ */
871
+ updateItem: makePartitionedUpdateItem(client, name, partitionField),
872
+ /**
873
+ * Update multiple items in the table within a partition.
874
+ * @param partitionKey The partition key value (e.g., tenantId)
875
+ * @param params Update parameters including items array, identity, and optional filters
876
+ * @returns A mutation query with OrThrow methods
877
+ */
878
+ updateItems: makePartitionedUpdateItems(client, name, partitionField)
744
879
  };
745
880
  };
746
881
  Object.defineProperty(exports, "Err", {
@@ -761,6 +896,7 @@ Object.defineProperty(exports, "Option", {
761
896
  });
762
897
  exports.Entity = Entity;
763
898
  exports.MultiMutationQuery = MultiMutationQuery;
899
+ exports.PartitionedEntity = PartitionedEntity;
764
900
  exports.SingleMutationQuery = SingleMutationQuery;
765
901
  exports.SupabaseError = SupabaseError;
766
902
  exports.addEntities = addEntities;