Skip to content

Commit

Permalink
asset comment schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 16, 2024
1 parent cb6d078 commit 284713c
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/v2/db/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const tableNames = {
userBlocked: "userBlocked",
gameAssetCategory: "gameAssetCategory",
assetLikes: "assetLikes",
assetComments: "assetComments",
userCollectionLikes: "userCollectionLikes",
userCollectionCollaborators: "userCollectionCollaborators",
game: "game",
Expand Down
1 change: 1 addition & 0 deletions src/v2/db/schema.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./schema/asset/asset"
export * from "./schema/asset/asset-comments"
export * from "./schema/asset/asset-external-files"
export * from "./schema/asset/asset-likes"

Expand Down
60 changes: 60 additions & 0 deletions src/v2/db/schema/asset/asset-comments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { tableNames } from "@/v2/db/drizzle"
import { relations } from "drizzle-orm"
import {
sqliteTable,
text,
// uniqueIndex,
index,
integer,
} from "drizzle-orm/sqlite-core"
import { authUser } from "../user/user"
import { asset } from "./asset"
import { createInsertSchema, createSelectSchema } from "drizzle-zod"

export const assetComments = sqliteTable(
tableNames.assetComments,
{
assetId: integer("asset_id")
.notNull()
.references(() => asset.id),
commentedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
comment: text("comment").notNull(),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
return new Date().toISOString()
}),
editedAt: text("edited_at").default(null),
},
(assetComments) => {
return {
assetIdx: index("assetcomments_asset_idx").on(
assetComments.assetId
),
commentedByIdx: index("assetcomments_commented_by_idx").on(
assetComments.commentedById
),
}
}
)

export type AssetComments = typeof assetComments.$inferSelect
export type NewAssetComments = typeof assetComments.$inferInsert

export const insertAssetCommentsSchema = createInsertSchema(assetComments)
export const selectAssetCommentsSchema = createSelectSchema(assetComments)

export const assetCommentsRelations = relations(assetComments, ({ one }) => ({
asset: one(asset, {
fields: [assetComments.assetId],
references: [asset.id],
relationName: "assetcomments_asset",
}),
commentedBy: one(authUser, {
fields: [assetComments.commentedById],
references: [authUser.id],
relationName: "assetcomments_commented_by",
}),
}))
2 changes: 2 additions & 0 deletions src/v2/db/schema/asset/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { assetTagAsset } from "../tags/asset-tags"
import { createInsertSchema, createSelectSchema } from "drizzle-zod"
import { assetLikes } from "./asset-likes"
import { assetExternalFile } from "./asset-external-files"
import { assetComments } from "./asset-comments"

/*
NOTE: Assets have a lot of relations, and can be quite complex in some cases.
Expand Down Expand Up @@ -100,6 +101,7 @@ export const assetRelations = relations(asset, ({ one, many }) => ({
assetTagAsset: many(assetTagAsset),
assetExternalFile: many(assetExternalFile),
assetLikes: many(assetLikes),
assetComments: many(assetComments),
authUser: one(authUser, {
fields: [asset.uploadedById, asset.uploadedByName],
references: [authUser.id, authUser.username],
Expand Down
2 changes: 2 additions & 0 deletions src/v2/db/schema/user/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { assetTagLikes } from "../tags/asset-tags-likes"
import { assetCategoryLikes } from "../categories/asset-categories-likes"
import { requestForm, requestFormUpvotes } from "../supporter/request-form"
import { userBlocked } from "./user-blocked"
import { assetComments } from "../asset/asset-comments"

/*
NOTE: Very basic user information
Expand Down Expand Up @@ -209,6 +210,7 @@ export const usersRelations = relations(authUser, ({ one, many }) => ({
userFavorite: one(userFavorite),
userCollectionLikes: many(userCollectionLikes),
assetLikes: many(assetLikes),
assetComments: many(assetComments),
socialsConnection: one(socialsConnection),
userCollection: many(userCollection),
stripeUser: one(stripeUser),
Expand Down

0 comments on commit 284713c

Please sign in to comment.