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
a336deb2
Commit
a336deb2
authored
Jul 16, 2022
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add more hooks
parent
1893d4fd
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
17 deletions
+70
-17
README.md
README.md
+26
-0
src/bases/time-base.ts
src/bases/time-base.ts
+25
-8
src/crud-base.ts
src/crud-base.ts
+19
-9
No files found.
README.md
View file @
a336deb2
...
...
@@ -36,6 +36,32 @@ export class User extends IdBase() {
@
NotColumn
()
somethingElse
:
any
;
// Would not come from client input, and would not go into OpenAPI document.
// possible optional override operations
override
isValidInCreation
()
{
// Custom before-create check.
if
(
!
this
.
name
.
length
)
{
return
'
Name cannot be empty!
'
;
}
}
override
isValidInUpdate
()
{
// Custom before-update check.
if
(
this
.
name
&&
!
this
.
name
.
length
)
{
return
'
Name cannot be empty!
'
;
}
}
override
async
beforeCreate
()
{
this
.
name
=
this
.
name
.
toLowerCase
();
// Do something before create.
}
override
async
afterCreate
()
{
this
.
name
=
this
.
name
.
toUpperCase
();
// Do something after create before sending to user.
}
override
async
beforeGet
()
{}
override
async
afterGet
()
{}
override
async
beforeUpdate
()
{}
}
```
...
...
src/bases/time-base.ts
View file @
a336deb2
...
...
@@ -6,15 +6,19 @@ export interface DeletionWise {
deleteTime
?:
Date
;
}
export
interface
ImportWise
{
isValidInCreation
():
string
|
undefined
;
beforeSaving
():
Promise
<
void
>
;
afterSaving
():
void
;
export
interface
EntityHooks
{
isValidInCreate
():
string
|
undefined
;
beforeCreate
():
Promise
<
void
>
;
afterCreate
():
Promise
<
void
>
;
beforeGet
():
Promise
<
void
>
;
afterGet
():
Promise
<
void
>
;
isValidInUpdate
():
string
|
undefined
;
beforeUpdate
():
Promise
<
void
>
;
}
export
class
TimeBase
extends
PageSettingsDto
implements
DeletionWise
,
ImportWise
implements
DeletionWise
,
EntityHooks
{
@
CreateDateColumn
({
select
:
false
})
@
NotColumn
()
...
...
@@ -28,15 +32,28 @@ export class TimeBase
@
NotColumn
()
deleteTime
:
Date
;
isValidInCreat
ion
():
string
|
undefined
{
isValidInCreat
e
():
string
|
undefined
{
return
;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
before
Saving
():
Promise
<
void
>
{}
async
before
Create
():
Promise
<
void
>
{}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
afterSaving
():
Promise
<
void
>
{}
async
afterCreate
():
Promise
<
void
>
{}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
beforeGet
():
Promise
<
void
>
{}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
afterGet
():
Promise
<
void
>
{}
isValidInUpdate
():
string
|
undefined
{
return
;
}
// eslint-disable-next-line @typescript-eslint/no-empty-function
async
beforeUpdate
():
Promise
<
void
>
{}
}
export
const
TimeBaseFields
:
(
keyof
TimeBase
)[]
=
[
...
...
src/crud-base.ts
View file @
a336deb2
...
...
@@ -15,7 +15,7 @@ import {
}
from
'
typeorm
'
;
import
{
DeletionWise
,
ImportWise
,
EntityHooks
,
PageSettingsFactory
,
QueryWise
,
}
from
'
./bases
'
;
...
...
@@ -38,7 +38,7 @@ export type ValidCrudEntity<T> = Record<string, any> & {
id
:
any
;
}
&
QueryWise
<
T
>
&
DeletionWise
&
ImportWise
&
EntityHooks
&
PageSettingsFactory
;
export
interface
CrudOptions
<
T
extends
ValidCrudEntity
<
T
>>
{
...
...
@@ -143,7 +143,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
400
,
'
Invalid entity
'
).
toException
();
}
const
invalidReason
=
ent
.
isValidInCreat
ion
();
const
invalidReason
=
ent
.
isValidInCreat
e
();
if
(
invalidReason
)
{
throw
new
BlankReturnMessageDto
(
400
,
invalidReason
).
toException
();
}
...
...
@@ -169,10 +169,10 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
if
(
beforeCreate
)
{
await
beforeCreate
(
repo
);
}
await
ent
.
before
Saving
();
await
ent
.
before
Create
();
try
{
const
savedEnt
=
await
repo
.
save
(
ent
as
DeepPartial
<
T
>
);
await
savedEnt
.
after
Saving
();
await
savedEnt
.
after
Create
();
}
catch
(
e
)
{
this
.
log
.
error
(
`Failed to create entity
${
JSON
.
stringify
(
ent
)}
:
${
e
.
toString
()}
`
,
...
...
@@ -246,6 +246,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
`
${
this
.
entityName
}
ID
${
id
}
not found.`
,
).
toException
();
}
await
ent
.
afterGet
();
return
new
this
.
entityReturnMessageDto
(
200
,
'
success
'
,
ent
);
}
...
...
@@ -258,6 +259,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
const
newEnt
=
new
this
.
entityClass
();
if
(
ent
)
{
Object
.
assign
(
newEnt
,
ent
);
await
newEnt
.
beforeGet
();
newEnt
.
applyQuery
(
query
,
this
.
entityAliasName
);
}
this
.
_applyRelationsToQuery
(
query
);
...
...
@@ -265,6 +267,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
extraQuery
(
query
);
try
{
const
[
ents
,
count
]
=
await
query
.
getManyAndCount
();
await
Promise
.
all
(
ents
.
map
((
ent
)
=>
ent
.
afterGet
()));
return
new
this
.
entityPaginatedReturnMessageDto
(
200
,
'
success
'
,
...
...
@@ -289,13 +292,20 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
cond
:
FindOptionsWhere
<
T
>
=
{},
)
{
let
result
:
UpdateResult
;
const
ent
=
new
this
.
entityClass
();
Object
.
assign
(
ent
,
entPart
);
const
invalidReason
=
ent
.
isValidInUpdate
();
if
(
invalidReason
)
{
throw
new
BlankReturnMessageDto
(
400
,
invalidReason
).
toException
();
}
await
ent
.
beforeUpdate
();
try
{
result
=
await
this
.
repo
.
update
(
{
id
,
...
cond
,
},
ent
Part
,
ent
,
);
}
catch
(
e
)
{
this
.
log
.
error
(
...
...
@@ -344,7 +354,7 @@ export class CrudBase<T extends ValidCrudEntity<T>> {
const
invalidResults
=
_
.
compact
(
await
Promise
.
all
(
ents
.
map
(
async
(
ent
)
=>
{
const
reason
=
ent
.
isValidInCreat
ion
();
const
reason
=
ent
.
isValidInCreat
e
();
if
(
reason
)
{
return
{
entry
:
ent
,
result
:
reason
};
}
...
...
@@ -360,9 +370,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
.
before
Saving
()));
await
Promise
.
all
(
remainingEnts
.
map
((
ent
)
=>
ent
.
before
Create
()));
const
data
=
await
this
.
_batchCreate
(
remainingEnts
,
undefined
,
true
);
await
Promise
.
all
(
data
.
results
.
map
((
e
)
=>
e
.
after
Saving
()));
await
Promise
.
all
(
data
.
results
.
map
((
e
)
=>
e
.
after
Create
()));
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