Skip to content

Commit

Permalink
handle deleted assets in extension
Browse files Browse the repository at this point in the history
  • Loading branch information
mertalev committed Mar 18, 2024
1 parent f3be6c2 commit 434c66d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
28 changes: 28 additions & 0 deletions server/src/infra/prisma/extensions/findNonDeleted.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { Prisma } from '@prisma/client';

const excludeDeleted = ({ args, query }: { args: any; query: any }) => {
if (args.where === undefined) {
args.where = { deletedAt: null };
} else if (args.where.deletedAt === undefined) {
args.where.deletedAt = null;
}

return query(args);
};

const findNonDeleted = {
findFirst: excludeDeleted,
findFirstOrThrow: excludeDeleted,
findMany: excludeDeleted,
findUnique: excludeDeleted,
findUniqueOrThrow: excludeDeleted,
};

export const findNonDeletedExtension = Prisma.defineExtension({
query: {
albums: findNonDeleted,
assets: findNonDeleted,
libraries: findNonDeleted,
users: findNonDeleted,
},
});
11 changes: 4 additions & 7 deletions server/src/infra/repositories/asset.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ export class AssetRepository implements IAssetRepository {
ownerId,
isVisible: true,
isArchived: false,
deletedAt: null,
resizePath: { not: null },
localDateTime: {
gte: date.startOf('day').toJSDate(),
Expand All @@ -97,7 +96,7 @@ export class AssetRepository implements IAssetRepository {
@ChunkedArray()
async getByIds(ids: string[], relations?: Prisma.AssetsInclude): Promise<AssetEntity[]> {
const res = await this.prismaRepository.assets.findMany({
where: { id: { in: ids }, deletedAt: null },
where: { id: { in: ids } },
include: {
...relations,
library: relations?.library ? { include: { assets: true, owner: true } } : undefined,
Expand All @@ -110,7 +109,7 @@ export class AssetRepository implements IAssetRepository {
@ChunkedArray()
async getByIdsWithAllRelations(ids: string[]): Promise<AssetEntity[]> {
const res = await this.prismaRepository.assets.findMany({
where: { id: { in: ids }, deletedAt: null },
where: { id: { in: ids } },
include: {
exifInfo: true,
smartInfo: true,
Expand Down Expand Up @@ -567,7 +566,6 @@ export class AssetRepository implements IAssetRepository {
const where = {
ownerId,
isVisible: true,
deletedAt: null,
};

const count = await this.prismaRepository.assets.count({ where });
Expand Down Expand Up @@ -643,7 +641,7 @@ export class AssetRepository implements IAssetRepository {
const res = await this.prismaRepository.exif.groupBy({
by: 'city',
where: {
assets: { ownerId, isVisible: true, isArchived: false, deletedAt: null, type: AssetType.IMAGE },
assets: { ownerId, isVisible: true, isArchived: false, type: AssetType.IMAGE },
city: { not: null },
},
having: {
Expand Down Expand Up @@ -688,7 +686,7 @@ export class AssetRepository implements IAssetRepository {
const res = await this.prismaRepository.smartInfo.groupBy({
by: 'tags',
where: {
assets: { ownerId, isVisible: true, isArchived: false, deletedAt: null, type: AssetType.IMAGE },
assets: { ownerId, isVisible: true, isArchived: false, type: AssetType.IMAGE },
},
having: {
assetId: {
Expand Down Expand Up @@ -736,7 +734,6 @@ export class AssetRepository implements IAssetRepository {
},
isVisible: true,
isArchived: false,
deletedAt: null,
OR: [
{
originalFileName: {
Expand Down
3 changes: 2 additions & 1 deletion server/src/infra/repositories/prisma.repository.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';
import { findNonDeletedExtension } from '../prisma/extensions/findNonDeleted';
import { metricsExtension } from '../prisma/extensions/metrics';

@Injectable()
export class PrismaRepository extends PrismaClient implements OnModuleInit, OnModuleDestroy {
constructor() {
super();
return this.$extends(metricsExtension) as this;
return this.$extends(metricsExtension).$extends(findNonDeletedExtension) as this;
}

async onModuleInit() {
Expand Down

0 comments on commit 434c66d

Please sign in to comment.