Getting started with a database for Node - node.js

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.

Related

Moving specific collections from mongodb atlas to archive db

I did my homework before posting this question
So the case is that I want to create a utility in my nodejs application that will move specific collections from my main database to an archive database and vice versa. I am using mongo db atlas for my application. I have been doing my research and I found two possible ways one is to create a mongodump and store and other is to create a backup file myself using my node application and upload it to archive db. Using the later approach will cause to loose my collection indexes.
I am planning to use mongodump for the purpose but can't find a resource that shows how to achieve that. Any help would be appreciated. Also if any one has any experience with similar situation I am open to suggestions as well.
I recently created a mongodump & mongorestore wrapper for nodejs: node-mongotools
What does it mean?
you have to install mongo binary on your host by following official mongo documentation(example) and then, you could use node-mongotools to call them from nodeJS.
Here is an example but tool doc contains more details:
var mt = new MongoTools();
const dumpResult = await mt.mongodump({ uri, path })
.catch(console.log);

use Waterline as Standalone (no express)

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(...)

User specific database in MongoDB

I am currently working on an inventory management software in Node js and MongoDB. I am pretty new to MongoDB, having worked in Oracle and MySQL for most of my projects.
Is it possible to create a separate database schema for every client who uses my software, with each client having access only to his copy of the database schema and collections?
The equivalent of selecting data in Oracle database would be
Select * from User1.table,
Select * from User2.table etc
Also, if it were possible, how would it be implemented using a node js mongo db client like mongoose?
I looked at MongoDB documentation, but it talks mainly about adding users to a database for authorization.
I apologize if it seems like a silly question, but id appreciate it if someone could point me in the right direction for this.
Before starting to invest a lot of time in the development of your project, check out other possible approaches to the scenario that you are trying to build.
I did a quick search on SO and found some additional threads with similar scenarios:
MongoDB Database vs. Collection
MongoDB Web App - Database per User
Additional info about mongoose database creation
Whenever you call the connect method on the mongoose object, you are either connecting to an existing database or you are creating it in case it doesn't already exist.
You could have a function that allows you to pass in a name argument with the name and create databases programmatically:
function createDatabase(name) {
var conn_string = 'mongodb://localhost/';
if (typeof name == 'string') {
conn_string += name;
}else{
return false;
}
mongoose.connect(conn_string);
}
Also, be aware that a database will be created when you first insert a record in a collection of that particular database.
It is not sufficient to only connect to the database, you also have to insert a record.
As per my previous example, you could also pass a schema parameter to the function, tailored to each user's profile and fire an insert statement after you connect to that database.

From where can we decide collection name in use of "sails-mongoose" package, in node.js + sailsjs?

I had code for Connection with mongo with username, password, host, DBName etc in adapter.js,
schema in model,
and for CRUD we can code in controller. but from where can we select particular collection of selected DB of mongo?
I am the creator of Sails-mongoose. I've not really continued my work with sails-mongoose because to use mongoose, you will have to cut out Waterline which is built completely into the eco system of Sailsjs.
I would recommend using against the library i myself built.
Apologies & Thanks
It will automatically create or use model's name collection in selected DB of mongo.

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);
});

Resources