Skip to content

Commit

Permalink
Fix: Temporary pg repository, prisma annotate
Browse files Browse the repository at this point in the history
  • Loading branch information
J-Hoplin committed Jul 28, 2024
1 parent 026c671 commit ab251e1
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 159 deletions.
136 changes: 68 additions & 68 deletions src/modules/folders/folders.pg.repository.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import { FolderType } from '@prisma/client';
import { PrismaService } from '../prisma/prisma.service';
import { F002 } from './error';
// import { Injectable, NotFoundException } from '@nestjs/common';
// import { FolderType } from '@prisma/client';
// import { PrismaService } from '../prisma/prisma.service';
// import { F002 } from './error';

// TODO: Service class name will be changed after the naming convention is discussed later.
@Injectable()
export class FoldersPGRepository {
constructor(private readonly prisma: PrismaService) {}
// // TODO: Service class name will be changed after the naming convention is discussed later.
// @Injectable()
// export class FoldersPGRepository {
// constructor(private readonly prisma: PrismaService) {}

async create(userId: string, name: string, type: FolderType) {
const folder = await this.prisma.folder.create({
data: {
userId,
name,
type,
},
});
// async create(userId: string, name: string, type: FolderType) {
// const folder = await this.prisma.folder.create({
// data: {
// userId,
// name,
// type,
// },
// });

return folder;
}
// return folder;
// }

async createMany(
folders: { userId: string; name: string; type: FolderType }[],
) {
const createdFolders = await this.prisma.folder.createMany({
data: folders,
});
return createdFolders;
}
// async createMany(
// folders: { userId: string; name: string; type: FolderType }[],
// ) {
// const createdFolders = await this.prisma.folder.createMany({
// data: folders,
// });
// return createdFolders;
// }

async findByUserId(userId: string) {
const folders = await this.prisma.folder.findMany({
where: { userId },
});
return folders;
}
// async findByUserId(userId: string) {
// const folders = await this.prisma.folder.findMany({
// where: { userId },
// });
// return folders;
// }

async checkUserHasFolder(userId: string, name: string) {
const checkFolder = await this.prisma.folder.findFirst({
where: {
userId: userId,
name: name,
},
});
return checkFolder ? true : false;
}
// async checkUserHasFolder(userId: string, name: string) {
// const checkFolder = await this.prisma.folder.findFirst({
// where: {
// userId: userId,
// name: name,
// },
// });
// return checkFolder ? true : false;
// }

async findOneOrFail(param: { userId: string; name: string }) {
const folder = await this.prisma.folder.findFirst({
where: param,
});
if (!folder) {
throw new NotFoundException(F002);
}
// async findOneOrFail(param: { userId: string; name: string }) {
// const folder = await this.prisma.folder.findFirst({
// where: param,
// });
// if (!folder) {
// throw new NotFoundException(F002);
// }

return folder;
}
// return folder;
// }

async deleteAllCustomFolder(userId: string) {
await this.prisma.folder.deleteMany({
where: {
userId,
type: FolderType.custom,
},
});
}
// async deleteAllCustomFolder(userId: string) {
// await this.prisma.folder.deleteMany({
// where: {
// userId,
// type: FolderType.custom,
// },
// });
// }

async getDefaultFolder(userId: string) {
const folder = await this.prisma.folder.findFirst({
where: {
userId,
type: FolderType.default,
},
});
// async getDefaultFolder(userId: string) {
// const folder = await this.prisma.folder.findFirst({
// where: {
// userId,
// type: FolderType.default,
// },
// });

return folder;
}
}
// return folder;
// }
// }
16 changes: 8 additions & 8 deletions src/modules/prisma/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Global, Module } from '@nestjs/common';
import { PrismaService } from './prisma.service';
// import { Global, Module } from '@nestjs/common';
// import { PrismaService } from './prisma.service';

@Global()
@Module({
providers: [PrismaService],
exports: [PrismaService],
})
export class PrismaModule {}
// @Global()
// @Module({
// providers: [PrismaService],
// exports: [PrismaService],
// })
// export class PrismaModule {}
110 changes: 55 additions & 55 deletions src/modules/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,59 @@
import {
Injectable,
Logger,
OnModuleDestroy,
OnModuleInit,
} from '@nestjs/common';
import { Prisma, PrismaClient } from '@prisma/client';
// import {
// Injectable,
// Logger,
// OnModuleDestroy,
// OnModuleInit,
// } from '@nestjs/common';
// import { Prisma, PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService
extends PrismaClient<Prisma.PrismaClientOptions, Prisma.LogLevel>
implements OnModuleInit, OnModuleDestroy
{
private context = 'Prisma-Debug';
private logger = new Logger(this.context);
// @Injectable()
// export class PrismaService
// extends PrismaClient<Prisma.PrismaClientOptions, Prisma.LogLevel>
// implements OnModuleInit, OnModuleDestroy
// {
// private context = 'Prisma-Debug';
// private logger = new Logger(this.context);

// Prisma Logging Reference: https://www.prisma.io/docs/orm/prisma-client/observability-and-logging/logging#event-based-logging
constructor() {
super({
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'event',
level: 'error',
},
{
emit: 'event',
level: 'warn',
},
],
});
}
// // Prisma Logging Reference: https://www.prisma.io/docs/orm/prisma-client/observability-and-logging/logging#event-based-logging
// constructor() {
// super({
// log: [
// {
// emit: 'event',
// level: 'query',
// },
// {
// emit: 'event',
// level: 'error',
// },
// {
// emit: 'event',
// level: 'warn',
// },
// ],
// });
// }

async onModuleDestroy() {
await this.$disconnect();
}
// async onModuleDestroy() {
// await this.$disconnect();
// }

async onModuleInit() {
// Prisma Raw Query logging Config
Object.assign(
this,
this.$on('query', (event) => {
this.logger.debug(
`Query - [${event.timestamp}] - ${event.query}, Duration - ${event.duration}ms`,
);
}),
this.$on('error', (event) => {
this.logger.error(`Error - [${event.timestamp}] - ${event.message}`);
}),
this.$on('warn', (event) => {
this.logger.warn(`Warn - [${event.timestamp}] - ${event.message}`);
}),
);
await this.$connect();
}
}
// async onModuleInit() {
// // Prisma Raw Query logging Config
// Object.assign(
// this,
// this.$on('query', (event) => {
// this.logger.debug(
// `Query - [${event.timestamp}] - ${event.query}, Duration - ${event.duration}ms`,
// );
// }),
// this.$on('error', (event) => {
// this.logger.error(`Error - [${event.timestamp}] - ${event.message}`);
// }),
// this.$on('warn', (event) => {
// this.logger.warn(`Warn - [${event.timestamp}] - ${event.message}`);
// }),
// );
// await this.$connect();
// }
// }
56 changes: 28 additions & 28 deletions src/modules/users/users.pg.repository.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { Injectable } from '@nestjs/common';
import { PrismaService } from '../prisma/prisma.service';
// import { Injectable } from '@nestjs/common';
// import { PrismaService } from '../prisma/prisma.service';

// TODO: Service class name will be changed after the naming convention is discussed later.
@Injectable()
export class UsersPGRepository {
constructor(private readonly prisma: PrismaService) {}
private defaultFolderName = '나중에 읽을 링크';
// // TODO: Service class name will be changed after the naming convention is discussed later.
// @Injectable()
// export class UsersPGRepository {
// constructor(private readonly prisma: PrismaService) {}
// private defaultFolderName = '나중에 읽을 링크';

async findOrCreate(deviceToken: string) {
const user = await this.prisma.user.upsert({
where: {
deviceToken: deviceToken,
},
create: {
deviceToken: deviceToken,
folders: {
create: [
{
name: this.defaultFolderName,
type: 'default',
},
],
},
},
update: {},
});
return user;
}
}
// async findOrCreate(deviceToken: string) {
// const user = await this.prisma.user.upsert({
// where: {
// deviceToken: deviceToken,
// },
// create: {
// deviceToken: deviceToken,
// folders: {
// create: [
// {
// name: this.defaultFolderName,
// type: 'default',
// },
// ],
// },
// },
// update: {},
// });
// return user;
// }
// }

0 comments on commit ab251e1

Please sign in to comment.