Skip to content

Commit efbeca7

Browse files
authored
Merge pull request #10 from SecJS/feat/len-add-resource-class
Add resource class to Nest templates
2 parents e6fb16d + 351407b commit efbeca7

11 files changed

Lines changed: 178 additions & 47 deletions

app/templates/nestjsMongoose/__name__.ts.txt

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,3 @@ export class <%= namePascal %> {
88

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

app/templates/nestjsMongoose/__name__Service.ts.txt

Lines changed: 27 additions & 12 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'
@@ -13,13 +14,7 @@ export class <%= namePascal %>Service {
1314
@Inject(<%= namePascal %>Repository)
1415
private <%= nameCamel %>Repository: <%= namePascal %>Repository
1516

16-
@Options()
17-
async findAll(paginate: PaginationContract, options?: ApiRequestContract) {
18-
return this.<%= nameCamel %>Repository.getAll(paginate, options)
19-
}
20-
21-
@Options()
22-
async findOne(id: string, options?: ApiRequestContract) {
17+
async findOneAndValidate(id: string, options?: ApiRequestContract) {
2318
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.getOne(id, options)
2419

2520
if (!<%= nameCamel %>) {
@@ -29,19 +24,39 @@ export class <%= namePascal %>Service {
2924
return <%= nameCamel %>
3025
}
3126

27+
@Options()
28+
async findAll(paginate: PaginationContract, options?: ApiRequestContract) {
29+
const { data, meta, links }: any = await this.<%= nameCamel %>Repository.getAll(paginate, options)
30+
31+
return {
32+
meta,
33+
links,
34+
data: new <%= namePascal %>Resource().toArray(data),
35+
}
36+
}
37+
38+
@Options()
39+
async findOne(id: string, options?: ApiRequestContract) {
40+
const <%= nameCamel %> = await this.findOneAndValidate(id, options)
41+
42+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
43+
}
44+
3245
async createOne(dto: Create<%= namePascal %>Dto) {
33-
return this.<%= nameCamel %>Repository.storeOne(dto)
46+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.storeOne(dto)
47+
48+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
3449
}
3550

3651
async updateOne(id: string, dto: Update<%= namePascal %>Dto) {
37-
const <%= nameCamel %> = await this.findOne(id)
52+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.updateOne(await this.findOneAndValidate(id), dto)
3853

39-
return this.<%= nameCamel %>Repository.updateOne(<%= nameCamel %>, dto)
54+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4055
}
4156

4257
async deleteOne(id: string) {
43-
const <%= nameCamel %> = await this.findOne(id)
58+
const <%= nameCamel %> = await this.<%= nameCamel %>Repository.deleteOne(await this.findOneAndValidate(id))
4459

45-
return this.<%= nameCamel %>Repository.deleteOne(<%= nameCamel %>)
60+
return new <%= namePascal %>Resource().toJson(<%= nameCamel %>)
4661
}
4762
}
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
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "generator-secjs",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "🧬 Generator for any NodeJS Project or Framework",
55
"main": "app/index.js",
66
"scripts": {

tests/nestjsMongoose.spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ describe('\n NestJS Mongoose 😸', () => {
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/BarRepository.ts',
24+
'./Foo/BarController.ts',
2425
'./Foo/E2E/Bar/delete.spec.ts',
2526
'./Foo/E2E/Bar/index.spec.ts',
2627
'./Foo/E2E/Bar/show.spec.ts',

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',

0 commit comments

Comments
 (0)