-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
API Generator: Support position field (#2255)
Generate code to correctly update position fields in create, update and delete mutations. - position missing for create: add at the end - position set for create: update all later entities - position set for update: move others - entity deleted: remove gap - position in input is always optional, min(1) and int - position in input to high: set current max value - support for position-grouping, always adding scope if existing --------- Co-authored-by: Johannes Obermair <[email protected]>
- Loading branch information
1 parent
5a48ae4
commit f2da11d
Showing
13 changed files
with
508 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@comet/cms-api": minor | ||
--- | ||
|
||
API Generator: Add support for position field | ||
|
||
Add a field named `position` to enable this feature. This field will hold and update the position. This should be an integer number field >= 1. It's also possible to define fields (in CrudGenerator-Decorator) to group position by. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Migration } from '@mikro-orm/migrations'; | ||
|
||
export class Migration20240723114541 extends Migration { | ||
|
||
async up(): Promise<void> { | ||
this.addSql('delete from "ProductCategory";'); | ||
this.addSql('alter table "ProductCategory" add column "position" integer not null;'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
demo/api/src/products/generated/product-categories.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This file has been generated by comet api-generator. | ||
// You may choose to use this file as scaffold by moving this file out of generated folder and removing this comment. | ||
import { InjectRepository } from "@mikro-orm/nestjs"; | ||
import { EntityManager, EntityRepository } from "@mikro-orm/postgresql"; | ||
import { Injectable } from "@nestjs/common"; | ||
|
||
import { ProductCategory } from "../entities/product-category.entity"; | ||
|
||
@Injectable() | ||
export class ProductCategoriesService { | ||
constructor( | ||
private readonly entityManager: EntityManager, | ||
@InjectRepository(ProductCategory) private readonly repository: EntityRepository<ProductCategory>, | ||
) {} | ||
|
||
async incrementPositions(lowestPosition: number, highestPosition?: number) { | ||
// Increment positions between newPosition (inclusive) and oldPosition (exclusive) | ||
await this.repository.nativeUpdate( | ||
{ position: { $gte: lowestPosition, ...(highestPosition ? { $lt: highestPosition } : {}) } }, | ||
{ position: this.entityManager.raw("position + 1") }, | ||
); | ||
} | ||
|
||
async decrementPositions(lowestPosition: number, highestPosition?: number) { | ||
// Decrement positions between oldPosition (exclusive) and newPosition (inclusive) | ||
await this.repository.nativeUpdate( | ||
{ position: { $gt: lowestPosition, ...(highestPosition ? { $lte: highestPosition } : {}) } }, | ||
{ position: this.entityManager.raw("position - 1") }, | ||
); | ||
} | ||
|
||
async getLastPosition() { | ||
return this.repository.count({}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.