Skip to content

Commit c0e8f0f

Browse files
committed
add multiple database; middleware optimization
1 parent 08a48aa commit c0e8f0f

36 files changed

+454
-126
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,7 @@ conf/db.conf.ts
1616
npm-debug.log*
1717
yarn-debug.log*
1818
yarn-error.log*
19+
20+
21+
*.suo
22+
.gitkeep

conf/server.conf.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11

22
const _PROD_: boolean = process.env.NODE_ENV === 'production'
33

4-
let PORT: number = 8020
5-
6-
if(_PROD_) {
7-
PORT = 8021
8-
}
4+
let PORT = _PROD_ ? 8021 : 8020
95

106
export {
117
PORT

nodemon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"watch": ["src", "conf"],
33
"ext": "ts,js",
44
"ignore": ["src/**/*.spec.ts"],
5-
"exec": "ts-node conf/server.ts",
5+
"exec": "ts-node src/server.ts",
66
"delay": 300
77
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"dependencies": {
2222
"@types/ioredis": "^4.0.3",
2323
"axios": "^0.18.0",
24+
"dataloader": "^2.0.0",
2425
"graphql": "^14.0.2",
2526
"ioredis": "^4.2.0",
2627
"koa": "^2.5.3",

src/@types/$http.ts

Whitespace-only changes.

src/@types/global.d.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
export {}
3+
4+
declare global {
5+
export interface IAnyObject {
6+
[key: string]: any
7+
}
8+
}

src/controllers/AccountController.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ import Store from "../utils/session/store";
66
import { JWT_SECRET, EXP_TIME } from '../constants'
77
import { sign } from '../core/jwt/sign'
88
import { cryptoPwd } from "../utils/tools"
9+
import { getBlogManager } from '../database/dbUtils';
910

10-
const store = new Store
1111

1212
export default class AccountController {
1313

@@ -17,7 +17,7 @@ export default class AccountController {
1717
let username = inputs.username;
1818
let password = inputs.password;
1919
if ((username && username.length > 0) && (password && password.length > 5)) {
20-
const result = await getManager().findOne(User, {
20+
const result = await getBlogManager().findOne(User, {
2121
select: ['id', 'username', 'nickName', 'sex', 'userType'],
2222
where: {
2323
username: username,
@@ -26,7 +26,7 @@ export default class AccountController {
2626
});
2727
if(result) {
2828
const token = sign({ ...result, exp: EXP_TIME }, JWT_SECRET)
29-
await store.set('true', {
29+
await Store.set('true', {
3030
sid: token,
3131
maxAge: EXP_TIME // millisecond
3232
})
@@ -43,7 +43,7 @@ export default class AccountController {
4343
static async logout(ctx: Context) {
4444
const tokens = ctx.header['authorization']
4545
const token = tokens.split(' ')[1]
46-
await store.destroy(token)
46+
await Store.destroy(token)
4747
ctx.Json({ data: 1, msg: '退出成功!' });
4848
}
4949

src/controllers/ArticleController.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import {getManager, getRepository, Equal, Like, Between, FindManyOptions} from "typeorm";
1+
import { Equal, Like, Between, FindManyOptions} from "typeorm";
22
import { Context } from '@core/koa'
33
import { Article } from '../entities/mysql/article'
44
import { Guid } from "../utils/tools";
55
import * as Moment from 'moment'
6+
import { getBlogManager, getBlogRepository } from '../database/dbUtils';
67

78

89
export default class ArticleController {
910

1011
static async getAll(args: any) {
1112
console.log(args)
12-
return await getManager().find(Article);
13+
return await getBlogManager().find(Article);
1314
}
1415

1516

1617
static async getById(id: string = '') {
17-
const article = await getRepository(Article).findOne({id})
18+
const article = await getBlogRepository(Article).findOne({id})
1819
// console.log('article: ', article)
1920
return article
2021
}
@@ -47,7 +48,7 @@ export default class ArticleController {
4748
}
4849
console.log(options, '----options')
4950

50-
const pages = await getRepository(Article).findAndCount(options)
51+
const pages = await getBlogRepository(Article).findAndCount(options)
5152
// .createQueryBuilder()
5253
// .where({
5354
// // title: Like(args.title)
@@ -74,7 +75,7 @@ export default class ArticleController {
7475
model.createdAt = Date.now()
7576
model.updatedBy = ctx.state['CUR_USER'].id
7677
model.updatedAt = Date.now()
77-
const result = await getRepository(Article).save(model)
78+
const result = await getBlogRepository(Article).save(model)
7879
return result
7980
}
8081

@@ -89,7 +90,7 @@ export default class ArticleController {
8990
article.tag = args.tag
9091
article.updatedAt = Date.now()
9192
article.updatedBy = ctx.state['CUR_USER'].id
92-
const result = await getRepository(Article).save(article)
93+
const result = await getBlogRepository(Article).save(article)
9394
return result
9495
}
9596

src/controllers/ArticleTypeController.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
import {getManager, getRepository, Like, Between, FindManyOptions} from "typeorm";
1+
import { Like, Between, FindManyOptions} from "typeorm";
22
import { Context } from '@core/koa'
33
import { ArticleType } from '../entities/mysql/articleType'
44
import { Guid } from "../utils/tools";
55
import * as Moment from 'moment'
6+
import { getBlogManager, getBlogRepository } from '../database/dbUtils';
67

78

89
export default class ArticleController {
910

1011
static async getAll(args: any) {
11-
return await getManager().find(ArticleType);
12+
return await getBlogManager().find(ArticleType);
1213
}
1314

1415

1516
static async getById(id: string = '') {
1617
// getManager().findOne()
17-
const articleType = await getRepository(ArticleType).findOne({id})
18+
const articleType = await getBlogRepository(ArticleType).findOne({id})
1819
return articleType
1920
}
2021

@@ -38,7 +39,7 @@ export default class ArticleController {
3839
options.order = Object.assign(options.order, args.order)
3940
}
4041
console.log(options, '----options')
41-
const pages = await getRepository('articleType').findAndCount(options)
42+
const pages = await getBlogRepository('articleType').findAndCount(options)
4243
// .createQueryBuilder()
4344
// .orderBy({createdAt: 'DESC'})
4445
// .offset(args.page < 2 ? 0 : (args.page - 1) * args.pageSize)
@@ -57,7 +58,7 @@ export default class ArticleController {
5758
model.createdBy = ctx.state['CUR_USER'].id
5859
model.updatedAt = Date.now()
5960
model.updatedBy = ctx.state['CUR_USER'].id
60-
const result = await getRepository(ArticleType).save(model)
61+
const result = await getBlogRepository(ArticleType).save(model)
6162
return result
6263
}
6364

@@ -68,7 +69,7 @@ export default class ArticleController {
6869
model.remark = args.remark
6970
model.updatedAt = Date.now()
7071
model.updatedBy = ctx.state['CUR_USER'].id
71-
const result = await getRepository(ArticleType).update(args.id, model)
72+
const result = await getBlogRepository(ArticleType).update(args.id, model)
7273
console.log('result:', result)
7374
return result
7475
}

src/controllers/CommentController.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
import {getManager, getRepository, Like, Between, FindManyOptions} from "typeorm";
1+
import { Like, Between, FindManyOptions} from "typeorm";
22
import { Context } from '@core/koa'
33
import { Comment } from '../entities/mysql/comment'
44
import { Guid } from "../utils/tools";
55
import * as Moment from 'moment'
6+
import { getBlogManager, getBlogRepository } from '../database/dbUtils';
67

78
export default class CommentController {
89

910
static async getAll(args: any) {
10-
return await getManager().find(Comment);
11+
return await getBlogManager().find(Comment);
1112
}
1213

1314

1415
static async getById(id: string = '') {
15-
const article = await getRepository(Comment).findOne({id})
16+
const article = await getBlogRepository(Comment).findOne({id})
1617
return article
1718
}
1819

@@ -35,7 +36,7 @@ export default class CommentController {
3536
if(args.order) {
3637
options.order = Object.assign(options.order, args.order)
3738
}
38-
const pages = await getRepository('comment').findAndCount(options)
39+
const pages = await getBlogRepository('comment').findAndCount(options)
3940
return pages
4041
}
4142

0 commit comments

Comments
 (0)