Support custom ID fields #9

Closed
opened 2016-07-13 05:42:30 +00:00 by casalot · 14 comments
casalot commented 2016-07-13 05:42:30 +00:00 (Migrated from github.com)

@mdibaiee, I'm seeing some strange behaviour with the auto route creation.

Here's an example of 2 of my entity models:
Position

module.exports = function(sequelize, DataTypes) {
  var Position = sequelize.define('Position', {
    positionId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: 'CURRENT_TIMESTAMP'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: 'CURRENT_TIMESTAMP'
    }
  }, {
    tableName: 'Position'
  });
  return Position;
};

Question

module.exports = function(sequelize, DataTypes) {
  var Question = sequelize.define('Question', {
    questionId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    questionnaireSectionId: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0',
      references: {
        model: 'QuestionnaireSection',
        key: 'questionnaireSectionId'
      }
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    },
    description: {
      type: DataTypes.STRING,
      allowNull: true
    },
    weight: {
      type: DataTypes.INTEGER(4),
      allowNull: false,
      defaultValue: '1'
    },
    sortOrder: {
      type: DataTypes.INTEGER(11),
      allowNull: false,
      defaultValue: '0'
    },
    createdAt: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: 'CURRENT_TIMESTAMP'
    },
    updatedAt: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: 'CURRENT_TIMESTAMP'
    }
  }, {
    tableName: 'Question'
  });
  return Question;
};

And, the created routes:
Position

POST   /api/seq/position
GET    /api/seq/position/{id?}
DELETE /api/seq/position/{id?}
PUT    /api/seq/position/{id}
DELETE /api/seq/positions
GET    /api/seq/positions
GET    /api/seq/positions/{scope}
DELETE /api/seq/positions/{scope}

Question

GET    /api/seq/questions
DELETE /api/seq/questions
DELETE /api/seq/questions/{scope}
GET    /api/seq/questions/{scope}
@mdibaiee, I'm seeing some strange behaviour with the auto route creation. Here's an example of 2 of my entity models: `Position` ``` javascript module.exports = function(sequelize, DataTypes) { var Position = sequelize.define('Position', { positionId: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, title: { type: DataTypes.STRING, allowNull: false }, createdAt: { type: DataTypes.DATE, allowNull: true, defaultValue: 'CURRENT_TIMESTAMP' }, updatedAt: { type: DataTypes.DATE, allowNull: true, defaultValue: 'CURRENT_TIMESTAMP' } }, { tableName: 'Position' }); return Position; }; ``` `Question` ``` javascript module.exports = function(sequelize, DataTypes) { var Question = sequelize.define('Question', { questionId: { type: DataTypes.INTEGER(11), allowNull: false, primaryKey: true, autoIncrement: true }, questionnaireSectionId: { type: DataTypes.INTEGER(11), allowNull: false, defaultValue: '0', references: { model: 'QuestionnaireSection', key: 'questionnaireSectionId' } }, title: { type: DataTypes.STRING, allowNull: false }, description: { type: DataTypes.STRING, allowNull: true }, weight: { type: DataTypes.INTEGER(4), allowNull: false, defaultValue: '1' }, sortOrder: { type: DataTypes.INTEGER(11), allowNull: false, defaultValue: '0' }, createdAt: { type: DataTypes.DATE, allowNull: true, defaultValue: 'CURRENT_TIMESTAMP' }, updatedAt: { type: DataTypes.DATE, allowNull: true, defaultValue: 'CURRENT_TIMESTAMP' } }, { tableName: 'Question' }); return Question; }; ``` And, the created routes: `Position` ``` POST /api/seq/position GET /api/seq/position/{id?} DELETE /api/seq/position/{id?} PUT /api/seq/position/{id} DELETE /api/seq/positions GET /api/seq/positions GET /api/seq/positions/{scope} DELETE /api/seq/positions/{scope} ``` `Question` ``` GET /api/seq/questions DELETE /api/seq/questions DELETE /api/seq/questions/{scope} GET /api/seq/questions/{scope} ```
mdibaiee commented 2016-07-13 05:53:39 +00:00 (Migrated from github.com)

This is indeed weird, hmm...

One thing is, we use { id: request.query.id } for finding by id, and you are using custom fields, questionId and positionId.

But you say the Position model's id route works! Now that's inconsistency. Do you get any errors/warnings?

Let me see if I can reproduce your problem.

This is indeed weird, hmm... One thing is, we use `{ id: request.query.id }` for finding by `id`, and you are using custom fields, `questionId` and `positionId`. But you say the Position model's id route works! Now that's inconsistency. Do you get any errors/warnings? Let me see if I can reproduce your problem.
casalot commented 2016-07-13 06:05:03 +00:00 (Migrated from github.com)

/position/1and /question/1 throws 500.
/positions/1 and /questions/1 throws 400, with message "child "scope" fails because ["scope" must be one of []]".

/poisitions and /questions (without id's) works 100%.

`/position/1`and `/question/1` throws 500. `/positions/1` and `/questions/1` throws 400, with message "**child \"scope\" fails because [\"scope\" must be one of []]**". `/poisitions` and `/questions` (without id's) works 100%.
mdibaiee commented 2016-07-13 06:09:30 +00:00 (Migrated from github.com)

Okay, so it's not inconsistency, is it? You get the same routes for both models, if that's the case, your problem is with ids.

You have positions/{scope}, not positions/{id}, instead there is position/{id} (which doesn't work in your case.

If I'm correct, the problem is with custom id fields.

Okay, so it's not inconsistency, is it? You get the same routes for both models, if that's the case, your problem is with `id`s. You have `positions/{scope}`, not `positions/{id}`, instead there is `position/{id}` (which doesn't work in your case. If I'm correct, the problem is with custom id fields.
casalot commented 2016-07-13 06:12:37 +00:00 (Migrated from github.com)

I think the custom id is one of the issues. I don't understand why the position entity has 8 routes defined, and the question just 4? Or, are these two questions/issues related?

I think the custom id is one of the issues. I don't understand why the `position` entity has 8 routes defined, and the `question` just 4? Or, are these two questions/issues related?
mdibaiee commented 2016-07-13 06:13:49 +00:00 (Migrated from github.com)

@casalot: Maybe you have relations/associations defined for your Position model?

@casalot: Maybe you have relations/associations defined for your `Position` model?
casalot commented 2016-07-13 06:17:56 +00:00 (Migrated from github.com)

Nope. Neither Position nor Question has any relations defined.

Nope. Neither `Position` nor `Question` has any relations defined.
mdibaiee commented 2016-07-13 06:25:09 +00:00 (Migrated from github.com)

Umm, I'm not sure about that. But I think we can address the id issue here, and if you encounter any problem with the routes, we'll get to that later. What do you say?

Umm, I'm not sure about that. But I think we can address the id issue here, and if you encounter any problem with the routes, we'll get to that later. What do you say?
casalot commented 2016-07-13 06:28:09 +00:00 (Migrated from github.com)

Cool. Should I rename the custom id's to id? Or, is there a why to alias it?

Cool. Should I rename the custom id's to `id`? Or, is there a why to alias it?
mdibaiee commented 2016-07-13 06:29:34 +00:00 (Migrated from github.com)

I think we should allow custom id fields, I'm seeing if there is a way to automatically detect ID fields (using PRIMARY KEYs, INDEXes, etc.), or we should ask the programmer to explicitly set the custom field's name.

I think we should allow custom id fields, I'm seeing if there is a way to automatically detect ID fields (using PRIMARY KEYs, INDEXes, etc.), or we should ask the programmer to explicitly set the custom field's name.
casalot commented 2016-07-13 06:32:24 +00:00 (Migrated from github.com)

I have set primaryKey: true on both entities. Didn't make a difference.

Changing the id from positionId to id on the database table, now gets results back for /position/1.

I have set `primaryKey: true` on both entities. Didn't make a difference. Changing the id from `positionId` to `id` on the database table, now gets results back for `/position/1`.
mdibaiee commented 2016-07-13 06:32:56 +00:00 (Migrated from github.com)

Okay it seems there is a way to automatically use the primary key set by programmer, I'm going to patch it right now. 👍

Okay it seems there is a way to automatically use the primary key set by programmer, I'm going to patch it right now. :+1:
mdibaiee commented 2016-07-13 07:00:13 +00:00 (Migrated from github.com)

Published to npm hapi-sequelize-crud@2.4.0, see if it works for you.

Published to npm `hapi-sequelize-crud@2.4.0`, see if it works for you.
casalot commented 2016-07-13 07:02:07 +00:00 (Migrated from github.com)

Working perfect now!

Working perfect now!
mdibaiee commented 2016-07-13 07:03:06 +00:00 (Migrated from github.com)

Great! 👍

Great! :+1:
Sign in to join this conversation.
No Milestone
No project
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: thereadme/hapi-sequelize-crud#9
No description provided.