Skip to content

Commit

Permalink
fix(core): fix mapping of params with custom types
Browse files Browse the repository at this point in the history
Closes #940
  • Loading branch information
B4nan committed Oct 22, 2020
1 parent 1eb6374 commit e5049b1
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/metadata/MetadataDiscovery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ export class MetadataDiscovery {

private initManyToManyFields(meta: EntityMetadata, prop: EntityProperty): void {
const meta2 = this.metadata.get(prop.type);
prop.referencedPKs = meta2.primaryKeys;
Utils.defaultValue(prop, 'fixedOrder', !!prop.fixedOrderColumn);

if (!prop.pivotTable && prop.owner && this.platform.usesPivotTable()) {
Expand Down Expand Up @@ -321,6 +322,7 @@ export class MetadataDiscovery {

private initManyToOneFields(prop: EntityProperty): void {
const meta2 = this.metadata.get(prop.type);
prop.referencedPKs = meta2.primaryKeys;
const fieldNames = Utils.flatten(meta2.primaryKeys.map(primaryKey => meta2.properties[primaryKey].fieldNames));
Utils.defaultValue(prop, 'referencedTableName', meta2.collection);

Expand All @@ -335,6 +337,7 @@ export class MetadataDiscovery {

private initOneToManyFields(prop: EntityProperty): void {
const meta2 = this.metadata.get(prop.type);
prop.referencedPKs = meta2.primaryKeys;

if (!prop.joinColumns) {
prop.joinColumns = [this.namingStrategy.joinColumnName(prop.name)];
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/typings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ export interface EntityProperty<T extends AnyEntity<T> = any> {
inverseJoinColumns: string[];
referencedColumnNames: string[];
referencedTableName: string;
referencedPKs: string[];
serializer?: (value: any) => any;
serializedName?: string;
comment?: string;
Expand Down
7 changes: 6 additions & 1 deletion packages/core/src/utils/QueryHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,12 @@ export class QueryHelper {
static processCustomType<T>(prop: EntityProperty<T>, cond: FilterQuery<T>, platform: Platform, key?: string, fromQuery?: boolean): FilterQuery<T> {
if (Utils.isPlainObject(cond)) {
return Object.keys(cond).reduce((o, k) => {
o[k] = QueryHelper.processCustomType(prop, cond[k], platform, k, fromQuery);
if (Utils.isOperator(k, true) || prop.referencedPKs.includes(k)) {
o[k] = QueryHelper.processCustomType(prop, cond[k], platform, k, fromQuery);
} else {
o[k] = cond[k];
}

return o;
}, {});
}
Expand Down
2 changes: 1 addition & 1 deletion packages/knex/src/schema/SchemaGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export class SchemaGenerator {
.forEach(prop => this.createForeignKey(table, meta, prop, undefined, createdColumns));
}

private createForeignKey(table: TableBuilder, meta: EntityMetadata, prop: EntityProperty, diff?: IsSame, createdColumns: string[] = []): void {
private createForeignKey(table: TableBuilder, meta: EntityMetadata, prop: EntityProperty, diff?: IsSame, createdColumns: string[]): void {
if (this.helper.supportsSchemaConstraints()) {
this.createForeignKeyReference(table, prop);

Expand Down
70 changes: 70 additions & 0 deletions tests/issues/GH940.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { BigIntType, Collection, Entity, ManyToOne, MikroORM, OneToMany, PrimaryKey, Property } from '@mikro-orm/core';
import { SchemaGenerator, SqliteDriver } from '@mikro-orm/sqlite';

@Entity()
class User {

@PrimaryKey({ type: BigIntType })
id!: string;

@OneToMany('UserOrganization', 'user')
organizations = new Collection<UserOrganization>(this);

}

@Entity()
class UserOrganization {

@PrimaryKey({ type: BigIntType })
id!: string;

@ManyToOne(() => User)
user: User;

@Property()
isAdmin: boolean;

constructor(user: User, isAdmin: boolean) {
this.user = user;
this.isAdmin = isAdmin;
}

}

describe('GH issue 940', () => {

let orm: MikroORM<SqliteDriver>;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [User, UserOrganization],
dbName: `:memory:`,
type: 'sqlite',
port: 3307,
});
await new SchemaGenerator(orm.em).createSchema();
});

afterAll(async () => await orm.close(true));

test('A boolean in the nested where conditions is kept even if the primary key is BigIntType', async () => {

const user1 = new User();
const user2 = new User();
const user1org = new UserOrganization(user1, true);
const user2org = new UserOrganization(user2, false);

await orm.em.persistAndFlush([user1org, user2org]);

const users = await orm.em.find(User, { organizations: { isAdmin: true } });
expect(users).toMatchObject([
{
id: user1.id,
organizations: {
0: { id: user1org.id, isAdmin: true },
},
},
]);
});

});

0 comments on commit e5049b1

Please sign in to comment.