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 20, 2024
1 parent b53c7a5 commit 685334e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 27 deletions.
4 changes: 2 additions & 2 deletions adminSiteServer/apiRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2218,10 +2218,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 @@ -294,7 +294,7 @@ export async function renderDataPageV2(
)

datapageData.relatedResearch =
await getRelatedResearchAndWritingForVariable(variableId)
await getRelatedResearchAndWritingForVariable(knex, variableId)

const tagToSlugMap = await getTagToSlugMap()

Expand Down
56 changes: 32 additions & 24 deletions db/model/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,32 +71,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 @@ -156,6 +158,8 @@ export const getFullPostByIdFromSnapshot = async (
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 @@ -267,6 +271,7 @@ export const getBlogIndex = memoize(
knex,
[WP_PostType.Post],
selectHomepagePosts
// TODO: consider doing this as a join instead of a 1+N query
).then((posts) =>
posts.map((post) => getFullPost(knex, post, true))
)
Expand Down Expand Up @@ -328,7 +333,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 @@ -376,7 +381,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 Expand Up @@ -441,9 +446,11 @@ export const getPostTags = async (
}

export const getRelatedResearchAndWritingForVariable = async (
knex: db.KnexReadonlyTransaction,
variableId: number
): Promise<DataPageRelatedResearch[]> => {
const wp_posts: RelatedResearchQueryResult[] = await db.queryMysql(
const wp_posts: RelatedResearchQueryResult[] = await db.knexRaw(
knex,
`-- sql
-- What we want here is to get from the variable to the charts
-- to the posts and collect different pieces of information along the way
Expand Down Expand Up @@ -507,7 +514,8 @@ export const getRelatedResearchAndWritingForVariable = async (
[variableId]
)

const gdocs_posts: RelatedResearchQueryResult[] = await db.queryMysql(
const gdocs_posts: RelatedResearchQueryResult[] = await db.knexRaw(
knex,
`-- sql
select
distinct
Expand Down

0 comments on commit 685334e

Please sign in to comment.