supabase-typed-query 0.7.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/dist/index.js CHANGED
@@ -665,6 +665,82 @@ const upsertEntities = (client, table, entities, identity = "id", where, is, whe
665
665
  return functype.Err(toError(error));
666
666
  }
667
667
  });
668
+ const deleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
669
+ try {
670
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
671
+ const baseQuery = tableQuery.delete().match(where);
672
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
673
+ (query2, [column, values]) => query2.in(column, values)
674
+ ) : baseQuery;
675
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
676
+ (query2, [column, value]) => query2.is(column, value)
677
+ ) : queryWithIn;
678
+ const { data, error } = await queryWithIs.select().single();
679
+ if (error) {
680
+ return functype.Err(toError(error));
681
+ }
682
+ return functype.Ok(data);
683
+ } catch (error) {
684
+ return functype.Err(toError(error));
685
+ }
686
+ });
687
+ const deleteEntities = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
688
+ try {
689
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
690
+ const baseQuery = tableQuery.delete().match(where);
691
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
692
+ (query2, [column, values]) => query2.in(column, values)
693
+ ) : baseQuery;
694
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
695
+ (query2, [column, value]) => query2.is(column, value)
696
+ ) : queryWithIn;
697
+ const { data, error } = await queryWithIs.select();
698
+ if (error) {
699
+ return functype.Err(toError(error));
700
+ }
701
+ return functype.Ok(functype.List(data));
702
+ } catch (error) {
703
+ return functype.Err(toError(error));
704
+ }
705
+ });
706
+ const softDeleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
707
+ try {
708
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
709
+ const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
710
+ const queryWithIn = wherein ? functype.List(Object.entries(wherein)).foldLeft(baseQuery)(
711
+ (query2, [column, values]) => query2.in(column, values)
712
+ ) : baseQuery;
713
+ const queryWithIs = is ? functype.List(Object.entries(is)).foldLeft(queryWithIn)(
714
+ (query2, [column, value]) => query2.is(column, value)
715
+ ) : queryWithIn;
716
+ const { data, error } = await queryWithIs.select().single();
717
+ if (error) {
718
+ return functype.Err(toError(error));
719
+ }
720
+ return functype.Ok(data);
721
+ } catch (error) {
722
+ return functype.Err(toError(error));
723
+ }
724
+ });
725
+ const softDeleteEntities = (client, table, 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({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).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();
736
+ if (error) {
737
+ return functype.Err(toError(error));
738
+ }
739
+ return functype.Ok(functype.List(data));
740
+ } catch (error) {
741
+ return functype.Err(toError(error));
742
+ }
743
+ });
668
744
  const query = (client, table, where = {}, is, wherein, order, schema) => {
669
745
  return createQuery(client, table, where, is, wherein, order, void 0, schema);
670
746
  };
@@ -880,6 +956,68 @@ function makePartitionedUpsertItems(client, name, _partitionField, schema) {
880
956
  return createUpsertItemsMutation(client, name, items, identity, schema);
881
957
  };
882
958
  }
959
+ function createDeleteItemMutation(client, name, whereConditions, is, wherein, softDelete, schema) {
960
+ const operation = softDelete ? softDeleteEntity(client, name, whereConditions, is, wherein, schema) : deleteEntity(client, name, whereConditions, is, wherein, schema);
961
+ return SingleMutationQuery(operation);
962
+ }
963
+ function createDeleteItemsMutation(client, name, whereConditions, is, wherein, softDelete, schema) {
964
+ const operation = softDelete ? softDeleteEntities(client, name, whereConditions, is, wherein, schema) : deleteEntities(client, name, whereConditions, is, wherein, schema);
965
+ return MultiMutationQuery(operation);
966
+ }
967
+ function makeDeleteItem(client, name, softDelete, schema) {
968
+ return function deleteItem({ where, is, wherein }) {
969
+ return createDeleteItemMutation(
970
+ client,
971
+ name,
972
+ where,
973
+ is,
974
+ wherein,
975
+ softDelete,
976
+ schema
977
+ );
978
+ };
979
+ }
980
+ function makeDeleteItems(client, name, softDelete, schema) {
981
+ return function deleteItems({ where, is, wherein }) {
982
+ return createDeleteItemsMutation(
983
+ client,
984
+ name,
985
+ where,
986
+ is,
987
+ wherein,
988
+ softDelete,
989
+ schema
990
+ );
991
+ };
992
+ }
993
+ function makePartitionedDeleteItem(client, name, partitionField, softDelete, schema) {
994
+ return function deleteItem(partitionKey, { where, is, wherein }) {
995
+ const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
996
+ return createDeleteItemMutation(
997
+ client,
998
+ name,
999
+ whereConditions,
1000
+ is,
1001
+ wherein,
1002
+ softDelete,
1003
+ schema
1004
+ );
1005
+ };
1006
+ }
1007
+ function makePartitionedDeleteItems(client, name, partitionField, softDelete, schema) {
1008
+ return function deleteItems(partitionKey, { where, is, wherein }) {
1009
+ const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
1010
+ return createDeleteItemsMutation(
1011
+ client,
1012
+ name,
1013
+ whereConditions,
1014
+ is,
1015
+ wherein,
1016
+ softDelete,
1017
+ schema
1018
+ );
1019
+ };
1020
+ }
883
1021
  const Entity = (client, name, config) => {
884
1022
  const softDeleteMode = getSoftDeleteMode(config.softDelete);
885
1023
  const { schema } = config;
@@ -919,7 +1057,21 @@ const Entity = (client, name, config) => {
919
1057
  * @param params Upsert parameters including items array and identity columns
920
1058
  * @returns A mutation query with OrThrow methods
921
1059
  */
922
- upsertItems: makeUpsertItems(client, name, schema)
1060
+ upsertItems: makeUpsertItems(client, name, schema),
1061
+ /**
1062
+ * Delete a single item from the table.
1063
+ * When softDelete is true, sets the deleted timestamp instead of hard deleting.
1064
+ * @param params Delete parameters including where conditions
1065
+ * @returns A mutation query with OrThrow methods, returns deleted row
1066
+ */
1067
+ deleteItem: makeDeleteItem(client, name, config.softDelete, schema),
1068
+ /**
1069
+ * Delete multiple items from the table.
1070
+ * When softDelete is true, sets the deleted timestamp instead of hard deleting.
1071
+ * @param params Delete parameters including where conditions
1072
+ * @returns A mutation query with OrThrow methods, returns deleted rows
1073
+ */
1074
+ deleteItems: makeDeleteItems(client, name, config.softDelete, schema)
923
1075
  };
924
1076
  };
925
1077
  const PartitionedEntity = (client, name, config) => {
@@ -968,7 +1120,23 @@ const PartitionedEntity = (client, name, config) => {
968
1120
  * @param params Upsert parameters including items array and identity columns
969
1121
  * @returns A mutation query with OrThrow methods
970
1122
  */
971
- upsertItems: makePartitionedUpsertItems(client, name, partitionField, schema)
1123
+ upsertItems: makePartitionedUpsertItems(client, name, partitionField, schema),
1124
+ /**
1125
+ * Delete a single item from the table within a partition.
1126
+ * When softDelete is true, sets the deleted timestamp instead of hard deleting.
1127
+ * @param partitionKey The partition key value (e.g., tenantId)
1128
+ * @param params Delete parameters including where conditions
1129
+ * @returns A mutation query with OrThrow methods, returns deleted row
1130
+ */
1131
+ deleteItem: makePartitionedDeleteItem(client, name, partitionField, config.softDelete, schema),
1132
+ /**
1133
+ * Delete multiple items from the table within a partition.
1134
+ * When softDelete is true, sets the deleted timestamp instead of hard deleting.
1135
+ * @param partitionKey The partition key value (e.g., tenantId)
1136
+ * @param params Delete parameters including where conditions
1137
+ * @returns A mutation query with OrThrow methods, returns deleted rows
1138
+ */
1139
+ deleteItems: makePartitionedDeleteItems(client, name, partitionField, config.softDelete, schema)
972
1140
  };
973
1141
  };
974
1142
  Object.defineProperty(exports, "Err", {