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" ) } |
好了,我们成功写入一条数据进去,是不是很方便???
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· 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 让容器管理更轻松!