我想为我的 Nestjs“课程”存储库服务(依赖于 Mongoose 模型和 Redis 的服务)编写一个单元测试。
课程.存储库.ts:
import { Injectable, HttpException, NotFoundException } from "@nestjs/common";
import { InjectModel } from "@nestjs/mongoose"
import { Course } from "../../../../shared/course";
import { Model } from "mongoose";
import { RedisService } from 'nestjs-redis';
@Injectable({})
export class CoursesRepository {
private redisClient;
constructor(
@InjectModel('Course') private courseModel: Model<Course>,
private readonly redisService: RedisService,
) {
this.redisClient = this.redisService.getClient();
}
async findAll(): Promise<Course[]> {
const courses = await this.redisClient.get('allCourses');
if (!courses) {
console.log('return from DB');
const mongoCourses = await …Run Code Online (Sandbox Code Playgroud) 我定义了一个人和故事模式:
@Schema()
export class Person extends Document {
@Prop()
name: string;
}
export const PersonSchema = SchemaFactory.createForClass(Person);
@Schema()
export class Story extends Document {
@Prop()
title: string;
@Prop()
author: { type: MongooseSchema.Types.ObjectId , ref: 'Person' }
}
export const StorySchema = SchemaFactory.createForClass(Story);
Run Code Online (Sandbox Code Playgroud)
在我的服务中,我实现了保存和读取功能:
async saveStory(){
const newPerson = new this.personModel();
newPerson.name = 'Ian Fleming';
await newPerson.save();
const newStory = new this.storyModel();
newStory.title = 'Casino Royale';
newStory.author = newPerson._id;
await newStory.save();
}
async readStory(){
const stories = await this.storyModel.
findOne({ title: …Run Code Online (Sandbox Code Playgroud) 有没有办法在 Laravel(MySQL 数据库)中批量插入(插入或更新,如果存在)记录?
假设我有一张桌子:
[id:1 value:4]
[id:2 value:5]
Run Code Online (Sandbox Code Playgroud)
我想执行:
Model::massupsert([
[id:1 value:100],
[id:3 value:20]
]);
Run Code Online (Sandbox Code Playgroud)
之后的表格是:
[id:1 value:100]
[id:2 value:5]
[id:3 value:20]
Run Code Online (Sandbox Code Playgroud) 我的主题中有一个给定的颜色常量,比如说“#FFBA60”。
我想使用 React Material UI 函数从其他一些基本颜色中派生出来。(假设一个亮 30%,一个暗 10%)。
有这个功能吗?(类似于 Sass brighten('#FFBA60,30%))
我有一个TextInput组件调用'OnChangeText'事件:
<TextInput onChangeText={(text) => this.setState({zip:text})}/>
Run Code Online (Sandbox Code Playgroud)
这很好.
现在我想将call事件更改为函数:
<TextInput onChangeText={_handleTextChange}/>
_handleTextChange(event) {
this.setState({zip:
event.nativeEvent.text});
}
Run Code Online (Sandbox Code Playgroud)
这不起作用.我收到以下错误:
undefined不是对象(评估'event.nativeEvent.text'
如何将'Event'对象发送到_handleTextChange函数?
谢谢你,亚伦
我想使用一个字段中的先前值在模型上运行更新查询。
这会将模型(行ID = 4)的“ seq”字段更新为5。
Model.update({
seq: 5
},{
where:{
'id':4,
}
});
Run Code Online (Sandbox Code Playgroud)
现在如何将模型更新为存储在“ seq”字段中的先前值+ 5?
Model.update({
seq: 'seq' + 5
},{
where:{
'id':4,
}
});
Run Code Online (Sandbox Code Playgroud) nestjs ×2
node.js ×2
reactjs ×2
eloquent ×1
jestjs ×1
laravel ×1
material-ui ×1
mongoose ×1
react-native ×1
sequelize.js ×1
typescript ×1
unit-testing ×1