Union of schema with relations

This is code for my 2 tables where I am creating a new role and the adding its permissions
import { array, nativeEnum, string, union } from "zod";
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';

export const roles = mysqlTable(
"role",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
slug: varchar("slug", { length: 256 }).notNull().notNull(),
name: varchar("name", { length: 256 }).unique().notNull(),
updatedAt: timestamp("updatedAt", { mode: "date" }).defaultNow().onUpdateNow().notNull(),
},
(role) => ({
roleNameIndex: index("name_idx").on(role.name),
roleIdIdx: index("roleId_idx").on(role.id),
roleSlug: index("roleSlug_idx").on(role.slug),
})
);

export const rolesRelations = relations(roles, ({ one }) => ({
permissions: one(rolePermissions, { fields: [roles.name], references: [rolePermissions.role] }),
}));

export const roleInsertSchema = createInsertSchema(roles);

export const rolePermissions = mysqlTable(
"role-permission",
{
role: varchar("role", { length: 256 }).notNull(),
permissions: varchar("permissions", { length: 256 }).$type<Permission[]>().notNull(),
},
(rolePermission) => ({
compositeKey: primaryKey({ columns: [rolePermission.role, rolePermission.permissions] }),
})
);

export const rolePermissionRelations = relations(rolePermissions, ({ one }) => ({
role: one(roles, { fields: [rolePermissions.role], references: [roles.name] }),
}));

export const rolePermissionInsertSchema = createInsertSchema(rolePermissions, {
permissions: array(nativeEnum(Permission)),
});
export const rolePermissionSelectSchema = createSelectSchema(rolePermissions, {
permissions: array(nativeEnum(Permission)),
});
import { array, nativeEnum, string, union } from "zod";
import { createInsertSchema, createSelectSchema } from 'drizzle-zod';

export const roles = mysqlTable(
"role",
{
id: bigint("id", { mode: "number" }).primaryKey().autoincrement(),
slug: varchar("slug", { length: 256 }).notNull().notNull(),
name: varchar("name", { length: 256 }).unique().notNull(),
updatedAt: timestamp("updatedAt", { mode: "date" }).defaultNow().onUpdateNow().notNull(),
},
(role) => ({
roleNameIndex: index("name_idx").on(role.name),
roleIdIdx: index("roleId_idx").on(role.id),
roleSlug: index("roleSlug_idx").on(role.slug),
})
);

export const rolesRelations = relations(roles, ({ one }) => ({
permissions: one(rolePermissions, { fields: [roles.name], references: [rolePermissions.role] }),
}));

export const roleInsertSchema = createInsertSchema(roles);

export const rolePermissions = mysqlTable(
"role-permission",
{
role: varchar("role", { length: 256 }).notNull(),
permissions: varchar("permissions", { length: 256 }).$type<Permission[]>().notNull(),
},
(rolePermission) => ({
compositeKey: primaryKey({ columns: [rolePermission.role, rolePermission.permissions] }),
})
);

export const rolePermissionRelations = relations(rolePermissions, ({ one }) => ({
role: one(roles, { fields: [rolePermissions.role], references: [roles.name] }),
}));

export const rolePermissionInsertSchema = createInsertSchema(rolePermissions, {
permissions: array(nativeEnum(Permission)),
});
export const rolePermissionSelectSchema = createSelectSchema(rolePermissions, {
permissions: array(nativeEnum(Permission)),
});
How would I make an insert schema for it so that the name from roles table is added to the insert schema and used as the role feild in rolePermissions
1 Reply
arafays
arafaysOP12mo ago
something like this
export const RoleWithPermissionInsertSchema = union(
[
roleInsertSchema,
rolePermissionInsertSchema,
]
)
export const RoleWithPermissionInsertSchema = union(
[
roleInsertSchema,
rolePermissionInsertSchema,
]
)
but with the name of role from roles table is used in role validation from rolePermissions table
Want results from more Discord servers?
Add your server