Transformers and FindOperators like MoreThan or Raw
See original GitHub issueIssue type:
[ ] question [x] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[x] mysql / mariadb
[ ] oracle
[ ] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[ ] 0.x.x (or put your version here)
Steps to reproduce or a small repository showing the problem: when use transformer like this
@Column({
name: "date_time",
type: "datetime",
transformer: {
from: (date: Date) =>moment(date).format("YYYY-MM-DD HH:mm:ss"),
to: (date: string) => moment(date).toDate(),
}
})
dateTime: string;
find operators like MoreThan and Raw doesn’t work properly for date fields.
// success
let data = await getRepository(BdBill).find({
dateTime:"2019-04-10 06:47:18"
});
// all failure under
let data = await getRepository(BdBill).find({
dateTime:LessThan("2019-04-10 06:47:18")
});
let data = await getRepository(BdBill).find({
dateTime:LessThan(new Date())
});
let data = await getRepository(BdBill).find({
dateTime: Raw(alias =>`${alias} > ${new Date()}`)
});
let data = await getRepository(BdBill).find({
dateTime: Raw(alias =>`${alias} > str_to_date('2019-04-10 06:47:18','%Y-%m-%d %H:%i:%s')`)
});
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:9 (2 by maintainers)
Top Results From Across the Web
TypeORM FindOperators with transformer - Stack Overflow
Ok so after several hours of reading through the code and how queries are assembled in TypeORM, I've come up with the following...
Read more >Globals - typeorm
Column decorator is used to mark a specific class property as a table column. Only properties decorated with this decorator will be persisted...
Read more >typeorm: CHANGELOG
FindOperator -s can be applied for relations in where statement, for example: userRepository.find({. where: {. photos: MoreThan(10),. }.
Read more >typeorm | Yarn - Package Manager
Elegant-syntax, flexible and powerful QueryBuilder. Left and inner joins. Proper pagination for queries using joins. Query caching. Streaming raw results.
Read more >Source - GitHub
membership: MembershipKind` you could have a query like: ```ts ... closes [#1505](https://github.com/typeorm/typeorm/issues/1505) * add transformer to ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

So what is the recommended workaround if you want to use value transformers and FindOperators? I didn’t really understand the reasoning why this problem cannot be solved in Typeorm?
There is a hacky way to manipulate the value inside the FindOperator
This seems to work with all FindOperators. Better solution would be to flag value inside FindOperator to NOT readonly …