Deepkit ORM // Examples

User Entity with Password Hashing

Back to all Deepkit ORM examples
import {entity, AutoIncrement, PrimaryKey, cast} from "@deepkit/type";
import {Database} from '@deepkit/orm';
import * as bcrypt from 'bcrypt';

@entity.collection('users')
class User {
    id: number & PrimaryKey & AutoIncrement = 0;
    password: string = '';

    constructor(public username: string & Unique) {}
}

async function addUser(data: Partial<User>, database: Database) {
    const user = cast<User>(data);
    user.password = await bcrypt.hash(user.password, 10);
    await database.persist(user);
}