Skip to content

Commit 44367ac

Browse files
committed
解决类型声明问题
1 parent 8b8d6df commit 44367ac

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

src/components/MysqlSafeTransaction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export class MysqlSafeTransaction {
1313

1414
async safeTransaction<T>(handler: (trx: CoaMysql.Transaction) => Promise<T>): Promise<T> {
1515
let clearCacheNsps: any[] = []
16-
const result = await this.bin.io.transaction(async (trx: any) => {
16+
const result = await this.bin.io.transaction(async (trx: CoaMysql.Transaction) => {
1717
trx.__isSafeTransaction = true
1818
trx.clearCacheNsps = []
1919

src/services/MysqlCache.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
7070
}
7171

7272
async getIdBy(field: string, value: string | number, trx?: CoaMysql.Transaction) {
73-
return (trx as any).__isSafeTransaction ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(this.getCacheNsp('index', field), '' + value, async () => await super.getIdBy(field, value, trx))
73+
return trx?.__isSafeTransaction ? await super.getIdBy(field, value, trx) : await this.redisCache.warp(this.getCacheNsp('index', field), '' + value, async () => await super.getIdBy(field, value, trx))
7474
}
7575

7676
async mGetByIds(ids: string[], pick = this.pick, trx?: CoaMysql.Transaction, ms = this.ms, force = false) {
77-
const result = (trx as any).__isSafeTransaction ? await super.mGetByIds(ids, this.columns, trx) : await this.redisCache.mWarp(this.getCacheNsp('id'), ids, async ids => await super.mGetByIds(ids, this.columns, trx), ms, force)
77+
const result = trx?.__isSafeTransaction ? await super.mGetByIds(ids, this.columns, trx) : await this.redisCache.mWarp(this.getCacheNsp('id'), ids, async ids => await super.mGetByIds(ids, this.columns, trx), ms, force)
7878
_.forEach(result, (v, k) => {
7979
result[k] = this.pickResult(v, pick)
8080
})
@@ -88,23 +88,23 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
8888

8989
protected async findListCount(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
9090
const cacheId = 'list-count:' + secure.sha1($.sortQueryString(...finger))
91-
return (trx as any)?.__isSafeTransaction ? await super.selectListCount(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectListCount(query, trx))
91+
return trx?.__isSafeTransaction ? await super.selectListCount(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectListCount(query, trx))
9292
}
9393

9494
protected async findIdList(finger: Array<CoaMysql.Dic<any>>, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
9595
const cacheId = 'list:' + secure.sha1($.sortQueryString(...finger))
96-
return (trx as any)?.__isSafeTransaction ? await super.selectIdList(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdList(query, trx))
96+
return trx?.__isSafeTransaction ? await super.selectIdList(query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdList(query, trx))
9797
}
9898

9999
protected async findIdSortList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
100100
const cacheId = `sort-list:${pager.rows}:${pager.last}:` + secure.sha1($.sortQueryString(...finger))
101-
return (trx as any)?.__isSafeTransaction ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdSortList(pager, query, trx))
101+
return trx?.__isSafeTransaction ? await super.selectIdSortList(pager, query, trx) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdSortList(pager, query, trx))
102102
}
103103

104104
protected async findIdViewList(finger: Array<CoaMysql.Dic<any>>, pager: CoaMysql.Pager, query: CoaMysql.Query, trx?: CoaMysql.Transaction) {
105105
const cacheId = `view-list:${pager.rows}:${pager.page}:` + secure.sha1($.sortQueryString(...finger))
106106
const count = await this.findListCount(finger, query, trx)
107-
return (trx as any)?.__isSafeTransaction ? await super.selectIdViewList(pager, query, trx, count) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdViewList(pager, query, trx, count))
107+
return trx?.__isSafeTransaction ? await super.selectIdViewList(pager, query, trx, count) : await this.redisCache.warp(this.getCacheNsp('data'), cacheId, async () => await super.selectIdViewList(pager, query, trx, count))
108108
}
109109

110110
protected async mGetCountBy(field: string, ids: string[], trx?: CoaMysql.Transaction) {
@@ -114,7 +114,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
114114
_.forEach(rows, ({ id, count }) => (result[id] = count))
115115
return result
116116
}
117-
const result = (trx as any)?.__isSafeTransaction ? await queryFunction() : await this.redisCache.mWarp(this.getCacheNsp('count', field), ids, queryFunction)
117+
const result = trx?.__isSafeTransaction ? await queryFunction() : await this.redisCache.mWarp(this.getCacheNsp('count', field), ids, queryFunction)
118118
return result
119119
}
120120

@@ -125,7 +125,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
125125
const rows = await qb
126126
return (rows[0]?.count as number) || 0
127127
}
128-
const result = (trx as any)?.__isSafeTransaction ? await queryFunction() : await this.redisCache.warp(this.getCacheNsp('count', field), value, queryFunction)
128+
const result = trx?.__isSafeTransaction ? await queryFunction() : await this.redisCache.warp(this.getCacheNsp('count', field), value, queryFunction)
129129
return result
130130
}
131131

@@ -154,7 +154,7 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
154154

155155
async deleteCache(ids: string[], dataList: Array<CoaMysql.SafePartial<Scheme>>, trx?: CoaMysql.Transaction) {
156156
const deleteIds = [] as CoaRedis.CacheDelete[]
157-
if ((trx as any)?.__isSafeTransaction) {
157+
if (trx?.__isSafeTransaction) {
158158
(trx as any)?.clearCacheNsps.push([this.getCacheNsp('id'), ids]);
159159
(trx as any)?.clearCacheNsps.push([this.getCacheNsp('data'), []])
160160
} else {
@@ -172,10 +172,10 @@ export class MysqlCache<Scheme> extends MysqlNative<Scheme> {
172172
})
173173
ids.push(...keys.slice(1))
174174
if (ids.length) {
175-
((trx as any)?.__isSafeTransaction) ? (trx as any)?.clearCacheNsps.push([this.getCacheNsp(name, key), ids]) : deleteIds.push([this.getCacheNsp(name, key), ids])
175+
(trx?.__isSafeTransaction) ? (trx as any)?.clearCacheNsps.push([this.getCacheNsp(name, key), ids]) : deleteIds.push([this.getCacheNsp(name, key), ids])
176176
}
177177
})
178178
})
179-
if (!(trx as any)?.__isSafeTransaction) await this.redisCache.mDelete(deleteIds)
179+
if (!trx?.__isSafeTransaction) await this.redisCache.mDelete(deleteIds)
180180
}
181181
}

src/typings.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@ export namespace CoaMysql {
77
export type SafePartial<T> = T extends {} ? Partial<T> : any
88
export type Query = (qb: Knex.QueryBuilder) => void
99
export type QueryBuilder = Knex.QueryBuilder
10-
export type Transaction = Knex.Transaction
10+
export interface Transaction extends Knex.Transaction {
11+
__isSafeTransaction?: boolean
12+
clearCacheNsps?: any[]
13+
}
1114
export interface Pager {
1215
rows: number
1316
last: number

0 commit comments

Comments
 (0)