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
c4901b41
Commit
c4901b41
authored
Dec 11, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
paginate
parent
d70bebb5
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
5 deletions
+83
-5
things/nest-typeorm/src/crud-base/crud-base.ts
things/nest-typeorm/src/crud-base/crud-base.ts
+12
-2
things/nest-typeorm/src/dto/PageSettings.dto.ts
things/nest-typeorm/src/dto/PageSettings.dto.ts
+22
-2
things/nest-typeorm/src/dto/ReturnMessage.dto.ts
things/nest-typeorm/src/dto/ReturnMessage.dto.ts
+44
-0
things/nest-typeorm/src/utility/pipes.ts
things/nest-typeorm/src/utility/pipes.ts
+5
-1
No files found.
things/nest-typeorm/src/crud-base/crud-base.ts
View file @
c4901b41
...
...
@@ -10,11 +10,13 @@ import {
}
from
'
typeorm
'
;
import
{
BlankReturnMessageDto
,
PaginatedReturnMessageDto
,
ReturnMessageDto
,
}
from
'
../dto/ReturnMessage.dto
'
;
import
{
QueryWise
}
from
'
../entities/interfaces/QueryWise
'
;
import
{
camelCase
}
from
'
typeorm/util/StringUtils
'
;
import
{
DeletionWise
}
from
'
../entities/bases/TimeBase.entity
'
;
import
{
PageSettingsFactory
}
from
'
../dto/PageSettings.dto
'
;
export
type
EntityId
<
T
extends
{
id
:
any
}
>
=
T
[
'
id
'
];
...
...
@@ -22,7 +24,8 @@ export class CrudBase<
T
extends
Record
<
string
,
any
>
&
{
id
:
string
|
number
;
}
&
QueryWise
<
T
>
&
DeletionWise
DeletionWise
&
PageSettingsFactory
>
extends
ConsoleLogger
{
protected
readonly
entityName
:
string
;
constructor
(
...
...
@@ -181,7 +184,14 @@ export class CrudBase<
this
.
extraGetQuery
(
query
);
extraQuery
(
query
);
try
{
return
new
ReturnMessageDto
(
200
,
'
success
'
,
await
query
.
getMany
());
const
[
ents
,
count
]
=
await
query
.
getManyAndCount
();
return
new
PaginatedReturnMessageDto
(
200
,
'
success
'
,
ents
,
count
,
ent
.
getActualPageSettings
(),
);
}
catch
(
e
)
{
const
[
sql
,
params
]
=
query
.
getQueryAndParameters
();
this
.
error
(
...
...
things/nest-typeorm/src/dto/PageSettings.dto.ts
View file @
c4901b41
...
...
@@ -3,7 +3,16 @@ import { SelectQueryBuilder } from 'typeorm';
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
import
{
NotWritable
}
from
'
../entities/decorators/transform
'
;
export
class
PageSettingsDto
{
export
interface
PageSettingsWise
{
pageCount
:
number
;
recordsPerPage
:
number
;
}
export
interface
PageSettingsFactory
{
getActualPageSettings
():
PageSettingsWise
;
}
export
class
PageSettingsDto
implements
PageSettingsWise
,
PageSettingsFactory
{
@
NotWritable
()
@
IsPositive
()
@
IsInt
()
...
...
@@ -16,12 +25,23 @@ export class PageSettingsDto {
@
ApiProperty
({
description
:
'
每页显示的数量
'
,
required
:
false
})
recordsPerPage
:
number
;
getActualPageSettings
():
PageSettingsWise
{
return
{
pageCount
:
this
.
getPageCount
(),
recordsPerPage
:
this
.
getRecordsPerPage
(),
};
}
private
getPageCount
()
{
return
this
.
pageCount
||
1
;
}
private
getRecordsPerPage
()
{
return
this
.
recordsPerPage
||
25
;
}
private
getStartingFrom
()
{
return
(
(
this
.
pageCount
||
1
)
-
1
)
*
this
.
getRecordsPerPage
();
return
(
this
.
getPageCount
(
)
-
1
)
*
this
.
getRecordsPerPage
();
}
applyQuery
(
qb
:
SelectQueryBuilder
<
PageSettingsDto
>
,
entityName
:
string
)
{
...
...
things/nest-typeorm/src/dto/ReturnMessage.dto.ts
View file @
c4901b41
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
import
{
HttpException
}
from
'
@nestjs/common
'
;
import
{
User
}
from
'
../entities/User.entity
'
;
import
{
PageSettingsWise
}
from
'
./PageSettings.dto
'
;
export
interface
BlankReturnMessage
{
statusCode
:
number
;
...
...
@@ -29,6 +31,31 @@ export class BlankReturnMessageDto implements BlankReturnMessage {
}
}
export
class
BlankPaginatedReturnMessageDto
extends
BlankReturnMessageDto
implements
PageSettingsWise
{
@
ApiProperty
({
description
:
'
总数
'
})
total
:
number
;
@
ApiProperty
({
description
:
'
总页数
'
})
totalPages
:
number
;
@
ApiProperty
({
description
:
'
当前页
'
})
pageCount
:
number
;
@
ApiProperty
({
description
:
'
每页数量
'
})
recordsPerPage
:
number
;
constructor
(
statusCode
:
number
,
message
:
string
,
total
:
number
,
pageSettings
:
PageSettingsWise
,
)
{
super
(
statusCode
,
message
);
this
.
total
=
total
;
this
.
pageCount
=
pageSettings
.
pageCount
;
this
.
recordsPerPage
=
pageSettings
.
recordsPerPage
;
this
.
totalPages
=
Math
.
ceil
(
total
/
pageSettings
.
recordsPerPage
);
}
}
export
class
ReturnMessageDto
<
T
>
extends
BlankReturnMessageDto
implements
ReturnMessage
<
T
>
{
...
...
@@ -40,6 +67,23 @@ export class ReturnMessageDto<T>
}
}
export
class
PaginatedReturnMessageDto
<
T
>
extends
BlankPaginatedReturnMessageDto
implements
PageSettingsWise
{
@
ApiProperty
({
description
:
'
返回内容
'
})
data
:
T
[];
constructor
(
statusCode
:
number
,
message
:
string
,
data
:
T
[],
total
:
number
,
pageSettings
:
PageSettingsWise
,
)
{
super
(
statusCode
,
message
,
total
,
pageSettings
);
this
.
data
=
data
;
}
}
export
class
StringReturnMessageDto
extends
BlankReturnMessageDto
implements
ReturnMessage
<
string
>
{
...
...
things/nest-typeorm/src/utility/pipes.ts
View file @
c4901b41
...
...
@@ -18,6 +18,10 @@ export class ClassGetPipe<T> extends ValidationPipe {
constructor
(
private
readonly
classConstructor
:
ClassConstructor
<
T
>
)
{
super
({
transform
:
false
,
transformOptions
:
{
groups
:
[
'
r
'
],
enableImplicitConversion
:
true
,
},
skipMissingProperties
:
true
,
skipNullProperties
:
true
,
skipUndefinedProperties
:
true
,
...
...
@@ -28,7 +32,7 @@ export class ClassGetPipe<T> extends ValidationPipe {
const
obj
=
await
super
.
transform
(
value
,
metadata
);
return
plainToClass
(
this
.
classConstructor
,
obj
,
{
groups
:
[
'
r
'
],
enableImplicitConversion
:
tru
e
,
enableImplicitConversion
:
fals
e
,
});
}
}
...
...
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