fix closure table saves on item creation
This commit is contained in:
parent
c1df170c6a
commit
93398c8279
|
@ -44,34 +44,33 @@ export class ItemsService {
|
|||
}
|
||||
|
||||
async createItem(input: NewItemInput): Promise<Item> {
|
||||
// must use Repository.save() for the closure table to work,
|
||||
// so we have to check whether the provided ID exists in the first place
|
||||
if (input.id && (await this.itemRepository.count({ id: input.id })) !== 0) {
|
||||
throw new Error('Item with this ID already exists');
|
||||
}
|
||||
|
||||
const highestId = input.id
|
||||
? // nobody cares in this case
|
||||
null
|
||||
: (
|
||||
await this.itemRepository.findOne({
|
||||
select: ['id'],
|
||||
order: { id: 'DESC' },
|
||||
})
|
||||
)?.id || '139999999999';
|
||||
|
||||
const item = this.itemRepository.create({
|
||||
...input,
|
||||
id: input.id || undefined,
|
||||
// if id not provided, use the highest one in db +1
|
||||
id: highestId ? (BigInt(highestId) + 1n).toString(10) : input.id,
|
||||
parent: input.parent
|
||||
? await this.itemRepository.findOneOrFail(input.parent)
|
||||
: undefined,
|
||||
});
|
||||
|
||||
const highestIdQuery = this.itemRepository
|
||||
.createQueryBuilder()
|
||||
.select('id')
|
||||
.orderBy('id', 'DESC')
|
||||
.limit(1);
|
||||
await this.itemRepository.save(item);
|
||||
|
||||
const insert = await this.itemRepository
|
||||
.createQueryBuilder()
|
||||
.insert()
|
||||
.into(Item)
|
||||
.values({
|
||||
...item,
|
||||
// if id is specified, use it.
|
||||
// if not, get the highest id in table and use highest_id+1
|
||||
id: input.id || (() => `((${highestIdQuery.getSql()}) + 1)`),
|
||||
})
|
||||
.returning(['id'])
|
||||
.execute();
|
||||
|
||||
item.id = insert.identifiers[0].id || item.id;
|
||||
return item;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
/* Basic Options */
|
||||
"incremental": true /* Enable incremental compilation */,
|
||||
"target": "es2017" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
|
||||
"target": "es2020" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', 'ES2021', or 'ESNEXT'. */,
|
||||
"module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */,
|
||||
// "lib": [], /* Specify library files to be included in the compilation. */
|
||||
// "allowJs": true, /* Allow javascript files to be compiled. */
|
||||
|
|
Loading…
Reference in a new issue