Untitled

mail@pastecode.io avatar
unknown
typescript
22 days ago
1.7 kB
3
Indexable
Never
import { Table } from 'drizzle-orm';
import { db } from "@server/ks-app/db/db";


export class User {
    Id!: string;
    UserName!: string;
    PasswordHash!: string;

    get UserId(): string {
        return this.Id;
    }

    genericGet<T extends Table>(t: T) {
        return db.select().from(t);
    }

    setCurrentUserId() {
        this.genericSet();
    }

    private genericSet() {
    ...
    }
}

import { User } from "../models/user";
import { userTable } from "@/schema";
import { eq } from "drizzle-orm";
import { singleOrDefault } from "@server/fd/fd-toolbox/collections/single-warnings";

export async function tryGetOneByUserName(userName: string): Promise<User | undefined> {
    const user = new User();
    const query = user.genericGet(userTable).where(eq(userTable.userName, userName));
    
    const result = await singleOrDefault(query);
    
    if (result) {
        user.Id = result.id;
        user.UserName = result.userName;
        user.PasswordHash = result.passwordHash;
        
        user.setCurrentUserId();
        
        console.log(`Retrieved user with ID: ${user.UserId}`);
        
        return user;
    }
    
    return undefined;
}


export async function getUser(id: string): Promise<User | undefined> {
    const user = new User();
    const query = user.genericGet(userTable).where(eq(userTable.id, id));
    
    const result = await singleOrDefault(query);
    
    if (result) {
        user.Id = result.id;
        user.UserName = result.userName;
        user.PasswordHash = result.passwordHash;
        
        user.setCurrentUserId();
        
        return user;
    }
    
    return undefined;
}

Leave a Comment