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