Closed bwgjoseph closed 3 years ago
@bwgjoseph no this is not a feature that Ottoman supports
In that case, would there be any issue if I define multiple of the same model?
// in file1.ts
ottoman.model('Airline', airlineSchema);
// in file2.ts
ottoman.model('Airline', airlineSchema);
Good Question, I see where you are coming from you want to make sure that ottoman internally is not keeping track of all models. Let me double check that for you
Now, I am curious to know why would one do that. Unless its a demo that Eric and I were working. As a good practice will you as a developer not define all the models and Schemas under your repository once and use them everywhere ? Why would someone do it in a prod application ?
I got answer from the team
Ottoman will track all models by Unique name, for this case only 1 model will be track 'Airline', models will be register by connection into a dictionary data structure.
Yup, you're right that one shouldn't do such thing (defining Model multiple times). However, it doesn't stop developer from writing such code, hence, it is important to know how the library (in this case ottoman) handles this kind of scenario so that developer are aware of it, and handle it if necessary.
So in this case, if I were to declare twice, mongoose
will throw me an error so that I know something is wrong, so I can verify my code to ensure that I'm not accidentally calling it twice, or write my code such that it will use existing or create new
.
Hence, if ottoman
doesn't throw anything if called twice (creating model), and it is something that the library handles it internally, then I think is fine but maybe it could be mentioned in the docs? I don't think I saw anything related to this.
Also, In the docs, it mentioned
The model() function makes a copy of the schema. Make sure that you've added everything you want to the schema, including hooks, before calling model()!
So if I were to do something like
// this kind of thing shouldn't happen
// in file1.ts
// add some hooks
ottoman.model('Airline', airlineSchema);
// in file2.ts
// add some hooks
ottoman.model('Airline', airlineSchema);
Based on the description, I'm assuming that whatever hooks I added in file2.ts
will not take effect. But I wouldn't know, until I encounter some problem (hooks not running), then trace through the codebase to figure out what's wrong.
Hope I have made some sense out of it, assuming I get how Model
works correctly
Absolutely, you have always made more sense Joseph :)
Will see that documentation gets updated accordingly, Thanks for the feedback.
Closing this in favor of #38
For
mongoose
, I am able to lookup existing model and return otherwise, create the model as Models created cannot be overwritten inmongoose
.