{"id":201075,"date":"2025-05-06T12:02:28","date_gmt":"2025-05-06T04:02:28","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201075\/"},"modified":"2025-05-06T12:02:28","modified_gmt":"2025-05-06T04:02:28","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:\/\/server.hk\/cnblog\/201075\/","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><b><\/b> <\/p>\n<h1>Nest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f <\/h1>\n<p>\u6b22\u8fce\u5404\u4f4d\u5c0f\u4f19\u4f34\u6765\u5230\u4e3b\u673a\u5b9d\u8d1d\uff0c\u76f8\u805a\u4e8e\u6b64\u90fd\u662f\u7f18\u54c8\u54c8\u54c8\uff01\u4eca\u5929\u6211\u7ed9\u5927\u5bb6\u5e26\u6765\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\uff0c\u5982\u679c\u4f60\u5bf9\u76f8\u5173\u7684\u77e5\u8bc6\u975e\u5e38\u611f\u5174\u8da3\u6216\u8005\u6b63\u5728\u81ea\u5b66\uff0c\u90fd\u53ef\u4ee5\u5173\u6ce8\u6211\uff0c\u6211\u4f1a\u6301\u7eed\u66f4\u65b0\u76f8\u5173\u6587\u7ae0\uff01\u5f53\u7136\uff0c\u6709\u4ec0\u4e48\u5efa\u8bae\u4e5f\u6b22\u8fce\u5728\u8bc4\u8bba\u7559\u8a00\u63d0\u51fa\uff01\u4e00\u8d77\u5b66\u4e60\uff01<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241129\/17328796606749a52c058a1.jpg\" class=\"aligncenter\"><\/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><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>\u5230\u8fd9\u91cc\uff0c\u6211\u4eec\u4e5f\u5c31\u8bb2\u5b8c\u4e86\u300aNest \u4e2d TypeORM \u7684\u6b63\u786e\u7528\u6cd5\uff1a\u5982\u4f55\u89e3\u51b3 BookEntityRepository \u5728 AppModule \u4e2d\u4e0d\u53ef\u7528\u7684\u9519\u8bef\uff1f \u300b\u7684\u5185\u5bb9\u4e86\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u57fa\u7840\u77e5\u8bc6\u7684\u5b66\u4e60\u548c\u5de9\u56fa\uff0c\u662f\u4e3a\u4e86\u66f4\u597d\u7684\u5c06\u5176\u8fd0\u7528\u5230\u9879\u76ee\u4e2d\uff0c\u6b22\u8fce\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u5e26\u4f60\u4e86\u89e3\u66f4\u591a\u5173\u4e8e\u7684\u77e5\u8bc6\u70b9\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Nest \u4e2d TypeORM \u7684&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201075","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201075"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201075\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}