From b1db3a100b19ca573c8a4ec6058ebdfa594d249e Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Fri, 1 Mar 2024 19:05:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20clean=20up=20posts=20access=20vi?= =?UTF-8?q?a=20knex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteServer/apiRouter.ts | 4 +-- baker/GrapherBaker.tsx | 2 +- db/model/Post.ts | 56 ++++++++++++++++++++---------------- 3 files changed, 35 insertions(+), 27 deletions(-) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index af9c73756de..50f2e8692af 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -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 } }) diff --git a/baker/GrapherBaker.tsx b/baker/GrapherBaker.tsx index 10682d74b24..ba3b80c54c3 100644 --- a/baker/GrapherBaker.tsx +++ b/baker/GrapherBaker.tsx @@ -294,7 +294,7 @@ export async function renderDataPageV2( ) datapageData.relatedResearch = - await getRelatedResearchAndWritingForVariable(variableId) + await getRelatedResearchAndWritingForVariable(knex, variableId) const tagToSlugMap = await getTagToSlugMap() diff --git a/db/model/Post.ts b/db/model/Post.ts index bfd96d7e19b..cbb317f8d6b 100644 --- a/db/model/Post.ts +++ b/db/model/Post.ts @@ -71,32 +71,34 @@ export const getTagsByPostId = async ( } export const setTags = async ( + trx: db.KnexReadWriteTransaction, postId: number, tagIds: number[] -): Promise => - 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 => { + 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 => - 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 => { + 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, @@ -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) => { @@ -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)) ) @@ -328,7 +333,7 @@ export const getWordpressPostReferencesByChartId = async ( ): Promise => { const relatedWordpressPosts: PostReference[] = await db.knexRaw( knex, - ` + `-- sql SELECT DISTINCT p.title, p.slug, @@ -376,7 +381,7 @@ export const getGdocsPostReferencesByChartId = async ( ): Promise => { const relatedGdocsPosts: PostReference[] = await db.knexRaw( knex, - ` + `-- sql SELECT DISTINCT pg.content ->> '$.title' AS title, pg.slug AS slug, @@ -441,9 +446,11 @@ export const getPostTags = async ( } export const getRelatedResearchAndWritingForVariable = async ( + knex: db.KnexReadonlyTransaction, variableId: number ): Promise => { - 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 @@ -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