Skip to content

Commit

Permalink
🐝 (db) add type and sort to db type definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamersmann committed Mar 28, 2024
1 parent 7930d26 commit 20a8611
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/@ourworldindata/types/src/dbTypes/Variables.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OwidVariableType } from "../OwidVariable.js"
import { OwidVariableDisplayConfigInterface } from "../OwidVariableDisplayConfigInterface.js"
import { JsonString } from "../domainTypes/Various.js"
import { GrapherInterface } from "../grapherTypes/GrapherTypes.js"
Expand Down Expand Up @@ -37,7 +38,10 @@ export interface DbInsertVariable {
titleVariant?: string | null
unit: string
updatedAt?: Date | null
type?: OwidVariableType | null
sort?: JsonString | null
}

export type DbRawVariable = Required<DbInsertVariable>

export interface VariableDisplayDimension {
Expand Down Expand Up @@ -65,6 +69,7 @@ export type DbEnrichedVariable = Omit<
| "grapherConfigAdmin"
| "grapherConfigETL"
| "processingLog"
| "sort"
> & {
display: OwidVariableDisplayConfigInterface
license: License | null
Expand All @@ -75,6 +80,7 @@ export type DbEnrichedVariable = Omit<
grapherConfigAdmin: GrapherInterface | null
grapherConfigETL: GrapherInterface | null
processingLog: unknown | null
sort: string[] | null
}

export function parseVariableDisplayConfig(
Expand Down Expand Up @@ -179,6 +185,16 @@ export function serializeVariableProcessingLog(
return processingLog ? JSON.stringify(processingLog) : null
}

export function parseVariableSort(sort: JsonString | null): string[] | null {
return sort ? JSON.parse(sort) : null
}

export function serializeVariableSort(
sort: string[] | null
): JsonString | null {
return sort ? JSON.stringify(sort) : null
}

export function parseVariablesRow(row: DbRawVariable): DbEnrichedVariable {
return {
...row,
Expand All @@ -193,6 +209,7 @@ export function parseVariablesRow(row: DbRawVariable): DbEnrichedVariable {
),
grapherConfigETL: parseVariableGrapherConfigETL(row.grapherConfigETL),
processingLog: parseVariableProcessingLog(row.processingLog),
sort: parseVariableSort(row.sort),
}
}

Expand All @@ -214,5 +231,6 @@ export function serializeVariablesRow(row: DbEnrichedVariable): DbRawVariable {
row.grapherConfigETL
),
processingLog: serializeVariableProcessingLog(row.processingLog),
sort: serializeVariableSort(row.sort),
}
}

0 comments on commit 20a8611

Please sign in to comment.