Announcing Ditsmod 2.0 beta (with RealWorld example)
Tags: node, typescript, ditsmod, nestjs
Published: 2022-01-10 09:27:49

Today I'm excited to announce the release of Ditsmod 2.0 beta!

For those who aren’t familiar with Ditsmod, it's NodeJS framework written in TypeScript. To learn more, you can visit [our website][1].

To get started using Ditsmod 2.0 beta, you can get it from github [ditsmod/seed][2]:

```bash
git clone https://github.com/ditsmod/seed.git ditsmod-app
cd ditsmod-app
yarn
```

Also, you can browse [Ditsmod application][3] containing real world examples (CRUD, auth, advanced patterns, etc) that adheres to the [RealWorld spec and API][4].

For most users, Ditsmod is a hitherto unknown framework, so I will present it here as for the first time.

Some major highlights of Ditsmod are:
- Modular architecture on decorators, which allows you to declaratively describe the structure of the application.
- Convenient mechanism for [specifying and resolving][5] between different application classes: you in constructor specify instances of which classes you need, and DI undertakes a difficult task "how to get it".
- Ability to write your own extensions (sometimes called plugins) that can be asynchronously initialized and that can depend on each other.
- Ability to dynamically add and remove modules after starting the web server, without the need to restart.
- Has OpenAPI support, and has the ability to validate requests based on OpenAPI metadata.
- To date, Ditsmod is [one of the fastest][6] among Node.js web frameworks.

Example controller:

```ts
import { Controller, Res, Route } from '@ditsmod/core';

@controller()
export class HelloWorldController {
  constructor(private res: Res) {}

  @route('GET')
  tellHello() {
    this.res.send('Hello World!');
  }
}
```

Example module that has controllers:

```ts
import { featureModule } from '@ditsmod/core';

import { Params } from '@models/params';
import { ArticlesController } from './articles.controller';
import { CommentsModule } from './comments/comments.module';
import { DbService } from './db.service';
import { FavoriteModule } from './favorite/favorite.module';

@featureModule({
  imports: [
    { prefix: 'comments', module: CommentsModule },
    { prefix: 'favorite', module: FavoriteModule },
  ],
  controllers: [ArticlesController],
  providersPerReq: [DbService]
})
export class ArticlesModule {}
```

Example module that has services only:

```ts
import { featureModule } from '@ditsmod/core';
import { JwtModule } from '@ditsmod/jwt';

import { AuthService } from './auth.service';
import { BearerGuard } from './bearer.guard';
import { ModuleConfigService } from './config.service';
import { CryptoService } from './crypto.service';
import { PermissionsGuard } from './permissions.guard';

const jwtModuleWithParams = JwtModule.withParams({ secret: process.env.JWT_SECRET, signOptions: { expiresIn: '1y' } });

@featureModule({
  imports: [jwtModuleWithParams],
  providersPerMod: [ModuleConfigService],
  providersPerReq: [BearerGuard, CryptoService, AuthService, PermissionsGuard],
  exports: [BearerGuard, CryptoService, AuthService, PermissionsGuard]
})
export class AuthModule {}
```

## Ditsmod vs NestJS

Ditsmod is similar to NestJS only because both of these frameworks are inspired by the concepts that [Angular][7] has. But Ditsmod under the hood does not have Express, Fastify or other frameworks. Ditsmod is 11% faster than NestJS + Fastify, provided that an instance of controller is created for each request. And more than twice as fast as NestJS + Express. Ditsmod also consumes significantly less memory compared to NestJS.

Thanks to the system of extensions, Ditsmod applications are more expressive and with less code. For example, if you compare OpenAPI modules written in [Ditsmod][8] and [NestJS][9], the Ditsmod module is 3.5 times smaller in the number of lines of code or the number of files.


[1]: https://ditsmod.github.io/en/
[2]: https://github.com/ditsmod/seed
[3]: https://github.com/ditsmod/realworld
[4]: https://github.com/gothinkster/realworld
[5]: https://en.wikipedia.org/wiki/Dependency_injection
[6]: https://github.com/ditsmod/vs-webframework#readme
[7]: https://github.com/angular/angular
[8]: https://github.com/ditsmod/ditsmod/tree/main/packages/openapi/src
[9]: https://github.com/nestjs/swagger/tree/master/lib