Skip to content

Commit

Permalink
Merge pull request #2 from ATeals/feature-prisma
Browse files Browse the repository at this point in the history
Feature : PrismaModule
  • Loading branch information
ATeals authored Jul 3, 2024
2 parents 68541f7 + ff335a1 commit 99badae
Show file tree
Hide file tree
Showing 14 changed files with 205 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ yarn-debug.log*
yarn-error.log*
lerna-debug.log*

pnpm-lock.yaml

# OS
.DS_Store

Expand Down Expand Up @@ -39,4 +41,4 @@ lerna-debug.log*
.env

#Docker

postgres-data
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
posetgres:
image: postgres:15
restart: always
ports:
- '5432:5432'
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: postgres
volumes:
- ./postgres-data:/var/lib/postgresql/data
3 changes: 3 additions & 0 deletions libs/prisma/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
node_modules
# Keep environment variables out of version control
.env
9 changes: 9 additions & 0 deletions libs/prisma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Prisma

> Prisma 관련 packages
### 의존성

- `@prisma/client`

### 기능
14 changes: 14 additions & 0 deletions libs/prisma/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@libs/prisma",
"version": "v0.0.1",
"description": "monorepo prisma lib",
"scripts": {},
"engines": {
"node": ">=14 <=16"
},
"dependencies": {
"@prisma/client": "^5.16.1",
"pg": "^8.11.5"
},
"license": "MIT"
}
3 changes: 3 additions & 0 deletions libs/prisma/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from './prisma.module';
export * from './prisma.service';
export * from './prisma.repository';
5 changes: 5 additions & 0 deletions libs/prisma/src/prisma.entity.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export class PrismaEntity<T> {
constructor(data: Partial<T>) {
Object.assign(this, data);
}
}
24 changes: 24 additions & 0 deletions libs/prisma/src/prisma.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Global, Module, DynamicModule } from '@nestjs/common';
import { PrismaService } from './prisma.service';
import { PrismaRepository } from './prisma.repository';

@Global()
@Module({
providers: [PrismaService, PrismaRepository],
exports: [PrismaService, PrismaRepository],
})
export class PrismaModule {
static forRoot({ service }: { service: new () => PrismaService }): DynamicModule {
return {
module: PrismaModule,
providers: [
{
provide: PrismaService,
useValue: new service(),
},
PrismaRepository,
],
exports: [PrismaService, PrismaRepository],
};
}
}
88 changes: 88 additions & 0 deletions libs/prisma/src/prisma.repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
export interface PrismaDelegate {
aggregate(data: any): any;
count(data: any): any;
create(data: any): any;
delete(data: any): any;
deleteMany(data: any): any;
findFirst(data: any): any;
findMany(data: any): any;
findUnique(data: any): any;
update(data: any): any;
updateMany(data: any): any;
upsert(data: any): any;
}

export class PrismaRepository<D extends PrismaDelegate> implements PrismaDelegate {
constructor(protected delegate: D, protected entity?: new (data: any) => any) {
return new Proxy(this, {
get: function (target, prop) {
const origMethod = target[prop];

if (typeof origMethod !== 'function' || prop === 'getDelegate' || !target.entity) {
return origMethod;
}

return async function (...args) {
const result = await origMethod.apply(target, args);
return target.responseSerializer(result);
};
},
});
}

public responseSerializer(data: any) {
if (Array.isArray(data)) {
return data.map((item) => new this.entity(item));
} else {
return new this.entity(data);
}
}

public getDelegate(): D {
return this.delegate;
}

aggregate(data: Parameters<D['aggregate']>[0]): ReturnType<D['aggregate']> {
return this.getDelegate().aggregate(data);
}

count(data: Parameters<D['count']>[0]): ReturnType<D['count']> {
return this.getDelegate().count(data);
}

create(data: Parameters<D['create']>[0]): ReturnType<D['create']> {
return this.getDelegate().create(data);
}

delete(data: Parameters<D['delete']>[0]): ReturnType<D['delete']> {
return this.getDelegate().delete(data);
}

deleteMany(data: Parameters<D['deleteMany']>[0]): ReturnType<D['deleteMany']> {
return this.getDelegate().deleteMany(data);
}

findFirst(data: Parameters<D['findFirst']>[0]): ReturnType<D['findFirst']> {
return this.getDelegate().findFirst(data);
}

async findMany(data: Parameters<D['findMany']>[0]): Promise<ReturnType<D['findMany']>> {
return await this.getDelegate().findMany(data);
}

findUnique(data: Parameters<D['findUnique']>[0]): ReturnType<D['findUnique']> {
return this.getDelegate().findUnique(data);
}

update(data: Parameters<D['update']>[0]): ReturnType<D['update']> {
return this.getDelegate().update(data);
}

updateMany(data: Parameters<D['updateMany']>[0]): ReturnType<D['updateMany']> {
return this.getDelegate().updateMany(data);
}

upsert(data: Parameters<D['upsert']>[0]): ReturnType<D['upsert']> {
return this.getDelegate().upsert(data);
}
}
13 changes: 13 additions & 0 deletions libs/prisma/src/prisma.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Injectable, OnModuleDestroy, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit, OnModuleDestroy {
async onModuleInit() {
await this.$connect();
}

async onModuleDestroy() {
await this.$disconnect();
}
}
9 changes: 9 additions & 0 deletions libs/prisma/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"declaration": true,
"outDir": "../../dist/libs/prisma"
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test", "**/*spec.ts"]
}
9 changes: 9 additions & 0 deletions nest-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@
"compilerOptions": {
"tsConfigPath": "libs/config/tsconfig.lib.json"
}
},
"prisma": {
"type": "library",
"root": "libs/prisma",
"entryFile": "index",
"sourceRoot": "libs/prisma/src",
"compilerOptions": {
"tsConfigPath": "libs/prisma/tsconfig.lib.json"
}
}
}
}
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
"@nestjs/common": "^10.0.0",
"@nestjs/core": "^10.0.0",
"@nestjs/platform-express": "^10.0.0",
"@nestjs/swagger": "^7.3.1",
"@prisma/client": "^5.16.1",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"pg": "^8.11.5",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1"
},
Expand Down Expand Up @@ -71,7 +76,8 @@
],
"moduleNameMapper": {
"^@app/test2(|/.*)$": "<rootDir>/libs/test2/src/$1",
"^@modules/config(|/.*)$": "<rootDir>/libs/config/src/$1"
"^@modules/config(|/.*)$": "<rootDir>/libs/config/src/$1",
"^@modules/prisma(|/.*)$": "<rootDir>/libs/prisma/src/$1"
}
}
}
6 changes: 6 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
],
"@modules/config/*": [
"libs/config/src/*"
],
"@modules/prisma": [
"libs/prisma/src"
],
"@modules/prisma/*": [
"libs/prisma/src/*"
]
}
}
Expand Down

0 comments on commit 99badae

Please sign in to comment.