{"id":345821,"date":"2023-02-23T21:02:57","date_gmt":"2023-02-23T21:02:57","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=345821"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=345821","title":{"rendered":"<span>\u0421\u0442\u0430\u0440\u0442\u0443\u0435\u043c \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441 \u043d\u0430 Node.js + fastify + Typescript + prisma + mongodb + grpc<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441. \u0412\u043e\u0442 \u0438 \u0443 \u043c\u0435\u043d\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e  \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c. \u0410 \u0432\u0435\u0434\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0438 \u0447\u0435\u0433\u043e-\u0442\u043e \u043d\u043e\u0432\u0435\u043d\u044c\u043a\u043e\u0433\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c. <\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0431\u044b\u043b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0441\u0442\u0435\u043a \u0438 \u0445\u043e\u0442\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u043d\u0435 \u043d\u043e\u0432\u044b\u0439, \u043d\u043e \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0438\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b.<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<h2>\u0428\u0430\u0433 1. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 TS<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c typescript \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 \u0442\u0430\u0439\u043f\u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u0441 \u043d\u0443\u043b\u044f &#8212; \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439. \u0412 \u044d\u0442\u043e\u043c \u043f\u043b\u0430\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u0435\u043d nest, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0430\u043c \u0438\u0434\u0435\u0442 \u0432\u0441\u0435 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438, \u043d\u043e \u043e\u043d \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u044f\u0436\u0435\u043b\u044b\u0439 \u0438 \u0441\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0443\u0436\u0435 \u043a\u0430\u043a \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u043e \u043d\u0435 \u043d\u0430\u0448 \u043f\u0443\u0442\u044c.<\/p>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435<\/p>\n<pre><code class=\"bash\">npm init -y yarn add typescript tsconfig-paths ts-node @types\/node nodemon concurrently --dev mkdir src &amp;&amp; touch src\/index.ts npx tsc --init --rootDir src --outDir build --esModuleInterop --resolveJsonModule --module commonjs --allowJs true --noImplicitAny true --target esnext --moduleResolution node touch .gitignore &amp;&amp; echo \"node_modules \\n.vscode\\nbuild\" >> .gitignore touch nodemon.json echo \"console.log('Hello world')\" >> src\/index.ts<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <strong>typescript.json<\/strong>, \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c, \u0435\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439<\/p>\n<pre><code class=\"json\">{   \"compilerOptions\": {...},   \"include\": [\"src\"],   \"exclude\": [     \"node_modules\",     \"dist\",     \"examples\",   ], } <\/code><\/pre>\n<p>\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u0435\u043a\u0446\u0438\u044e <strong>scripts \u0432 package.json<\/strong><\/p>\n<pre><code class=\"json\">  \"scripts\": {     \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\",     \"build\": \"tsc\",     \"dev\": \"export NODE_ENV=development TS_NODE_BASEURL=.\/dist &amp;&amp; concurrently -k -p \\\"[{name}]\\\" -n \\\"TypeScript,App\\\" -c \\\"yellow.bold,cyan.bold\\\" \\\"tsc -w\\\" \\\"nodemon\\\"\"   },<\/code><\/pre>\n<p>\u0414\u043b\u044f dev \u0440\u0435\u0436\u0438\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <strong>nodemon<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u0432 <strong>nodemon.json<\/strong><\/p>\n<pre><code class=\"json\">{   \"ignore\": [     \"**\/*.test.ts\",     \"**\/*.spec.ts\",     \".git\",     \"node_modules\"   ],   \"watch\": [     \"src\"   ],   \"exec\": \"tsc &amp;&amp; node -r tsconfig-paths\/register -r ts-node\/register build\/index.js\",   \"ext\": \"ts, js\" }<\/code><\/pre>\n<p>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u044d\u0442\u043e\u043c \u0448\u0430\u0433\u0435, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435<\/p>\n<pre><code class=\"bash\">yarn dev  [App] [nodemon] starting `tsc &amp;&amp; node -r tsconfig-paths\/register -r ts-node\/register build\/index.js` [App] Hello world [App] [nodemon] clean exit - waiting for changes before rest<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f, \u0431\u0430\u0437\u043e\u0432\u0430\u044f, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u043e\u0442\u043e\u0432\u0430.<\/p>\n<h2>\u0428\u0430\u0433 2. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 Fastify<\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u043f\u0435\u0440\u0432\u0430 \u043e\u0442\u0432\u0435\u0447\u0443 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441: &#171;\u041f\u043e\u0447\u0435\u043c\u0443 fastify ? &#171;<\/p>\n<p>Fastify \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0435\u0433\u043a\u0438\u0439 \u0438 \u043d\u0430 \u043d\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c REST. \u0423 \u043d\u0435\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438 \u043d\u0435\u0442.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u043a\u0435\u0442 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<pre><code class=\"bash\">yarn add fastify touch src\/app.ts<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <strong>app.ts<\/strong>.<\/p>\n<pre><code class=\"typescript\">import Fastify, { FastifyServerOptions } from 'fastify'  export type AppOptions = Partial&lt;FastifyServerOptions>;  async function buildApp(options: AppOptions = {}) {   const fastify = Fastify(options);    return fastify; }  export { buildApp }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 <strong>index.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { buildApp, AppOptions } from '.\/app';  const options: AppOptions = {   logger: true, };  const start = async () => {   const app = await buildApp(options);    try {     await app.listen({       port: 3000,       host: 'localhost',     });    } catch (err) {     app.log.error(err);     process.exit(1);   } };  start();<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 dev \u0440\u0435\u0436\u0438\u043c\u0435, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"bash\">yarn dev  [App] {\"level\":30,\"time\":1676434938643,\"pid\":39600,\"hostname\":\"Anatolys-MacBook-Pro.local\",\"msg\":\"Server listening at http:\/\/[::1]:3000\"}<\/code><\/pre>\n<h2>\u0428\u0430\u0433 3. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 prisma<\/h2>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f Mongodb. \u0418 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 Prisma ?<br \/>\u0412\u043f\u0435\u0440\u0432\u044b\u0435, \u043d\u0435 \u0442\u0430\u043a \u0434\u0430\u0432\u043d\u043e, \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b prisma \u0438 \u0431\u044b\u043b \u0432 \u0432\u043e\u0441\u0442\u043e\u0440\u0433\u0435 &#8212; \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0448\u044c \u0441\u0445\u0435\u043c\u0443, \u0430 \u043e\u043d\u0430 \u0441\u0430\u043c\u0430 \u0443\u0436\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0442 \u0442\u043e\u043d\u043d\u044b \u043b\u043e\u0433\u0438\u043a\u0438, \u0447\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u0411\u0414.<\/p>\n<p>\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u043d\u0433\u0443 \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u0432 \u0434\u043e\u043a\u0435\u0440 \u0445\u0430\u0431\u0435 <a href=\"https:\/\/hub.docker.com\/_\/mongo\" rel=\"noopener noreferrer nofollow\">https:\/\/hub.docker.com\/_\/mongo<\/a>.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u043e\u043d\u0433\u0430 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <code>mongodb:\/\/localhost:27017<\/code><\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b<\/p>\n<pre><code class=\"bash\">yarn add prisma @prisma\/client fastify-plugin npx prisma init --datasource-provider mongodb<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0445\u0435\u043c\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 <strong>prisma\/schema.prisma<\/strong><\/p>\n<pre><code class=\"javascript\">generator client {   provider = \"prisma-client-js\" }  datasource db {   provider = \"mongodb\"   url      = \"mongodb:\/\/localhost:27017\/example\" }  model Post {   id       String    @id @default(auto()) @map(\"_id\") @db.ObjectId   slug     String    @unique   title    String   body     String   author   User      @relation(fields: [authorId], references: [id])   authorId String    @db.ObjectId }  model User {   id      String   @id @default(auto()) @map(\"_id\") @db.ObjectId   email   String   @unique   name    String?   address Address?   posts   Post[] }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u043d\u0430\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0430\u043c\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u0441\u0435\u043a\u0446\u0438\u044e <strong>scrips &#8212; package.json<\/strong><\/p>\n<pre><code class=\"bash\">yarn prisma generate npx prisma db push<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043f\u0440\u0438\u0437\u043c\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u043f\u043b\u0430\u0433\u0438\u043d\u044b fastify <code>touch src\/prisma.plugin.ts<\/code><\/p>\n<pre><code class=\"typescript\">import { PrismaClient } from '@prisma\/client'; import fp from 'fastify-plugin';  async function initDatabaseConnection(): Promise&lt;PrismaClient> {   const db = new PrismaClient();   await db.$connect();   return db; }  \/\/ Use TypeScript module augmentation to declare the type of server.prisma to be PrismaClient declare module 'fastify' {   interface FastifyInstance {     prisma: PrismaClient   } }  const prismaPlugin = fp(async (server) => {   const prisma = await initDatabaseConnection();    \/\/ Make Prisma Client available through the fastify server instance: server.prisma   server.decorate('prisma', prisma);   server.addHook('onClose', async () =>  {     await server.prisma.$disconnect();   }); });  export default prismaPlugin; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 <strong>app.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import Fastify, { FastifyServerOptions } from 'fastify' import prismaPlugin from '.\/prisma.plugin';  export type AppOptions = Partial&lt;FastifyServerOptions>;  async function buildApp(options: AppOptions = {}) {   const fastify = Fastify(options);   fastify.register(prismaPlugin);      return fastify; }  export { buildApp }<\/code><\/pre>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0431\u043e\u0442o\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 <code>touch src\/services.ts<\/code><\/p>\n<pre><code class=\"typescript\">import { PrismaClient } from '@prisma\/client';  async function main(prisma: PrismaClient) {   await prisma.user.deleteMany();   await prisma.user.create({ data: { email: 'test@email.com' } });   const usersCount = await prisma.user.count();   console.log({ users_count: usersCount });  }  export { main };<\/code><\/pre>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432\u043d\u0435\u0441\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 <strong>index.js<\/strong><\/p>\n<pre><code class=\"typescript\">import { buildApp, AppOptions } from '.\/app'; import { main } from '.\/services';  const options: AppOptions = {   logger: true, };  const start = async () => {   const app = await buildApp(options);    try {     await app.listen({       port: 3000,       host: 'localhost',     });      await main(app.prisma);   } catch (err) {     app.log.error(err);     process.exit(1);   } };  start();<\/code><\/pre>\n<p>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0448\u0430\u0433\u0435 3, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435<\/p>\n<pre><code class=\"bash\">yarn dev  [App] { users_count: 1 }<\/code><\/pre>\n<h2>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 grpc<\/h2>\n<p>\u0418 \u0432\u043e\u0442 \u043c\u044b \u043f\u043e\u0434\u043e\u0448\u043b\u0438 \u043a \u0444\u0438\u043d\u0438\u0448\u0443, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043b\u044f <strong>grpc<\/strong>, \u0430 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0430\u043c grpc?  <\/p>\n<p>Grpc \u0445\u043e\u0440\u043e\u0448 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0431\u0435\u043a-\u0431\u0435\u043a, \u043e\u043d \u0448\u0443\u0441\u0442\u0440\u0435\u0435 \u0440\u0435\u0441\u0442\u0430, \u043d\u043e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435.  <\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b.<\/p>\n<pre><code>yarn add @grpc\/grpc-js @grpc\/proto-loader mkdir proto touch proto\/example.proto<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043e\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b proto \u0444\u0430\u0439\u043b\u0430 <strong>example.proto<\/strong>, \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">syntax = \"proto3\";  package example;  message User {   string id = 1;   string email = 2; };  message GetUsersResponse {   repeated User users = 1; }  message GetUsersRequest {}  service UserService {   rpc GetUsers(GetUsersRequest) returns (GetUsersResponse); }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043e\u043f\u0438\u0441\u0430\u043b\u0438 proto \u0444\u0430\u0439\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0442\u0438\u043f\u044b \u0434\u043b\u044f typescript, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0432 <strong>proto-loader<\/strong> \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c <strong>proto-loader-gen-types. <\/strong>\u0421\u0440\u0430\u0437\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0443\u0436\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0441\u0435\u043a\u0446\u0438\u044e <strong>scrips \u0432 <\/strong>\u0444\u0430\u0439\u043b\u0435<strong> package.json<\/strong><\/p>\n<pre><code>  \"scripts\": {     \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\",     \"build\": \"tsc\",     \"dev\": \"export NODE_ENV=development TS_NODE_BASEURL=.\/dist &amp;&amp; concurrently -k -p \\\"[{name}]\\\" -n \\\"TypeScript,App\\\" -c \\\"yellow.bold,cyan.bold\\\" \\\"tsc -w\\\" \\\"nodemon\\\"\",     \"gen-proto\": \"$(npm bin)\/proto-loader-gen-types --longs=String --enums=String  --oneofs --grpcLib=@grpc\/grpc-js --outDir=src\/proto\/interfaces proto\/*.proto\"   }, <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0442\u0438\u043f\u043e\u0432 <code>yarn gen-proto<\/code><\/p>\n<p>\u0412\u0441\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0431\u0443\u0434\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0432 <strong>src\/proto\/interfaces<\/strong><\/p>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c <strong>grpc<\/strong> server \u0431\u0443\u0434\u0435\u043c \u0442\u0430\u043a \u0436\u0435 \u0447\u0435\u0440\u0435\u0437 <strong>fasify<\/strong> \u043f\u043b\u0430\u0433\u0438\u043d\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>touch src\/grpc.plugin.ts<\/code>  \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<pre><code class=\"typescript\">import { GetUsersResponse } from '.\/proto\/interfaces\/example\/GetUsersResponse'; import { GetUsersRequest__Output } from '.\/proto\/interfaces\/example\/GetUsersRequest'; import fp from 'fastify-plugin'; import { join } from 'path'; import * as grpc from '@grpc\/grpc-js'; import * as protoLoader from '@grpc\/proto-loader'; import { ProtoGrpcType } from '.\/proto\/interfaces\/example';  declare module 'fastify' {   interface FastifyInstance {     grpcServer: {       start: () => void,     },   } }  const grpcServerOptions = {   keepCase: false,   longs: String,   enums: String,   defaults: false,   oneofs: true, };  const grpcServerPlugin = fp(async (fastify) => {   \/\/ load proto files from directory   const packageDefinition = protoLoader.loadSync([join(__dirname, '..\/proto\/example.proto')], grpcServerOptions);   const proto = grpc.loadPackageDefinition(packageDefinition) as unknown as ProtoGrpcType;   const grpcServer = new grpc.Server();    \/\/ mapping between handlers and rpc services   grpcServer.addService(proto.example.UserService.service, {     GetUsers: async (       req: grpc.ServerUnaryCall&lt;GetUsersRequest__Output, GetUsersResponse>,       res: grpc.sendUnaryData&lt;GetUsersResponse>) => {         return res(null, {           users: [{             id: 'test',             email: 'test',           }],         })     },   });    function start(opts: { port: number } = { port: 50501 }) {     return grpcServer.bindAsync(       `0.0.0.0:${opts.port}`,       grpc.ServerCredentials.createInsecure(),       (err, port) => {         if (err) {           console.error(err);           return;         }          grpcServer.start();         console.log(`GRPC Server listening on ${port}`);       },     );   }    fastify.decorate('grpcServer', { start }); });  export { grpcServerPlugin };<\/code><\/pre>\n<p>\u041e\u0442\u0432\u0435\u0442 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a\u0430 \u0435\u043d\u0434\u043f\u043e\u0438\u043d\u0442\u0430 <strong>GetUsers \u0437\u0434\u0435\u0441\u044c <\/strong>\u0437\u0430\u043c\u043e\u043a\u0430\u043d \u0432 \u0434\u0435\u043c\u043e\u043d\u0441\u0442\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0445 \u0446\u0435\u043b\u044f\u0445.<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043f\u043b\u0430\u0433\u0438\u043d \u0433\u043e\u0442\u043e\u0432 &#8212; \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0435\u0433\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u0432 <strong>app.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import Fastify, { FastifyServerOptions } from 'fastify' import { grpcServerPlugin } from '.\/grpc.plugin'; import prismaPlugin from '.\/prisma.plugin';  export type AppOptions = Partial&lt;FastifyServerOptions>;  async function buildApp(options: AppOptions = {}) {   const fastify = Fastify(options);   fastify.register(prismaPlugin);   fastify.register(grpcServerPlugin);    return fastify; }  export { buildApp }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u043b\u0430\u0433\u0438\u043d \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d \u0438 \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0442\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 index.ts<\/p>\n<pre><code class=\"typescript\">import { buildApp, AppOptions } from '.\/app'; import { main } from '.\/services';  const options: AppOptions = {   logger: true, };  const start = async () => {   const app = await buildApp(options);    try {     await app.listen({       port: 3000,       host: 'localhost',     });      app.grpcServer.start();      await main(app.prisma);   } catch (err) {     app.log.error(err);     process.exit(1);   } };  start();<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440<code>yarn dev, <\/code>\u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u0430 \u043e\u0442\u043e\u0440\u0431\u0430\u0437\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0430\u044f \u0438\u043d\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044f <\/p>\n<pre><code>yarn dev  [App] {\"level\":30,\"time\":1676521988065,\"pid\":69311,\"hostname\":\"Anatolys-MacBook-Pro.local\",\"msg\":\"Server listening at http:\/\/127.0.0.1:3000\"} [App] GRPC Server listening on 50501 [App] { users_count: 1 }<\/code><\/pre>\n<p>\u041f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u043e\u0442\u0432\u0435\u0442 \u043c\u043e\u0436\u043d\u043e \u043b\u0438\u0431\u043e \u043f\u0440\u0438 \u043f\u043e\u043c\u043e\u0449\u0438 \u0442\u0435\u0441\u0442\u0430, \u043b\u0438\u0431\u043e \u0447\u0435\u0440\u0435\u0437 \u0441\u0442\u043e\u0440\u043e\u043d\u043d\u0435\u0435 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0435, \u044f \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043b bloomRPC.<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/a81\/310\/e83\/a81310e839ac003e0608647ad1d913ed.png\" width=\"auto\" height=\"auto\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/a81\/310\/e83\/a81310e839ac003e0608647ad1d913ed.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u0412 \u043e\u0442\u0432\u0435\u0442\u0435 \u0434\u043e\u043b\u0436\u0435\u043d \u043f\u0440\u0438\u0439\u0442\u0438 \u043d\u0430\u0448 \u0437\u0430\u043c\u043e\u043a\u0430\u043d\u044b\u0439 \u043e\u0442\u0432\u0435\u0442.<\/p>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p>\u041f\u043e\u0434\u0432\u0435\u0434\u0435\u043c \u0438\u0442\u043e\u0433\u0438, \u0431\u043e\u043b\u0432\u0430\u043d\u043a\u0430 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441\u0430 \u043c\u043e\u0436\u0435\u0442 \u043f\u0440\u0438\u043d\u0438\u043c\u0430\u0442\u044c \u0437\u0430\u043f\u0440\u043e\u0441\u044b \u043f\u043e grpc \u0438 \u043f\u043e rest \u043e\u0434\u043d\u043e\u0432\u0440\u0435\u043c\u0435\u043d\u043d\u043e. Rest \u0437\u0430\u043f\u0443\u0449\u0435\u043d \u043d\u0430 3000 \u043f\u043e\u0440\u0442\u0443, grpc \u043d\u0430 50501.<\/p>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 ORM \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f prisma. \u041f\u043e\u0436\u0430\u043b\u0443\u0439 \u043d\u0435 \u0445\u0432\u0430\u0442\u0430\u0435\u0442 \u0442\u043e\u043b\u044c\u043a\u043e \u0442\u0435\u0441\u0442\u043e\u0432, \u043d\u043e \u044d\u0442\u043e \u0443\u0436\u0435 \u0437\u0430 \u0440\u0430\u043c\u043a\u0430\u043c\u0438 \u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b\u0430!<\/p>\n<p>\u0421\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e \u0441 \u043f\u0440\u043e\u0435\u043a\u0442\u043e\u043c: <a href=\"https:\/\/github.com\/iseekyouu\/habr-tfpg\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/iseekyouu\/habr-tfpg<\/a><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/718774\/\"> https:\/\/habr.com\/ru\/post\/718774\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u0412\u0441\u0442\u0443\u043f\u043b\u0435\u043d\u0438\u0435<\/h2>\n<p>\u0417\u0430\u0447\u0430\u0441\u0442\u0443\u044e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u0435\u0442 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e\u0441\u0442\u044c \u043d\u0430\u0447\u0430\u0442\u044c \u043d\u043e\u0432\u044b\u0439 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u0440\u0432\u0438\u0441. \u0412\u043e\u0442 \u0438 \u0443 \u043c\u0435\u043d\u044f \u0441\u043e\u0432\u0441\u0435\u043c \u043d\u0435\u0434\u0430\u0432\u043d\u043e  \u0432\u043e\u0437\u043d\u0438\u043a\u043b\u0430 \u0442\u0430\u043a\u0430\u044f \u043f\u043e\u0442\u0440\u0435\u0431\u043d\u043e\u0441\u0442\u044c. \u0410 \u0432\u0435\u0434\u044c \u0445\u043e\u0447\u0435\u0442\u0441\u044f \u0435\u0449\u0435 \u0438 \u0447\u0435\u0433\u043e-\u0442\u043e \u043d\u043e\u0432\u0435\u043d\u044c\u043a\u043e\u0433\u043e \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u0442\u044c. <\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u0431\u044b\u043b \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d \u0441\u0442\u0435\u043a \u0438 \u0445\u043e\u0442\u044f \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u0434\u043b\u044f \u043c\u0435\u043d\u044f \u043d\u0435 \u043d\u043e\u0432\u044b\u0439, \u043d\u043e \u044f \u0441\u0442\u043e\u043b\u043a\u043d\u0443\u043b\u0441\u044f \u0441 \u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e\u043c \u043f\u043e\u0434\u0432\u043e\u0434\u043d\u044b\u0445 \u043a\u0430\u043c\u043d\u0435\u0439. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u0440\u043e\u0434\u0438\u043b\u0430\u0441\u044c \u0438\u0434\u0435\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u0442\u044c \u044d\u0442\u043e\u0442 \u0442\u0443\u0442\u043e\u0440\u0438\u0430\u043b.<\/p>\n<p>\u0412 \u043a\u043e\u043d\u0446\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u0430 \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u0441 \u043a\u043e\u0434\u043e\u043c.<\/p>\n<h2>\u0428\u0430\u0433 1. \u0418\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 TS<\/h2>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0438\u043d\u0438\u0446\u0438\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0441\u0430\u043c typescript \u0438 \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u044b\u0435 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0438. <\/p>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430 \u0442\u0430\u0439\u043f\u0441\u043a\u0440\u0438\u043f\u0442\u0435 \u0441 \u043d\u0443\u043b\u044f &#8212; \u0437\u0430\u0434\u0430\u0447\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430, \u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043f\u043e\u0441\u0442\u043e\u044f\u043d\u043d\u043e \u0432\u043e\u0437\u043d\u0438\u043a\u0430\u044e\u0442 \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u0438 \u0441 \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u043e\u0439. \u0412 \u044d\u0442\u043e\u043c \u043f\u043b\u0430\u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u0443\u0434\u043e\u0431\u0435\u043d nest, \u0442\u0430\u043a \u043a\u0430\u043a \u0442\u0430\u043c \u0438\u0434\u0435\u0442 \u0432\u0441\u0435 \u0438\u0437 \u043a\u043e\u0440\u043e\u0431\u043a\u0438, \u043d\u043e \u043e\u043d \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0442\u044f\u0436\u0435\u043b\u044b\u0439 \u0438 \u0441\u0430\u043c \u043f\u043e \u0441\u0435\u0431\u0435 \u0443\u0436\u0435 \u043a\u0430\u043a \u044f\u0437\u044b\u043a \u043f\u0440\u043e\u0433\u0440\u0430\u043c\u043c\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u044d\u0442\u043e \u043d\u0435 \u043d\u0430\u0448 \u043f\u0443\u0442\u044c.<\/p>\n<p>\u0412\u044b\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0435 \u043a\u043e\u043c\u0430\u043d\u0434\u044b \u0432 \u0442\u0435\u0440\u043c\u0438\u043d\u0430\u043b\u0435<\/p>\n<pre><code class=\"bash\">npm init -y yarn add typescript tsconfig-paths ts-node @types\/node nodemon concurrently --dev mkdir src &amp;&amp; touch src\/index.ts npx tsc --init --rootDir src --outDir build --esModuleInterop --resolveJsonModule --module commonjs --allowJs true --noImplicitAny true --target esnext --moduleResolution node touch .gitignore &amp;&amp; echo \"node_modules \\n.vscode\\nbuild\" >> .gitignore touch nodemon.json echo \"console.log('Hello world')\" >> src\/index.ts<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0434\u043e\u043f\u043e\u043b\u043d\u0438\u043c \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u043e\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <strong>typescript.json<\/strong>, \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0443 \u0442\u0440\u043e\u0433\u0430\u0442\u044c \u043d\u0435 \u0431\u0443\u0434\u0435\u043c, \u0435\u0435 \u0432\u043f\u043e\u043b\u043d\u0435 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u0434\u043b\u044f \u043d\u0430\u0448\u0438\u0445 \u0446\u0435\u043b\u0435\u0439<\/p>\n<pre><code class=\"json\">{   \"compilerOptions\": {...},   \"include\": [\"src\"],   \"exclude\": [     \"node_modules\",     \"dist\",     \"examples\",   ], } <\/code><\/pre>\n<p>\u0417\u0430\u043f\u043e\u043b\u043d\u0438\u043c \u0441\u0435\u043a\u0446\u0438\u044e <strong>scripts \u0432 package.json<\/strong><\/p>\n<pre><code class=\"json\">  \"scripts\": {     \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\",     \"build\": \"tsc\",     \"dev\": \"export NODE_ENV=development TS_NODE_BASEURL=.\/dist &amp;&amp; concurrently -k -p \\\"[{name}]\\\" -n \\\"TypeScript,App\\\" -c \\\"yellow.bold,cyan.bold\\\" \\\"tsc -w\\\" \\\"nodemon\\\"\"   },<\/code><\/pre>\n<p>\u0414\u043b\u044f dev \u0440\u0435\u0436\u0438\u043c\u0430 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f <strong>nodemon<\/strong>, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0435\u0433\u043e \u0442\u043e\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c, \u0432 <strong>nodemon.json<\/strong><\/p>\n<pre><code class=\"json\">{   \"ignore\": [     \"**\/*.test.ts\",     \"**\/*.spec.ts\",     \".git\",     \"node_modules\"   ],   \"watch\": [     \"src\"   ],   \"exec\": \"tsc &amp;&amp; node -r tsconfig-paths\/register -r ts-node\/register build\/index.js\",   \"ext\": \"ts, js\" }<\/code><\/pre>\n<p>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u044d\u0442\u043e\u043c \u0448\u0430\u0433\u0435, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u043f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0443\u0432\u0438\u0434\u0435\u0442\u044c \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435<\/p>\n<pre><code class=\"bash\">yarn dev  [App] [nodemon] starting `tsc &amp;&amp; node -r tsconfig-paths\/register -r ts-node\/register build\/index.js` [App] Hello world [App] [nodemon] clean exit - waiting for changes before rest<\/code><\/pre>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043f\u0435\u0440\u0432\u043e\u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0430\u044f, \u0431\u0430\u0437\u043e\u0432\u0430\u044f, \u043d\u0430\u0441\u0442\u0440\u043e\u0439\u043a\u0430 \u0433\u043e\u0442\u043e\u0432\u0430.<\/p>\n<h2>\u0428\u0430\u0433 2. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 Fastify<\/h2>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0430\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0430\u043c \u0441\u0435\u0440\u0432\u0435\u0440. \u0421\u043f\u0435\u0440\u0432\u0430 \u043e\u0442\u0432\u0435\u0447\u0443 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441: &#171;\u041f\u043e\u0447\u0435\u043c\u0443 fastify ? &#171;<\/p>\n<p>Fastify \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043b\u0435\u0433\u043a\u0438\u0439 \u0438 \u043d\u0430 \u043d\u0435\u043c \u0431\u044b\u0441\u0442\u0440\u043e \u0438 \u043f\u0440\u043e\u0441\u0442\u043e \u043f\u0438\u0441\u0430\u0442\u044c REST. \u0423 \u043d\u0435\u0433\u043e \u0443\u0434\u043e\u0431\u043d\u0430\u044f \u0441\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 \u0431\u044b \u0438 \u043d\u0435\u0442.<\/p>\n<p>\u0414\u043e\u0431\u0430\u0432\u0438\u043c \u043f\u0430\u043a\u0435\u0442 \u0438 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b \u0434\u043b\u044f \u043d\u0430\u0448\u0435\u0433\u043e \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<pre><code class=\"bash\">yarn add fastify touch src\/app.ts<\/code><\/pre>\n<p>\u041d\u0430 \u0434\u0430\u043d\u043d\u043e\u043c \u044d\u0442\u0430\u043f\u0435 \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043f\u0440\u043e\u0441\u0442, \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0441\u0430\u043c\u043e\u0433\u043e \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044f \u0432\u044b\u043d\u0435\u0441\u0435\u043c \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0439 \u0444\u0430\u0439\u043b <strong>app.ts<\/strong>.<\/p>\n<pre><code class=\"typescript\">import Fastify, { FastifyServerOptions } from 'fastify'  export type AppOptions = Partial&lt;FastifyServerOptions>;  async function buildApp(options: AppOptions = {}) {   const fastify = Fastify(options);    return fastify; }  export { buildApp }<\/code><\/pre>\n<p>\u041e\u043f\u0438\u0448\u0435\u043c \u0437\u0430\u043f\u0443\u0441\u043a \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0432 <strong>index.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import { buildApp, AppOptions } from '.\/app';  const options: AppOptions = {   logger: true, };  const start = async () => {   const app = await buildApp(options);    try {     await app.listen({       port: 3000,       host: 'localhost',     });    } catch (err) {     app.log.error(err);     process.exit(1);   } };  start();<\/code><\/pre>\n<p>\u0414\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0441\u0435\u0440\u0432\u0435\u0440 \u0432 dev \u0440\u0435\u0436\u0438\u043c\u0435, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435.<\/p>\n<pre><code class=\"bash\">yarn dev  [App] {\"level\":30,\"time\":1676434938643,\"pid\":39600,\"hostname\":\"Anatolys-MacBook-Pro.local\",\"msg\":\"Server listening at http:\/\/[::1]:3000\"}<\/code><\/pre>\n<h2>\u0428\u0430\u0433 3. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 prisma<\/h2>\n<p>\u0412 \u043a\u0430\u0447\u0435\u0441\u0442\u0432\u0435 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f Mongodb. \u0418 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043f\u043e\u0447\u0435\u043c\u0443 Prisma ?<br \/>\u0412\u043f\u0435\u0440\u0432\u044b\u0435, \u043d\u0435 \u0442\u0430\u043a \u0434\u0430\u0432\u043d\u043e, \u043f\u043e\u043f\u0440\u043e\u0431\u043e\u0432\u0430\u043b prisma \u0438 \u0431\u044b\u043b \u0432 \u0432\u043e\u0441\u0442\u043e\u0440\u0433\u0435 &#8212; \u043e\u043f\u0438\u0441\u044b\u0432\u0430\u0435\u0448\u044c \u0441\u0445\u0435\u043c\u0443, \u0430 \u043e\u043d\u0430 \u0441\u0430\u043c\u0430 \u0443\u0436\u0435 \u0433\u0435\u043d\u0435\u0440\u0438\u0442 \u0442\u043e\u043d\u043d\u044b \u043b\u043e\u0433\u0438\u043a\u0438, \u0447\u0442\u043e \u0441\u0438\u043b\u044c\u043d\u043e \u0443\u043f\u0440\u043e\u0449\u0430\u0435\u0442 \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u0441 \u0411\u0414.<\/p>\n<p>\u0412\u0430\u043c \u043d\u0443\u0436\u043d\u043e \u043f\u043e\u0434\u043d\u044f\u0442\u044c \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e \u043c\u043e\u043d\u0433\u0443 \u0438\u043b\u0438 \u0432\u0437\u044f\u0442\u044c \u0432 \u0434\u043e\u043a\u0435\u0440 \u0445\u0430\u0431\u0435 <a href=\"https:\/\/hub.docker.com\/_\/mongo\" rel=\"noopener noreferrer nofollow\">https:\/\/hub.docker.com\/_\/mongo<\/a>.<\/p>\n<p>\u041f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043c \u0447\u0442\u043e \u0443 \u0432\u0430\u0441 \u0443\u0436\u0435 \u0435\u0441\u0442\u044c \u043c\u043e\u043d\u0433\u0430 \u043f\u043e \u0430\u0434\u0440\u0435\u0441\u0443 <code>mongodb:\/\/localhost:27017<\/code><\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b<\/p>\n<pre><code class=\"bash\">yarn add prisma @prisma\/client fastify-plugin npx prisma init --datasource-provider mongodb<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0438\u0442\u044c \u0441\u0445\u0435\u043c\u0443 \u0431\u0430\u0437\u044b \u0434\u0430\u043d\u043d\u044b\u0445 <strong>prisma\/schema.prisma<\/strong><\/p>\n<pre><code class=\"javascript\">generator client {   provider = \"prisma-client-js\" }  datasource db {   provider = \"mongodb\"   url      = \"mongodb:\/\/localhost:27017\/example\" }  model Post {   id       String    @id @default(auto()) @map(\"_id\") @db.ObjectId   slug     String    @unique   title    String   body     String   author   User      @relation(fields: [authorId], references: [id])   authorId String    @db.ObjectId }  model User {   id      String   @id @default(auto()) @map(\"_id\") @db.ObjectId   email   String   @unique   name    String?   address Address?   posts   Post[] }<\/code><\/pre>\n<p>\u0417\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0431\u0438\u0437\u043d\u0435\u0441 \u043b\u043e\u0433\u0438\u043a\u0438 \u0440\u0430\u0431\u043e\u0442\u044b \u0441 \u0411\u0414 \u0438 \u043d\u0430\u043a\u0430\u0442\u044b\u0432\u0430\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 \u0441\u0430\u043c\u0443 \u0431\u0430\u0437\u0443 \u0434\u0430\u043d\u043d\u044b\u0445. \u042d\u0442\u043e \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u043a\u0430\u0436\u0434\u044b\u0439 \u0440\u0430\u0437 \u043f\u043e\u0441\u043b\u0435 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0441\u0445\u0435\u043c\u044b, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u0432\u044b\u043d\u0435\u0441\u0442\u0438 \u0432 \u0441\u0435\u043a\u0446\u0438\u044e <strong>scrips &#8212; package.json<\/strong><\/p>\n<pre><code class=\"bash\">yarn prisma generate npx prisma db push<\/code><\/pre>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c \u043f\u0440\u0438\u0437\u043c\u0443 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0447\u0435\u0440\u0435\u0437 \u043f\u043b\u0430\u0433\u0438\u043d\u044b fastify <code>touch src\/prisma.plugin.ts<\/code><\/p>\n<pre><code class=\"typescript\">import { PrismaClient } from '@prisma\/client'; import fp from 'fastify-plugin';  async function initDatabaseConnection(): Promise&lt;PrismaClient> {   const db = new PrismaClient();   await db.$connect();   return db; }  \/\/ Use TypeScript module augmentation to declare the type of server.prisma to be PrismaClient declare module 'fastify' {   interface FastifyInstance {     prisma: PrismaClient   } }  const prismaPlugin = fp(async (server) => {   const prisma = await initDatabaseConnection();    \/\/ Make Prisma Client available through the fastify server instance: server.prisma   server.decorate('prisma', prisma);   server.addHook('onClose', async () =>  {     await server.prisma.$disconnect();   }); });  export default prismaPlugin; <\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0438\u0442\u044c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 <strong>app.ts<\/strong><\/p>\n<pre><code class=\"typescript\">import Fastify, { FastifyServerOptions } from 'fastify' import prismaPlugin from '.\/prisma.plugin';  export type AppOptions = Partial&lt;FastifyServerOptions>;  async function buildApp(options: AppOptions = {}) {   const fastify = Fastify(options);   fastify.register(prismaPlugin);      return fastify; }  export { buildApp }<\/code><\/pre>\n<p>\u0421\u0434\u0435\u043b\u0430\u0435\u043c \u043f\u0440\u043e\u0441\u0442\u0435\u043d\u044c\u043a\u0438\u0439 \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u0447\u0438\u043a \u0434\u043b\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0438 \u0440\u0430\u0431\u043e\u0442o\u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0438 <code>touch src\/services.ts<\/code><\/p>\n<pre><code class=\"typescript\">import { PrismaClient } from '@prisma\/client';  async function main(prisma: PrismaClient) {   await prisma.user.deleteMany();   await prisma.user.create({ data: { email: 'test@email.com' } });   const usersCount = await prisma.user.count();   console.log({ users_count: usersCount });  }  export { main };<\/code><\/pre>\n<p>\u0418 \u043d\u0430\u043a\u043e\u043d\u0435\u0446, \u0432\u043d\u0435\u0441\u0435\u043c \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u044f \u0432 <strong>index.js<\/strong><\/p>\n<pre><code class=\"typescript\">import { buildApp, AppOptions } from '.\/app'; import { main } from '.\/services';  const options: AppOptions = {   logger: true, };  const start = async () => {   const app = await buildApp(options);    try {     await app.listen({       port: 3000,       host: 'localhost',     });      await main(app.prisma);   } catch (err) {     app.log.error(err);     process.exit(1);   } };  start();<\/code><\/pre>\n<p>\u0424\u0438\u043d\u0430\u043b\u044c\u043d\u0430\u044f \u043f\u0440\u043e\u0432\u0435\u0440\u043a\u0430 \u043d\u0430 \u0448\u0430\u0433\u0435 3, \u0432 \u043a\u043e\u043d\u0441\u043e\u043b\u0438 \u0434\u043e\u043b\u0436\u043d\u043e \u043f\u043e\u044f\u0432\u0438\u0442\u044c\u0441\u044f \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0435\u0435 \u0441\u043e\u043e\u0431\u0449\u0435\u043d\u0438\u0435<\/p>\n<pre><code class=\"bash\">yarn dev  [App] { users_count: 1 }<\/code><\/pre>\n<h2>\u0428\u0430\u0433 4. \u0414\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 grpc<\/h2>\n<p>\u0418 \u0432\u043e\u0442 \u043c\u044b \u043f\u043e\u0434\u043e\u0448\u043b\u0438 \u043a \u0444\u0438\u043d\u0438\u0448\u0443, \u043e\u0441\u0442\u0430\u043b\u043e\u0441\u044c \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u0435\u0440\u0432\u0435\u0440 \u0434\u043b\u044f <strong>grpc<\/strong>, \u0430 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0434\u043b\u044f \u0447\u0435\u0433\u043e \u043d\u0430\u043c grpc?  <\/p>\n<p>Grpc \u0445\u043e\u0440\u043e\u0448 \u0434\u043b\u044f \u0432\u0437\u0430\u0438\u043c\u043e\u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0439 \u0431\u0435\u043a-\u0431\u0435\u043a, \u043e\u043d \u0448\u0443\u0441\u0442\u0440\u0435\u0435 \u0440\u0435\u0441\u0442\u0430, \u043d\u043e \u0438\u043c\u043f\u043b\u0435\u043c\u0435\u043d\u0442\u0438\u0440\u0443\u0435\u0442\u0441\u044f \u0441\u043b\u043e\u0436\u043d\u0435\u0435.  <\/p>\n<p>\u0421\u043f\u0435\u0440\u0432\u0430 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0443\u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u044c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u043f\u0430\u043a\u0435\u0442\u044b.<\/p>\n<pre><code>yarn add @grpc\/grpc-js @grpc\/proto-loader mkdir proto touch proto\/example.proto<\/code><\/pre>\n<p>\u0417\u0430\u0442\u0435\u043c \u043e\u043f\u0438\u0448\u0435\u043c \u043d\u0430\u0448 \u0441\u0435\u0440\u0432\u0435\u0440 \u0441\u043e \u0441\u0442\u043e\u0440\u043e\u043d\u044b proto \u0444\u0430\u0439\u043b\u0430 <strong>example.proto<\/strong>, \u0434\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u0434\u043e\u0441\u0442\u0430\u0442\u043e\u0447\u043d\u043e \u043e\u043f\u0438\u0441\u0430\u0442\u044c \u043e\u0434\u0438\u043d \u0441\u0435\u0440\u0432\u0438\u0441, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043e\u0442\u0434\u0430\u0432\u0430\u0442\u044c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u0435\u043b\u0435\u0439:<\/p>\n<pre><code class=\"javascript\">syntax = \"proto3\";  package example;  message User {   string id = 1;   string email = 2; };  message GetUsersResponse {   repeated User users = 1; }  message GetUsersRequest {}  service UserService {   rpc GetUsers(GetUsersRequest) returns (GetUsersResponse); }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e \u043a\u0430\u043a \u043c\u044b \u043e\u043f\u0438\u0441\u0430\u043b\u0438 proto \u0444\u0430\u0439\u043b, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0442\u044c \u0442\u0438\u043f\u044b \u0434\u043b\u044f typescript, \u0434\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0432\u043e\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u0441\u044f \u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u044b\u043c \u0432 <strong>proto-loader<\/strong> \u0433\u0435\u043d\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u043c <strong>proto-loader-gen-types. <\/strong>\u0421\u0440\u0430\u0437\u0443 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0443\u0436\u043d\u0443\u044e \u043a\u043e\u043c\u0430\u043d\u0434\u0443 \u0432 \u0441\u0435\u043a\u0446\u0438\u044e <strong>scrips \u0432 <\/strong>\u0444\u0430\u0439\u043b\u0435<strong> package.json<\/strong><\/p>\n<pre><code>  \"scripts\": {     \"test\": \"echo \\\"Error: no test specified\\\" &amp;&amp; exit 1\",     \"build\": \"tsc\",     \"dev\": \"export NODE_ENV=development TS_NODE_BASEURL=.\/dist &amp;&amp; concurrently -k -p \\\"[{name}]\\\" -n \\\"TypeScript,App\\\" -c \\\"yellow.bold,cyan.bold\\\" \\\"tsc -w\\\" \\\"nodemon\\\"\",     \"gen-proto\": \"$(npm bin)\/proto-loader-gen-types --longs=String --enums=String  --oneofs --grpcLib=@grpc\/grpc-js --outDir=src\/proto\/interfaces proto\/*.proto\"   }, <\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0437\u0430\u043f\u0443\u0441\u043a\u0430\u0435\u043c \u0433\u0435\u043d\u0435\u0440\u0430\u0446\u0438\u044e \u0442\u0438\u043f\u043e\u0432 <code>yarn gen-proto<\/code><\/p>\n<p>\u0412\u0441\u0435 \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0435 \u0442\u0438\u043f\u044b \u0431\u0443\u0434\u0443\u0442 \u043b\u0435\u0436\u0430\u0442\u044c \u0432 <strong>src\/proto\/interfaces<\/strong><\/p>\n<p>\u041f\u043e\u0434\u043a\u043b\u044e\u0447\u0430\u0442\u044c <strong>grpc<\/strong> server \u0431\u0443\u0434\u0435\u043c \u0442\u0430\u043a \u0436\u0435 \u0447\u0435\u0440\u0435\u0437 <strong>fasify<\/strong> \u043f\u043b\u0430\u0433\u0438\u043d\u044b. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0444\u0430\u0439\u043b <code>touch src\/grpc.plugin.ts<\/code>  \u0438 \u0437\u0430\u043f\u0438\u0448\u0435\u043c \u0432 \u043d\u0435\u0433\u043e \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u0438 \u0441\u0435\u0440\u0432\u0435\u0440\u0430.<\/p>\n<pre><code class=\"typescript\">import { GetUsersResponse } from '.\/proto\/interfaces\/example\/GetUsersResponse'; import { GetUsersRequest__Output } from '.\/proto\/interfaces\/example\/GetUsersRequest'; import fp from 'fastify-plugin'; import { join } from 'path'; import * as grpc from '@grpc\/grpc-js'; import * as protoLoader from '@grpc\/proto-loader'; import { ProtoGrpcType } from '.\/proto\/interfaces\/example';  declare module 'fastify' {   interface FastifyInstance {     grpcServer: {       start: () => void,     },   } }  const grpcServerOptions = {   keepCase: false,   longs: String,   enums: String,   defaults: false,   oneofs: true, };  const grpcServerPlugin = fp(async (fastify) => {   \/\/ load proto files from directory   const packageDefinition = protoLoader.loadSync([join(__dirname, '..\/proto\/example.proto')], grpcServerOptions);   const proto = grpc.loadPackageDefinition(packageDefinition) as unknown as ProtoGrpcType;   const grpcServer = new grpc.Server();    \/\/ mapping between handlers and rpc services<\/code><\/pre>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-345821","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=345821"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/345821\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=345821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=345821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=345821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}