-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathuser.repository.ts
31 lines (29 loc) · 1.13 KB
/
user.repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { UserRepository } from '@/modules/user/database/user.repository.port';
import { UserEntity, UserRoles } from '@/modules/user/domain/user.types';
import { Static, Type } from '@sinclair/typebox';
export const userSchema = Type.Object({
id: Type.String({ format: 'uuid' }),
createdAt: Type.String({ format: 'date-time' }),
updatedAt: Type.String({ format: 'date-time' }),
email: Type.String({ format: 'email' }),
country: Type.String({ minLength: 1, maxLength: 255 }),
postalCode: Type.String({ minLength: 1, maxLength: 20 }),
street: Type.String({ minLength: 1, maxLength: 255 }),
role: Type.Enum(UserRoles),
});
export type UserModel = Static<typeof userSchema>;
export default function userRepository({
db,
userMapper,
repositoryBase,
}: Dependencies): UserRepository {
const tableName = 'users';
return {
...repositoryBase({ tableName, mapper: userMapper }),
async findOneByEmail(email: string): Promise<UserEntity | undefined> {
const [user]: [UserModel?] =
await db`SELECT * FROM ${tableName} WHERE email = ${email} LIMIT 1`;
return user ? userMapper.toDomain(user) : undefined;
},
};
}