export const course = pgTable('Course', {
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
name: text('name').notNull(),
description: text('description').notNull(),
ordinal: integer('ordinal').notNull(),
duration: integer('duration').default(0).notNull(),
thumbnails: text('thumbnails').array(),
tags: text('tags').array(),
supportingMedias: text('supportingMedias').array(),
active: integer('active').default(0).notNull(),
previewVideo: text('previewVideo'),
downloadMediaZip: text('downloadMediaZip'),
rating: numericNumber('rating', { precision: 65, scale: 30 }).default(0).notNull(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
});
export const courseRelations = relations(course, ({ one, many }) => ({
// 1 - N
user: one(user, { fields: [course.userId], references: [user.id] }),
// M - N
bookmarks: many(bookmark),
chapters: many(chapter, { relationName: 'chaptersCourse' }),
comments: many(comment),
courseSubscriptions: many(courseSubscription),
likes: many(like),
notes: many(note),
ratingsReviews: many(ratingsReview),
videoLogs: many(videoLog),
// M - M
recommendations: many(recommendation)
}));
export const recommendation = pgTable(
'Recommendation',
{
id: text('id').primaryKey().default(uuid()).notNull(),
createdAt: timestamp('createdAt', { precision: 3, mode: 'string' }).defaultNow().notNull(),
// updatedAt: timestamp('updatedAt', { precision: 3, mode: 'string' }).notNull(),
tags: text('tags').default('{}').array(),
userId: text('userId')
.notNull()
.references(() => user.id, cascade)
},
(table) => {
return {
userIdKey: uniqueIndex('Recommendation_userId_key').on(table.userId)
};
}
);
export const recommendationRelations = relations(recommendation, ({ one, many }) => ({
user: one(user, { fields: [recommendation.userId], references: [user.id] }),
courses: many(course),
interests: many(interest)
}));
/**
* Many to Many
*/
export const recommendationsToCourses = pgTable(
'Recommendations_to_Courses',
{
recommendationId: text('recommendationId')
.notNull()
.references(() => recommendation.id, cascade),
courseId: text('courseId')
.notNull()
.references(() => course.id, cascade)
},
(t) => ({ pk: primaryKey({ columns: [t.recommendationId, t.courseId] }) })
);
export const recommendationsToCoursesRelations = relations(recommendationsToCourses, ({ one }) => ({
course: one(course, {
fields: [recommendationsToCourses.courseId],
references: [course.id]
}),
recommendation: one(recommendation, {
fields: [recommendationsToCourses.recommendationId],
references: [recommendation.id]
})
}));