Skip to content

Commit 887df20

Browse files
committed
fix(query-typeorm): Fixed getManyToManyOwnerMeta
It was not add the select correctly and mapping the raw relations back
1 parent 3897f30 commit 887df20

File tree

9 files changed

+66
-47
lines changed

9 files changed

+66
-47
lines changed

examples/jest.config.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
"coverage/examples"
2020
],
2121
"options": {
22-
"jestConfig": "examples/jest.config.ts"
22+
"jestConfig": "jest.e2e.ts",
23+
"runInBand": true
2324
}
2425
}
2526
},

jest.e2e.config.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

jest.e2e.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
module.exports = {
2+
displayName: 'examples',
3+
preset: './jest.preset.ts',
4+
globals: {
5+
'ts-jest': {
6+
tsconfig: './examples/tsconfig.spec.json'
7+
}
8+
},
9+
testEnvironment: 'node',
10+
transform: {
11+
'^.+\\.[tj]s$': 'ts-jest'
12+
},
13+
moduleFileExtensions: ['ts', 'js', 'html'],
14+
testMatch: ['**/e2e/**/*.spec.ts'],
15+
16+
setupFilesAfterEnv: ['jest-extended'],
17+
snapshotSerializers: ['jest-snapshot-serializer-raw/always'],
18+
19+
coverageDirectory: '../coverage/examples'
20+
};

jest.preset.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ module.exports = {
55

66
collectCoverage: true,
77
coverageReporters: ['html', 'clover'],
8-
collectCoverageFrom: [
9-
'!**/node_modules/**',
10-
'src/**',
11-
'!**/__snapshots__/**',
12-
'!**/*.test.js',
13-
'!**/index.ts',
14-
'!**/*.json'
15-
],
16-
8+
collectCoverageFrom: ['packages/**/*.ts', '!**/__tests__/**', '!**/dist/**', '!**/node_modules/**'],
9+
moduleNameMapper: {
10+
'@ptc-org/nestjs-query-core': '<rootDir>/packages/core/src',
11+
'@ptc-org/nestjs-query-graphql': '<rootDir>/packages/query-graphql/src',
12+
'@ptc-org/nestjs-query-typeorm': '<rootDir>/packages/query-typeorm/src',
13+
'@ptc-org/nestjs-query-sequelize': '<rootDir>/packages/query-sequelize/src',
14+
'@ptc-org/nestjs-query-typegoose': '<rootDir>/packages/query-typegoose/src',
15+
'@ptc-org/nestjs-query-mongoose': '<rootDir>/packages/query-mongoose/src',
16+
},
1717
testEnvironment: 'node',
1818
setupFilesAfterEnv: ['jest-extended'],
1919
snapshotSerializers: ['jest-snapshot-serializer-raw/always']

packages/core/src/helpers/filter.helpers.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const isBooleanComparisonOperators = (op: unknown): op is BooleanComparis
2323
op === 'eq' || op === 'neq' || op === 'is' || op === 'isNot';
2424

2525
export const isComparison = <DTO, K extends keyof DTO>(
26-
maybeComparison?: FilterFieldComparison<DTO[K]> | Filter<DTO[K]>,
26+
maybeComparison?: FilterFieldComparison<DTO[K]> | Filter<DTO[K]>
2727
): maybeComparison is FilterFieldComparison<DTO[K]> => {
2828
if (!maybeComparison) {
2929
return false;
@@ -35,19 +35,19 @@ export const isComparison = <DTO, K extends keyof DTO>(
3535
isInComparisonOperators(op) ||
3636
isBetweenComparisonOperators(op) ||
3737
isRangeComparisonOperators(op) ||
38-
isBooleanComparisonOperators(op),
38+
isBooleanComparisonOperators(op)
3939
);
4040
};
4141

4242
// TODO: test
4343
export const getFilterFieldComparison = <DTO, K extends keyof FilterComparisons<DTO>>(
4444
obj: FilterComparisons<DTO>,
45-
field: K,
45+
field: K
4646
): FilterFieldComparison<DTO[K]> & Filter<DTO[K]> => obj[field] as FilterFieldComparison<DTO[K]> & Filter<DTO[K]>;
4747

4848
export const transformFilter = <From, To>(
4949
filter: Filter<From> | undefined,
50-
fieldMap: QueryFieldMap<From, To>,
50+
fieldMap: QueryFieldMap<From, To>
5151
): Filter<To> | undefined => {
5252
if (!filter) {
5353
return undefined;
@@ -79,33 +79,40 @@ export const getFilterFields = <DTO>(filter: Filter<DTO>): string[] => {
7979
const fieldSet: Set<string> = Object.keys(filter).reduce((fields: Set<string>, filterField: string): Set<string> => {
8080
if (filterField === 'and' || filterField === 'or') {
8181
const andOrFilters = filter[filterField];
82+
8283
if (andOrFilters !== undefined) {
8384
return andOrFilters.reduce(
8485
(andOrFields, andOrFilter) => new Set<string>([...andOrFields, ...getFilterFields(andOrFilter)]),
85-
fields,
86+
fields
8687
);
8788
}
89+
8890
} else {
8991
fields.add(filterField);
9092
}
93+
9194
return fields;
9295
}, new Set<string>());
96+
9397
return [...fieldSet];
9498
};
9599

96100
export const getFilterComparisons = <DTO, K extends keyof FilterComparisons<DTO>>(
97101
filter: Filter<DTO>,
98-
key: K,
102+
key: K
99103
): FilterFieldComparison<DTO[K]>[] => {
100104
const results: FilterFieldComparison<DTO[K]>[] = [];
105+
101106
if (filter.and || filter.or) {
102107
const filters = [...(filter.and ?? []), ...(filter.or ?? [])];
103108
filters.forEach((f) => getFilterComparisons(f, key).forEach((comparison) => results.push(comparison)));
104109
}
110+
105111
const comparison = getFilterFieldComparison(filter as FilterComparisons<DTO>, key);
106112
if (isComparison(comparison)) {
107113
results.push(comparison);
108114
}
115+
109116
return [...results];
110117
};
111118

packages/query-typeorm/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@ptc-org/nestjs-query-typeorm",
3-
"version": "1.0.0-alpha.1",
3+
"version": "1.0.0-alpha.2",
44
"description": "Typeorm adapter for @ptc-org/nestjs-query-core",
55
"author": "doug-martin <[email protected]>",
66
"homepage": "https://github.com/tripss/nestjs-query#readme",

packages/query-typeorm/src/query/relation-query.builder.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -383,10 +383,9 @@ export class RelationQueryBuilder<Entity, Relation> {
383383
// First filter the raw relations with the PK of the entity, then filter the relations
384384
// with the PK of the raw relation
385385
return lodashFilter(rawRelations, rawFilter).reduce((entityRelations, rawRelation) => {
386-
const filter = relation.inverseJoinColumns.reduce((columns, column) => ({
386+
const filter = this.getRelationPrimaryKeysPropertyNameAndColumnsName().reduce((columns, column) => ({
387387
...columns,
388-
389-
[column.referencedColumn.propertyName]: rawRelation[`${joinAlias}_${column.propertyName}`]
388+
[column.propertyName]: rawRelation[column.columnName]
390389
}), {} as Partial<Entity>);
391390

392391
return entityRelations.concat(lodashFilter(relations, filter) as any);
@@ -402,7 +401,7 @@ export class RelationQueryBuilder<Entity, Relation> {
402401
params[paramName] = entities.map((entity) => column.referencedColumn!.getEntityValue(entity));
403402

404403
// We also want to select the field, so we can map them back in the mapper
405-
andSelect.push(`${joinAlias}.${column.propertyName}`);
404+
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
406405
return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
407406
}).join(' AND ');
408407

@@ -489,7 +488,7 @@ export class RelationQueryBuilder<Entity, Relation> {
489488
params[paramName] = entities.map((entity) => column.referencedColumn!.getEntityValue(entity));
490489

491490
// We also want to select the field, so we can map them back in the mapper
492-
andSelect.push(`${joinAlias}.${column.propertyName}`);
491+
andSelect.push(`${joinAlias}.${column.propertyName} AS ${joinAlias}_${column.propertyName}`);
493492
return `${joinAlias}.${column.propertyName} IN (:...${paramName})`;
494493
}).join(' AND ');
495494

tsconfig.spec.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"outDir": "./dist/out-tsc",
5+
"module": "commonjs",
6+
"types": [
7+
"jest",
8+
"node"
9+
]
10+
},
11+
"include": [
12+
"jest.config.ts",
13+
"**/*.test.ts",
14+
"**/*.spec.ts",
15+
"**/*.d.ts"
16+
]
17+
}

0 commit comments

Comments
 (0)