Skip to content

Data-type UP / DOWN system #2042

Closed
Closed

Description

@janmeier

Supersedes #1690

What we want is some kind of system, which allows a more or less dialect-independent way of converting data types between their javascript at DB representations. This system should also be extendable by the user

Activity

  1. floross commented on Mar 6, 2015

    @floross

    Hi,

    Do you know for when this feature will be implement ? May I can help ?

    Because I would like to use ST_AsGeoJSON and ST_GeomFromGeoJSON from postgis but in this state I can't.

  2. mickhansen commented on Mar 6, 2015

    @mickhansen
    Contributor

    @florian-rossiaud No estimate. When we get to it is all we can say i'm afraid.

  3. janmeier commented on Mar 6, 2015

    @janmeier
    MemberAuthor

    ... But pull requests are always welcome :-).

    For this feature we'd probably need a bit of api discussion first - feel free to provide a suggestion, or more details into the use case you'd like this to support

  4. floross commented on Mar 6, 2015

    @floross

    I think the DataType should know how to escape his value. We could have a function inside DataType escape who espace the value on the query.

    For now escape is like that :

    escape: function(value, field) {
      if (value && value._isSequelizeMethod) {
        return this.handleSequelizeMethod(value);
      }
    
      if (Utils._.isObject(value) && field && (field.type instanceof DataTypes.HSTORE || DataTypes.ARRAY.is(field.type, DataTypes.HSTORE))) {
        if (field.type instanceof DataTypes.HSTORE){
          return "'"  + hstore.stringify(value) + "'";
        } else if (DataTypes.ARRAY.is(field.type, DataTypes.HSTORE)) {
          return "ARRAY[" + Utils._.map(value, function(v){return "'" + hstore.stringify(v) + "'::hstore";}).join(",") + "]::HSTORE[]";
        }
      } else if(Utils._.isArray(value) && field && (field.type instanceof DataTypes.RANGE || DataTypes.ARRAY.is(field.type, DataTypes.RANGE))) {
        if(field.type instanceof DataTypes.RANGE) { // escape single value
          return "'"  + range.stringify(value) + "'";
        }
        else if (DataTypes.ARRAY.is(field.type, DataTypes.RANGE)) { // escape array of ranges
          return "ARRAY[" + Utils._.map(value, function(v){return "'" + range.stringify(v) + "'";}).join(",") + "]::" + field.type.toString();
        }
      } else if (field && (field.type instanceof DataTypes.JSON || field.type instanceof DataTypes.JSONB)) {
        value = JSON.stringify(value);
      } else if (Array.isArray(value) && field && DataTypes.ARRAY.is(field.type, DataTypes.JSON)) {
        return "ARRAY[" + value.map(function (v) {
          return SqlString.escape(JSON.stringify(v), false, this.options.timezone, this.dialect, field);
        }, this).join(",") + "]::JSON[]";
      }
    
      return SqlString.escape(value, false, this.options.timezone, this.dialect, field);
    },
    

    So for each DataType in each dialect you return an formatted string in function of what you need.

    You could use a things like this :

    /**
     * A GEOMETRY column. Only available in postgis.
     * @property GEOMETRY
     */
    var GEOMETRY = function () {
        if (!(this instanceof GEOMETRY)) return new GEOMETRY();
        Sequelize.ABSTRACT.apply(this, arguments);
    };
    util.inherits(GEOMETRY, Sequelize.ABSTRACT);
    
    GEOMETRY.prototype.key = GEOMETRY.key = 'GEOMETRY';
    GEOMETRY.prototype.setter = GEOMETRY.setter = function(value) {
        return 'ST_GeomFromGeoJSON(' + JSON.stringify(value) + ')';
    }
    GEOMETRY.prototype.getter = GEOMETRY.getter = function(field) {
        return 'ST_AsGeoJSON(' + field + ')';
    }
    

    All your logic will be in your dataType.

  5. mickhansen commented on Mar 6, 2015

    @mickhansen
    Contributor

    @florian-rossiaud we would agree with that, hence the proposal for an UP/DOWN system that would take all the current escaping/parsing logic and place it with the data types.

  6. janmeier commented on Mar 7, 2015

    @janmeier
    MemberAuthor

    In your example both ST_GeomFromGeoJSON and ST_AsGeoJSON are DB methods right?

    In my mind, one method takes a JS representation and converts in to DB representation ready for insert, the other takes a DB representation and converts in to JS. An example for DATE would be

    up: function (dateObj) {
      return dateObj.format('YY-M-D h:m:d Z');
    },
    down: function (dateString) {
      return new Date(dateString);
    }
  7. floross commented on Mar 9, 2015

    @floross

    We can already do that with get and set method in the model. But we can do a up and down method for the DataType and a get and set method for have both functionality.

  8. janmeier commented on Mar 9, 2015

    @janmeier
    MemberAuthor

    I know something like that is already possible, but my idea was to move the code up one level of abstraction so to speak - so that it is applied to all field instances of that type:

  9. floross commented on Mar 9, 2015

    @floross

    True that.

    I don't have so much time to implement this pr but if I found some time in the next week ill give a try (to implement up / down and set / get)

  10. self-assigned this
    on Jul 14, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: feature

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions