feat: rename schema (#19891)

This commit is contained in:
Jason Rasmussen
2025-07-14 10:13:06 -04:00
committed by GitHub
parent 33c29e4305
commit c699df002a
103 changed files with 4378 additions and 3224 deletions

View File

@@ -34,12 +34,12 @@ export interface UserFindOptions {
withDeleted?: boolean;
}
const withMetadata = (eb: ExpressionBuilder<DB, 'users'>) => {
const withMetadata = (eb: ExpressionBuilder<DB, 'user'>) => {
return jsonArrayFrom(
eb
.selectFrom('user_metadata')
.select(['user_metadata.key', 'user_metadata.value'])
.whereRef('users.id', '=', 'user_metadata.userId'),
.whereRef('user.id', '=', 'user_metadata.userId'),
).as('metadata');
};
@@ -52,11 +52,11 @@ export class UserRepository {
options = options || {};
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.select(withMetadata)
.where('users.id', '=', userId)
.$if(!options.withDeleted, (eb) => eb.where('users.deletedAt', 'is', null))
.where('user.id', '=', userId)
.$if(!options.withDeleted, (eb) => eb.where('user.deletedAt', 'is', null))
.executeTakeFirst();
}
@@ -71,21 +71,21 @@ export class UserRepository {
@GenerateSql()
getAdmin() {
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.select(withMetadata)
.where('users.isAdmin', '=', true)
.where('users.deletedAt', 'is', null)
.where('user.isAdmin', '=', true)
.where('user.deletedAt', 'is', null)
.executeTakeFirst();
}
@GenerateSql()
async hasAdmin(): Promise<boolean> {
const admin = await this.db
.selectFrom('users')
.select('users.id')
.where('users.isAdmin', '=', true)
.where('users.deletedAt', 'is', null)
.selectFrom('user')
.select('user.id')
.where('user.isAdmin', '=', true)
.where('user.deletedAt', 'is', null)
.executeTakeFirst();
return !!admin;
@@ -94,59 +94,59 @@ export class UserRepository {
@GenerateSql({ params: [DummyValue.UUID] })
getForPinCode(id: string) {
return this.db
.selectFrom('users')
.select(['users.pinCode', 'users.password'])
.where('users.id', '=', id)
.where('users.deletedAt', 'is', null)
.selectFrom('user')
.select(['user.pinCode', 'user.password'])
.where('user.id', '=', id)
.where('user.deletedAt', 'is', null)
.executeTakeFirstOrThrow();
}
@GenerateSql({ params: [DummyValue.UUID] })
getForChangePassword(id: string) {
return this.db
.selectFrom('users')
.select(['users.id', 'users.password'])
.where('users.id', '=', id)
.where('users.deletedAt', 'is', null)
.selectFrom('user')
.select(['user.id', 'user.password'])
.where('user.id', '=', id)
.where('user.deletedAt', 'is', null)
.executeTakeFirstOrThrow();
}
@GenerateSql({ params: [DummyValue.EMAIL] })
getByEmail(email: string, options?: { withPassword?: boolean }) {
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.select(withMetadata)
.$if(!!options?.withPassword, (eb) => eb.select('password'))
.where('email', '=', email)
.where('users.deletedAt', 'is', null)
.where('user.deletedAt', 'is', null)
.executeTakeFirst();
}
@GenerateSql({ params: [DummyValue.STRING] })
getByStorageLabel(storageLabel: string) {
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.where('users.storageLabel', '=', storageLabel)
.where('users.deletedAt', 'is', null)
.where('user.storageLabel', '=', storageLabel)
.where('user.deletedAt', 'is', null)
.executeTakeFirst();
}
@GenerateSql({ params: [DummyValue.STRING] })
getByOAuthId(oauthId: string) {
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.select(withMetadata)
.where('users.oauthId', '=', oauthId)
.where('users.deletedAt', 'is', null)
.where('user.oauthId', '=', oauthId)
.where('user.deletedAt', 'is', null)
.executeTakeFirst();
}
@GenerateSql({ params: [DateTime.now().minus({ years: 1 })] })
getDeletedAfter(target: DateTime) {
return this.db.selectFrom('users').select(['id']).where('users.deletedAt', '<', target.toJSDate()).execute();
return this.db.selectFrom('user').select(['id']).where('user.deletedAt', '<', target.toJSDate()).execute();
}
@GenerateSql(
@@ -155,18 +155,18 @@ export class UserRepository {
)
getList({ id, withDeleted }: UserListFilter = {}) {
return this.db
.selectFrom('users')
.selectFrom('user')
.select(columns.userAdmin)
.select(withMetadata)
.$if(!withDeleted, (eb) => eb.where('users.deletedAt', 'is', null))
.$if(!!id, (eb) => eb.where('users.id', '=', id!))
.$if(!withDeleted, (eb) => eb.where('user.deletedAt', 'is', null))
.$if(!!id, (eb) => eb.where('user.id', '=', id!))
.orderBy('createdAt', 'desc')
.execute();
}
async create(dto: Insertable<UserTable>) {
return this.db
.insertInto('users')
.insertInto('user')
.values(dto)
.returning(columns.userAdmin)
.returning(withMetadata)
@@ -175,10 +175,10 @@ export class UserRepository {
update(id: string, dto: Updateable<UserTable>) {
return this.db
.updateTable('users')
.updateTable('user')
.set(dto)
.where('users.id', '=', asUuid(id))
.where('users.deletedAt', 'is', null)
.where('user.id', '=', asUuid(id))
.where('user.deletedAt', 'is', null)
.returning(columns.userAdmin)
.returning(withMetadata)
.executeTakeFirstOrThrow();
@@ -186,9 +186,9 @@ export class UserRepository {
restore(id: string) {
return this.db
.updateTable('users')
.updateTable('user')
.set({ status: UserStatus.ACTIVE, deletedAt: null })
.where('users.id', '=', asUuid(id))
.where('user.id', '=', asUuid(id))
.returning(columns.userAdmin)
.returning(withMetadata)
.executeTakeFirstOrThrow();
@@ -213,24 +213,24 @@ export class UserRepository {
delete(user: { id: string }, hard?: boolean) {
return hard
? this.db.deleteFrom('users').where('id', '=', user.id).execute()
: this.db.updateTable('users').set({ deletedAt: new Date() }).where('id', '=', user.id).execute();
? this.db.deleteFrom('user').where('id', '=', user.id).execute()
: this.db.updateTable('user').set({ deletedAt: new Date() }).where('id', '=', user.id).execute();
}
@GenerateSql()
getUserStats() {
return this.db
.selectFrom('users')
.leftJoin('assets', (join) => join.onRef('assets.ownerId', '=', 'users.id').on('assets.deletedAt', 'is', null))
.leftJoin('exif', 'exif.assetId', 'assets.id')
.select(['users.id as userId', 'users.name as userName', 'users.quotaSizeInBytes'])
.selectFrom('user')
.leftJoin('asset', (join) => join.onRef('asset.ownerId', '=', 'user.id').on('asset.deletedAt', 'is', null))
.leftJoin('asset_exif', 'asset_exif.assetId', 'asset.id')
.select(['user.id as userId', 'user.name as userName', 'user.quotaSizeInBytes'])
.select((eb) => [
eb.fn
.countAll<number>()
.filterWhere((eb) =>
eb.and([
eb('assets.type', '=', sql.lit(AssetType.IMAGE)),
eb('assets.visibility', '!=', sql.lit(AssetVisibility.HIDDEN)),
eb('asset.type', '=', sql.lit(AssetType.IMAGE)),
eb('asset.visibility', '!=', sql.lit(AssetVisibility.HIDDEN)),
]),
)
.as('photos'),
@@ -238,20 +238,23 @@ export class UserRepository {
.countAll<number>()
.filterWhere((eb) =>
eb.and([
eb('assets.type', '=', sql.lit(AssetType.VIDEO)),
eb('assets.visibility', '!=', sql.lit(AssetVisibility.HIDDEN)),
eb('asset.type', '=', sql.lit(AssetType.VIDEO)),
eb('asset.visibility', '!=', sql.lit(AssetVisibility.HIDDEN)),
]),
)
.as('videos'),
eb.fn
.coalesce(eb.fn.sum<number>('exif.fileSizeInByte').filterWhere('assets.libraryId', 'is', null), eb.lit(0))
.coalesce(
eb.fn.sum<number>('asset_exif.fileSizeInByte').filterWhere('asset.libraryId', 'is', null),
eb.lit(0),
)
.as('usage'),
eb.fn
.coalesce(
eb.fn
.sum<number>('exif.fileSizeInByte')
.sum<number>('asset_exif.fileSizeInByte')
.filterWhere((eb) =>
eb.and([eb('assets.libraryId', 'is', null), eb('assets.type', '=', sql.lit(AssetType.IMAGE))]),
eb.and([eb('asset.libraryId', 'is', null), eb('asset.type', '=', sql.lit(AssetType.IMAGE))]),
),
eb.lit(0),
)
@@ -259,45 +262,45 @@ export class UserRepository {
eb.fn
.coalesce(
eb.fn
.sum<number>('exif.fileSizeInByte')
.sum<number>('asset_exif.fileSizeInByte')
.filterWhere((eb) =>
eb.and([eb('assets.libraryId', 'is', null), eb('assets.type', '=', sql.lit(AssetType.VIDEO))]),
eb.and([eb('asset.libraryId', 'is', null), eb('asset.type', '=', sql.lit(AssetType.VIDEO))]),
),
eb.lit(0),
)
.as('usageVideos'),
])
.groupBy('users.id')
.orderBy('users.createdAt', 'asc')
.groupBy('user.id')
.orderBy('user.createdAt', 'asc')
.execute();
}
@GenerateSql({ params: [DummyValue.UUID, DummyValue.NUMBER] })
async updateUsage(id: string, delta: number): Promise<void> {
await this.db
.updateTable('users')
.updateTable('user')
.set({ quotaUsageInBytes: sql`"quotaUsageInBytes" + ${delta}`, updatedAt: new Date() })
.where('id', '=', asUuid(id))
.where('users.deletedAt', 'is', null)
.where('user.deletedAt', 'is', null)
.execute();
}
@GenerateSql({ params: [DummyValue.UUID] })
async syncUsage(id?: string) {
const query = this.db
.updateTable('users')
.updateTable('user')
.set({
quotaUsageInBytes: (eb) =>
eb
.selectFrom('assets')
.leftJoin('exif', 'exif.assetId', 'assets.id')
.select((eb) => eb.fn.coalesce(eb.fn.sum<number>('exif.fileSizeInByte'), eb.lit(0)).as('usage'))
.where('assets.libraryId', 'is', null)
.where('assets.ownerId', '=', eb.ref('users.id')),
.selectFrom('asset')
.leftJoin('asset_exif', 'asset_exif.assetId', 'asset.id')
.select((eb) => eb.fn.coalesce(eb.fn.sum<number>('asset_exif.fileSizeInByte'), eb.lit(0)).as('usage'))
.where('asset.libraryId', 'is', null)
.where('asset.ownerId', '=', eb.ref('user.id')),
updatedAt: new Date(),
})
.where('users.deletedAt', 'is', null)
.$if(id != undefined, (eb) => eb.where('users.id', '=', asUuid(id!)));
.where('user.deletedAt', 'is', null)
.$if(id != undefined, (eb) => eb.where('user.id', '=', asUuid(id!)));
await query.execute();
}