mean-expert-official / loopback-sdk-builder

Tool for auto-generating Software Development Kits (SDKs) for LoopBack
Other
399 stars 175 forks source link

Missing imports for many to many relations #87

Closed l0stpenguin closed 8 years ago

l0stpenguin commented 8 years ago

I have a Company model which has a many to many relation with EboardUser through CompanyUser.

Company.json "relations": { "users": { "type": "hasMany", "model": "EboardUser", "foreignKey": "idCompany", "through": "CompanyUser" } }

When i generated the sdk, i got the following method in the Company model:

public linkUsers(id: any, fk: any, data: CompanyUser = undefined) {
    let method: string = "PUT";
    let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
    "/Companies/:id/users/rel/:fk";
    let routeParams: any = {
      id: id,
      fk: fk
    };
    let postBody: any = {
      data: data
    };
    let urlParams: any = {};
    let result = this.request(method, url, routeParams, urlParams, postBody);
    return result;
  }

But the import is missing CompanyUser (error TS2304: Cannot find name 'CompanyUser'.)

import {
  Company,
  EboardUser,
  MeetingType,
  CompanyDoc,
  LoopBackFilter
} from '../../models/index';

Same error occurs in EboardUser file ( error TS2304: Cannot find name 'CompanyUser'):

public linkCompanies(id: any, fk: any, data: CompanyUser = undefined) {
    let method: string = "PUT";
    let url: string = LoopBackConfig.getPath() + "/" + LoopBackConfig.getApiVersion() +
    "/EboardUsers/:id/companies/rel/:fk";
    let routeParams: any = {
      id: id,
      fk: fk
    };
    let postBody: any = {
      data: data
    };
    let urlParams: any = {};
    let result = this.request(method, url, routeParams, urlParams, postBody);
    return result;
  }
jonathan-casarrubias commented 8 years ago

@mevinDhun thanks for reporting, I have 2 questions

1.- Is the CompanyUser private model?

2.- If it is not private, it is at least even generated within sdk/models and sdk/services

l0stpenguin commented 8 years ago

@jonathan-casarrubias no it is not private. It has been generated and is available along with the other models.

jonathan-casarrubias commented 8 years ago

@mevinDhun which version of the SDK builder are you using? I can not replicate this issue, but I recently refactored the method that build the services method properties and there is a validation that if a model could not be imported -like your case- then it should not be assigned as method param type, in which you should see something like:

public linkCompanies(id: any, fk: any, data: any = undefined) {
}

This is a validation when no target class was found, I fixed that in version 8 or 8.1 so its a new fix, please check your version and see if the issue persists.

Cheers jon

l0stpenguin commented 8 years ago

I am using version 2.0.0-rc.8.1. I have created a sample app to replicate this issue: https://github.com/mevinDhun/loopbacksdk-many-test

jonathan-casarrubias commented 8 years ago

gr8, let me verify the issue

jonathan-casarrubias commented 8 years ago

@mevinDhun you may not know but you actually found 2 different issues deep in the core of the builder.

One of the issues was really specific to models that contains the word "any" like Comp{any}, etc. So it was not really because of the type of relation but the name of the models.

But there was actually another issue regarding only to hasManyThrough relations, since loopback does not list the through as related model, it was not being imported which is kind of the bug described in this issue. So I made some tweaks to also add through models to the list.

This issue is fixed and will be published under version RC9

Cheers! Jon

ostapch commented 8 years ago

I had the same issue. So waiting for rc9. Thank you for your work

sprypradeep commented 8 years ago

There is another issue snicked in due to this. I have a model school, and two sguides (school has many) and guides (school has many thru sguides), now with 9.1 I get Sguide model imported but the model definition file is skipped (as its private). guide and school are not private, so those models get generated.

Another condition in the fold.


    "guides": {
      "type": "hasMany",
      "model": "guide",
      "foreignKey": "schoolId",
      "through": "sguide"
    },
    "sguides": {
      "type": "hasMany",
      "model": "sguide",
      "foreignKey": ""
    },
import {
...
  Sguide,
...
}
.....
  public updateByIdSguides(id: any, fk: any, data: Sguide = undefined) {
jonathan-casarrubias commented 8 years ago

@sprypradeep thanks for reporting, I will verify... now that some models are coming from a different place I believe I forgot to verify if these were also private, I will verify and send a fix ASAP.

@dooddy you are welcome, I'm always happy to see more people involved in this project.

Cheers Jon

jonathan-casarrubias commented 8 years ago

A new condition has been added so Through models that are private won't be loaded. This will be released under version 2.0.0-stable