zoukankan      html  css  js  c++  java
  • node.js---sails项目开发(4)---配置MongoDB数据库连接

    1、安装sails对mongo的依赖

    npm install sails-mongo --save
    

    2、 配置mongo连接  

    修改config/connections.js:

    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属性:

    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

    module.exports = {
      models: {
        connection: 'someMongodbServer'
      }
    
    };
    

    5、 接下来,我们启动sails

    sails lift
    

      

  • 相关阅读:
    含字母数字的字符串排序算法,仿Windows文件名排序算法
    WCF、WPF、Silverlight和区别(转)
    线程组的介绍
    python基础字符串的修改
    c语言
    python 字典
    单元测试相关
    python列表
    如何才能设计出好的测试用例
    字符串查找
  • 原文地址:https://www.cnblogs.com/shenggen/p/5435657.html
Copyright © 2011-2022 走看看