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

Browser Model #4292

Open
DylanPiercey opened this issue Jul 2, 2016 · 2 comments
Open

Browser Model #4292

DylanPiercey opened this issue Jul 2, 2016 · 2 comments

Comments

@DylanPiercey
Copy link

DylanPiercey commented Jul 2, 2016

Although 90% of "model.js" doesn't make sense in the browser I think implementing it with some basic features (like creating a schema from an object and storing the model name) would be handy to make the following code easier to be isomorphic.

var Test = mongoose.model('test', {
  name: { type: String },
  test: { type: String },
});

// Browser should have
Test.schema
Test.modelName

Currently I have to separate out that schema if I want to use it in the browser, also I have to explicitly pass around the modelName since schema's don't have names.

Thoughts?

@mkastner
Copy link

mkastner commented Jul 4, 2016

I've run into the same problem. Maybe this abstracted shared schema module could work for you (with CommonJS):

// schema-def.js
// abstracts the schema definition from the schema object creation

var schemaDef = {
    test: {
        name: { type: String },
        test: { type: String }
    }
}

module.exports = schemaDef;

Now you can require the abstracted schema definition from client and server side like this:

// client-models.js
// sharing same schema defintion with server

var schemaDef = require('schema-def.js');

var Test = mongoose.model('test', schemaDef.test);

// of course no plugins and other fancy server stuff

// server-models.js
// sharing same schema defintion with client

var schemaDef = require('schema-def.js');

var Test = mongoose.model('test', schemaDef.test);

// Since the schema definition is abstracted,
// on the server side you can add your plugins, indexes etc. 

My thoughts.

@vkarpov15
Copy link
Collaborator

I like this idea. Hasn't really been a high priority since getting documents and schemas to work in the browser was already quite an odyssey

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

3 participants