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.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]) =>
|
|
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
|
-
|
|
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
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
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
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
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
|
|
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
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
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
|
|
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
|
-
|
|
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
|
|
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 = () =>
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
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.
|
|
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
|
|
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 = () =>
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
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
|
-
|
|
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
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
const
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
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) =>
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
}
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
});
|
|
684
|
-
const
|
|
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
|
-
|
|
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(
|
|
815
|
-
|
|
816
|
-
many: () =>
|
|
726
|
+
function MultiMutationQuery(task) {
|
|
727
|
+
return {
|
|
728
|
+
many: () => task,
|
|
817
729
|
manyOrThrow: async () => {
|
|
818
|
-
|
|
819
|
-
return taskResult.orThrow();
|
|
730
|
+
return task.runOrThrow();
|
|
820
731
|
},
|
|
821
|
-
execute: () =>
|
|
732
|
+
execute: () => task,
|
|
822
733
|
executeOrThrow: async () => {
|
|
823
|
-
|
|
824
|
-
return taskResult.orThrow();
|
|
734
|
+
return task.runOrThrow();
|
|
825
735
|
}
|
|
826
|
-
}
|
|
827
|
-
return result;
|
|
736
|
+
};
|
|
828
737
|
}
|
|
829
|
-
function SingleMutationQuery(
|
|
830
|
-
|
|
831
|
-
one: () =>
|
|
738
|
+
function SingleMutationQuery(task) {
|
|
739
|
+
return {
|
|
740
|
+
one: () => task.map((value) => functype.Option(value)),
|
|
832
741
|
oneOrThrow: async () => {
|
|
833
|
-
|
|
834
|
-
return taskResult.orThrow();
|
|
742
|
+
return task.runOrThrow();
|
|
835
743
|
},
|
|
836
|
-
execute: () =>
|
|
744
|
+
execute: () => task.map((value) => functype.Option(value)),
|
|
837
745
|
executeOrThrow: async () => {
|
|
838
|
-
const
|
|
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, "
|
|
1188
|
+
Object.defineProperty(exports, "IO", {
|
|
1279
1189
|
enumerable: true,
|
|
1280
|
-
get: () => functype.
|
|
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
|