Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate user repository to kysely #15296

Merged
merged 15 commits into from
Jan 14, 2025
Merged
1 change: 1 addition & 0 deletions server/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export const DummyValue = {
BUFFER: Buffer.from('abcdefghi'),
DATE: new Date(),
TIME_BUCKET: '2024-01-01T00:00:00.000Z',
BOOLEAN: true,
};

export const GENERATE_SQL_KEY = 'generate-sql-key';
Expand Down
9 changes: 9 additions & 0 deletions server/src/entities/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { ExpressionBuilder } from 'kysely';
import { jsonArrayFrom } from 'kysely/helpers/postgres';
import { DB } from 'src/db';
import { AssetEntity } from 'src/entities/asset.entity';
import { TagEntity } from 'src/entities/tag.entity';
import { UserMetadataEntity } from 'src/entities/user-metadata.entity';
Expand Down Expand Up @@ -71,3 +74,9 @@ export class UserEntity {
@Column({ type: 'timestamptz', default: () => 'CURRENT_TIMESTAMP' })
profileChangedAt!: Date;
}

export const withMetadata = (eb: ExpressionBuilder<DB, 'users'>) => {
return jsonArrayFrom(
eb.selectFrom('user_metadata').selectAll('user_metadata').whereRef('users.id', '=', 'user_metadata.userId'),
).as('metadata');
};
16 changes: 9 additions & 7 deletions server/src/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Insertable, Updateable } from 'kysely';
import { Users } from 'src/db';
import { UserMetadata } from 'src/entities/user-metadata.entity';
import { UserEntity } from 'src/entities/user.entity';

Expand All @@ -23,17 +25,17 @@ export interface UserFindOptions {
export const IUserRepository = 'IUserRepository';

export interface IUserRepository {
get(id: string, options: UserFindOptions): Promise<UserEntity | null>;
getAdmin(): Promise<UserEntity | null>;
get(id: string, options: UserFindOptions): Promise<UserEntity | undefined>;
getAdmin(): Promise<UserEntity | undefined>;
hasAdmin(): Promise<boolean>;
getByEmail(email: string, withPassword?: boolean): Promise<UserEntity | null>;
getByStorageLabel(storageLabel: string): Promise<UserEntity | null>;
getByOAuthId(oauthId: string): Promise<UserEntity | null>;
getByEmail(email: string, withPassword?: boolean): Promise<UserEntity | undefined>;
getByStorageLabel(storageLabel: string): Promise<UserEntity | undefined>;
getByOAuthId(oauthId: string): Promise<UserEntity | undefined>;
getDeletedUsers(): Promise<UserEntity[]>;
getList(filter?: UserListFilter): Promise<UserEntity[]>;
getUserStats(): Promise<UserStatsQueryResponse[]>;
create(user: Partial<UserEntity>): Promise<UserEntity>;
update(id: string, user: Partial<UserEntity>): Promise<UserEntity>;
create(user: Insertable<Users>): Promise<UserEntity>;
update(id: string, user: Updateable<Users>): Promise<UserEntity>;
upsertMetadata<T extends keyof UserMetadata>(id: string, item: { key: T; value: UserMetadata[T] }): Promise<void>;
deleteMetadata<T extends keyof UserMetadata>(id: string, key: T): Promise<void>;
delete(user: UserEntity, hard?: boolean): Promise<UserEntity>;
Expand Down
Loading
Loading