Skip to content

Support for custom range types (Automatic) #9863

Closed

Description

@javiertury

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
  • Use postgres global DataTypes.postgres.RANGE.oid_map
  • Use global DataTypes.RANGE.subtypes for RANGE.prototype.toSql
  • Use global DataTypes.RANGE.castTypes for RANGE.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 subtypes and castTypes have 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_map of another instance?

Activity

  1. sushantdhiman commented on Oct 21, 2018

    @sushantdhiman
    Contributor

    Does overriding basetype range properties subtypes and castTypes have 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_map of another instance?

    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.

    Datatypes.postgres.CITEXT.oids // say it is set to empty at start
    
    // instance one initialises, query for OIDs
    Datatypes.postgres.CITEXT.oids // is now set to 100
    
    // instance two initialises, query for OIDs
    Datatypes.postgres.CITEXT.oids // is now set to 101
    
    // Both instances are parsing data correctly
    // because oid map in unique to each instance with connectionManager.oidMap

    I have never tested this but I think we can somehow link oid information to instance then it would be great

  2. added
    status: in discussion
    on Oct 21, 2018
  3. javiertury commented on Oct 22, 2018

    @javiertury
    ContributorAuthor

    I've made further progress at my fork, ranges are configured automatically. RANGE.oid_map, RANGE.subtypes and RANGE.castTypes are automatically setup at the time _refreshDynamicOIDs is 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.subtypes and RANGE.castTypes will 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.oids and <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?

  4. sushantdhiman commented on Oct 23, 2018

    @sushantdhiman
    Contributor

    The problematic components are RANGE.oid_map, .types.postgres.oids and .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.

    This bugs me as well. I think we need to keep them in connectionManager rather than connection. This https://github.com/sequelize/sequelize/blob/master/lib/dialects/postgres/connection-manager.js#L45 type can be improved

  5. javiertury commented on Oct 25, 2018

    @javiertury
    ContributorAuthor

    Alright, I took your suggestion and replaced oidMap and arrayOidMap.

    • OIDs are completely contained in connection manager
    • Data types in dataTypes.postgres have their OIDs dynamically mapped
      • Both existing and new types in dataTypes.postgres are dynamically mapped
      • The rest just use this.lib.types.getTypeParser
    • Ranges are automatically detected and setup

    I managed to contain OIDs in connection manager. Now dataType.types.postgres uses names instead of oids. Connection manager creates a nameOidMap that contains OIDs of postgres data types. This object is populated at _refreshDynamicOIDs(). All data types, except enums, fit into this structure.

    {
      [pgTypeName]: {
        oid: 1,
        arrayOid: 2,
        rangeOid: 3,
        arrayRangeOid: 4,
      }
      ...
    }
    

    At _refreshTypeParser(), each pgTypeName in the array dataType.types.postgres is processed. The name is mapped to an oid using nameOidMap which is associated to dataType.parser. Ranges and enums are handled separatedly.

    Data types in dataTypes.postgres with .parse and .types.postgres properties are dynamically processed. New base data types in dataTypes.postgres should 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.

    // lib/dialects/postgres/data-types.js
    
    newDataType.types.postgres = ['<pgTypeName>'];
    newDataType.parse = ...
    

    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.

  6. javiertury commented on Oct 26, 2018

    @javiertury
    ContributorAuthor

    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.

    // lib/dialects/postgres/data-types.js
    
    newDataType.key = '<seqBaseTypeName>'
    newDataType.types.postgres = ['<pgBaseTypeName>'];
    newDataType.parse = ...
    
    BaseTypes.RANGE.types.postgres.subtypes['<seqBaseTypeName>'] = '<pgRangeName>';
    BaseTypes.RANGE.types.postgres.castTypes['<seqBaseTypeName>'] = '<pgBaseTypeName>';
    

    The reason I dropped automatic range detection is simplicity. Data types methods(toSql(), strinfigy(), etc) need information about range subtypes and castTypes immediately after Sequelize is available. One solution is to promisify new Sequelize() or to syncronously delay it until subtypes and castTypes are loaded from the database. An alternative is to promisify data types methods like toSql(), stringify(), etc. I think both alternatives are ugly.

  7. javiertury commented on Nov 1, 2018

    @javiertury
    ContributorAuthor

    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 subtypes and castTypes.

    The reason is that automatic range detectino requires subtypes and castTypes to be populated on Sequelize initialization, or data type methods like toSql(), stringify()... to be promisified.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions