Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
I
init-things
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
nanahira
init-things
Commits
b8168cdb
Commit
b8168cdb
authored
Apr 01, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new
parent
de8ceaeb
Pipeline
#11416
passed with stages
in 1 minute and 29 seconds
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
125 additions
and
0 deletions
+125
-0
things/nest-typeorm/src/decorators/crud.decorators.ts
things/nest-typeorm/src/decorators/crud.decorators.ts
+125
-0
No files found.
things/nest-typeorm/src/decorators/crud.decorators.ts
0 → 100644
View file @
b8168cdb
import
{
ApiBody
,
ApiCreatedResponse
,
ApiNoContentResponse
,
ApiOkResponse
,
ApiOperation
,
ApiParam
,
ApiProperty
,
OmitType
,
PartialType
,
}
from
'
@nestjs/swagger
'
;
import
{
Body
,
Delete
,
Get
,
Patch
,
Post
,
Query
,
Type
}
from
'
@nestjs/common
'
;
import
{
BlankPaginatedReturnMessageDto
,
BlankReturnMessageDto
,
PaginatedReturnMessageDto
,
ReturnMessageDto
,
}
from
'
../dto/ReturnMessage.dto
'
;
import
{
TimeBase
,
TimeBaseFields
}
from
'
../entities/bases/TimeBase.entity
'
;
import
{
ClassGetPipe
,
CreatePipe
,
UpdatePipe
}
from
'
../utility/pipes
'
;
export
function
MergeMethodDecorators
(
decorators
:
MethodDecorator
[],
):
MethodDecorator
{
return
(
target
:
any
,
key
:
string
,
descriptor
:
PropertyDescriptor
)
=>
{
decorators
.
forEach
((
decorator
)
=>
{
decorator
(
target
,
key
,
descriptor
);
});
};
}
export
class
CrudFactory
<
T
extends
TimeBase
>
{
readonly
createDto
:
Type
<
Omit
<
T
,
keyof
T
>>
;
readonly
updateDto
:
Type
<
Partial
<
Omit
<
T
,
keyof
T
>>>
;
readonly
entityReturnMessageDto
:
Type
<
ReturnMessageDto
<
T
>>
;
readonly
entityArrayReturnMessageDto
:
Type
<
PaginatedReturnMessageDto
<
T
>>
;
constructor
(
public
readonly
entityClass
:
Type
<
T
>
,
fieldsToOmit
:
(
keyof
T
)[]
=
[],
// eslint-disable-next-line @typescript-eslint/ban-types
public
readonly
idType
:
Function
=
Number
,
)
{
this
.
createDto
=
OmitType
(
this
.
entityClass
,
[
...
TimeBaseFields
,
...
fieldsToOmit
,
]);
this
.
updateDto
=
PartialType
(
this
.
createDto
);
this
.
entityReturnMessageDto
=
class
EntityReturnMessageDto
extends
(
BlankReturnMessageDto
)
{
data
:
T
;
};
ApiProperty
({
type
:
this
.
entityClass
})(
this
.
entityReturnMessageDto
.
prototype
,
'
data
'
,
);
this
.
entityArrayReturnMessageDto
=
class
EntityArrayReturnMessageDto
extends
(
BlankPaginatedReturnMessageDto
)
{
data
:
T
[];
};
ApiProperty
({
type
:
[
this
.
entityClass
]
})(
this
.
entityArrayReturnMessageDto
.
prototype
,
'
data
'
,
);
}
create
():
MethodDecorator
{
return
MergeMethodDecorators
([
Post
(),
ApiOperation
({
summary
:
`Create a new
${
this
.
entityClass
.
name
}
`
}),
ApiBody
({
type
:
this
.
createDto
}),
ApiCreatedResponse
({
type
:
this
.
entityReturnMessageDto
}),
]);
}
createParam
()
{
return
Body
(
CreatePipe
);
}
findOne
():
MethodDecorator
{
return
MergeMethodDecorators
([
Get
(
'
:id
'
),
ApiOperation
({
summary
:
`Find a
${
this
.
entityClass
.
name
}
by id`
}),
ApiParam
({
name
:
'
id
'
,
type
:
this
.
idType
,
required
:
true
}),
ApiOkResponse
({
type
:
this
.
entityReturnMessageDto
}),
]);
}
findAll
():
MethodDecorator
{
return
MergeMethodDecorators
([
Get
(),
ApiOperation
({
summary
:
`Find all
${
this
.
entityClass
.
name
}
`
}),
ApiOkResponse
({
type
:
this
.
entityArrayReturnMessageDto
}),
]);
}
findAllParam
()
{
return
Query
(
new
ClassGetPipe
(
this
.
entityClass
));
}
update
():
MethodDecorator
{
return
MergeMethodDecorators
([
Patch
(
'
:id
'
),
ApiOperation
({
summary
:
`Update a
${
this
.
entityClass
.
name
}
by id`
}),
ApiParam
({
name
:
'
id
'
,
type
:
this
.
idType
,
required
:
true
}),
ApiBody
({
type
:
this
.
updateDto
}),
ApiOkResponse
({
type
:
BlankReturnMessageDto
}),
]);
}
updateParam
()
{
return
Body
(
UpdatePipe
);
}
delete
():
MethodDecorator
{
return
MergeMethodDecorators
([
Delete
(
'
:id
'
),
ApiOperation
({
summary
:
`Delete a
${
this
.
entityClass
.
name
}
by id`
}),
ApiParam
({
name
:
'
id
'
,
type
:
this
.
idType
,
required
:
true
}),
ApiNoContentResponse
({
type
:
BlankReturnMessageDto
}),
]);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment