Skip to content

Commit

Permalink
refactor(entities): use db types
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelgerber committed Mar 28, 2024
1 parent 8ec47da commit 9fa5f53
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 14 deletions.
16 changes: 11 additions & 5 deletions baker/updateChartEntities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

import { Grapher } from "@ourworldindata/grapher"
import {
ChartsTableName,
ChartsXEntitiesTableName,
DbRawChart,
GrapherInterface,
GrapherTabOption,
MultipleOwidVariableDataDimensionsMap,
Expand Down Expand Up @@ -120,10 +123,13 @@ const obtainAvailableEntitiesForAllGraphers = async (
) => {
const entityNameToIdMap = await mapEntityNamesToEntityIds(trx)

const allPublishedGraphers = await trx
const allPublishedGraphers = (await trx
.select("id", "config")
.from("charts")
.whereRaw("config ->> '$.isPublished' = 'true'")
.from(ChartsTableName)
.whereRaw("config ->> '$.isPublished' = 'true'")) as Pick<
DbRawChart,
"id" | "config"
>[]

const availableEntitiesByChartId = new Map<number, number[]>()
await pMap(
Expand Down Expand Up @@ -181,13 +187,13 @@ const updateAvailableEntitiesForAllGraphers = async (

console.log("--- Updating charts_x_entities ---")

await trx.delete().from("charts_x_entities") // clears out the WHOLE table
await trx.delete().from(ChartsXEntitiesTableName) // clears out the WHOLE table
for (const [chartId, availableEntityIds] of availableEntitiesByChartId) {
const rows = availableEntityIds.map((entityId) => ({
chartId,
entityId,
}))
if (rows.length) await trx("charts_x_entities").insert(rows)
if (rows.length) await trx(ChartsXEntitiesTableName).insert(rows)
}

console.log("--- ✅ Done ---")
Expand Down
18 changes: 9 additions & 9 deletions db/model/Entity.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { DbPlainEntity } from "@ourworldindata/types"
import { DbPlainEntity, EntitiesTableName } from "@ourworldindata/types"
import * as db from "../db"

export async function mapEntityNamesToEntityIds(
knex: db.KnexReadonlyTransaction
): Promise<Map<string, number>> {
const entities = (await knex("entities").select("id", "name")) as Pick<
DbPlainEntity,
"id" | "name"
>[]
const entities = (await knex(EntitiesTableName).select(
"id",
"name"
)) as Pick<DbPlainEntity, "id" | "name">[]
const entityNameToIdMap = new Map<string, number>(
entities.map((entity) => [entity.name, entity.id])
)
Expand All @@ -18,10 +18,10 @@ export async function mapEntityNamesToEntityIds(
export async function mapEntityIdsToEntityNames(
knex: db.KnexReadonlyTransaction
): Promise<Map<number, string>> {
const entities = (await knex("entities").select("id", "name")) as Pick<
DbPlainEntity,
"id" | "name"
>[]
const entities = (await knex(EntitiesTableName).select(
"id",
"name"
)) as Pick<DbPlainEntity, "id" | "name">[]
const entityIdToNameMap = new Map<number, string>(
entities.map((entity) => [entity.id, entity.name])
)
Expand Down
8 changes: 8 additions & 0 deletions packages/@ourworldindata/types/src/dbTypes/ChartsXEntities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export const ChartsXEntitiesTableName = "charts_x_entities"

export interface DbInsertChartXEntity {
chartId: number
entityId: number
}

export type DbPlainChartXEntity = Required<DbInsertChartXEntity>
5 changes: 5 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,11 @@ export {
ChartTagsTableName,
type DbChartTagJoin,
} from "./dbTypes/ChartTags.js"
export {
ChartsXEntitiesTableName,
type DbInsertChartXEntity,
type DbPlainChartXEntity,
} from "./dbTypes/ChartsXEntities.js"
export {
type DbPlainCountryLatestData,
type DbInsertCountryLatestData,
Expand Down

0 comments on commit 9fa5f53

Please sign in to comment.