Skip to content

Commit

Permalink
Merge pull request #62 from UN-OCHA/drop-bigint-and-numeric
Browse files Browse the repository at this point in the history
Define bigint and numeric columns as a union of string and number
  • Loading branch information
czmj authored Dec 1, 2021
2 parents fa37611 + fd15d67 commit 10fb9af
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unocha/hpc-api-core",
"version": "0.9.0",
"version": "0.9.1",
"description": "Core libraries supporting HPC.Tools API Backend",
"license": "Apache-2.0",
"private": false,
Expand Down
30 changes: 27 additions & 3 deletions src/db/models/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ export default defineIDModel({
id: { kind: 'branded-integer', brand: FLOW_ID },
},
required: {
amountUSD: { kind: 'checked', type: t.bigint },
/**
* Union type of string and number is used because int8 (bigint)
* DB type is read as string, but when inserting rows, we don't want
* library clients to provide numbers as strings.
*
* TODO: Add the possibility to define separate types for reading
* and writing, then use string for reading and number for writing
*/
amountUSD: { kind: 'checked', type: t.union([t.string, t.number]) },
},
nonNullWithDefault: {
versionID: { kind: 'checked', type: t.number },
Expand All @@ -29,9 +37,25 @@ export default defineIDModel({
decisionDate: { kind: 'checked', type: DATE },
firstReportedDate: { kind: 'checked', type: DATE },
budgetYear: { kind: 'checked', type: t.string },
origAmount: { kind: 'checked', type: t.bigint },
/**
* Union type of string and number is used because int8 (bigint)
* DB type is read as string, but when inserting rows, we don't want
* library clients to provide numbers as strings.
*
* TODO: Add the possibility to define separate types for reading
* and writing, then use string for reading and number for writing
*/
origAmount: { kind: 'checked', type: t.union([t.string, t.number]) },
origCurrency: { kind: 'checked', type: t.string },
exchangeRate: { kind: 'checked', type: t.number },
/**
* Union type of string and number is used because numeric DB type
* is read as string, but when inserting rows, we don't want
* library clients to provide numbers as strings.
*
* TODO: Add the possibility to define separate types for reading
* and writing, then use string for reading and number for writing
*/
exchangeRate: { kind: 'checked', type: t.union([t.string, t.number]) },
description: { kind: 'checked', type: t.string },
notes: { kind: 'checked', type: t.string },
versionStartDate: { kind: 'checked', type: DATE },
Expand Down
10 changes: 9 additions & 1 deletion src/db/models/location.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ export default defineIDModel({
longitude: { kind: 'checked', type: t.number },
iso3: { kind: 'checked', type: t.string },
pcode: { kind: 'checked', type: t.string },
validOn: { kind: 'checked', type: t.bigint },
/**
* Union type of string and number is used because int8 (bigint)
* DB type is read as string, but when inserting rows, we don't want
* library clients to provide numbers as strings.
*
* TODO: Add the possibility to define separate types for reading
* and writing, then use string for reading and number for writing
*/
validOn: { kind: 'checked', type: t.union([t.string, t.number]) },
parentId: { kind: 'branded-integer', brand: LOCATION_ID },
},
accidentallyOptional: {
Expand Down
8 changes: 8 additions & 0 deletions src/db/models/projectVersion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ export default defineIDModel({
id: { kind: 'branded-integer', brand: PROJECT_VERSION_ID },
},
optional: {
/**
* Union type of string and number is used because int8 (bigint)
* DB type is read as string, but when inserting rows, we don't want
* library clients to provide numbers as strings.
*
* TODO: Add the possibility to define separate types for reading
* and writing, then use string for reading and number for writing
*/
currentRequestedFunds: {
kind: 'checked',
type: t.union([t.string, t.number]),
Expand Down

0 comments on commit 10fb9af

Please sign in to comment.