sails database connection monogodb

1, the overall installation: the install -g CNPM sails
2, projects into the command window positions
New Project: sails new sails_cqwu --fast, select 2 (rapid establishment of sails projects)

. 3, the following items CD into the sails, installation dependencies required to install npm

cd sails_cqwu
cnpm install 注:安装依赖包


4, into the bin directory mongodb installed mongodb input command to start the database, the data exists inside the data folder

mongod --dbpath F:\webItem\nodejs\sails_cqwu\data


NOTE: monogodb default port is 27017, To modify, when the connection to the database port number added in the back, such as

mongod --dbpath F:\webItem\nodejs\sails_cqwu --port=2700

5, back to the sails project, mongo database connection
(1) requires the installation package

cnpm install sails-mongo --save

Note: If mysql database, you also need to install

cnpm install sails-mysql --save

(2) modify the database file config-> datastores.js file, if only one database, write directly to the default inside

<!--config/datastores.js-->
      module.exports.datastores = {
        default: {//默认使用MySQL数据库
          adapter: 'sails-mysql',
          url: 'mysql://user:password@localhost:3306/my_db_name',
        },
        mongoDb: {//使用mongo数据库
          adapter: 'sails-mongo',
          url: 'mongodb://root:@localhost:27017/shop'//后面是数据库名
        }
      };

Note: mongodb database without having to manually create the database, here to write after the code is automatically created
(3) a new model, in fact, create a table or a collection, I created a new collection of news

sails generate model news

(4) modify the model file in config-> models.js

<!--config/models.js-->
  schema: true,//严格匹配数据表的模式,
 migrate: 'alter',//在尽量不丢失数据的情况下,允许sails修改表的结构
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },//数据创建时间,自动生成
updatedAt: { type: 'number', autoUpdatedAt: true, },//数据更新时间
//id: { type: 'number', autoIncrement: true, } 注:mysql使用
id: { type: 'string', columnName: '_id' }//mongodb使用,两者只能用其一
    }
    //允许sails自动添加三个字段,即共有的数据

(5) allow a collection model has a separate mode, commonly used in mongodb collections, such as my own new news table, the following data is the news private members (columns);

如:<!--api/models/News.js-->
 module.exports = {
  attributes: {
     title:{type:'string',required:true},
      author:{type:'string',required:true},
      content:{type:'string',required:true},
      url:{type:'string',required:true}
  },
};

6, create a set of data in which news, whether the database connection is successful is determined according to the value of the console, the params is a json

  var row=await News.create(params).fetch();
  res.send(row);

6, sails into the project directory, start the project: sails lift, browser access: localhost: 1337 / admin / manage
Note: The front-access port is a small sailing port following the project started

Guess you like

Origin www.cnblogs.com/xyyl/p/11116582.html