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

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

1
sails generate api user

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

修改文件 api/modes/User.js

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
 * 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

页面将出入如下信息

1
2
3
4
5
6
7
8
9
10
{
  "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()可加,可不加

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

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

1
2
3
4
5
6
7
8
9
10
{
    "_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   shenggen  阅读(580)  评论(0编辑  收藏  举报

编辑推荐:
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示