{"id":13280,"date":"2023-03-22T01:06:22","date_gmt":"2023-03-21T21:36:22","guid":{"rendered":"https:\/\/nabfollower.com\/blog\/api-with-typescript-nest-typeorm-postgres-docker-1m81\/"},"modified":"2023-03-22T01:06:22","modified_gmt":"2023-03-21T21:36:22","slug":"api-with-typescript-nest-typeorm-postgres-docker-1m81","status":"publish","type":"post","link":"https:\/\/nabfollower.com\/blog\/api-with-typescript-nest-typeorm-postgres-docker-1m81\/","title":{"rendered":"API \u0628\u0627 Typescript + Nest + TypeORM + Postgres + Docker"},"content":{"rendered":"<div data-article-id=\"1351399\" id=\"article-body\">\n<p>\u062f\u0631 \u0627\u06cc\u0646 \u067e\u0633\u062a \u0648\u0628\u0644\u0627\u06af\u060c \u0645\u0646 \u0628\u0647 \u0634\u0645\u0627 \u0646\u0634\u0627\u0646 \u062e\u0648\u0627\u0647\u0645 \u062f\u0627\u062f \u06a9\u0647 \u0686\u06af\u0648\u0646\u0647 \u06cc\u06a9 \u067e\u0631\u0648\u0698\u0647 \u0631\u0627 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 TypeScript\u060c Nest\u060c Postgres\u060c Docker \u062a\u0646\u0638\u06cc\u0645 \u06a9\u0646\u06cc\u062f \u0648 \u0686\u06af\u0648\u0646\u0647 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 \u062e\u0648\u062f \u0631\u0627 \u0628\u0630\u0631 \u06a9\u0646\u06cc\u062f.<\/p>\n<p><strong>TypeScript<\/strong> \u06cc\u06a9 \u0632\u0628\u0627\u0646 \u062a\u0627\u06cc\u067e \u0627\u06cc\u0633\u062a\u0627 \u0627\u0633\u062a \u06a9\u0647 \u06cc\u06a9 \u0627\u0628\u0631 \u0645\u062c\u0645\u0648\u0639\u0647 \u0646\u062d\u0648\u06cc \u062f\u0642\u06cc\u0642 \u062c\u0627\u0648\u0627 \u0627\u0633\u06a9\u0631\u06cc\u067e\u062a \u0627\u0633\u062a.  \u0627\u06cc\u0645\u0646\u06cc \u0646\u0648\u0639 \u0628\u0647\u0628\u0648\u062f \u06cc\u0627\u0641\u062a\u0647 \u0648 \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0627\u0632 \u0627\u0628\u0632\u0627\u0631 \u0631\u0627 \u0627\u0631\u0627\u0626\u0647 \u0645\u06cc \u062f\u0647\u062f \u0648 \u0622\u0646 \u0631\u0627 \u0628\u0647 \u06af\u0632\u06cc\u0646\u0647 \u0627\u06cc \u0645\u062d\u0628\u0648\u0628 \u0628\u0631\u0627\u06cc \u067e\u0631\u0648\u0698\u0647 \u0647\u0627\u06cc \u062f\u0631 \u0645\u0642\u06cc\u0627\u0633 \u0628\u0632\u0631\u06af\u062a\u0631 \u062a\u0628\u062f\u06cc\u0644 \u0645\u06cc \u06a9\u0646\u062f. <\/p>\n<p><strong>\u0644\u0627\u0646\u0647<\/strong> \u0686\u0627\u0631\u0686\u0648\u0628\u06cc \u0628\u0631\u0627\u06cc \u0633\u0627\u062e\u062a\u0646 \u0627\u067e\u0644\u06cc\u06a9\u06cc\u0634\u0646 \u0647\u0627\u06cc \u06a9\u0627\u0631\u0622\u0645\u062f \u0648 \u0645\u0642\u06cc\u0627\u0633 \u067e\u0630\u06cc\u0631 Node.js \u0633\u0645\u062a \u0633\u0631\u0648\u0631 \u0627\u0633\u062a.  \u0627\u06cc\u0646 \u06cc\u06a9 \u0633\u0627\u062e\u062a\u0627\u0631 \u0648 \u0637\u06cc\u0641 \u0648\u0633\u06cc\u0639\u06cc \u0627\u0632 \u0648\u06cc\u0698\u06af\u06cc \u0647\u0627 \u0631\u0627 \u0627\u0631\u0627\u0626\u0647 \u0645\u06cc \u062f\u0647\u062f \u06a9\u0647 \u062a\u0648\u0633\u0639\u0647 \u0648 \u0622\u0632\u0645\u0627\u06cc\u0634 \u0628\u0631\u0646\u0627\u0645\u0647 \u0647\u0627 \u0631\u0627 \u0622\u0633\u0627\u0646 \u062a\u0631 \u0645\u06cc \u06a9\u0646\u062f.<\/p>\n<p><strong>Postgres<\/strong> \u06cc\u06a9 \u0633\u06cc\u0633\u062a\u0645 \u0645\u062f\u06cc\u0631\u06cc\u062a \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 \u0631\u0627\u0628\u0637\u0647 \u0627\u06cc \u0645\u0646\u0628\u0639 \u0628\u0627\u0632 \u0642\u062f\u0631\u062a\u0645\u0646\u062f \u0627\u0633\u062a \u06a9\u0647 \u0628\u0631\u0627\u06cc \u0633\u0627\u062e\u062a\u0627\u0631\u0647\u0627\u06cc \u062f\u0627\u062f\u0647 \u067e\u06cc\u0686\u06cc\u062f\u0647 \u0648 \u067e\u0631\u062f\u0627\u0632\u0634 \u062f\u0627\u062f\u0647 \u062f\u0631 \u0645\u0642\u06cc\u0627\u0633 \u0628\u0632\u0631\u06af \u0645\u0646\u0627\u0633\u0628 \u0627\u0633\u062a.<\/p>\n<p><strong>TypeORM<\/strong> \u0627\u0628\u0632\u0627\u0631\u06cc \u0645\u0646\u0628\u0639 \u0628\u0627\u0632 Object Relational Mapping (ORM) \u0627\u0633\u062a \u06a9\u0647 \u0631\u0627\u0647\u06cc \u0628\u0631\u0627\u06cc \u062a\u0639\u0627\u0645\u0644 \u0628\u0627 \u067e\u0627\u06cc\u06af\u0627\u0647 \u0647\u0627\u06cc \u062f\u0627\u062f\u0647 \u0631\u0627\u0628\u0637\u0647 \u0627\u06cc \u0645\u0627\u0646\u0646\u062f MySQL\u060c PostgreSQL \u0648 SQLite \u0627\u0631\u0627\u0626\u0647 \u0645\u06cc \u062f\u0647\u062f.  \u0627\u06cc\u0646 \u0628\u0647 \u0634\u0645\u0627 \u06a9\u0645\u06a9 \u0645\u06cc \u06a9\u0646\u062f \u062a\u0627 \u0628\u0647 \u062c\u0627\u06cc \u0646\u0648\u0634\u062a\u0646 \u062f\u0633\u062a\u0648\u0631\u0627\u062a SQL \u062e\u0627\u0645\u060c \u06a9\u062f\u0647\u0627\u06cc \u0645\u0631\u062a\u0628\u0637 \u0628\u0627 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 \u0631\u0627 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 \u0628\u0631\u0646\u0627\u0645\u0647 \u0646\u0648\u06cc\u0633\u06cc \u0634\u06cc \u06af\u0631\u0627 \u062f\u0631 TypeScript \u06cc\u0627 JavaScript \u0628\u0646\u0648\u06cc\u0633\u06cc\u062f.  TypeORM \u0648\u06cc\u0698\u06af\u06cc \u0647\u0627\u06cc \u0632\u06cc\u0627\u062f\u06cc \u0645\u0627\u0646\u0646\u062f \u0645\u062f\u06cc\u0631\u06cc\u062a \u062a\u0631\u0627\u06a9\u0646\u0634\u060c \u0627\u0646\u062a\u0642\u0627\u0644 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647\u060c \u0627\u062f\u063a\u0627\u0645 \u0627\u062a\u0635\u0627\u0644 \u0648 \u063a\u06cc\u0631\u0647 \u0631\u0627 \u0627\u0631\u0627\u0626\u0647 \u0645\u06cc \u062f\u0647\u062f \u06a9\u0647 \u06a9\u0627\u0631 \u0628\u0627 \u067e\u0627\u06cc\u06af\u0627\u0647 \u0647\u0627\u06cc \u062f\u0627\u062f\u0647 \u0631\u0627 \u062f\u0631 \u0628\u0631\u0646\u0627\u0645\u0647 \u0647\u0627\u06cc \u0634\u0645\u0627 \u0622\u0633\u0627\u0646 \u0645\u06cc \u06a9\u0646\u062f.<\/p>\n<p><strong>\u062f\u0627\u06a9\u0631<\/strong> \u06cc\u06a9 \u067e\u0644\u062a \u0641\u0631\u0645 Containerization \u0627\u0633\u062a \u06a9\u0647 \u0627\u0633\u062a\u0642\u0631\u0627\u0631 \u0648 \u0627\u062c\u0631\u0627\u06cc \u0628\u0631\u0646\u0627\u0645\u0647 \u0647\u0627 \u0631\u0627 \u062f\u0631 \u0645\u062d\u06cc\u0637 \u0647\u0627\u06cc \u0645\u062e\u062a\u0644\u0641 \u0622\u0633\u0627\u0646 \u062a\u0631 \u0645\u06cc \u06a9\u0646\u062f.  \u0645\u0627 \u06cc\u06a9 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 Postgres \u0648 \u062e\u062f\u0645\u0627\u062a \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0631\u0627 \u062f\u0631 docker \u0631\u0627\u0647 \u0627\u0646\u062f\u0627\u0632\u06cc \u062e\u0648\u0627\u0647\u06cc\u0645 \u06a9\u0631\u062f.<\/p>\n<hr\/>\n<p><strong>\u0644\u0627\u0646\u0647<\/strong>:<br \/>\u0627\u0628\u062a\u062f\u0627 nest-cli \u0631\u0627 \u0646\u0635\u0628 \u06a9\u0646\u06cc\u062f<br \/><code>npm i -g @nestjs\/cli<\/code><\/p>\n<p>\u0633\u067e\u0633 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 nest cli \u06cc\u06a9 \u067e\u0631\u0648\u0698\u0647 \u062c\u062f\u06cc\u062f \u0627\u06cc\u062c\u0627\u062f \u06a9\u0646\u06cc\u062f<br \/><code>nest new project-name<\/code><\/p>\n<p>\u0627\u0646\u062a\u062e\u0627\u0628 \u06a9\u0646\u06cc\u062f: npm \u0628\u0631\u0627\u06cc \u0645\u062f\u06cc\u0631\u06cc\u062a \u0628\u0633\u062a\u0647\u060c<\/p>\n<p>\u0633\u067e\u0633 \u062f\u0631 \u067e\u0648\u0634\u0647 \u067e\u0631\u0648\u0698\u0647 \u0648\u0627\u0631\u062f \u0634\u0648\u06cc\u062f<br \/><code>cd project-name<\/code><\/p>\n<hr\/>\n<p><strong>\u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 PostgreSQL<\/strong><br \/>pg \u0631\u0627 \u0646\u0635\u0628 \u06a9\u0646\u06cc\u062f<br \/><code>npm install pg --save<\/code><\/p>\n<hr\/>\n<p><strong>TypeORM<\/strong><br \/>TypeOrm \u0631\u0627 \u0646\u0635\u0628 \u06a9\u0646\u06cc\u062f<br \/><code>npm install --save @nestjs\/typeorm typeorm postgres<\/code><\/p>\n<hr\/>\n<p>\u067e\u0631\u0648\u0698\u0647 \u062e\u0648\u062f \u0631\u0627 \u0628\u0627 IDE \u062e\u0648\u062f \u0628\u0627\u0632 \u06a9\u0646\u06cc\u062f<\/p>\n<p>\u062f\u0631 app.module.ts\u060c \u0627\u062a\u0635\u0627\u0644 \u0631\u0627 \u0628\u0627 TypeOrmModule.forRoot():<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ app.module.ts\nimport { Module } from '@nestjs\/common';\nimport { TypeOrmModule } from '@nestjs\/typeorm';\n\n@Module({\n  imports: [\n    TypeOrmModule.forRoot({\n      type: 'postgres',\n      host: 'localhost',\n      port: 5432,\n      username: 'root',\n      password: 'password',\n      database: 'postgres',\n      entities: [`${__dirname}\/typeorm\/entities\/*{.js,.ts}`],\n      synchronize: true, \/\/ do not use in prod\n    }),\n  ],\n})\nexport class AppModule {}\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>BTW: \u0645\u06cc \u062a\u0648\u0627\u0646\u06cc\u062f app.service \u0648 app.controller \u0631\u0627 \u062d\u0630\u0641 \u06a9\u0646\u06cc\u062f<\/p>\n<hr\/>\n<p>\u0627\u06a9\u0646\u0648\u0646 \u0628\u0627\u06cc\u062f \u0627\u0648\u0644\u06cc\u0646 \u0645\u0627\u0698\u0648\u0644 \u062e\u0648\u062f \u0631\u0627 \u0628\u0627 nest cli \u0627\u06cc\u062c\u0627\u062f \u06a9\u0646\u06cc\u0645:<br \/><code>nest g resource modules\/users<\/code><\/p>\n<p>REST API \u0631\u0627 \u062f\u0631 \u062a\u0631\u0645\u06cc\u0646\u0627\u0644 \u0627\u0646\u062a\u062e\u0627\u0628 \u06a9\u0646\u06cc\u062f<br \/>\u0628\u0631\u0627\u06cc \u0627\u06cc\u062c\u0627\u062f \u0646\u0642\u0627\u0637 \u0648\u0631\u0648\u062f\u06cc CRUD\u060c &#8220;\u0628\u0644\u0647&#8221; \u0631\u0627 \u0627\u0646\u062a\u062e\u0627\u0628 \u06a9\u0646\u06cc\u062f<\/p>\n<hr\/>\n<p>\u0628\u0627 \u0627\u06cc\u062c\u0627\u062f \u0645\u0627\u0698\u0648\u0644\u060c \u0627\u06a9\u0646\u0648\u0646 \u0628\u0627\u06cc\u062f \u067e\u0631\u0648\u0698\u0647 \u062e\u0648\u062f \u0631\u0627 \u0628\u0647 \u0627\u06cc\u0646 \u0635\u0648\u0631\u062a \u0633\u0627\u0632\u0645\u0627\u0646\u062f\u0647\u06cc \u06a9\u0646\u06cc\u0645:<\/p>\n<p>1) \u0686\u0646\u062f \u067e\u0648\u0634\u0647 \u0628\u0647 \u0646\u0627\u0645 typeorm > database \/ entities \/ seed \u0627\u06cc\u062c\u0627\u062f \u06a9\u0646\u06cc\u062f<br \/>2) data-source.ts\u060c main.seed.ts \u0648 user.seed.ts \u0631\u0627 \u0627\u06cc\u062c\u0627\u062f \u06a9\u0646\u06cc\u062f<br \/>3) \u0645\u0648\u062c\u0648\u062f\u06cc\u062a \u06a9\u0627\u0631\u0628\u0631 \u0631\u0627 \u0628\u0647 \u067e\u0648\u0634\u0647 \u0647\u0627\u06cc \u0645\u0648\u062c\u0648\u062f\u06cc\u062a \u0645\u0646\u062a\u0642\u0644 \u06a9\u0646\u06cc\u062f<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/src\/typeorm\n\u251c\u2500\u2500 database\n\u2502   \u2514\u2500\u2500 data-source.ts\n\u251c\u2500\u2500 seeds\n\u2502   \u2514\u2500\u2500 main.seed.ts\n\u2514\u2500\u2500 entities\n    \u2514\u2500\u2500 user.entity.ts *(generated by the script)*\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<hr\/>\n<p>\u06a9\u062f \u0632\u06cc\u0631 \u0631\u0627 \u062f\u0631 \u0641\u0627\u06cc\u0644 \u0647\u0627 \u0642\u0631\u0627\u0631 \u062f\u0647\u06cc\u062f:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ users.entity.ts\nimport { Column, CreateDateColumn, DeleteDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from 'typeorm';\n\n@Entity('users')\nexport class User {\n  @PrimaryGeneratedColumn()\n  id: number;\n\n  @Column()\n  name: string;\n\n  @Column()\n  email: string;\n\n  @Column()\n  active: boolean;\n\n  @Column()\n  birthday: Date;\n\n  @CreateDateColumn()\n  createdAt: Date;\n\n  @UpdateDateColumn()\n  updatedAt: Date;\n\n  @DeleteDateColumn()\n  deletedAt: Date;\n}\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ users.service.ts\nimport { Injectable } from '@nestjs\/common';\nimport { User } from 'src\/typeorm\/entities\/user.entity';\nimport { DataSource } from 'typeorm';\nimport { CreateUserDto } from '.\/dto\/create-user.dto';\nimport { UpdateUserDto } from '.\/dto\/update-user.dto';\n\n@Injectable()\nexport class UsersService {\n  constructor(private dataSource: DataSource) {}\n  private userRepository = this.dataSource.getRepository(User);\n\n  async create(createUserDto: CreateUserDto) {\n    return this.userRepository.save(createUserDto);\n  }\n\n  async findAll(): Promise&lt;User[]&gt; {\n    return this.userRepository.find();\n  }\n\n  async findOne(id: number) {\n    return this.userRepository.findOneBy({ id });\n  }\n\n  async update(id: number, updateUserDto: UpdateUserDto) {\n    let user = await this.findOne(id);\n    user = { ...user, ...updateUserDto };\n    return await this.userRepository.save(user);\n  }\n\n  async remove(id: number) {\n    const user = await this.findOne(id);\n    return await this.userRepository.softRemove(user);\n  }\n}\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ data-source.ts\nimport { DataSource, DataSourceOptions } from 'typeorm';\n\nexport const dataSourceOptions: DataSourceOptions = {\n  type: 'postgres',\n  host: 'localhost',\n  port: 5432,\n  username: 'root',\n  password: 'password',\n  database: 'postgres',\n  entities: ['src\/**\/*.entity{.js,.ts}'],\n};\n\nexport default new DataSource(dataSourceOptions);\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ main.seed.ts\nimport { DataSource } from 'typeorm';\nimport { dataSourceOptions } from '..\/database\/data-source';\nimport { User } from '..\/entities\/user.entity';\n\nconst dataSource = new DataSource(dataSourceOptions);\nconst userRepository = dataSource.getRepository(User);\n\nasync function connect() {\n  try {\n    if (dataSource.isInitialized) {\n      await dataSource.destroy();\n    }\n    await dataSource.initialize();\n    console.log('Data Source has been initialized!');\n  } catch (err) {\n    console.error('Error during Data Source connect', err);\n  }\n}\n\nasync function disconnect() {\n  try {\n    await dataSource.destroy();\n\n    console.log('Data Source disconnected!');\n  } catch (err) {\n    console.error('Error during Data Source disconnect', err);\n  }\n}\n\nasync function seed() {\n  const UserSeed = () =&gt; [\n    {\n      name: 'John Doe',\n      email: 'john@doe.com',\n      active: true,\n      birthday: '1990-01-01',\n    },\n    {\n      name: 'Jane Doe',\n      email: 'jane@doe.com',\n      active: false,\n      birthday: '1990-01-31',\n    },\n    {\n      name: 'Josh Doe',\n      email: 'josh@doe.com',\n      active: true,\n      birthday: '2020-12-31',\n    },\n  ];\n\n  await userRepository.save(UserSeed());\n  console.log('created seeds');\n}\n\nasync function runSeed() {\n  await connect();\n  console.log('connected');\n  await seed();\n  console.log('seed done');\n  await disconnect();\n  console.log('disconnected');\n}\n\nrunSeed();\n\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<hr\/>\n<p>\u0627\u06a9\u0646\u0648\u0646\u060c \u0645\u0627 \u0628\u0627\u06cc\u062f \u0641\u0627\u06cc\u0644 &#8216;docker-compose.yml&#8217; \u062e\u0648\u062f \u0631\u0627 \u062f\u0631 \u067e\u0648\u0634\u0647 \u0631\u06cc\u0634\u0647 \u0627\u06cc\u062c\u0627\u062f \u06a9\u0631\u062f\u0647 \u0648 \u0633\u067e\u0633 \u0627\u062c\u0631\u0627 \u06a9\u0646\u06cc\u0645 <code>sudo docker-compose up<\/code>:<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>version: '3.1'\n\nservices:\n  db:\n    image: postgres:14.2-alpine\n    restart: always\n    environment:\n      POSTGRES_USER: \"root\"\n      POSTGRES_PASSWORD: \"password\"\n      POSTGRES_DB: \"postgres\"\n    ports:\n      - \"5432:5432\"\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>\u0686\u0646\u062f \u062f\u0633\u062a\u0648\u0631 \u0645\u0641\u06cc\u062f docker:<br \/><code>sudo docker ps<\/code>  \u0628\u0631\u0627\u06cc \u0641\u0647\u0631\u0633\u062a \u06a9\u0631\u062f\u0646 \u0647\u0645\u0647 \u06a9\u0627\u0646\u062a\u06cc\u0646\u0631\u0647\u0627<br \/><code>sudo docker stop my_container_name<\/code>  \u0628\u0631\u0627\u06cc \u0645\u062a\u0648\u0642\u0641 \u06a9\u0631\u062f\u0646 \u06cc\u06a9 \u0638\u0631\u0641 \u062e\u0627\u0635<\/p>\n<hr\/>\n<p>\u062d\u0627\u0644\u0627 \u06cc\u06a9 \u0627\u0633\u06a9\u0631\u06cc\u067e\u062a \u0628\u0647 package.json \u0627\u0636\u0627\u0641\u0647 \u06a9\u0646\u06cc\u062f<\/p>\n<div class=\"highlight js-code-highlight\">\n<pre class=\"highlight plaintext\"><code>\/\/ package.json\n\"seed\":\"npx ts-node src\/typeorm\/seeds\/main.seed.ts\",\n<\/code><\/pre>\n<div class=\"highlight__panel js-actions-panel\">\n<div class=\"highlight__panel-action js-fullscreen-code-action\">\n    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-on\"><title>\u0648\u0627\u0631\u062f \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M16 3h6v6h-2V5h-4V3zM2 3h6v2H4v4H2V3zm18 16v-4h2v6h-6v-2h4zM4 19h4v2H2v-6h2v4z\"\/>\n<\/svg><\/p>\n<p>    <svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" class=\"highlight-action crayons-icon highlight-action--fullscreen-off\"><title>\u0627\u0632 \u062d\u0627\u0644\u062a \u062a\u0645\u0627\u0645 \u0635\u0641\u062d\u0647 \u062e\u0627\u0631\u062c \u0634\u0648\u06cc\u062f<\/title>\n    <path d=\"M18 7h4v2h-6V3h2v4zM8 9H2V7h4V3h2v6zm10 8v4h-2v-6h6v2h-4zM8 15v6H6v-4H2v-2h6z\"\/>\n<\/svg><\/p>\n<\/div>\n<\/div>\n<\/div>\n<p>\u062f\u0631 \u06cc\u06a9 \u062a\u0631\u0645\u06cc\u0646\u0627\u0644 \u062c\u062f\u06cc\u062f \u062f\u0631 IDE \u06a9\u062f \u0631\u0627 \u0627\u062c\u0631\u0627 \u06a9\u0646\u06cc\u062f <code>npm start<\/code> \u0628\u0631\u0627\u06cc \u0647\u0645\u06af\u0627\u0645 \u0633\u0627\u0632\u06cc \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647<\/p>\n<p>\u0628\u0631\u0646\u0627\u0645\u0647 \u0631\u0627 \u0628\u06a9\u0634\u06cc\u062f \u0648 \u0633\u067e\u0633 \u0627\u0633\u06a9\u0631\u06cc\u067e\u062a \u0631\u0627 \u0627\u062c\u0631\u0627 \u06a9\u0646\u06cc\u062f <code>npm run seed<\/code> \u0628\u0631\u0627\u06cc \u067e\u0631 \u06a9\u0631\u062f\u0646 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647<\/p>\n<hr\/>\n<p>\u0627\u0646\u062c\u0627\u0645 \u0634\u062f!  \u062d\u0627\u0644\u0627 \u0627\u06af\u0631 \u0641\u0631\u0627\u0631 \u06a9\u0646\u06cc <code>npm start<\/code> \u0648 \u0627\u0631\u0633\u0627\u0644 \u06a9\u0646\u06cc\u062f <code>GET<\/code> \u062f\u0631\u062e\u0648\u0627\u0633\u062a \u062f\u0631 <code>localhost:3000\/users<\/code>\u060c \u0628\u0627\u06cc\u062f \u0647\u0645\u0647 \u06a9\u0627\u0631\u0628\u0631\u0627\u0646 \u0631\u0627 \u0627\u0632 seed \u062f\u0631\u06cc\u0627\u0641\u062a \u06a9\u0646\u06cc\u062f.<\/p>\n<\/p><\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u062f\u0631 \u0627\u06cc\u0646 \u067e\u0633\u062a \u0648\u0628\u0644\u0627\u06af\u060c \u0645\u0646 \u0628\u0647 \u0634\u0645\u0627 \u0646\u0634\u0627\u0646 \u062e\u0648\u0627\u0647\u0645 \u062f\u0627\u062f \u06a9\u0647 \u0686\u06af\u0648\u0646\u0647 \u06cc\u06a9 \u067e\u0631\u0648\u0698\u0647 \u0631\u0627 \u0628\u0627 \u0627\u0633\u062a\u0641\u0627\u062f\u0647 \u0627\u0632 TypeScript\u060c Nest\u060c Postgres\u060c Docker \u062a\u0646\u0638\u06cc\u0645 \u06a9\u0646\u06cc\u062f \u0648 \u0686\u06af\u0648\u0646\u0647 \u067e\u0627\u06cc\u06af\u0627\u0647 \u062f\u0627\u062f\u0647 \u062e\u0648\u062f \u0631\u0627 \u0628\u0630\u0631 \u06a9\u0646\u06cc\u062f. TypeScript \u06cc\u06a9 \u0632\u0628\u0627\u0646 \u062a\u0627\u06cc\u067e \u0627\u06cc\u0633\u062a\u0627 \u0627\u0633\u062a \u06a9\u0647 \u06cc\u06a9 \u0627\u0628\u0631 \u0645\u062c\u0645\u0648\u0639\u0647 \u0646\u062d\u0648\u06cc \u062f\u0642\u06cc\u0642 \u062c\u0627\u0648\u0627 \u0627\u0633\u06a9\u0631\u06cc\u067e\u062a \u0627\u0633\u062a. \u0627\u06cc\u0645\u0646\u06cc \u0646\u0648\u0639 \u0628\u0647\u0628\u0648\u062f \u06cc\u0627\u0641\u062a\u0647 \u0648 \u067e\u0634\u062a\u06cc\u0628\u0627\u0646\u06cc \u0627\u0632 \u0627\u0628\u0632\u0627\u0631 &hellip;<\/p>\n","protected":false},"author":2,"featured_media":13281,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"","fifu_image_alt":"","footnotes":""},"categories":[339],"tags":[],"class_list":["post-13280","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dev"],"_links":{"self":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/13280","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/comments?post=13280"}],"version-history":[{"count":0,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/posts\/13280\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media\/13281"}],"wp:attachment":[{"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/media?parent=13280"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/categories?post=13280"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nabfollower.com\/blog\/wp-json\/wp\/v2\/tags?post=13280"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}