Model.knex(knex);
const app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(i18nextMiddleware);
Run Code Online (Sandbox Code Playgroud)
我想测试用户控制器的 getUsers 方法。它从 usersModel getUsers 方法获取数据。我通过 Objections ORM 从 MySQL 获取日期。
app.get(
'/',
checkFilter(['type']),
users.getUsers
);
Run Code Online (Sandbox Code Playgroud)
根据说明,我正在更改查询方法。
class MyQueryBuilder extends QueryBuilder {
query() {
return this.resolve({ test: 11111 });
}
}
class UsersModel extends Model {
static get QueryBuilder() {
return MyQueryBuilder;
}
}
jest.spyOn(Users, 'query')
.mockImplementation(UsersModel.query);
Run Code Online (Sandbox Code Playgroud)
描述测试。
describe('get errors', () => {
beforeAll(done => {
i18next.on('initialized', () => {
done()
});
});
it('with filter', done => {
request(app) …Run Code Online (Sandbox Code Playgroud) 从查询中我得到限制参数。如何转化为数字并通过Dto进行检查?
@Get('currency/:type')
getCurrency(
@Param() params: CurrencyTypeDto,
@Query('limit', ParseIntPipe) limit: number,
@Query() query: PaginationLimitDto
) {
Run Code Online (Sandbox Code Playgroud)
分页限制
export class PaginationLimitDto {
@IsOptional()
@IsInt()
limit: number;
}
Run Code Online (Sandbox Code Playgroud) 我想在 typeorm 中合并两个表。
第一个版本:我为每个表创建了 sql 原始查询
const tableUn = this.tableUnion.createQueryBuilder('tu')
.select('id')
.addSelect('description', 'name')
.getQuery();
const tableTg = this.tags.createQueryBuilder('tg')
.select(['id', 'name'])
.getQuery();
Run Code Online (Sandbox Code Playgroud)
使用 union 创建 slq 原始查询后:
const tags = await entityManager.query(`${ tableUn } UNION ${ tableTg }`);
Run Code Online (Sandbox Code Playgroud)
创建新实体并向其插入接收到的数据。
const createdEntity = entityManager.create(TableUnion, tags);
Run Code Online (Sandbox Code Playgroud)
最后,我想在连接查询生成器中使用这个实体,但它不起作用
const result = await connection.createQueryBuilder()
.from(createdEntity, 'cE')
.getRawMany();
Run Code Online (Sandbox Code Playgroud)
第二个版本:
const a = connection
.createQueryBuilder()
.select('*')
.from(qb => qb
.from(TableUnion, 'tu')
.select('id')
.addSelect('description', 'tu.name'),
'ttu'
)
.addFrom(qb => qb
.from(TagsEntity, 'tg')
.select('id')
.addSelect('tg.name'),
'ttg'
)
Run Code Online (Sandbox Code Playgroud)
它创建了包含两个表中的数据和列的表,但未连接
我想在 Nestjs 中使用正则表达式进行验证。
例如:
正则表达式
pagePattern = '[a-z0-9\-]+';
Run Code Online (Sandbox Code Playgroud)
方法
@Get('/:article')
getIndex(
@Param('article')
) {
}
Run Code Online (Sandbox Code Playgroud)
我可以用什么?验证管道?
我想添加 api 块的描述。
我努力了:
@ApiOperation({
description: 'Operation description'
})
Run Code Online (Sandbox Code Playgroud)
这不起作用。