From 4f1e1475ba1fca00cfa95aedce5cde790fb0d474 Mon Sep 17 00:00:00 2001 From: Aleksey Stepanov Date: Thu, 3 Aug 2023 11:30:43 +0700 Subject: [PATCH] Added IOC-container --- booksService/package-lock.json | 24 +++++++++++++++++++++++- booksService/package.json | 4 +++- booksService/src/index.ts | 1 + booksService/src/ioc-container.ts | 7 +++++++ booksService/src/routes/books.ts | 10 +++++++++- booksService/tsconfig.json | 7 +++++-- 6 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 booksService/src/ioc-container.ts diff --git a/booksService/package-lock.json b/booksService/package-lock.json index a90d11a..cb1f68b 100644 --- a/booksService/package-lock.json +++ b/booksService/package-lock.json @@ -13,11 +13,13 @@ "express": "^4.18.2", "express-ejs-layouts": "^2.5.1", "express-session": "^1.17.3", + "inversify": "^6.0.1", "module-alias": "^2.2.2", "mongoose": "^7.2.2", "multer": "^1.4.5-lts.1", "passport": "^0.6.0", "passport-local": "^1.0.0", + "reflect-metadata": "^0.1.13", "socket.io": "^4.7.1", "uuid": "^9.0.0", "yargs": "^17.7.2" @@ -43,7 +45,7 @@ }, "engines": { "node": "18.x", - "npm": "8.x" + "npm": ">8.x" } }, "node_modules/@cspotcode/source-map-support": { @@ -2069,6 +2071,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "node_modules/inversify": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/inversify/-/inversify-6.0.1.tgz", + "integrity": "sha512-B3ex30927698TJENHR++8FfEaJGqoWOgI6ZY5Ht/nLUsFCwHn6akbwtnUAPCgUepAnTpe2qHxhDNjoKLyz6rgQ==" + }, "node_modules/ip": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", @@ -2938,6 +2945,11 @@ "node": ">=8.10.0" } }, + "node_modules/reflect-metadata": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", + "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" + }, "node_modules/require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", @@ -5302,6 +5314,11 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "inversify": { + "version": "6.0.1", + "resolved": "https://registry.npmjs.org/inversify/-/inversify-6.0.1.tgz", + "integrity": "sha512-B3ex30927698TJENHR++8FfEaJGqoWOgI6ZY5Ht/nLUsFCwHn6akbwtnUAPCgUepAnTpe2qHxhDNjoKLyz6rgQ==" + }, "ip": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ip/-/ip-2.0.0.tgz", @@ -5920,6 +5937,11 @@ "picomatch": "^2.2.1" } }, + "reflect-metadata": { + "version": "0.1.13", + "resolved": "https://registry.npmjs.org/reflect-metadata/-/reflect-metadata-0.1.13.tgz", + "integrity": "sha512-Ts1Y/anZELhSsjMcU605fU9RE4Oi3p5ORujwbIKXfWa+0Zxs510Qrmrce5/Jowq3cHSZSJqBjypxmHarc+vEWg==" + }, "require-directory": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/require-directory/-/require-directory-2.1.1.tgz", diff --git a/booksService/package.json b/booksService/package.json index d461143..eb0d37c 100644 --- a/booksService/package.json +++ b/booksService/package.json @@ -16,11 +16,13 @@ "express": "^4.18.2", "express-ejs-layouts": "^2.5.1", "express-session": "^1.17.3", + "inversify": "^6.0.1", "module-alias": "^2.2.2", "mongoose": "^7.2.2", "multer": "^1.4.5-lts.1", "passport": "^0.6.0", "passport-local": "^1.0.0", + "reflect-metadata": "^0.1.13", "socket.io": "^4.7.1", "uuid": "^9.0.0", "yargs": "^17.7.2" @@ -46,6 +48,6 @@ }, "engines": { "node": "18.x", - "npm": "8.x" + "npm": ">8.x" } } diff --git a/booksService/src/index.ts b/booksService/src/index.ts index 7770d2b..8ba2c31 100644 --- a/booksService/src/index.ts +++ b/booksService/src/index.ts @@ -1,3 +1,4 @@ +import "reflect-metadata"; import express from "express"; import session from "express-session"; import { Server } from "socket.io"; diff --git a/booksService/src/ioc-container.ts b/booksService/src/ioc-container.ts new file mode 100644 index 0000000..4e32a8e --- /dev/null +++ b/booksService/src/ioc-container.ts @@ -0,0 +1,7 @@ +import { Container } from "inversify"; +import { BooksRepository } from "./BooksRepository"; + +const iocContainer = new Container(); +iocContainer.bind(BooksRepository).toSelf(); + +export { iocContainer }; diff --git a/booksService/src/routes/books.ts b/booksService/src/routes/books.ts index 7608ba3..cd5d470 100644 --- a/booksService/src/routes/books.ts +++ b/booksService/src/routes/books.ts @@ -2,6 +2,8 @@ import { Router } from "express"; import BooksController from "@root/controllers/books"; import fileUpload from "@root/middleware/file"; +import { iocContainer } from "@root/ioc-container"; +import { BooksRepository } from "@root/BooksRepository"; const booksRouter = Router({ strict: true }); @@ -51,7 +53,13 @@ booksRouter.get(BOOKS_PATHS.MVC.EDIT_BOOK, BooksController.getEditBookView); booksRouter.post(BOOKS_PATHS.MVC.EDIT_BOOK, BooksController.updateBook); /** Get book */ -booksRouter.get(BOOKS_PATHS.MVC.GET_BOOK, BooksController.getBookView); +// booksRouter.get(BOOKS_PATHS.MVC.GET_BOOK, BooksController.getBookView); +booksRouter.get(BOOKS_PATHS.MVC.GET_BOOK, async (req, res, next) => { + const repo = iocContainer.get(BooksRepository); + const book = await repo.getBook(req.params.id); + + res.json(book); +}); /** Get all books */ booksRouter.get(BOOKS_PATHS.MVC.GET_BOOKS, BooksController.getBooksView); diff --git a/booksService/tsconfig.json b/booksService/tsconfig.json index 3857b61..b2671a1 100644 --- a/booksService/tsconfig.json +++ b/booksService/tsconfig.json @@ -21,7 +21,7 @@ /* Modules */ "module": "commonjs", /* Specify what module code is generated. */ // "rootDir": "./", /* Specify the root folder within your source files. */ - // "moduleResolution": "node10", /* Specify how TypeScript looks up a file from a given module specifier. */ + "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */ "baseUrl": ".", /* Specify the base directory to resolve non-relative module names. */ "paths": { "@root/*": [ @@ -35,7 +35,8 @@ ], /* Specify multiple folders that act like './node_modules/@types'. */ "types": [ "node", - "express" + "express", + "reflect-metadata" ], // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */ // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */ @@ -44,6 +45,8 @@ // "resolvePackageJsonImports": true, /* Use the package.json 'imports' field when resolving imports. */ // "customConditions": [], /* Conditions to set in addition to the resolver-specific defaults when resolving imports. */ "resolveJsonModule": true, /* Enable importing .json files. */ + "experimentalDecorators": true, + "emitDecoratorMetadata": true, // "allowArbitraryExtensions": true, /* Enable importing files with any extension, provided a declaration file is present. */ // "noResolve": true, /* Disallow 'import's, 'require's or ''s from expanding the number of files TypeScript should add to a project. */