Skip to content

Add support for custom API Urls for exposed models #236

Closed

Description

@ariskemper

Nice feature would be possibility to rewrite default urls of main and related models.

Example:
for main models like

/api/accessTokens to /api/access-tokens

or for related models like

/api/users/{id}/bankAccount to /api/users/{id}/bank-account

There should be setting like url for a model in models.json and also as a paramater options when creating or extending a model. There is option in models.json config called plurals, but would be good to have separate settings to set custom url for model/related models and endpoints ( custom routing ).

Activity

  1. pennycoders commented on Apr 15, 2014

    @pennycoders
    Contributor

    Actually, you can already do that, i am pretty sure, it's just not documented too much Change the plurals of the models and you're good, or use the js api, as it follows:

    loopback.remoteMethod(
      Location.nearby,
      {
        description: 'Find nearby locations around the geo point',
        accepts: [
          {arg: 'here', type: 'GeoPoint', required: true, description: 'geo location (lat & lng)'},
          {arg: 'page', type: 'Number', description: 'number of pages (page size=10)'},
          {arg: 'max', type: 'Number', description: 'max distance in miles'}
        ],
        returns: {arg: 'locations', root: true},
        http: {verb: 'POST', path: '/nearby'}
      }
    );

    Please check this url for more details.

    The reason, i presume for not having this feature implemented is because in any real world scenario, you need some sort of consistency, and this is actually the best way of having something that makes sense /products/id/orders makes a lot of sense to me, but anyway, maybe i am wrong. I guess you could alter/override the http property of the desired method, but i would not see that as the best way to obtain consistency.

  2. ariskemper commented on Apr 16, 2014

    @ariskemper
    MemberAuthor

    Plurals for relations doesn't work, only for main model configuration in models.json. I would like to use hyphens instead camel case.
    Ritch said that base urls will be probably supported in version 2.0.

  3. raymondfeng commented on Apr 18, 2014

    @raymondfeng
    Member

    The relation name can be customized as follows:

    1. In models.json, the relation name is the key
     "user": {
        "dataSource": "db",
        "public": true,
        "options": {
          "base": "User",
          "relations": {
            "accessTokens": {
              "model": "accessToken",
              "type": "hasMany",
              "foreignKey": "userId"
            }
          }
        }
      },
    1. in code
    Order.belongsTo(Customer, {as: ‘customer’, foreignKey: ‘customerId’);

    The 'as' property denotes the relation name.

    Please note that hyphen might cause problems for JS method names.

    Check out:
    http://strongloop.com/strongblog/defining-and-mapping-data-relations-with-loopback-connected-models/

  4. fabien commented on Aug 23, 2014

    @fabien
    Contributor
  5. superkhau commented on Feb 26, 2015

    @superkhau
    Contributor

    @raymondfeng @fabien Safe to close?

  6. superkhau commented on Feb 4, 2016

    @superkhau
    Contributor

    I am closing this since the feature is already there. @ariskemper Please open a new issue if you are still running into problems.

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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions