npm package: @jojoxd/tsed-util
Description: Generic Ts.ED Utilities
Author: Joey Vos
Latest version: 1.3.2

# @jojoxd/tsed-util

Multiple small modules to make Ts.ED a bit better.

You can check out the `test/integration` directory for usages.

> Note: Most peer dependencies are marked as optional,  
> read below to find out what dependencies are needed for which functionality.

## MikroORM

```
yarn install @jojoxd/tsed-util @tsed/mikro-orm @mikro-orm/core
```

Usage:

```typescript
import { InjectRepository } from "@jojoxd/tsed-util/mikro-orm";
import { Entity, EntityRepository } from "@mikro-orm/core";

export class MyEntityRepository extends EntityRepository<MyEntity> {}

@Entity({ repository: () => MyEntityRepository, })
export class MyEntity {}

export class MyService
{
    @InjectRepository(MyEntity)
    protected readonly myEntityRepository!: MyEntityRepository;

    // With a specific contextName:
    @InjectRepository(MyEntity, "context")
    protected readonly myContextEntityRepository!: MyEntityRepository;
}
```

## express-session

Note: The ProviderScope should be set to REQUEST on the services you use `@InjectSession()` in.

```typescript
// Session.d.ts
import {ParamOptions} from "@tsed/platform-params";
import {Session as ExpressSession} from "express-session";

declare module "@tsed/common"
{
    // As we are re-declaring the interface, we need to re-define the Session decorator
    export function Session(expression: string): ParameterDecorator;
    export function Session(options: Partial<ParamOptions>): ParameterDecorator;
    export function Session(): ParameterDecorator;

    // Your session variables go here
    export interface Session extends ExpressSession
    {
        mySessionVariable?: string;
    }
}
```

```typescript
// service.ts
import { Injectable, ProviderScope } from "@tsed/di";

// NOTE: Session is just a re-export of Session in "@tsed/common"
import { InjectSession, Session } from "@jojoxd/tsed-util/express-session";

@Injectable({ scope: ProviderScope.REQUEST, })
export class MyService
{
    @InjectSession()
    protected readonly session?: Session;

    public test()
    {
        const sessionVariable = this.session?.mySessionVariable;
    
        // express-session functions are also available:
        this.session?.save();
    }
}
```

## Redoc

```
yarn install @jojoxd/tsed-util @tsed/swagger @tsed/schema
```

```typescript
// Server.ts
import "@tsed/swagger";
import { defineRedocSettings } from "@jojoxd/tsed-util/redoc";

@Configuration({
    swagger: [
        defineRedocSettings({
            // All your spec options go here, mostly the same as the swagger config
            path: '/api/docs',
        }),
    ],
})
export class Server {}
```

## Scheduler

The scheduler is used to schedule jobs using `node-scheduler`.

```
yarn install @jojoxd/tsed-util node-scheduler
```

```typescript
// Server.ts
import "@jojoxd/tsed-util/scheduler";
import "./MyScheduler";

@Configuration({
    scheduler: {
        // Maybe only enable in production
        enabled: process.env.NODE_ENV === "production",

        disableScheduleSummary: false,
        logging: true,
    }
})
export class Server {}

// MyScheduler.ts
import { Scheduler, Schedule } from "@jojoxd/tsed-util/scheduler";

@Scheduler({ namespace: "some-namespace", })
class MyScheduler
{
    // This is a Service, so @Inject will work here

    @Schedule({ name: "My Job", rule: "*/5 * * * * *", })
    runMyJob()
    {
        // Do something every 5 seconds
    }
}
```

## Prometheus

Prometheus adds metrics to your app.

```
yarn install @jojoxd/tsed-util prom-client express-prom-bundle
```

```typescript
// Server.ts
import "@jojoxd/tsed-util/prometheus";

@Configuration({
    prometheus: {
        enabled: true,
        path: '/metrics',

        collectDefaultMetrics: true,
        options: { /* Options of express-prom-bundle */ }
    }
})
export class Server {}
```