db
This commit is contained in:
parent
10d3d05781
commit
f72005af77
29
src/db.ts
Normal file
29
src/db.ts
Normal file
@ -0,0 +1,29 @@
|
||||
import { DB } from '@/types/db';
|
||||
import { createPool } from 'mysql2';
|
||||
import { Generated, Kysely, MysqlDialect } from 'kysely';
|
||||
|
||||
const createDb = () => {
|
||||
if ((globalThis as any).db)
|
||||
return (globalThis as any).db as Kysely<GeneratedDB>;
|
||||
|
||||
const dialect = new MysqlDialect({
|
||||
pool: createPool(process.env.DATABASE_URL as string)
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV === 'production')
|
||||
delete process.env.DATABASE_URL;
|
||||
|
||||
return (globalThis as any).db = new Kysely<GeneratedDB>({ dialect });
|
||||
}
|
||||
|
||||
|
||||
type IdGenerated<C, T> = C extends 'id' ? Generated<T> : T;
|
||||
|
||||
// mark all id fields as generated
|
||||
export type GeneratedDB = {
|
||||
[Table in keyof DB]: {
|
||||
[Column in keyof DB[Table]]: IdGenerated<Column, DB[Table][Column]>
|
||||
}
|
||||
};
|
||||
|
||||
export const db = createDb();
|
3458
src/types/db.d.ts
vendored
Normal file
3458
src/types/db.d.ts
vendored
Normal file
File diff suppressed because it is too large
Load Diff
3
src/types/deep-is/index.ts
Normal file
3
src/types/deep-is/index.ts
Normal file
@ -0,0 +1,3 @@
|
||||
declare module 'deep-is' {
|
||||
export default function (a: any, b: any): boolean;
|
||||
}
|
16
src/types/next-auth/index.d.ts
vendored
Normal file
16
src/types/next-auth/index.d.ts
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
import NextAuth from 'next-auth';
|
||||
import { UserPayload } from '@/types/user';
|
||||
|
||||
declare module 'next-auth' {
|
||||
interface Session {
|
||||
user: {
|
||||
[T in keyof UserPayload]: UserPayload[T]
|
||||
},
|
||||
}
|
||||
|
||||
interface JWT {
|
||||
user: {
|
||||
[T in keyof UserPayload]: UserPayload[T]
|
||||
}
|
||||
}
|
||||
}
|
13
src/types/user.ts
Normal file
13
src/types/user.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import { AimeUser } from '@/types/db';
|
||||
|
||||
export type DBUserPayload = Omit<AimeUser, 'password'> & {
|
||||
chuni: boolean
|
||||
};
|
||||
|
||||
export type UserPayload = {
|
||||
[T in keyof DBUserPayload]: DBUserPayload[T] extends (Date | null) ? (string | null) :
|
||||
DBUserPayload[T]
|
||||
} & {
|
||||
iat: number,
|
||||
exp: number,
|
||||
};
|
Loading…
Reference in New Issue
Block a user