Comments (6)
Not that I know of, but seems like this would have to be a setting on Postgres if this were fixable at all. Not much pg-types can do here.
from node-pg-types.
If pg at least passed the tableID
& columnID
to the parserFn
, then I could work with that. As it stands I would need to manually loop over rows
, check corresponding Field
object and map manually.
from node-pg-types.
For those who are too lazy to fork and can do with a bit of hackery, here's code that may break at any moment:
const db = require("pg");
const Result = require("pg/lib/result");
const ID_PARSERS = {};
// get OIDs of tables where custom_id is used, and their column-orders in
// such tables
await db.pool.query(`
SELECT
t.typbasetype "dataTypeID",
c.oid "tableID",
a.attnum "columnID"
FROM pg_class c
INNER JOIN pg_attribute a
ON a.attrelid = c.oid
INNER JOIN pg_type t
ON t.oid = a.atttypid
WHERE
t.typname = 'custom_id'
ORDER BY c.oid DESC
`).then(({ rows }) => {
const idParser = (value) => {
// do whatever you want here
return value;
};
rows.forEach(({dataTypeID, tableID, columnID}) => {
ID_PARSERS[`${dataTypeID}.${tableID}.${columnID}`] = idParser;
});
});
// https://github.com/brianc/node-postgres/blob/ea6ac2ad2313af57159b10a0292c0c178e8e0923/packages/pg/lib/result.js#L86
Result.prototype.addFields = function (fieldDescriptions) {
this.fields = fieldDescriptions;
if (this.fields.length) {
this._parsers = new Array(fieldDescriptions.length);
}
for (var i = 0; i < fieldDescriptions.length; i++) {
const {dataTypeID, tableID, columnID, format} = fieldDescriptions[i];
const customParser = ID_PARSERS[`${dataTypeID}.${tableID}.${columnID}`];
if (customParser) {
this._parsers[i] = customParser;
} else if (this._types) {
this._parsers[i] = this._types.getTypeParser(dataTypeID, format || "text");
} else {
this._parsers[i] = db.types.getTypeParser(dataTypeID, format || "text");
}
}
};
from node-pg-types.
@bendrucker btw, there is a way to support this natively: use pg_type.oid
instead of pg_type.typbasetype
which is what pg seems to be using for domains when it builds dataTypeID
for the fieldDescriptions
.
https://www.postgresql.org/docs/current/catalog-pg-type.html
from node-pg-types.
Nice! Is there a change we should be making to pg to support this better?
from node-pg-types.
I'm getting lost in the codebase. E.g., I'm not sure where the reader
is populated:
In any case, I'm not sure if changing dataTypeID
to be pg_type.oid
is possible as that would be a breaking change. So my suggestion would be to add an extra property, something like exactDataTypeID
, which would be set to pg_type.oid
(note sure if you're fetching from pg_type
. If you're using pg_attribute
table instead, then use pg_type.atttypid
which should correspond to the same value).
That said, for that to be useful you'd need to add a new method setExactTypeParser(exactDataTypeID, parserFn)
. But even if you just exposed it in the fieldDescriptions[]
it would go a long way as users could just override Result.prototype.addFields
or Result.prototype.getTypeParser
to use exactDataTypeID
instead of dataTypeID
.
from node-pg-types.
Related Issues (20)
- Parser not working for bigint values HOT 3
- Can I customize type parsing per query? HOT 3
- please throw when oid is nullish HOT 1
- Reuse postgres-bytea HOT 6
- Releasing the latest changes HOT 4
- parsing postgres box type? HOT 2
- should handle null values HOT 1
- overriding a type parser HOT 1
- Does full text search with to_tsquery work ? HOT 1
- Is the Date parsing fix in the current pg release? HOT 1
- How to import pg-types using ES6 syntax? HOT 2
- Not able to use with modules HOT 1
- +num over parseInt(num, 10) HOT 1
- Changelog with breaking changes? HOT 1
- Add support for parsing RECORD types (OIDs 2249 and 2287) HOT 3
- Can this package be used in the browser? HOT 2
- Tracking down geometry oids HOT 3
- Readme. What number represents what type. HOT 2
- types.setTypeParser(types.builtins.DATE) does not work
- Numeric values are parsed as strings but arrays of numerics are parsed as number arrays HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from node-pg-types.