Where to define custom schema using RailwayJS

Go To StackoverFlow.com

1

The docs simply say "or define custom schema (non-juggling), for example, mongoose. Please note, in case of custom schema all jugglingdb features of course will be disabled."

However..

Where exactly should this schema be created?

2012-04-05 16:38
by Alex


1

I believe you can still create it inside db/schema.js. For example:

customSchema(function () {

   var mongoose = require('mongoose');
   mongoose.connect('mongodb://localhost/test');

   var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;

   var BlogPost = new Schema({
        author    : ObjectId
      , title     : String
      , body      : String
      , date      : Date
   });

  var Post = mongoose.model('BlogPost', BlogPost);
  Post.modelName = 'BlogPost'; // this is for some features inside railway (helpers, etc)

  module.exports['BlogPost'] = Post;

});
2012-04-11 02:04
by jesal
great, thanks! how would I use the exported Post in my controller - Alex 2012-04-13 10:55
Ha! Good question. I am stuck at the same problem. I defined a User schema but didn't have any luck accessing it from the controller (same as this person - https://github.com/bnoguchi/mongoose-auth/issues/71). I spent countless hours searching online but didn't get anywhere.

I love where RailwayJS/JugglingDB is going but it still has a long way to becoming a mature framework with proper documentation. I've since given up on it and going back to RoR. Anyway I hope you have better luck! Let me know if you find a solution - jesal 2012-04-13 23:36

I use this approach in my app, and the models are just available in the controller without me doing anything special. In the above example, you should be able to just do "new BlogPost() - Kevin Dente 2012-05-14 19:34


1

I was having similar issues with getting the customSchema example above to work. I think this little tip might be a huge time-saver for others trying to use railwayjs customschemas.

After I put a console.log("custom schema initialized"); within the customSchema(function() {...}) block of code, I noticed that the console.log was not firing... which obviously meant that the customSchema block in my db/schema.js file wasn't even getting called.

After much fiddling around I realized that within your config/database.json file you must have the driver set to "memory" (for whichever environment you want the customSchema to run). If you have it set to driver: "mongoose" for example, railway will not run the customSchema block of code.

So in summary, if you want to run customSchema, make sure your config/database.json file looks something like this:

{
"production": 
  {
    "driver": "memory",
  },
"development":
  {  
    "driver": "memory"
  },
"test":
  { 
    "driver": "memory"
  }
}

your customSchema WILL NOT work if you have something like this:

{
"production": 
  {
    "driver": "mongoose",
    "url": "mongodb://<user>:<pass>@localhost:<port>/<database>"
  },
"development":
  {  
    "driver": "mongoose",
    "url": "mongodb://<user>:<pass>@localhost:<port>/<database>"
  },
"test":
  { 
    "driver": "memory"
  }
}

at least as of this writing... railwayjs is still under development so I'm sure things might change. I'm using railwaysjs version 0.2.17-pre4

** one more caveat - if you remove 'jugglingdb' - which is the ORM that is packaged with railway - from your node_modules folder then customSchema will not be invoked. I believe this is because 'jugglingdb' is tightly integrated into the railwaysjs framework. The rationale, I guess, is that the author of the framework was trying to mimic ruby-on-rails - which we know is tightly integrated with activeRecord.

So even if you are not using jugglingdb and are running your own custom schema's (say through mongoose or some other ORM), don't remove jugglingdb.

I've logged a bug for this: https://github.com/1602/express-on-railway/issues/212

2012-08-05 07:54
by Calvin Alvin
Ads