panates / postgrejs

Professional PostgreSQL client for NodeJS
https://postgrejs.panates.com
MIT License
50 stars 12 forks source link

Registering an object type in the GlobalTypeMap but 'json' type always wins #21

Closed unilynx closed 1 year ago

unilynx commented 1 year ago

How can I properly define my own object type in the GlobalTypeMap - or am I not supposed to do that ?

I'm trying to add a type using GlobalTypeMap.register but its isType is never invoked as the builtin JSONType simply checks for typeof v === "object" and GlobalTypeMap.register adds new types to the end of the list.

erayhanoglu commented 1 year ago

Hi GlobalTypeMap is a registry that matches PostgreSQL's OID's with JavaScript types. And you are right "DataTypeMap.determine" method lookups for data types in "fifo" order. So this might be a problem for registering new types. To overcome this issue, I have released a new version (2.6.0) with the following changes. Now DataTypeMap.determine method lookup for data-types in reverse order. So last registered data-type returns first. Please let me know if this fixes your problem.

unilynx commented 1 year ago

Please let me know if this fixes your problem.

Thanks. I'll try to doublecheck later but I expect it should - I simply replaced push with unshift in the register call which fixed it too and has about the same effect

unilynx commented 1 year ago

yep ,works

erayhanoglu commented 1 year ago

Which OID do you registering as a new type? Are there any missed data types in the library?

unilynx commented 1 year ago

It's not an existing type, but a custom type (CREATE TYPE webhare_internal.webhare_blob AS (id text, size int8)) which I convert from/to a JS Object that wraps a blob/file stored outside postgres.

unilynx commented 1 year ago

@erayhanoglu doesn't simply reversing the lookup order break other assumptions/risk regressions? Eg JsonType and JsonbType have the same isType check. So 2.6.0 would match with the JsonbType where older versions would match JsonType.

I spoke too soon, you also reordered the registrations.

erayhanoglu commented 1 year ago

You are welcome. :grinning: