-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ATeals/feature-prisma
Feature : PrismaModule
- Loading branch information
Showing
14 changed files
with
205 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
# Keep environment variables out of version control | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Prisma | ||
|
||
> Prisma 관련 packages | ||
### 의존성 | ||
|
||
- `@prisma/client` | ||
|
||
### 기능 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters