Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
N
nicot
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
3rdeye
nicot
Commits
3fce2344
Commit
3fce2344
authored
Jul 16, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add readme
parent
bb7b7d18
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
86 additions
and
2 deletions
+86
-2
README.md
README.md
+84
-0
tests/module.spec.ts
tests/module.spec.ts
+2
-2
No files found.
README.md
View file @
3fce2344
# nicot
Nest.js interacting with class-validator + OpenAPI + TypeORM for Nest.js Restful API development.
## Install
In your Nest.js project, run the following command:
```
bash
npm
install
@nestjs/swagger typeorm @nestjs/typeorm class-validator class-transformer nicot
```
## Entity
Those decorators would all decorate the following, with the SAME settings.
-
TypeORM
`@Entity()`
settings.
-
class-validator validation settings.
-
`@nestjs/swagger`
`@ApiProperty()`
settings.
```
ts
@
Entity
()
export
class
User
extends
IdBase
()
{
@
Index
()
@
StringColumn
(
5
,
{
required
:
true
,
description
:
'
User name
'
,
})
name
:
string
;
@
IntColumn
(
'
int
'
,
{
unsigned
:
true
,
description
:
'
User age
'
,
default
:
20
})
age
:
number
;
@
EnumColumn
(
Gender
,
{
description
:
'
User gender
'
})
gender
:
Gender
;
}
```
## CrudService
Creates a service for database operation in one word.
```
ts
@
Injectable
()
export
class
UserService
extends
CrudService
(
User
)
{
constructor
(@
InjectDataSource
()
db
:
DataSource
)
{
super
(
db
.
getRepository
(
User
));
}
}
```
## Controller decorators
```
ts
const
dec
=
new
RestfulFactory
(
User
);
class
UpdateUserDto
extends
dec
.
updateDto
{}
// to extract type and class
@
Controller
(
'
user
'
)
export
class
UserController
{
constructor
(
private
userService
:
UserService
)
{}
@
dec
.
create
()
// POST /
create
(@
dec
.
createParam
()
user
:
User
)
{
return
this
.
userService
.
create
(
user
);
}
@
dec
.
findOne
()
// GET /:id
findOne
(@
dec
.
idParam
()
id
:
number
)
{
return
this
.
userService
.
findOne
(
id
);
}
@
dec
.
findAll
()
// GET /
findAll
(@
dec
.
findAllParam
()
user
:
UpdateUserDto
)
{
return
this
.
userService
.
findAll
(
user
);
}
@
dec
.
update
()
// PATH /:id
update
(@
dec
.
idParam
()
id
:
number
,
@
dec
.
updateParam
()
user
:
UpdateUserDto
)
{
return
this
.
userService
.
update
(
id
,
user
);
}
@
dec
.
delete
()
// DELETE /:id
delete
(@
dec
.
idParam
()
id
:
number
)
{
return
this
.
userService
.
delete
(
id
);
}
}
```
tests/module.spec.ts
View file @
3fce2344
...
...
@@ -4,13 +4,13 @@ import { CrudService } from '../src/crud-base';
import
{
Gender
,
User
}
from
'
./utility/user
'
;
import
{
RestfulFactory
}
from
'
../src/decorators
'
;
import
{
Test
}
from
'
@nestjs/testing
'
;
import
{
TypeOrmModule
}
from
'
@nestjs/typeorm
'
;
import
{
InjectDataSource
,
TypeOrmModule
}
from
'
@nestjs/typeorm
'
;
import
{
DataSource
}
from
'
typeorm
'
;
import
request
from
'
supertest
'
;
@
Injectable
()
class
UserService
extends
CrudService
(
User
)
{
constructor
(
db
:
DataSource
)
{
constructor
(
@
InjectDataSource
()
db
:
DataSource
)
{
super
(
db
.
getRepository
(
User
));
}
}
...
...
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