自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

MySQL向GraphQL遷移

大數(shù)據(jù)
GraphQL 是一個開源的圖形數(shù)據(jù)庫(基于Node.js實現(xiàn)), sequelize-auto 將 MySQL 數(shù)據(jù)庫轉(zhuǎn)變成模型。

MySQL向GraphQL遷移

GraphQL 是一個開源的圖形數(shù)據(jù)庫(基于Node.js實現(xiàn)), 中文文檔: https://graphql.js.cool/

sequelize-auto 將 MySQL 數(shù)據(jù)庫轉(zhuǎn)變成模型

  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 參數(shù): -h, --host 主機地址 [必須] -d, --database 數(shù)據(jù)名 [必須] -u, --user 用戶名 -x, --pass 密碼 -p, --port 端口號 -c, --config 配置文件,參考: https://sequelize.readthedocs.org/en/latest/api/sequelize/ -o, --output 輸出目錄 -e, --dialect 數(shù)據(jù)庫引擎: postgres, mysql, sqlite -t, --tables 需要導入的表 -T, --skip-tables 需要排除的表 -C, --camel 使用用駝峰命名法 -n, --no-write 不需要寫入文件 -s, --schema 數(shù)據(jù)庫結(jié)構(gòu) 

使用數(shù)據(jù)模型

這里是生成的一個示例模型:

  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. };  

創(chuàng)建數(shù)據(jù)庫模型:

  1. const Sequelize = require('sequelize'); const Db = new Sequelize('數(shù)據(jù)庫名''用戶名''密碼', { 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 轉(zhuǎn)換 MySQL -> GraphQL 結(jié)構(gòu)

  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大數(shù)據(jù)
相關(guān)推薦

2013-01-06 09:43:35

MySQLMySQL遷移Redis

2010-01-13 17:24:34

SQL Server遷

2009-03-09 16:27:17

數(shù)據(jù)遷移PHPOracle

2010-11-10 09:03:27

云計算遷移

2012-07-31 09:55:53

云計算

2011-06-09 10:36:51

IPv6IPv6部署

2015-11-24 17:46:42

云遷移IT基礎設施云服務

2016-01-29 10:26:47

云端云遷移

2015-09-09 15:16:19

混合云云遷移

2020-12-02 10:35:09

云端災難恢復云遷移

2012-06-12 09:13:14

2011-08-11 18:10:58

iCoremail云計算企業(yè)郵箱

2016-08-05 15:04:33

javascripthtmljs

2012-08-01 10:08:10

云計算云遷移

2014-06-06 09:52:20

802.11acWi-Fi

2023-12-15 16:45:22

微軟Azure遷移

2011-05-04 13:11:29

Exchange

2020-09-28 06:57:39

Node.jsGraphQLAPI

2021-08-12 13:35:35

云計算云服務安全

2012-10-18 09:54:22

點贊
收藏

51CTO技術(shù)棧公眾號