Skip to content

Commit

Permalink
Merge pull request #106 from balena-io-modules/joshbwlng/bigint-string
Browse files Browse the repository at this point in the history
Update BigInteger and BigSerial TsTypes
  • Loading branch information
flowzone-app[bot] authored Jul 19, 2024
2 parents 8a9d068 + 45d3cd6 commit ab5ed18
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
10 changes: 5 additions & 5 deletions src/types/big-integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const types = {
},
};

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

export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
Expand All @@ -18,11 +18,11 @@ export const fetchProcessing: TypeUtils.FetchProcessing<Types['Read']> = (
if (data == null) {
return data;
}
let value: bigint;
if (typeof data === 'bigint') {
let value: string;
if (typeof data === 'string') {
value = data;
} else if (typeof data === 'string' || typeof data === 'number') {
value = BigInt(data);
} else if (typeof data === 'bigint' || typeof data === 'number') {
value = data.toString();
} else {
throw new Error('Fetched bigint is not valid: ' + typeof data);
}
Expand Down
2 changes: 1 addition & 1 deletion src/types/big-serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const types = {
},
};

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

export const validate: TypeUtils.Validate<Types['Write'], DbWriteType> =
Expand Down
11 changes: 6 additions & 5 deletions test/Big Integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ 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(BigInt(1), '1');
test.fetch(1, '1');
test.fetch('1', '1');
test.fetch(testBigIntString, testBigIntString);
test.fetch(BigInt(testBigIntString), testBigIntString);
test.fetch({}, new Error('Fetched bigint is not valid: object'));
test.fetch(null, null);
});

describe('validate', function () {
Expand Down
12 changes: 7 additions & 5 deletions test/Big Serial.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ 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));
test.fetch(BigInt(1), '1');
test.fetch(1, '1');
test.fetch('1', '1');
test.fetch(testBigIntString, testBigIntString);
test.fetch(BigInt(testBigIntString), testBigIntString);
test.fetch({}, new Error('Fetched bigint is not valid: object'));
test.fetch(null, null);
});

describe('validate', function () {
Expand Down

0 comments on commit ab5ed18

Please sign in to comment.