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
5bf4f8fc
Commit
5bf4f8fc
authored
Dec 06, 2021
by
nanahira
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
batchCreate
parent
5f342950
Pipeline
#7582
passed with stages
in 1 minute and 51 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
56 additions
and
7 deletions
+56
-7
things/nest-typeorm/src/crud-base/crud-base.ts
things/nest-typeorm/src/crud-base/crud-base.ts
+56
-7
No files found.
things/nest-typeorm/src/crud-base/crud-base.ts
View file @
5bf4f8fc
...
...
@@ -3,8 +3,7 @@ import { ClassConstructor } from 'class-transformer';
import
{
DeleteResult
,
FindConditions
,
IsNull
,
Not
,
In
,
Repository
,
SelectQueryBuilder
,
UpdateResult
,
...
...
@@ -37,6 +36,47 @@ export class CrudBase<
this
.
entityName
=
entityClass
.
name
;
}
async
batchCreate
(
ents
:
T
[],
beforeCreate
?:
(
repo
:
Repository
<
T
>
)
=>
void
)
{
const
entsWithId
=
ents
.
filter
((
ent
)
=>
ent
.
id
!=
null
);
const
savedEnt
=
await
this
.
repo
.
manager
.
transaction
(
async
(
mdb
)
=>
{
const
repo
=
mdb
.
getRepository
(
this
.
entityClass
);
if
(
entsWithId
.
length
)
{
const
existingEnts
=
await
repo
.
find
({
where
:
{
id
:
In
(
entsWithId
.
map
((
ent
)
=>
ent
.
id
))
},
select
:
[
'
id
'
,
'
deleteTime
'
],
withDeleted
:
true
,
});
if
(
existingEnts
.
length
)
{
const
existingEntsWithoutDeleteTime
=
existingEnts
.
filter
(
(
ent
)
=>
ent
.
deleteTime
==
null
,
);
if
(
existingEntsWithoutDeleteTime
.
length
)
{
throw
new
BlankReturnMessageDto
(
404
,
`
${
this
.
entityName
}
ID
${
existingEntsWithoutDeleteTime
.
join
(
'
,
'
,
)}
already exists`
,
).
toException
();
}
await
repo
.
delete
(
existingEnts
.
map
((
ent
)
=>
ent
.
id
)
as
any
[]);
}
}
if
(
beforeCreate
)
{
await
beforeCreate
(
repo
);
}
try
{
return
await
repo
.
save
(
ents
);
}
catch
(
e
)
{
this
.
error
(
`Failed to create entity
${
JSON
.
stringify
(
ents
)}
:
${
e
.
toString
()}
`
,
);
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
}
});
return
new
ReturnMessageDto
(
201
,
'
success
'
,
savedEnt
);
}
async
create
(
ent
:
T
,
beforeCreate
?:
(
repo
:
Repository
<
T
>
)
=>
void
)
{
const
savedEnt
=
await
this
.
repo
.
manager
.
transaction
(
async
(
mdb
)
=>
{
const
repo
=
mdb
.
getRepository
(
this
.
entityClass
);
...
...
@@ -52,7 +92,7 @@ export class CrudBase<
}
else
{
throw
new
BlankReturnMessageDto
(
404
,
'
already exists
'
,
`
${
this
.
entityName
}
ID
${
ent
.
id
}
already exists`
,
).
toException
();
}
}
...
...
@@ -121,13 +161,16 @@ export class CrudBase<
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
}
if
(
!
ent
)
{
throw
new
BlankReturnMessageDto
(
404
,
`ID
${
id
}
not found.`
).
toException
();
throw
new
BlankReturnMessageDto
(
404
,
`
${
this
.
entityName
}
ID
${
id
}
not found.`
,
).
toException
();
}
return
new
ReturnMessageDto
(
200
,
'
success
'
,
ent
);
}
async
findAll
(
ent
?:
T
,
ent
?:
Partial
<
T
>
,
extraQuery
:
(
qb
:
SelectQueryBuilder
<
T
>
)
=>
void
=
()
=>
{},
)
{
const
query
=
this
.
queryBuilder
();
...
...
@@ -173,7 +216,10 @@ export class CrudBase<
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
}
if
(
!
result
.
affected
)
{
throw
new
BlankReturnMessageDto
(
404
,
`ID
${
id
}
not found.`
).
toException
();
throw
new
BlankReturnMessageDto
(
404
,
`
${
this
.
entityName
}
ID
${
id
}
not found.`
,
).
toException
();
}
return
new
BlankReturnMessageDto
(
200
,
'
success
'
);
}
...
...
@@ -197,7 +243,10 @@ export class CrudBase<
throw
new
BlankReturnMessageDto
(
500
,
'
internal error
'
).
toException
();
}
if
(
!
result
.
affected
)
{
throw
new
BlankReturnMessageDto
(
404
,
`ID
${
id
}
not found.`
).
toException
();
throw
new
BlankReturnMessageDto
(
404
,
`
${
this
.
entityName
}
ID
${
id
}
not found.`
,
).
toException
();
}
return
new
BlankReturnMessageDto
(
204
,
'
success
'
);
}
...
...
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