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
5ea314ec
Commit
5ea314ec
authored
Jul 16, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
rename some entity abstract functions
parent
5d02661a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
28 deletions
+29
-28
src/bases/time-base.ts
src/bases/time-base.ts
+3
-7
src/crud-base.ts
src/crud-base.ts
+26
-21
No files found.
src/bases/time-base.ts
View file @
5ea314ec
...
...
@@ -8,7 +8,7 @@ export interface DeletionWise {
export
interface
ImportWise
{
isValidInCreation
():
string
|
undefined
;
prepareFor
Saving
():
Promise
<
void
>
;
before
Saving
():
Promise
<
void
>
;
afterSaving
():
void
;
}
...
...
@@ -28,19 +28,15 @@ export class TimeBase
@
NotColumn
()
deleteTime
:
Date
;
toObject
()
{
return
JSON
.
parse
(
JSON
.
stringify
(
this
));
}
isValidInCreation
():
string
|
undefined
{
return
;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
prepareFor
Saving
():
Promise
<
void
>
{}
async
before
Saving
():
Promise
<
void
>
{}
// eslint-disable-next-line @typescript-eslint/no-empty-function
a
fterSaving
()
{}
a
sync
afterSaving
():
Promise
<
void
>
{}
}
export
const
TimeBaseFields
:
(
keyof
TimeBase
)[]
=
[
...
...
src/crud-base.ts
View file @
5ea314ec
...
...
@@ -69,7 +69,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
this
.
extraGetQuery
=
crudOptions
.
extraGetQuery
||
((
qb
)
=>
{});
}
async
batchCreate
(
async
_
batchCreate
(
ents
:
T
[],
beforeCreate
?:
(
repo
:
Repository
<
T
>
)
=>
Promise
<
void
>
,
skipErrors
=
false
,
...
...
@@ -133,13 +133,20 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
this
.
log
.
error
(
`Failed to create entity
${
JSON
.
stringify
(
ents
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
});
return
result
;
}
async
create
(
ent
:
T
,
beforeCreate
?:
(
repo
:
Repository
<
T
>
)
=>
Promise
<
void
>
)
{
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
400
,
'
Invalid entity
'
).
toException
();
}
const
invalidReason
=
ent
.
isValidInCreation
();
if
(
invalidReason
)
{
throw
new
BlankReturnMessageDto
(
400
,
invalidReason
).
toException
();
}
const
savedEnt
=
await
this
.
repo
.
manager
.
transaction
(
async
(
mdb
)
=>
{
const
repo
=
mdb
.
getRepository
(
this
.
entityClass
);
if
(
ent
.
id
!=
null
)
{
...
...
@@ -162,13 +169,15 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
if
(
beforeCreate
)
{
await
beforeCreate
(
repo
);
}
await
ent
.
beforeSaving
();
try
{
return
await
repo
.
save
(
ent
as
DeepPartial
<
T
>
);
const
savedEnt
=
await
repo
.
save
(
ent
as
DeepPartial
<
T
>
);
await
savedEnt
.
afterSaving
();
}
catch
(
e
)
{
this
.
log
.
error
(
`Failed to create entity
${
JSON
.
stringify
(
ent
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
});
return
new
this
.
entityReturnMessageDto
(
201
,
'
success
'
,
savedEnt
);
...
...
@@ -178,7 +187,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
return
camelCase
(
this
.
entityName
);
}
applyRelationToQuery
(
qb
:
SelectQueryBuilder
<
T
>
,
relation
:
RelationDef
)
{
_
applyRelationToQuery
(
qb
:
SelectQueryBuilder
<
T
>
,
relation
:
RelationDef
)
{
const
{
name
}
=
relation
;
const
relationUnit
=
name
.
split
(
'
.
'
);
const
base
=
...
...
@@ -193,12 +202,12 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
qb
[
methodName
](
`
${
base
}
.
${
property
}
`
,
properyAlias
);
}
applyRelationsToQuery
(
qb
:
SelectQueryBuilder
<
T
>
)
{
_
applyRelationsToQuery
(
qb
:
SelectQueryBuilder
<
T
>
)
{
for
(
const
relation
of
this
.
entityRelations
)
{
if
(
typeof
relation
===
'
string
'
)
{
this
.
applyRelationToQuery
(
qb
,
{
name
:
relation
});
this
.
_
applyRelationToQuery
(
qb
,
{
name
:
relation
});
}
else
{
this
.
applyRelationToQuery
(
qb
,
relation
);
this
.
_
applyRelationToQuery
(
qb
,
relation
);
}
}
}
...
...
@@ -215,7 +224,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
const
query
=
this
.
queryBuilder
()
.
where
(
`
${
this
.
entityAliasName
}
.id = :id`
,
{
id
})
.
take
(
1
);
this
.
applyRelationsToQuery
(
query
);
this
.
_
applyRelationsToQuery
(
query
);
this
.
extraGetQuery
(
query
);
extraQuery
(
query
);
query
.
take
(
1
);
...
...
@@ -229,7 +238,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
'
,
'
,
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
...
...
@@ -251,7 +260,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
Object
.
assign
(
newEnt
,
ent
);
newEnt
.
applyQuery
(
query
,
this
.
entityAliasName
);
}
this
.
applyRelationsToQuery
(
query
);
this
.
_
applyRelationsToQuery
(
query
);
this
.
extraGetQuery
(
query
);
extraQuery
(
query
);
try
{
...
...
@@ -270,7 +279,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
ent
,
)}
with SQL
${
sql
}
param
${
params
.
join
(
'
,
'
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
}
...
...
@@ -294,7 +303,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
entPart
,
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
if
(
!
result
.
affected
)
{
throw
new
BlankReturnMessageDto
(
...
...
@@ -317,7 +326,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
:
this
.
repo
.
softDelete
(
searchCond
));
}
catch
(
e
)
{
this
.
log
.
error
(
`Failed to delete entity ID
${
id
}
:
${
e
.
toString
()}
`
);
throw
new
BlankReturnMessageDto
(
500
,
'
i
nternal error
'
).
toException
();
throw
new
BlankReturnMessageDto
(
500
,
'
I
nternal error
'
).
toException
();
}
if
(
!
result
.
affected
)
{
throw
new
BlankReturnMessageDto
(
...
...
@@ -351,13 +360,9 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
const
remainingEnts
=
ents
.
filter
(
(
ent
)
=>
!
invalidResults
.
find
((
result
)
=>
result
.
entry
===
ent
),
);
await
Promise
.
all
(
remainingEnts
.
map
((
ent
)
=>
ent
.
prepareForSaving
()));
const
data
=
await
this
.
batchCreate
(
remainingEnts
,
undefined
,
true
);
data
.
results
.
forEach
((
e
)
=>
{
if
(
e
.
afterSaving
)
{
e
.
afterSaving
();
}
});
await
Promise
.
all
(
remainingEnts
.
map
((
ent
)
=>
ent
.
beforeSaving
()));
const
data
=
await
this
.
_batchCreate
(
remainingEnts
,
undefined
,
true
);
await
Promise
.
all
(
data
.
results
.
map
((
e
)
=>
e
.
afterSaving
()));
const
results
=
[
...
invalidResults
,
...
data
.
skipped
,
...
...
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