Skip to content

Commit 351407b

Browse files
committed
feat: Resource class to nest typeorm and prisma
1 parent 1def086 commit 351407b

6 files changed

Lines changed: 110 additions & 20 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// import { isArrayOfObjects } from '@secjs/utils'
2+
3+
export interface <%= namePascal %>Json {
4+
id: string
5+
status: string
6+
createdAt: Date
7+
updatedAt: Date
8+
deletedAt?: Date
9+
}
10+
11+
export class <%= namePascal %>Resource {
12+
toJson(model: any): <%= namePascal %>Json {
13+
if (!model) return null
14+
15+
const json: <%= namePascal %>Json = {
16+
id: model.id,
17+
status: model.status,
18+
createdAt: model.createdAt,
19+
updatedAt: model.updatedAt,
20+
deletedAt: model.deletedAt,
21+
}
22+
23+
// if (model.relations && isArrayOfObjects(model.relations)) {
24+
// json.relations = new YourRelationResource().toArray(
25+
// model.relations,
26+
// )
27+
// }
28+
29+
return json
30+
}
31+
32+
toArray(models: any[]): <%= namePascal %>Json[] {
33+
return models.map(model => this.toJson(model))
34+
}
35+
}

app/templates/nestjsPrismaOrm/__name__Service.ts.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from 'app/Contracts/Dtos/<%= namePascal %>Dto'
55

66
import { Options } from 'app/Decorators/Services/Options'
7+
import { <%= namePascal %>Resource } from 'app/Resources/<%= namePascal %>Resource'
78
import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository'
89
import { Inject, Injectable, NotFoundException } from '@nestjs/common'
910
import { ApiRequestContract, PaginationContract } from '@secjs/contracts'
@@ -15,7 +16,13 @@ export class <%= namePascal %>Service {
1516

1617
@Options()
1718
async findAll(paginate: PaginationContract, options?: ApiRequestContract) {
18-
return this.<%= nameCamel %>Repository.getAll(paginate, options)
19+
const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options)
20+
21+
return {
22+
meta,
23+
links,
24+
data: new <%= namePascal %>Resource().toArray(data),
25+
}
1926
}
2027

2128
@Options()
@@ -26,22 +33,24 @@ export class <%= namePascal %>Service {
2633
throw new NotFoundException('NOT_FOUND_<%= nameUp %>')
2734
}
2835

29-
return <%= nameCamel %>
36+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
3037
}
3138

3239
async createOne(dto: Create<%= namePascal %>Dto) {
33-
return this.<%= nameCamel %>Repository.storeOne(dto)
40+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto)
41+
42+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
3443
}
3544

3645
async updateOne(id: string, dto: Update<%= namePascal %>Dto) {
37-
const <%= nameCamel %> = await this.findOne(id)
46+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOne(id), dto)
3847

39-
return this.<%= nameCamel %>Repository.updateOne(<%= nameCamel %>, dto)
48+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4049
}
4150

4251
async deleteOne(id: string) {
43-
const <%= nameCamel %> = await this.findOne(id)
52+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOne(id))
4453

45-
return this.<%= nameCamel %>Repository.deleteOne(<%= nameCamel %>)
54+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4655
}
4756
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// import { isArrayOfObjects } from '@secjs/utils'
2+
3+
export interface <%= namePascal %>Json {
4+
id: string
5+
status: string
6+
createdAt: Date
7+
updatedAt: Date
8+
deletedAt?: Date
9+
}
10+
11+
export class <%= namePascal %>Resource {
12+
toJson(model: any): <%= namePascal %>Json {
13+
if (!model) return null
14+
15+
const json: <%= namePascal %>Json = {
16+
id: model.id,
17+
status: model.status,
18+
createdAt: model.createdAt,
19+
updatedAt: model.updatedAt,
20+
deletedAt: model.deletedAt,
21+
}
22+
23+
// if (model.relations && isArrayOfObjects(model.relations)) {
24+
// json.relations = new YourRelationResource().toArray(
25+
// model.relations,
26+
// )
27+
// }
28+
29+
return json
30+
}
31+
32+
toArray(models: any[]): <%= namePascal %>Json[] {
33+
return models.map(model => this.toJson(model))
34+
}
35+
}

app/templates/nestjsTypeOrm/__name__Service.ts.txt

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
} from 'app/Contracts/Dtos/<%= namePascal %>Dto'
55

66
import { Options } from 'app/Decorators/Services/Options'
7+
import { <%= namePascal %>Resource } from 'app/Resources/<%= namePascal %>Resource'
78
import { <%= namePascal %>Repository } from 'app/Repositories/<%= namePascal %>Repository'
89
import { Inject, Injectable, NotFoundException } from '@nestjs/common'
910
import { ApiRequestContract, PaginationContract } from '@secjs/contracts'
@@ -15,7 +16,13 @@ export class <%= namePascal %>Service {
1516

1617
@Options()
1718
async findAll(paginate: PaginationContract, options?: ApiRequestContract) {
18-
return this.<%= nameCamel %>Repository.getAll(paginate, options)
19+
const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options)
20+
21+
return {
22+
meta,
23+
links,
24+
data: new <%= namePascal %>Resource().toArray(data),
25+
}
1926
}
2027

2128
@Options()
@@ -26,22 +33,24 @@ export class <%= namePascal %>Service {
2633
throw new NotFoundException('NOT_FOUND_<%= nameUp %>')
2734
}
2835

29-
return <%= nameCamel %>
36+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
3037
}
3138

3239
async createOne(dto: Create<%= namePascal %>Dto) {
33-
return this.<%= nameCamel %>Repository.storeOne(dto)
40+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto)
41+
42+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
3443
}
3544

3645
async updateOne(id: string, dto: Update<%= namePascal %>Dto) {
37-
const <%= nameCamel %> = await this.findOne(id)
46+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOne(id), dto)
3847

39-
return this.<%= nameCamel %>Repository.updateOne(<%= nameCamel %>, dto)
48+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4049
}
4150

4251
async deleteOne(id: string) {
43-
const <%= nameCamel %> = await this.findOne(id)
52+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOne(id))
4453

45-
return this.<%= nameCamel %>Repository.deleteOne(<%= nameCamel %>)
54+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4655
}
4756
}

tests/nestjsPrismaOrm.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,12 @@ describe('\n NestJS PrismaORM 😸', () => {
1515

1616
it('should create all files from resource Bar in folder Foo', () => {
1717
assert.file([
18-
'./Foo/BarController.ts',
19-
'./Foo/BarRepository.ts',
20-
'./Foo/BarService.ts',
2118
'./Foo/BarSeeder.ts',
19+
'./Foo/BarService.ts',
2220
'./Foo/Dtos/BarDto.ts',
21+
'./Foo/BarResource.ts',
22+
'./Foo/BarController.ts',
23+
'./Foo/BarRepository.ts',
2324
'./Foo/E2E/Bar/delete.spec.ts',
2425
'./Foo/E2E/Bar/index.spec.ts',
2526
'./Foo/E2E/Bar/show.spec.ts',

tests/nestjsTypeOrm.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ describe('\n NestJS TypeORM 😸', () => {
1616
it('should create all files from resource Bar in folder Foo', () => {
1717
assert.file([
1818
'./Foo/Bar.ts',
19-
'./Foo/BarController.ts',
20-
'./Foo/BarRepository.ts',
21-
'./Foo/BarService.ts',
2219
'./Foo/BarSeeder.ts',
20+
'./Foo/BarService.ts',
2321
'./Foo/Dtos/BarDto.ts',
22+
'./Foo/BarResource.ts',
23+
'./Foo/BarController.ts',
24+
'./Foo/BarRepository.ts',
2425
'./Foo/E2E/Bar/delete.spec.ts',
2526
'./Foo/E2E/Bar/index.spec.ts',
2627
'./Foo/E2E/Bar/show.spec.ts',

0 commit comments

Comments
 (0)