I have a one file which has details of the database named xyz.js and another file which created the database using that file in mongodb and also has various functions like adding to database etc.The name of this file is create.js. Now I want to convert create.js code using mongodb to waterline.
me_aj, there is no possible short and simple answer for what you want. Nor there is any tool that will do what you need. Here are some pointers for what you'll need to do.
1. Learn about using waterline
It may sound obvious but you should start by getting acquainted with waterline and the best places are the waterline project and the Waterline Docs.
By then you'll know waterline is an adapter based ORM and that you'll have to decide which adapter to use. If you are using mongodb, you are probably looking at sails-mongo.
2. Config waterline to connect to the desired data store
Waterline, contrary to some other ORMs, has to be initialised before you can start using it. If you are using it outside sails you should look at these examples on how to set up a simple app. You'll also need to configure it by following the specific adapter settings. If you are connecting to mongodb check the instructions on the sails-mongo project.
3. CRUD operations
By now you've setup waterline to initialise and connect to your chosen data store, now it's time to do things with it. Similar to many other ORMs. waterline uses methods such as find, update, create and destroy to perform CRUD operations, to learn more about using these check the query methods documentation page.
This should be enough to point you in the right direction.
Related
I have some node.js code that connects to CouchDB and now I'm exploring other NoSQL databases (DynamoDB, MongoDB, etc).
Is there a DB-agnostic module that would allow me to switch NoSQL databases without much trouble?
For sure you will need to change your code to adapt to a new database.
Anyways, there are a few options that allow you to switch from one of other database easily.
If you consider building from scratch, Loopback has a juggler that allows to setup each model to connect to a different database. If you want to include it as a module in your app. probably you are looking something like Waterline.
I have only used Loopback, it's great.
I haven't used Waterline.
I'm looking for a way to run custom query using Waterline in Sails.js.
For example I want to create a view, e.g.: CREATE VIEW ... it doesn't make sense to run it via some model, like User.query().
Is there a way to run native query without referencing some specific model?
Waterline doesn't provide direct access to the underlying adapters; you have to go through a model. If it really bothers you philosophically to run a generic query through a specific model, you can always install and use the database driver directly (e.g. npm install pg or npm install node-mysql). But there are advantages to going through a model. For one thing, Waterline handles all the connection overhead for you. And if you change the connection for the model, then the generic query will automatically be using the new connection without you having to hunt it down and switch it yourself.
Hey everyone I am working on nodeJS app. I searched some modules to manage my database (orientdb).
My question is: Why we use any ORM or ODM (or why is it recommenced), because there is a module which can provide many functions to manage DB.
I am still confused what should I use orientorm (https://github.com/mav-im/orientorm) or oriento (https://github.com/codemix/oriento)
Thank in advance..
Depending on the goal and depending on the ORM, ORMs have the advantage of adding support for:
schemas / models / collections: this makes it easier to create all classes/properties and, in some cases, migrations;
validations: make it easier to verify what gets saved in the DB.
All OrientDB ORM's I've seen for node.js expose Oriento, so that makes it easy to access the underlying oriento methods for doing more complex stuff.
Having said all this I recommend you try the waterline ORM with waterline-orientdb adapter. Waterline is an adapter based ORM with support for multiple databases (including support for associations between databases). Waterline-orientdb is the adapter for OrientDB which is based on Oriento. If at any point you need to use Oriento you can call .getDB() to access Oriento's instance.
Oriento is much more mature and supported. I suggest you to go with it.
What is the optimal way to perform CRUD operations when using Node.js with MongoDB. Can I reuse the same queries that work on mongo shell? What advantages does a ODM like mongoose provide? Any other ODMs which fit into the mean.io stack?
vmr.
Well, I guess it depends of what you want to do.
Lets take what Mongoose says in their website:
Mongoose provides a straight-forward, schema-based solution to modeling your application data and includes built-in type casting, validation, query building, business logic hooks and more, out of the box.
Resuming what I understood of that it helps you to model your database and helps you to mantain your logic organized using model like in an MVC. It's a very mature ODM and very recomended for using with MVC.
In my personal experience I started using Monk, which did the trick for a while, but the I started to need to use aggregate and other stuff that apparently Monk can't handle. And I don't wanted to tie my system to an model because is a very mutable project, so I started using Mongoskin which is, at least for now, perfect for me because I can use pratically the same query that I use at Robomongo (Which is like a Navicat, PgAdmin, PhpMyAdmin but for MongoDB) in my ExpressJs code.
Mongoose is saving your time by mapping the JavaScript Data objects to the MongoDB database.
MongoDB and NodeJS Integration
I'm reviewing various frameworks for node.js and I'm just now testing Sails.js by writing a small CRUD app. I would like to know if there is an elegant way to use a different ORM other than the one sails includes.
I haven't seen anything in the documentation in Sails.js for using a different ORM. Are there any "integrated" non-orm specific features that I may be missing by attempting to use a different ORM?
Waterline should be considered a core part of sails. There isn't any easy way to replace it with a different ORM. In the google group for Sails.js, Mike McNeil (the creator of sails) acknowledges that Sails is not currently built to support swapping in a different ORM[1].
"[...]I'm totally open towards efforts to extend Sails to support other ORMs[...]"
[1] https://groups.google.com/d/msg/sailsjs/jmR36bD-Zys/-F0ZFy1Q1IoJ
In the upcoming v0.10 release of Sails the hooks and generators have been broken out and can easily be replaced to support whichever ORM you would like to use.
The community will be able to write ORM-loaders to replace the Waterline ORM loader and a generator to generate models in the correct format for the ORM of your choice. Custom blueprint controllers will also need to be written to talk to the ORM.
It's a lot of work but something that should start to happen once a stable v0.10 is released.
I wrote up the steps to consistently disable waterline throughout sails (v0.10, v0.9.8) here: https://stackoverflow.com/a/21612024/3263412
Without replacing the orm hook you definitely loose a big chunk of Sails' features, at least until the eco system particlebanana describes is developed:
pubsub
blueprints and rest routes (but could be worked around easily)
model scaffolding
custom adapters
On the other hand i figure it would not be too incredible hard to write up an orm hook for node-orm2 or sequelize plus the facades the other Sails hooks would probably need to work properly.