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

MySQL向GraphQL迁移 MySQL向GraphQL迁移(mysql表数据迁移)  大数据 MySQL GraphQL 第1张

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

创建数据库模型:

  1. constSequelize=require('sequelize');constDb=newSequelize('数据库名','用户名','密码',{host:'localhost',dialect:'mysql'})constUser=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. userType=newGraphQLObjectType({
  5. name:'User',
  6. description:'Auser',
  7. fields:attributeFields(User)
  8. });
  9. constQuery=newGraphQLObjectType({
  10. name:'Query',
  11. description:'Rootqueryobject',
  12. fields:()=>{
  13. return{
  14. user:{
  15. type:newGraphQLList(userType),
  16. args:{
  17. uid:{
  18. type:GraphQLInt
  19. },
  20. email:{
  21. type:GraphQLString
  22. }
  23. },
  24. resolve(root,args){
  25. returnDb.models.user.findAll({where:args});
  26. }
  27. }
  28. };
  29. }
  30. });
  31. constSchema=newGraphQLSchema({
  32. query:Query
  33. });
  34. module.exports=Schema;

启动服务器

  1. constExpress=require('express');
  2. constGraphHTTP=require('express-graphql');
  3. constSchema=require('./schema');
  4. //Config
  5. constAPP_PORT=3000;
  6. //Start
  7. constapp=Express();
  8. //GraphQL
  9. app.use('/graphql',GraphHTTP({
  10. schema:Schema,
  11. pretty:true,
  12. graphiql:true
  13. }));
  14. app.listen(APP_PORT,()=>{
  15. console.log(`Applisteningonport${APP_PORT}`);
转载请说明出处
知优网 » MySQL向GraphQL迁移(mysql表数据迁移)

发表评论

您需要后才能发表评论