MySQL向GraphQL迁移

大数据
GraphQL 是一个开源的图形数据库(基于Node.js实现), sequelize-auto 将 MySQL 数据库转变成模型。

MySQL向GraphQL迁移

GraphQL 是一个开源的图形数据库(基于Node.js实现), 中文文档: https://graphql.js.cool/

sequelize-auto 将 MySQL 数据库转变成模型

  1. [node] sequelize-auto -h <host> -d <database> -u <user> -x [password] -p [port] --dialect [dialect] -c [/path/to/config] -o [/path/to/models] -t [tableName] -C 参数: -h, --host 主机地址 [必须] -d, --database 数据名 [必须] -u, --user 用户名 -x, --pass 密码 -p, --port 端口号 -c, --config 配置文件,参考: https://sequelize.readthedocs.org/en/latest/api/sequelize/ -o, --output 输出目录 -e, --dialect 数据库引擎: postgres, mysql, sqlite -t, --tables 需要导入的表 -T, --skip-tables 需要排除的表 -C, --camel 使用用驼峰命名法 -n, --no-write 不需要写入文件 -s, --schema 数据库结构 

使用数据模型

这里是生成的一个示例模型:

  1. /* jshint indent: 2 */ 
  2.  
  3. module.exports = function(sequelize, DataTypes) { 
  4.   return sequelize.define('d_user', { 
  5.     uid: { 
  6.       type: DataTypes.INTEGER(11).UNSIGNED, 
  7.       allowNull: false
  8.       primaryKey: true 
  9.     }, 
  10.     username: { 
  11.       type: DataTypes.STRING(16), 
  12.       allowNull: false
  13.       defaultValue: '' 
  14.     }, 
  15.     mobile: { 
  16.       type: DataTypes.STRING(16), 
  17.       allowNull: false
  18.       defaultValue: '' 
  19.     }, 
  20.     email: { 
  21.       type: DataTypes.STRING(32), 
  22.       allowNull: false
  23.       defaultValue: '' 
  24.     }, 
  25.     password: { 
  26.       type: DataTypes.STRING(32), 
  27.       allowNull: false
  28.       defaultValue: '' 
  29.     }, 
  30.     salt: { 
  31.       type: DataTypes.STRING(8), 
  32.       allowNull: false
  33.       defaultValue: '' 
  34.     }, 
  35.     updatedAt: { 
  36.       type: DataTypes.INTEGER(10).UNSIGNED, 
  37.       allowNull: false 
  38.     } 
  39.   }, { 
  40.     tableName: 'user' 
  41.   }); 
  42. };  

创建数据库模型:

  1. const Sequelize = require('sequelize'); const Db = new Sequelize('数据库名''用户名''密码', { host: 'localhost', dialect: 'mysql' }) const User = Db.define('user', { uid: { type: Sequelize.INTEGER(11).UNSIGNED, allowNull: false, primaryKey: true }, username: { type: Sequelize.STRING(16), allowNull: false, defaultValue: '' }, mobile: { type: Sequelize.STRING(16), allowNull: false, defaultValue: '' }, email: { type: Sequelize.STRING(32), allowNull: false, defaultValue: '' }, password: { type: Sequelize.STRING(32), allowNull: false, defaultValue: '' }, salt: { type: Sequelize.STRING(8), allowNull: false, defaultValue: '' } }, { tableName: 'user', // 取消默认的时间戳, 否则会报 createdAt 不存在错误 timestamps: false }); Db.sync(); module.exports = { Db, User }; 

graphql-sequelize 转换 MySQL -> GraphQL 结构

  1. const { GraphQLObjectType,GraphQLSchema,GraphQLList,GraphQLInt,GraphQLString } = require('graphql'); 
  2. const { attributeFields, resolver } = require('graphql-sequelize'); 
  3. const { Db, User } = require('./db'); 
  4.  
  5. userType = new GraphQLObjectType({ 
  6.   name'User'
  7.   description: 'A user'
  8.   fields: attributeFields(User
  9. }); 
  10.  
  11. const Query = new GraphQLObjectType({ 
  12.   name'Query'
  13.   description: 'Root query object'
  14.   fields: () => { 
  15.     return { 
  16.       user: { 
  17.         type: new GraphQLList(userType), 
  18.         args: { 
  19.           uid: { 
  20.             type: GraphQLInt 
  21.           }, 
  22.           email: { 
  23.             type: GraphQLString 
  24.           } 
  25.         }, 
  26.         resolve(root, args) { 
  27.           return Db.models.user.findAll({ where: args }); 
  28.         } 
  29.       } 
  30.     }; 
  31.   } 
  32. }); 
  33.  
  34. const Schema = new GraphQLSchema({ 
  35.   query: Query 
  36. }); 
  37.  
  38. module.exports = Schema 

启动服务器

  1. const Express =require( 'express'); 
  2. const GraphHTTP =require( 'express-graphql'); 
  3. const Schema =require( './schema'); 
  4.  
  5. // Config 
  6. const APP_PORT = 3000; 
  7.  
  8. // Start 
  9. const app = Express(); 
  10.  
  11. // GraphQL 
  12. app.use('/graphql', GraphHTTP({ 
  13.   schemaSchema
  14.   pretty: true
  15.   graphiql: true 
  16. })); 
  17.  
  18. app.listen(APP_PORT, ()=> { 
  19.   console.log(`App listening on port ${APP_PORT}`);  
责任编辑:庞桂玉 来源: 36大数据
相关推荐

2013-01-06 09:43:35

MySQLMySQL迁移Redis

2010-01-13 17:24:34

SQL Server迁

2009-03-09 16:27:17

数据迁移PHPOracle

2010-11-10 09:03:27

云计算迁移

2015-11-24 17:46:42

云迁移IT基础设施云服务

2016-01-29 10:26:47

云端云迁移

2015-09-09 15:16:19

混合云云迁移

2011-06-09 10:36:51

IPv6IPv6部署

2012-07-31 09:55:53

云计算

2012-06-12 09:13:14

2020-12-02 10:35:09

云端灾难恢复云迁移

2012-08-01 10:08:10

云计算云迁移

2014-06-06 09:52:20

802.11acWi-Fi

2011-05-04 13:11:29

Exchange

2023-12-15 16:45:22

微软Azure迁移

2011-08-11 18:10:58

iCoremail云计算企业邮箱

2016-08-05 15:04:33

javascripthtmljs

2019-03-10 12:15:57

迁移云计算数据分析

2018-02-27 10:49:53

5G迁移安全

2011-12-01 09:45:11

虚拟化
点赞
收藏

51CTO技术栈公众号