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/README.md +39 -0
- package/dist/index.js +230 -324
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +231 -322
- package/dist/index.mjs.map +1 -1
- package/dist/src/entity/core.d.ts +5 -2
- package/dist/src/entity/core.d.ts.map +1 -1
- package/dist/src/entity/types.d.ts +12 -6
- package/dist/src/entity/types.d.ts.map +1 -1
- package/dist/src/index.d.ts +2 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/query/Query.d.ts +18 -13
- package/dist/src/query/Query.d.ts.map +1 -1
- package/dist/src/query/QueryBuilder.d.ts +6 -0
- package/dist/src/query/QueryBuilder.d.ts.map +1 -1
- package/dist/src/query/index.d.ts +19 -19
- package/dist/src/query/index.d.ts.map +1 -1
- package/dist/src/query/rpc.d.ts +7 -5
- package/dist/src/query/rpc.d.ts.map +1 -1
- package/package.json +31 -33
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
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]) =>
|
|
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
|
-
|
|
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
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
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
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
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
|
|
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
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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 = () =>
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
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
|
|
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
|
|
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 = () =>
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
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
|
-
|
|
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
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
const
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
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) =>
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
}
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
});
|
|
683
|
-
const
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
)
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
)
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
}
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
)
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
)
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
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(
|
|
814
|
-
|
|
815
|
-
many: () =>
|
|
725
|
+
function MultiMutationQuery(task) {
|
|
726
|
+
return {
|
|
727
|
+
many: () => task,
|
|
816
728
|
manyOrThrow: async () => {
|
|
817
|
-
|
|
818
|
-
return taskResult.orThrow();
|
|
729
|
+
return task.runOrThrow();
|
|
819
730
|
},
|
|
820
|
-
execute: () =>
|
|
731
|
+
execute: () => task,
|
|
821
732
|
executeOrThrow: async () => {
|
|
822
|
-
|
|
823
|
-
return taskResult.orThrow();
|
|
733
|
+
return task.runOrThrow();
|
|
824
734
|
}
|
|
825
|
-
}
|
|
826
|
-
return result;
|
|
735
|
+
};
|
|
827
736
|
}
|
|
828
|
-
function SingleMutationQuery(
|
|
829
|
-
|
|
830
|
-
one: () =>
|
|
737
|
+
function SingleMutationQuery(task) {
|
|
738
|
+
return {
|
|
739
|
+
one: () => task.map((value) => Option(value)),
|
|
831
740
|
oneOrThrow: async () => {
|
|
832
|
-
|
|
833
|
-
return taskResult.orThrow();
|
|
741
|
+
return task.runOrThrow();
|
|
834
742
|
},
|
|
835
|
-
execute: () =>
|
|
743
|
+
execute: () => task.map((value) => Option(value)),
|
|
836
744
|
executeOrThrow: async () => {
|
|
837
|
-
const
|
|
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
|
-
|
|
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,
|