Node.js combining Mongoose and Sails Waterline - javascript

I'm working on an Open Source microservice based project in Node.js.
Description of the problem
Some microservices are so thin and only interact with MongoDB, so I haven't used Sails there and preferred using Mongoose directly. Thus, I've implemented mongoose schemas for each of the objects I need to persist in mongo.
I'm also going to have Sails.js in some other microservices, Therefore I'll have to implement Waterline-type schemas for the objects I want to persist & query there.
The problem is the duplication of schemas, which I find useless.
Solutions I thought of
I wish to use one type of schemas across all the services.
There are 3 options now:
A magic method to connect between the schemas (which I'm not aware of), and have both worlds work simultaneously (least probable option).
To use Waterline across all the microservices, whether I use Sails.js or not, therefore have 1 schema type in the system.
To force Sails.js to use mongoose adapter instead of Waterline, and then implement some CRUD Data Access Layer (DAL) which uses Mongoose internally, and use it everywhere to access the DB.
Therefore the Mongoose schemas will be across all the system. But then I give up on the Waterline encapsulation capabilities, and if in the future I'd like to change DB, I'll be in trouble.
I could also compromise and duplicate schemas in my app, but that's the last resort as I believe there has to be some solution to this problem.

Whoever consider Waterline vs Mongoose... right to this date, choose Mongoose.
Waterline made us so much head-ache, while mongoose is so smooth and straightforward.

I could think of the following to go fully Sails:
put the common parts, like the Sails models, in a Sails project and push it to a (private) npm package
use https://github.com/tjwebb/marlinspike or other sails hooks to import them in every instance you need it.
I have not tried this, but preparing for similar steps in the future.
Also, I saw this issue: https://github.com/tjwebb/marlinspike/issues/4

Related

Ember.js dynamic model

I'm looking into building an application with Ember.js but I'm not sure about whether its model support is appropriate for my needs. I'm working on a database front-end application which connects to a central server; the main problem here is that the database definition might change (i.e. removing or adding columns).
Editing the code manually to accomodate all changes to the database is something I'd like to avoid at all cost. Therefore, I'd like to know if Ember.js provides a way to generate dynamic models. I've come across the defineProperty method but I haven't been able to figure out how it works, neither have I found anything about it in the Ember.js docs. Besides, it seems to be necessary to run it once for each object; is there a way to run it just once?
A possible approach may be to automate the generation of your client side schema from your database schema.
A similar solution has been demonstrated here (though the implementation is Rails specific) : http://techblog.fundinggates.com/blog/2013/03/automatically-generate-ember-models-from-rails-serializers/

Is there a good MSSQL / SQL Server adapter for sails.js?

I want to preface this with saying that I really like sails.js for a simple MVC framework, but feel that it lacks in its documentation and api reference.
I searched all over to try and find any information on a good mssql (SQL server) adapter for Sails.js, but cannot find one. I am hoping someone has come across one, and could recommend it. I also looked into building a custom adapter, but found the documentation to not be helpful. Any help on this topic would be greatly appreciated. Thank you.
We don't currently have a MS SQL Server adapter but it's something I'd love to add. There is a basic definition of the various interfaces and how adapters are created at: API Adapter Interface.
If anyone would like to tackle this you can use the Sails MySQL Adapter or the Sails PostgreSQL Adapter as examples.
I don't have access to a MS SQL Server to build one out but there is an Integration Test library I use to build out adapters that support the CRUD interface: Waterline Adapter Tests.
I have recently started building a sails adapter for MSSQL based on the node-sqlserver module.
It isn't finished yet but I have finished the main parts of the adapter and it still needs testing, but there is enough there to get going.
It can be found at https://github.com/swelham/sails-mssql
There is now: https://github.com/cnect/sails-sqlserver
Complete with sails 0.10 associations support, and fully unit tested against the Waterline spec:
Technically since Sails is base on express.js you could use custom express middleware to specify the use of your an adapter that has already been created.
The first one is the top rated adapter
checkout https://nodejsmodules.org/tags/mysql
Otherwise you can just use the waterline ORM and the MySQL adapter that is provided for sails.js
https://github.com/balderdashy/sails-mysql
For documentation on how to use waterline models checkout the 0.9x docuemntation
http://sailsjs.org/#!documentation/models
sails-mssql is working fine for me on 0.10.x, here's a gist of how to configure it, using sails-mssql 0.10.x
https://gist.github.com/hybrisCole/868f979d8d129247a2da
my package.json has this:
"sails-mssql": "git://github.com/jaredfromsubway/sails-mssql.git#master",
Sails-sql is currently the latest adapter for MySQL/MSSQL (eventually PostgreSQL as well). It's currently safe, fine-tuned, and upgraded for use in production with MySQL and MSSQL. Most developments are primarily pending for PostgreSQL at the moment.
Another current option is https://www.npmjs.com/package/sails-mssql (https://github.com/intel/sails-mssql).

Complex database queries in Sails.js

How can I do complex database queries such as joins using Sails.js?
I tried the generate method using the command line, it's great and I connected it to my MySQL database and it works great, but now what I need to understand is how I can do complex SELECT statements. I can't find it in the documentation.
For complicated queries refer to this answer by particlebanana, one of Sails.js core contributors.
As an alternative to perform joins, you can use the associations branch.

Recommendations regarding sharing model code in node.js and browser

I am using Sequelize as my server side ORM. Is there a recommended approach towards sharing the Model code (especially the validations) with my client application ?
Please don't recommend solutions which require me to move to a NoSQL database. Currently that is not an option for me. While I really Sequelize as an ORM, I am willing to move onto some other model implementation if it is beneficial.
Persistencejs, but it doesn't appear to be as well maintained as Sequelize:
https://github.com/zefhemel/persistencejs
At present there does not seem to be an end to end solution ie. a model library which you could just require in either browser or server and define your models - where methods like save, update would be polymorphic requiring the developer to just extend standard model classes and use them on either client or server.
However, for people looking for a similar solution - I recommend using JSON schema validators, which are quite hassle free and provide a simple means to share your validation logic between client and server.

Questions about node.js

I've been trying to do some research on node.js and Mongodb and have a few things that need clarifying.
I'm trying to query Mongodb from the web and want to use JavaScript because I'm familiar with the language, plus it makes sense because that's what Mongodb uses in the command line interface.
It seems that I'll have to use node.js to query mongo from the web, but what I can't find info on is a way to tell my node.js server what I want to query, from the web.
If someone can point me in the proper direction ( reading material, how to, examples) that would be awesome.
Here's some articles I used...
http://css.dzone.com/articles/nodejs-and-mongodb-beginner%E2%80%99s-0
http://srirangan.net/2012-02-node-js-and-mongodb-getting-started-with-mongojs
http://mattkopala.com/blog/2012/02/12/getting-started-with-nodejs/
I found Mongoose to be the best Node.js MongoDB library (the creator of Mongoose actually now works for 10gen, the company behind MongoDB).
Besides a lot of good examples on the official Mongoose website, you can find lots of other resources:
http://pixelhandler.com/blog/2012/02/09/develop-a-restful-api-using-node-js-with-express-and-mongoose/
http://backbonetutorials.com/nodejs-restify-mongodb-mongoose/
http://dailyjs.com/web-app.html (how to build an app from the start, multiple lessons)
One of the most important things I like about Node.js and MongoDB (Mongoose in my case) is that you are writing the query commands sometimes exactly like you write them in the MongoDB shell.
P.S. Last but not least, checkout the videos from 10gen related to Node.js and MongoDB, there are lots of them (and people using Node with Mongo in production): http://www.10gen.com/presentations#programming_lang__javascript_nodejs

Categories