Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom getter not being called properly. #129

Closed
subodhpareek18 opened this issue Jun 23, 2017 · 7 comments
Closed

Custom getter not being called properly. #129

subodhpareek18 opened this issue Jun 23, 2017 · 7 comments

Comments

@subodhpareek18
Copy link

I have the following model set up in sequelize:

import uid from 'uid-safe';

export default (sequelize, DataTypes) => {
  const Model = sequelize.define(
    'location',
    {
      id: {
        type: DataTypes.STRING,
        defaultValue: () => `loc_${uid.sync(6)}`,
        primaryKey: true
      },
      coordinates: {
        type: DataTypes.GEOMETRY('POINT'),
        get() {
          const value = this.getDataValue('coordinates');
          return value === null ? null : value.coordinates;
        },
        set(value) {
          if (value === null) {
            this.setDataValue('coordinates', null);
          } else {
            this.setDataValue('coordinates', {
              type: 'Point',
              coordinates: value
            });
          }
        },
        validations: {
          isCoordinateArray: value => {
            if (!Array.isArray(value) || value.length !== 2) {
              throw new Error('Must be an array with 2 elements');
            }
          }
        }
      },
    }
  );
  return Model;
};

Now every thing works fine when I create a record.

// request
{
	"postcode": "XYZ",
	"coordinates": [-75.343, 39.984]
}
 
// response
{
	"coordinates": [
		-75.343,
		39.984
	],
	"id": "loc_Eeksy8q8",
	"postcode": "XYZ",
}

But when I look at the response on a get or find method the custom getter doesn't get called and I see this instead:

{
	"coordinates": {
		"type": "Point",
		"coordinates": [
			-75.343,
			39.984
		]
	},
	"id": "loc_Eeksy8q8",
	"postcode": "XYZ",
}

System configuration

Module versions:

{
    "feathers": "^2.1.1",
    "feathers-sequelize": "^2.0.1",
    "pg": "^6.1.5",
    "sequelize": "^3.30.4"
}

Database Setup:
PostGreSQL 9.6
with postGIS 2.3 Bundle

NodeJS version:
v8.0.0

Operating System:
Windows 10 Pro

Browser Version:
Chrome Version 60.0.3112.40 (Official Build) beta (64-bit)

@subodhpareek18
Copy link
Author

Hi, any input on this? Sorry for being impatient.

@daffl
Copy link
Member

daffl commented Jul 21, 2017

Setting raw: false in the service options should solve your issue.

@DesignByOnyx
Copy link
Contributor

@zusamann - I can confirm. By default, we set raw: true because 1) it's more efficient - way less processing overhead, and 2) it allows feathers-sequelize to work with common hooks and other 3rd party integrations out of the box. If you are needing something which is specific to sequelize (such as getters and setters), you will indeed need to follow this part of the guide. I just submitted a PR to update the guides and make this more prevalent.

@daffl daffl closed this as completed Mar 9, 2018
@knoxcard
Copy link

Heard a fix is in the works. When it will be released? unknown, but sounds like within the next 6 months or so.

@sharadshetty2
Copy link

Setting raw: false in the service options should solve your issue.

I'm using the sequelize migrations.I've gone through the feathersjs link provided still I've no idea where to pass the option raw:false.

@daffl
Copy link
Member

daffl commented Apr 1, 2020

In the options of src/services/<name>/<name>.service.js (e.g. /~https://github.com/feathersjs/feathers-chat/blob/master/src/services/users/users.service.js#L10)

@sharadshetty2
Copy link

I'm using the project structure created by sequelize-cli. ( https://sequelize.org/v5/manual/migrations.html)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants