{"id":7275,"date":"2024-11-14T13:26:11","date_gmt":"2024-11-14T05:26:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/7275\/"},"modified":"2024-11-14T13:26:11","modified_gmt":"2024-11-14T05:26:11","slug":"nest-%e4%b8%ad-typeorm-%e7%9a%84%e6%ad%a3%e7%a1%ae%e7%94%a8%e6%b3%95%ef%bc%9a%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3-bookentityrepository-%e5%9c%a8-appmodule-%e4%b8%ad%e4%b8%8d%e5%8f%af%e7%94%a8%e7%9a%84","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/7275\/","title":{"rendered":"Nest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173067876367011.jpg\" class=\"aligncenter\" title=\"Nest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f\u63d2\u56fe\" alt=\"Nest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>nest \u4e2d typeorm \u7684\u6b63\u786e\u7528\u6cd5<\/strong><\/p>\n<p>\u4e3a\u4e86\u5229\u7528 nest \u4e2d\u5f3a\u5927\u7684 typeorm orm\uff0c\u6b63\u786e\u96c6\u6210\u90fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63a2\u8ba8 typeorm \u7684\u4f7f\u7528\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u901a\u8fc7\u89e3\u51b3\u5e38\u89c1\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u6765\u63d0\u4f9b\u4e00\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0<\/strong><\/p>\n<p>\u4e00\u4f4d nest \u65b0\u624b\u5728\u8fd0\u884c\u65f6\u9047\u5230\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u6307\u51fa\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u7684 bookentityrepository \u672a\u5728 appmodule \u4e0a\u4e0b\u6587\u4e2d\u53ef\u7528\uff1a<\/p>\n<pre>@Module({\n    imports: [\n        TypeOrmModule.forRoot({ ... }),\n        BookModule,\n    ],\n    controllers: [BookController],\n    providers: [BookService],\n})<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p><strong>\u7b54\u6848<\/strong><\/p>\n<p>\u6839\u636e\u7ed9\u51fa\u7684\u9519\u8bef\uff0c\u8be5\u89e3\u51b3\u65b9\u6cd5\u662f\u5220\u9664 appmodule \u4e2d\u7684 controllers\uff08bookcontroller\uff09\u548c providers\uff08bookservice\uff09\u6570\u7ec4\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u6309\u9884\u671f\u89e3\u6790 bookentityrepository \u7684\u4f9d\u8d56\u9879\u3002<\/p>\n<p><strong>\u6700\u4f73\u5b9e\u8df5<\/strong><\/p>\n<p>\u9664\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5916\uff0c\u5728 nest \u4e2d\u4f7f\u7528 typeorm \u65f6\u8fd8\u5e94\u6ce8\u610f\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a<\/p>\n<ul>\n<li>\u5c06 typeormmodule.forroot() \u4ec5\u7f6e\u4e8e\u6839\u6a21\u5757\uff08appmodule\uff09\u4e2d\u3002<\/li>\n<li>\u5728\u7279\u6027\u6a21\u5757\u4e2d\u4f7f\u7528 typeormmodule.forfeature() \u5bfc\u5165\u5b9e\u4f53\u3002<\/li>\n<li>\u4f9d\u8d56\u6ce8\u5165\u5b9e\u4f53\u5b58\u50a8\u5e93\uff0c\u4f7f\u7528 @injectrepository \u88c5\u9970\u5668\uff08\u5982 bookservice.ts \u4e2d\u6240\u793a\uff09\u3002<\/li>\n<li>\u907f\u514d\u5728\u670d\u52a1\u4e2d\u76f4\u63a5\u64cd\u4f5c entitymanager \u6216 repository\uff0c\u56e0\u4e3a\u5b83\u4f1a\u7834\u574f\u4f9d\u8d56\u6ce8\u5165\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u9075\u5faa\u8fd9\u4e9b\u6700\u4f73\u5b9e\u8df5\uff0c\u4f60\u53ef\u4ee5\u786e\u4fdd typeorm \u5728 nest \u5e94\u7528\u7a0b\u5e8f\u4e2d\u5f97\u5230\u6b63\u786e\u4e14\u6709\u6548\u7684\u4f7f\u7528\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fNest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>nest \u4e2d typeorm \u7684\u6b63\u786e\u7528\u6cd5 \u4e3a\u4e86\u5229\u7528 nest \u4e2d\u5f3a\u5927\u7684 typeorm orm\uff0c\u6b63\u786e\u96c6\u6210\u90fd\u81f3\u5173\u91cd\u8981\u3002\u672c\u6587\u5c06\u63a2\u8ba8 typeorm \u7684\u4f7f\u7528\u6700\u4f73\u5b9e\u8df5\uff0c\u5e76\u901a\u8fc7\u89e3\u51b3\u5e38\u89c1\u7684\u8fd0\u884c\u65f6\u9519\u8bef\u6765\u63d0\u4f9b\u4e00\u4e2a\u95ee\u9898\u7684\u7b54\u6848\u3002 \u95ee\u9898\u63cf\u8ff0 \u4e00\u4f4d nest \u65b0\u624b\u5728\u8fd0\u884c\u65f6\u9047\u5230\u4e86\u4e00\u4e2a\u9519\u8bef\uff0c\u6307\u51fa\u4ee5\u4e0b\u4ee3\u7801\u4e2d\u7684 bookentityrepository \u672a\u5728 appmodule \u4e0a\u4e0b\u6587\u4e2d\u53ef\u7528\uff1a @Module({ imports: [ TypeOrmModule.forRoot({ &#8230; }), BookModule, ], controllers: [BookController], providers: [BookService], }) \u767b\u5f55\u540e\u590d\u5236 \u7b54\u6848 \u6839\u636e\u7ed9\u51fa\u7684\u9519\u8bef\uff0c\u8be5\u89e3\u51b3\u65b9\u6cd5\u662f\u5220\u9664 appmodule \u4e2d\u7684 controllers\uff08bookcontroller\uff09\u548c providers\uff08bookservice\uff09\u6570\u7ec4\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6a21\u5757\u6309\u9884\u671f\u89e3\u6790 bookentityrepository \u7684\u4f9d\u8d56\u9879\u3002 \u6700\u4f73\u5b9e\u8df5 \u9664\u4e86\u89e3\u51b3\u4e0a\u8ff0\u95ee\u9898\u5916\uff0c\u5728 nest \u4e2d\u4f7f\u7528 typeorm \u65f6\u8fd8\u5e94\u6ce8\u610f\u4ee5\u4e0b\u6700\u4f73\u5b9e\u8df5\uff1a \u5c06 typeormmodule.forroot() \u4ec5\u7f6e\u4e8e\u6839\u6a21\u5757\uff08appmodule\uff09\u4e2d\u3002 \u5728\u7279\u6027\u6a21\u5757\u4e2d\u4f7f\u7528 typeormmodule.forfeature() \u5bfc\u5165\u5b9e\u4f53\u3002 \u4f9d\u8d56\u6ce8\u5165\u5b9e\u4f53\u5b58\u50a8\u5e93\uff0c\u4f7f\u7528 @injectrepository [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-7275","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7275","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=7275"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/7275\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=7275"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=7275"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=7275"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}