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
}
}
};
Related
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);
},
};
How do I set up Sequelize to use an auto increment version number?
According to the documentation, all I need is just setting version: true: http://docs.sequelizejs.com/manual/tutorial/models-definition.html#configuration
First attempt:
import Sequelize from "sequelize";
export const sequelize = new Sequelize('my_connection', 'fake-user-name', 'fake-password', {
host: 'localhost',
dialect: 'postgres'
});
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false}
}, {
underscored: true,
tableName: 'r_users',
version: true // <- here
});
Result: there is no version column created.
Second attempt:
import Sequelize from "sequelize";
export const sequelize = new Sequelize('my_connection', 'fake-user-name', 'fake-password', {
host: 'localhost',
dialect: 'postgres'
});
export const User = sequelize.define('user', {
id: {type: Sequelize.STRING, primaryKey: true},
name: {type: Sequelize.STRING, allowNull: false},
version: { // <- here as well
type: Sequelize.INTEGER,
allowNull: false,
defaultValue: 0
}
}, {
underscored: true,
tableName: 'r_users',
version: true
});
Result: version column is created, but it doesn't auto increment when updating the record (I expect the below would increment version):
User.find({where: {id: 1}}).then(item => {
item.name = "Bob";
item.save();
});
Notes:
I'm using postgres 9.6 and the libraries I'm using are as below:
"sequelize": "^3.30.4"
"pg": "^6.2.3",
"pg-hstore": "^2.3.2",
Question:
Could anyone help me to point out what I have done wrong?
Thanks in advance.
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'
}
}
}
I'm using PostgreSQL, Sequelize, and Sequelize-cli.
Using sequelize-cli and some manipulation in an IDE I set up the following model and migration file (simplified for the example, I didn't include the "Users" migration file):
Model File
// models/annotation.js
module.exports = function (sequelize, DataTypes) {
var Annotation = sequelize.define('Annotation', {
userId: {
type: DataTypes.INTEGER,
references: {model: "User", key: 'id'},
},
url: DataTypes.STRING,
source: DataTypes.STRING,
body: DataTypes.TEXT,
exact: DataTypes.TEXT,
}, {
classMethods: {
associate: function (models) {
// associations can be defined here
Annotation.belongsTo(models.User, {foreignKey: "userId"});
},
},
});
return Annotation;
};
Corresponding migration file
// migrations/20161121050521-create-annotation.js
const User = require("../models/user");
module.exports = {
up: function(queryInterface, Sequelize) {
return queryInterface.createTable('Annotations', {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
userId: {
type: Sequelize.INTEGER,
references: {model: User, key: 'id'},
allowNull: false
},
...
Referenced Model
// models/user.js
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User', {
title: DataTypes.STRING,
name: DataTypes.STRING,
email: DataTypes.STRING,
}, {
classMethods: {
associate: function(models) {
// associations can be defined here
User.hasMany(models.Annotation, {foreignKey: "userId"});
}
}
});
return User;
};
I'm running the follow cli commands:
"dropdb -U postgres annotate && curl -XDELETE 'http://localhost:9200/_all'"
"createdb -U postgres annotate"
"node_modules/.bin/sequelize db:migrate"
When I migrate I get the following error:
SequelizeDatabaseError: relation "User" does not exist
From the documentation I was under the impression that Annotation.belongsTo(models.User would have been enough to establish the association, but when I did that my db tables didn't have any foreign key fields.
How can I establish tables with associations to one another?
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'
}