Skip to content

Commit 6f41c53

Browse files
committed
Updated mysql functions
1 parent f47ec6e commit 6f41c53

File tree

4 files changed

+46
-21
lines changed

4 files changed

+46
-21
lines changed

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1095,4 +1095,10 @@ The examples here use the fluent API.
10951095

10961096
+ `mysql.decimal()` now returns the `Decimal` interface and not `string`.
10971097
This breaking change was introduced because not much code should break
1098-
and this increases type safety overall.
1098+
and this increases type safety overall.
1099+
1100+
+ 1.24.0 -> 1.25.0
1101+
1102+
+ `mysql.xxxIntXxx()` functions now return `bigint` and not `number`.
1103+
This breaking change was introduced because this increases type safety overall.
1104+
This also follows the general direction the `tsql` project wants to follow.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "type-mapping",
3-
"version": "1.24.0",
3+
"version": "1.25.0",
44
"description": "Map input data safely",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/mysql-lib/boolean/index.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
} from "../../mapper";
66
import {FluentMapper} from "../../fluent-mapper";
77
import * as fLib from "../../fluent-lib";
8+
import * as TypeUtil from "../../type-util";
89

910
/**
1011
Converts some values to `boolean`.
@@ -20,13 +21,14 @@ export function boolean () : (
2021
FluentMapper<
2122
& SafeMapper<boolean>
2223
& ExpectedInput<boolean>
23-
& MappableInput<boolean | 0 | 1 | "0" | "1" | "false" | "true">
24+
& MappableInput<boolean | 0 | 1 | "0" | "1" | "false" | "true" | 0n | 1n>
2425
>
2526
) {
27+
const BigInt = TypeUtil.getBigIntFactoryFunctionOrError();
2628
return fLib.or(
2729
fLib.boolean(),
28-
fLib.literal("0", "1", 0, 1, "false", "true").pipe(
29-
(name : string, v : "0"|"1"|0|1|"false"|"true") : boolean => {
30+
fLib.literal("0", "1", 0, 1, "false", "true", BigInt(0) as 0n, BigInt(1) as 1n).pipe(
31+
(name : string, v : "0"|"1"|0|1|"false"|"true"|0n|1n) : boolean => {
3032
switch (v) {
3133
case "0": return false;
3234
case "1": return true;
@@ -35,8 +37,14 @@ export function boolean () : (
3537
case "false": return false;
3638
case "true": return true;
3739
default : {
40+
const str = String(v);
41+
if (str == "0") {
42+
return false;
43+
} else if (str == "1") {
44+
return true;
45+
}
3846
//Shouldn't happen
39-
throw new Error(`Expected ${name} to be one of '0'|'1'|0|1|'false'|'true'`);
47+
throw new Error(`Expected ${name} to be one of '0'|'1'|0|1|'false'|'true'|'0n'|'1n'`);
4048
}
4149
}
4250
}
@@ -58,18 +66,23 @@ function toTrue () : (
5866
FluentMapper<
5967
& SafeMapper<true>
6068
& ExpectedInput<true>
61-
& MappableInput<true | 1 | "1" | "true">
69+
& MappableInput<true | 1 | "1" | "true" | 1n>
6270
>
6371
) {
72+
const BigInt = TypeUtil.getBigIntFactoryFunctionOrError();
6473
return fLib.or(
6574
fLib.literal(true),
66-
fLib.literal("1", 1, "true").pipe(
67-
(name : string, v : "1"|1|"true") : true => {
75+
fLib.literal("1", 1, "true", BigInt(1) as 1n).pipe(
76+
(name : string, v : "1"|1|"true"|1n) : true => {
6877
switch (v) {
6978
case "1": return true;
7079
case 1: return true;
7180
case "true": return true;
7281
default : {
82+
const str = String(v);
83+
if (str == "1") {
84+
return true;
85+
}
7386
//Shouldn't happen
7487
throw new Error(`Expected ${name} to be one of '1'|1|'true'`);
7588
}
@@ -93,18 +106,23 @@ function toFalse () : (
93106
FluentMapper<
94107
& SafeMapper<false>
95108
& ExpectedInput<false>
96-
& MappableInput<false | 0 | "0" | "false">
109+
& MappableInput<false | 0 | "0" | "false" | 0n>
97110
>
98111
) {
112+
const BigInt = TypeUtil.getBigIntFactoryFunctionOrError();
99113
return fLib.or(
100114
fLib.literal(false),
101-
fLib.literal("0", 0, "false").pipe(
102-
(name : string, v : "0"|0|"false") : false => {
115+
fLib.literal("0", 0, "false", BigInt(0) as 0n).pipe(
116+
(name : string, v : "0"|0|"false"|0n) : false => {
103117
switch (v) {
104118
case "0": return false;
105119
case 0: return false;
106120
case "false": return false;
107121
default : {
122+
const str = String(v);
123+
if (str == "0") {
124+
return false;
125+
}
108126
//Shouldn't happen
109127
throw new Error(`Expected ${name} to be one of '0'|0|'false'`);
110128
}

src/mysql-lib/int/int.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@ import {
55
} from "../../mapper";
66
import {FluentMapper} from "../../fluent-mapper";
77
import * as fLib from "../../fluent-lib";
8-
import {range} from "../../functional-lib";
8+
import {bigIntRange} from "../../functional-lib";
9+
import {getBigIntFactoryFunctionOrError} from "../../type-util";
910

1011
/**
11-
May result in loss in precision if passing in
12-
`string|bigint`
12+
Uses `integerFormatString()` and `BigInt()` internally.
1313
*/
1414
export function unsafeInt () : (
1515
FluentMapper<
16-
& SafeMapper<number>
17-
& ExpectedInput<number>
16+
& SafeMapper<bigint>
17+
& ExpectedInput<bigint>
1818
& MappableInput<string | number | bigint>
1919
>
2020
) {
21-
return fLib.toInteger();
21+
return fLib.toBigInt();
2222
}
2323

2424
function intDelegate (min : number, max : number) {
25+
const bigIntFactory = getBigIntFactoryFunctionOrError();
2526
return unsafeInt().pipe(
26-
range({
27-
gtEq : min,
28-
ltEq : max,
27+
bigIntRange({
28+
gtEq : bigIntFactory(min),
29+
ltEq : bigIntFactory(max),
2930
})
3031
);
3132
}

0 commit comments

Comments
 (0)