Skip to content

Commit

Permalink
refactor(TypeScript/ditsmod): upgrade to v3. (#9475)
Browse files Browse the repository at this point in the history
  • Loading branch information
KostyaTretyak authored Dec 25, 2024
1 parent 082b573 commit 989d88d
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 39 deletions.
12 changes: 6 additions & 6 deletions frameworks/TypeScript/ditsmod/benchmark_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
},
Expand All @@ -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"
}
Expand Down
4 changes: 2 additions & 2 deletions frameworks/TypeScript/ditsmod/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
6 changes: 2 additions & 4 deletions frameworks/TypeScript/ditsmod/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
16 changes: 5 additions & 11 deletions frameworks/TypeScript/ditsmod/src/app/init.extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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 });
}
Expand Down
27 changes: 12 additions & 15 deletions frameworks/TypeScript/ditsmod/src/app/one.controller.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
2 changes: 1 addition & 1 deletion frameworks/TypeScript/ditsmod/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

0 comments on commit 989d88d

Please sign in to comment.