1、安装sails对mongo的依赖
1 npm install sails-mongo --save2、 配置mongo连接
修改config/connections.js:
1 2 3 4 5 6 7 8 9 10 11 module.exports.connections = { someMongodbServer: { adapter: 'sails-mongo' , host: '127.0.0.1' , port: 27017, user: 'test' , //optional password: 'test' , //optional database: 'sails' //optional } };3、 模型层的基本配置
修改config/models.js,配置模型的连接数据库位mongodb,并为每个模型添加updatedAt以及createdAt属性:
1 2 3 4 5 6 7 module.exports.models = { migrate: 'alter' , 'connection' : 'someMongodbServer' , autoCreatedAt: true , autoUpdatedAt: true };
下面对migrate进行一些说明:
safe - never auto-migrate my database(s). I will do it myself (by hand)[不自动合并数据,需要手动控制] alter - auto-migrate, but attempt to keep my existing data (experimental)[与老数据自动合并,当添加新字段后,数据表才会被删除,推荐使用] drop - wipe/drop ALL my data and rebuild models every time I lift Sails[删除数据表,建立新表,插入新数据]
4、修改config/env/development.js
1 2 3 4 5 6 module.exports = { models: { connection: 'someMongodbServer' } };
5、 接下来,我们启动sails
1 sails lift