Skip to content

Commit

Permalink
fix(core): quote column name for the returning statement when using `…
Browse files Browse the repository at this point in the history
…convertToJSValueSQL`

Closes #5563
  • Loading branch information
B4nan committed May 14, 2024
1 parent 534f088 commit 4783945
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/knex/src/query/QueryBuilderHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,8 @@ export class QueryBuilderHelper {
if (returningProps.length > 0) {
qb.returning(returningProps.flatMap(prop => {
if (prop.hasConvertToJSValueSQL) {
const sql = prop.customType!.convertToJSValueSQL!(prop.fieldNames[0], this.platform) + ' as ' + this.platform.quoteIdentifier(prop.fieldNames[0]);
const aliased = this.platform.quoteIdentifier(prop.fieldNames[0]);
const sql = prop.customType!.convertToJSValueSQL!(aliased, this.platform) + ' as ' + this.platform.quoteIdentifier(prop.fieldNames[0]);
return [this.knex.raw(sql) as any];
}
return prop.fieldNames;
Expand Down
58 changes: 58 additions & 0 deletions tests/features/custom-types/GH5563.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { Entity, MikroORM, PrimaryKey, Property, Type, BaseEntity } from '@mikro-orm/postgresql';

class IntervalType extends Type<number, number | null | undefined> {

getColumnType() {
return `interval`;
}

override get runtimeType(): string {
return 'number';
}

compareAsType(): string {
return 'number';
}

convertToJSValueSQL(key: string) {
return `(extract (epoch from ${key}::interval) * 1000)::int`;
}

convertToDatabaseValueSQL(key: string) {
return `(${key} || 'milliseconds')::interval`;
}

}

@Entity()
export class A extends BaseEntity {

@PrimaryKey()
id!: number;

@Property({ type: IntervalType })
end!: number;

}


let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [A],
dbName: '5563',
});
await orm.schema.refreshDatabase();
});

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

test('update customType', async () => {
const bk = orm.em.create(A, { end: 10 });
await orm.em.flush();
orm.em.clear();
const entityA = await orm.em.findOneOrFail(A, { id: bk.id });
entityA.assign({ end: 500 });
await orm.em.flush();
});

0 comments on commit 4783945

Please sign in to comment.