trailsjs / trailpack-sequelize

:package: Sequelize.js Trailpack http://sequelizejs.com
MIT License
5 stars 9 forks source link

Footprints do not work #16

Closed cesar2064 closed 8 years ago

cesar2064 commented 8 years ago

I have created an user api and have the proper config. in the models, and because the orm is working now. But when I try to acces /api/v1/user or /api/v1/user/find, the server responds: {"error":"Not found"}

UserController.js:

'use strict'

const Controller = require('trails-controller')

/**
 * @module UserController
 * @description Generated Trails.js Controller.
 */
module.exports = class UserController extends Controller{
}

config/main.js:

'use strict'

const path = require('path')

module.exports = {

  /**
   * Order does *not* matter. Each module is loaded according to its own
   * requirements.
   */
  packs: [
    require('trailpack-core'),
    require('trailpack-repl'),
    require('trailpack-router'),
    require('trailpack-express'),
    require('trailpack-sequelize'),
    require('trailpack-footprints')
  ],

  /**
   * Define application paths here. "root" is the only required path.
   */
  paths: {
    root: path.resolve(__dirname, '..'),
    temp: path.resolve(__dirname, '..', '.tmp')
  }
}

config/footprint.js:

/**
 * Footprints Configuration
 * (config.footprints)
 *
 * Footprints are routes that are auto-generated from your model and controller
 * definitions in api/controllers and api/models.
 *
 * @see http://trailsjs.io/doc/config/footprints
 */
module.exports = {

  /**
   * Generate routes for controller handlers.
   */
  controllers: {

    /**
     * Default methods to accept for routes generated from controller handlers.
     */
    method: '*',

    /**
     * List of controllers to ignore; that is, do not generate footprint routes
     * for them.
     */
    ignore: [ ]
  },

  /**
   * Generate conventional Create, Read, Update, and Delete (CRUD) routes for
   * each Model.
   */
  models: {
    options: {

      /**
       * The max number of objects to return by default. Can be overridden in
       * the request using the ?limit argument.
       */
      defaultLimit: 100,

      /**
       * Whether to populate all model associations by default (for "find")
       */
      populate: true
    },

    actions: {
      create: true,
      find: true,
      update: true,
      destroy: true,

      /**
       * Specify which "association" endpoints to activate.
       */
      createAssociation: true,
      findAssociation: true,
      updateAssociation: true,
      destroyAssociation: true
    }
  },

  /**
   * Prefix your footprint routes
   */
  prefix: '/api/v1'
}

Are something wrong or missing in my configuration?

jaumard commented 8 years ago

You don't need an empty controller, where is your model ? Be sure to add it under your api/models/index.js or it will be not load. Also you can use the interactive shell to see if it's load. Just write app.models to see loaded models

cesar2064 commented 8 years ago

yes, all models were loaded and the orm works, I tried to save an user and it worked via shell. User.js

'use strict'

const Model = require('trails-model')

/**
 * @module User
 * @description handle user crud
 */
module.exports = class User extends Model {
  static config (app,Sequelize) {
    return {
       options: {
         classMethods: {
           //If you need associations, put them here
           associate: (models) => {
             //More information about associations here : http://docs.sequelizejs.com/en/latest/docs/associations/
             models.User.belongsToMany(models.Role, {
               as: 'roles',
               onDelete: 'CASCADE',
               through:{
                model: models.UserRole,
                unique:false
               },
               foreignKey: "user_id"
             })
           }
         }
       }
     }
  }

  static schema (app,Sequelize) {
    return {
         name: { type: Sequelize.STRING, allowNull: false },
         password: Sequelize.STRING,
         displayName: Sequelize.STRING
    }
  }
}

modesl/index.js

'use strict'
exports.User = require('./User')
exports.Role = require('./Role')
exports.UserRole = require('./UserRole')
jaumard commented 8 years ago

/api/v1/user/find will never work (not implemented on footprints) but GET /api/v1/user should work... can you try GET /user

cesar2064 commented 8 years ago

GET /user, doesn't work. My current dependencies :

  "dependencies": {
    "eslint": "^2.11.1",
    "express": "^5.0.0-alpha.2",
    "pg": "^4.5.6",
    "trailpack-core": "^1.0.0",
    "trailpack-express": "^1.0.1",
    "trailpack-footprints": "^1.0.0",
    "trailpack-repl": "^1.0.7",
    "trailpack-router": "^1.0.2",
    "trailpack-sequelize": "^1.0.0",
    "trails": "^1.0.4",
    "trails-controller": "^1.0.0-beta-2",
    "trails-model": "^1.0.0-beta-2",
    "trails-policy": "^1.0.2",
    "trails-service": "^1.0.0-beta-2",
    "winston": "^2.2.0"
  }
jaumard commented 8 years ago

What version of trailpack-sequelize ? Because here it's trailpack-mongoose lol I'll try to generate a project with same dependencies to see if I can replicate

cesar2064 commented 8 years ago

yes, sorry I edited my dependencies again, wrong dependencies :P

cesar2064 commented 8 years ago

"trailpack-sequelize": "^1.0.0"

cesar2064 commented 8 years ago

Do you need my other models?

jaumard commented 8 years ago

Yes like this I can make full test like you. What's your node and npm version also please ?

cesar2064 commented 8 years ago

node version: 6.2.0 npm version : 3.5.3

Role.js

module.exports = class Role extends Model {

  static config () {
    return {
       options: {
         classMethods: {
           //If you need associations, put them here
           associate: (models) => {
             //More information about associations here : http://docs.sequelizejs.com/en/latest/docs/associations/
             models.Role.belongsToMany(models.User, {
               as: 'users',
               onDelete: 'CASCADE',
               through:{
                model: models.UserRole,
                unique:false
               },
               foreignKey: "role_id"
             })
           }
         }
       }
     }
  }

  static schema (app,Sequelize) {
    return {
         name: { type: Sequelize.STRING, allowNull: false }
    }
  }
}

UserRole.js

module.exports = class UserRole extends Model {

  static config () {
  }

  static schema (app,Sequelize) {
    return {
      role_id: {
        type: Sequelize.INTEGER,
        references: {
          model: 'role',
          key: 'id'
        },
        allowNull: false
      },
      user_id:{
        type: Sequelize.INTEGER,
        references:{
          model: 'user',
          key: 'id'
        },
        allowNull: false
      }
    }
  }
}
jaumard commented 8 years ago

Ho just see you're using footprints v1.0.0 witch is not the latest version try "trailpack-footprints": "^1.0.0-beta-7",

Just try with this version and all is working for me :) Don't know why there a v1.0.0 on npm... Let me know if it's fix the problem

cesar2064 commented 8 years ago

yes, so weird, when I use npm update --save, updates trailpack-footprints to v 1.0.0. In the other hand, With the "trailpack-footprints": "^1.0.0-beta-7" works now, thanks :D

jaumard commented 8 years ago

Ok cool ! :) closing this then ^^

jaumard commented 8 years ago

I release a v1.0.1 like this npm update will be ok normally