mgan59 / mongoose-fixture

A bootstrap library to import javascript objects into mongodb using a series of raw json files
25 stars 3 forks source link

Difference in schema between mongoose and mongoose fixture #24

Open tobiasoberrauch opened 10 years ago

tobiasoberrauch commented 10 years ago

Hi,

during develop my project I regnocnized a difference between the expected schemas for mongoose [1] and mongoose fixture [2]:

[1]

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports = mongoose.model('User', new Schema({
  name: String
}));

[2]

module.exports = function (mongoose) {
  return {
    name: 'User',
    schema: mongoose.Schema({
      name: String
    })
  };
};

My problem is that I can't find a schema that fits for both. What did I wrong?

Thanks for helping me :)

Cheers Tobias

mgan59 commented 10 years ago

@tobiasoberrauch sorry I missed this originally, are you still having issues and need help?

mgan59 commented 10 years ago

There is no prescribed way to register your schemas, it depends on your application/usage pattern. In order to have reusable schemas I recommend this method which plays nicely with mongoose-fixture and still allows for shared schemas for express-apps or other services.

//user.js

module.exports = function(mongoose, conn) {

    var Schema = mongoose.Schema;
    var UserSchema = new mongoose.Schema({
        name: {type: String, required: true}
    });

    // register model here, though I'd recommend against it see 
    // https://github.com/mgan59/mongoose-fixture/issues/25 
    mongoose.model('User', UserSchema);

    return {
        name: 'User',
        schema: UserSchema
    };
};

Let me know if this doesn't work and feel free to post more code if you need help.