Browse Source

refactor(TypeScript/ditsmod): upgrade to v3. (#9475)

Костя Третяк 7 months ago
parent
commit
989d88d1b3

+ 6 - 6
frameworks/TypeScript/ditsmod/benchmark_config.json

@@ -18,7 +18,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod",
+        "display_name": "ditsmod v3.0",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "nodejs"
       },
@@ -41,7 +41,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod [postgres]",
+        "display_name": "ditsmod v3.0 [postgres]",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "nodejs"
       },
@@ -64,7 +64,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod [mysql]",
+        "display_name": "ditsmod v3.0 [mysql]",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "nodejs"
       },
@@ -84,7 +84,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod on bun",
+        "display_name": "ditsmod-bun v3.0",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "bun"
       },
@@ -107,7 +107,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod on bun [postgres]",
+        "display_name": "ditsmod-bun v3.0 [postgres]",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "bun"
       },
@@ -130,7 +130,7 @@
         "webserver": "None",
         "os": "Linux",
         "database_os": "Linux",
-        "display_name": "ditsmod on bun [mysql]",
+        "display_name": "ditsmod-bun v3.0 [mysql]",
         "notes": "Simplified use of Dependency Injection (no request level injector).",
         "versus": "bun"
       }

+ 2 - 2
frameworks/TypeScript/ditsmod/package.json

@@ -14,8 +14,8 @@
   "author": "Костя Третяк",
   "license": "MIT",
   "dependencies": {
-    "@ditsmod/core": "~2.55.0",
-    "@ditsmod/routing": "~2.3.0",
+    "@ditsmod/core": "^3.0.0-alpha.2",
+    "@ditsmod/routing": "^3.0.0-alpha.2",
     "handlebars": "^4.7.8",
     "lru-cache": "^11.0.0",
     "mariadb": "^3.3.1",

+ 2 - 4
frameworks/TypeScript/ditsmod/src/app/app.module.ts

@@ -2,15 +2,13 @@ import { Providers, rootModule } from '@ditsmod/core';
 import { PRE_ROUTER_EXTENSIONS, RoutingModule } from '@ditsmod/routing';
 
 import { OneController } from './one.controller.js';
-import { DbService } from './db.service.js';
 import { InitExtension } from './init.extension.js';
 import { DB_INIT_EXTENSIONS } from './tokens.js';
-import { ModelService } from './types.js';
 
 @rootModule({
   imports: [RoutingModule],
-  providersPerApp: new Providers().passThrough(DbService).passThrough(ModelService).useLogConfig({ level: 'off' }),
-  extensions: [{ extension: InitExtension, groupToken: DB_INIT_EXTENSIONS, nextToken: PRE_ROUTER_EXTENSIONS }],
+  providersPerApp: new Providers().useLogConfig({ level: 'off' }),
+  extensions: [{ extension: InitExtension, group: DB_INIT_EXTENSIONS, beforeGroup: PRE_ROUTER_EXTENSIONS }],
   controllers: [OneController],
 })
 export class AppModule {}

+ 5 - 11
frameworks/TypeScript/ditsmod/src/app/init.extension.ts

@@ -5,18 +5,12 @@ import { ModelService } from './types.js';
 
 @injectable()
 export class InitExtension implements Extension<void> {
-  #inited: boolean;
-
   constructor(
     private perAppService: PerAppService,
     private logger: Logger,
   ) {}
 
-  async init(): Promise<void> {
-    if (this.#inited) {
-      return;
-    }
-
+  async stage1(): Promise<void> {
     const dbType = process.env.DATABASE as 'mysql' | 'postgres';
 
     if (dbType == 'mysql') {
@@ -28,13 +22,13 @@ export class InitExtension implements Extension<void> {
     } else {
       this.logger.log('warn', `Unknown database "${dbType}"`);
     }
-
-    this.#inited = true;
   }
 
   protected async setDbService(useClass: Class) {
-    const injector = this.perAppService.injector.resolveAndCreateChild([{ token: ModelService, useClass }]);
-    const dbService = injector.pull(DbService) as DbService;
+    const dbService = this.perAppService.injector
+      .resolveAndCreateChild([DbService, { token: ModelService, useClass }])
+      .get(DbService) as DbService;
+
     await dbService.setWorldsToCache();
     this.perAppService.providers.push({ token: DbService, useValue: dbService });
   }

+ 12 - 15
frameworks/TypeScript/ditsmod/src/app/one.controller.ts

@@ -1,4 +1,5 @@
-import { AnyObj, controller, RequestContext, SingletonRequestContext, route } from '@ditsmod/core';
+import { AnyObj, controller, RequestContext, SingletonRequestContext, optional } from '@ditsmod/core';
+import { route } from '@ditsmod/routing';
 import Handlebars from 'handlebars';
 
 import { DbService } from './db.service.js';
@@ -27,9 +28,9 @@ const tmpl = Handlebars.compile(
   ].join(''),
 );
 
-@controller({ isSingleton: true })
+@controller({ scope: 'module' })
 export class OneController {
-  constructor(private dbService: DbService) {}
+  constructor(@optional() private dbService: DbService) {}
 
   @route('GET', 'db')
   async getSingleQuery(ctx: RequestContext) {
@@ -61,28 +62,24 @@ export class OneController {
     const fortunes = await this.dbService.findAllFortunes();
     fortunes.push(additionalFortune);
     fortunes.sort(compare);
-    ctx.nodeRes.setHeader('Server', 'Ditsmod');
-    ctx.nodeRes.setHeader('Content-Type', 'text/html; charset=utf-8');
-    ctx.nodeRes.end(tmpl({ fortunes }));
+    ctx.rawRes.setHeader('Server', 'Ditsmod');
+    ctx.rawRes.setHeader('Content-Type', 'text/html; charset=utf-8');
+    ctx.rawRes.end(tmpl({ fortunes }));
   }
 
   @route('GET', 'plaintext')
   getHello(ctx: SingletonRequestContext) {
-    ctx.nodeRes.setHeader('Server', 'Ditsmod');
-    ctx.nodeRes.setHeader('Content-Type', 'text/plain; charset=utf-8');
-    ctx.nodeRes.end('Hello, World!');
+    ctx.rawRes.setHeader('Server', 'Ditsmod');
+    ctx.rawRes.setHeader('Content-Type', 'text/plain; charset=utf-8');
+    ctx.rawRes.end('Hello, World!');
   }
 
   @route('GET', 'json')
   getJson(ctx: SingletonRequestContext) {
-    ctx.nodeRes.setHeader('Server', 'Ditsmod');
-    ctx.nodeRes.setHeader('Content-Type', 'application/json; charset=utf-8');
-    ctx.nodeRes.end(JSON.stringify({ message: 'Hello, World!' }));
+    this.sendJson(ctx, { message: 'Hello, World!' });
   }
 
   protected sendJson(ctx: RequestContext, value: AnyObj) {
-    ctx.nodeRes.setHeader('Server', 'Ditsmod');
-    ctx.nodeRes.setHeader('Content-Type', 'application/json; charset=utf-8');
-    ctx.nodeRes.end(JSON.stringify(value));
+    ctx.setHeader('Server', 'Ditsmod').sendJson(value);
   }
 }

+ 1 - 1
frameworks/TypeScript/ditsmod/src/main.ts

@@ -13,6 +13,6 @@ if (numCpus > 1 && cluster.isPrimary) {
   }
 } else {
   const serverOptions: ServerOptions = { keepAlive: true, keepAliveTimeout: 0 };
-  const app = await new Application().bootstrap(AppModule, { serverOptions });
+  const app = await Application.create(AppModule, { serverOptions });
   app.server.listen(8080, '0.0.0.0');
 }