首页 > 其他分享 >typmorm 类似ef的懒加载

typmorm 类似ef的懒加载

时间:2024-04-14 18:34:46浏览次数:22  
标签:false typmorm Column ef eager user Post 加载


typmorm 类似ef的懒加载

在TypeORM中,可以通过设置关系的eager: false属性来实现类似于Entity Framework Core(EF)的懒加载功能。这意味着关联的实体将默认不会立即加载,而是在访问它们时才加载。

以下是一个简单的例子,演示如何在TypeORM中实现懒加载:

    import { Entity, PrimaryGeneratedColumn, Column, OneToMany, ManyToOne, ManyToMany, JoinTable, RelationId, CreateDateColumn, UpdateDateColumn } from 'typeorm';   @Entity() export class User { @PrimaryGeneratedColumn() id: number;   @Column() name: string;   @OneToMany(() => Post, post => post.user, { eager: false }) // 懒加载 posts: Post[]; }   @Entity() export class Post { @PrimaryGeneratedColumn() id: number;   @Column() title: string;   @Column() content: string;   @ManyToOne(() => User, user => user.posts, { eager: false }) // 懒加载 user: User; }

在上述代码中,User实体有一个posts属性,它是Post的一个OneToMany关系。通过将eager属性设置为false,当你访问user.posts时,Posts数据才会被加载。这就是实现懒加载的方法。

要注意的是,懒加载只有在查询关联数据时才会生效。如果你在创建查询时已经加载了关联数据(例如通过join或者preload方法),则懒加载不会起作用。

标签:false,typmorm,Column,ef,eager,user,Post,加载
From: https://www.cnblogs.com/sexintercourse/p/18134495

相关文章

  • vue3在构建时,使用魔法糖语法时defineProps和defineEmits的注意事项
    在Vue3.2+版本中,可以使用<scriptsetup>替代传统的script标签来编写组件,它提供了更简洁的语法来编写CompositionAPI代码。在<scriptsetup>中,使用defineProps和defineEmits时需要注意:如果显式地导入defineProps时,在编译时会提示以下wanning<scriptsteup>impo......
  • 【官方题解】Codeforces Round 939 (Div. 2)
    CodeforcesRoundAyachiNeneSolutions(div.2)A.Nene'sGameIdea:[user:Otomachi_Una]Solution不难发现如果一个人初始位置\(p\geqa_1\),那么必然会在有限次回合当中被踢出。答案即为\(\min(n,a_1-1)\)。B.NeneandtheCardGameIdea:[user:Otomachi_Una]Hint......
  • Reflective Journal II
    1.Inthefirstplace,Ithoughtofmygrandpawhenitcometothepersonwhohasagreatinfluenceonme.Therefore,Ichosemygrandpaasthemainroleinmypresentationanddividedfourpointstodescribedetails.AfterfinishingthePPT,Ibegantoge......
  • Redefine library-自定义函数库
    1.jjVolcano_Redefinelibrary(scRNAtoolVis)#jjVolcano只有9个颜色,Redefine到我的24个颜色,并与我umap中的分群颜色对应jjVolcano_Redefine<-function(diffData=NULL,myMarkers=NULL,order.by=c("avg_log2FC"),log2FC.cutoff=0.......
  • 图片懒加载不完全指南
    图片懒加载不完全指南菜菜菜鸡 1人赞同了该文章图片懒加载在日常开发中,我们常用的两种图片加载方式如下:使用 img 标签加载图片;使用 cssbackground 加载图片。在这篇文章中,您将了解如何延迟加载这两种类型的图像。img标签图片懒加载对......
  • JavaScript判断图片是否已经加载完毕的方法汇总_javascript技巧
    JavaScript判断图片是否已经加载完毕的方法汇总_javascript技巧 在网上有很多关于判断图片是否已经加载完毕的文章,但是有的浏览器并不适合,下面小编给大家分享一些有关JavaScript判断图片是否已经加载完毕方法汇总,具体内容如下所示:一.onload事件通过监听图片的onload事件,可......
  • React.js 网站开发:实现滚动加载动画
    React.js网站开发:实现滚动加载动画极客前端探索者前沿技术的探索者,编码艺术的实践者 最近在开发官网的过程中,涉及到UI动画的制作,其中滚动效果的使用比较频繁,特此整理一下,以便查询和温习。平滑向上过渡动画这种往下滚动过渡渐变显示的动画是最常......
  • Reflective Journal 2
    First,Ichoseadescriptionofsomeoneimportanttome.Itellourstoryinchronologicalorder.Atthesametime,Ihaveattachedsomephotosandvideosofustomakethecharacterizationmorevividanddelicate.Intheproductionofmypowerpoint,Imobili......
  • Reflective Journal II
    1.Firstly,IdecidedwhoIwantedtowriteabout,mymom,consideringtheloveandcomplexrelationshipbetweenus.Then,IfoundasuitablematerialtoputwhatIwantedtoexpressintoit,suchasthephotosofmymomandme,thetopicswetalkedabout,thebea......
  • Reflective Journal II
    Whenitcomestothepeoplewhohaveinfluencedme,IimmediatelythinkofmygoodfriendTang.Firstofall,Ilistedthefoursectionstobeintroduced,thensearchedforphotosoftherelevantcontent,andrecalledthestorybetweenus.Afterfinishing......