Skip to content

Commit

Permalink
🔨 clean up posts access via knex
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Mar 1, 2024
1 parent bc1a121 commit a017102
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 37 deletions.
4 changes: 2 additions & 2 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2204,10 +2204,10 @@ apiRouter.get("/posts.json", async (req) => {
return { posts: rows }
})

apiRouter.post("/posts/:postId/setTags", async (req, res) => {
postRouteWithRWTransaction(apiRouter, "/posts/:postId/setTags", async (req, res, trx) => {
const postId = expectInt(req.params.postId)

await setTagsForPost(postId, req.body.tagIds)
await setTagsForPost(trx, postId, req.body.tagIds)

return { success: true }
})
Expand Down
2 changes: 1 addition & 1 deletion baker/GrapherBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ const renderGrapherPage = async (
: undefined
const relatedCharts =
post && isWordpressDBEnabled
? await getPostRelatedCharts(post.id)
? await getPostRelatedCharts(knex, post.id)
: undefined
const relatedArticles =
grapher.id && isWordpressAPIEnabled
Expand Down
6 changes: 4 additions & 2 deletions baker/SiteBaker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ export class SiteBaker {

const postSlugs = []
for (const postApi of postsApi) {
const post = await getFullPost(postApi)
const post = await getFullPost(knex, postApi)
postSlugs.push(post.slug)
}

Expand Down Expand Up @@ -472,7 +472,9 @@ export class SiteBaker {
await pMap(
postsApi,
async (postApi) =>
getFullPost(postApi).then((post) => this.bakePost(post, knex)),
getFullPost(knex, postApi).then((post) =>
this.bakePost(post, knex)
),
{ concurrency: 10 }
)

Expand Down
2 changes: 1 addition & 1 deletion baker/algolia/indexToAlgolia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ async function generateWordpressRecords(
const records: PageRecord[] = []

for (const postApi of postsApi) {
const rawPost = await getFullPost(postApi)
const rawPost = await getFullPost(knex, postApi)
if (isEmpty(rawPost.content)) {
// we have some posts that are only placeholders (e.g. for a redirect); don't index these
console.log(
Expand Down
7 changes: 5 additions & 2 deletions db/DEPRECATEDwpdb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
FOR_SYNC_ONLY_graphqlQuery,
} from "./wpdb.js"
import { getFullPost } from "./model/Post.js"
import { KnexReadonlyTransaction } from "./db.js"

const DEPRECATED_ENTRIES_CATEGORY_ID = 44
const DEPRECATED_OWID_API_ENDPOINT = `${WORDPRESS_URL}/wp-json/owid/v1`
Expand Down Expand Up @@ -88,6 +89,7 @@ export const DEPRECATEDgetPosts = async (
// We might want to cache this as the network of prominent links densifies and
// multiple requests to the same posts are happening.
export const DEPRECATEDgetPostBySlugFromApi = async (
trx: KnexReadonlyTransaction,
slug: string
): Promise<FullPost> => {
if (!isWordpressAPIEnabled) {
Expand All @@ -96,12 +98,13 @@ export const DEPRECATEDgetPostBySlugFromApi = async (

const postApi = await FOR_SYNC_ONLY_getPostApiBySlugFromApi(slug)

return getFullPost(postApi)
return getFullPost(trx, postApi)
}

// the /revisions endpoint does not send back all the metadata required for
// the proper rendering of the post (e.g. authors), hence the double request.
export const DEPRECATEDgetLatestPostRevision = async (
trx: KnexReadonlyTransaction,
id: number
): Promise<FullPost> => {
const type = await DEPRECATEDgetPostType(id)
Expand All @@ -125,7 +128,7 @@ export const DEPRECATEDgetLatestPostRevision = async (
// and could have been modified in the sidebar.)
// - authors
// ...
return getFullPost({
return getFullPost(trx, {
...postApi,
content: revision.content,
title: revision.title,
Expand Down
2 changes: 1 addition & 1 deletion db/migrateWpPostsToArchieMl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const migrate = async (trx: db.KnexReadWriteTransaction): Promise<void> => {
const text = post.content
let relatedCharts: RelatedChart[] = []
if (isEntry) {
relatedCharts = await getPostRelatedCharts(post.id)
relatedCharts = await getPostRelatedCharts(trx, post.id)
}

const shouldIncludeMaxAsAuthor = isPostSlugCitable(post.slug)
Expand Down
69 changes: 41 additions & 28 deletions db/model/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,32 +68,34 @@ export const getTagsByPostId = async (
}

export const setTags = async (
trx: db.KnexReadWriteTransaction,
postId: number,
tagIds: number[]
): Promise<void> =>
await db.transaction(async (t) => {
const tagRows = tagIds.map((tagId) => [tagId, postId])
await t.execute(`DELETE FROM post_tags WHERE post_id=?`, [postId])
if (tagRows.length)
await t.execute(
`INSERT INTO post_tags (tag_id, post_id) VALUES ?`,
[tagRows]
)
})
): Promise<void> => {
const tagRows = tagIds.map((tagId) => [tagId, postId])
await db.knexRaw(trx, `DELETE FROM post_tags WHERE post_id=?`, [postId])
if (tagRows.length)
await db.knexRaw(
trx,
`INSERT INTO post_tags (tag_id, post_id) VALUES ?`,
[tagRows]
)
}

export const setTagsForPost = async (
trx: db.KnexReadWriteTransaction,
postId: number,
tagIds: number[]
): Promise<void> =>
await db.transaction(async (t) => {
const tagRows = tagIds.map((tagId) => [tagId, postId])
await t.execute(`DELETE FROM post_tags WHERE post_id=?`, [postId])
if (tagRows.length)
await t.execute(
`INSERT INTO post_tags (tag_id, post_id) VALUES ?`,
[tagRows]
)
})
): Promise<void> => {
const tagRows = tagIds.map((tagId) => [tagId, postId])
await db.knexRaw(trx, `DELETE FROM post_tags WHERE post_id=?`, [postId])
if (tagRows.length)
await db.knexRaw(
trx,
`INSERT INTO post_tags (tag_id, post_id) VALUES ?`,
[tagRows]
)
}

export const getPostRawBySlug = async (
trx: db.KnexReadonlyTransaction,
Expand Down Expand Up @@ -136,7 +138,7 @@ export const getFullPostBySlugFromSnapshot = async (
)
throw new JsonError(`No page snapshot found by slug ${slug}`, 404)

return getFullPost(postEnriched.wpApiSnapshot)
return getFullPost(trx, postEnriched.wpApiSnapshot)
}

export const getFullPostByIdFromSnapshot = async (
Expand All @@ -150,9 +152,11 @@ export const getFullPostByIdFromSnapshot = async (
)
throw new JsonError(`No page snapshot found by id ${id}`, 404)

return getFullPost(postEnriched.wpApiSnapshot)
return getFullPost(trx, postEnriched.wpApiSnapshot)
}

// TODO: I suggest that in the place where we define SiteNavigationStatic we create a Set with all the leaves and
// then this one becomes a simple lookup in the set. Probably nicest to do the set creation as a memoized function.
export const isPostSlugCitable = (slug: string): boolean => {
const entries = SiteNavigationStatic.categories
return entries.some((category) => {
Expand Down Expand Up @@ -204,9 +208,12 @@ export const getPostsFromSnapshots = async (
}

export const getPostRelatedCharts = async (
trx: db.KnexReadonlyTransaction,
postId: number
): Promise<RelatedChart[]> =>
db.queryMysql(`
db.knexRaw(
trx,
`-- sql
SELECT DISTINCT
charts.config->>"$.slug" AS slug,
charts.config->>"$.title" AS title,
Expand All @@ -218,9 +225,11 @@ export const getPostRelatedCharts = async (
WHERE post_tags.post_id=${postId}
AND charts.config->>"$.isPublished" = "true"
ORDER BY title ASC
`)
`
)

export const getFullPost = async (
trx: db.KnexReadonlyTransaction,
postApi: PostRestApi,
excludeContent?: boolean
): Promise<FullPost> => ({
Expand All @@ -243,7 +252,8 @@ export const getFullPost = async (
imageId: postApi.featured_media,
relatedCharts:
postApi.type === "page"
? await getPostRelatedCharts(postApi.id)
? // TODO: Instead of the nested await I think we should fetch the related charts via a join and pass them into this function already joined
await getPostRelatedCharts(trx, postApi.id)
: undefined,
})

Expand All @@ -259,7 +269,10 @@ export const getBlogIndex = memoize(
knex,
[WP_PostType.Post],
selectHomepagePosts
).then((posts) => posts.map((post) => getFullPost(post, true)))
// TODO: consider doing this as a join instead of a 1+N query
).then((posts) =>
posts.map((post) => getFullPost(knex, post, true))
)
)

const gdocSlugs = new Set(gdocPosts.map(({ slug }) => slug))
Expand Down Expand Up @@ -318,7 +331,7 @@ export const getWordpressPostReferencesByChartId = async (
): Promise<PostReference[]> => {
const relatedWordpressPosts: PostReference[] = await db.knexRaw(
knex,
`
`-- sql
SELECT DISTINCT
p.title,
p.slug,
Expand Down Expand Up @@ -366,7 +379,7 @@ export const getGdocsPostReferencesByChartId = async (
): Promise<PostReference[]> => {
const relatedGdocsPosts: PostReference[] = await db.knexRaw(
knex,
`
`-- sql
SELECT DISTINCT
pg.content ->> '$.title' AS title,
pg.slug AS slug,
Expand Down

0 comments on commit a017102

Please sign in to comment.