How to connect the relationships between user, roles and permissions?

I try to figure out how to create a tables (user, role and permissions) with relationships and make the an example how to use them.
const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
password: text("password").notNull(),
});

const roles = pgTable("roles", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
});

const permissions = pgTable("permissions", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
});

const usersRelations = relations(users, ({ one, many }) => ({
role: one(roles, {
fields: [users.id],
references: [roles.userId],
}),
permissions: many(permissions, (join) => join.through(userRoles)),
}));
const users = pgTable("users", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
email: text("email").notNull(),
password: text("password").notNull(),
});

const roles = pgTable("roles", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
});

const permissions = pgTable("permissions", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
});

const usersRelations = relations(users, ({ one, many }) => ({
role: one(roles, {
fields: [users.id],
references: [roles.userId],
}),
permissions: many(permissions, (join) => join.through(userRoles)),
}));
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server