my blog my blog

Category: Sails.js
Sails.js配置MongoDB数据库

 

在Sails.js中,默认使用的是本地的一个数据库localDiskDb,我们在项目中如果想使用数据库MongoDB的话,需要自己手动配置,首先是config/connections.js文件,我们需要将我们想使用的数据库通过配置文件添加进去。入红字部分

  1. /** 
  2.  * Connections 
  3.  * (sails.config.connections) 
  4.  * 
  5.  * `Connections` are like "saved settings" for your adapters.  What's the difference between 
  6.  * a connection and an adapter, you might ask?  An adapter (e.g. `sails-mysql`) is generic-- 
  7.  * it needs some additional information to work (e.g. your database host, password, user, etc.) 
  8.  * A `connection` is that additional information. 
  9.  * 
  10.  * Each model must have a `connection` property (a string) which is references the name of one 
  11.  * of these connections.  If it doesn't, the default `connection` configured in `config/models.js` 
  12.  * will be applied.  Of course, a connection can (and usually is) shared by multiple models. 
  13.  * . 
  14.  * Note: If you're using version control, you should put your passwords/api keys 
  15.  * in `config/local.js`, environment variables, or use another strategy. 
  16.  * (this is to prevent you inadvertently sensitive credentials up to your repository.) 
  17.  * 
  18.  * For more information on configuration, check out: 
  19.  * http://sailsjs.org/#!/documentation/reference/sails.config/sails.config.connections.html 
  20.  */ 
  21.  
  22. module.exports.connections = { 
  23.  
  24.   /*************************************************************************** 
  25.   *                                                                          * 
  26.   * Local disk storage for DEVELOPMENT ONLY                                  * 
  27.   *                                                                          * 
  28.   * Installed by default.                                                    * 
  29.   *                                                                          * 
  30.   ***************************************************************************/ 
  31.   localDiskDb: { 
  32.     adapter: 'sails-disk' 
  33.   }, 
  34.  
  35.   /*************************************************************************** 
  36.   *                                                                          * 
  37.   * MySQL is the world's most popular relational database.                   * 
  38.   * http://en.wikipedia.org/wiki/MySQL                                       * 
  39.   *                                                                          * 
  40.   * Run: npm install sails-mysql                                             * 
  41.   *                                                                          * 
  42.   ***************************************************************************/ 
  43.   someMysqlServer: { 
  44.     adapter: 'sails-mysql', 
  45.     host: 'YOUR_MYSQL_SERVER_HOSTNAME_OR_IP_ADDRESS', 
  46.     user: 'YOUR_MYSQL_USER', 
  47.     password: 'YOUR_MYSQL_PASSWORD', 
  48.     database: 'YOUR_MYSQL_DB' 
  49.   }, 
  50.  
  51.   /*************************************************************************** 
  52.   *                                                                          * 
  53.   * MongoDB is the leading NoSQL database.                                   * 
  54.   * http://en.wikipedia.org/wiki/MongoDB                                     * 
  55.   *                                                                          * 
  56.   * Run: npm install sails-mongo                                             * 
  57.   *                                                                          * 
  58.   ***************************************************************************/ 
  59.   MongodbServer: { 
  60.     adapter: 'sails-mongo', 
  61.     host: 'localhost', 
  62.     port: 27017, 
  63.     // user: 'username', 
  64.     // password: 'password', 
  65.     database: 'catgood' 
  66.   }, 
  67.  
  68.   /*************************************************************************** 
  69.   *                                                                          * 
  70.   * PostgreSQL is another officially supported relational database.          * 
  71.   * http://en.wikipedia.org/wiki/PostgreSQL                                  * 
  72.   *                                                                          * 
  73.   * Run: npm install sails-postgresql                                        * 
  74.   *                                                                          * 
  75.   *                                                                          * 
  76.   ***************************************************************************/ 
  77.   somePostgresqlServer: { 
  78.     adapter: 'sails-postgresql', 
  79.     host: 'YOUR_POSTGRES_SERVER_HOSTNAME_OR_IP_ADDRESS', 
  80.     user: 'YOUR_POSTGRES_USER', 
  81.     password: 'YOUR_POSTGRES_PASSWORD', 
  82.     database: 'YOUR_POSTGRES_DB' 
  83.   } 
  84.  
  85.  
  86.   /*************************************************************************** 
  87.   *                                                                          * 
  88.   * More adapters: https://github.com/balderdashy/sails                      * 
  89.   *                                                                          * 
  90.   ***************************************************************************/ 
  91.  
  92. }; 

到这里,数据库并没有切换,我们还需要设置config/models.js

  1. /** 
  2.  * Default model configuration 
  3.  * (sails.config.models) 
  4.  * 
  5.  * Unless you override them, the following properties will be included 
  6.  * in each of your models. 
  7.  * 
  8.  * For more info on Sails models, see: 
  9.  * http://sailsjs.org/#!/documentation/concepts/ORM 
  10.  */ 
  11.  
  12. module.exports.models = { 
  13.  
  14.   /*************************************************************************** 
  15.   *                                                                          * 
  16.   * Your app's default connection. i.e. the name of one of your app's        * 
  17.   * connections (see `config/connections.js`)                                * 
  18.   *                                                                          * 
  19.   ***************************************************************************/ 
  20.   connection: 'MongodbServer', 
  21.  
  22.   /*************************************************************************** 
  23.   *                                                                          * 
  24.   * How and whether Sails will attempt to automatically rebuild the          * 
  25.   * tables/collections/etc. in your schema.                                  * 
  26.   *                                                                          * 
  27.   * See http://sailsjs.org/#!/documentation/concepts/ORM/model-settings.html  * 
  28.   *                                                                          * 
  29.   ***************************************************************************/ 
  30.   migrate: 'safe' 
  31.  
  32. }; 

其中的connection后面的字段就是connection.js中配置的MongoDB的名称,然后migrate这里我们使用safe模式,其实这里有三种模式可以选择,如果注释掉这一行,启动sails lift的时候会提示。

到这里我们配置文件已经配置了,需要用npm来添加支持组件了。

  1. npm install sails-mongo 

到此为止,MongoDB的安装就完成了。

Sails.js安装教程

 

很久不搞web了,学点儿新东西。最近打算学习node.js,做个小项目,找了下node.js的框架,感觉比较喜欢Sails.js,但是国内的Sails.js的资料实在是少之又少,于是乎,决定自己把Sails的教程整理下,也不知道能学到啥样,能做出啥东西来,一点儿点儿来吧,奶牛Sails.js折腾现在开始。

1.Sails.js需要node.js的支持,所以先安装Node.js。过程不详述。

2.安装Sails.js

  1. npm -g install sails 

3.使用Sails.js建立Sails.js项目

  1. sails new nenewTestProject 

4.启动Sails.js项目

  1. cd nenewTestProject 
  2. sails lift 

这时候如果sails正常运行会看到如下提示

  1. info: Starting app... 
  2.  
  3. info: 
  4. info:                .-..-. 
  5. info: 
  6. info:    Sails              <|    .-..-. 
  7. info:    v0.12.1             |\ 
  8. info:                       /|.\ 
  9. info:                      / || \ 
  10. info:                    ,'  |'  \ 
  11. info:                 .-'.-==|/_--' 
  12. info:                 `--'-------' 
  13. info:    __---___--___---___--___---___--___ 
  14. info:  ____---___--___---___--___---___--___-__ 
  15. info: 
  16. info: Server lifted in `C:\Users\Nenew\nenewTestProject` 
  17. info: To see your app, visit http://localhost:1337 
  18. info: To shut down Sails, press <CTRL> + C at any time. 
  19.  
  20. debug: -------------------------------------------------------- 
  21. debug: :: Sat Feb 27 2016 11:34:28 GMT+0800 (中国标准时间) 
  22.  
  23. debug: Environment : development 
  24. debug: Port        : 1337 
  25. debug: -------------------------------------------------------- 

至此,Sails.js已经安装运行成功,我们可以通过http://localhost:1337对Sails.js的默认页面进行访问。