I use sails 0.9.
Here is adapters.js code:
module.exports.adapters = {
mongo: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'tracker'
},
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
}
};
And model:
module.exports = {
adapter: 'mongo2',
config: {
database: 'offer'
},
attributes: {
name: {
type: 'string',
unique: true,
required: true
}
}
};
From docs http://sailsjs.org/#!documentation/models i get that this model will be saved in database named "offer", but it use db named "sails". Looks like its just ignore config section of model.
What is my mistake?
You should put the database in adapter config itself
mongo2: {
module: 'sails-mongo',
host: 'localhost',
port: 27017,
user: '',
password: '',
database: 'offer'
}
Related
As written in deno.land/x/mongo, to connect to mongo Atlas, such configuration should be use :
import { MongoClient } from "https://deno.land/x/mongo#v0.21.0/mod.ts";
const client1 = new MongoClient();
await client.connect({
db: "<db>",
tls: true,
servers: [
{
host: "<host>",
port: 27017,
},
],
credential: {
username: "<user>",
password: "<password>",
db: "<db>",
mechanism: "SCRAM-SHA-1",
},
});
const db = client1.database("TestingDB");
export default db;
There, <host>should be replaced by a specific cluster address (looks something like that : cluster0.hmdnu.mongodb.net) and not a full string as mongodb+srv://user1:MYPASSWORD#cluster0.hmdnu.mongodb.net/TestingDB?retryWrites=true&w=majority.
This solution works well. But there is, I believe, a downside : Sometimes, a Mongo cluster can fail and a Secondary cluster can become Primary. Has we are using a specific cluster on our code, that cluster might be down and our program as well. Any one encounter a similar issue ? and how did you resolve it ?
Add more cluster
servers: [
{
host: "<host>", /*primary*/
port: 27017,
},
{
host: "<host>", /*secondary*/
port: 27017,
},
{
host: "<host>", /*secondary*/
port: 27017,
},
],
I'm using:
"sails": "1.2.1",
"sails-mongo": "1.0.1"
when i get documents of a mongodb collection, i've got error like this:
In model `archive`:
debug: The default primary key attribute (`id`) is not set up correctly.
debug: When using `sails-mongo`, primary keys MUST have `columnName: '_id'`,
debug: and must _not_ have `autoIncrement: true`.
debug: Also, in most cases (unless `dontUseObjectIds` has been set to `true` for the model),
debug: then the `type` of the primary key must also be `string`.
sails.config.datastores.js
module.exports.datastores = {
default: {
adapter: 'sails-mongo',
host: 'localhost',
port: 27017,
database: 'test',
}
};
sails.config.models.js
module.exports.models = {
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'string', columnName: '_id' },
},
dataEncryptionKeys: {
default: 'RWcFGJN8+at5E7eIwNCIQxkR7P0nRAW8Fg4c9tzwFTw='
},
cascadeOnDestroy: true
};
api.models.User.js
module.exports = {
tableName: 'user',
attributes: {
name: {
type: 'string'
},
age: {
type: 'number',
}
},
};
i've got error when i run api.controllers.UserController.js
module.exports = {
getUsers: function (req, res) {
let users = User.find();
return res.send(users);
},
};
I have deployed a meteor app to port 80 of my AWS instance using MUP. I deployed a second meteor app to port 3000, but this time omitting mongo setup and specifying mongo url in the mup.js file. The setup works fine and the second app is deployed but none of my publications seems to work. I have tried the same setup with two test Apps previously and it worked.
MUP.JS of App 1
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'Dashboard',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 80,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
mongo: {
oplog: true,
port: 27017,
servers: {
one: {},
},
},
};
MUP.JS of App 2
module.exports = {
servers: {
one: {
host: 'IP',
username: 'ubuntu',
pem: 'path to my pem file'
}
},
meteor: {
name: 'DashBoard2',
path: 'Path to my project',
servers: {
one: {}
},
buildOptions: {
serverOnly: true,
},
docker: {
image: 'abernix/meteord:base',
},
env: {
PORT: 3000,
ROOT_URL: 'base url/',
MONGO_URL: 'mongodb://mongodb:27017/dbname'
},
deployCheckWaitTime: 320,
enableUploadProgressBar: true
},
};
Are you certain the connection to the database has been made? Typically the Meteor console will give an error if it can't connect to the MongoDB database. Also not sure if you have omitted your connection string, but it should be;
mongodb://user:password#server:port/database
I'm struggling to connect with sails to a mongodb database that uses a database for authentication named "dbadmin". Where I am DBA decided to have all users centralized in a users database.
I can connect to this "dbadmin" database but then sails complains it cannot create collections there.
How can I use an authentication database and then a different database for collections?
You can define two database connection in your connections config file and then you can pass the connection in your data model directly
see the example:
in your config file
// server/config/connections.js
dbadmin: {
adapter: 'sails-mongo',
host: 'dbadmin-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbadmin'
}
dbuser: {
adapter: 'sails-mongo',
host: 'dbuser-host',
port: 27017,
user: 'username',
password: 'password',
database: 'dbuser'
}
and now you can use in your model:
// server/api/models/AdminUser.js
module.exports = {
connection:'dbadmin',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
// server/api/models/Users.js
module.exports = {
connection:'dbuser',
attributes: {
name : { type: 'string' },
pass : { type: 'string' }
}
};
NOTE: you can not make any collection between this two model .
It turns out that using the URL for the connection has more possibilities. So all I had to do is append at the end authSource=dbadmin
module.exports = {
models: {
connection: 'mongoRM'
},
connections: {
mongoRM: {
adapter: 'sails-mongo',
url: 'mongodb://user:password#db_url:port/database?authSource=dbadmin'
}
}
}
Blockquote
i will planing use more database on same models on sails.Just wanna change db on progress.How can i do it on sails configure
Just change your connection config in config/connections.js to the db you will be using, then, in the model set the connection, example:
Connection
mysql: {
adapter: 'sails-mysql',
host: 'your-host',
user: 'user',
password: 'pass',
database: 'your-db'
port: 3306
}
Model
module.exports = {
schema: true,
connection: 'mysql',
tableName: 'users',
attributes: {
user:{
type:"string",
primaryKey: true,
unique: true
},
password:{
type:"string",
unique: true
}
}
};