天天看點

MySQL向GraphQL遷移

MySQL向GraphQL遷移

graphql 是一個開源的圖形資料庫(基于node.js實作), 中文文檔: https://graphql.js.cool/

sequelize-auto 将 mysql 資料庫轉變成模型

[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 資料庫結構 

使用資料模型

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

/* jshint indent: 2 */ 

module.exports = function(sequelize, datatypes) { 

  return sequelize.define('d_user', { 

    uid: { 

      type: datatypes.integer(11).unsigned, 

      allownull: false, 

      primarykey: true 

    }, 

    username: { 

      type: datatypes.string(16), 

      defaultvalue: '' 

    mobile: { 

    email: { 

      type: datatypes.string(32), 

    password: { 

    salt: { 

      type: datatypes.string(8), 

    updatedat: { 

      type: datatypes.integer(10).unsigned, 

      allownull: false 

    } 

  }, { 

    tablename: 'user' 

  }); 

};  

建立資料庫模型:

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 結構

const { graphqlobjecttype,graphqlschema,graphqllist,graphqlint,graphqlstring } = require('graphql'); 

const { attributefields, resolver } = require('graphql-sequelize'); 

const { db, user } = require('./db'); 

usertype = new graphqlobjecttype({ 

  name: 'user', 

  description: 'a user', 

  fields: attributefields(user) 

}); 

const query = new graphqlobjecttype({ 

  name: 'query', 

  description: 'root query object', 

  fields: () => { 

    return { 

      user: { 

        type: new graphqllist(usertype), 

        args: { 

          uid: { 

            type: graphqlint 

          }, 

          email: { 

            type: graphqlstring 

          } 

        }, 

        resolve(root, args) { 

          return db.models.user.findall({ where: args }); 

        } 

      } 

    }; 

  } 

const schema = new graphqlschema({ 

  query: query 

module.exports = schema;  

啟動伺服器

const express =require( 'express'); 

const graphhttp =require( 'express-graphql'); 

const schema =require( './schema'); 

// config 

const app_port = 3000; 

// start 

const app = express(); 

// graphql 

app.use('/graphql', graphhttp({ 

  schema: schema, 

  pretty: true, 

  graphiql: true 

})); 

app.listen(app_port, ()=> { 

  console.log(`app listening on port ${app_port}`);  

本文作者:佚名

來源:51cto