Node 使用 Egg 框架 之 上TS 的教程(二)
阿新 • • 發佈:2020-06-24
Node + Egg + TS + Mongodb + Resetful + graphql
本節課內容: graphql 和 egg 定時任務
執行環境: Node ,Yarn/NPM,MongoDB
樑老師又開課啦!上節課講到Node使用Mongodb上TS的一些操作,上節課的連結 )。
老規矩,本地教程的地址為:github.com/liangwei010…
egg-demo
├── app
│ ├── controller (前端的請求會到這裡來!)
│ │ └── home.ts
│ ├── model(資料庫表結構抽象出來的模型)
│ │ └── User.ts
│ ├── graphql(graphql資料夾)
│ │ └── mutation(所有的mutation宣告資料夾)
│ │ └── schema.graphql(所有的mutation宣告檔案)
│ │ └── query(所有的query宣告資料夾)
│ │ └── schema.graphql(所有的query宣告檔案)
│ │ └── user(user model的宣告和實現)
│ │ └── resolver.js(宣告函式的實現)
│ │ └── schema.graphql(user schema的欄位宣告)
│ ├── service(controller 層不建議承載過多的業務,業務重時放在service層)
│ │ └── user.ts
│ ├── schedule(定時任務資料夾)
│ │ └── addUserJob.ts
│ └── router.ts (Url的相關對映)
├── config (框架的配置檔案)
│ ├── config.default.ts
│ ├── config.local.ts
│ ├── config.prod.ts
│ └── plugin.ts
├── test (測試資料夾)
│ └── **/*.test.ts
├── typings (目錄用於放置 d.ts 檔案)
│ └── **/*.d.ts
├── README.md
├── package.json
├── tsconfig.json
└── tslint.json
複製程式碼
本次教程增加了schedule資料夾和graphql資料夾。
egg 使用 graphql
graphql 只是一種restful的一種不足的一種解決方案吧。它就是在 資料庫操作完以後,將欄位的返回權交給了使用者,意思是,使用者想返回哪個欄位就返回哪個欄位,假如說,我pc端和移動端公用一套介面,pc端需要100個欄位,移動端需要10個欄位,使用resetful無論你需不需要,都會給你返回,而graphql很好的解決了這個問題,因為選擇權在呼叫方。
是不是感覺很神奇,js和ts可以共存。此教程,先上一個js和ts版本共存的,等下期教程將使用純ts教程。使用js的問題是,在 resolver.js 中,將不能享受ts程式碼的程式碼提示和編譯檢查。但是很多專案可能都是之前存在的專案,此類js不能立馬重構或者改動。共存也是有意義的,逐步重構或者重寫吧。
// plugin.ts
const plugin: EggPlugin = {
// mongoose
mongoose: {
enable: true,package: 'egg-mongoose',},// 新增 graphql
graphql: {
enable: true ,package: 'egg-graphql',};
複製程式碼
// config.default.ts
config.graphql = {
router: '/graphql',// 是否載入到 app 上,預設開啟
app: true,// 是否載入到 agent 上,預設關閉
agent: false,// 是否載入開發者工具 graphiql,預設開啟。路由同 router 欄位。使用瀏覽器開啟該可見。
graphiql: true,};
config.middleware = ['graphql'];
複製程式碼
使用user舉例: 定義User schema 的返回欄位
// schema
type User {
_id: String
userNo: String
userName: String
}
複製程式碼
// 查詢所有宣告
type Query {
// 函式名字為user 返回為 User的陣列
user: [User]
}
// Mutation 所有宣告
type Mutation {
// 函式名字為user 返回為 User物件
user: User
}
複製程式碼
Mutation 和 Query 宣告函式的實現處:
'use strict';
module.exports = {
Query: {
async user(root,{ },ctx) {
return await ctx.model.User.find();
},Mutation: {
async user(root,ctx) {
return await ctx.model.User.create({userName: 'add user',userNo: 99});
},}
}
複製程式碼
然後開啟瀏覽器輸入:http://127.0.0.1:7001/graphql,沒病,查詢一個瞧瞧!!
egg 定時任務
定時任務和我們一般定時任務差不多。定時任務一般分兩種:
- 一種是 間隔若干時間執行 某個任務
- 另一種是 某個時間點執行 某個任務
- 間隔若干時間執行(每隔60s將會執行一次)
static get schedule() {
return {
interval: '60s',// 60s 間隔
type: 'all',// 指定所有的 worker 都需要執行
};
}
async subscribe() {
const ctx = this.ctx;
console.log('每60s執行一次增加User的定時任務!!' + new Date())
const test = await ctx.service.user.addUserByScheduleTest();
console.log(test)
}
複製程式碼
- 某個時間點執行(每個月的15號:00:00 分執行)
static get schedule() {
return {
cron: '0 0 0 15 * *',// 每個月的15號:00:00 分執行
type: 'worker',// 只指定一個隨機程式執行job 防止出現資料衝突
disable: false,// 是否開啟
cronOptions: {
tz: 'Asia/Shanghai',};
}
async subscribe () {
const ctx = this.ctx;
console.log('每個月的15號:00:00 分執行!!' + new Date())
}
複製程式碼
時間的配置請看:egg的官方檔案。又挺晚啦,下節課給大家上graphql 的ts版本。