assigning ids on item creation
This commit is contained in:
parent
550f6cbce3
commit
bf85f181ea
24
migration/1626475937881-itemIdAssigning.ts
Normal file
24
migration/1626475937881-itemIdAssigning.ts
Normal file
|
@ -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()
|
@InputType()
|
||||||
export class NewItemInput {
|
export class NewItemInput {
|
||||||
|
@Field((type) => ID, { nullable: true })
|
||||||
|
id?: string;
|
||||||
|
|
||||||
@Field()
|
@Field()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import {
|
import {
|
||||||
Column,
|
Column,
|
||||||
Entity,
|
Entity,
|
||||||
PrimaryGeneratedColumn,
|
PrimaryColumn,
|
||||||
Tree,
|
Tree,
|
||||||
TreeChildren,
|
TreeChildren,
|
||||||
TreeParent,
|
TreeParent,
|
||||||
|
@ -12,7 +12,7 @@ import {
|
||||||
closureTableName: 'item_closure',
|
closureTableName: 'item_closure',
|
||||||
})
|
})
|
||||||
export class Item {
|
export class Item {
|
||||||
@PrimaryGeneratedColumn('increment', { type: 'bigint' })
|
@PrimaryColumn({ type: 'bigint' })
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
|
|
|
@ -27,13 +27,34 @@ export class ItemsService {
|
||||||
}
|
}
|
||||||
|
|
||||||
async createItem(input: NewItemInput): Promise<Item> {
|
async createItem(input: NewItemInput): Promise<Item> {
|
||||||
const item = new Item();
|
const item = this.itemRepository.create({
|
||||||
item.name = input.name;
|
...input,
|
||||||
item.notes = input.notes;
|
id: input.id || undefined,
|
||||||
if (input.parent) {
|
parent: input.parent
|
||||||
const parent = await this.itemRepository.findOneOrFail(input.parent);
|
? await this.itemRepository.findOneOrFail(input.parent)
|
||||||
item.parent = parent;
|
: undefined,
|
||||||
}
|
});
|
||||||
return this.itemRepository.save(item);
|
|
||||||
|
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 a new issue