Skip to content

Commit

Permalink
🚧 remove most typeorm classes, start work on Links to turn them into …
Browse files Browse the repository at this point in the history
…objects
  • Loading branch information
danyx23 committed Mar 6, 2024
1 parent 0833773 commit 47f72ea
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 223 deletions.
60 changes: 30 additions & 30 deletions db/model/Chart.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
ManyToOne,
OneToMany,
type Relation,
} from "typeorm"
// import {
// Entity,
// PrimaryGeneratedColumn,
// Column,
// BaseEntity,
// ManyToOne,
// OneToMany,
// type Relation,
// } from "typeorm"
import * as lodash from "lodash"
import * as db from "../db.js"
import { getDataForMultipleVariables } from "./Variable.js"
import { User } from "./User.js"
import { ChartRevision } from "./ChartRevision.js"
// import { User } from "./User.js"
// import { ChartRevision } from "./ChartRevision.js"
import {
JsonError,
KeyChartLevel,
Expand Down Expand Up @@ -42,25 +42,25 @@ export const PUBLIC_TAG_PARENT_IDS = [
1505, 1508, 1512, 1510, 1834, 1835,
]

@Entity("charts")
export class Chart extends BaseEntity {
@PrimaryGeneratedColumn() id!: number
@Column({ type: "json" }) config!: GrapherInterface
@Column() lastEditedAt!: Date
@Column() lastEditedByUserId!: number
@Column({ nullable: true }) publishedAt!: Date
@Column({ nullable: true }) publishedByUserId!: number
@Column() createdAt!: Date
@Column() updatedAt!: Date
@Column() isExplorable!: boolean

@ManyToOne(() => User, (user) => user.lastEditedCharts)
lastEditedByUser!: Relation<User>
@ManyToOne(() => User, (user) => user.publishedCharts)
publishedByUser!: Relation<User>
@OneToMany(() => ChartRevision, (rev) => rev.chart)
logs!: Relation<ChartRevision[]>
}
// @Entity("charts")
// export class Chart extends BaseEntity {
// @PrimaryGeneratedColumn() id!: number
// @Column({ type: "json" }) config!: GrapherInterface
// @Column() lastEditedAt!: Date
// @Column() lastEditedByUserId!: number
// @Column({ nullable: true }) publishedAt!: Date
// @Column({ nullable: true }) publishedByUserId!: number
// @Column() createdAt!: Date
// @Column() updatedAt!: Date
// @Column() isExplorable!: boolean

// @ManyToOne(() => User, (user) => user.lastEditedCharts)
// lastEditedByUser!: Relation<User>
// @ManyToOne(() => User, (user) => user.publishedCharts)
// publishedByUser!: Relation<User>
// @OneToMany(() => ChartRevision, (rev) => rev.chart)
// logs!: Relation<ChartRevision[]>
// }
// Only considers published charts, because only in that case the mapping slug -> id is unique
export async function mapSlugsToIds(
knex: db.KnexReadonlyTransaction
Expand Down
46 changes: 23 additions & 23 deletions db/model/ChartRevision.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
ManyToOne,
type Relation,
} from "typeorm"
import { Chart } from "./Chart.js"
import { User } from "./User.js"
// import {
// Entity,
// PrimaryGeneratedColumn,
// Column,
// BaseEntity,
// ManyToOne,
// type Relation,
// } from "typeorm"
// // import { Chart } from "./Chart.js"
// import { User } from "./User.js"

@Entity("chart_revisions")
export class ChartRevision extends BaseEntity {
@PrimaryGeneratedColumn() id!: number
@Column() chartId!: number
@Column({ type: "json" }) config: any
@Column() userId!: number
// @Entity("chart_revisions")
// export class ChartRevision extends BaseEntity {
// @PrimaryGeneratedColumn() id!: number
// @Column() chartId!: number
// @Column({ type: "json" }) config: any
// @Column() userId!: number

@Column() createdAt!: Date
@Column() updatedAt!: Date
// @Column() createdAt!: Date
// @Column() updatedAt!: Date

@ManyToOne(() => User, (user) => user.editedCharts)
user!: Relation<User>
// @ManyToOne(() => User, (user) => user.editedCharts)
// user!: Relation<User>

@ManyToOne(() => Chart, (chart) => chart.logs)
chart!: Relation<Chart>
}
// @ManyToOne(() => Chart, (chart) => chart.logs)
// chart!: Relation<Chart>
// }
46 changes: 18 additions & 28 deletions db/model/Dataset.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
ManyToOne,
Unique,
type Relation,
} from "typeorm"
import { Writable } from "stream"

import { User } from "./User.js"
import { getSourcesForDataset, sourceToDatapackage } from "./Source.js"

import * as db from "../db.js"
Expand All @@ -23,25 +13,25 @@ import {
DbRawVariable,
} from "@ourworldindata/types"

@Entity("datasets")
@Unique(["name", "namespace"])
export class Dataset extends BaseEntity {
@PrimaryGeneratedColumn() id!: number
@Column() name!: string
@Column({ default: "owid" }) namespace!: string
@Column({ default: "" }) description!: string
@Column() createdAt!: Date
@Column() updatedAt!: Date
@Column() metadataEditedAt!: Date
@Column() metadataEditedByUserId!: number
@Column() dataEditedAt!: Date
@Column() dataEditedByUserId!: number
@Column({ default: false }) isPrivate!: boolean
@Column({ default: false }) nonRedistributable!: boolean
// @Entity("datasets")
// @Unique(["name", "namespace"])
// export class Dataset extends BaseEntity {
// @PrimaryGeneratedColumn() id!: number
// @Column() name!: string
// @Column({ default: "owid" }) namespace!: string
// @Column({ default: "" }) description!: string
// @Column() createdAt!: Date
// @Column() updatedAt!: Date
// @Column() metadataEditedAt!: Date
// @Column() metadataEditedByUserId!: number
// @Column() dataEditedAt!: Date
// @Column() dataEditedByUserId!: number
// @Column({ default: false }) isPrivate!: boolean
// @Column({ default: false }) nonRedistributable!: boolean

@ManyToOne(() => User, (user) => user.createdDatasets)
createdByUser!: Relation<User>
}
// @ManyToOne(() => User, (user) => user.createdDatasets)
// createdByUser!: Relation<User>
// }

export async function getDatasetById(
knex: db.KnexReadonlyTransaction,
Expand Down
14 changes: 7 additions & 7 deletions db/model/GdocXImage.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"
// import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"

@Entity("posts_gdocs_x_images")
export class GdocXImage extends BaseEntity {
@PrimaryGeneratedColumn() id!: number
@Column() gdocId!: string
@Column() imageId!: number
}
// @Entity("posts_gdocs_x_images")
// export class GdocXImage extends BaseEntity {
// @PrimaryGeneratedColumn() id!: number
// @Column() gdocId!: string
// @Column() imageId!: number
// }
14 changes: 7 additions & 7 deletions db/model/GdocXTag.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Entity, BaseEntity, PrimaryColumn } from "typeorm"
// import { Entity, BaseEntity, PrimaryColumn } from "typeorm"

@Entity("posts_gdocs_x_tags")
export class GdocXTag extends BaseEntity {
static table = "posts_gdocs_x_tags"
@PrimaryColumn() gdocId!: string
@PrimaryColumn() tagId!: number
}
// @Entity("posts_gdocs_x_tags")
// export class GdocXTag extends BaseEntity {
// static table = "posts_gdocs_x_tags"
// @PrimaryColumn() gdocId!: string
// @PrimaryColumn() tagId!: number
// }
37 changes: 5 additions & 32 deletions db/model/Image.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
import {
Entity,
Column,
BaseEntity,
PrimaryGeneratedColumn,
ValueTransformer,
} from "typeorm"
import { drive_v3, google } from "googleapis"
import {
PutObjectCommand,
Expand Down Expand Up @@ -133,32 +126,12 @@ const s3Client = new S3Client({
secretAccessKey: IMAGE_HOSTING_SPACE_SECRET_ACCESS_KEY,
},
})

// https://github.com/typeorm/typeorm/issues/873#issuecomment-424643086
// otherwise epochs are retrieved as string instead of number
export class ColumnNumericTransformer implements ValueTransformer {
to(data: number): number {
return data
}
from(data: string): number {
return parseFloat(data)
}
}

@Entity("images")
export class Image extends BaseEntity implements ImageMetadata {
@PrimaryGeneratedColumn() id!: number
@Column() googleId!: string
@Column() filename!: string
@Column() defaultAlt!: string
@Column({
transformer: new ColumnNumericTransformer(),
})
export class Image implements ImageMetadata {
id!: number
googleId!: string
filename!: string
defaultAlt!: string
updatedAt!: number
@Column({
transformer: new ColumnNumericTransformer(),
nullable: true,
})
originalWidth?: number

get isSvg(): boolean {
Expand Down
106 changes: 49 additions & 57 deletions db/model/Link.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,56 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
BaseEntity,
ManyToOne,
type Relation,
In,
} from "typeorm"
import { getLinkType, getUrlTarget } from "@ourworldindata/components"
import { OwidGdocLinkJSON, Url } from "@ourworldindata/utils"
import { GdocBase } from "./Gdoc/GdocBase.js"
import { formatUrls } from "../../site/formatting.js"
import { OwidGdocLinkType } from "@ourworldindata/types"

@Entity("posts_gdocs_links")
export class Link extends BaseEntity implements OwidGdocLinkJSON {
@PrimaryGeneratedColumn() id!: number
@ManyToOne(() => GdocBase, (gdoc) => gdoc.id) source!: Relation<GdocBase>
@Column() linkType!: OwidGdocLinkType
@Column() target!: string
@Column() queryString!: string
@Column() hash!: string
@Column() componentType!: string
@Column() text!: string
import {
DbInsertPostGdocLink,
DbPlainPostGdocLink,
OwidGdocLinkType,
} from "@ourworldindata/types"
import { KnexReadonlyTransaction, knexRaw } from "../db.js"

static async getPublishedLinksTo(
ids: string[],
linkType?: Link["linkType"]
): Promise<Link[]> {
return Link.find({
where: { target: In(ids), linkType },
relations: ["source"],
}).then((links) => links.filter((link) => link.source.published))
}
export async function getPublishedLinksTo(
knex: KnexReadonlyTransaction,
ids: string[],
linkType?: OwidGdocLinkType
): Promise<DbPlainPostGdocLink[]> {
const rows = await knexRaw<DbPlainPostGdocLink>(
knex,
`-- sql
select posts_gdocs_links.* from posts_gdocs_links
join posts_gdocs on posts_gdocs_links.source = posts_gdocs.id
where target in (?) and linkType = ?
and published = true
`,
[ids, linkType]
)
return rows
}

static createFromUrl({
url,
source,
text = "",
componentType = "",
}: {
url: string
source: GdocBase
text?: string
componentType?: string
}): Link {
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 Link.create({
target,
linkType,
queryString,
hash,
source,
text,
componentType,
})
}
export function createFromUrl({
url,
source,
text = "",
componentType = "",
}: {
url: string
source: GdocBase
text?: string
componentType?: string
}): DbInsertPostGdocLink {
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,
text,
componentType,
sourceId: source.id,
} satisfies DbInsertPostGdocLink
}
Loading

0 comments on commit 47f72ea

Please sign in to comment.