Skip to content

bindParam missing from AbstractDataType type definition #11550

Closed

Description

@defunctzombie

The bindParam function is missing from the AbstractDataTypes type definition. The result is that a user who implements a custom type must be aware to also implement the bindParam function if they want to do custom escaping or formatting of their type when used with the bind query interface.

Previously it was enough to just support stringify but this is no longer the case with the new bind based query builder.

Activity

  1. added
    typescript
    on Oct 16, 2019
  2. papb commented on Oct 30, 2019

    @papb
    Member

    Hello, can you please show some code that should compile but doesn't? Thanks

  3. added
    status: awaiting response
    on Oct 30, 2019
  4. defunctzombie commented on Nov 6, 2019

    @defunctzombie
    ContributorAuthor

    Absolutely. Tho the issue isn't that the code doesn't compile - its that without the bindParam being a required method to define my custom type did not work properly.

    Below is my custom type definition which is meant to be a wrapper around the postgres NUMERIC type using the big.js library to support arbitrary precision and accurate decimal math operations in my application logic.

    import { AbstractDataType } from 'sequelize';
    import Big from 'big.js';
    
    export default class BigType implements AbstractDataType {
      public dialectTypes: string = '';
      public key: string = 'NUMERIC';
    
      toSql(): string {
        return 'NUMERIC';
      }
    
      toString(): string {
        return 'NUMERIC';
      }
    
      stringify(value: Big): string {
        return value.toString();
      }
    
      bindParam(value: Big): string {
        return value.toString();
      }
    
      _sanitize(value: string): Big {
        return Big(value);
      }
    
      parse(value: string): Big {
        return Big(value);
      }
    }

    When I ported from v4 to v5 I noticed that my queries were failing. I traced the problem down to missing the bindParam method from my implementation. Without this method present in the class, the queries were not serializing properly - this was a runtime error rather than compile time error.

    After debugging by digging into the query generator code, I realized that I needed to add the bindParam method but this method was not part of AbstractDataType and so was not obvious to me that I needed to include it in my type for it to properly serialize. If the bindParam method was in the interface I would have gotten a build time error (what I wanted to see) and it would be obvious that I am missing an important method for proper functioning of my custom type.

  5. papb commented on Jan 15, 2020

    @papb
    Member

    Ah, interesting! Thanks for the explanation and sorry to take so long to reply. It looks like fixing this is quite simple then, just adding one line to the type declarations, am I right? Or did I misunderstand something?

    Also, by the way, since I noticed you are implementing a custom data type, is there any chance you could take a look at this issue to see if you have any ideas? #11177 Many people is currently having trouble to make a custom data type work - myself included. The steps provided in our current docs don't work. Thank you!!

  6. defunctzombie commented on Jan 18, 2020

    @defunctzombie
    ContributorAuthor

    just adding one line to the type declarations, am I right

    Yep. I believe that would do it.

    Also, by the way, since I noticed you are implementing a custom data type, is there any chance you could take a look at this issue to see if you have any ideas? #11177 Many people is currently having trouble to make a custom data type work - myself included.

    I took a quick look at the issue and my initial impression is maybe the missing bindParam implementation. The referenced issue starts with someone wanting to implement a custom big number datatype - which is exactly what I needed. The code I pasted above should work for that use case (it does for me) granted I am using postgres rather than mysql as the original issue states.

  7. papb commented on Jan 19, 2020

    @papb
    Member

    @defunctzombie Thank you!! I will look into it

  8. self-assigned this
    on Jan 19, 2020
  9. added
    status: understood
    and removed
    status: awaiting response
    on Jan 19, 2020
  10. defunctzombie commented on Sep 11, 2021

    @defunctzombie
    ContributorAuthor

    Is this issue still awaiting any other feedback?

  11. github-actions commented on Nov 6, 2021

    @github-actions
    Contributor

    This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: understoodtypescript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions