Skip to content

Commit

Permalink
🐝 move Post types to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Dec 15, 2023
1 parent d5700dd commit 3717429
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 65 deletions.
31 changes: 31 additions & 0 deletions packages/@ourworldindata/utils/src/dbTypes/Posts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FormattingOptions, WP_PostType } from "../owidTypes.js"

export interface PostRowPlainFields {
id: number
title: string
slug: string
type: WP_PostType
status: string
content: string
published_at: Date | null
updated_at: Date | null
updated_at_in_wordpress: Date | null
archieml: string
archieml_update_statistics: string
gdocSuccessorId: string | null
excerpt: string
created_at_in_wordpress: Date | null
featured_image: string
}

export interface PostRowUnparsedFields {
authors: string
formattingOptions: string
}

export interface PostRowParsedFields {
authors: string[]
formattingOptions: FormattingOptions
}
export type PostRowRaw = PostRowPlainFields & PostRowUnparsedFields
export type PostRowEnriched = PostRowPlainFields & PostRowParsedFields
31 changes: 31 additions & 0 deletions packages/@ourworldindata/utils/src/dbTypes/PostsUtilities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { FormattingOptions } from "../owidTypes.js"
import { PostRowEnriched, PostRowRaw } from "./Posts.js"

export function parsePostFormattingOptions(
formattingOptions: string
): FormattingOptions {
return JSON.parse(formattingOptions)
}

export function parsePostAuthors(authors: string): string[] {
const authorsJson = JSON.parse(authors)
return authorsJson
}

export function parsePostRow(postRow: PostRowRaw): PostRowEnriched {
return {
...postRow,
authors: parsePostAuthors(postRow.authors),
formattingOptions: parsePostFormattingOptions(
postRow.formattingOptions
),
}
}

export function serializePostRow(postRow: PostRowEnriched): PostRowRaw {
return {
...postRow,
authors: JSON.stringify(postRow.authors),
formattingOptions: JSON.stringify(postRow.formattingOptions),
}
}
18 changes: 12 additions & 6 deletions packages/@ourworldindata/utils/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ export {
type PositionMap,
type PostReference,
type PostRestApi,
type PostRowEnriched,
type PostRowRaw,
type PrimitiveType,
type RawBlockAllCharts,
type RawBlockAdditionalCharts,
Expand Down Expand Up @@ -238,14 +236,22 @@ export {
type RawBlockAlign,
type BreadcrumbItem,
type DisplaySource,
type PostParsedFields,
type PostPlainFields,
type PostUnparsedFields,
} from "./owidTypes.js"

export {
parsePostFormattingOptions,
parsePostAuthors,
parsePostRow,
serializePostRow,
} from "./owidTypes.js"
} from "./dbTypes/PostsUtilities.js"

export {
type PostRowParsedFields,
type PostRowPlainFields,
type PostRowUnparsedFields,
type PostRowEnriched,
type PostRowRaw,
} from "./dbTypes/Posts.js"

export {
pairs,
Expand Down
60 changes: 1 addition & 59 deletions packages/@ourworldindata/utils/src/owidTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { gdocUrlRegex } from "./GdocsConstants.js"
import { OwidOrigin } from "./OwidOrigin.js"
import { OwidSource } from "./OwidSource.js"
import { OwidProcessingLevel } from "./OwidVariable.js"
import { PostRowEnriched } from "./dbTypes/Posts.js"

// todo: remove when we ditch Year and YearIsDay
export const EPOCH_DATE = "2020-01-21"
Expand Down Expand Up @@ -165,65 +166,6 @@ export interface TocHeadingWithTitleSupertitle extends TocHeading {
supertitle?: string
}

export interface PostPlainFields {
id: number
title: string
slug: string
type: WP_PostType
status: string
content: string
published_at: Date | null
updated_at: Date | null
updated_at_in_wordpress: Date | null
archieml: string
archieml_update_statistics: string
gdocSuccessorId: string | null
excerpt: string
created_at_in_wordpress: Date | null
featured_image: string
}

export interface PostUnparsedFields {
authors: string
formattingOptions: string
}

export interface PostParsedFields {
authors: string[]
formattingOptions: FormattingOptions
}
export type PostRowRaw = PostPlainFields & PostUnparsedFields
export type PostRowEnriched = PostPlainFields & PostParsedFields

export function parsePostFormattingOptions(
formattingOptions: string
): FormattingOptions {
return JSON.parse(formattingOptions)
}

export function parsePostAuthors(authors: string): string[] {
const authorsJson = JSON.parse(authors)
return authorsJson
}

export function parsePostRow(postRow: PostRowRaw): PostRowEnriched {
return {
...postRow,
authors: parsePostAuthors(postRow.authors),
formattingOptions: parsePostFormattingOptions(
postRow.formattingOptions
),
}
}

export function serializePostRow(postRow: PostRowEnriched): PostRowRaw {
return {
...postRow,
authors: JSON.stringify(postRow.authors),
formattingOptions: JSON.stringify(postRow.formattingOptions),
}
}

export interface PostRowWithGdocPublishStatus extends PostRowEnriched {
isGdocPublished: boolean
}
Expand Down

0 comments on commit 3717429

Please sign in to comment.