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:
@ -7,7 +7,7 @@ import {
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
|
||||
import { v4 as uuidV4 } from 'uuid';
|
||||
import { Repository } from 'typeorm';
|
||||
import { FindOneOptions, Repository } from 'typeorm';
|
||||
import { TypeOrmQueryService } from '@ptc-org/nestjs-query-typeorm';
|
||||
|
||||
import { WorkspaceMigrationRunnerService } from 'src/workspace/workspace-migration-runner/workspace-migration-runner.service';
|
||||
@ -47,8 +47,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
): Promise<FieldMetadataEntity> {
|
||||
const objectMetadata =
|
||||
await this.objectMetadataService.findOneWithinWorkspace(
|
||||
record.objectMetadataId,
|
||||
record.workspaceId,
|
||||
{
|
||||
where: {
|
||||
id: record.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!objectMetadata) {
|
||||
@ -156,8 +160,12 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
|
||||
const objectMetadata =
|
||||
await this.objectMetadataService.findOneWithinWorkspace(
|
||||
existingFieldMetadata?.objectMetadataId,
|
||||
record.workspaceId,
|
||||
{
|
||||
where: {
|
||||
id: existingFieldMetadata?.objectMetadataId,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
if (!objectMetadata) {
|
||||
@ -200,11 +208,15 @@ export class FieldMetadataService extends TypeOrmQueryService<FieldMetadataEntit
|
||||
}
|
||||
|
||||
public async findOneWithinWorkspace(
|
||||
fieldMetadataId: string,
|
||||
workspaceId: string,
|
||||
options: FindOneOptions<FieldMetadataEntity>,
|
||||
) {
|
||||
return this.fieldMetadataRepository.findOne({
|
||||
where: { id: fieldMetadataId, workspaceId },
|
||||
...options,
|
||||
where: {
|
||||
...options.where,
|
||||
workspaceId,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -27,10 +27,11 @@ export class BeforeDeleteOneField implements BeforeDeleteOneHook<any> {
|
||||
}
|
||||
|
||||
const fieldMetadata =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
instance.id.toString(),
|
||||
workspaceId,
|
||||
);
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!fieldMetadata) {
|
||||
throw new BadRequestException('Field does not exist');
|
||||
|
||||
@ -31,10 +31,11 @@ export class BeforeUpdateOneField<T extends UpdateFieldInput>
|
||||
}
|
||||
|
||||
const fieldMetadata =
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(
|
||||
instance.id.toString(),
|
||||
workspaceId,
|
||||
);
|
||||
await this.fieldMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!fieldMetadata) {
|
||||
throw new BadRequestException('Field does not exist');
|
||||
|
||||
@ -16,6 +16,7 @@ export interface FieldMetadataInterface<
|
||||
defaultValue?: FieldMetadataDefaultValue<T>;
|
||||
options?: FieldMetadataOptions<T>;
|
||||
objectMetadataId: string;
|
||||
workspaceId?: string;
|
||||
description?: string;
|
||||
isNullable?: boolean;
|
||||
fromRelationMetadata?: RelationMetadataEntity;
|
||||
|
||||
@ -3,6 +3,7 @@ import { BadRequestException } from '@nestjs/common';
|
||||
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 { 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.
|
||||
@ -16,7 +17,9 @@ export function generateTargetColumnMap(
|
||||
isCustomField: boolean,
|
||||
fieldName: string,
|
||||
): FieldMetadataTargetColumnMap {
|
||||
const columnName = isCustomField ? `_${fieldName}` : fieldName;
|
||||
const columnName = isCustomField
|
||||
? createCustomColumnName(fieldName)
|
||||
: fieldName;
|
||||
|
||||
switch (type) {
|
||||
case FieldMetadataType.UUID:
|
||||
|
||||
@ -26,10 +26,11 @@ export class BeforeDeleteOneObject implements BeforeDeleteOneHook<any> {
|
||||
}
|
||||
|
||||
const objectMetadata =
|
||||
await this.objectMetadataService.findOneWithinWorkspace(
|
||||
instance.id.toString(),
|
||||
workspaceId,
|
||||
);
|
||||
await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!objectMetadata) {
|
||||
throw new BadRequestException('Object does not exist');
|
||||
|
||||
@ -39,10 +39,11 @@ export class BeforeUpdateOneObject<T extends UpdateObjectInput>
|
||||
}
|
||||
|
||||
const objectMetadata =
|
||||
await this.objectMetadataService.findOneWithinWorkspace(
|
||||
instance.id.toString(),
|
||||
workspaceId,
|
||||
);
|
||||
await this.objectMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!objectMetadata) {
|
||||
throw new BadRequestException('Object does not exist');
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
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 { WorkspaceMigrationService } from 'src/metadata/workspace-migration/workspace-migration.service';
|
||||
@ -21,6 +21,7 @@ import {
|
||||
RelationMetadataEntity,
|
||||
RelationMetadataType,
|
||||
} 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';
|
||||
|
||||
@ -63,7 +64,7 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
const createdObjectMetadata = await super.createOne({
|
||||
...record,
|
||||
dataSourceId: lastDataSourceMetadata.id,
|
||||
targetTableName: `_${record.nameSingular}`,
|
||||
targetTableName: createCustomColumnName(record.nameSingular),
|
||||
isActive: true,
|
||||
isCustom: true,
|
||||
isSystem: false,
|
||||
@ -298,39 +299,39 @@ export class ObjectMetadataService extends TypeOrmQueryService<ObjectMetadataEnt
|
||||
return createdObjectMetadata;
|
||||
}
|
||||
|
||||
public async getObjectMetadataFromWorkspaceId(workspaceId: string) {
|
||||
return this.objectMetadataRepository.find({
|
||||
where: { workspaceId },
|
||||
public async findOneWithinWorkspace(
|
||||
workspaceId: string,
|
||||
options: FindOneOptions<ObjectMetadataEntity>,
|
||||
): Promise<ObjectMetadataEntity | null> {
|
||||
return this.objectMetadataRepository.findOne({
|
||||
...options,
|
||||
where: {
|
||||
...options.where,
|
||||
workspaceId,
|
||||
},
|
||||
relations: [
|
||||
'fields',
|
||||
'fields.fromRelationMetadata',
|
||||
'fields.fromRelationMetadata.fromObjectMetadata',
|
||||
'fields.fromRelationMetadata.toObjectMetadata',
|
||||
'fields.fromRelationMetadata.toObjectMetadata.fields',
|
||||
'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(
|
||||
objectMetadataIds: string[],
|
||||
workspaceId: string,
|
||||
options?: FindManyOptions<ObjectMetadataEntity>,
|
||||
) {
|
||||
return this.objectMetadataRepository.findBy({
|
||||
id: In(objectMetadataIds),
|
||||
workspaceId: Equal(workspaceId),
|
||||
return this.objectMetadataRepository.find({
|
||||
...options,
|
||||
where: {
|
||||
...options?.where,
|
||||
workspaceId,
|
||||
},
|
||||
relations: [
|
||||
'fields',
|
||||
'fields.fromRelationMetadata',
|
||||
'fields.toRelationMetadata',
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@ -26,10 +26,11 @@ export class BeforeDeleteOneRelation implements BeforeDeleteOneHook<any> {
|
||||
}
|
||||
|
||||
const relationMetadata =
|
||||
await this.relationMetadataService.findOneWithinWorkspace(
|
||||
instance.id.toString(),
|
||||
workspaceId,
|
||||
);
|
||||
await this.relationMetadataService.findOneWithinWorkspace(workspaceId, {
|
||||
where: {
|
||||
id: instance.id.toString(),
|
||||
},
|
||||
});
|
||||
|
||||
if (!relationMetadata) {
|
||||
throw new BadRequestException('Relation does not exist');
|
||||
|
||||
@ -6,7 +6,7 @@ import {
|
||||
import { InjectRepository } from '@nestjs/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 { 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 { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.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 {
|
||||
RelationMetadataEntity,
|
||||
@ -85,14 +87,18 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
|
||||
const objectMetadataEntries =
|
||||
await this.objectMetadataService.findManyWithinWorkspace(
|
||||
[record.fromObjectMetadataId, record.toObjectMetadataId],
|
||||
record.workspaceId,
|
||||
{
|
||||
where: {
|
||||
id: In([record.fromObjectMetadataId, record.toObjectMetadataId]),
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const objectMetadataMap = objectMetadataEntries.reduce((acc, curr) => {
|
||||
acc[curr.id] = curr;
|
||||
return acc;
|
||||
}, {});
|
||||
}, {} as { [key: string]: ObjectMetadataEntity });
|
||||
|
||||
if (
|
||||
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([
|
||||
// FROM
|
||||
@ -126,7 +136,11 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
description: record.toDescription,
|
||||
icon: record.toIcon,
|
||||
isCustom: true,
|
||||
targetColumnMap: {},
|
||||
targetColumnMap: isToCustom
|
||||
? {
|
||||
value: createCustomColumnName(record.toName),
|
||||
}
|
||||
: {},
|
||||
isActive: true,
|
||||
type: FieldMetadataType.RELATION,
|
||||
objectMetadataId: record.toObjectMetadataId,
|
||||
@ -134,12 +148,14 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
},
|
||||
// FOREIGN KEY
|
||||
{
|
||||
name: foreignKeyColumnName,
|
||||
name: baseColumnName,
|
||||
label: `${record.toLabel} Foreign Key`,
|
||||
description: undefined,
|
||||
description: `${record.toDescription} Foreign Key`,
|
||||
icon: undefined,
|
||||
isCustom: true,
|
||||
targetColumnMap: {},
|
||||
targetColumnMap: {
|
||||
value: foreignKeyColumnName,
|
||||
},
|
||||
isActive: true,
|
||||
// Should not be visible on the front side
|
||||
isSystem: true,
|
||||
@ -203,11 +219,15 @@ export class RelationMetadataService extends TypeOrmQueryService<RelationMetadat
|
||||
}
|
||||
|
||||
public async findOneWithinWorkspace(
|
||||
relationMetadataId: string,
|
||||
workspaceId: string,
|
||||
options: FindOneOptions<RelationMetadataEntity>,
|
||||
) {
|
||||
return this.relationMetadataRepository.findOne({
|
||||
where: { id: relationMetadataId, workspaceId },
|
||||
...options,
|
||||
where: {
|
||||
...options.where,
|
||||
workspaceId,
|
||||
},
|
||||
relations: ['fromFieldMetadata', 'toFieldMetadata'],
|
||||
});
|
||||
}
|
||||
|
||||
@ -0,0 +1,3 @@
|
||||
export const createCustomColumnName = (name: string) => {
|
||||
return `_${name}`;
|
||||
};
|
||||
@ -20,11 +20,11 @@ export class CreateManyQueryFactory {
|
||||
private readonly argsAliasFactory: ArgsAliasFactory,
|
||||
) {}
|
||||
|
||||
create<Record extends IRecord = IRecord>(
|
||||
async create<Record extends IRecord = IRecord>(
|
||||
args: CreateManyResolverArgs<Record>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -11,8 +11,11 @@ import { FieldsStringFactory } from './fields-string.factory';
|
||||
export class DeleteManyQueryFactory {
|
||||
constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}
|
||||
|
||||
create(args: DeleteManyResolverArgs, options: WorkspaceQueryBuilderOptions) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
async create(
|
||||
args: DeleteManyResolverArgs,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -11,8 +11,11 @@ export class DeleteOneQueryFactory {
|
||||
|
||||
constructor(private readonly fieldsStringFactory: FieldsStringFactory) {}
|
||||
|
||||
create(args: DeleteOneResolverArgs, options: WorkspaceQueryBuilderOptions) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
async create(
|
||||
args: DeleteOneResolverArgs,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -23,7 +23,7 @@ export class FieldsStringFactory {
|
||||
create(
|
||||
info: GraphQLResolveInfo,
|
||||
fieldMetadataCollection: FieldMetadataInterface[],
|
||||
) {
|
||||
): Promise<string> {
|
||||
const selectedFields: Record<string, any> = graphqlFields(info);
|
||||
|
||||
return this.createFieldsStringRecursive(
|
||||
@ -33,12 +33,12 @@ export class FieldsStringFactory {
|
||||
);
|
||||
}
|
||||
|
||||
createFieldsStringRecursive(
|
||||
async createFieldsStringRecursive(
|
||||
info: GraphQLResolveInfo,
|
||||
selectedFields: Record<string, any>,
|
||||
fieldMetadataCollection: FieldMetadataInterface[],
|
||||
accumulator = '',
|
||||
): string {
|
||||
): Promise<string> {
|
||||
const fieldMetadataMap = new Map(
|
||||
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 (isRelationFieldMetadataType(fieldMetadata.type)) {
|
||||
const alias = this.relationFieldAliasFactory.create(
|
||||
const alias = await this.relationFieldAliasFactory.create(
|
||||
fieldKey,
|
||||
fieldValue,
|
||||
fieldMetadata,
|
||||
@ -80,7 +80,7 @@ export class FieldsStringFactory {
|
||||
!isEmpty(fieldValue)
|
||||
) {
|
||||
accumulator += `${fieldKey} {\n`;
|
||||
accumulator = this.createFieldsStringRecursive(
|
||||
accumulator = await this.createFieldsStringRecursive(
|
||||
info,
|
||||
fieldValue,
|
||||
fieldMetadataCollection,
|
||||
|
||||
@ -19,14 +19,14 @@ export class FindManyQueryFactory {
|
||||
private readonly argsStringFactory: ArgsStringFactory,
|
||||
) {}
|
||||
|
||||
create<
|
||||
async create<
|
||||
Filter extends RecordFilter = RecordFilter,
|
||||
OrderBy extends RecordOrderBy = RecordOrderBy,
|
||||
>(
|
||||
args: FindManyResolverArgs<Filter, OrderBy>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -16,11 +16,11 @@ export class FindOneQueryFactory {
|
||||
private readonly argsStringFactory: ArgsStringFactory,
|
||||
) {}
|
||||
|
||||
create<Filter extends RecordFilter = RecordFilter>(
|
||||
async create<Filter extends RecordFilter = RecordFilter>(
|
||||
args: FindOneResolverArgs<Filter>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -11,6 +11,7 @@ import {
|
||||
RelationDirection,
|
||||
} from 'src/workspace/utils/deduce-relation-direction.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 { ArgsStringFactory } from './args-string.factory';
|
||||
@ -23,6 +24,7 @@ export class RelationFieldAliasFactory {
|
||||
@Inject(forwardRef(() => FieldsStringFactory))
|
||||
private readonly fieldsStringFactory: FieldsStringFactory,
|
||||
private readonly argsStringFactory: ArgsStringFactory,
|
||||
private readonly objectMetadataService: ObjectMetadataService,
|
||||
) {}
|
||||
|
||||
create(
|
||||
@ -30,7 +32,7 @@ export class RelationFieldAliasFactory {
|
||||
fieldValue: any,
|
||||
fieldMetadata: FieldMetadataInterface,
|
||||
info: GraphQLResolveInfo,
|
||||
) {
|
||||
): Promise<string> {
|
||||
if (!isRelationFieldMetadataType(fieldMetadata.type)) {
|
||||
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);
|
||||
}
|
||||
|
||||
private createRelationAlias(
|
||||
private async createRelationAlias(
|
||||
fieldKey: string,
|
||||
fieldValue: any,
|
||||
fieldMetadata: FieldMetadataInterface,
|
||||
info: GraphQLResolveInfo,
|
||||
) {
|
||||
): Promise<string> {
|
||||
const relationMetadata =
|
||||
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(
|
||||
fieldMetadata.objectMetadataId,
|
||||
relationMetadata,
|
||||
);
|
||||
// Retrieve the referenced object metadata based on the relation direction
|
||||
// Mandatory to handle n+n relations
|
||||
const referencedObjectMetadata =
|
||||
relationDirection == RelationDirection.TO
|
||||
? relationMetadata.fromObjectMetadata
|
||||
: relationMetadata.toObjectMetadata;
|
||||
await this.objectMetadataService.findOneWithinWorkspace(
|
||||
fieldMetadata.workspaceId,
|
||||
{
|
||||
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 (
|
||||
@ -70,21 +94,26 @@ export class RelationFieldAliasFactory {
|
||||
const args = getFieldArgumentsByKey(info, fieldKey);
|
||||
const argsString = this.argsStringFactory.create(
|
||||
args,
|
||||
relationMetadata.toObjectMetadata.fields ?? [],
|
||||
referencedObjectMetadata.fields ?? [],
|
||||
);
|
||||
const fieldsString =
|
||||
await this.fieldsStringFactory.createFieldsStringRecursive(
|
||||
info,
|
||||
fieldValue,
|
||||
referencedObjectMetadata.fields ?? [],
|
||||
);
|
||||
|
||||
return `
|
||||
${fieldKey}: ${referencedObjectMetadata.targetTableName}Collection${
|
||||
argsString ? `(${argsString})` : ''
|
||||
} {
|
||||
${this.fieldsStringFactory.createFieldsStringRecursive(
|
||||
info,
|
||||
fieldValue,
|
||||
relationMetadata.toObjectMetadata.fields ?? [],
|
||||
)}
|
||||
${fieldsString}
|
||||
}
|
||||
`;
|
||||
}
|
||||
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
|
||||
// so we need to alias it to the field key
|
||||
@ -94,15 +123,17 @@ export class RelationFieldAliasFactory {
|
||||
) {
|
||||
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
|
||||
return `
|
||||
${relationAlias} {
|
||||
${this.fieldsStringFactory.createFieldsStringRecursive(
|
||||
info,
|
||||
fieldValue,
|
||||
referencedObjectMetadata.fields ?? [],
|
||||
)}
|
||||
${fieldsString}
|
||||
}
|
||||
`;
|
||||
}
|
||||
|
||||
@ -18,14 +18,14 @@ export class UpdateManyQueryFactory {
|
||||
private readonly argsAliasFactory: ArgsAliasFactory,
|
||||
) {}
|
||||
|
||||
create<
|
||||
async create<
|
||||
Record extends IRecord = IRecord,
|
||||
Filter extends RecordFilter = RecordFilter,
|
||||
>(
|
||||
args: UpdateManyResolverArgs<Record, Filter>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -18,11 +18,11 @@ export class UpdateOneQueryFactory {
|
||||
private readonly argsAliasFactory: ArgsAliasFactory,
|
||||
) {}
|
||||
|
||||
create<Record extends IRecord = IRecord>(
|
||||
async create<Record extends IRecord = IRecord>(
|
||||
args: UpdateOneResolverArgs<Record>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
) {
|
||||
const fieldsString = this.fieldsStringFactory.create(
|
||||
const fieldsString = await this.fieldsStringFactory.create(
|
||||
options.info,
|
||||
options.fieldMetadataCollection,
|
||||
);
|
||||
|
||||
@ -44,35 +44,35 @@ export class WorkspaceQueryBuilderFactory {
|
||||
>(
|
||||
args: FindManyResolverArgs<Filter, OrderBy>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.findManyQueryFactory.create<Filter, OrderBy>(args, options);
|
||||
}
|
||||
|
||||
findOne<Filter extends RecordFilter = RecordFilter>(
|
||||
args: FindOneResolverArgs<Filter>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.findOneQueryFactory.create<Filter>(args, options);
|
||||
}
|
||||
|
||||
createMany<Record extends IRecord = IRecord>(
|
||||
args: CreateManyResolverArgs<Record>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.createManyQueryFactory.create<Record>(args, options);
|
||||
}
|
||||
|
||||
updateOne<Record extends IRecord = IRecord>(
|
||||
initialArgs: UpdateOneResolverArgs<Record>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.updateOneQueryFactory.create<Record>(initialArgs, options);
|
||||
}
|
||||
|
||||
deleteOne(
|
||||
args: DeleteOneResolverArgs,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.deleteOneQueryFactory.create(args, options);
|
||||
}
|
||||
|
||||
@ -82,14 +82,14 @@ export class WorkspaceQueryBuilderFactory {
|
||||
>(
|
||||
args: UpdateManyResolverArgs<Record, Filter>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.updateManyQueryFactory.create(args, options);
|
||||
}
|
||||
|
||||
deleteMany<Filter extends RecordFilter = RecordFilter>(
|
||||
args: DeleteManyResolverArgs<Filter>,
|
||||
options: WorkspaceQueryBuilderOptions,
|
||||
): string {
|
||||
): Promise<string> {
|
||||
return this.deleteManyQueryFactory.create(args, options);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,11 +1,13 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
|
||||
import { ObjectMetadataModule } from 'src/metadata/object-metadata/object-metadata.module';
|
||||
|
||||
import { WorkspaceQueryBuilderFactory } from './workspace-query-builder.factory';
|
||||
|
||||
import { workspaceQueryBuilderFactories } from './factories/factories';
|
||||
|
||||
@Module({
|
||||
imports: [],
|
||||
imports: [ObjectMetadataModule],
|
||||
providers: [...workspaceQueryBuilderFactories, WorkspaceQueryBuilderFactory],
|
||||
exports: [WorkspaceQueryBuilderFactory],
|
||||
})
|
||||
|
||||
@ -45,7 +45,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<IConnection<Record> | undefined> {
|
||||
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);
|
||||
|
||||
return this.parseResult<IConnection<Record>>(result, targetTableName, '');
|
||||
@ -62,7 +65,10 @@ export class WorkspaceQueryRunnerService {
|
||||
throw new BadRequestException('Missing filter argument');
|
||||
}
|
||||
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 parsedResult = this.parseResult<IConnection<Record>>(
|
||||
result,
|
||||
@ -78,7 +84,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<Record[] | undefined> {
|
||||
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);
|
||||
|
||||
return this.parseResult<PGGraphQLMutation<Record>>(
|
||||
@ -102,9 +111,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<Record | undefined> {
|
||||
const { workspaceId, targetTableName } = options;
|
||||
|
||||
const query = this.workspaceQueryBuilderFactory.updateOne(args, options);
|
||||
|
||||
const query = await this.workspaceQueryBuilderFactory.updateOne(
|
||||
args,
|
||||
options,
|
||||
);
|
||||
const result = await this.execute(query, workspaceId);
|
||||
|
||||
return this.parseResult<PGGraphQLMutation<Record>>(
|
||||
@ -119,7 +129,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<Record | undefined> {
|
||||
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);
|
||||
|
||||
return this.parseResult<PGGraphQLMutation<Record>>(
|
||||
@ -134,9 +147,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<Record[] | undefined> {
|
||||
const { workspaceId, targetTableName } = options;
|
||||
|
||||
const query = this.workspaceQueryBuilderFactory.updateMany(args, options);
|
||||
|
||||
const query = await this.workspaceQueryBuilderFactory.updateMany(
|
||||
args,
|
||||
options,
|
||||
);
|
||||
const result = await this.execute(query, workspaceId);
|
||||
|
||||
return this.parseResult<PGGraphQLMutation<Record>>(
|
||||
@ -154,9 +168,10 @@ export class WorkspaceQueryRunnerService {
|
||||
options: WorkspaceQueryRunnerOptions,
|
||||
): Promise<Record[] | undefined> {
|
||||
const { workspaceId, targetTableName } = options;
|
||||
|
||||
const query = this.workspaceQueryBuilderFactory.deleteMany(args, options);
|
||||
|
||||
const query = await this.workspaceQueryBuilderFactory.deleteMany(
|
||||
args,
|
||||
options,
|
||||
);
|
||||
const result = await this.execute(query, workspaceId);
|
||||
|
||||
return this.parseResult<PGGraphQLMutation<Record>>(
|
||||
|
||||
@ -18,7 +18,7 @@ export class ArgsFactory {
|
||||
) {}
|
||||
|
||||
public create(
|
||||
{ args, objectMetadata }: ArgsMetadata,
|
||||
{ args, objectMetadataId }: ArgsMetadata,
|
||||
options: WorkspaceBuildSchemaOptions,
|
||||
): GraphQLFieldConfigArgumentMap {
|
||||
const fieldConfigMap: GraphQLFieldConfigArgumentMap = {};
|
||||
@ -65,21 +65,21 @@ export class ArgsFactory {
|
||||
// Argument is an input type
|
||||
if (arg.kind) {
|
||||
const inputType = this.typeDefinitionsStorage.getInputTypeByKey(
|
||||
objectMetadata.id,
|
||||
objectMetadataId,
|
||||
arg.kind,
|
||||
);
|
||||
|
||||
if (!inputType) {
|
||||
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,
|
||||
},
|
||||
);
|
||||
|
||||
throw new Error(
|
||||
`Could not find a GraphQL input type for ${objectMetadata.id}`,
|
||||
`Could not find a GraphQL input type for ${objectMetadataId}`,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@ -155,7 +155,7 @@ export class ExtendObjectTypeDefinitionFactory {
|
||||
argsType = this.argsFactory.create(
|
||||
{
|
||||
args,
|
||||
objectMetadata: relationMetadata.toObjectMetadata,
|
||||
objectMetadataId: relationMetadata.toObjectMetadataId,
|
||||
},
|
||||
options,
|
||||
);
|
||||
|
||||
@ -81,7 +81,7 @@ export class RootTypeFactory {
|
||||
const argsType = this.argsFactory.create(
|
||||
{
|
||||
args,
|
||||
objectMetadata,
|
||||
objectMetadataId: objectMetadata.id,
|
||||
},
|
||||
options,
|
||||
);
|
||||
|
||||
@ -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 { FieldMetadataType } from 'src/metadata/field-metadata/field-metadata.entity';
|
||||
|
||||
@ -15,5 +13,5 @@ export interface ArgsMetadata {
|
||||
args: {
|
||||
[key: string]: ArgMetadata;
|
||||
};
|
||||
objectMetadata: ObjectMetadataInterface;
|
||||
objectMetadataId: string;
|
||||
}
|
||||
|
||||
@ -51,9 +51,7 @@ export class WorkspaceFactory {
|
||||
// If object metadata is not cached, get it from the database
|
||||
if (!objectMetadataCollection) {
|
||||
objectMetadataCollection =
|
||||
await this.objectMetadataService.getObjectMetadataFromWorkspaceId(
|
||||
workspaceId,
|
||||
);
|
||||
await this.objectMetadataService.findManyWithinWorkspace(workspaceId);
|
||||
|
||||
await this.workspaceSchemaStorageService.setObjectMetadata(
|
||||
workspaceId,
|
||||
|
||||
Reference in New Issue
Block a user