0

I would like to be able to have the formatting.formatValue function convert undefined into null.

But there does not appear to be a way that I can set the default function from the initOptions given to pg-promise as pgFormatting is a boolean, format value wants it as an argument. But the library does not appear to pass it for normal queries.

vitaly-t wrote

But i'm not sure how much value it would add, since you do not have access to formatting options from query methods.

And that is true. But I kind of just want to set a default of null for all missing values being formatted into queries. Partial does not mater I don't use it?

Using coalesce to have "optional" values in some of the queries, to default not set variables anyway.

Example:

UPDATE sometable SET value = COALESCE(${value}, value)

How can I stop getting the "Property doesn't exist Error?

I think that I need a way to pass a value to this options for all calls to the format.

if ('default' in options) {
    const d = options.default, value = typeof d === 'function' ? d.call(obj, v.name, obj) : d;
    return formatValue(value, v.fm, obj);
}

Related issue that led to creation of options.default. https://github.com/vitaly-t/pg-promise/issues/173

I can't over-ride the format value function as it is added as a non configurable read only property.

Do I just have to bite the bullet and put the variables in as null everywhere I want them to be optional...

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
Liam Mitchell
  • 1,001
  • 13
  • 23

1 Answers1

0

I kind of just want to set a default of null for all missing values being formatted into queries...

Here's your formatting function for this, if you want to format queries manually:

function format(query, values) {
    return pgp.as.format(query, values, {'default': null});
}

And for automatically generated queries within the helpers namespace, you would provide the value for property def, as per the Column API.

Other than that, the query methods make use of the format function on their own, so you cannot override that, neither you should, it is generally not needed, if you make the right use of the library's API, i.e. if you can give me a specific example of where you think you need it, then I can advise you of the better approach that avoids it.

One other approach - method helpers.concat can consume the formatting options, as it implements a specific case of joining queries.

vitaly-t
  • 24,279
  • 15
  • 116
  • 138
  • The use case is for having a query where the parameters may or may not have the value set. Where a set value will be updated as-is but a non set value should pass null to use the current value that is in that record because of the use of COALESCE(${value}, value) It would be nice to be able to set it as an option so that those query methods could have the default not found to null behavior. Why might you consider this generally not needed? To me it seems like something lacking from query methods that can be done with the format method. It would be odd to call the format method twice. – Liam Mitchell Jan 05 '18 at 02:13
  • @LiamMitchell In reality, one can be missing properties only during `INSERT` and `UPDATE` operations, for which you have methods within the [helpers](http://vitaly-t.github.io/pg-promise/helpers.html) namespace. In all other cases it is not really needed. – vitaly-t Jan 05 '18 at 09:51