supabase-typed-query 0.9.4 → 0.9.7

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 CHANGED
@@ -24,6 +24,15 @@ pnpm add supabase-typed-query functype
24
24
  yarn add supabase-typed-query functype
25
25
  ```
26
26
 
27
+ ### Compatibility
28
+
29
+ | Dependency | Version | Notes |
30
+ | ----------------------- | ---------- | ----------------------------------- |
31
+ | `@supabase/supabase-js` | `^2.0.0` | Tested with 2.86.x |
32
+ | `functype` | `>=0.20.1` | Required for error handling |
33
+ | TypeScript | `>=5.0` | Recommended for best type inference |
34
+ | Node.js | `>=18` | Required |
35
+
27
36
  ## Quick Start
28
37
 
29
38
  ### 1. Set up your database types
package/dist/index.js CHANGED
@@ -47,7 +47,7 @@ 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$1 = (fn) => {
50
+ const wrapAsync$2 = (fn) => {
51
51
  return fn();
52
52
  };
53
53
  const QueryBuilder = (client, config) => {
@@ -415,7 +415,7 @@ const QueryBuilder = (client, config) => {
415
415
  * Execute query expecting exactly one result
416
416
  */
417
417
  one: () => {
418
- return wrapAsync$1(async () => {
418
+ return wrapAsync$2(async () => {
419
419
  try {
420
420
  const query2 = buildSupabaseQuery();
421
421
  const { data, error } = await query2.single();
@@ -439,7 +439,7 @@ const QueryBuilder = (client, config) => {
439
439
  * Execute query expecting zero or more results
440
440
  */
441
441
  many: () => {
442
- return wrapAsync$1(async () => {
442
+ return wrapAsync$2(async () => {
443
443
  try {
444
444
  const query2 = buildSupabaseQuery();
445
445
  const { data, error } = await query2;
@@ -460,7 +460,7 @@ const QueryBuilder = (client, config) => {
460
460
  * Execute query expecting first result from potentially multiple
461
461
  */
462
462
  first: () => {
463
- return wrapAsync$1(async () => {
463
+ return wrapAsync$2(async () => {
464
464
  const manyResult = await QueryBuilder(client, config).many();
465
465
  const list = manyResult.orThrow();
466
466
  if (list.isEmpty) {
@@ -504,7 +504,7 @@ const createMappedQuery = (sourceQuery, mapFn) => {
504
504
  return createMappedQuery(filteredQuery, mapFn);
505
505
  },
506
506
  one: () => {
507
- return wrapAsync$1(async () => {
507
+ return wrapAsync$2(async () => {
508
508
  const maybeItemResult = await sourceQuery.one();
509
509
  const maybeItem = maybeItemResult.orThrow();
510
510
  return maybeItem.fold(
@@ -514,14 +514,14 @@ const createMappedQuery = (sourceQuery, mapFn) => {
514
514
  });
515
515
  },
516
516
  many: () => {
517
- return wrapAsync$1(async () => {
517
+ return wrapAsync$2(async () => {
518
518
  const itemsResult = await sourceQuery.many();
519
519
  const items = itemsResult.orThrow();
520
520
  return functype.Ok(items.map(mapFn));
521
521
  });
522
522
  },
523
523
  first: () => {
524
- return wrapAsync$1(async () => {
524
+ return wrapAsync$2(async () => {
525
525
  const maybeItemResult = await sourceQuery.first();
526
526
  const maybeItem = maybeItemResult.orThrow();
527
527
  return maybeItem.fold(
@@ -572,6 +572,73 @@ const isQuery = (obj) => {
572
572
  const isMappedQuery = (obj) => {
573
573
  return typeof obj === "object" && obj !== null && "one" in obj && "many" in obj && "first" in obj && "map" in obj && "filter" in obj;
574
574
  };
575
+ const wrapAsync$1 = (fn) => {
576
+ return fn();
577
+ };
578
+ const rpc = (client, functionName, args, options) => {
579
+ const executeRpc = () => {
580
+ return client.rpc(functionName, args ?? {}, {
581
+ count: options?.count
582
+ });
583
+ };
584
+ const one = () => wrapAsync$1(async () => {
585
+ try {
586
+ const { data, error } = await executeRpc();
587
+ if (error) {
588
+ return functype.Err(toError(error));
589
+ }
590
+ if (data === null || data === void 0) {
591
+ return functype.Ok(functype.Option.none());
592
+ }
593
+ if (Array.isArray(data)) {
594
+ if (data.length === 0) {
595
+ return functype.Ok(functype.Option.none());
596
+ }
597
+ return functype.Ok(functype.Option(data[0]));
598
+ }
599
+ return functype.Ok(functype.Option(data));
600
+ } catch (error) {
601
+ return functype.Err(toError(error));
602
+ }
603
+ });
604
+ const oneOrThrow = async () => {
605
+ const result = await one();
606
+ const option = result.orThrow();
607
+ return option.fold(
608
+ () => {
609
+ throw new Error("RPC call returned no result");
610
+ },
611
+ (value) => value
612
+ );
613
+ };
614
+ const many = () => wrapAsync$1(async () => {
615
+ try {
616
+ const { data, error } = await executeRpc();
617
+ if (error) {
618
+ return functype.Err(toError(error));
619
+ }
620
+ if (data === null || data === void 0) {
621
+ return functype.Ok(functype.List([]));
622
+ }
623
+ if (Array.isArray(data)) {
624
+ return functype.Ok(functype.List(data));
625
+ }
626
+ return functype.Ok(functype.List([data]));
627
+ } catch (error) {
628
+ return functype.Err(toError(error));
629
+ }
630
+ });
631
+ const manyOrThrow = async () => {
632
+ const result = await many();
633
+ return result.orThrow();
634
+ };
635
+ return {
636
+ one,
637
+ oneOrThrow,
638
+ many,
639
+ manyOrThrow
640
+ };
641
+ };
575
642
  const wrapAsync = (fn) => {
576
643
  return fn();
577
644
  };
@@ -1168,6 +1235,7 @@ exports.getEntity = getEntity;
1168
1235
  exports.isMappedQuery = isMappedQuery;
1169
1236
  exports.isQuery = isQuery;
1170
1237
  exports.query = query;
1238
+ exports.rpc = rpc;
1171
1239
  exports.softDeleteEntities = softDeleteEntities;
1172
1240
  exports.softDeleteEntity = softDeleteEntity;
1173
1241
  exports.toError = toError;