在两种方法之间共享代码的最佳方法是什么
What is the best way to mutualize code between both methods
我想知道在 javascript 中这两种方法之间共享代码的最佳解决方案是什么:
async getAllActiveRooms(ctx: ?ContextType): Promise<RoomType[]> {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true);
}
async getActiveRoomsBySessionId(ctx: ?ContextType, sessionId: number): Promise<RoomType[]> {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true)
.andWhere('sessionId',sessionId)
}
谢谢
您可以重用 getAllActiveRooms
,方法是将其 return 类型更改为扩展 Bluebird 的 promise 接口的 knex 的 QueryBuilder
。
你将丢失 RoomType[]
承诺负载类型,因为它扩展了 Bluebird<any>
来自 knex 的最新类型定义(QueryBuilder
扩展 ChainableInterface
):
interface QueryBuilder extends QueryInterface, ChainableInterface {
or: QueryBuilder;
and: QueryBuilder;
//TODO: Promise?
columnInfo(column?: string): Bluebird<ColumnInfo>;
forUpdate(): QueryBuilder;
forShare(): QueryBuilder;
toSQL(): Sql;
on(event: string, callback: Function): QueryBuilder;
}
interface ChainableInterface extends Bluebird<any> {
toQuery(): string;
options(options: any): QueryBuilder;
stream(callback: (readable: stream.PassThrough) => any): Bluebird<any>;
stream(options?: { [key: string]: any }): stream.PassThrough;
stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Bluebird<any>;
pipe(writable: any): stream.PassThrough;
exec(callback: Function): QueryBuilder;
}
async getAllActiveRooms(ctx: ?ContextType): QueryBuilder {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true);
}
async getActiveRoomsBySessionId(ctx: ?ContextType, sessionId: number): Promise<RoomType[]> {
//log getAllActiveRooms
return this.getAllActiveRooms(ctx)
.andWhere('sessionId',sessionId)
}
我想知道在 javascript 中这两种方法之间共享代码的最佳解决方案是什么:
async getAllActiveRooms(ctx: ?ContextType): Promise<RoomType[]> {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true);
}
async getActiveRoomsBySessionId(ctx: ?ContextType, sessionId: number): Promise<RoomType[]> {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true)
.andWhere('sessionId',sessionId)
}
谢谢
您可以重用 getAllActiveRooms
,方法是将其 return 类型更改为扩展 Bluebird 的 promise 接口的 knex 的 QueryBuilder
。
你将丢失 RoomType[]
承诺负载类型,因为它扩展了 Bluebird<any>
来自 knex 的最新类型定义(QueryBuilder
扩展 ChainableInterface
):
interface QueryBuilder extends QueryInterface, ChainableInterface {
or: QueryBuilder;
and: QueryBuilder;
//TODO: Promise?
columnInfo(column?: string): Bluebird<ColumnInfo>;
forUpdate(): QueryBuilder;
forShare(): QueryBuilder;
toSQL(): Sql;
on(event: string, callback: Function): QueryBuilder;
}
interface ChainableInterface extends Bluebird<any> {
toQuery(): string;
options(options: any): QueryBuilder;
stream(callback: (readable: stream.PassThrough) => any): Bluebird<any>;
stream(options?: { [key: string]: any }): stream.PassThrough;
stream(options: { [key: string]: any }, callback: (readable: stream.PassThrough) => any): Bluebird<any>;
pipe(writable: any): stream.PassThrough;
exec(callback: Function): QueryBuilder;
}
async getAllActiveRooms(ctx: ?ContextType): QueryBuilder {
//log getAllActiveRooms
return this.i.knex('users_rooms')
.transacting(ctx ? ctx.transaction : null)
.leftJoin('rooms', 'users_rooms.roomId', 'rooms.id')
.select('rooms.*')
.where('active', true);
}
async getActiveRoomsBySessionId(ctx: ?ContextType, sessionId: number): Promise<RoomType[]> {
//log getAllActiveRooms
return this.getAllActiveRooms(ctx)
.andWhere('sessionId',sessionId)
}