fix: nested relations not working and relations not prefixed (#2782)

* fix: nested relations n+n

* fix: prefix custom relations

* fix: only apply targetColumnMap when it's a custom object

* fix: force workspaceId to be provided

* fix: toIsCustom -> isToCustom

* fix: remove console.log
This commit is contained in:
Jérémy M
2023-12-01 15:26:48 +01:00
committed by GitHub
parent 6e6f0af26e
commit 474db1e142
28 changed files with 226 additions and 131 deletions

View File

@ -7,7 +7,7 @@ import {
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { v4 as uuidV4 } from 'uuid'; import { v4 as uuidV4 } from 'uuid';
import { Repository } from 'typeorm'; import { FindOneOptions, Repository } from 'typeorm';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm'; import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migration-runner/workspace-migration-runner.service'; import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migration-runner/workspace-migration-runner.service';
@ -47,8 +47,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
): Promise<FieldMetadataEntity> { ): Promise<FieldMetadataEntity> {
const objectMetadata = const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace( await this.objectMetadataService.findOneWithinWorkspace(
record.objectMetadataId,
record.workspaceId, record.workspaceId,
{
where: {
id: record.objectMetadataId,
},
},
); );
if (!objectMetadata) { if (!objectMetadata) {
@ -156,8 +160,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
const objectMetadata = const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace( await this.objectMetadataService.findOneWithinWorkspace(
existingFieldMetadata?.objectMetadataId,
record.workspaceId, record.workspaceId,
{
where: {
id: existingFieldMetadata?.objectMetadataId,
},
},
); );
if (!objectMetadata) { if (!objectMetadata) {
@ -200,11 +208,15 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
} }
public async findOneWithinWorkspace( public async findOneWithinWorkspace(
fieldMetadataId: string,
workspaceId: string, workspaceId: string,
options: FindOneOptions<FieldMetadataEntity>,
) { ) {
return this.fieldMetadataRepository.findOne({ return this.fieldMetadataRepository.findOne({
where: { id: fieldMetadataId, workspaceId }, ...options,
where: {
...options.where,
workspaceId,
},
}); });
} }

View File

@ -27,10 +27,11 @@ export class BeforeDeleteOneField implements BeforeDeleteOneHook<any> {
} }
const fieldMetadata = const fieldMetadata =
await this.fieldMetadataService.findOneWithinWorkspace( await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
instance.id.toString(), where: {
workspaceId, id: instance.id.toString(),
); },
});
if (!fieldMetadata) { if (!fieldMetadata) {
throw new BadRequestException('Field does not exist'); throw new BadRequestException('Field does not exist');

View File

@ -31,10 +31,11 @@ export class BeforeUpdateOneField<T extends UpdateFieldInput>
} }
const fieldMetadata = const fieldMetadata =
await this.fieldMetadataService.findOneWithinWorkspace( await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
instance.id.toString(), where: {
workspaceId, id: instance.id.toString(),
); },
});
if (!fieldMetadata) { if (!fieldMetadata) {
throw new BadRequestException('Field does not exist'); throw new BadRequestException('Field does not exist');

View File

@ -16,6 +16,7 @@ export interface FieldMetadataInterface<
defaultValue?: FieldMetadataDefaultValue<T>; defaultValue?: FieldMetadataDefaultValue<T>;
options?: FieldMetadataOptions<T>; options?: FieldMetadataOptions<T>;
objectMetadataId: string; objectMetadataId: string;
workspaceId?: string;
description?: string; description?: string;
isNullable?: boolean; isNullable?: boolean;
fromRelationMetadata?: RelationMetadataEntity; fromRelationMetadata?: RelationMetadataEntity;

View File

@ -3,6 +3,7 @@ import { BadRequestException } from '@nestjs/common';
import { FieldMetadataTargetColumnMap } from 'src/metadata/field-metadata/interfaces/field-metadata-target-column-map.interface'; import { FieldMetadataTargetColumnMap } from 'src/metadata/field-metadata/interfaces/field-metadata-target-column-map.interface';
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity'; import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';
/** /**
* Generate a target column map for a given type, this is used to map the field to the correct column(s) in the database. * Generate a target column map for a given type, this is used to map the field to the correct column(s) in the database.
@ -16,7 +17,9 @@ export function generateTargetColumnMap(
isCustomField: boolean, isCustomField: boolean,
fieldName: string, fieldName: string,
): FieldMetadataTargetColumnMap { ): FieldMetadataTargetColumnMap {
const columnName = isCustomField ? `_${fieldName}` : fieldName; const columnName = isCustomField
? createCustomColumnName(fieldName)
: fieldName;
switch (type) { switch (type) {
case FieldMetadataType.UUID: case FieldMetadataType.UUID:

View File

@ -26,10 +26,11 @@ export class BeforeDeleteOneObject implements BeforeDeleteOneHook<any> {
} }
const objectMetadata = const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace( await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
instance.id.toString(), where: {
workspaceId, id: instance.id.toString(),
); },
});
if (!objectMetadata) { if (!objectMetadata) {
throw new BadRequestException('Object does not exist'); throw new BadRequestException('Object does not exist');

View File

@ -39,10 +39,11 @@ export class BeforeUpdateOneObject<T extends UpdateObjectInput>
} }
const objectMetadata = const objectMetadata =
await this.objectMetadataService.findOneWithinWorkspace( await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
instance.id.toString(), where: {
workspaceId, id: instance.id.toString(),
); },
});
if (!objectMetadata) { if (!objectMetadata) {
throw new BadRequestException('Object does not exist'); throw new BadRequestException('Object does not exist');

View File

@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { Equal, In, Repository } from 'typeorm'; import { FindManyOptions, FindOneOptions, Repository } from 'typeorm';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm'; import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service'; import { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service';
@ -21,6 +21,7 @@ import {
RelationMetadataEntity, RelationMetadataEntity,
RelationMetadataType, RelationMetadataType,
} from 'src/metadata/relation-metadata/relation-metadata.entity'; } from 'src/metadata/relation-metadata/relation-metadata.entity';
import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';
import { ObjectMetadataEntity } from './object-metadata.entity'; import { ObjectMetadataEntity } from './object-metadata.entity';
@ -63,7 +64,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
const createdObjectMetadata = await super.createOne({ const createdObjectMetadata = await super.createOne({
...record, ...record,
dataSourceId: lastDataSourceMetadata.id, dataSourceId: lastDataSourceMetadata.id,
targetTableName: `_${record.nameSingular}`, targetTableName: createCustomColumnName(record.nameSingular),
isActive: true, isActive: true,
isCustom: true, isCustom: true,
isSystem: false, isSystem: false,
@ -298,39 +299,39 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
return createdObjectMetadata; return createdObjectMetadata;
} }
public async getObjectMetadataFromWorkspaceId(workspaceId: string) { public async findOneWithinWorkspace(
return this.objectMetadataRepository.find({ workspaceId: string,
where: { workspaceId }, options: FindOneOptions<ObjectMetadataEntity>,
): Promise<ObjectMetadataEntity | null> {
return this.objectMetadataRepository.findOne({
...options,
where: {
...options.where,
workspaceId,
},
relations: [ relations: [
'fields', 'fields',
'fields.fromRelationMetadata', 'fields.fromRelationMetadata',
'fields.fromRelationMetadata.fromObjectMetadata',
'fields.fromRelationMetadata.toObjectMetadata',
'fields.fromRelationMetadata.toObjectMetadata.fields',
'fields.toRelationMetadata', 'fields.toRelationMetadata',
'fields.toRelationMetadata.fromObjectMetadata',
'fields.toRelationMetadata.fromObjectMetadata.fields',
'fields.toRelationMetadata.toObjectMetadata',
], ],
}); });
} }
public async findOneWithinWorkspace(
objectMetadataId: string,
workspaceId: string,
) {
return this.objectMetadataRepository.findOne({
where: { id: objectMetadataId, workspaceId },
});
}
public async findManyWithinWorkspace( public async findManyWithinWorkspace(
objectMetadataIds: string[],
workspaceId: string, workspaceId: string,
options?: FindManyOptions<ObjectMetadataEntity>,
) { ) {
return this.objectMetadataRepository.findBy({ return this.objectMetadataRepository.find({
id: In(objectMetadataIds), ...options,
workspaceId: Equal(workspaceId), where: {
...options?.where,
workspaceId,
},
relations: [
'fields',
'fields.fromRelationMetadata',
'fields.toRelationMetadata',
],
}); });
} }

View File

@ -26,10 +26,11 @@ export class BeforeDeleteOneRelation implements BeforeDeleteOneHook<any> {
} }
const relationMetadata = const relationMetadata =
await this.relationMetadataService.findOneWithinWorkspace( await this.relationMetadataService.findOneWithinWorkspace(workspaceId, {
instance.id.toString(), where: {
workspaceId, id: instance.id.toString(),
); },
});
if (!relationMetadata) { if (!relationMetadata) {
throw new BadRequestException('Relation does not exist'); throw new BadRequestException('Relation does not exist');

View File

@ -6,7 +6,7 @@ import {
import { InjectRepository } from '@nestjs/typeorm'; import { InjectRepository } from '@nestjs/typeorm';
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm'; import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
import { Repository } from 'typeorm'; import { FindOneOptions, In, Repository } from 'typeorm';
import camelCase from 'lodash.camelcase'; import camelCase from 'lodash.camelcase';
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service'; import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
@ -16,6 +16,8 @@ import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migrati
import { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service'; import { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service';
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity'; import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
import { WorkspaceMigrationColumnActionType } from 'src/metadata/workspace-migration/workspace-migration.entity'; import { WorkspaceMigrationColumnActionType } from 'src/metadata/workspace-migration/workspace-migration.entity';
import { ObjectMetadataEntity } from 'src/metadata/object-metadata/object-metadata.entity';
import { createCustomColumnName } from 'src/metadata/utils/create-custom-column-name.util';
import { import {
RelationMetadataEntity, RelationMetadataEntity,
@ -85,14 +87,18 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
const objectMetadataEntries = const objectMetadataEntries =
await this.objectMetadataService.findManyWithinWorkspace( await this.objectMetadataService.findManyWithinWorkspace(
[record.fromObjectMetadataId, record.toObjectMetadataId],
record.workspaceId, record.workspaceId,
{
where: {
id: In([record.fromObjectMetadataId, record.toObjectMetadataId]),
},
},
); );
const objectMetadataMap = objectMetadataEntries.reduce((acc, curr) => { const objectMetadataMap = objectMetadataEntries.reduce((acc, curr) => {
acc[curr.id] = curr; acc[curr.id] = curr;
return acc; return acc;
}, {}); }, {} as { [key: string]: ObjectMetadataEntity });
if ( if (
objectMetadataMap[record.fromObjectMetadataId] === undefined || objectMetadataMap[record.fromObjectMetadataId] === undefined ||
@ -103,7 +109,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
); );
} }
const foreignKeyColumnName = `${camelCase(record.toName)}Id`; const baseColumnName = `${camelCase(record.toName)}Id`;
const isToCustom = objectMetadataMap[record.toObjectMetadataId].isCustom;
const foreignKeyColumnName = isToCustom
? createCustomColumnName(baseColumnName)
: baseColumnName;
const createdFields = await this.fieldMetadataService.createMany([ const createdFields = await this.fieldMetadataService.createMany([
// FROM // FROM
@ -126,7 +136,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
description: record.toDescription, description: record.toDescription,
icon: record.toIcon, icon: record.toIcon,
isCustom: true, isCustom: true,
targetColumnMap: {}, targetColumnMap: isToCustom
? {
value: createCustomColumnName(record.toName),
}
: {},
isActive: true, isActive: true,
type: FieldMetadataType.RELATION, type: FieldMetadataType.RELATION,
objectMetadataId: record.toObjectMetadataId, objectMetadataId: record.toObjectMetadataId,
@ -134,12 +148,14 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
}, },
// FOREIGN KEY // FOREIGN KEY
{ {
name: foreignKeyColumnName, name: baseColumnName,
label: `${record.toLabel} Foreign Key`, label: `${record.toLabel} Foreign Key`,
description: undefined, description: `${record.toDescription} Foreign Key`,
icon: undefined, icon: undefined,
isCustom: true, isCustom: true,
targetColumnMap: {}, targetColumnMap: {
value: foreignKeyColumnName,
},
isActive: true, isActive: true,
// Should not be visible on the front side // Should not be visible on the front side
isSystem: true, isSystem: true,
@ -203,11 +219,15 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
} }
public async findOneWithinWorkspace( public async findOneWithinWorkspace(
relationMetadataId: string,
workspaceId: string, workspaceId: string,
options: FindOneOptions<RelationMetadataEntity>,
) { ) {
return this.relationMetadataRepository.findOne({ return this.relationMetadataRepository.findOne({
where: { id: relationMetadataId, workspaceId }, ...options,
where: {
...options.where,
workspaceId,
},
relations: ['fromFieldMetadata', 'toFieldMetadata'], relations: ['fromFieldMetadata', 'toFieldMetadata'],
}); });
} }

View File

@ -0,0 +1,3 @@
export const createCustomColumnName = (name: string) => {
return `_${name}`;
};

View File

@ -20,11 +20,11 @@ export class CreateManyQueryFactory {
private readonly argsAliasFactory: ArgsAliasFactory, private readonly argsAliasFactory: ArgsAliasFactory,
) {} ) {}
create<Record extends IRecord = IRecord>( async create<Record extends IRecord = IRecord>(
args: CreateManyResolverArgs<Record>, args: CreateManyResolverArgs<Record>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
) { ) {
const fieldsString = this.fieldsStringFactory.create( const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -11,8 +11,11 @@ import { FieldsStringFactory } from './fields-string.factory';
export class DeleteManyQueryFactory { export class DeleteManyQueryFactory {
constructor(private readonly fieldsStringFactory: FieldsStringFactory) {} constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}
create(args: DeleteManyResolverArgs, options: WorkspaceQueryBuilderOptions) { async create(
const fieldsString = this.fieldsStringFactory.create( args: DeleteManyResolverArgs,
options: WorkspaceQueryBuilderOptions,
) {
const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -11,8 +11,11 @@ export class DeleteOneQueryFactory {
constructor(private readonly fieldsStringFactory: FieldsStringFactory) {} constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}
create(args: DeleteOneResolverArgs, options: WorkspaceQueryBuilderOptions) { async create(
const fieldsString = this.fieldsStringFactory.create( args: DeleteOneResolverArgs,
options: WorkspaceQueryBuilderOptions,
) {
const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -23,7 +23,7 @@ export class FieldsStringFactory {
create( create(
info: GraphQLResolveInfo, info: GraphQLResolveInfo,
fieldMetadataCollection: FieldMetadataInterface[], fieldMetadataCollection: FieldMetadataInterface[],
) { ): Promise<string> {
const selectedFields: Record<string, any> = graphqlFields(info); const selectedFields: Record<string, any> = graphqlFields(info);
return this.createFieldsStringRecursive( return this.createFieldsStringRecursive(
@ -33,12 +33,12 @@ export class FieldsStringFactory {
); );
} }
createFieldsStringRecursive( async createFieldsStringRecursive(
info: GraphQLResolveInfo, info: GraphQLResolveInfo,
selectedFields: Record<string, any>, selectedFields: Record<string, any>,
fieldMetadataCollection: FieldMetadataInterface[], fieldMetadataCollection: FieldMetadataInterface[],
accumulator = '', accumulator = '',
): string { ): Promise<string> {
const fieldMetadataMap = new Map( const fieldMetadataMap = new Map(
fieldMetadataCollection.map((metadata) => [metadata.name, metadata]), fieldMetadataCollection.map((metadata) => [metadata.name, metadata]),
); );
@ -54,7 +54,7 @@ export class FieldsStringFactory {
// If the field is a relation field, we need to create a special alias // If the field is a relation field, we need to create a special alias
if (isRelationFieldMetadataType(fieldMetadata.type)) { if (isRelationFieldMetadataType(fieldMetadata.type)) {
const alias = this.relationFieldAliasFactory.create( const alias = await this.relationFieldAliasFactory.create(
fieldKey, fieldKey,
fieldValue, fieldValue,
fieldMetadata, fieldMetadata,
@ -80,7 +80,7 @@ export class FieldsStringFactory {
!isEmpty(fieldValue) !isEmpty(fieldValue)
) { ) {
accumulator += `${fieldKey} {\n`; accumulator += `${fieldKey} {\n`;
accumulator = this.createFieldsStringRecursive( accumulator = await this.createFieldsStringRecursive(
info, info,
fieldValue, fieldValue,
fieldMetadataCollection, fieldMetadataCollection,

View File

@ -19,14 +19,14 @@ export class FindManyQueryFactory {
private readonly argsStringFactory: ArgsStringFactory, private readonly argsStringFactory: ArgsStringFactory,
) {} ) {}
create< async create<
Filter extends RecordFilter = RecordFilter, Filter extends RecordFilter = RecordFilter,
OrderBy extends RecordOrderBy = RecordOrderBy, OrderBy extends RecordOrderBy = RecordOrderBy,
>( >(
args: FindManyResolverArgs<Filter, OrderBy>, args: FindManyResolverArgs<Filter, OrderBy>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
) { ) {
const fieldsString = this.fieldsStringFactory.create( const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -16,11 +16,11 @@ export class FindOneQueryFactory {
private readonly argsStringFactory: ArgsStringFactory, private readonly argsStringFactory: ArgsStringFactory,
) {} ) {}
create<Filter extends RecordFilter = RecordFilter>( async create<Filter extends RecordFilter = RecordFilter>(
args: FindOneResolverArgs<Filter>, args: FindOneResolverArgs<Filter>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
) { ) {
const fieldsString = this.fieldsStringFactory.create( const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -11,6 +11,7 @@ import {
RelationDirection, RelationDirection,
} from 'src/workspace/utils/deduce-relation-direction.util'; } from 'src/workspace/utils/deduce-relation-direction.util';
import { getFieldArgumentsByKey } from 'src/workspace/workspace-query-builder/utils/get-field-arguments-by-key.util'; import { getFieldArgumentsByKey } from 'src/workspace/workspace-query-builder/utils/get-field-arguments-by-key.util';
import { ObjectMetadataService } from 'src/metadata/object-metadata/object-metadata.service';
import { FieldsStringFactory } from './fields-string.factory'; import { FieldsStringFactory } from './fields-string.factory';
import { ArgsStringFactory } from './args-string.factory'; import { ArgsStringFactory } from './args-string.factory';
@ -23,6 +24,7 @@ export class RelationFieldAliasFactory {
@Inject(forwardRef(() => FieldsStringFactory)) @Inject(forwardRef(() => FieldsStringFactory))
private readonly fieldsStringFactory: FieldsStringFactory, private readonly fieldsStringFactory: FieldsStringFactory,
private readonly argsStringFactory: ArgsStringFactory, private readonly argsStringFactory: ArgsStringFactory,
private readonly objectMetadataService: ObjectMetadataService,
) {} ) {}
create( create(
@ -30,7 +32,7 @@ export class RelationFieldAliasFactory {
fieldValue: any, fieldValue: any,
fieldMetadata: FieldMetadataInterface, fieldMetadata: FieldMetadataInterface,
info: GraphQLResolveInfo, info: GraphQLResolveInfo,
) { ): Promise<string> {
if (!isRelationFieldMetadataType(fieldMetadata.type)) { if (!isRelationFieldMetadataType(fieldMetadata.type)) {
throw new Error(`Field ${fieldMetadata.name} is not a relation field`); throw new Error(`Field ${fieldMetadata.name} is not a relation field`);
} }
@ -38,12 +40,12 @@ export class RelationFieldAliasFactory {
return this.createRelationAlias(fieldKey, fieldValue, fieldMetadata, info); return this.createRelationAlias(fieldKey, fieldValue, fieldMetadata, info);
} }
private createRelationAlias( private async createRelationAlias(
fieldKey: string, fieldKey: string,
fieldValue: any, fieldValue: any,
fieldMetadata: FieldMetadataInterface, fieldMetadata: FieldMetadataInterface,
info: GraphQLResolveInfo, info: GraphQLResolveInfo,
) { ): Promise<string> {
const relationMetadata = const relationMetadata =
fieldMetadata.fromRelationMetadata ?? fieldMetadata.toRelationMetadata; fieldMetadata.fromRelationMetadata ?? fieldMetadata.toRelationMetadata;
@ -53,14 +55,36 @@ export class RelationFieldAliasFactory {
); );
} }
if (!fieldMetadata.workspaceId) {
throw new Error(
`Workspace id not found for field ${fieldMetadata.name} in object metadata ${fieldMetadata.objectMetadataId}`,
);
}
const relationDirection = deduceRelationDirection( const relationDirection = deduceRelationDirection(
fieldMetadata.objectMetadataId, fieldMetadata.objectMetadataId,
relationMetadata, relationMetadata,
); );
// Retrieve the referenced object metadata based on the relation direction
// Mandatory to handle n+n relations
const referencedObjectMetadata = const referencedObjectMetadata =
relationDirection == RelationDirection.TO await this.objectMetadataService.findOneWithinWorkspace(
? relationMetadata.fromObjectMetadata fieldMetadata.workspaceId,
: relationMetadata.toObjectMetadata; {
where: {
id:
relationDirection == RelationDirection.TO
? relationMetadata.fromObjectMetadataId
: relationMetadata.toObjectMetadataId,
},
},
);
if (!referencedObjectMetadata) {
throw new Error(
`Referenced object metadata not found for relation ${relationMetadata.id}`,
);
}
// If it's a relation destination is of kind MANY, we need to add the collection suffix and extract the args // If it's a relation destination is of kind MANY, we need to add the collection suffix and extract the args
if ( if (
@ -70,21 +94,26 @@ export class RelationFieldAliasFactory {
const args = getFieldArgumentsByKey(info, fieldKey); const args = getFieldArgumentsByKey(info, fieldKey);
const argsString = this.argsStringFactory.create( const argsString = this.argsStringFactory.create(
args, args,
relationMetadata.toObjectMetadata.fields ?? [], referencedObjectMetadata.fields ?? [],
); );
const fieldsString =
await this.fieldsStringFactory.createFieldsStringRecursive(
info,
fieldValue,
referencedObjectMetadata.fields ?? [],
);
return ` return `
${fieldKey}: ${referencedObjectMetadata.targetTableName}Collection${ ${fieldKey}: ${referencedObjectMetadata.targetTableName}Collection${
argsString ? `(${argsString})` : '' argsString ? `(${argsString})` : ''
} { } {
${this.fieldsStringFactory.createFieldsStringRecursive( ${fieldsString}
info,
fieldValue,
relationMetadata.toObjectMetadata.fields ?? [],
)}
} }
`; `;
} }
let relationAlias = fieldMetadata.isCustom ? `_${fieldKey}` : fieldKey; let relationAlias = fieldMetadata.isCustom
? `${fieldKey}: ${fieldMetadata.targetColumnMap.value}`
: fieldKey;
// For one to one relations, pg_graphql use the targetTableName on the side that is not storing the foreign key // For one to one relations, pg_graphql use the targetTableName on the side that is not storing the foreign key
// so we need to alias it to the field key // so we need to alias it to the field key
@ -94,15 +123,17 @@ export class RelationFieldAliasFactory {
) { ) {
relationAlias = `${fieldKey}: ${referencedObjectMetadata.targetTableName}`; relationAlias = `${fieldKey}: ${referencedObjectMetadata.targetTableName}`;
} }
const fieldsString =
await this.fieldsStringFactory.createFieldsStringRecursive(
info,
fieldValue,
referencedObjectMetadata.fields ?? [],
);
// Otherwise it means it's a relation destination is of kind ONE // Otherwise it means it's a relation destination is of kind ONE
return ` return `
${relationAlias} { ${relationAlias} {
${this.fieldsStringFactory.createFieldsStringRecursive( ${fieldsString}
info,
fieldValue,
referencedObjectMetadata.fields ?? [],
)}
} }
`; `;
} }

View File

@ -18,14 +18,14 @@ export class UpdateManyQueryFactory {
private readonly argsAliasFactory: ArgsAliasFactory, private readonly argsAliasFactory: ArgsAliasFactory,
) {} ) {}
create< async create<
Record extends IRecord = IRecord, Record extends IRecord = IRecord,
Filter extends RecordFilter = RecordFilter, Filter extends RecordFilter = RecordFilter,
>( >(
args: UpdateManyResolverArgs<Record, Filter>, args: UpdateManyResolverArgs<Record, Filter>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
) { ) {
const fieldsString = this.fieldsStringFactory.create( const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -18,11 +18,11 @@ export class UpdateOneQueryFactory {
private readonly argsAliasFactory: ArgsAliasFactory, private readonly argsAliasFactory: ArgsAliasFactory,
) {} ) {}
create<Record extends IRecord = IRecord>( async create<Record extends IRecord = IRecord>(
args: UpdateOneResolverArgs<Record>, args: UpdateOneResolverArgs<Record>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
) { ) {
const fieldsString = this.fieldsStringFactory.create( const fieldsString = await this.fieldsStringFactory.create(
options.info, options.info,
options.fieldMetadataCollection, options.fieldMetadataCollection,
); );

View File

@ -44,35 +44,35 @@ export class WorkspaceQueryBuilderFactory {
>( >(
args: FindManyResolverArgs<Filter, OrderBy>, args: FindManyResolverArgs<Filter, OrderBy>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.findManyQueryFactory.create<Filter, OrderBy>(args, options); return this.findManyQueryFactory.create<Filter, OrderBy>(args, options);
} }
findOne<Filter extends RecordFilter = RecordFilter>( findOne<Filter extends RecordFilter = RecordFilter>(
args: FindOneResolverArgs<Filter>, args: FindOneResolverArgs<Filter>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.findOneQueryFactory.create<Filter>(args, options); return this.findOneQueryFactory.create<Filter>(args, options);
} }
createMany<Record extends IRecord = IRecord>( createMany<Record extends IRecord = IRecord>(
args: CreateManyResolverArgs<Record>, args: CreateManyResolverArgs<Record>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.createManyQueryFactory.create<Record>(args, options); return this.createManyQueryFactory.create<Record>(args, options);
} }
updateOne<Record extends IRecord = IRecord>( updateOne<Record extends IRecord = IRecord>(
initialArgs: UpdateOneResolverArgs<Record>, initialArgs: UpdateOneResolverArgs<Record>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.updateOneQueryFactory.create<Record>(initialArgs, options); return this.updateOneQueryFactory.create<Record>(initialArgs, options);
} }
deleteOne( deleteOne(
args: DeleteOneResolverArgs, args: DeleteOneResolverArgs,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.deleteOneQueryFactory.create(args, options); return this.deleteOneQueryFactory.create(args, options);
} }
@ -82,14 +82,14 @@ export class WorkspaceQueryBuilderFactory {
>( >(
args: UpdateManyResolverArgs<Record, Filter>, args: UpdateManyResolverArgs<Record, Filter>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.updateManyQueryFactory.create(args, options); return this.updateManyQueryFactory.create(args, options);
} }
deleteMany<Filter extends RecordFilter = RecordFilter>( deleteMany<Filter extends RecordFilter = RecordFilter>(
args: DeleteManyResolverArgs<Filter>, args: DeleteManyResolverArgs<Filter>,
options: WorkspaceQueryBuilderOptions, options: WorkspaceQueryBuilderOptions,
): string { ): Promise<string> {
return this.deleteManyQueryFactory.create(args, options); return this.deleteManyQueryFactory.create(args, options);
} }
} }

View File

@ -1,11 +1,13 @@
import { Module } from '@nestjs/common'; import { Module } from '@nestjs/common';
import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module';
import { WorkspaceQueryBuilderFactory } from './workspace-query-builder.factory'; import { WorkspaceQueryBuilderFactory } from './workspace-query-builder.factory';
import { workspaceQueryBuilderFactories } from './factories/factories'; import { workspaceQueryBuilderFactories } from './factories/factories';
@Module({ @Module({
imports: [], imports: [ObjectMetadataModule],
providers: [...workspaceQueryBuilderFactories, WorkspaceQueryBuilderFactory], providers: [...workspaceQueryBuilderFactories, WorkspaceQueryBuilderFactory],
exports: [WorkspaceQueryBuilderFactory], exports: [WorkspaceQueryBuilderFactory],
}) })

View File

@ -45,7 +45,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<IConnection<Record> | undefined> { ): Promise<IConnection<Record> | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = this.workspaceQueryBuilderFactory.findMany(args, options); const query = await this.workspaceQueryBuilderFactory.findMany(
args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<IConnection<Record>>(result, targetTableName, ''); return this.parseResult<IConnection<Record>>(result, targetTableName, '');
@ -62,7 +65,10 @@ export class WorkspaceQueryRunnerService {
throw new BadRequestException('Missing filter argument'); throw new BadRequestException('Missing filter argument');
} }
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = this.workspaceQueryBuilderFactory.findOne(args, options); const query = await this.workspaceQueryBuilderFactory.findOne(
args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
const parsedResult = this.parseResult<IConnection<Record>>( const parsedResult = this.parseResult<IConnection<Record>>(
result, result,
@ -78,7 +84,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<Record[] | undefined> { ): Promise<Record[] | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = this.workspaceQueryBuilderFactory.createMany(args, options); const query = await this.workspaceQueryBuilderFactory.createMany(
args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<PGGraphQLMutation<Record>>( return this.parseResult<PGGraphQLMutation<Record>>(
@ -102,9 +111,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<Record | undefined> { ): Promise<Record | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = await this.workspaceQueryBuilderFactory.updateOne(
const query = this.workspaceQueryBuilderFactory.updateOne(args, options); args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<PGGraphQLMutation<Record>>( return this.parseResult<PGGraphQLMutation<Record>>(
@ -119,7 +129,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<Record | undefined> { ): Promise<Record | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = this.workspaceQueryBuilderFactory.deleteOne(args, options); const query = await this.workspaceQueryBuilderFactory.deleteOne(
args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<PGGraphQLMutation<Record>>( return this.parseResult<PGGraphQLMutation<Record>>(
@ -134,9 +147,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<Record[] | undefined> { ): Promise<Record[] | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = await this.workspaceQueryBuilderFactory.updateMany(
const query = this.workspaceQueryBuilderFactory.updateMany(args, options); args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<PGGraphQLMutation<Record>>( return this.parseResult<PGGraphQLMutation<Record>>(
@ -154,9 +168,10 @@ export class WorkspaceQueryRunnerService {
options: WorkspaceQueryRunnerOptions, options: WorkspaceQueryRunnerOptions,
): Promise<Record[] | undefined> { ): Promise<Record[] | undefined> {
const { workspaceId, targetTableName } = options; const { workspaceId, targetTableName } = options;
const query = await this.workspaceQueryBuilderFactory.deleteMany(
const query = this.workspaceQueryBuilderFactory.deleteMany(args, options); args,
options,
);
const result = await this.execute(query, workspaceId); const result = await this.execute(query, workspaceId);
return this.parseResult<PGGraphQLMutation<Record>>( return this.parseResult<PGGraphQLMutation<Record>>(

View File

@ -18,7 +18,7 @@ export class ArgsFactory {
) {} ) {}
public create( public create(
{ args, objectMetadata }: ArgsMetadata, { args, objectMetadataId }: ArgsMetadata,
options: WorkspaceBuildSchemaOptions, options: WorkspaceBuildSchemaOptions,
): GraphQLFieldConfigArgumentMap { ): GraphQLFieldConfigArgumentMap {
const fieldConfigMap: GraphQLFieldConfigArgumentMap = {}; const fieldConfigMap: GraphQLFieldConfigArgumentMap = {};
@ -65,21 +65,21 @@ export class ArgsFactory {
// Argument is an input type // Argument is an input type
if (arg.kind) { if (arg.kind) {
const inputType = this.typeDefinitionsStorage.getInputTypeByKey( const inputType = this.typeDefinitionsStorage.getInputTypeByKey(
objectMetadata.id, objectMetadataId,
arg.kind, arg.kind,
); );
if (!inputType) { if (!inputType) {
this.logger.error( this.logger.error(
`Could not find a GraphQL input type for ${objectMetadata.id}`, `Could not find a GraphQL input type for ${objectMetadataId}`,
{ {
objectMetadata, objectMetadataId,
options, options,
}, },
); );
throw new Error( throw new Error(
`Could not find a GraphQL input type for ${objectMetadata.id}`, `Could not find a GraphQL input type for ${objectMetadataId}`,
); );
} }

View File

@ -155,7 +155,7 @@ export class ExtendObjectTypeDefinitionFactory {
argsType = this.argsFactory.create( argsType = this.argsFactory.create(
{ {
args, args,
objectMetadata: relationMetadata.toObjectMetadata, objectMetadataId: relationMetadata.toObjectMetadataId,
}, },
options, options,
); );

View File

@ -81,7 +81,7 @@ export class RootTypeFactory {
const argsType = this.argsFactory.create( const argsType = this.argsFactory.create(
{ {
args, args,
objectMetadata, objectMetadataId: objectMetadata.id,
}, },
options, options,
); );

View File

@ -1,5 +1,3 @@
import { ObjectMetadataInterface } from 'src/metadata/field-metadata/interfaces/object-metadata.interface';
import { InputTypeDefinitionKind } from 'src/workspace/workspace-schema-builder/factories/input-type-definition.factory'; import { InputTypeDefinitionKind } from 'src/workspace/workspace-schema-builder/factories/input-type-definition.factory';
import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity'; import { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
@ -15,5 +13,5 @@ export interface ArgsMetadata {
args: { args: {
[key: string]: ArgMetadata; [key: string]: ArgMetadata;
}; };
objectMetadata: ObjectMetadataInterface; objectMetadataId: string;
} }

View File

@ -51,9 +51,7 @@ export class WorkspaceFactory {
// If object metadata is not cached, get it from the database // If object metadata is not cached, get it from the database
if (!objectMetadataCollection) { if (!objectMetadataCollection) {
objectMetadataCollection = objectMetadataCollection =
await this.objectMetadataService.getObjectMetadataFromWorkspaceId( await this.objectMetadataService.findManyWithinWorkspace(workspaceId);
workspaceId,
);
await this.workspaceSchemaStorageService.setObjectMetadata( await this.workspaceSchemaStorageService.setObjectMetadata(
workspaceId, workspaceId,