forked from kevinaskin/apijson-node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson.service.ts
More file actions
45 lines (37 loc) · 1.46 KB
/
json.service.ts
File metadata and controls
45 lines (37 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { Injectable} from '@nestjs/common'
import { InjectRepository } from '@nestjs/typeorm'
import { Repository } from 'typeorm'
import { UserEntity, CommentEntity } from '../../entities'
@Injectable()
export class JsonService {
@InjectRepository(UserEntity)
private readonly UserRepository: Repository<UserEntity>
@InjectRepository(CommentEntity)
private readonly CommentRepository: Repository<CommentEntity>
async insert(entityName: string, payload: any = {}): Promise<any> {
const currentRepository = this[`${entityName}Repository`]
return currentRepository && currentRepository.insert(payload)
}
async update(id: any, entityName: string, payload: any = {}): Promise<any> {
const currentRepository = this[`${entityName}Repository`]
return currentRepository && currentRepository.update(id, payload)
}
async findOne(entityName: string, options: any = {}): Promise<any> {
const currentRepository = this[`${entityName}Repository`]
return currentRepository && currentRepository.findOne(options)
}
async find(entityName: string, options: any = {}, listOptions: any = {
page: 1, count: 10
}): Promise<any[]> {
const currentRepository = this[`${entityName}Repository`]
const { select, ...restOptions} = options
return currentRepository && currentRepository.find({
where: {
...restOptions
},
select,
skip: (listOptions.page - 1) * listOptions.count,
take: listOptions.count
})
}
}