sequelize

全栈项目|小书架|服务器开发-用户模块设计(用户表设计,注册登录,退出登录)

佐手、 提交于 2019-12-05 12:08:57
写系列文章最容易出现的情况是写到一半,然后写不下去了。一个因素是人变懒了;一个因素是这几天在整理用户模块这部分的知识,发现有太多的内容要写,而深入Google之后又发现好多内容是自己未知的,写着写着就不知道该如何写了。 常见的和用户模块相关的操作有: 注册登录、信息修改、退出登录 用户表设计 一个系统中,用户模块是最基础也是最重要的。如果只考虑一种登录方式,那用户表可以用一张 users 表搞定一切。但是现实情况是登录方式有很多种,比如: 账号密码登录 , 微信、QQ、微博登录 , 手机号、邮箱登录 。而且一个用户可以使用 账号密码登录 之后再关联 微信、QQ、微博 ,之后可以直接使用 微信、QQ、微博 登录;那么如何保证设计 用户表 将变得至关重要。 参考: 可扩展的用户表设计 设计一个可扩展的用户登录系统 (1) 用户系统设计与实现 多平台统一用户系统设计 通过以上部分博文的介绍,我们可以将用户表设计为: 用户基础表:user_base , 用户授权表:user_auth , 用户扩展表:user_extends 。 用户基础表:uid、user_role、user_name、avatar、password、signature、birthday、gender等 用户授权表:id、uid、identity_type、identifier、certificate等 用户扩展表:id

全栈项目|小书架|服务器开发-NodeJS 中使用 Sequelize 操作 MySQL数据库

旧时模样 提交于 2019-12-04 15:16:40
安装 官网: https://sequelize.org/v5/manual/getting-started.html 安装 sequelize 及数据库连接驱动 npm install --save sequelize $ npm install --save mysql2 使用 创建连接: const Sequelize = require('sequelize'); const sequelize = new Sequelize('database', 'username', 'password', { host: 'localhost', dialect: 'mysql' }); 创建Model表: const Model = Sequelize.Model; class User extends Model {} User.init({ // attributes firstName: { type: Sequelize.STRING, allowNull: false }, lastName: { type: Sequelize.STRING // allowNull defaults to true } }, { sequelize, modelName: 'user' // options }); 生产数据: // Note: using `force: true`

sequelize 别名排序

南楼画角 提交于 2019-12-04 10:33:17
User.findAll({ attributes: [ 'User.username', [sequelize.literal('(SELECT COUNT(*) FROM Posts WHERE Posts.userId = User.id)'), 'PostCount'] ], order: [[sequelize.literal('PostCount'), 'DESC']] }); 来源: https://www.cnblogs.com/xiguachaodan/p/11854960.html

Sequelize JSON data type

匿名 (未验证) 提交于 2019-12-03 08:54:24
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have defined a model as module . exports = function ( sequelize , DataTypes ) { const MyModel = sequelize . define ( 'MyModel' , { data : { type : DataTypes . JSON , ... }, ... }); return MyModel ; }; I query it using MyModel . findAll (). then ( myModels => ...); However, data field in the query result is a string, not a JSON object. How do I fix it? 回答1: It's not supported yet MySQL JSON Data Type #4727 . But you can do something like this: module . exports = function ( sequelize , DataTypes ) { const MyModel = sequelize .

How can I do a group by across 3 tables with Sequelize?

匿名 (未验证) 提交于 2019-12-03 08:52:47
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: My models are: module . exports = function ( sequelize , DataTypes ) { var CommitFileStatistic ; return CommitFileStatistic = sequelize . define ( 'CommitFileStatistic' , { additions : { type : DataTypes . INTEGER , allowNull : false }, deletions : { type : DataTypes . INTEGER , allowNull : false }, status : { type : DataTypes . STRING , allowNull : false }, fileSize : { type : DataTypes . INTEGER , allowNull : true }, levenshteinDelta : { type : DataTypes . INTEGER , allowNull : true }, fileHash : { type : DataTypes . STRING ,

Sequelize Deprecated Error Message

匿名 (未验证) 提交于 2019-12-03 08:46:08
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm very new to Node and I'm getting my head around how ORM and Sequelize works. I've been on the Sequelize website and copied the connection string and altered it to work with my database. When I execute the file, it seems to execute OK creating the table in my database however I get the error "String based operators are now deprecated.Please use Symbol based operators for better security ....node_modules/sequelize/lib/sequelize.js:236:13" I understand why the operators have been deprecated, however as I've installed this as a new package

Sequelize模糊查询

我们两清 提交于 2019-12-03 07:44:55
const Sequelize = require('sequelize'); const Op = Sequelize.Op; User.findAll({ raw: true, order: [ ['name', 'DESC'] ], // 排序 where: { // name: 'cheny', // 精确查询 mobile_no: { // 模糊查询 [Op.like]:'%' +mobile_no + '%' } }, attributes:['id','name'], // 控制查询字段 }) . 来源: https://www.cnblogs.com/xiangsj/p/11783666.html

Sequelize: update in bulk

匿名 (未验证) 提交于 2019-12-03 03:10:03
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using Node.js, MySQL and Sequelize. I'd like to insert some 10k rows into a table at once. The table has custom primaryKey field, that is being set manually. The data are downloaded from web and are overlapping. I'd like to have a version of bulkCreate that wouldn't fail if any of the rows in data have unique keys that are already present in the table. Such kind of things is done in MySQL via INSERT ... ON DUPLICATE KEY UPDATE construct. How do I do it in Sequelize? 回答1: Pass in an options object to bulkCreate with ignoreDuplicates set

Is there a simple way to make Sequelize return it's date/time fields in a particular format?

匿名 (未验证) 提交于 2019-12-03 03:08:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: We need to have sequelize return dates in a particular format, not the default one. As far as I can tell, there is no way to set that up in options, or any other way. Short of manually updating the dates every time after they are retrieved, anyone been able to solve this easily? Or am I missing something? 回答1: you can define custom instance methods getDate/setDate which would translate date between sequelize internal representation and desired format like so http://sequelize.readthedocs.org/en/latest/docs/models-definition/index.html (see

Sequelize bulkCreate() returns NULL value for primary key

匿名 (未验证) 提交于 2019-12-03 03:06:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I am writing rest using node, sequelize as ORM for mySQL. I am using bulkCreate function to create record in bulk. But in response it is returning null for primary key value. Model sequelize.define('category', { cat_id:{ type:DataTypes.INTEGER, field:'cat_id', primaryKey: true, autoIncrement: true, unique:true }, cat_name:{ type: DataTypes.STRING, field: 'cat_name', defaultValue:null } }); Bulk Create operation : var data = [ { 'cat_name':'fashion' }, { 'cat_name':'food' } ]; orm.models.category.bulkCreate(data) .then(function(response){ res