use Waterline as Standalone (no express) - node.js

Good Afternoon,
I am new with node.js and I try to develope an only command app.
For this app I need an ORM and I wish to use WATERLINE as standalone but not in express framework.
I looked at the example and I succeed to see my different collections.
// Our collections (i.e. models):
ontology.collections;
console.log(ontology.collections);
// Our connections (i.e. databases):
ontology.connections;
I am stucked after this. I can't find a way to return my models and make queries.
If someone could help me taht would be great.
Thanks

If you initialized Waterline in the ontology variable and got the collections successfully loaded as you say, now you can access each collection loaded (with loadCollection()) like this:
ontology.collection.mycollection
Where mycollection is the identity defined in your model.
Then you can make queries:
ontology.collection.mycollection.find(...)

Related

How can i configure Mongoose to automatically create objects in my mongoDB on app start if none is found?

I am coming from a Java Hibernate frameworks, where an sql script can be written and added to a project, with the intentions of creating objects in my Mongo Database when the app runs and no data was found in a specific database table. I am using Mongoose presently with mongoDB, and i want to initialize my mongoDB table with objects if none is found. Initially i achieved this my creating an object and calling the new mongoose.save(obj) inside my app.js which is my start up file. But is there a cleaner way one already supported by Mongoose, or is this my best bet ?

Can I change databases on each request (Sails.js)

I have a few PHP scripts which I am in the process of migrating to Node.js. I am using Sails.js for this and I would like to know how I can change databases for each request based on a request parameter.
Currently I have 3-4 identical PostgreSQL databases. Let's just say that each database corresponds to a different client.
Below is a segment of the current PHP script where the database connection is established:
$database = $_GET['db'];
$conn_details = "host=localhost port=5432 dbname=$database user=****** password=******";
$dbconn = pg_connect($conn_details);
Here you can see that the database name is coming from the request parameter "db".
I would like to have a similar functionality in my sails.js controller. I know that i can declare multiple databases in the connections.js and that I can have models use different databases but what i am after is for the models to stay the same and only the database to change based on each request.
I have found 2 similar questions but they have both stayed unanswered for quite some time now. (Here and here)
I think you are looking for something like sub apps
sails-hook-subapps
but it's experimental module. So i wouldn't recommend using it on production. Other option also not good is multiplying your Models like that:
One main model with all methods, attributes and "stuff"
Many models with connections config
In 'parent' model you will select to which model you want to send send action. For example write method:
getModel: function(dbName){
return models[dbName];
}
in models Object you will store all Models with different connections. Not sure how validators will works in this scenario. You need to test if it will not be required do do something like this in child Models
attributes: parentModel.attributes

Data not returned from custom collection

I have a hand-made collection (mymodel) in MongoDB. I have created a sails model+controller using 'sails generate api mymodel'
I am now trying to use the sails find() method using the REST api: http://localhost:1377/mymodel, but I do not get any data returned. I have even tried to write my own end-point method and used the Waterline ORM find() method, but still don't get anything returned.
Am I missing something? Is there something more to be done?
Its a simple model, like {A:123,b:"wf"}
No specific data pieces
From mongo shell, I get the complete information
Found the problem.
If a create a collection using the name "MyModel" in MongoDB, and then create a controller+model in sails.js (sails generate api MyModel), sailsjs does not seem to use the "MyModel" collection, but rather tries to create a new collection completely in lower case ("mymodel") where all the transactions are committed.
So the trick is to create a collection in lowercase only so that sails starts using it
Not sure if this is a bug in sails.js

How to perform SQL Joins and Relations in Sails.js and Waterline?

Can anyone guide me on how to setup relational schema & performs joins in sails.js?
Associations are officially Supported in Waterline
Overview
From the docs:
With Sails and Waterline, you can associate models across multiple data stores. This means that even if your users live in PostgreSQL and their photos live in MongoDB, you can interact with the data as if they lived together in the same database. You can also have associations that span different connections (i.e. datastores/databases) using the same adapter. This comes in handy if, for example, your app needs to access/update legacy recipe data stored in a MySQL database in your company's data center, but also store/retrieve ingredient data from a brand new MySQL database in the cloud.
Supported Association Types
One to Many
Many to Many
Cross-adapter Dominance
One to One
One Way
Planned Association Types
Through Associations
Original Post
I'm the author of Waterline, the ORM used in Sails. Waterline is brand
new and we are adding features all the time. Currently we don't have
support for associations but it's next on the roadmap. We worked out
an API for associations that I think most people will really like. You
can view the work in progress and the proposed API at: [Proposed Sails
Associations API][1].
We are going to tackle Associations and Transactions next and hope to
have them ready in the next month or so.
In the mean time if you are using the MySQL or PostgreSQL adapters
they both expose a raw .query() method that allows you to pass in a
hand built sql query and have it executed. I totally realize this
isn't ideal but should allow you to continue building your app while
we get support for associations and joins.
The function signature for the query method is:
Model.query(<sql query>, <optional data>, callback);
The example from particle banana works but should actually use "new" like "var instance = new User._model(values)". I'm using the following code and it works.
Accounts.query(query, function(err, accounts) {
if (err)
return fn(err);
accounts = _.map(accounts, function(account) {
return new Accounts._model(account);
});
fn(null, accounts);
});

Getting started with a database for Node

The most relevant question here on StackOverflow was this, which still doesn't answer my question because the answer nor the redis repo give a tutorial/walk-through that beginners don't understand.
The thing is, I have absolutely no idea how to setup a simple database in order to create a simple to-do list or blog on my own. This is probably the closest tutorial on how to setup a database. But it's lacking in a sense due to not having a schema or so defined in order for me to edit or add "tables".
Simply put, I'm looking for a tutorial a complete beginner is able to follow on how to setup a database and define custom schemas for data (e.g. products that can have reviews nested in them).
Any suggestions?
I guess this should cover it: Node.js, MongoDB and Mongoose
You mentioned that you wanted to create a Todo application. There is TodoMVC. You can see various MVC Frameworks in action. There's also an example with Mongoose and Backbone.
For installing different NoSQL options including MongoDB, CochDB, Redis and SQLite for use with Node.js this is a nice walk through.
Once installed the following steps are required to get going with the database. Typical example for mongodb:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/your_database');
var Schema = mongoose.Schema;
var User = new Schema({
'_id' : String,
'name' : String,
'votes' : Number });
var User_Model = mongoose.model('User', User);
Approaches to read/ write data from/to the database varies.
I think the simplest thing to do is to download and install MongoDB and use the mongodb-native driver to store and access your data.
MongoDB is schema-less so you won't need to define any table or keys in advance. Simply open a collection (which will be created automatically if it doesn't exist) and start storing documents/objects in it.
Mongo is fast, powerful and, in my opinion, easy to use.
See mongodb.org for more information.
I find this screencasts very helpful for nodeJS + MongoDB, even though the mongoose has been updated a lot since the video, but the basics remained. And you'll learn the new one in no time just by skimming mongoose's doc.
I've downloaded all of his videos and watch them every time I need brushing up my node skillz :p
Sidenote: He just uploaded screencast for couchDB, for other who prefers different cup of tea.

Resources