Skip to content

Commit

Permalink
🔨 add exports for db types
Browse files Browse the repository at this point in the history
  • Loading branch information
danyx23 committed Jan 4, 2024
1 parent 20bd258 commit 3862ef8
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 79 deletions.
8 changes: 8 additions & 0 deletions db/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ import {
GRAPHER_DB_PORT,
} from "../settings/serverSettings.js"
import { registerExitHandler } from "./cleanup.js"
import {
PostsGdocsXImagesRow,

Check warning on line 14 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRow' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRow' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 14 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRow' is defined but never used. Allowed unused vars must match /^_/u
PostsGdocsXImagesRowTableName,

Check warning on line 15 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRowTableName' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 15 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRowTableName' is defined but never used. Allowed unused vars must match /^_/u

Check warning on line 15 in db/db.ts

View workflow job for this annotation

GitHub Actions / eslint

'PostsGdocsXImagesRowTableName' is defined but never used. Allowed unused vars must match /^_/u
} from "./sql-ts/Database.js"
let typeormDataSource: DataSource

export const getConnection = async (
Expand Down Expand Up @@ -105,6 +109,10 @@ export const knexInstance = (): Knex<any, any[]> => {
return _knexInstance
}

// export const bla = knexInstance()
// .from<PostsGdocsXImagesRow>(PostsGdocsXImagesRowTableName)
// .select("gdocId")

export const knexTable = (table: string): Knex.QueryBuilder =>
knexInstance().table(table)

Expand Down
6 changes: 6 additions & 0 deletions db/tests/basic.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,9 @@ test("timestamps are automatically created and updated", async () => {
}
}
})

test("knex interface", async () => {
if (!knexInstance) throw new Error("Knex connection not initialized")
// const result = await knexInstance<
// expect(result.rows[0].solution).toBe(2)
})
4 changes: 2 additions & 2 deletions packages/@ourworldindata/types/src/dbTypes/ChartTags.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { KeyChartLevel } from "../grapherTypes/GrapherTypes.js"
import { Tag } from "./Tags.js"
import { TagsRow } from "./Tags.js"

export const ChartTagsRowTableName = "chart_tags"
export interface ChartTagsRowForInsert {
Expand All @@ -16,5 +16,5 @@ export type ChartTagsRow = Required<ChartTagsRowForInsert>
* A common minimal union of the tags and chart_tags entities.
* Used anywhere we're using the TagBadge component.
*/
export type ChartTagJoin = Pick<Tag, "id" | "name"> &
export type ChartTagJoin = Pick<TagsRow, "id" | "name"> &
Partial<Pick<ChartTagsRow, "isApproved" | "keyChartLevel">>
6 changes: 2 additions & 4 deletions packages/@ourworldindata/types/src/dbTypes/Charts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ export function serializeChartConfig(config: GrapherInterface): JsonString {
return JSON.stringify(config)
}

export function parseChartRevisionsRow(row: ChartsRowRaw): ChartsRowEnriched {
export function parseChartsRow(row: ChartsRowRaw): ChartsRowEnriched {
return { ...row, config: parseChartConfig(row.config) }
}

export function serializeChartRevisionsRow(
row: ChartsRowEnriched
): ChartsRowRaw {
export function serializeChartsRow(row: ChartsRowEnriched): ChartsRowRaw {
return {
...row,
config: serializeChartConfig(row.config),
Expand Down
43 changes: 43 additions & 0 deletions packages/@ourworldindata/types/src/dbTypes/Posts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
} from "../gdocTypes/Gdoc.js"
import { FormattingOptions } from "../Wordpress.js"

export const PostRowTableName = "posts"
export interface PostRowPlainFields {
id: number
title: string
Expand Down Expand Up @@ -42,3 +43,45 @@ export type PostRowEnriched = Required<PostRowPlainFields & PostRowParsedFields>
export interface PostRowWithGdocPublishStatus extends PostRowRaw {
isGdocPublished: boolean
}

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 parsePostArchieml(archieml: string): any {
// TODO: validation would be nice here
return JSON.parse(archieml)
}

export function parsePostRow(postRow: PostRowRaw): PostRowEnriched {
return {
...postRow,
authors: postRow.authors ? parsePostAuthors(postRow.authors) : null,
formattingOptions: postRow.formattingOptions
? parsePostFormattingOptions(postRow.formattingOptions)
: null,
archieml: postRow.archieml ? parsePostArchieml(postRow.archieml) : null,
archieml_update_statistics: postRow.archieml_update_statistics
? JSON.parse(postRow.archieml_update_statistics)
: null,
}
}

export function serializePostRow(postRow: PostRowEnriched): PostRowRaw {
return {
...postRow,
authors: JSON.stringify(postRow.authors),
formattingOptions: JSON.stringify(postRow.formattingOptions),
archieml: JSON.stringify(postRow.archieml),
archieml_update_statistics: JSON.stringify(
postRow.archieml_update_statistics
),
}
}
44 changes: 0 additions & 44 deletions packages/@ourworldindata/types/src/dbTypes/PostsUtilities.ts

This file was deleted.

18 changes: 10 additions & 8 deletions packages/@ourworldindata/types/src/dbTypes/Tags.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/** the entity in the `tags` table */
export interface Tag {
id: number
export const TagsRowTableName = "tags"
export interface TagsRowForInsert {
createdAt?: Date
id?: number
isBulkImport?: number
name: string
createdAt: Date
updatedAt: Date
parentId: number
isBulkImport: boolean
specialType: string
slug: string | null
parentId?: number | null
slug?: string | null
specialType?: string | null
updatedAt?: Date | null
}
export type TagsRow = Required<TagsRowForInsert>
10 changes: 10 additions & 0 deletions packages/@ourworldindata/types/src/domainTypes/Tag.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Tag {
id: number
name: string
createdAt: Date
updatedAt: Date
parentId: number
isBulkImport: boolean
specialType: string
slug: string | null
}
2 changes: 1 addition & 1 deletion packages/@ourworldindata/types/src/gdocTypes/Gdoc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tag } from "../dbTypes/Tags.js"
import { Tag } from "../domainTypes/Tag.js"
import { RelatedChart } from "../grapherTypes/GrapherTypes.js"
import { BreadcrumbItem } from "../domainTypes/Site.js"
import { TocHeadingWithTitleSupertitle } from "../domainTypes/Toc.js"
Expand Down
Loading

0 comments on commit 3862ef8

Please sign in to comment.