Remove Metadata SoftDelete and page limit size (#2237)
* Remove Metadata SoftDelete and page limit size * add cascade deletion * add missing queryRunner release
This commit is contained in:
@ -42,6 +42,8 @@ export class DataSourceService implements OnModuleInit, OnModuleDestroy {
|
|||||||
|
|
||||||
await queryRunner.createSchema(schemaName, true);
|
await queryRunner.createSchema(schemaName, true);
|
||||||
|
|
||||||
|
await queryRunner.release();
|
||||||
|
|
||||||
return schemaName;
|
return schemaName;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -3,7 +3,6 @@ import { Field, ID, ObjectType } from '@nestjs/graphql';
|
|||||||
import {
|
import {
|
||||||
Column,
|
Column,
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
DeleteDateColumn,
|
|
||||||
Entity,
|
Entity,
|
||||||
JoinColumn,
|
JoinColumn,
|
||||||
ManyToOne,
|
ManyToOne,
|
||||||
@ -35,11 +34,14 @@ export type FieldMetadataTargetColumnMap = {
|
|||||||
})
|
})
|
||||||
@QueryOptions({
|
@QueryOptions({
|
||||||
defaultResultSize: 10,
|
defaultResultSize: 10,
|
||||||
maxResultsSize: 100,
|
|
||||||
disableFilter: true,
|
disableFilter: true,
|
||||||
disableSort: true,
|
disableSort: true,
|
||||||
})
|
})
|
||||||
@Unique('IndexOnNameAndWorkspaceIdUnique', ['name', 'objectId', 'workspaceId'])
|
@Unique('IndexOnNameObjectIdAndWorkspaceIdUnique', [
|
||||||
|
'name',
|
||||||
|
'objectId',
|
||||||
|
'workspaceId',
|
||||||
|
])
|
||||||
export class FieldMetadata {
|
export class FieldMetadata {
|
||||||
@IDField(() => ID)
|
@IDField(() => ID)
|
||||||
@PrimaryGeneratedColumn('uuid')
|
@PrimaryGeneratedColumn('uuid')
|
||||||
@ -92,7 +94,9 @@ export class FieldMetadata {
|
|||||||
@Column({ nullable: false, name: 'workspace_id' })
|
@Column({ nullable: false, name: 'workspace_id' })
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
|
|
||||||
@ManyToOne(() => ObjectMetadata, (object) => object.fields)
|
@ManyToOne(() => ObjectMetadata, (object) => object.fields, {
|
||||||
|
onDelete: 'CASCADE',
|
||||||
|
})
|
||||||
@JoinColumn({ name: 'object_id' })
|
@JoinColumn({ name: 'object_id' })
|
||||||
object: ObjectMetadata;
|
object: ObjectMetadata;
|
||||||
|
|
||||||
@ -103,7 +107,4 @@ export class FieldMetadata {
|
|||||||
@Field()
|
@Field()
|
||||||
@UpdateDateColumn({ name: 'updated_at' })
|
@UpdateDateColumn({ name: 'updated_at' })
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
||||||
@DeleteDateColumn({ name: 'deleted_at' })
|
|
||||||
deletedAt?: Date;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,4 +1,5 @@
|
|||||||
import {
|
import {
|
||||||
|
BadRequestException,
|
||||||
ConflictException,
|
ConflictException,
|
||||||
Injectable,
|
Injectable,
|
||||||
NotFoundException,
|
NotFoundException,
|
||||||
@ -7,6 +8,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
|||||||
|
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||||
|
import { DeleteOneOptions } from '@ptc-org/nestjs-query-core';
|
||||||
|
|
||||||
import { FieldMetadata } from 'src/metadata/field-metadata/field-metadata.entity';
|
import { FieldMetadata } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||||
import {
|
import {
|
||||||
@ -28,7 +30,30 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadata> {
|
|||||||
private readonly tenantMigrationService: TenantMigrationService,
|
private readonly tenantMigrationService: TenantMigrationService,
|
||||||
private readonly migrationRunnerService: MigrationRunnerService,
|
private readonly migrationRunnerService: MigrationRunnerService,
|
||||||
) {
|
) {
|
||||||
super(fieldMetadataRepository, { useSoftDelete: true });
|
super(fieldMetadataRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
override async deleteOne(
|
||||||
|
id: string,
|
||||||
|
opts?: DeleteOneOptions<FieldMetadata> | undefined,
|
||||||
|
): Promise<FieldMetadata> {
|
||||||
|
const fieldMetadata = await this.fieldMetadataRepository.findOne({
|
||||||
|
where: { id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!fieldMetadata) {
|
||||||
|
throw new NotFoundException('Field does not exist');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!fieldMetadata.isCustom) {
|
||||||
|
throw new BadRequestException("Standard fields can't be deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (fieldMetadata.isActive) {
|
||||||
|
throw new BadRequestException("Active fields can't be deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.deleteOne(id, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async createOne(record: FieldMetadata): Promise<FieldMetadata> {
|
override async createOne(record: FieldMetadata): Promise<FieldMetadata> {
|
||||||
|
|||||||
@ -13,6 +13,7 @@ import { AddSoftDelete1697474804403 } from './migrations/1697474804403-addSoftDe
|
|||||||
import { RemoveSingularPluralFromFieldLabelAndName1697534910933 } from './migrations/1697534910933-removeSingularPluralFromFieldLabelAndName';
|
import { RemoveSingularPluralFromFieldLabelAndName1697534910933 } from './migrations/1697534910933-removeSingularPluralFromFieldLabelAndName';
|
||||||
import { AddNameAndIsCustomToTenantMigration1697622715467 } from './migrations/1697622715467-addNameAndIsCustomToTenantMigration';
|
import { AddNameAndIsCustomToTenantMigration1697622715467 } from './migrations/1697622715467-addNameAndIsCustomToTenantMigration';
|
||||||
import { AddUniqueConstraintsOnFieldObjectMetadata1697630766924 } from './migrations/1697630766924-addUniqueConstraintsOnFieldObjectMetadata';
|
import { AddUniqueConstraintsOnFieldObjectMetadata1697630766924 } from './migrations/1697630766924-addUniqueConstraintsOnFieldObjectMetadata';
|
||||||
|
import { RemoveMetadataSoftDelete1698328717102 } from './migrations/1698328717102-removeMetadataSoftDelete';
|
||||||
|
|
||||||
config();
|
config();
|
||||||
|
|
||||||
@ -37,6 +38,7 @@ export const typeORMMetadataModuleOptions: TypeOrmModuleOptions = {
|
|||||||
RemoveSingularPluralFromFieldLabelAndName1697534910933,
|
RemoveSingularPluralFromFieldLabelAndName1697534910933,
|
||||||
AddNameAndIsCustomToTenantMigration1697622715467,
|
AddNameAndIsCustomToTenantMigration1697622715467,
|
||||||
AddUniqueConstraintsOnFieldObjectMetadata1697630766924,
|
AddUniqueConstraintsOnFieldObjectMetadata1697630766924,
|
||||||
|
RemoveMetadataSoftDelete1698328717102,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -0,0 +1,37 @@
|
|||||||
|
import { MigrationInterface, QueryRunner } from 'typeorm';
|
||||||
|
|
||||||
|
export class RemoveMetadataSoftDelete1698328717102
|
||||||
|
implements MigrationInterface
|
||||||
|
{
|
||||||
|
name = 'RemoveMetadataSoftDelete1698328717102';
|
||||||
|
|
||||||
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" DROP CONSTRAINT "FK_38179b299795e48887fc99f937a"`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."object_metadata" DROP COLUMN "deleted_at"`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" DROP COLUMN "deleted_at"`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" ADD CONSTRAINT "FK_38179b299795e48887fc99f937a" FOREIGN KEY ("object_id") REFERENCES "metadata"."object_metadata"("id") ON DELETE CASCADE ON UPDATE NO ACTION`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" DROP CONSTRAINT "FK_38179b299795e48887fc99f937a"`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" ADD "deleted_at" TIMESTAMP`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."object_metadata" ADD "deleted_at" TIMESTAMP`,
|
||||||
|
);
|
||||||
|
await queryRunner.query(
|
||||||
|
`ALTER TABLE "metadata"."field_metadata" ADD CONSTRAINT "FK_38179b299795e48887fc99f937a" FOREIGN KEY ("object_id") REFERENCES "metadata"."object_metadata"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -3,7 +3,6 @@ import { ObjectType, ID, Field } from '@nestjs/graphql';
|
|||||||
import {
|
import {
|
||||||
Column,
|
Column,
|
||||||
CreateDateColumn,
|
CreateDateColumn,
|
||||||
DeleteDateColumn,
|
|
||||||
Entity,
|
Entity,
|
||||||
OneToMany,
|
OneToMany,
|
||||||
PrimaryGeneratedColumn,
|
PrimaryGeneratedColumn,
|
||||||
@ -32,7 +31,6 @@ import { BeforeCreateOneObject } from './hooks/before-create-one-object.hook';
|
|||||||
})
|
})
|
||||||
@QueryOptions({
|
@QueryOptions({
|
||||||
defaultResultSize: 10,
|
defaultResultSize: 10,
|
||||||
maxResultsSize: 100,
|
|
||||||
disableFilter: true,
|
disableFilter: true,
|
||||||
disableSort: true,
|
disableSort: true,
|
||||||
})
|
})
|
||||||
@ -89,7 +87,9 @@ export class ObjectMetadata {
|
|||||||
@Column({ nullable: false, name: 'workspace_id' })
|
@Column({ nullable: false, name: 'workspace_id' })
|
||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
|
|
||||||
@OneToMany(() => FieldMetadata, (field) => field.object)
|
@OneToMany(() => FieldMetadata, (field) => field.object, {
|
||||||
|
cascade: true,
|
||||||
|
})
|
||||||
fields: FieldMetadata[];
|
fields: FieldMetadata[];
|
||||||
|
|
||||||
@Field()
|
@Field()
|
||||||
@ -99,7 +99,4 @@ export class ObjectMetadata {
|
|||||||
@Field()
|
@Field()
|
||||||
@UpdateDateColumn({ name: 'updated_at' })
|
@UpdateDateColumn({ name: 'updated_at' })
|
||||||
updatedAt: Date;
|
updatedAt: Date;
|
||||||
|
|
||||||
@DeleteDateColumn({ name: 'deleted_at' })
|
|
||||||
deletedAt?: Date;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,8 +1,13 @@
|
|||||||
import { Injectable } from '@nestjs/common';
|
import {
|
||||||
|
BadRequestException,
|
||||||
|
Injectable,
|
||||||
|
NotFoundException,
|
||||||
|
} from '@nestjs/common';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||||
|
import { DeleteOneOptions } from '@ptc-org/nestjs-query-core';
|
||||||
|
|
||||||
import { TenantMigrationService } from 'src/metadata/tenant-migration/tenant-migration.service';
|
import { TenantMigrationService } from 'src/metadata/tenant-migration/tenant-migration.service';
|
||||||
import { TenantMigrationTableAction } from 'src/metadata/tenant-migration/tenant-migration.entity';
|
import { TenantMigrationTableAction } from 'src/metadata/tenant-migration/tenant-migration.entity';
|
||||||
@ -18,7 +23,30 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadata> {
|
|||||||
private readonly tenantMigrationService: TenantMigrationService,
|
private readonly tenantMigrationService: TenantMigrationService,
|
||||||
private readonly migrationRunnerService: MigrationRunnerService,
|
private readonly migrationRunnerService: MigrationRunnerService,
|
||||||
) {
|
) {
|
||||||
super(objectMetadataRepository, { useSoftDelete: true });
|
super(objectMetadataRepository);
|
||||||
|
}
|
||||||
|
|
||||||
|
override async deleteOne(
|
||||||
|
id: string,
|
||||||
|
opts?: DeleteOneOptions<ObjectMetadata> | undefined,
|
||||||
|
): Promise<ObjectMetadata> {
|
||||||
|
const objectMetadata = await this.objectMetadataRepository.findOne({
|
||||||
|
where: { id },
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!objectMetadata) {
|
||||||
|
throw new NotFoundException('Object does not exist');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!objectMetadata.isCustom) {
|
||||||
|
throw new BadRequestException("Standard Objects can't be deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (objectMetadata.isActive) {
|
||||||
|
throw new BadRequestException("Active objects can't be deleted");
|
||||||
|
}
|
||||||
|
|
||||||
|
return super.deleteOne(id, opts);
|
||||||
}
|
}
|
||||||
|
|
||||||
override async createOne(record: ObjectMetadata): Promise<ObjectMetadata> {
|
override async createOne(record: ObjectMetadata): Promise<ObjectMetadata> {
|
||||||
|
|||||||
Reference in New Issue
Block a user