Files
twenty_crm/server/src/metadata/object-metadata/services/object-metadata.service.ts
Weiko 7fbef6d60d Add Tenant initialisation service (#2100)
* Add Tenant initialisation service

* few fixes

* fix constraint

* fix tests

* update metadata json with employees and address

* add V2

* remove metadata.gql
2023-10-18 18:01:52 +02:00

60 lines
2.0 KiB
TypeScript

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { TenantMigrationService } from 'src/metadata/tenant-migration/tenant-migration.service';
import { TenantMigrationTableAction } from 'src/metadata/tenant-migration/tenant-migration.entity';
import { MigrationRunnerService } from 'src/metadata/migration-runner/migration-runner.service';
import { ObjectMetadata } from 'src/metadata/object-metadata/object-metadata.entity';
@Injectable()
export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadata> {
constructor(
@InjectRepository(ObjectMetadata, 'metadata')
private readonly objectMetadataRepository: Repository<ObjectMetadata>,
private readonly tenantMigrationService: TenantMigrationService,
private readonly migrationRunnerService: MigrationRunnerService,
) {
super(objectMetadataRepository, { useSoftDelete: true });
}
override async createOne(record: ObjectMetadata): Promise<ObjectMetadata> {
const createdObjectMetadata = await super.createOne(record);
await this.tenantMigrationService.createCustomMigration(
createdObjectMetadata.workspaceId,
[
{
name: createdObjectMetadata.targetTableName,
action: 'create',
} satisfies TenantMigrationTableAction,
],
);
await this.migrationRunnerService.executeMigrationFromPendingMigrations(
createdObjectMetadata.workspaceId,
);
return createdObjectMetadata;
}
public async getObjectMetadataFromDataSourceId(dataSourceId: string) {
return this.objectMetadataRepository.find({
where: { dataSourceId },
relations: ['fields'],
});
}
public async findOneWithinWorkspace(
objectMetadataId: string,
workspaceId: string,
) {
return this.objectMetadataRepository.findOne({
where: { id: objectMetadataId, workspaceId },
});
}
}