-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CLI: Import LB3 model(s) into LB4 project [EXPERIMENTAL] #3688
Conversation
477d523
to
483c18e
Compare
5dadaee
to
d4def83
Compare
Based on feedback from @raymondfeng and the fact that many important parts of model definitions are contributed by runtime (e.g. PK type and Screenshot of the new version at work, using our examples/lb3-application/lb3app:
Here is the content of generated import {Entity, model, property} from '@loopback/repository';
@model({
settings: {
strict: false,
forceId: false,
base: 'PersistedModel',
replaceOnPUT: true,
validateUpsert: true,
idInjection: true
}
})
export class CoffeeShop extends Entity {
@property({
type: 'number',
id: 1,
generated: true,
updateOnly: false,
})
id?: number;
@property({
type: 'string',
required: true,
})
name: string;
@property({
type: 'string',
})
city?: string;
// Define well-known properties here
// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;
constructor(data?: Partial<CoffeeShop>) {
super(data);
}
}
export interface CoffeeShopRelations {
// describe navigational properties here
}
export type CoffeeShopWithRelations = CoffeeShop & CoffeeShopRelations; @raymondfeng what do you think, is this closer to the user experience you are envisioning for @strongloop/loopback-next WDYT? |
19ddd92
to
d231ac9
Compare
d231ac9
to
41f400b
Compare
@bajtos Please investigate CI failures. |
d0ac234
to
f84e454
Compare
@raymondfeng thank you for the initial approval. The pull request was not done yet. Today, I added many new tests and fixed handling of edge cases. Now I consider the pull request ready for final review & landing. Does it still LGTY? |
f84e454
to
42ec9ed
Compare
}; | ||
|
||
// List of built-in LB models to exclude from the prompt | ||
const EXCLUDED_MODEL_NAMES = [ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am a bit lost here. In the docs, it says
you can modify your LB3 model to inherit from
Model
,
PersistedModel
orKeyValueModel
, then import the model and finally update
the generated model
if a model extends from the list, can it be imported or not?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a model extends from the list, can it be imported or not?
TL;DR: These two aspects are orthogonal. If a model extends from Model
, PersistedModel
or KeyValueModel
(all of them are in the list), then it can be imported. If it inherits from a different model class, then it cannot be imported yet.
We have two list of model names:
-
List of model names to hide from the CLI prompt when asking which app models to import. This list is
EXCLUDED_MODEL_NAMES
plus any model name starting withAnonymousModel_
.The purpose of this filter is to hide models that cannot be imported (e.g.
Model
andPersistedModel
are built-in in both LB3 and LB4) or which does not make sense to import (e.g.Email
).The filter does not take into account model inheritance.
I suppose I could improve the filter to take into account model inheritance. The difficult part is how to explain the user why some models are included in the list and some are not. I would prefer to invest our time into implementing import of models inheriting from other app models instead.
-
List of base models that we know how to handle at LB4 side, see https://github.com/strongloop/loopback-next/blob/42ec9ed2b972fec9ae25b974b65c268a6b2ed34b/packages/cli/generators/import-lb3-model/migrate-model.js#L120-L124
I created this list by picking built-in LB3 models that have their direct counter-part in LB4.
I'll be opening a new GH issue to implement support for importing models that are inheriting from a model that's not built into LB4. There are two major use cases - import from an LB3 built-in model like User
and import from an application-specific model. There are few difficult aspects to figure out, that's why I am leaving this feature out of scope of the initial version.
- How to know where to import the base model from (what path to use in the
import
statement)? - How to ensure that the base LB3 model has been either already imported to the LB4 app or is part of the import being in progress?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mostly LGTM 👏, I just have one question
<tr> | ||
<td><code>lb4 import-lb3-model</code></td> | ||
<td>Import a LoopBack 3 model to a LoopBack 4 application</td> | ||
<td><a href="Importing-LB3-models.html">Model generator</a></td> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Importer for LoopBack 3 models
42ec9ed
to
f67b98c
Compare
@agnes512 @raymondfeng @hacksparrow thank you for the feedback, I pushed a new commit to address your comments. Does the patch LGTY now? |
One more commit 644d33b - I reworked formatting of "Known limitations" in the docs and added links to follow-up GitHub issues. |
644d33b
to
19df37e
Compare
See #2480
This pull request is adding a new CLI command
lb4 import-lb3-models
:require()
, the user is presented with a list of models to import.User
andAccessToken
are included in that list.src/models/{model-name}.ts
file is created.Known limitations (features intentionally left out of this initial pull request):
User
) cannot be imported yet. Spike: how to import LB3 models extending a custom base model #3813ObjectID
type. LB3 model import: handle ObjectID type for MongoDB #3814/cc @strongloop/loopback-next
Checklist
👉 Read and sign the CLA (Contributor License Agreement) 👈
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated👉 Check out how to submit a PR 👈