supabase-typed-query 0.12.0 → 1.0.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.mjs CHANGED
@@ -1,5 +1,5 @@
1
- import { Ok, Option, Err, List } from "functype";
2
- import { Err as Err2, List as List2, Ok as Ok2, Option as Option2 } from "functype";
1
+ import { Option, IO, List } from "functype";
2
+ import { IO as IO2, List as List2, Option as Option2 } from "functype";
3
3
  class SupabaseError extends Error {
4
4
  code;
5
5
  details;
@@ -46,9 +46,6 @@ const log = {
46
46
  info: (msg) => process.env.NODE_ENV !== "test" && console.info(`[supabase-typed-query] ${msg}`)
47
47
  };
48
48
  const TABLES_WITHOUT_DELETED = /* @__PURE__ */ new Set([]);
49
- const wrapAsync$2 = (fn) => {
50
- return fn();
51
- };
52
49
  const QueryBuilder = (client, config) => {
53
50
  const buildSupabaseQuery = () => {
54
51
  const { table, conditions, order, limit, offset, schema } = config;
@@ -68,7 +65,7 @@ const QueryBuilder = (client, config) => {
68
65
  return finalQuery;
69
66
  };
70
67
  const applyCondition = (query2, condition) => {
71
- const { where, is, wherein, gt, gte, lt, lte, neq, like, ilike } = condition;
68
+ const { where, is, wherein, gt, gte, lt, lte, neq, like, ilike, not } = condition;
72
69
  const processedWhere = {};
73
70
  const extractedOperators = {};
74
71
  if (where) {
@@ -182,10 +179,18 @@ const QueryBuilder = (client, config) => {
182
179
  const queryWithGte = Object.keys(mergedGte).length > 0 ? Object.entries(mergedGte).reduce((q, [key, value]) => q.gte(key, value), queryWithGt) : queryWithGt;
183
180
  const queryWithLt = Object.keys(mergedLt).length > 0 ? Object.entries(mergedLt).reduce((q, [key, value]) => q.lt(key, value), queryWithGte) : queryWithGte;
184
181
  const queryWithLte = Object.keys(mergedLte).length > 0 ? Object.entries(mergedLte).reduce((q, [key, value]) => q.lte(key, value), queryWithLt) : queryWithLt;
185
- const queryWithNeq = Object.keys(mergedNeq).length > 0 ? Object.entries(mergedNeq).reduce((q, [key, value]) => q.neq(key, value), queryWithLte) : queryWithLte;
182
+ const queryWithNeq = Object.keys(mergedNeq).length > 0 ? Object.entries(mergedNeq).reduce((q, [key, value]) => {
183
+ if (value === null) {
184
+ log.warn(`neq: null is deprecated. Use not: { is: { ${key}: null } } for IS NOT NULL`);
185
+ return q.not(key, "is", null);
186
+ }
187
+ return q.neq(key, value);
188
+ }, queryWithLte) : queryWithLte;
186
189
  const queryWithLike = Object.keys(mergedLike).length > 0 ? Object.entries(mergedLike).reduce((q, [key, pattern]) => q.like(key, pattern), queryWithNeq) : queryWithNeq;
187
190
  const queryWithIlike = Object.keys(mergedIlike).length > 0 ? Object.entries(mergedIlike).reduce((q, [key, pattern]) => q.ilike(key, pattern), queryWithLike) : queryWithLike;
188
- return queryWithIlike;
191
+ const queryWithNotIs = not?.is ? Object.entries(not.is).reduce((q, [key, value]) => q.not(key, "is", value), queryWithIlike) : queryWithIlike;
192
+ const queryWithNot = not?.in ? Object.entries(not.in).reduce((q, [key, values]) => q.not(key, "in", values), queryWithNotIs) : queryWithNotIs;
193
+ return queryWithNot;
189
194
  };
190
195
  const applyOrConditions = (query2, conditions) => {
191
196
  const selectQuery = query2.select("*");
@@ -222,7 +227,8 @@ const QueryBuilder = (client, config) => {
222
227
  return {
223
228
  where: newWhere,
224
229
  is: condition.is,
225
- wherein: condition.wherein
230
+ wherein: condition.wherein,
231
+ not: condition.not
226
232
  };
227
233
  })
228
234
  );
@@ -286,6 +292,7 @@ const QueryBuilder = (client, config) => {
286
292
  if (condition.neq) {
287
293
  Object.entries(condition.neq).forEach(([key, value]) => {
288
294
  if (value === null) {
295
+ log.warn(`neq: null is deprecated. Use not: { is: { ${key}: null } } for IS NOT NULL`);
289
296
  parts.push(`${key}.not.is.null`);
290
297
  } else {
291
298
  parts.push(`${key}.neq."${value}"`);
@@ -302,6 +309,25 @@ const QueryBuilder = (client, config) => {
302
309
  parts.push(`${key}.ilike."${pattern}"`);
303
310
  });
304
311
  }
312
+ if (condition.not) {
313
+ if (condition.not.is) {
314
+ Object.entries(condition.not.is).forEach(([key, value]) => {
315
+ if (value === null) {
316
+ parts.push(`${key}.not.is.null`);
317
+ } else {
318
+ parts.push(`${key}.not.is.${value}`);
319
+ }
320
+ });
321
+ }
322
+ if (condition.not.in) {
323
+ Object.entries(condition.not.in).forEach(([key, values]) => {
324
+ if (values && Array.isArray(values) && values.length > 0) {
325
+ const valueList = values.map((v) => `"${v}"`).join(",");
326
+ parts.push(`${key}.not.in.(${valueList})`);
327
+ }
328
+ });
329
+ }
330
+ }
305
331
  return parts.join(",");
306
332
  }).filter((condition) => condition.length > 0);
307
333
  const finalQuery = orConditions.length > 0 ? queryWithCommon.or(orConditions.join(",")) : queryWithCommon;
@@ -414,81 +440,52 @@ const QueryBuilder = (client, config) => {
414
440
  * Execute query expecting exactly one result
415
441
  */
416
442
  one: () => {
417
- return wrapAsync$2(async () => {
418
- try {
419
- const query2 = buildSupabaseQuery();
420
- const { data, error } = await query2.single();
421
- if (error) {
422
- log.error(`Error getting ${config.table} item: ${toError(error).toString()}`);
423
- return Err(toError(error));
424
- }
425
- const result = data;
426
- const filteredResult = config.filterFn ? config.filterFn(result) : true;
427
- if (!filteredResult) {
428
- return Ok(Option.none());
429
- }
430
- return Ok(Option(result));
431
- } catch (error) {
432
- log.error(`Error executing single query on ${config.table}: ${toError(error).toString()}`);
433
- return Err(toError(error));
434
- }
435
- });
443
+ return IO.tryAsync(async () => {
444
+ const query2 = buildSupabaseQuery();
445
+ const { data, error } = await query2.single();
446
+ if (error) throw toError(error);
447
+ const result = data;
448
+ const passes = config.filterFn ? config.filterFn(result) : true;
449
+ return passes ? Option(result) : Option.none();
450
+ }, toError).tapError((err) => log.error(`Error getting ${config.table} item: ${err}`));
436
451
  },
437
452
  /**
438
453
  * Execute query expecting zero or more results
439
454
  */
440
455
  many: () => {
441
- return wrapAsync$2(async () => {
442
- try {
443
- const query2 = buildSupabaseQuery();
444
- const { data, error } = await query2;
445
- if (error) {
446
- log.error(`Error getting ${config.table} items: ${toError(error).toString()}`);
447
- return Err(toError(error));
448
- }
449
- const rawResults = data;
450
- const results = config.filterFn ? rawResults.filter(config.filterFn) : rawResults;
451
- return Ok(List(results));
452
- } catch (error) {
453
- log.error(`Error executing multi query on ${config.table}: ${toError(error).toString()}`);
454
- return Err(toError(error));
455
- }
456
- });
456
+ return IO.tryAsync(async () => {
457
+ const query2 = buildSupabaseQuery();
458
+ const { data, error } = await query2;
459
+ if (error) throw toError(error);
460
+ const rawResults = data;
461
+ const results = config.filterFn ? rawResults.filter(config.filterFn) : rawResults;
462
+ return List(results);
463
+ }, toError).tapError((err) => log.error(`Error getting ${config.table} items: ${err}`));
457
464
  },
458
465
  /**
459
466
  * Execute query expecting first result from potentially multiple
460
467
  */
461
468
  first: () => {
462
- return wrapAsync$2(async () => {
463
- const manyResult = await QueryBuilder(client, config).many();
464
- const list = manyResult.orThrow();
465
- if (list.isEmpty) {
466
- return Ok(Option.none());
467
- }
468
- return Ok(Option(list.head));
469
- });
469
+ return QueryBuilder(client, config).many().map((list) => list.isEmpty ? Option.none() : Option(list.head));
470
470
  },
471
471
  /**
472
472
  * Execute query expecting exactly one result, throw if error or not found
473
473
  */
474
474
  oneOrThrow: async () => {
475
- const result = await QueryBuilder(client, config).one();
476
- const option = result.orThrow();
475
+ const option = await QueryBuilder(client, config).one().runOrThrow();
477
476
  return option.orThrow(new Error(`No record found in ${config.table}`));
478
477
  },
479
478
  /**
480
479
  * Execute query expecting zero or more results, throw if error
481
480
  */
482
481
  manyOrThrow: async () => {
483
- const result = await QueryBuilder(client, config).many();
484
- return result.orThrow();
482
+ return QueryBuilder(client, config).many().runOrThrow();
485
483
  },
486
484
  /**
487
485
  * Execute query expecting first result, throw if error or empty
488
486
  */
489
487
  firstOrThrow: async () => {
490
- const result = await QueryBuilder(client, config).first();
491
- const option = result.orThrow();
488
+ const option = await QueryBuilder(client, config).first().runOrThrow();
492
489
  return option.orThrow(new Error(`No records found in ${config.table}`));
493
490
  }
494
491
  };
@@ -503,61 +500,50 @@ const createMappedQuery = (sourceQuery, mapFn) => {
503
500
  return createMappedQuery(filteredQuery, mapFn);
504
501
  },
505
502
  one: () => {
506
- return wrapAsync$2(async () => {
507
- const maybeItemResult = await sourceQuery.one();
508
- const maybeItem = maybeItemResult.orThrow();
509
- return maybeItem.fold(
510
- () => Ok(Option.none()),
511
- (item) => Ok(Option(mapFn(item)))
512
- );
513
- });
503
+ return sourceQuery.one().map(
504
+ (maybeItem) => maybeItem.fold(
505
+ () => Option.none(),
506
+ (item) => Option(mapFn(item))
507
+ )
508
+ );
514
509
  },
515
510
  many: () => {
516
- return wrapAsync$2(async () => {
517
- const itemsResult = await sourceQuery.many();
518
- const items = itemsResult.orThrow();
519
- return Ok(items.map(mapFn));
520
- });
511
+ return sourceQuery.many().map((items) => items.map(mapFn));
521
512
  },
522
513
  first: () => {
523
- return wrapAsync$2(async () => {
524
- const maybeItemResult = await sourceQuery.first();
525
- const maybeItem = maybeItemResult.orThrow();
526
- return maybeItem.fold(
527
- () => Ok(Option.none()),
528
- (item) => Ok(Option(mapFn(item)))
529
- );
530
- });
514
+ return sourceQuery.first().map(
515
+ (maybeItem) => maybeItem.fold(
516
+ () => Option.none(),
517
+ (item) => Option(mapFn(item))
518
+ )
519
+ );
531
520
  },
532
521
  /**
533
522
  * Execute mapped query expecting exactly one result, throw if error or not found
534
523
  */
535
524
  oneOrThrow: async () => {
536
- const result = await createMappedQuery(sourceQuery, mapFn).one();
537
- const option = result.orThrow();
525
+ const option = await createMappedQuery(sourceQuery, mapFn).one().runOrThrow();
538
526
  return option.orThrow(new Error(`No record found`));
539
527
  },
540
528
  /**
541
529
  * Execute mapped query expecting zero or more results, throw if error
542
530
  */
543
531
  manyOrThrow: async () => {
544
- const result = await createMappedQuery(sourceQuery, mapFn).many();
545
- return result.orThrow();
532
+ return createMappedQuery(sourceQuery, mapFn).many().runOrThrow();
546
533
  },
547
534
  /**
548
535
  * Execute mapped query expecting first result, throw if error or empty
549
536
  */
550
537
  firstOrThrow: async () => {
551
- const result = await createMappedQuery(sourceQuery, mapFn).first();
552
- const option = result.orThrow();
538
+ const option = await createMappedQuery(sourceQuery, mapFn).first().runOrThrow();
553
539
  return option.orThrow(new Error(`No records found`));
554
540
  }
555
541
  };
556
542
  };
557
- const createQuery = (client, table, where = {}, is, wherein, order, softDeleteConfig, schema, comparison) => {
543
+ const createQuery = (client, table, where = {}, is, wherein, order, softDeleteConfig, schema, comparison, not) => {
558
544
  const config = {
559
545
  table,
560
- conditions: [{ where, is, wherein, ...comparison }],
546
+ conditions: [{ where, is, wherein, ...comparison, not }],
561
547
  order,
562
548
  softDeleteMode: softDeleteConfig?.mode,
563
549
  softDeleteAppliedByDefault: softDeleteConfig?.appliedByDefault,
@@ -571,38 +557,28 @@ const isQuery = (obj) => {
571
557
  const isMappedQuery = (obj) => {
572
558
  return typeof obj === "object" && obj !== null && "one" in obj && "many" in obj && "first" in obj && "map" in obj && "filter" in obj;
573
559
  };
574
- const wrapAsync$1 = (fn) => {
575
- return fn();
576
- };
577
560
  const rpc = (client, functionName, args, options) => {
578
561
  const executeRpc = () => {
579
562
  return client.rpc(functionName, args ?? {}, {
580
563
  count: options?.count
581
564
  });
582
565
  };
583
- const one = () => wrapAsync$1(async () => {
584
- try {
585
- const { data, error } = await executeRpc();
586
- if (error) {
587
- return Err(toError(error));
588
- }
589
- if (data === null || data === void 0) {
590
- return Ok(Option.none());
591
- }
592
- if (Array.isArray(data)) {
593
- if (data.length === 0) {
594
- return Ok(Option.none());
595
- }
596
- return Ok(Option(data[0]));
566
+ const one = () => IO.tryAsync(async () => {
567
+ const { data, error } = await executeRpc();
568
+ if (error) throw toError(error);
569
+ if (data === null || data === void 0) {
570
+ return Option.none();
571
+ }
572
+ if (Array.isArray(data)) {
573
+ if (data.length === 0) {
574
+ return Option.none();
597
575
  }
598
- return Ok(Option(data));
599
- } catch (error) {
600
- return Err(toError(error));
576
+ return Option(data[0]);
601
577
  }
602
- });
578
+ return Option(data);
579
+ }, toError);
603
580
  const oneOrThrow = async () => {
604
- const result = await one();
605
- const option = result.orThrow();
581
+ const option = await one().runOrThrow();
606
582
  return option.fold(
607
583
  () => {
608
584
  throw new Error("RPC call returned no result");
@@ -610,26 +586,19 @@ const rpc = (client, functionName, args, options) => {
610
586
  (value) => value
611
587
  );
612
588
  };
613
- const many = () => wrapAsync$1(async () => {
614
- try {
615
- const { data, error } = await executeRpc();
616
- if (error) {
617
- return Err(toError(error));
618
- }
619
- if (data === null || data === void 0) {
620
- return Ok(List([]));
621
- }
622
- if (Array.isArray(data)) {
623
- return Ok(List(data));
624
- }
625
- return Ok(List([data]));
626
- } catch (error) {
627
- return Err(toError(error));
589
+ const many = () => IO.tryAsync(async () => {
590
+ const { data, error } = await executeRpc();
591
+ if (error) throw toError(error);
592
+ if (data === null || data === void 0) {
593
+ return List([]);
594
+ }
595
+ if (Array.isArray(data)) {
596
+ return List(data);
628
597
  }
629
- });
598
+ return List([data]);
599
+ }, toError);
630
600
  const manyOrThrow = async () => {
631
- const result = await many();
632
- return result.orThrow();
601
+ return many().runOrThrow();
633
602
  };
634
603
  return {
635
604
  one,
@@ -638,208 +607,145 @@ const rpc = (client, functionName, args, options) => {
638
607
  manyOrThrow
639
608
  };
640
609
  };
641
- const wrapAsync = (fn) => {
642
- return fn();
643
- };
644
- const getEntity = (client, table, where, is, schema) => wrapAsync(async () => {
645
- try {
646
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
647
- const baseQuery = tableQuery.select("*").match(where);
648
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(baseQuery)(
649
- (query2, [column, value]) => query2.is(column, value)
650
- ) : baseQuery;
651
- const { data, error } = await queryWithIs.single();
652
- if (error) {
653
- return Err(toError(error));
654
- }
655
- return Ok(data);
656
- } catch (error) {
657
- return Err(toError(error));
658
- }
659
- });
610
+ const getEntity = (client, table, where, is, schema) => IO.tryAsync(async () => {
611
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
612
+ const baseQuery = tableQuery.select("*").match(where);
613
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(baseQuery)(
614
+ (query2, [column, value]) => query2.is(column, value)
615
+ ) : baseQuery;
616
+ const { data, error } = await queryWithIs.single();
617
+ if (error) throw toError(error);
618
+ return data;
619
+ }, toError);
660
620
  const getEntities = (client, table, where = {}, is, wherein, order = [
661
621
  "id",
662
622
  { ascending: true }
663
- ], schema) => wrapAsync(async () => {
664
- try {
665
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
666
- const baseQuery = tableQuery.select("*").match(where);
667
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
668
- (query2, [column, values]) => query2.in(column, values)
669
- ) : baseQuery;
670
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
671
- (query2, [column, value]) => query2.is(column, value)
672
- ) : queryWithIn;
673
- const queryOrderBy = queryWithIs.order(order[0], order[1]);
674
- const { data, error } = await queryOrderBy;
675
- if (error) {
676
- return Err(toError(error));
677
- }
678
- return Ok(List(data));
679
- } catch (error) {
680
- return Err(toError(error));
681
- }
682
- });
683
- const addEntities = (client, table, entities, schema) => wrapAsync(async () => {
684
- try {
685
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
686
- const { data, error } = await tableQuery.insert(entities).select();
687
- if (error) {
688
- return Err(toError(error));
689
- }
690
- return Ok(List(data));
691
- } catch (error) {
692
- return Err(toError(error));
693
- }
694
- });
695
- const updateEntity = (client, table, entities, where, is, wherein, schema) => wrapAsync(async () => {
696
- try {
697
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
698
- const baseQuery = tableQuery.update(entities).match(where);
699
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
700
- (query2, [column, values]) => query2.in(column, values)
701
- ) : baseQuery;
702
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
703
- (query2, [column, value]) => query2.is(column, value)
704
- ) : queryWithIn;
705
- const { data, error } = await queryWithIs.select().single();
706
- if (error) {
707
- return Err(toError(error));
708
- }
709
- return Ok(data);
710
- } catch (error) {
711
- return Err(toError(error));
712
- }
713
- });
714
- const upsertEntities = (client, table, entities, identity = "id", where, is, wherein, schema) => wrapAsync(async () => {
715
- try {
716
- const onConflict = Array.isArray(identity) ? identity.join(",") : identity;
717
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
718
- const baseQuery = tableQuery.upsert(entities, { onConflict }).match(where ?? {});
719
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
720
- (query2, [column, values]) => query2.in(column, values)
721
- ) : baseQuery;
722
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
723
- (query2, [column, value]) => query2.is(column, value)
724
- ) : queryWithIn;
725
- const { data, error } = await queryWithIs.select();
726
- if (error) {
727
- return Err(toError(error));
728
- }
729
- return Ok(List(data));
730
- } catch (error) {
731
- return Err(toError(error));
732
- }
733
- });
734
- const deleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
735
- try {
736
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
737
- const baseQuery = tableQuery.delete().match(where);
738
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
739
- (query2, [column, values]) => query2.in(column, values)
740
- ) : baseQuery;
741
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
742
- (query2, [column, value]) => query2.is(column, value)
743
- ) : queryWithIn;
744
- const { data, error } = await queryWithIs.select().single();
745
- if (error) {
746
- return Err(toError(error));
747
- }
748
- return Ok(data);
749
- } catch (error) {
750
- return Err(toError(error));
751
- }
752
- });
753
- const deleteEntities = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
754
- try {
755
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
756
- const baseQuery = tableQuery.delete().match(where);
757
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
758
- (query2, [column, values]) => query2.in(column, values)
759
- ) : baseQuery;
760
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
761
- (query2, [column, value]) => query2.is(column, value)
762
- ) : queryWithIn;
763
- const { data, error } = await queryWithIs.select();
764
- if (error) {
765
- return Err(toError(error));
766
- }
767
- return Ok(List(data));
768
- } catch (error) {
769
- return Err(toError(error));
770
- }
771
- });
772
- const softDeleteEntity = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
773
- try {
774
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
775
- const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
776
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
777
- (query2, [column, values]) => query2.in(column, values)
778
- ) : baseQuery;
779
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
780
- (query2, [column, value]) => query2.is(column, value)
781
- ) : queryWithIn;
782
- const { data, error } = await queryWithIs.select().single();
783
- if (error) {
784
- return Err(toError(error));
785
- }
786
- return Ok(data);
787
- } catch (error) {
788
- return Err(toError(error));
789
- }
790
- });
791
- const softDeleteEntities = (client, table, where, is, wherein, schema) => wrapAsync(async () => {
792
- try {
793
- const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
794
- const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
795
- const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
796
- (query2, [column, values]) => query2.in(column, values)
797
- ) : baseQuery;
798
- const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
799
- (query2, [column, value]) => query2.is(column, value)
800
- ) : queryWithIn;
801
- const { data, error } = await queryWithIs.select();
802
- if (error) {
803
- return Err(toError(error));
804
- }
805
- return Ok(List(data));
806
- } catch (error) {
807
- return Err(toError(error));
808
- }
809
- });
623
+ ], schema) => IO.tryAsync(async () => {
624
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
625
+ const baseQuery = tableQuery.select("*").match(where);
626
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
627
+ (query2, [column, values]) => query2.in(column, values)
628
+ ) : baseQuery;
629
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
630
+ (query2, [column, value]) => query2.is(column, value)
631
+ ) : queryWithIn;
632
+ const queryOrderBy = queryWithIs.order(order[0], order[1]);
633
+ const { data, error } = await queryOrderBy;
634
+ if (error) throw toError(error);
635
+ return List(data);
636
+ }, toError);
637
+ const addEntities = (client, table, entities, schema) => IO.tryAsync(async () => {
638
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
639
+ const { data, error } = await tableQuery.insert(entities).select();
640
+ if (error) throw toError(error);
641
+ return List(data);
642
+ }, toError);
643
+ const updateEntity = (client, table, entities, where, is, wherein, schema) => IO.tryAsync(async () => {
644
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
645
+ const baseQuery = tableQuery.update(entities).match(where);
646
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
647
+ (query2, [column, values]) => query2.in(column, values)
648
+ ) : baseQuery;
649
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
650
+ (query2, [column, value]) => query2.is(column, value)
651
+ ) : queryWithIn;
652
+ const { data, error } = await queryWithIs.select().single();
653
+ if (error) throw toError(error);
654
+ return data;
655
+ }, toError);
656
+ const upsertEntities = (client, table, entities, identity = "id", where, is, wherein, schema) => IO.tryAsync(async () => {
657
+ const onConflict = Array.isArray(identity) ? identity.join(",") : identity;
658
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
659
+ const baseQuery = tableQuery.upsert(entities, { onConflict }).match(where ?? {});
660
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
661
+ (query2, [column, values]) => query2.in(column, values)
662
+ ) : baseQuery;
663
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
664
+ (query2, [column, value]) => query2.is(column, value)
665
+ ) : queryWithIn;
666
+ const { data, error } = await queryWithIs.select();
667
+ if (error) throw toError(error);
668
+ return List(data);
669
+ }, toError);
670
+ const deleteEntity = (client, table, where, is, wherein, schema) => IO.tryAsync(async () => {
671
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
672
+ const baseQuery = tableQuery.delete().match(where);
673
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
674
+ (query2, [column, values]) => query2.in(column, values)
675
+ ) : baseQuery;
676
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
677
+ (query2, [column, value]) => query2.is(column, value)
678
+ ) : queryWithIn;
679
+ const { data, error } = await queryWithIs.select().single();
680
+ if (error) throw toError(error);
681
+ return data;
682
+ }, toError);
683
+ const deleteEntities = (client, table, where, is, wherein, schema) => IO.tryAsync(async () => {
684
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
685
+ const baseQuery = tableQuery.delete().match(where);
686
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
687
+ (query2, [column, values]) => query2.in(column, values)
688
+ ) : baseQuery;
689
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
690
+ (query2, [column, value]) => query2.is(column, value)
691
+ ) : queryWithIn;
692
+ const { data, error } = await queryWithIs.select();
693
+ if (error) throw toError(error);
694
+ return List(data);
695
+ }, toError);
696
+ const softDeleteEntity = (client, table, where, is, wherein, schema) => IO.tryAsync(async () => {
697
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
698
+ const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
699
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
700
+ (query2, [column, values]) => query2.in(column, values)
701
+ ) : baseQuery;
702
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
703
+ (query2, [column, value]) => query2.is(column, value)
704
+ ) : queryWithIn;
705
+ const { data, error } = await queryWithIs.select().single();
706
+ if (error) throw toError(error);
707
+ return data;
708
+ }, toError);
709
+ const softDeleteEntities = (client, table, where, is, wherein, schema) => IO.tryAsync(async () => {
710
+ const tableQuery = schema ? client.schema(schema).from(table) : client.from(table);
711
+ const baseQuery = tableQuery.update({ deleted: (/* @__PURE__ */ new Date()).toISOString() }).match(where);
712
+ const queryWithIn = wherein ? List(Object.entries(wherein)).foldLeft(baseQuery)(
713
+ (query2, [column, values]) => query2.in(column, values)
714
+ ) : baseQuery;
715
+ const queryWithIs = is ? List(Object.entries(is)).foldLeft(queryWithIn)(
716
+ (query2, [column, value]) => query2.is(column, value)
717
+ ) : queryWithIn;
718
+ const { data, error } = await queryWithIs.select();
719
+ if (error) throw toError(error);
720
+ return List(data);
721
+ }, toError);
810
722
  const query = (client, table, where = {}, is, wherein, order, schema) => {
811
723
  return createQuery(client, table, where, is, wherein, order, void 0, schema);
812
724
  };
813
- function MultiMutationQuery(promise) {
814
- const result = Object.assign(promise, {
815
- many: () => promise,
725
+ function MultiMutationQuery(task) {
726
+ return {
727
+ many: () => task,
816
728
  manyOrThrow: async () => {
817
- const taskResult = await promise;
818
- return taskResult.orThrow();
729
+ return task.runOrThrow();
819
730
  },
820
- execute: () => promise,
731
+ execute: () => task,
821
732
  executeOrThrow: async () => {
822
- const taskResult = await promise;
823
- return taskResult.orThrow();
733
+ return task.runOrThrow();
824
734
  }
825
- });
826
- return result;
735
+ };
827
736
  }
828
- function SingleMutationQuery(promise) {
829
- const result = Object.assign(promise, {
830
- one: () => promise.then((outcome) => outcome.map((value) => Option(value))),
737
+ function SingleMutationQuery(task) {
738
+ return {
739
+ one: () => task.map((value) => Option(value)),
831
740
  oneOrThrow: async () => {
832
- const taskResult = await promise;
833
- return taskResult.orThrow();
741
+ return task.runOrThrow();
834
742
  },
835
- execute: () => promise.then((outcome) => outcome.map((value) => Option(value))),
743
+ execute: () => task.map((value) => Option(value)),
836
744
  executeOrThrow: async () => {
837
- const taskResult = await promise;
838
- const value = taskResult.orThrow();
745
+ const value = await task.runOrThrow();
839
746
  return Option(value);
840
747
  }
841
- });
842
- return result;
748
+ };
843
749
  }
844
750
  function getSoftDeleteMode(softDelete) {
845
751
  return softDelete ? "exclude" : "include";
@@ -867,7 +773,7 @@ function createGetItemQuery(client, name, whereConditions, is, softDeleteMode, s
867
773
  schema
868
774
  );
869
775
  }
870
- function createGetItemsQuery(client, name, whereConditions, is, wherein, order, softDeleteMode, schema, comparison) {
776
+ function createGetItemsQuery(client, name, whereConditions, is, wherein, order, softDeleteMode, schema, comparison, not) {
871
777
  return createQuery(
872
778
  client,
873
779
  name,
@@ -880,7 +786,8 @@ function createGetItemsQuery(client, name, whereConditions, is, wherein, order,
880
786
  appliedByDefault: true
881
787
  },
882
788
  schema,
883
- comparison
789
+ comparison,
790
+ not
884
791
  );
885
792
  }
886
793
  function createAddItemsMutation(client, name, items, schema) {
@@ -947,7 +854,8 @@ function makeGetItems(client, name, softDeleteMode, schema) {
947
854
  lt,
948
855
  neq,
949
856
  like,
950
- ilike
857
+ ilike,
858
+ not
951
859
  } = {}) {
952
860
  return createGetItemsQuery(
953
861
  client,
@@ -958,7 +866,8 @@ function makeGetItems(client, name, softDeleteMode, schema) {
958
866
  order,
959
867
  softDeleteMode,
960
868
  schema,
961
- { gte, gt, lte, lt, neq, like, ilike }
869
+ { gte, gt, lte, lt, neq, like, ilike },
870
+ not
962
871
  );
963
872
  };
964
873
  }
@@ -976,7 +885,7 @@ function makePartitionedGetItem(client, name, partitionField, softDeleteMode, sc
976
885
  };
977
886
  }
978
887
  function makePartitionedGetItems(client, name, partitionField, softDeleteMode, schema) {
979
- return function getItems(partitionKey, { where, is, wherein, order, gte, gt, lte, lt, neq, like, ilike } = {}) {
888
+ return function getItems(partitionKey, { where, is, wherein, order, gte, gt, lte, lt, neq, like, ilike, not } = {}) {
980
889
  const whereConditions = buildWhereWithPartition(partitionField, partitionKey, where);
981
890
  return createGetItemsQuery(
982
891
  client,
@@ -987,7 +896,8 @@ function makePartitionedGetItems(client, name, partitionField, softDeleteMode, s
987
896
  order,
988
897
  softDeleteMode,
989
898
  schema,
990
- { gte, gt, lte, lt, neq, like, ilike }
899
+ { gte, gt, lte, lt, neq, like, ilike },
900
+ not
991
901
  );
992
902
  };
993
903
  }
@@ -1276,10 +1186,9 @@ const PartitionedEntity = (client, name, config) => {
1276
1186
  };
1277
1187
  export {
1278
1188
  Entity,
1279
- Err2 as Err,
1189
+ IO2 as IO,
1280
1190
  List2 as List,
1281
1191
  MultiMutationQuery,
1282
- Ok2 as Ok,
1283
1192
  Option2 as Option,
1284
1193
  PartitionedEntity,
1285
1194
  SingleMutationQuery,