diff --git a/baker/updateChartEntities.ts b/baker/updateChartEntities.ts index 425019d087b..412b80ec361 100644 --- a/baker/updateChartEntities.ts +++ b/baker/updateChartEntities.ts @@ -6,6 +6,9 @@ import { Grapher } from "@ourworldindata/grapher" import { + ChartsTableName, + ChartsXEntitiesTableName, + DbRawChart, GrapherInterface, GrapherTabOption, MultipleOwidVariableDataDimensionsMap, @@ -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() await pMap( @@ -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 ---") diff --git a/db/model/Entity.ts b/db/model/Entity.ts index a49774695b5..ba190d3f757 100644 --- a/db/model/Entity.ts +++ b/db/model/Entity.ts @@ -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> { - const entities = (await knex("entities").select("id", "name")) as Pick< - DbPlainEntity, - "id" | "name" - >[] + const entities = (await knex(EntitiesTableName).select( + "id", + "name" + )) as Pick[] const entityNameToIdMap = new Map( entities.map((entity) => [entity.name, entity.id]) ) @@ -18,10 +18,10 @@ export async function mapEntityNamesToEntityIds( export async function mapEntityIdsToEntityNames( knex: db.KnexReadonlyTransaction ): Promise> { - const entities = (await knex("entities").select("id", "name")) as Pick< - DbPlainEntity, - "id" | "name" - >[] + const entities = (await knex(EntitiesTableName).select( + "id", + "name" + )) as Pick[] const entityIdToNameMap = new Map( entities.map((entity) => [entity.id, entity.name]) ) diff --git a/packages/@ourworldindata/types/src/dbTypes/ChartsXEntities.ts b/packages/@ourworldindata/types/src/dbTypes/ChartsXEntities.ts new file mode 100644 index 00000000000..b5c31d664be --- /dev/null +++ b/packages/@ourworldindata/types/src/dbTypes/ChartsXEntities.ts @@ -0,0 +1,8 @@ +export const ChartsXEntitiesTableName = "charts_x_entities" + +export interface DbInsertChartXEntity { + chartId: number + entityId: number +} + +export type DbPlainChartXEntity = Required diff --git a/packages/@ourworldindata/types/src/index.ts b/packages/@ourworldindata/types/src/index.ts index 543dae7d134..679d3fb9dee 100644 --- a/packages/@ourworldindata/types/src/index.ts +++ b/packages/@ourworldindata/types/src/index.ts @@ -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,