Javascript function that can lookup and return an instance or create a new one
<p>I'm very new to JavaScript so I'm sure the title is pretty horrible.</p>

<p>As an exercise I'm trying to design an ORM for node.js like Mongoose.  I'm hung up on how they're doing things:</p>

<pre><code>// myThing manages/delegates everything for the ORM.
// db connection information, defined models, etc.
// same as mongoose = new Mongoose()
var myThing = function(args) {
  this.connections = [];
  this.models = {};
  // etc
}
// define or retrieve a model
myThing.prototype.model = function(modelName, schema) {
  //psuedocode
  // real code checks if schema is a valid object
  if (!schema) {  // if we didnt spec a schema, retrieve the model
    return this.models[modelName]; 
  } else {
    // otherwise store the schema and return it.
    this.models[modelName] = new Model(schema);
    return this.models[modelName];
  }
}

var Schema = function(name,specs) { 
  this.modelName = name; 
  this.specs = specs;
}

var aSchema = new Schema(
  { field: 'type' }
)


var Model = function(schema) {
    this.schema = schema;
}


var thing = new myThing();

thing.model('aModel', aSchema);

// im obviously going to get back an object/map/hash
// with no constructor.
var aModel = thing.model('aModel');

// again, no constructor
var instance = new aModel();

instance.field = 'test';
</code></pre>

<p>Perhaps I need a Model function/class that is stores in myThing and returned instead of the schema?</p>
