Skip to content

Commit

Permalink
refactor(wp): add parsePostWpApiSnapshot function
Browse files Browse the repository at this point in the history
  • Loading branch information
mlbrgl committed Feb 19, 2024
1 parent 35e8687 commit e93d1ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
12 changes: 7 additions & 5 deletions db/model/Post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
DbRawPost,
DbEnrichedPost,
parsePostRow,
parsePostWpApiSnapshot,
FullPost,
JsonError,
CategoryWithEntries,
Expand Down Expand Up @@ -154,11 +155,12 @@ export const getPostsFromSnapshots = async (
postTypes: string[] = [WP_PostType.Post, WP_PostType.Page],
filterFunc?: FilterFnPostRestApi
): Promise<PostRestApi[]> => {
const rawPosts: DbRawPost[] = (
const rawPosts: Pick<DbRawPost, "wpApiSnapshot">[] = (
await db.knexInstance().raw(
`
SELECT * FROM ${postsTable}
WHERE status = "publish"
SELECT wpApiSnapshot FROM ${postsTable}
WHERE wpApiSnapshot IS NOT NULL
AND status = "publish"
AND type IN (?)
ORDER BY wpApiSnapshot->>'$.date' DESC;
`,
Expand All @@ -167,9 +169,9 @@ export const getPostsFromSnapshots = async (
)[0]

const posts = rawPosts
.map(parsePostRow)
.map((p) => p.wpApiSnapshot)
.filter((p) => p !== null) as PostRestApi[]
.filter((snapshot) => snapshot !== null)
.map((snapshot) => parsePostWpApiSnapshot(snapshot!))

// Published pages excluded from public views
const excludedSlugs = [BLOG_SLUG]
Expand Down
6 changes: 5 additions & 1 deletion packages/@ourworldindata/types/src/dbTypes/Posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ export function parsePostArchieml(archieml: string): any {
return JSON.parse(archieml)
}

export function parsePostWpApiSnapshot(wpApiSnapshot: string): PostRestApi {
return JSON.parse(wpApiSnapshot)
}

export function parsePostRow(postRow: DbRawPost): DbEnrichedPost {
return {
...postRow,
Expand All @@ -78,7 +82,7 @@ export function parsePostRow(postRow: DbRawPost): DbEnrichedPost {
? JSON.parse(postRow.archieml_update_statistics)
: null,
wpApiSnapshot: postRow.wpApiSnapshot
? JSON.parse(postRow.wpApiSnapshot)
? parsePostWpApiSnapshot(postRow.wpApiSnapshot)
: null,
}
}
Expand Down
2 changes: 2 additions & 0 deletions packages/@ourworldindata/types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ export {
WP_ColumnStyle,
WP_PostType,
type PostRestApi,
type BlockGraphQlApi,
type FilterFnPostRestApi,
type FormattingOptions,
SubNavId,
Expand Down Expand Up @@ -499,6 +500,7 @@ export {
parsePostFormattingOptions,
parsePostAuthors,
parsePostRow,
parsePostWpApiSnapshot,
serializePostRow,
parsePostArchieml,
snapshotIsPostRestApi,
Expand Down

0 comments on commit e93d1ba

Please sign in to comment.