Skip to content

Commit

Permalink
test nested asset comments + impl likes schema
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 284713c commit 4b3149b
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/v2/db/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const tableNames = {
gameAssetCategory: "gameAssetCategory",
assetLikes: "assetLikes",
assetComments: "assetComments",
assetCommentsLikes: "assetCommentsLikes",
userCollectionLikes: "userCollectionLikes",
userCollectionCollaborators: "userCollectionCollaborators",
game: "game",
Expand Down
71 changes: 68 additions & 3 deletions src/v2/db/schema/asset/asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,23 @@ import {
import { authUser } from "../user/user"
import { asset } from "./asset"
import { createInsertSchema, createSelectSchema } from "drizzle-zod"
import { generateID } from "@/v2/lib/oslo"

export const assetComments = sqliteTable(
tableNames.assetComments,
{
assetId: integer("asset_id")
id: text("comment_id")
.primaryKey()
.notNull()
.$defaultFn(() => {
return generateID()
}),
assetId: integer("asset_id")
.default(null)
.references(() => asset.id),
parentCommentId: text("parent_comment_id")
.default(null)
.references(() => assetComments.id),
commentedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
Expand All @@ -33,6 +43,9 @@ export const assetComments = sqliteTable(
assetIdx: index("assetcomments_asset_idx").on(
assetComments.assetId
),
parentCommentIdx: index("assetcomments_parent_comment_idx").on(
assetComments.parentCommentId
),
commentedByIdx: index("assetcomments_commented_by_idx").on(
assetComments.commentedById
),
Expand All @@ -46,15 +59,67 @@ export type NewAssetComments = typeof assetComments.$inferInsert
export const insertAssetCommentsSchema = createInsertSchema(assetComments)
export const selectAssetCommentsSchema = createSelectSchema(assetComments)

export const assetCommentsLikes = sqliteTable(
tableNames.assetCommentsLikes,
{
commentId: text("comment_id")
.notNull()
.references(() => assetComments.id),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
return new Date().toISOString()
}),
},
(assetCommentsLikes) => {
return {
commentIdx: index("assetcommentslikes_comment_idx").on(
assetCommentsLikes.commentId
),
likedByIdx: index("assetcommentslikes_liked_by_idx").on(
assetCommentsLikes.likedById
),
}
}
)

export type AssetCommentsLikes = typeof assetCommentsLikes.$inferSelect
export type NewAssetCommentsLikes = typeof assetCommentsLikes.$inferInsert

export const insertAssetCommentsLikesSchema =
createInsertSchema(assetCommentsLikes)
export const selectAssetCommentsLikesSchema =
createSelectSchema(assetCommentsLikes)

// not too sure about this
export const assetCommentsRelations = relations(assetComments, ({ one }) => ({
asset: one(asset, {
fields: [assetComments.assetId],
references: [asset.id],
relationName: "assetcomments_asset",
relationName: "asset_comments_asset",
}),
commentedBy: one(authUser, {
fields: [assetComments.commentedById],
references: [authUser.id],
relationName: "assetcomments_commented_by",
relationName: "asset_comments_commented_by",
}),
}))

export const assetCommentsLikesRelations = relations(
assetComments,
({ one }) => ({
asset: one(asset, {
fields: [assetComments.assetId],
references: [asset.id],
relationName: "asset_comments_asset",
}),
commentedBy: one(authUser, {
fields: [assetComments.commentedById],
references: [authUser.id],
relationName: "asset_comments_commented_by",
}),
})
)
2 changes: 1 addition & 1 deletion src/v2/db/schema/asset/asset-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export type NewAssetLikes = typeof assetLikes.$inferInsert
export const insertAssetLikesSchema = createInsertSchema(assetLikes)
export const selectAssetLikesSchema = createSelectSchema(assetLikes)

export const assetNetworkingRelations = relations(assetLikes, ({ one }) => ({
export const assetLikesRelations = relations(assetLikes, ({ one }) => ({
asset: one(asset, {
fields: [assetLikes.assetId],
references: [asset.id],
Expand Down

0 comments on commit 4b3149b

Please sign in to comment.