Find operators get passed as objects to transformers
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql
/ mariadb
[ ] oracle
[ ] postgres
[x] sqlite
[ ] sqljs
[ ] react-native
TypeORM version:
[x] latest
[ ] @next
[ ] 0.x.x
(or put your version here)
Steps to reproduce or a small repository showing the problem:
import 'reflect-metadata';
import { createConnection, Entity, Column, BaseEntity, PrimaryGeneratedColumn, Between } from 'typeorm';
@Entity()
class Thing extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({
transformer: {
from(value) {
console.log('from', value);
return value;
},
to(value) {
console.log('to', value);
return value;
}
}
})
field: number;
}
async function run() {
await createConnection({
type: 'sqlite',
database: './db.sqlite',
entities: [Thing],
synchronize: true,
dropSchema: true
});
for (let i = 0; i < 5; i++) {
const thing = new Thing();
thing.field = i;
await thing.save();
}
const result = await Thing.find({ where: { field: Between(2, 3) } });
console.log(result);
}
run();
After running the above code you’ll see one piece of output from the transformer that looks like this:
to FindOperator {
_type: 'between',
_value: [ 2, 3 ],
_useParameter: true,
_multipleParameters: true }
What seems to be happening is that the Between
find operator is getting passed to the transformer as an object. I first ran into this issue because this caused my transformer to throw an exception.
I searched around for this issue and judging from @pleerock’s comment here it seems that transformers are perhaps an underdeveloped feature right now. That’s fine if they aren’t applied in all cases, however it’d be ideal if things like find operators still worked at all under a transformer.
Would it be an easy fix to just sidestep transformers for find operators? That is, you’d use an un-transformed value for that Between operator.
My concrete use case here BTW is converting Date columns to use Luxon DateTimes.
Thanks in advance!
Issue Analytics
- State:
- Created 5 years ago
- Reactions:19
- Comments:10 (4 by maintainers)
Im keeping open all issues related to
transformer
and once I get more free time I’ll resolve them allI think in most circumstances one would want the transformer to act on the parameters to the
FindOperator
, rather than the operator itself. Take the example below:Except for
Raw()
, which isn’t really an “operator”, all of the values here should be mapped to therank
representation using the transformer:Only in situations where more complex logic applies should a transformer be expected to modify an operator, and I think @Ciantic’s suggestion would be the best way to achieve that. A good use case for this would be special handling for
Date
objects.