Skip to content

Commit

Permalink
request form schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Feb 6, 2024
1 parent 24f5666 commit 83dbe28
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

![Quality]

Source code for the API powering [**wanderer.moe**](https://wanderer.moe) — using **Cloudflare Workers** and **Hono** with **R2 Storage** for the CDN, **Turso** and **Drizzle**.
Source code for the API powering [**wanderer.moe**](https://wanderer.moe) — using **Cloudflare Workers** and **Hono** with **R2 Storage** for the CDN, **Turso** and **Drizzle**. Durable Objects are used for rate limiting requests.

</div>

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"drizzle:studio": "drizzle-kit studio --port 7331 --host 127.0.0.1 --verbose"
},
"devDependencies": {
"@asteasolutions/zod-to-openapi": "^6.3.1",
"@cloudflare/workers-types": "^4.20240117.0",
"@types/node": "^20.11.8",
"dotenv": "^16.4.1",
Expand Down
17 changes: 15 additions & 2 deletions pnpm-lock.yaml

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

2 changes: 2 additions & 0 deletions src/v2/db/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ export const tableNames = {
userCollection: "userCollection",
userCollectionAsset: "userCollectionAsset",
socialsConnection: "socialsConnection",
requestForm: "requestForm",
requestFormUpvotes: "requestFormUpvotes",
}
2 changes: 2 additions & 0 deletions src/v2/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@ export * from "./schema/user/user-favorites"
export * from "./schema/user/user-following"
export * from "./schema/collections/user-collection-likes"
export * from "./schema/collections/user-collections-collaborators"

export * from "./schema/supporter/request-form"
110 changes: 110 additions & 0 deletions src/v2/db/schema/supporter/request-form.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import { tableNames } from "@/v2/db/drizzle"
import { relations } from "drizzle-orm"
import {
sqliteTable,
text,
// uniqueIndex,
index,
} from "drizzle-orm/sqlite-core"
import { createInsertSchema, createSelectSchema } from "drizzle-zod"
import { generateID } from "@/v2/lib/oslo"
import { authUser } from "../user/user"

type requestArea = "asset" | "game" | "site"

export const requestForm = sqliteTable(
tableNames.requestForm,
{
id: text("id")
.unique()
.notNull()
.$defaultFn(() => {
return generateID()
}),
userId: text("user_id")
.notNull()
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
area: text("type").notNull().$type<requestArea>(),
title: text("title").notNull(),
description: text("description").notNull(),
},
(requestForm) => {
return {
requestFormUserIdx: index("request_form_user_id_idx").on(
requestForm.userId
),
}
}
)

export type RequestForm = typeof requestForm.$inferSelect
export type NewRequestForm = typeof requestForm.$inferInsert
export const insertRequestFormSchema = createInsertSchema(requestForm)
export const selectRequestFormSchema = createSelectSchema(requestForm)

export const requestFormRelations = relations(requestForm, ({ one, many }) => ({
user: one(authUser, {
fields: [requestForm.userId],
references: [authUser.id],
relationName: "request_form_user",
}),
upvotes: many(requestFormUpvotes),
}))

export const requestFormUpvotes = sqliteTable(
tableNames.requestFormUpvotes,
{
id: text("id")
.unique()
.notNull()
.$defaultFn(() => {
return generateID()
}),
requestFormId: text("request_form_id")
.notNull()
.references(() => requestForm.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
userId: text("user_id")
.notNull()
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
},
(requestFormUpvotes) => {
return {
requestFormUpvotesIdx: index("request_form_upvotes_idx").on(
requestFormUpvotes.requestFormId,
requestFormUpvotes.userId
),
}
}
)

export type RequestFormUpvotes = typeof requestFormUpvotes.$inferSelect
export type NewRequestFormUpvotes = typeof requestFormUpvotes.$inferInsert
export const insertRequestFormUpvotesSchema =
createInsertSchema(requestFormUpvotes)
export const selectRequestFormUpvotesSchema =
createSelectSchema(requestFormUpvotes)

export const requestFormUpvotesRelations = relations(
requestFormUpvotes,
({ one }) => ({
user: one(authUser, {
fields: [requestFormUpvotes.userId],
references: [authUser.id],
relationName: "request_form_upvotes_user",
}),
requestForm: one(requestForm, {
fields: [requestFormUpvotes.requestFormId],
references: [requestForm.id],
relationName: "request_form_upvotes_request_form",
}),
})
)
3 changes: 3 additions & 0 deletions src/v2/db/schema/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { assetLikes } from "../asset/asset-likes"
import { gameLikes } from "../game/game-likes"
import { assetTagLikes } from "../tags/asset-tags-likes"
import { assetCategoryLikes } from "../categories/asset-categories-likes"
import { requestForm, requestFormUpvotes } from "../supporter/request-form"

/*
NOTE: Very basic user information
Expand Down Expand Up @@ -174,6 +175,8 @@ export const usersRelations = relations(authUser, ({ one, many }) => ({
assetTagLikes: many(assetTagLikes),
assetCategoryLikes: many(assetCategoryLikes),
userCollectionCollaborators: many(userCollectionCollaborators),
requestForm: many(requestForm),
requestFormUpvotes: many(requestFormUpvotes),
}))

export const authCredentialsRelations = relations(
Expand Down
4 changes: 2 additions & 2 deletions src/v2/middleware/ratelimit/limiter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import dayjs from "dayjs"
import { Context, MiddlewareHandler } from "hono"

const fakeDomain = "http://rate-limiter.com/"
const fakeDomain = "http://fake.wanderer.moe/"

const getRateLimitKey = (ctx: Context) => {
const ip = ctx.req.header("cf-connecting-ip")
Expand Down Expand Up @@ -92,7 +92,7 @@ export const rateLimit = (
{
success: false,
message:
"Rate limit exceeded, contact [email protected] if you're using this API in production and need a higher rate limit.",
"Rate limit exceeded, contact [email protected] or reach out to @dromzeh on discord if you're using this API in production and need a higher rate limit.",
},
429
)
Expand Down

0 comments on commit 83dbe28

Please sign in to comment.