Typing enums

First of all, I still really enjoy using Kysely, great library! I have the following pattern for enums. I create the role table as an "enum" table (which I got from another help post I was reading earlier) and add it as an foreign key to the teacher table.
export const up = async (db: Kysely<any>) => {
await db.schema
.createTable("role")
.addColumn("role", "text", (c) => c.primaryKey().defaultTo(ROLES.LESGEVER))
.execute();

await db.schema
.createTable("teacher")
.addColumn("ID", "uuid", (c) => c.primaryKey())
.addColumn("createdAt", "timestamptz", (c) =>
c.notNull().defaultTo("now()")
)
.addColumn("updatedAt", "timestamptz", (c) =>
c.notNull().defaultTo("now()")
)
.addColumn("email", "text", (c) => c.notNull().unique())
// Other columns
.addColumn("role", "text", (c) =>
c.notNull().references("role.role").onDelete("cascade")
)
.execute();

await sql`
CREATE TRIGGER set_teacher_updated_at
BEFORE UPDATE ON teacher
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();
`.execute(db);
};

export const down = async (db: Kysely<any>) => {
// Down function
};
export const up = async (db: Kysely<any>) => {
await db.schema
.createTable("role")
.addColumn("role", "text", (c) => c.primaryKey().defaultTo(ROLES.LESGEVER))
.execute();

await db.schema
.createTable("teacher")
.addColumn("ID", "uuid", (c) => c.primaryKey())
.addColumn("createdAt", "timestamptz", (c) =>
c.notNull().defaultTo("now()")
)
.addColumn("updatedAt", "timestamptz", (c) =>
c.notNull().defaultTo("now()")
)
.addColumn("email", "text", (c) => c.notNull().unique())
// Other columns
.addColumn("role", "text", (c) =>
c.notNull().references("role.role").onDelete("cascade")
)
.execute();

await sql`
CREATE TRIGGER set_teacher_updated_at
BEFORE UPDATE ON teacher
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();
`.execute(db);
};

export const down = async (db: Kysely<any>) => {
// Down function
};
I also have the ROLES enum to use across my back-end for various purposes
export const ROLES = {
LESGEVER: "Lesgever",
COORDINATOR: "Coördinator",
BESTUUR: "Bestuur",
} as const;

export type Role = (typeof ROLES)[keyof typeof ROLES];
export const ROLES = {
LESGEVER: "Lesgever",
COORDINATOR: "Coördinator",
BESTUUR: "Bestuur",
} as const;

export type Role = (typeof ROLES)[keyof typeof ROLES];
When I define the types for this table I do the following:
export interface GenderTable {
gender: string;
}

export type Gender = Selectable<GenderTable>;
export type InsertableGender = Insertable<GenderTable>;
export type UpdatableGender = Updateable<GenderTable>;

export interface TeacherTable {
ID: Generated<UUID>;
createdAt: ColumnType<Date | string, never, never>;
updatedAt: ColumnType<Date | string, never, never>;
email: string;

role: ColumnType<string, never, string>;
}

export type Teacher = Selectable<TeacherTable>;
export type InsertableTeacher = Insertable<TeacherTable>;
export type UpdateableTeacher = Updateable<TeacherTable>;
export interface GenderTable {
gender: string;
}

export type Gender = Selectable<GenderTable>;
export type InsertableGender = Insertable<GenderTable>;
export type UpdatableGender = Updateable<GenderTable>;

export interface TeacherTable {
ID: Generated<UUID>;
createdAt: ColumnType<Date | string, never, never>;
updatedAt: ColumnType<Date | string, never, never>;
email: string;

role: ColumnType<string, never, string>;
}

export type Teacher = Selectable<TeacherTable>;
export type InsertableTeacher = Insertable<TeacherTable>;
export type UpdateableTeacher = Updateable<TeacherTable>;
This works for typesafety of course, but I'd like for role to be only values of the ROLES enum. Does it make sense to just change it from string to Role or am I missing something
2 Replies
koskimas
koskimas7d ago
Just changing string to Role should work perfectly
TheMelonAssassin
TheMelonAssassinOP7d ago
Alright thanks I believe I was making it harder then needed to be. I was looking for a way to sync enum values from the database (either with a pgEnum or the enum table from above) with this:
export const ROLES = {
LESGEVER: "Lesgever",
COORDINATOR: "Coördinator",
BESTUUR: "Bestuur",
} as const;

export type Role = (typeof ROLES)[keyof typeof ROLES];
export const ROLES = {
LESGEVER: "Lesgever",
COORDINATOR: "Coördinator",
BESTUUR: "Bestuur",
} as const;

export type Role = (typeof ROLES)[keyof typeof ROLES];
But enum values should not change often so just adding them if it happens to the definition makes more sense
Want results from more Discord servers?
Add your server