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
971f1c8e
Commit
971f1c8e
authored
Nov 25, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
adapt
parent
69cf9949
Pipeline
#7149
passed with stages
in 1 minute and 28 seconds
Changes
7
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
40 additions
and
91 deletions
+40
-91
things/nest-typeorm/src/crud-base/crud-base.ts
things/nest-typeorm/src/crud-base/crud-base.ts
+23
-26
things/nest-typeorm/src/dto/PageSettings.dto.ts
things/nest-typeorm/src/dto/PageSettings.dto.ts
+3
-0
things/nest-typeorm/src/entities/bases/DeletionBase.entity.ts
...gs/nest-typeorm/src/entities/bases/DeletionBase.entity.ts
+0
-18
things/nest-typeorm/src/entities/bases/IdBase.entity.ts
things/nest-typeorm/src/entities/bases/IdBase.entity.ts
+2
-2
things/nest-typeorm/src/entities/bases/ManualIdBase.entity.ts
...gs/nest-typeorm/src/entities/bases/ManualIdBase.entity.ts
+2
-2
things/nest-typeorm/src/entities/bases/TimeBase.entity.ts
things/nest-typeorm/src/entities/bases/TimeBase.entity.ts
+10
-2
things/nest/src/dto/ReturnMessage.dto.ts
things/nest/src/dto/ReturnMessage.dto.ts
+0
-41
No files found.
things/nest-typeorm/src/crud-base/crud-base.ts
View file @
971f1c8e
import
{
ConsoleLogger
}
from
'
@nestjs/common
'
;
import
{
ClassConstructor
}
from
'
class-transformer
'
;
import
{
Repository
,
SelectQueryBuilder
,
UpdateResult
}
from
'
typeorm
'
;
import
{
IsNull
,
Not
,
Repository
,
SelectQueryBuilder
,
UpdateResult
,
}
from
'
typeorm
'
;
import
{
BlankReturnMessageDto
,
ReturnMessageDto
,
}
from
'
../dto/ReturnMessage.dto
'
;
import
{
DeletionWise
}
from
'
../entities/bases/DeletionBase.entity
'
;
import
{
QueryWise
}
from
'
../entities/interfaces/QueryWise
'
;
import
{
camelCase
}
from
'
typeorm/util/StringUtils
'
;
import
{
DeletionWise
}
from
'
../entities/bases/TimeBase.entity
'
;
export
type
EntityId
<
T
>
=
T
extends
{
id
:
string
}
?
string
...
...
@@ -38,13 +44,13 @@ export class CrudBase<
const
repo
=
mdb
.
getRepository
(
this
.
entityClass
);
if
(
ent
.
id
!=
null
)
{
const
existingEnt
=
await
repo
.
findOne
({
where
:
{
id
:
ent
.
id
},
take
:
1
,
select
:
[
'
id
'
,
'
isDeleted
'
]
,
where
:
{
id
:
ent
.
id
,
deleteTime
:
Not
(
IsNull
())
},
select
:
[
'
id
'
]
,
withDeleted
:
true
,
});
if
(
existingEnt
)
{
if
(
existingEnt
.
isDeleted
)
{
await
repo
.
delete
(
{
id
:
ent
.
id
}
);
if
(
existingEnt
)
{
await
repo
.
delete
(
existingEnt
.
id
);
}
else
{
throw
new
BlankReturnMessageDto
(
404
,
...
...
@@ -76,11 +82,7 @@ export class CrudBase<
:
`
${
relationUnit
[
relationUnit
.
length
-
2
]}
_
${
relationUnit
.
length
-
1
}
`
;
const
property
=
relationUnit
[
relationUnit
.
length
-
1
];
const
properyAlias
=
`
${
property
}
_
${
relationUnit
.
length
}
`
;
qb
.
leftJoinAndSelect
(
`
${
base
}
.
${
property
}
`
,
properyAlias
,
`
${
properyAlias
}
.isDeleted = false`
,
);
qb
.
leftJoinAndSelect
(
`
${
base
}
.
${
property
}
`
,
properyAlias
);
}
protected
applyRelationsToQuery
(
qb
:
SelectQueryBuilder
<
T
>
)
{
...
...
@@ -96,19 +98,12 @@ export class CrudBase<
async
findOne
(
id
:
EntityId
<
T
>
)
{
const
query
=
this
.
queryBuilder
()
.
where
(
`
${
this
.
entityAliasName
}
.id = :id`
,
{
id
})
.
andWhere
(
`
${
this
.
entityAliasName
}
.isDeleted = false`
)
.
take
(
1
);
this
.
applyRelationsToQuery
(
query
);
this
.
extraGetQuery
(
query
);
let
ent
:
T
;
try
{
const
ent
=
await
query
.
getOne
();
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
404
,
`ID
${
id
}
not found.`
,
).
toException
();
}
return
new
ReturnMessageDto
(
200
,
'
success
'
,
ent
);
ent
=
await
query
.
getOne
();
}
catch
(
e
)
{
const
[
sql
,
params
]
=
query
.
getQueryAndParameters
();
this
.
error
(
...
...
@@ -118,6 +113,10 @@ export class CrudBase<
);
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
}
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
404
,
`ID
${
id
}
not found.`
).
toException
();
}
return
new
ReturnMessageDto
(
200
,
'
success
'
,
ent
);
}
async
findAll
(
ent
?:
T
)
{
...
...
@@ -143,10 +142,10 @@ export class CrudBase<
async
update
(
id
:
EntityId
<
T
>
,
entPart
:
Partial
<
T
>
)
{
let
result
:
UpdateResult
;
try
{
result
=
await
this
.
repo
.
update
(
{
id
,
isDeleted
:
false
}
,
entPart
);
result
=
await
this
.
repo
.
update
(
id
,
entPart
);
}
catch
(
e
)
{
this
.
error
(
`Failed to
cre
ate entity ID
${
id
}
to
${
JSON
.
stringify
(
`Failed to
upd
ate entity ID
${
id
}
to
${
JSON
.
stringify
(
entPart
,
)}
:
${
e
.
toString
()}
`
,
);
...
...
@@ -161,9 +160,7 @@ export class CrudBase<
async
remove
(
id
:
EntityId
<
T
>
)
{
let
result
:
UpdateResult
;
try
{
result
=
await
this
.
repo
.
update
({
id
,
isDeleted
:
false
},
{
isDeleted
:
true
,
}
as
Partial
<
T
>
);
result
=
await
this
.
repo
.
softDelete
(
id
);
}
catch
(
e
)
{
this
.
error
(
`Failed to delete entity ID
${
id
}
:
${
e
.
toString
()}
`
);
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
...
...
things/nest-typeorm/src/dto/PageSettings.dto.ts
View file @
971f1c8e
import
{
IsInt
,
IsOptional
,
IsPositive
}
from
'
class-validator
'
;
import
{
SelectQueryBuilder
}
from
'
typeorm
'
;
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
export
class
PageSettingsDto
{
@
IsOptional
()
@
IsPositive
()
@
IsInt
()
@
ApiProperty
({
description
:
'
第 n 页,从 1 开始
'
})
pageCount
:
number
;
@
IsOptional
()
@
IsPositive
()
@
IsInt
()
@
ApiProperty
({
description
:
'
每页显示的数量
'
})
recordsPerPage
:
number
;
private
getRecordsPerPage
()
{
...
...
things/nest-typeorm/src/entities/bases/DeletionBase.entity.ts
deleted
100644 → 0
View file @
69cf9949
import
{
TimeBase
}
from
'
./TimeBase.entity
'
;
import
{
Column
,
SelectQueryBuilder
}
from
'
typeorm
'
;
import
{
Exclude
}
from
'
class-transformer
'
;
export
interface
DeletionWise
{
isDeleted
:
boolean
;
}
export
class
DeletionBase
extends
TimeBase
implements
DeletionWise
{
@
Column
(
'
boolean
'
,
{
select
:
false
,
default
:
false
})
@
Exclude
()
isDeleted
:
boolean
;
applyQuery
(
qb
:
SelectQueryBuilder
<
DeletionBase
>
,
entityName
:
string
)
{
super
.
applyQuery
(
qb
,
entityName
);
qb
.
where
(
`
${
entityName
}
.isDeleted = false`
);
}
}
things/nest-typeorm/src/entities/bases/IdBase.entity.ts
View file @
971f1c8e
import
{
DeletionBase
}
from
'
./DeletionBase.entity
'
;
import
{
Column
,
Generated
,
SelectQueryBuilder
}
from
'
typeorm
'
;
import
{
IdWise
}
from
'
../interfaces/wises
'
;
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
...
...
@@ -6,8 +5,9 @@ import { applyQueryProperty } from '../utility/query';
import
{
NotWritable
}
from
'
../decorators/transform
'
;
import
{
IsInt
,
IsPositive
}
from
'
class-validator
'
;
import
{
BigintTransformer
}
from
'
../utility/bigint-transform
'
;
import
{
TimeBase
}
from
'
./TimeBase.entity
'
;
export
class
IdBase
extends
Deletion
Base
implements
IdWise
{
export
class
IdBase
extends
Time
Base
implements
IdWise
{
@
Generated
(
'
increment
'
)
@
Column
(
'
bigint
'
,
{
primary
:
true
,
...
...
things/nest-typeorm/src/entities/bases/ManualIdBase.entity.ts
View file @
971f1c8e
import
{
PrimaryColumn
,
SelectQueryBuilder
}
from
'
typeorm
'
;
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
import
{
DeletionBase
}
from
'
./DeletionBase.entity
'
;
import
{
StringIdWise
}
from
'
../interfaces/wises
'
;
import
{
applyQueryProperty
}
from
'
../utility/query
'
;
import
{
NotChangeable
}
from
'
../decorators/transform
'
;
import
{
IsNotEmpty
,
IsString
}
from
'
class-validator
'
;
import
{
TimeBase
}
from
'
./TimeBase.entity
'
;
export
class
ManualIdBase
extends
Deletion
Base
implements
StringIdWise
{
export
class
ManualIdBase
extends
Time
Base
implements
StringIdWise
{
@
PrimaryColumn
(
'
varchar
'
,
{
length
:
32
})
@
ApiProperty
({
description
:
'
编号
'
})
@
NotChangeable
()
...
...
things/nest-typeorm/src/entities/bases/TimeBase.entity.ts
View file @
971f1c8e
import
{
CreateDateColumn
,
UpdateDateColumn
}
from
'
typeorm
'
;
import
{
CreateDateColumn
,
DeleteDateColumn
,
UpdateDateColumn
}
from
'
typeorm
'
;
import
{
Exclude
}
from
'
class-transformer
'
;
import
{
PageSettingsDto
}
from
'
../../dto/PageSettings.dto
'
;
export
class
TimeBase
extends
PageSettingsDto
{
export
interface
DeletionWise
{
deleteTime
?:
Date
;
}
export
class
TimeBase
extends
PageSettingsDto
implements
DeletionWise
{
@
CreateDateColumn
({
select
:
false
})
@
Exclude
()
createTime
:
Date
;
...
...
@@ -11,6 +15,10 @@ export class TimeBase extends PageSettingsDto {
@
Exclude
()
updateTime
:
Date
;
@
DeleteDateColumn
({
select
:
false
})
@
Exclude
()
deleteTime
:
Date
;
toObject
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
this
));
}
...
...
things/nest/src/dto/ReturnMessage.dto.ts
deleted
100644 → 0
View file @
69cf9949
import
{
ApiProperty
}
from
'
@nestjs/swagger
'
;
import
{
HttpException
}
from
'
@nestjs/common
'
;
export
interface
BlankReturnMessage
{
statusCode
:
number
;
message
:
string
;
success
:
boolean
;
}
export
interface
ReturnMessage
<
T
>
extends
BlankReturnMessage
{
data
?:
T
;
}
export
class
BlankReturnMessageDto
implements
BlankReturnMessage
{
@
ApiProperty
({
description
:
'
返回状态
'
})
statusCode
:
number
;
@
ApiProperty
({
description
:
'
返回信息
'
})
message
:
string
;
@
ApiProperty
({
description
:
'
是否成功
'
})
success
:
boolean
;
constructor
(
statusCode
:
number
,
message
?:
string
)
{
this
.
statusCode
=
statusCode
;
this
.
message
=
message
||
'
success
'
;
this
.
success
=
statusCode
<
400
;
}
toException
()
{
return
new
HttpException
(
this
,
this
.
statusCode
);
}
}
export
class
ReturnMessageDto
<
T
>
extends
BlankReturnMessageDto
implements
ReturnMessage
<
T
>
{
@
ApiProperty
({
description
:
'
返回内容
'
})
data
?:
T
;
constructor
(
statusCode
:
number
,
message
?:
string
,
data
?:
T
)
{
super
(
statusCode
,
message
);
this
.
data
=
data
;
}
}
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