Skip to content

Commit

Permalink
🐛 use Knex properly for Homepage rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
ikesau committed Mar 1, 2024
1 parent 6b9d981 commit 30aa001
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
14 changes: 4 additions & 10 deletions baker/siteRenderers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ import {
import { FormattingOptions, GrapherInterface } from "@ourworldindata/types"
import { CountryProfileSpec } from "../site/countryProfileProjects.js"
import { formatPost } from "./formatWordpressPost.js"
import { queryMysql, knexTable } from "../db/db.js"
import { queryMysql, knexTable, getHomepageId } from "../db/db.js"
import { getPageOverrides, isPageOverridesCitable } from "./pageOverrides.js"
import { ProminentLink } from "../site/blocks/ProminentLink.js"
import {
Expand Down Expand Up @@ -254,16 +254,10 @@ export const renderPost = async (
}

export const renderFrontPage = async (knex: Knex<any, any[]>) => {
const gdocHomepageResult = await knex.raw<{ id: string }>(
`--sql
SELECT id
FROM posts_gdocs
WHERE content->>"$.type" = "${OwidGdocType.Homepage}"
AND published = TRUE`
)
const gdocHomepageId = await getHomepageId(knex)

if (gdocHomepageResult) {
const gdocHomepage = await GdocFactory.load(gdocHomepageResult.id)
if (gdocHomepageId) {
const gdocHomepage = await GdocFactory.load(gdocHomepageId)
await gdocHomepage.loadState()
return renderGdoc(gdocHomepage)
} else {
Expand Down
19 changes: 19 additions & 0 deletions db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,22 @@ export const getTotalNumberOfInUseGrapherTags = (): Promise<number> => {
knexInstance()
).then((res) => res?.count ?? 0)
}

/**
* For usage with GdocFactory.load, until we refactor Gdocs to be entirely Knex-based.
*/
export const getHomepageId = (
knex: Knex<any, any[]>
): Promise<string | undefined> => {
return knexRawFirst<{ id: string }>(
`-- sql
SELECT
posts_gdocs.id
FROM
posts_gdocs
WHERE
content->>'$.type' = '${OwidGdocType.Homepage}'
AND published = TRUE`,
knex
).then((result) => result?.id)
}

0 comments on commit 30aa001

Please sign in to comment.