-
-
Notifications
You must be signed in to change notification settings - Fork 229
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrates the access to the post_links table from TypeORM to knex
- Loading branch information
Showing
3 changed files
with
139 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,92 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm" | ||
import { formatUrls } from "../../site/formatting.js" | ||
import { Url } from "@ourworldindata/utils" | ||
import { | ||
DbInsertPostLink, | ||
DbPlainPostLink, | ||
PostsLinksTableName, | ||
Url, | ||
} from "@ourworldindata/utils" | ||
import { getLinkType, getUrlTarget } from "@ourworldindata/components" | ||
import { Knex } from "knex" | ||
export function postLinkCreateFromUrl({ | ||
url, | ||
sourceId, | ||
text = "", | ||
componentType = "", | ||
}: { | ||
url: string | ||
sourceId: number | ||
text?: string | ||
componentType?: string | ||
}): Omit<DbPlainPostLink, "id"> { | ||
const formattedUrl = formatUrls(url) | ||
const urlObject = Url.fromURL(formattedUrl) | ||
const linkType = getLinkType(formattedUrl) | ||
const target = getUrlTarget(formattedUrl) | ||
const queryString = urlObject.queryStr | ||
const hash = urlObject.hash | ||
return { | ||
target, | ||
linkType, | ||
queryString, | ||
hash, | ||
sourceId, | ||
text, | ||
componentType, | ||
} | ||
} | ||
|
||
@Entity("posts_links") | ||
export class PostLink extends BaseEntity { | ||
@PrimaryGeneratedColumn() id!: number | ||
// TODO: posts is not a TypeORM but a Knex class so we can't use a TypeORM relationship here yet | ||
export async function getPostLinkById( | ||
knex: Knex<any, any[]>, | ||
id: number | ||
): Promise<DbPlainPostLink | undefined> { | ||
return knex<DbPlainPostLink>(PostsLinksTableName).where({ id }).first() | ||
} | ||
|
||
@Column({ type: "int", nullable: false }) sourceId!: number | ||
export async function getAllPostLinks( | ||
knex: Knex<any, any[]> | ||
): Promise<DbPlainPostLink[]> { | ||
return knex<DbPlainPostLink>(PostsLinksTableName) | ||
} | ||
|
||
@Column() linkType!: "gdoc" | "url" | "grapher" | "explorer" | ||
@Column() target!: string | ||
@Column() queryString!: string | ||
@Column() hash!: string | ||
@Column() componentType!: string | ||
@Column() text!: string | ||
export async function getPostLinksBySourceId( | ||
knex: Knex<any, any[]>, | ||
sourceId: number | ||
): Promise<DbPlainPostLink[]> { | ||
return knex<DbPlainPostLink>(PostsLinksTableName).where({ sourceId }) | ||
} | ||
|
||
static createFromUrl({ | ||
url, | ||
sourceId, | ||
text = "", | ||
componentType = "", | ||
}: { | ||
url: string | ||
sourceId: number | ||
text?: string | ||
componentType?: string | ||
}): PostLink { | ||
const formattedUrl = formatUrls(url) | ||
const urlObject = Url.fromURL(formattedUrl) | ||
const linkType = getLinkType(formattedUrl) | ||
const target = getUrlTarget(formattedUrl) | ||
const queryString = urlObject.queryStr | ||
const hash = urlObject.hash | ||
return PostLink.create({ | ||
target, | ||
linkType, | ||
queryString, | ||
hash, | ||
sourceId, | ||
text, | ||
componentType, | ||
}) | ||
} | ||
export async function insertPostLink( | ||
knex: Knex<any, any[]>, | ||
postLink: DbInsertPostLink | ||
): Promise<{ id: number }> { | ||
return knex(PostsLinksTableName).returning("id").insert(postLink) | ||
} | ||
|
||
export async function insertManyPostLinks( | ||
knex: Knex<any, any[]>, | ||
postLinks: DbInsertPostLink[] | ||
): Promise<void> { | ||
return knex.batchInsert(PostsLinksTableName, postLinks) | ||
} | ||
|
||
export async function updatePostLink( | ||
knex: Knex<any, any[]>, | ||
id: number, | ||
postLink: DbInsertPostLink | ||
): Promise<void> { | ||
return knex(PostsLinksTableName).where({ id }).update(postLink) | ||
} | ||
|
||
export async function deletePostLink( | ||
knex: Knex<any, any[]>, | ||
id: number | ||
): Promise<void> { | ||
return knex(PostsLinksTableName).where({ id }).delete() | ||
} | ||
|
||
export async function deleteManyPostLinks( | ||
knex: Knex<any, any[]>, | ||
ids: number[] | ||
): Promise<void> { | ||
return knex(PostsLinksTableName).whereIn("id", ids).delete() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters