Replies: 2 comments 5 replies
-
Hey, kinda late but I'm struggling with this rn, so you probably can do something like this: import type { InferModelFromColumns } from "drizzle-orm";
import type { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
import * as schema from "~/path/to/your/database/schema"
type InferSubjectsFromDrizzle<Schema> = {
[K in keyof Schema]: Schema[K] extends SQLiteTableWithColumns<infer Table>
? InferModelFromColumns<Table["columns"]> | Table["name"]
: never
};
type InferSubjects<Schema> = InferSubjectsFromDrizzle<Schema>[keyof InferSubjectsFromDrizzle<Schema>] | "all";
type Subjects = InferSubjects<typeof schema> Note that if you use import type { PgTableWithColumns } from "drizzle-orm/pg-core"
import type { MySqlTableWithColumns } from "drizzle-orm/mysql-core" You may also want to lower case the first letter of the table name(this vary based on your table names and it's just for folks that want to type "users" instead of "Users", not really required) import type { InferModelFromColumns } from "drizzle-orm";
import type { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
type LowercaseFirstLetter<S extends string> = S extends `${infer F}${infer R}` ? `${Lowercase<F>}${R}` : S;
type InferSubjectsFromDrizzle<Schema> = {
[K in keyof Schema]: Schema[K] extends SQLiteTableWithColumns<infer Table>
? InferModelFromColumns<Table["columns"]> | LowercaseFirstLetter<Table["name"]>
: never
};
type InferSubjects<Schema> = InferSubjectsFromDrizzle<Schema>[keyof InferSubjectsFromDrizzle<Schema>] | "all"; Warning Keep in mind that this doesn't auto detect the subject type at runtime, it's just TYPE INFERENCE, also I think it's not possible to detect at runtime at all since drizzle produced objects doesn't have any kind of runtime information that can be extracted later on (so you should stick with the subject function(https://casl.js.org/v6/en/guide/subject-type-detection#subject-helper) right now) This was tested with |
Beta Was this translation helpful? Give feedback.
-
an old question of mine. it is weird there isn't an open source tool that is simple and less verbose than casl at this point. |
Beta Was this translation helpful? Give feedback.
-
has anyone gotten the inferred types from drizzle orm or drizzle-zod inferred schema to supply the subject for the ability builder.
i figured this would work:
this did not yield the wanted result.
Beta Was this translation helpful? Give feedback.
All reactions