How Should I Design My User Class When IDs Are Database-Generated?

0
2
Asked By CuriousCoder92 On

I'm trying to figure out how to implement my User class since the database generates the user IDs. My issue is what to pass to the `function createUser(user: User)` because if the ID is a `private readonly` field in the User class, I can't create a User instance until it gets an ID from the database. I've thought of a few approaches: 1) Making the `id?` field optional, which means I'll have to check if it's defined everywhere; 2) Using a union type `number | undefined` and creating static methods like `create` (for initial creation) and `fromDatabase` (after fetching from the database), but that dilutes the definition of my User object; 3) Creating a UserDTO class without strict id requirements, which doubles the number of entity files. I'm hoping to get some feedback on the best way to handle this without resorting to an ORM, as I'm trying to improve my SQL skills. What do you all think?

8 Answers

Answered By DevDynamo On

I suggest focusing on the basic user details like username or email and password. Hash the password, create a record in your database, and then return the new record including the auto-generated ID.

Answered By IDExpert On

You could use a UUID, nanoId, or a snowflake ID generator inside the `createUser` function, which would eliminate the need to handle IDs from the database.

Answered By NotAUserYet On

You might consider defining a type like `NotAUserYet = Omit` and using that in your `createUser` method.

Answered By SimplifyIt On

If `createUser` is the first method you run when a visitor interacts, do you really need to pass a user object? Maybe just have `createUser()` that generates and throws an error if the database call fails.

Answered By CodeWhisperer On

I'd advise against making the ID optional in your User class. Instead, consider using a Partial type for scenarios where the user isn't created yet, but keep that to a minimum to avoid issues later on.

Answered By LogicGuru On

It seems like you're caught between needing a user object and not being able to create one without an ID. I'd suggest separating the creation interface from the finalized User interface. For example:

```typescript
type CreateUserPayload = { name: string, password: string }
type User = { id: string, name: string }

function createUser(user: CreateUserPayload): User {
const result = db.insert(user);
return {
id: result.id,
name: result.name,
};
}
```

SyntaxSleuth -

You could also write it like this:
```typescript
function createUser(user: Omit): User {
```

Answered By SkepticalMind On
Answered By TableMaster On

Make sure that when you're setting up your user table, you set the ID field to auto-increment. This way, you won't need to worry about the ID at all during user creation; the database will manage it.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.