convert from prisma

how can this relation and model with drizzle ?
No description
6 Replies
Noahh
Noahh16mo ago
What kind of database are you using? postgres, mysql, etc?
Angelelz
Angelelz16mo ago
Also, can you share what you have so far, you might be very close.
alirezasoltan
alirezasoltanOP16mo ago
postgres my base problem is in replayTo item so I have no idea to make relation replayTo with comment
Noahh
Noahh16mo ago
Disclaimer, I haven't tried running this, so I'm not positive it would work, but you can try something like this out:
import { createId } from '@paralleldrive/cuid2';

export const users = pgTable(...);

export const posts = pgTable(...);

export const comments = pgTable('comments', {
id: text('id').primaryKey().$default(() => createId()),
text: text('text').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
authorId: text('author_id').notNull().references(() => users.id),
postId: text('post_id').notNull().references(() => posts.id, { onDelete: 'cascade' }),
replyToId: text('reply_to_id').references(() => comments.id)
});

export const commentRelations = relations(comments, ({ one, many }) => ({
author: one(users, {
fields: [comments.authorId],
references: [users.id]
}),
post: one(posts, {
fields: [comments.postId],
references: [posts.id]
}),
replyTo: one(comments, {
fields: [comments.replyToId],
references: [comments.id]
}),
replies: many(comments)
}))
import { createId } from '@paralleldrive/cuid2';

export const users = pgTable(...);

export const posts = pgTable(...);

export const comments = pgTable('comments', {
id: text('id').primaryKey().$default(() => createId()),
text: text('text').notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
authorId: text('author_id').notNull().references(() => users.id),
postId: text('post_id').notNull().references(() => posts.id, { onDelete: 'cascade' }),
replyToId: text('reply_to_id').references(() => comments.id)
});

export const commentRelations = relations(comments, ({ one, many }) => ({
author: one(users, {
fields: [comments.authorId],
references: [users.id]
}),
post: one(posts, {
fields: [comments.postId],
references: [posts.id]
}),
replyTo: one(comments, {
fields: [comments.replyToId],
references: [comments.id]
}),
replies: many(comments)
}))
alirezasoltan
alirezasoltanOP16mo ago
TNX 🙏 i want replies nested in the comment i think that is ok Now with this model how can insert a reply for comment?
Noahh
Noahh16mo ago
You'd do something like
db.insert(comments).values({
replyToId: originalComment.id,
text: replyText,
authorId: replyAuthor.id,
postId: originalComment.postId
})
db.insert(comments).values({
replyToId: originalComment.id,
text: replyText,
authorId: replyAuthor.id,
postId: originalComment.postId
})
Want results from more Discord servers?
Add your server