Skip to content

Commit

Permalink
relocate prisma and lucia config
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Aug 1, 2023
1 parent 73034b4 commit 6061506
Show file tree
Hide file tree
Showing 6 changed files with 312 additions and 4 deletions.
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@
"name": "wanderer-moe-api",
"version": "1.0.1b",
"scripts": {
"prettier": "prettier --write .",
"prettier:fmt": "prettier --write .",
"dev": "wrangler dev --remote",
"publish": "wrangler publish --minify",
"lint": "eslint . --ext .ts",
"prettier:check": "prettier --check .",
"typecheck": "tsc --noEmit"
"typecheck": "tsc --noEmit",
"prisma:generate": "prisma generate",
"prisma:push": "prisma db push"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20230710.1",
Expand All @@ -19,11 +21,14 @@
},
"private": true,
"dependencies": {
"@lucia-auth/adapter-prisma": "^3.0.0",
"@planetscale/database": "^1.8.0",
"@prisma/client": "^5.0.0",
"@typescript-eslint/eslint-plugin": "^6.0.0",
"itty-router": "^4.0.14",
"lucia": "^2.0.0",
"prettier": "^3.0.0",
"prisma": "^5.1.0",
"render2": "^1.2.1"
}
}
54 changes: 52 additions & 2 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

189 changes: 189 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

generator client {
provider = "prisma-client-js"
previewFeatures = ["fullTextSearch", "fullTextIndex"]
}

model User {
id String @id @unique
avatar_url String?
banner_url String?
username String @unique
username_colour String?
email String @unique
email_verified Int @default(0)
pronouns String?
verified Int @default(0)
bio String? @default("")
assets Assets[]
date_joined DateTime
role RoleNames @default(USER)
self_assignable_tags SelfAssignableTag[]
socials_connections SocialsConnection[]
saved_oc_generators SavedOCGenerators[]
auth_session Session[]
auth_key Key[]
// relations
followers Follower[] @relation("FollowerToUser")
following Following[] @relation("FollowingToUser")
}

model Session {
id String @id @unique
user_id String
active_expires BigInt
idle_expires BigInt
user User @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@index([user_id])
}

model Key {
id String @id @unique
hashed_password String?
user_id String
user User @relation(references: [id], fields: [user_id], onDelete: Cascade)
@@index([user_id])
}

model EmailVerificationToken {
id String @id @unique
user_id String
expires BigInt
@@index([user_id])
}

model PasswordResetToken {
id String @id @unique
user_id String
expires BigInt
@@index([user_id])
}

model Follower {
id String @id @unique
user_id String
follower User @relation("FollowerToUser", fields: [user_id], references: [id], onDelete: Cascade)
@@index([user_id])
@@map("follower")
}

model Following {
id String @id @unique
user_id String
following User @relation("FollowingToUser", fields: [user_id], references: [id], onDelete: Cascade)
@@index([user_id])
@@map("following")
}

model SelfAssignableTag {
id Int @id @default(autoincrement())
name String
user_id String
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
@@index([user_id])
@@map("self_assignable_tag")
}

model SocialsConnection {
id String @id @unique
user_id String
tiktok String?
discord String?
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
@@index([user_id])
@@map("socials_connection")
}

model Games {
id Int @id @default(autoincrement())
name String
description String
asset_count Int @default(0)
asset_categories String @default("") // e.g characters,items,splash-art etc..
@@index([id])
@@index([name])
@@map("games")
}

model Assets {
id Int @id @default(autoincrement())
name String
game String
asset_category String
tags AssetTagsName @default(OFFICIAL)
url String
status Status @default(PENDING)
user User @relation(fields: [uploaded_by], references: [id], onDelete: Cascade)
uploaded_by String
uploaded_date String
view_count Int @default(0)
download_count Int @default(0)
file_size Int
@@index([id])
@@index([name])
@@index([game])
@@index([status])
@@index([tags])
@@index([uploaded_by])
@@map("assets")
}

model SavedOCGenerators {
id Int @id @default(autoincrement())
game String
data String
user User @relation(fields: [user_id], references: [id], onDelete: Cascade)
user_id String
saved_date String
@@index([id])
@@index([game])
@@index([user_id])
@@map("saved_oc_generators")
}

// role & tag enums
enum RoleNames {
USER
CONTRIBUTOR
TRANSLATOR
STAFF
DEVELOPER
CREATOR
}

enum SelfAssignTagsNames {
DESIGNER
DEVELOPER
ARTIST
WRITER
CONTENT_CREATOR
}

enum AssetTagsName {
OFFICIAL
FANMADE
}

// status enums
enum Status {
PENDING
APPROVED
REJECTED
}
37 changes: 37 additions & 0 deletions src/lib/auth/lucia.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { lucia } from "lucia";
import { web } from "lucia/middleware";
import { prisma as prismaAdapter } from "@lucia-auth/adapter-prisma";
import __prisma from "./prisma";

export const auth = lucia({
adapter: prismaAdapter(__prisma),
env: "DEV",
middleware: web(),
sessionExpiresIn: {
idlePeriod: 0,
activePeriod: 30 * 24 * 60 * 60 * 1000, // 30 days
},
sessionCookie: {
expires: false,
},
experimental: {
debugMode: true,
},
getUserAttributes: (dbUser) => {
return {
username: dbUser.username,
usernameColour: dbUser.username_colour,
avatarUrl: dbUser.avatar_url,
bannerUrl: dbUser.banner_url,
email: dbUser.email,
emailVerified: dbUser.email_verified,
pronouns: dbUser.pronouns,
verified: dbUser.verified,
bio: dbUser.bio,
role: dbUser.role,
dateJoined: dbUser.date_joined,
};
},
});

export type Auth = typeof auth;
9 changes: 9 additions & 0 deletions src/lib/auth/prisma.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { PrismaClient } from "@prisma/client";

declare global {
const __prisma: PrismaClient | undefined;
}

const __prisma = new PrismaClient();

export default __prisma;
Loading

0 comments on commit 6061506

Please sign in to comment.