bwgjoseph / mongoose-vs-ottoman

feature comparison between mongoose and ottoman
0 stars 1 forks source link

get existing model #16

Closed bwgjoseph closed 3 years ago

bwgjoseph commented 3 years ago

For mongoose, I am able to lookup existing model and return otherwise, create the model as Models created cannot be overwritten in mongoose.

const airlineSchema = new mongoose.Schema(schema);
// grab existing or create new
const Airline = mongoose.models.Airline || mongoose.model<AirlineModel>('Airline', airlineSchema);
const airlineSchema = new ottoman.Schema(schema);
// Is there such thing in `ottoman`, to be able to grab existing or create new?
AV25242 commented 3 years ago

@bwgjoseph no this is not a feature that Ottoman supports

bwgjoseph commented 3 years ago

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);
AV25242 commented 3 years ago

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 ?

AV25242 commented 3 years ago

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.

bwgjoseph commented 3 years ago

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

AV25242 commented 3 years ago

Absolutely, you have always made more sense Joseph :)

Will see that documentation gets updated accordingly, Thanks for the feedback.

AV25242 commented 3 years ago

An update to the documentation has been requested here

bwgjoseph commented 3 years ago

Closing this in favor of #38