Custom Tables | SQL Schema
:nuxt: Help me understand how Better-Auth works with the DB.
Better-Auth has its required core schema for the 4 core tables. For
id
uses string
[I'm looking at the core schema] and the generated SQL gets a text
type, for example.
1. If I create all tables on my own [Better-Auth Tables + other needed tables] and set that up at the DB level, does the auth
instance still need to know about all of them, i.e. via me configuring the better-auth
object [for a custom field I added on the user
table] and creating my own plugin [for the extra tables]?
2. Also, if I, upon creating my DB tables, set the id
PK field to uuid
, will better-auth
have problems with that?
3. On my user
table, I (want to) have a user_type
field which is a PostgreSQL Enum Type [just a couple of string options]. I can't set that on Better-Auth, as it only accepts string, number, etc. - is it okay for me, when configuring better-auth
, to set the field type as string
, when it is, in reality [at the DB level] an enum?
3. Does Better-Auth rely on the DB to auto, randomly generate the IDs or does it pass the id on every, say, user creation, for instance?
I'm asking this because the SQL generated by Better-Auth is kinda different from what I'd like. So I was thinking of creating all tables on my own.
But then I think I need to tell better-auth about them, and here comes the question about the Better-Auth accepted TS types vs PostgreSQL types I need.
PS: I'm working with Nuxt, PostgreSQL, Drizzle ORM [though I'm trying to avoid the latter, if I can]9 Replies
Also interested in answers to the above questions - in particular, why can't we use
uuid
for PK fields1. not exactly. Only if you extend the user table make sure to add additional fields in your auth config.
2. no. but you need to disable the default id generation by setting
advanced.database.generateId
to false
3. On Better Auth additional fields config you can pass a literal array as a type and it'll be inferred as an enum/union
4. if you don't disable generateId, it'll auto gen id itselfThank you very much.
Lemme see if I got this straight:
1. for the custom tables, I can create them on the database and that's all. I don't need to tell
better-auth
about them via a custom plugin, stating the extra tables, etc., right?
2. regarding the user
table, it will have an additional field (user_type
), I'll add that to the user's additionalFields
when setting up the auth
instance. For the type of this user_type
I didn't get to set string[]
or Array<LiteralString>
.
Could you help me out saying what exactly I need to type the user_type
with?
3. Okay. For the id
s, I'll just have them as uuid
, to be default generated with get_random_uuid()
, and disable better-auth's default id generation when setting up the auth
object.1. yes
2.
3. yes
Fascinating.
THANK YOU VERY MUCH.
I'll work on my Nuxt Project setup then.
If you need any help, please let us know how we can help you out.
Thank you.
@bekacru
Sorry for the ping, but...
... using a string literal array [as you suggested] for a type [so it can be mapped to a pg enum], I get an error when I try to generate the schema via the BA CLI
My
auth
object looks like
Error:
By using type: "string[]"
, I don't get the error and the generated schema has user_type: text('user_type').array()
, but that isn't what I wanted. I wanted it to be mapped to an enum with the coupla values possible.hey I don't think db enum values are supported. I suggested the above cause I thought you need the type hiniting
I'd be okay with the type hinting, at least, since these are the two values I'd expect and enforce.
For the actual types, I'd infer them from the edited table schemas. The thing is I can't even type the additional field with
["AP", "HF"]
.
Maybe I'll have to just type it with string and then edit the generated schema - create the pgEnum and properly type the table field.
I just don't know if better-auth is going to be okay with thatThe thing is I can't even type the additional field with ["AP", "HF"].you mean it's not being inferred as union?
Maybe I'll have to just type it with string and then edit the generated schema - create the pgEnum and properly type the table field.it might be okay to do this
I don't even get to generate the schema having the additional field typed like that
["AP", "HF"]
. I get the error I mentioned above.
However, now that I think about it...
TypeScript itself doesn't error out on that [on type: ["AP", "HF"]
]. So, if I just don't run the better-auth cli to generate the schema, then there we go, problem solved, hah. No BA CLI schema generation,no error😂 .