import { Kysely , Transaction } from 'kysely' ; import { Database } from '../database' ; import { InsertableUserRow , UserRow } from './user.table' ; export async function insertUser ( db , user ) { const insertedUser = await db . insertInto ( 'user' ) . values ( user ) . returningAll ( ) . executeTakeFirstOrThrow ( ) ; return insertedUser ; } export async function findUserById ( db , id ) { const user = await db . selectFrom ( 'user' ) . where ( 'user_id' , '=' , id ) . selectAll ( 'user' ) . executeTakeFirst ( ) ; return user ; } export async function lockUserById ( trx , id ) { return lockUser ( trx , 'user_id' , id ) ; } export async function lockUserByEmail ( trx , email ) { return lockUser ( trx , 'email' , email ) ; } async function lockUser ( trx , column , value ) { const user = await trx . selectFrom ( 'user' ) . where ( column , '=' , value ) . selectAll ( 'user' ) . forUpdate ( ) . executeTakeFirst ( ) ; return user ; } export async function setUserEmail ( db , id , email ) { await db . updateTable ( 'user' ) . where ( 'user_id' , '=' , id ) . set ( { email } ) . execute ( ) ; }