-
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: mikroorm boilerplate and example models
- Loading branch information
Showing
24 changed files
with
715 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,8 @@ | |
"email": "[email protected]" | ||
}, | ||
"dependencies": { | ||
"@mikro-orm/core": "^6.3.13", | ||
"@mikro-orm/postgresql": "^6.3.13", | ||
"@pm2/io": "^5.0.0", | ||
"compression": "^1.7.4", | ||
"cors": "^2.8.5", | ||
|
@@ -43,6 +45,7 @@ | |
}, | ||
"devDependencies": { | ||
"@changesets/cli": "^2.27.1", | ||
"@mikro-orm/cli": "^6.3.13", | ||
"@types/compression": "^1.7.2", | ||
"@types/cors": "^2.8.12", | ||
"@types/express": "^4.17.14", | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import type { Stringifiable } from "@/types/index.js"; | ||
import "dotenv/config"; | ||
|
||
/** | ||
* Grabs an environment variable's value, and casts it to a `string` (or what's passed in the TRetVal generic). | ||
* However if the string is empty (or unset), undefined is returned. | ||
*/ | ||
export function getEnvVar<TRetVal extends string>(varName: string, asType?: "stringNoEmpty"): undefined | TRetVal | ||
/** Grabs an environment variable's value, and casts it to a `string` (or what's passed in the TRetVal generic) */ | ||
export function getEnvVar<TRetVal extends string>(varName: string, asType?: "string"): undefined | TRetVal | ||
/** Grabs an environment variable's value, and casts it to a `number` (or what's passed in the TRetVal generic) */ | ||
export function getEnvVar<TRetVal extends number>(varName: string, asType: "number"): undefined | TRetVal | ||
/** Grabs an environment variable's value, and casts it to a `string[]` (or what's passed in the TRetVal generic) */ | ||
export function getEnvVar<TRetVal extends string[]>(varName: string, asType: "stringArray"): undefined | TRetVal | ||
/** Grabs an environment variable's value, and casts it to a `number[]` (or what's passed in the TRetVal generic) */ | ||
export function getEnvVar<TRetVal extends number[]>(varName: string, asType: "numberArray"): undefined | TRetVal | ||
/** Grabs an environment variable's value, and casts it to a specific type (stringNoEmpty by default) */ | ||
export function getEnvVar< | ||
T extends ("string" | "number" | "stringArray" | "numberArray" | "stringNoEmpty") | ||
>( | ||
varName: string, | ||
asType: T = "stringNoEmpty" as T, | ||
): undefined | (string | number | string[] | number[]) { | ||
const val = process.env[varName]; | ||
|
||
if(!val) | ||
return undefined; | ||
|
||
let transform: (value: string) => unknown = v => v.trim(); | ||
|
||
const commasRegex = /[,،,٫٬]/g; | ||
|
||
switch(asType) { | ||
case "number": | ||
transform = v => parseInt(v.trim()); | ||
break; | ||
case "stringArray": | ||
transform = v => v.trim().split(commasRegex); | ||
break; | ||
case "numberArray": | ||
transform = v => v.split(commasRegex).map(n => parseInt(n.trim())); | ||
break; | ||
case "stringNoEmpty": | ||
transform = v => String(v).trim().length == 0 ? undefined : String(v).trim(); | ||
} | ||
|
||
return transform(val) as string; // I'm lazy and ts is happy, so we can all be happy and pretend this doesn't exist | ||
} | ||
|
||
/** | ||
* Tests if the value of the environment variable {@linkcode varName} equals {@linkcode compareValue} casted to string. | ||
* Set {@linkcode caseSensitive} to true to make the comparison case-sensitive. | ||
*/ | ||
export function envVarEquals(varName: string, compareValue: Stringifiable, caseSensitive = false) { | ||
const envVal = (caseSensitive ? getEnvVar(varName) : getEnvVar(varName)?.toLowerCase()); | ||
const compVal = (caseSensitive ? String(compareValue) : String(compareValue).toLowerCase()); | ||
return envVal === compVal; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { defineConfig } from "@mikro-orm/core"; | ||
import { MikroORM } from "@mikro-orm/postgresql"; | ||
import { envVarEquals, getEnvVar } from "@lib/env.js"; | ||
|
||
const config = defineConfig({ | ||
clientUrl: getEnvVar("DATABASE_URL", "stringNoEmpty"), | ||
charset: "utf8", | ||
entities: ["dist/**/*.model.js"], | ||
entitiesTs: ["src/**/*.model.ts"], | ||
debug: envVarEquals("DATABASE_DEBUG", true), | ||
}); | ||
|
||
/** MikroORM instance */ | ||
export let orm: Awaited<ReturnType<typeof MikroORM.init>>; | ||
/** EntityManager instance */ | ||
export let em: typeof orm.em; | ||
|
||
/** Load MikroORM instances */ | ||
export async function initDatabase() { | ||
orm = await MikroORM.init(config); | ||
em = orm.em.fork(); | ||
|
||
// run migrations | ||
try { | ||
await orm.getSchemaGenerator().updateSchema(); | ||
} | ||
catch(e) { | ||
console.error("Error running migrations:", e); | ||
setImmediate(() => process.exit(1)); | ||
} | ||
} | ||
|
Empty file.
Empty file.
Empty file.
Empty file.
Empty file.
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { em } from "@/mikro-orm.config.js"; | ||
import { Entity, ManyToOne, PrimaryKey, Property } from "@mikro-orm/core"; | ||
import { User } from "@models/User.model.js"; | ||
|
||
export type ReportNoteProps = { | ||
text: string; | ||
authorId: string; | ||
}; | ||
|
||
@Entity() | ||
export class ReportNote { | ||
constructor(props: ReportNoteProps) { | ||
this.text = props.text; | ||
this.authorId = props.authorId; | ||
this.createdAt = this.updatedAt = new Date(); | ||
} | ||
|
||
@PrimaryKey({ type: "string", generated: "uuid" }) | ||
id!: string; | ||
|
||
@Property({ type: "string", length: 255 }) | ||
text: string; | ||
|
||
@Property({ type: "string" }) | ||
authorId: string; | ||
|
||
@ManyToOne({ entity: () => User }) | ||
get author(): User { | ||
return em.getReference(User, this.authorId); | ||
} | ||
|
||
@Property({ type: "date" }) | ||
createdAt: Date; | ||
|
||
@Property({ type: "date", onUpdate: () => new Date() }) | ||
updatedAt: Date; | ||
|
||
@Property({ type: "date", nullable: true }) | ||
deletedAt?: Date; | ||
} |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { Entity, PrimaryKey, Property } from "@mikro-orm/core"; | ||
import type { AccountTier, UserRole } from "@/types/user.js"; | ||
|
||
export type UserProps = { | ||
username: string; | ||
email: string; | ||
tier: AccountTier; | ||
roles: UserRole[]; | ||
}; | ||
|
||
@Entity() | ||
export class User { | ||
constructor(props: UserProps) { | ||
this.username = props.username; | ||
this.email = props.email; | ||
this.createdAt = this.updatedAt = new Date(); | ||
} | ||
|
||
@PrimaryKey({ type: "string", generated: "uuid" }) | ||
id!: string; | ||
|
||
@PrimaryKey({ type: "string", length: 24 }) | ||
username: string; | ||
|
||
@Property({ type: "string", length: 320 }) | ||
email: string; | ||
|
||
@Property({ type: "string", length: 16 }) | ||
tier: AccountTier = "free"; | ||
|
||
@Property({ type: "array", length: 16, runtimeType: "string" }) | ||
roles = ["user"]; | ||
|
||
// TODO: | ||
|
||
// @OneToOne({ entity: () => UserSettings, orphanRemoval: true }) | ||
// settings: UserSettings; | ||
|
||
// @OneToOne({ entity: () => UserBilling, orphanRemoval: true }) | ||
// billing: Billing; | ||
|
||
// @OneToMany({ entity: () => ReportNote, mappedBy: "author" }) | ||
// connections: Connection[]; | ||
|
||
@Property({ type: "date" }) | ||
createdAt: Date; | ||
|
||
@Property({ type: "date", onUpdate: () => new Date() }) | ||
updatedAt: Date; | ||
|
||
@Property({ type: "date", nullable: true }) | ||
deletedAt?: Date; | ||
} |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export * from "./Joke.model.js"; | ||
export * from "./Submission.model.js"; | ||
export * from "./User.model.js"; | ||
export * from "./UserSettings.model.js"; | ||
export * from "./Billing.model.js"; | ||
export * from "./Invoice.model.js"; | ||
export * from "./Connection.model.js"; | ||
export * from "./Report.model.js"; | ||
export * from "./ReportNote.model.js"; |
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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,3 +1,3 @@ | ||
export * from "./misc"; | ||
|
||
export * from "./jokeapi/index"; | ||
export * from "./jokeapi/index.js"; | ||
export * from "./misc.js"; | ||
export * from "./user.js"; |
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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export type UserRole = "user" | "moderator" | "admin"; | ||
|
||
export type AccountTier = "free" | "premium" | "enterprise"; |
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