Skip to content

Commit

Permalink
upd reference & self nesting fix, new seed data :3
Browse files Browse the repository at this point in the history
  • Loading branch information
dromzeh committed Mar 17, 2024
1 parent 4b3149b commit 46a397d
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 21 deletions.
92 changes: 92 additions & 0 deletions src/scripts/seed/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import {
userFollowing,
requestFormUpvotes,
requestForm,
assetComments,
assetCommentsLikes,
} from "@/v2/db/schema"
import { Scrypt } from "lucia"
import "dotenv/config"
Expand Down Expand Up @@ -397,6 +399,96 @@ async function main() {
`[SEED] [assetTagAsset] inserted ${newAssetTagAsset.length} rows\n`
)

console.log("[SEED] [assetComments] Seeding asset comments...")
const newAssetComments = await db
.insert(assetComments)
.values([
{
assetId: newAssets[0].id,
commentedById: newUsers[0].id,
comment: "test comment",
},
{
assetId: newAssets[0].id,
commentedById: newUsers[1].id,
comment: "test comment 2",
},
{
assetId: newAssets[1].id,
commentedById: newUsers[0].id,
comment: "test comment 3",
},
])
.returning()
console.log(
`[SEED] [assetComments] inserted ${newAssetComments.length} rows\n`
)

console.log(
"[SEED] [assetComments] Seeding replies to comments [self ref]..."
)
const newAssetCommentsReplies = await db
.insert(assetComments)
.values([
{
commentedById: newUsers[1].id,
comment: "test comment reply",
parentCommentId: newAssetComments[0].id,
},
{
commentedById: newUsers[0].id,
comment: "test comment reply 2",
parentCommentId: newAssetComments[1].id,
},
{
commentedById: newUsers[1].id,
comment: "test comment reply 3",
parentCommentId: newAssetComments[2].id,
},
])
.returning()
console.log(
`[SEED] [assetComments] inserted ${newAssetCommentsReplies.length} rows\n`
)

console.log(
"[SEED] [assetCommentsLikes] Seeding asset comments likes..."
)
const newAssetCommentsLikes = await db
.insert(assetCommentsLikes)
.values([
{
commentId: newAssetComments[0].id,
likedById: newUsers[1].id,
},
{
commentId: newAssetComments[1].id,
likedById: newUsers[0].id,
},
{
commentId: newAssetComments[2].id,
likedById: newUsers[1].id,
},
{
commentId: newAssetCommentsReplies[0].id,
likedById: newUsers[0].id,
},
{
commentId: newAssetCommentsReplies[1].id,
likedById: newUsers[1].id,
},
{
commentId: newAssetCommentsReplies[2].id,
likedById: newUsers[0].id,
}
])
.returning()
console.log(
`[SEED] [assetCommentsLikes] inserted ${newAssetCommentsLikes.length} rows\n`
)



console.log("[SEED] [userCollection] Seeding user collections...")
const newUserCollections = await db
.insert(userCollection)
Expand Down
2 changes: 0 additions & 2 deletions src/v2/db/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ export const tableNames = {
game: "game",
gameLikes: "gameLikes",
stripeUser: "stripeUser",
// atlas: "atlas",
// atlasToAsset: "atlasToAsset",
assetExternalFile: "assetExternalFile",
assetTag: "assetTag",
assetTagLikes: "assetTagLikes",
Expand Down
33 changes: 24 additions & 9 deletions src/v2/db/schema/asset/asset-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
// uniqueIndex,
index,
integer,
foreignKey,
} from "drizzle-orm/sqlite-core"
import { authUser } from "../user/user"
import { asset } from "./asset"
Expand All @@ -21,15 +22,18 @@ export const assetComments = sqliteTable(
.$defaultFn(() => {
return generateID()
}),
assetId: integer("asset_id")
.default(null)
.references(() => asset.id),
parentCommentId: text("parent_comment_id")
.default(null)
.references(() => assetComments.id),
assetId: integer("asset_id").references(() => asset.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
// typescript limitations means that the type will be set as `any` if we self reference, so we create FK manually
parentCommentId: text("parent_comment_id"),
commentedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
comment: text("comment").notNull(),
createdAt: text("created_at")
.notNull()
Expand All @@ -40,6 +44,11 @@ export const assetComments = sqliteTable(
},
(assetComments) => {
return {
parentCommentFk: foreignKey({
name: "self_reference_parent_comment_id",
columns: [assetComments.parentCommentId],
foreignColumns: [assetComments.id],
}),
assetIdx: index("assetcomments_asset_idx").on(
assetComments.assetId
),
Expand All @@ -64,10 +73,16 @@ export const assetCommentsLikes = sqliteTable(
{
commentId: text("comment_id")
.notNull()
.references(() => assetComments.id),
.references(() => assetComments.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
Expand Down
10 changes: 8 additions & 2 deletions src/v2/db/schema/asset/asset-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ export const assetLikes = sqliteTable(
{
assetId: integer("asset_id")
.notNull()
.references(() => asset.id),
.references(() => asset.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
Expand Down
10 changes: 8 additions & 2 deletions src/v2/db/schema/categories/asset-categories-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ export const assetCategoryLikes = sqliteTable(
{
assetCategoryId: text("asset_id")
.notNull()
.references(() => assetCategory.id),
.references(() => assetCategory.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
Expand Down
10 changes: 8 additions & 2 deletions src/v2/db/schema/collections/user-collection-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,16 @@ export const userCollectionLikes = sqliteTable(
{
collectionId: text("collection_id")
.notNull()
.references(() => userCollection.id),
.references(() => userCollection.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("createdAt")
.notNull()
.$defaultFn(() => {
Expand Down
10 changes: 8 additions & 2 deletions src/v2/db/schema/game/game-likes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@ export const gameLikes = sqliteTable(
{
gameId: text("asset_id")
.notNull()
.references(() => game.id),
.references(() => game.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
likedById: text("liked_by_id")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("created_at")
.notNull()
.$defaultFn(() => {
Expand Down
10 changes: 8 additions & 2 deletions src/v2/db/schema/user/user-following.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ export const userFollowing = sqliteTable(
{
followerId: text("followerId")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
followingId: text("followingId")
.notNull()
.references(() => authUser.id),
.references(() => authUser.id, {
onUpdate: "cascade",
onDelete: "cascade",
}),
createdAt: text("createdAt")
.notNull()
.$defaultFn(() => {
Expand Down

0 comments on commit 46a397d

Please sign in to comment.