loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Define models relations in JSON file or in JavaScript code

Open yagobski opened this issue 9 years ago • 15 comments

e.g. LoopBack 3 has models relations definition in JSON file or in JavaScript code We would like to define models relations in LoopBack 4 as well. Definition of relations need to auto generate the api services.

yagobski avatar Nov 02 '17 00:11 yagobski

@yagobski , do you think this spike cover what you're looking for? https://github.com/strongloop/loopback-next/issues/995 . If yes, is it ok to close this issue and continue the discussion over there? thanks.

dhmlau avatar Mar 14 '18 00:03 dhmlau

I feel it's too earlier for team to discuss the declarative relation file before story https://github.com/strongloop/loopback-next/issues/1372 finished, we can revisit it later this month to groom it.

jannyHou avatar Jun 06 '18 20:06 jannyHou

I think this feature proposal would be covered by https://github.com/strongloop/loopback-next/issues/1359

bajtos avatar Jun 25 '18 11:06 bajtos

We'd like to move to LB4, but can't move until we are able to have relationships specified in a JSON model. Any idea on what milestone this issue will be a part of? Thanks!

elv1s avatar Oct 24 '18 21:10 elv1s

@elv1s what kind of relations are you looking for? We already support hasMany and belongsTo as of 4.0 GA, the work on hasOne is in progress - see https://github.com/strongloop/loopback-next/pull/1879. The Epic https://github.com/strongloop/loopback-next/issues/1450 is keeping track of the remaining relations, could you please leave a comment there to mention the relations that are important to your project?

bajtos avatar Oct 25 '18 12:10 bajtos

@elv1s maybe I misunderstood you. Are you saying that you would like everything relation-related to be provided by LB4 runtime based on few lines of JSON config? I mean the controller for REST API, repositories for accessing relation data, etc.?

bajtos avatar Oct 25 '18 12:10 bajtos

@bajtos, yes your second comment is what I was interested in. Right now in LB3 we are driving the creation of the API with JSON models. Will we be able to use LB3 JSON models with relationships in LB4? The relationships hasMany, belongsTo and hasOne should cover our requirements.

elv1s avatar Oct 25 '18 22:10 elv1s

Cross-posting https://github.com/strongloop/loopback-next/pull/1194#issuecomment-433419070.

Can lb3 model relations be used in lb4 with https://github.com/strongloop/loopback-datasource-juggler?

Yes, I think that should work if you create the models using juggler's DataSource and/or ModelBuilder APIs and thus completely bypass @loopback/repository. You will loose the ability to describe models as the OpenAPI schema though, you will need to implement a helper to build OpenAPI schema for juggler models yourself. Possible leveraging loopback-swagger.

bajtos avatar Oct 26 '18 14:10 bajtos

@elv1s

Right now in LB3 we are driving the creation of the API with JSON models. Will we be able to use LB3 JSON models with relationships in LB4?

Hmm. I think our design should be flexible enough to allow you to do that, but I don't think we are going to support this use case out of the box, at least not in the next few months.

I have left a comment elsewhere explaining how to automatically wire repositories and controllers for models, the approach may work for you too. See https://github.com/strongloop/loopback-next/issues/1889#issuecomment-433384870

I think this discussion is somewhat related to Declarative Support #565

bajtos avatar Oct 26 '18 14:10 bajtos

LB4 is great but changing model declaration will be hard to migrate from LB3 to LB4 without rewriting the whole application. Do you have any migration plan to follow from LB3 to LB4.

yagobski avatar Nov 07 '18 17:11 yagobski

@yagobski

LB4 is great but changing model declaration will be hard to migrate from LB3 to LB4 without rewriting the whole application. Do you have any migration plan to follow from LB3 to LB4.

We are fully aware of this concern. We are discussing migration paths from LB 3.x in the issue https://github.com/strongloop/loopback-next/issues/1849 - feel free to join the discussion and/or upvote the issue.

bajtos avatar Nov 08 '18 13:11 bajtos

{
   "_id": ObjectId("5c8772e972fe042a505f663e"),
   "taxonomyTypeId": ObjectId("5b7ba50c5ee376051498b78a"),
   "name": "Name",
   "status": NumberInt(1),
   "created": ISODate("2019-03-12T08:50:49.257Z"),
   "modified": ISODate("2019-03-12T08:50:49.257Z"),
   "parentId": null 
}

How to create this ObjectId via loopback. I am using LB4. I am new for this

shashijangir avatar Mar 13 '19 12:03 shashijangir

@shashijangir sorry, your question is off-topic here. Please post it on StackOverflow, it's a place where are more people that can help you to get your question answered. Use the tag loopbackjs to make your question easier to find.

bajtos avatar Jun 24 '19 13:06 bajtos

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] avatar Jun 20 '20 17:06 stale[bot]

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.

stale[bot] avatar Jul 20 '20 18:07 stale[bot]