Created
September 17, 2019 18:43
-
-
Save char/fbcaf6ce53c0c90b27b78f3774d2a10b to your computer and use it in GitHub Desktop.
TypeORM
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
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; | |
import { EndUser } from "./EndUser"; | |
@Entity() | |
export class AccessLogEntry { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public details: string; | |
@ManyToOne(type => EndUser, endUser => endUser.accesses) | |
public endUser: Promise<AccessLogEntry[]>; | |
} | |
export interface AccessLogDetails { | |
ip: string; | |
artifact?: string; | |
artifact_file_identifier?: string; | |
} |
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
import crypto from "crypto"; | |
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; | |
import { User } from "./User"; | |
@Entity() | |
export class APIKey { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public identifier: string; | |
@Column({ unique: true }) | |
public token: string; | |
@ManyToOne(type => User, user => user.apiKeys) | |
public owner: Promise<User>; | |
} | |
export function generateToken() { | |
return crypto.randomBytes(32).toString("hex"); | |
} |
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
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; | |
import { Artifact } from "./Artifact"; | |
import { EndUser } from "./EndUser"; | |
import { User } from "./User"; | |
@Entity() | |
export class Application { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public identifier: string; | |
@ManyToOne(type => User, user => user.applications) | |
public owner: Promise<User>; | |
@OneToMany(type => Artifact, artifact => artifact.application) | |
public artifacts: Promise<Artifact[]>; | |
@OneToMany(type => EndUser, endUser => endUser.application) | |
public endUsers: Promise<EndUser[]>; | |
} |
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
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; | |
import { Application } from "./Application"; | |
import { StoredArtifactFile } from "./StoredArtifactFile"; | |
@Entity() | |
export class Artifact { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public identifier: string; | |
@ManyToOne(type => Application, application => application.artifacts) | |
public application: Promise<Application>; | |
@OneToMany(type => StoredArtifactFile, storedFile => storedFile.artifact) | |
public storedFiles: Promise<StoredArtifactFile[]>; | |
} |
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
import crypto from "crypto"; | |
import { Column, Entity, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm"; | |
import { AccessLogEntry } from "./AccessLogEntry"; | |
import { Application } from "./Application"; | |
@Entity() | |
export class EndUser { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public identifier: string; | |
@Column({ unique: true }) | |
public token: string; | |
@Column({ name: "secret_token", unique: true }) | |
public secretToken: string; | |
@ManyToOne(type => Application, application => application.artifacts) | |
public application: Promise<Application>; | |
@OneToMany(type => AccessLogEntry, access => access.endUser) | |
public accesses: Promise<AccessLogEntry[]>; | |
} | |
export function generateToken() { | |
return crypto.randomBytes(32).toString("hex"); | |
} | |
export function generateSecretToken() { | |
return crypto.randomBytes(128).toString("hex"); | |
} |
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
import { Column, Entity, PrimaryGeneratedColumn } from "typeorm"; | |
@Entity() | |
export class PendingUserRegistration { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column({ name: "email_address", unique: true }) | |
public emailAddress: string; | |
@Column({ name: "password_hash" }) | |
public passwordHash: string; | |
@Column({ name: "activation_token" }) | |
public activationToken: string; | |
} |
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
import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; | |
import { Artifact } from "./Artifact"; | |
@Entity() | |
export class StoredArtifactFile { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column() | |
public identifier: string; | |
@Column({ name: "storage_location" }) | |
public storageLocation: string; | |
@ManyToOne(type => Artifact, artifact => artifact.storedFiles) | |
public artifact: Promise<Artifact>; | |
} |
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
import { Column, Entity, OneToMany, PrimaryGeneratedColumn } from "typeorm"; | |
import { APIKey } from "./APIKey"; | |
import { Application } from "./Application"; | |
@Entity() | |
export class User { | |
@PrimaryGeneratedColumn() | |
public id: number; | |
@Column({ name: "email_address", unique: true }) | |
public emailAddress: string; | |
@Column({ name: "password_hash" }) | |
public passwordHash: string; | |
@Column("decimal", { default: 0, precision: 8, scale: 2 }) | |
public balance: number; | |
@Column({ name: "auth_token", nullable: true, unique: true }) | |
public authToken?: string; | |
@OneToMany(type => APIKey, apiKey => apiKey.owner) | |
public apiKeys: Promise<APIKey[]>; | |
@OneToMany(type => Application, application => application.owner) | |
public applications: Promise<Application[]>; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment