1602 / compound

MVC framework. Built on Node.JS. Works on server and browser.
http://compoundjs.com
1.6k stars 183 forks source link

Use Raw Mongoose Models From The Controller #167

Closed bshorrosh closed 11 years ago

bshorrosh commented 12 years ago

In the schema.js:

customSchema(function() { var mongoose = require('mongoose') , Schema = mongoose.Schema;

mongoose.connect('mongodb://localhost:27017/myDatabase');

var Plasmid = new Schema({ title: {type:String}, sequence: {type: String}, content: {type: String}, position: {type: String}, createdAt: {type:Date} });

module.exports["Plasmid"] = mongoose.model("Plasmid", Plasmid); });

Can't access the Plasmid model from the plasmids_controller.

Others have the same issue: http://stackoverflow.com/questions/10032524/where-to-define-custom-schema-using-railwayjs

It would be very helpful if someone shares a working example using the raw Mongoose Models with the controllers in railway. While JugglingDB is useful, we should be able to access the database via nodjs mongo driver.

Your help is very much appreciated.

Regards

jameswyse commented 12 years ago

This is a little old, but I hate seeing unanswered questions when searching for stuff. Hopefully this helps someone in need :)

This is how I've set up my schemas, your model should be available in the controllers as Plasmid and Schema as Plasmid.schema

Though I've had little success extending the schema from the model files, so all my validation and getters/setters are stuck in schema.js, it's getting huge.

_Model:_ schema.js

customSchema(function() {
    var mongoose = require('mongoose'),
        Schema   = mongoose.Schema,
        db       = mongoose.connect('mongodb://localhost:27017/myDatabase');

    var PlasmidSchema = new Schema({
        title:     { type: String },
        sequence:  { type: String },
        content:   { type: String },
        position:  { type: String },
        createdAt: { type: Date   }
    });

    var Plasmid                 = mongoose.model('Plasmid', PlasmidSchema);
    Plasmid.modelName           = 'Plasmid';
    module.exports["Plasmid"]   = Plasmid;
});

_Controller:_ _plasmidscontroller.js

load('application');

action(function index() {
    Plasmid.find().run(function (err, plasmids) {
        render({
            title: 'Listing all Plasmids',
            plasmids: plasmids
        });
    });
});

_View:_ plasmids/index.jade

div
   - plasmids.forEach(function(plasmid) {
   div= plasmid.title
   div= plasmid.sequence
   div= plasmid.content
   - }
bshorrosh commented 12 years ago

Thanks; you're awesome. I'll give it a try. Regards