Reputation: 199
ive read various tutorials and instructions how to connect sails to js. Every tutorial is telling me to do this. I am new to mongodb btw.
I followed the instructions
install sails-mongo (npm install)
Edit the config/connection
mongo: {
adapter: 'sails-mongo',
host: 'localhost',
port: 54321,
database:'dbname'
}
Edit the config/models.js
connection:'mongo'
Edit the local.js
connections: {
mongodb: {
host : 'localhost',
port : 54321,
database : 'dbname'
}
}
so in my api/model/User.js
module.exports = {
attributes:{
name: {
type: 'string'
},
employedIn:{
collection:'company'
}
},
findUsers :function(opts,cb){
Users.findOne(opts).exec(function (err, theUser) {
// to do
// i wanna show the data of the user
});
}
}
I run console.log(Users) but I didnt find column/documents there.
Now, how am i going to get the collection named users from mongodb? (Just like 'SELECT * FROM users' in SQL or db.users.find().pretty() )
Upvotes: 1
Views: 2802
Reputation: 6338
You query a waterline model by models find
or findOne
method. You create a new record by create
, update by update
and delete by destroy
methods. There are some more methods exposed by query interface. You have to call exec
and pass a callback to it, to get it run. Documentation is here: Waterline Query Interface
So basically it's just:
User.create({
name: 'Max Mustermann'
}).exec(function(console.log));
User.find().exec(function(console.log));
User.create({
name: 'Peter Pan'
}).exec(function(console.log));
User.find().exec(console.log);
User.findOne({
where: { name: 'Max Mustermann' }
}).exec(function(err, user) {
user.destroy().exec(console.log);
});
You do not need a custom findUsers
method on your model. This is just find:
// /api/model/User.js
module.exports = {
attributes:{
name: {
type: 'string'
},
employedIn:{
collection:'company'
}
}
}
You should use sails console
to test.
Upvotes: 1