Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Column: expose reference to Column object in init callback #952

Open
mielvds opened this issue Feb 3, 2025 · 4 comments
Open

Column: expose reference to Column object in init callback #952

mielvds opened this issue Feb 3, 2025 · 4 comments

Comments

@mielvds
Copy link

mielvds commented Feb 3, 2025

Hi!

I'm using the init callback function on Column to sanitize some input based on the column's type casting before inserting.

Quick & dirty example:

const column = new pgp.helpers.Column({
            name,
            cast,
            init: (col: [helpers.initCB](https://vitaly-t.github.io/pg-promise/helpers.html#.initCB)) => {
                // Drop invalid date value
                if (col.exists && cast === 'date' && !isValidDate(col.value)) {
                    return null
                }
                return col.value
            },
        })

While this works fine, It might be a nice addtion to allow access to access the Column object from inside init()
Either by (typescript for clarity):

  • extending the signature: init: (config: helpers.initCB, column: helpers.Column) => { ... }
  • extening helpers.initCB: init: (col: {source: any, name: string, value:any, exists: boolean, column:helpers.Column})

Is this something that can be considered? Would the be a performance hit? Happy to work on a PR

@vitaly-t
Copy link
Owner

vitaly-t commented Feb 4, 2025

You already have column of type Column in your code. So why not just use that?

@mielvds
Copy link
Author

mielvds commented Feb 4, 2025

I do, but that means using information outside of the init function. Fine by me, but it's probably less error-prone to avoid this.

@vitaly-t
Copy link
Owner

vitaly-t commented Feb 4, 2025

In retrospective, I should have passed Column as this into init and others. Instead, this is set to the source, for some forgotten now reasons. Correcting it though would be a breaking change, perhaps now worth doing, unless a major version is impending.

I am still not clear why do you want to access Column from inside the object itself? You didn't specify the reason.

@mielvds
Copy link
Author

mielvds commented Feb 4, 2025

Because I use Column.cast to check the target datatype and validate the value based on that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants