Where to define custom schema using RailwayJS

Problem

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?

Problem courtesy of: Alex

Solution

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;

});
Solution courtesy of: jesal

Discussion

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

Discussion courtesy of: Calvin Alvin

This recipe can be found in it's original form on Stack Over Flow.