Skip to content

Commit

Permalink
Improve BigInteger and BigSerial support
Browse files Browse the repository at this point in the history
Change-type: major
  • Loading branch information
joshbwlng committed Jun 12, 2024
1 parent ea44c24 commit b9b1fc3
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 10 deletions.
6 changes: 6 additions & 0 deletions src/type-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ export const validate = {
}
return processedValue;
}),
bigint: checkRequired((value) => {
if (value === '') {
throw new Error('Cannot convert empty string to a BigInt');
}
return BigInt(value);
}),
text: (length?: number) =>
checkRequired((value) => {
if (typeof value !== 'string') {
Expand Down
23 changes: 20 additions & 3 deletions src/types/big-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,30 @@ export const types = {
},
};

export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;
export type Types = TypeUtils.TsTypes<bigint, number | bigint>;
type DbWriteType = bigint;

export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
data,
) => {
if (data == null) {
return data;
}
let value: bigint;
if (typeof data === 'bigint') {
value = data;
} else if (typeof data === 'string' || typeof data === 'number') {
value = BigInt(data);
} else {
throw new Error('Fetched bigint is not valid: ' + typeof data);
}
return value;
};

export const nativeFactTypes: TypeUtils.NativeFactTypes = {
Integer: TypeUtils.nativeFactTypeTemplates.comparison,
Real: TypeUtils.nativeFactTypeTemplates.comparison,
};

export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
TypeUtils.validate.bigint;
7 changes: 4 additions & 3 deletions src/types/big-serial.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as TypeUtils from '../type-utils';
export { fetchProcessing } from './big-integer';

const mysqlType: TypeUtils.DatabaseTypeFn = (
necessity: string,
Expand All @@ -23,8 +24,8 @@ export const types = {
},
};

export type Types = TypeUtils.TsTypes<number, number>;
type DbWriteType = number;
export type Types = TypeUtils.TsTypes<bigint, number | bigint>;
type DbWriteType = bigint;

export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
TypeUtils.validate.integer;
TypeUtils.validate.bigint;
35 changes: 33 additions & 2 deletions test/Big Integer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,39 @@
import * as helpers from './helpers';

helpers.describe('Big Integer', (test) => {
const testBigIntString = String(Number.MAX_SAFE_INTEGER) + '00000001';

describe('fetchProcessing', function () {
test.fetch(1, BigInt(1));
test.fetch('1', BigInt(1));
test.fetch('1', BigInt(1));
test.fetch(BigInt(testBigIntString), BigInt(testBigIntString));
test.fetch(testBigIntString, BigInt(testBigIntString));
test.fetch({}, new Error('Fetched bigint is not valid: object'));
});

describe('validate', function () {
test.validate(1, true, 1);
test.validate('1', true, 1);
test.validate(
1.1,
new RangeError(
'The number 1.1 cannot be converted to a BigInt because it is not an integer',
),
);
test.validate('1.1', new SyntaxError('Cannot convert 1.1 to a BigInt'));
test.validate('', new Error('Cannot convert empty string to a BigInt'));
test.validate(
'testNotANumber',
new Error('Cannot convert testNotANumber to a BigInt'),
);
test.validate(
NaN,
new SyntaxError(
'The number NaN cannot be converted to a BigInt because it is not an integer',
),
);
test.validate(1, true, BigInt(1));
test.validate('1', true, BigInt(1));
test.validate(BigInt(testBigIntString), true, BigInt(testBigIntString));
test.validate(testBigIntString, true, BigInt(testBigIntString));
});
});
35 changes: 33 additions & 2 deletions test/Big Serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,39 @@ helpers.describe('Big Serial', function (test) {
});
});

const testBigIntString = String(Number.MAX_SAFE_INTEGER) + '00000001';

describe('fetchProcessing', function () {
test.fetch(1, BigInt(1));
test.fetch('1', BigInt(1));
test.fetch('1', BigInt(1));
test.fetch(BigInt(testBigIntString), BigInt(testBigIntString));
test.fetch(testBigIntString, BigInt(testBigIntString));
});

describe('validate', function () {
test.validate(1, true, 1);
test.validate('1', true, 1);
test.validate(
1.1,
new RangeError(
'The number 1.1 cannot be converted to a BigInt because it is not an integer',
),
);
test.validate('1.1', new SyntaxError('Cannot convert 1.1 to a BigInt'));
test.validate('', new Error('Cannot convert empty string to a BigInt'));
test.validate(
'testNotANumber',
new Error('Cannot convert testNotANumber to a BigInt'),
);
test.validate(
NaN,
new SyntaxError(
'The number NaN cannot be converted to a BigInt because it is not an integer',
),
);

test.validate(1, true, BigInt(1));
test.validate('1', true, BigInt(1));
test.validate(BigInt(testBigIntString), true, BigInt(testBigIntString));
test.validate(testBigIntString, true, BigInt(testBigIntString));
});
});
1 change: 1 addition & 0 deletions test/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type TestFn = (
| null
| string
| number
| bigint
| boolean
| Date
| Buffer
Expand Down

0 comments on commit b9b1fc3

Please sign in to comment.