How to run custom query using Waterline in Sails.js? - node.js

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.

Related

DB-agnostic Node.js code

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.

Automatically create and update tables in Node.JS?

I have a prototype server connected to a PostgreSQL database. Because I'm iterating rapidly, I'm creating tables and changing the schemas all the time. I've looked for an npm package which allows me to create such tables and update existing schemas once the server starts, but the options I've found are tied to an ORM implementation, which I do not need.
TL;DR: Are there any npm packages to automatically create tables and update schemas according to a single configuration object provided to it?
A little late, but node-pg-migrate should do what you need.

is there any way to convert mongodb database to waterline db?

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.

Why we use ORM or ODM to manage any graphDB?

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.

Unit testing queries with MongoDB

I'm currently building a rest api and I'm struggling to find the best way to unit test each route.
A route handler performs various things and one of them is to execute a query to mongodb. I can unit test the route handler by using stubs, but if I'm testing the query I cannot stub the query itself, I need to have an in-memory mongodb that I could reset and insert new data for each test.
How do you test queries? I'm thinking that the only real way to ensure that the query does what I need is to use a real mongodb database installed in the testing machine (typically in the same machine used for developing).
yes, just as for relation databases, you need to have real base. if mongo offers in-memory auto-created version then it's easy. if not, then each developer has to have running mongo before he runs integration tests. for CI you can have one single dedicated mongo but than you have to prevent concurrent access (schema creation, multiple transactions etc). you should also implement automatic creation of schema if needed and empty database before each test. in relational db rollback is usually enough. when it's not enough then trimming all tables helps. although we had to implement it manually as we couldn't find any existing tools

Resources