Skip to content
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

List of all models #1362

Closed
buschtoens opened this issue Feb 26, 2013 · 8 comments
Closed

List of all models #1362

buschtoens opened this issue Feb 26, 2013 · 8 comments

Comments

@buschtoens
Copy link

Working with mongoose.models is meh. What about mongoose.model() returning an array of all model names like:

var modelNames = mongoose.model() // ["Cars", "Drivers", "Pedestrians"]
  , models = modelNames.map(function(modelName) {
      return mongoose.model(modelName);
    });
@aheckmann
Copy link
Collaborator

That would break every app using mongoose. I find it odd that the application itself wouldn't know which models it created.

@buschtoens
Copy link
Author

Uhm, would it? mongoose.model() used to throw Error: Schema hasn't been registered for model "undefined" at you. Apps that would rely on this behaviour are IMHO pretty weird.

In my app I use some homebrew Model loader, which regards dependecies. For that it checks which models have yet been loaded (currently mongoose.models.hasOwnProperty(model)). I know that I could build my own array with all loaded Model names, but why not let Mongoose do the work?

@aheckmann
Copy link
Collaborator

Oh you're right, I misunderstood.

Models are cached in different ways, for example, if calling mongoose.model its cached at the mongoose level else if calling connection.model its cached at the connection level. Also, you can call x.model() and specify a different collection name as the original which returns the "same" model but subclassed to use the different collection.. so its not a "clean" thing. More of an implementation detail I'd rather not expose.

@buschtoens
Copy link
Author

Okay, so you would rather prefer a homebrew trycatch?

function hasModel(namespace, name) {
  try {
    namespace.model(name);
  } catch(e) {
    return false;
  }
  return true;
}

console.log(hasModel(mongoose, "Model"));
console.log(hasModel(connection, "Model"));

aheckmann added a commit that referenced this issue Feb 26, 2013
returns array of model names added.

closes #1362
@aheckmann
Copy link
Collaborator

You convinced me.

@buschtoens
Copy link
Author

Kudos!

@buschtoens
Copy link
Author

lib/index.js:328 will opts have a future use ({ includeConnections: true })?

@aheckmann
Copy link
Collaborator

oops, no.

On Tue, Feb 26, 2013 at 3:28 PM, Jan Buschtöns notifications@git.luolix.topwrote:

`lib/index.js:328'8a38663#L1R328will
opts have a future use ({ includeConnections: true })?


Reply to this email directly or view it on GitHubhttps://github.com//issues/1362#issuecomment-14146745
.

Aaron
@aaronheckmann https://twitter.com/#!/aaronheckmann

aheckmann added a commit that referenced this issue Mar 2, 2013
returns array of model names added.

closes #1362
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants