question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Transformers and FindOperators like MoreThan or Raw

See original GitHub issue

Issue 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:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
sgronblocommented, Jun 17, 2021

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?

2reactions
kaedwencommented, Jul 19, 2019

There is a hacky way to manipulate the value inside the FindOperator

      transformer: {
        from: (value: any) => {
          if (!NullOrUndefined(value) && Number(value)) {
            return new Date(Number(value));
          }
        },
        to: (value: any) => {
          if (value) {
            if (value instanceof Date) {
              return value.getTime();
            } else if (value instanceof FindOperator) {
              
              if (value.value instanceof Date) {
                (value as any)._value = value.value.getTime();
              }

              return value;
              
            }
          }
        }
      }

This seems to work with all FindOperators. Better solution would be to flag value inside FindOperator to NOT readonly …

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found