Commit 5bf4f8fc authored by nanahira's avatar nanahira

batchCreate

parent 5f342950
Pipeline #7582 passed with stages
in 1 minute and 51 seconds
......@@ -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');
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment