assigning ids on item creation
parent
550f6cbce3
commit
bf85f181ea
|
@ -0,0 +1,24 @@
|
|||
import {MigrationInterface, QueryRunner} from "typeorm";
|
||||
|
||||
export class itemIdAssigning1626475937881 implements MigrationInterface {
|
||||
name = 'itemIdAssigning1626475937881'
|
||||
|
||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "item" DROP CONSTRAINT "FK_2e3b654a1f669d356e259e7ca3c"`);
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" DROP CONSTRAINT "FK_854ec7dacad2df840aa8d4ee0a9"`);
|
||||
await queryRunner.query(`ALTER TABLE "item" ALTER COLUMN "id" DROP DEFAULT`);
|
||||
await queryRunner.query(`ALTER TABLE "item" ADD CONSTRAINT "FK_2e3b654a1f669d356e259e7ca3c" FOREIGN KEY ("parentId") REFERENCES "item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" ADD CONSTRAINT "FK_854ec7dacad2df840aa8d4ee0a9" FOREIGN KEY ("id_ancestor") REFERENCES "item"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" ADD CONSTRAINT "FK_58bff82facc806857035e516c9e" FOREIGN KEY ("id_descendant") REFERENCES "item"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
}
|
||||
|
||||
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" DROP CONSTRAINT "FK_58bff82facc806857035e516c9e"`);
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" DROP CONSTRAINT "FK_854ec7dacad2df840aa8d4ee0a9"`);
|
||||
await queryRunner.query(`ALTER TABLE "item" DROP CONSTRAINT "FK_2e3b654a1f669d356e259e7ca3c"`);
|
||||
await queryRunner.query(`ALTER TABLE "item" ALTER COLUMN "id" SET DEFAULT nextval('items_serial')`);
|
||||
await queryRunner.query(`ALTER TABLE "item_closure_closure" ADD CONSTRAINT "FK_854ec7dacad2df840aa8d4ee0a9" FOREIGN KEY ("id_ancestor") REFERENCES "item"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
|
||||
await queryRunner.query(`ALTER TABLE "item" ADD CONSTRAINT "FK_2e3b654a1f669d356e259e7ca3c" FOREIGN KEY ("parentId") REFERENCES "item"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
|
||||
}
|
||||
|
||||
}
|
|
@ -2,6 +2,9 @@ import { Field, ID, InputType } from '@nestjs/graphql';
|
|||
|
||||
@InputType()
|
||||
export class NewItemInput {
|
||||
@Field((type) => ID, { nullable: true })
|
||||
id?: string;
|
||||
|
||||
@Field()
|
||||
name: string;
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import {
|
||||
Column,
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
PrimaryColumn,
|
||||
Tree,
|
||||
TreeChildren,
|
||||
TreeParent,
|
||||
|
@ -12,7 +12,7 @@ import {
|
|||
closureTableName: 'item_closure',
|
||||
})
|
||||
export class Item {
|
||||
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
|
||||
@PrimaryColumn({ type: 'bigint' })
|
||||
id: string;
|
||||
|
||||
@Column()
|
||||
|
|
|
@ -27,13 +27,34 @@ export class ItemsService {
|
|||
}
|
||||
|
||||
async createItem(input: NewItemInput): Promise<Item> {
|
||||
const item = new Item();
|
||||
item.name = input.name;
|
||||
item.notes = input.notes;
|
||||
if (input.parent) {
|
||||
const parent = await this.itemRepository.findOneOrFail(input.parent);
|
||||
item.parent = parent;
|
||||
}
|
||||
return this.itemRepository.save(item);
|
||||
const item = this.itemRepository.create({
|
||||
...input,
|
||||
id: input.id || undefined,
|
||||
parent: input.parent
|
||||
? await this.itemRepository.findOneOrFail(input.parent)
|
||||
: undefined,
|
||||
});
|
||||
|
||||
const highestIdQuery = this.itemRepository
|
||||
.createQueryBuilder()
|
||||
.select('id')
|
||||
.orderBy('id', 'DESC')
|
||||
.limit(1);
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue