Mongoose model schema organization inside actionhero?

271 views
Skip to first unread message

Alex Parker

unread,
Sep 2, 2014, 8:39:23 AM9/2/14
to action...@googlegroups.com

So I have my actionhero actions structured where actions/users.js has multiple actions defined on the exports object like exports.index, exports.show, exports.create, exports.update, exports.destroy to define the RESTful action endpoints for the model. 

Then I have been messing around with creating a mongoose initializer, but can't find the right place to define my mongoose model schemas and where to instantiate them. This is partly an actionhero question, and part due to my newish-ness to organizing server-side javascript.

Any help here would be greatly appreciated. Recommended patterns, or advice or what has worked for you, or what you might do.

Giacomo Rebonato

unread,
Sep 2, 2014, 9:11:12 AM9/2/14
to action...@googlegroups.com
In attachment I show how I did it, and it works.. I don't know if a better way exists.
It's an ActionHero initializer for Mongoose schemas.
mongo.js

Brian Corrigan

unread,
Sep 2, 2014, 9:53:28 AM9/2/14
to Giacomo Rebonato, action...@googlegroups.com
Hey Alex - 

We usually end up creating a few additional top level folders, one for models, one called lib (because what project doesn't need a dumping ground for random stuff), and sometimes an assets folder.  Like you, we created an initializer to setup Mongoose and usually add a reference to it on the API object.  

At it's core action here is really a request handling framework.  It's nice because it abstracts out the transport into various servers.  It's very opinionated about databases, etc. and it expects you'll fill in the blanks for some of these other pieces from whatever you like in NPM.

I think you're on the right path!

Best,
Brian


---
Brian Corrigan
Managing Parter / MadGlory
Wb: http://madglory.com
Tw: @madglory
Ph: 518.867.1439


--
You received this message because you are subscribed to the Google Groups "actionHero.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to actionhero-j...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Alex Parker

unread,
Sep 2, 2014, 10:26:43 AM9/2/14
to action...@googlegroups.com, rebonato...@gmail.com, br...@madglory.com
Thanks for the reply. So you define your models in the models/ top level folder. How do you reference them or initialize/instantiate them? Giacomo has done it all in the initializer, which seems fine for him, but i'm looking for a more modular approach. 

Also, are all your models all available on the api namespace?

Thanks!

Brian Corrigan

unread,
Sep 2, 2014, 10:38:44 AM9/2/14
to Alex Parker, action...@googlegroups.com, rebonato...@gmail.com
Hey Alex - 

We just require them as needed.  So for example using the code here (http://mongoosejs.com/), we'd break it down like so:


// initializer
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost/test'); // models/cat.js
var mongoose = require('mongoose'); var Cat = mongoose.model('Cat', { name: String });
// Inside some action
var Cat = require('../models/cat.js'); var kitty = new Cat({ name: 'Zildjian' }); kitty.save(function (err) { if (err) // ... });


---
Brian Corrigan
Managing Parter / MadGlory
Wb: http://madglory.com
Tw: @madglory
Ph: 518.867.1439


Evan Tahler

unread,
Sep 2, 2014, 2:23:02 PM9/2/14
to Brian Corrigan, Alex Parker, action...@googlegroups.com, Giacomo Rebonato
I rarely use mongo, but you can see how I use sequelize to arrange a similar thing (in an AH plugin!).  Note that we expect a "models" and a "migrations" folder.  The models are loaded into `api.models.{}` and then I use them within the actions. 

Reply all
Reply to author
Forward
0 new messages