Repository navigation
Description
I want to add support for custom ranges. The only database that supports ranges is postgresql, so it is the only dialect I modify. The main challenge is that custom ranges oids are not consistent across database instances. I decided to hook postgres ranges on _refreshDynamicOIDs. Although this method works only on postgres versions above 8.3, it's not a problem. Support for ranges was added on 9.3.
My progress so far: https://github.com/javiertury/sequelize
- Automatically detect all base ranges, array ranges and their basetypes.
- Hooked ranges into
_refreshDynamicOIDs
- Hooked ranges into
- Use postgres global
DataTypes.postgres.RANGE.oid_map - Use global
DataTypes.RANGE.subtypesforRANGE.prototype.toSql - Use global
DataTypes.RANGE.castTypesforRANGE.prototype.toCastType
Notice that while postgres range class creates a new property for oid_map, it overrides basetype range property subtypes and castTypes. The reason is that subtypes and castTypes are theoretically usable across dialects, other databases could decide to implement ranges.
After this, implementing custom ranges should be as easy as
function addTimerange(DataTypes) {
DataTypes.RANGE.subtypes.custom = 'customrange';
DataTypes.RANGE.castTypes.custom = 'custombasetype';
}
Doubts
- Does overriding basetype range properties
subtypesandcastTypeshave any side effect? I mean, can sequelize theoretically use more than one database dialect at the same time? Then those properties would get overriden and the system would be messed up. - The purpose of this code is to workaround different oids across database instances. Can sequelize connect to more than one instance at the same time and override
oid_mapof another instance?
Activity
sushantdhiman commentedon Oct 21, 2018
Yes, these are very valid concerns. In same application you could be using multiple sequelize instances which may connect to different databases, hence multiple difference OIDs. Check this PR #9026
At least for now I think Postgres OIDs won't interfere with other instances, but I still think at given instance datatypes oids information may be invalid in multiple instance environment i.e.
I have never tested this but I think we can somehow link oid information to instance then it would be great
javiertury commentedon Oct 22, 2018
I've made further progress at my fork, ranges are configured automatically.
RANGE.oid_map,RANGE.subtypesandRANGE.castTypesare automatically setup at the time_refreshDynamicOIDsis called. Developeers have to create their custom base type in sequelize, create the custom base type in postgresql and create the custom range in postgresql. The custom range in sequelize will be automatically detected.Now I will go back to the multi-instance problem. We can assume that
RANGE.subtypesandRANGE.castTypeswill be equal across instances since they are the name of the of the range and the name of underlying data type in the database. Both strings are under the control of the developer.The problematic components are
RANGE.oid_map,<dataType>.types.postgres.oidsand<dataType>.types.postgres.array_oids. Custom data types can have different oids at different postgres instances, and developers can do nothing about it. One idea is to move these dynamic components to properties of the connection.@sushantdhiman, what do you think?
sushantdhiman commentedon Oct 23, 2018
This bugs me as well. I think we need to keep them in
connectionManagerrather thanconnection. This https://github.com/sequelize/sequelize/blob/master/lib/dialects/postgres/connection-manager.js#L45 type can be improvedjaviertury commentedon Oct 25, 2018
Alright, I took your suggestion and replaced
oidMapandarrayOidMap.dataTypes.postgreshave their OIDs dynamically mappeddataTypes.postgresare dynamically mappedthis.lib.types.getTypeParserI managed to contain OIDs in connection manager. Now
dataType.types.postgresuses names instead of oids. Connection manager creates anameOidMapthat contains OIDs of postgres data types. This object is populated at_refreshDynamicOIDs(). All data types, except enums, fit into this structure.At
_refreshTypeParser(), eachpgTypeNamein the arraydataType.types.postgresis processed. The name is mapped to an oid usingnameOidMapwhich is associated todataType.parser. Ranges and enums are handled separatedly.Data types in
dataTypes.postgreswith.parseand.types.postgresproperties are dynamically processed. New base data types indataTypes.postgresshould benefit from this. The OIDs for the base type, the array and range should be automatically handled by sequelize. After you create a new base type and a range in postgres, you only have to configure the base type in sequelize like this.I've just tested the changes with a toy project of mine. Although I have added integration tests, I don't know how to properly run them. Here are the changes I have made so far. Please let me know what you think about this approach, and if integration tests are alright.
javiertury commentedon Oct 26, 2018
Finally I backed off a bit. The pull requests only adds support for dynamic oids, which are completely contained in connection manager.
You don't have to worry about which is the OID of your custom data type at each postgres instance. Here is an example on how to add a custom baseType and its range. You have to manually map the range.
The reason I dropped automatic range detection is simplicity. Data types methods(
toSql(),strinfigy(), etc) need information about rangesubtypesandcastTypesimmediately after Sequelize is available. One solution is to promisifynew Sequelize()or to syncronously delay it untilsubtypesandcastTypesare loaded from the database. An alternative is to promisify data types methods liketoSql(),stringify(), etc. I think both alternatives are ugly.javiertury commentedon Nov 1, 2018
I will close this issue about creation of custom range types. I believe dynamic OID mapping is the most that can be done. Users will have to manually map the names in
subtypesandcastTypes.The reason is that automatic range detectino requires
subtypesandcastTypesto be populated on Sequelize initialization, or data type methods liketoSql(),stringify()... to be promisified.