node.js---sails项目开发(5)---用户表的建立

1、 ctrl+c  暂停sails项目  ,输入如下命令,创建一个user表

sails generate api user

2、在api目录分别建立了两个文件  api/controllers/UserController.js   api/modes/User.js

修改文件 api/modes/User.js

/**
 * User.js
 *
 * @description :: TODO: You might write a short summary of how this model works and what it represents here.
 * @docs        :: http://sailsjs.org/documentation/concepts/models-and-orm/models
 */

module.exports = {

  attributes: {
  	// 站点名称
    siteName: {
      type: 'string',
      required: true,
      minLength:1,
      maxLength:10
    },
    // 邮箱
    email: {
      type: 'email',
      unique: true,
      required: true
    },
    // 密码
    password: {
      type: 'string',
      required: true
    },
    // 站点简介
    siteDesc: {
      type: 'string',
      defaultsTo: '暂无简介',
      maxLength:40
    },
    // 是否管理员(默认为非管理员)
    isAdmin: {
      type: 'boolean',
      defaultsTo: false
    }
  }
};

3、打开如下连接向User表中插入一条数据

http://localhost:1337/user/create?siteName=lishenggen&email=275606870@qq.com&password=123456&siteDesc=description&isAdmin=true

页面将出入如下信息

{
  "siteName": "lishenggen",
  "email": "275606870@qq.com",
  "password": "123456",
  "siteDesc": "description",
  "isAdmin": true,
  "createdAt": "2016-04-26T09:19:53.600Z",
  "updatedAt": "2016-04-26T09:19:53.600Z",
  "id": "571f32b9b9d7e6e7041ad738"
}

4、接下来我们打开数据库看看,依次输入如下命令  step1 连接数据库 step2 检索当前库中所有的表,step3 查询user表中所有记录,加了pretty()的意思是格式化输出,pretty()可加,可不加

mongo 127.0.0.1/sails -u test -p test
show tables
db.user.find({}).pretty()

这时我们在终端可以看到可以打印出如下信息

{
	"_id" : ObjectId("571f32b9b9d7e6e7041ad738"),
	"siteName" : "lishenggen",
	"email" : "275606870@qq.com",
	"password" : "123456",
	"siteDesc" : "description",
	"isAdmin" : true,
	"createdAt" : ISODate("2016-04-26T09:19:53.600Z"),
	"updatedAt" : ISODate("2016-04-26T09:19:53.600Z")
}

好了,我们成功写入一条数据进去,是不是很方便???  

posted on 2016-04-26 17:27  shenggen  阅读(573)  评论(0编辑  收藏  举报

导航