export const CategoryTable = pgTable('category', {
id: serial('id').primaryKey().unique(),
slug: varchar('slug', { length: 128 }).notNull().unique(),
name: varchar('name', { length: 128 }).notNull().unique(),
image: varchar('image', { length: 1024 }).notNull(),
description: text('description').notNull(),
});
export const CategoryRelations = relations(CategoryTable, ({ many }) => ({
children: many(CategoryToChildTable, { relationName: 'parent' }),
}));
export const CategoryToChildTable = pgTable(
'category_to_child',
{
categoryId: integer('category_id')
.notNull()
.references(() => CategoryTable.id),
childId: integer('child_id')
.notNull()
.references(() => CategoryTable.id),
},
(tbl) => ({
pk: primaryKey({ columns: [tbl.categoryId, tbl.childId] }),
}),
);
export const CategoryToChildRelations = relations(
CategoryToChildTable,
({ one }) => ({
parent: one(CategoryTable, {
relationName: 'parent',
fields: [CategoryToChildTable.categoryId],
references: [CategoryTable.id],
}),
child: one(CategoryTable, {
relationName: 'child',
fields: [CategoryToChildTable.childId],
references: [CategoryTable.id],
}),
}),
);
export const CategoryTable = pgTable('category', {
id: serial('id').primaryKey().unique(),
slug: varchar('slug', { length: 128 }).notNull().unique(),
name: varchar('name', { length: 128 }).notNull().unique(),
image: varchar('image', { length: 1024 }).notNull(),
description: text('description').notNull(),
});
export const CategoryRelations = relations(CategoryTable, ({ many }) => ({
children: many(CategoryToChildTable, { relationName: 'parent' }),
}));
export const CategoryToChildTable = pgTable(
'category_to_child',
{
categoryId: integer('category_id')
.notNull()
.references(() => CategoryTable.id),
childId: integer('child_id')
.notNull()
.references(() => CategoryTable.id),
},
(tbl) => ({
pk: primaryKey({ columns: [tbl.categoryId, tbl.childId] }),
}),
);
export const CategoryToChildRelations = relations(
CategoryToChildTable,
({ one }) => ({
parent: one(CategoryTable, {
relationName: 'parent',
fields: [CategoryToChildTable.categoryId],
references: [CategoryTable.id],
}),
child: one(CategoryTable, {
relationName: 'child',
fields: [CategoryToChildTable.childId],
references: [CategoryTable.id],
}),
}),
);