How to add userId foreign key to session entity in nestjs and typorm?

Viewed 11

I'm using nestjs and typeorm/mysql and have a login system. i'm storing sessions in database using interface called ISession

import { ISession } from 'connect-typeorm';

I need to add a foreign key in Session table to Users table put when add it the following error appears:

src/session/session.middleware.ts:30:15 - error TS2345: Argument of type
'Repository<Session>' is not assignable to parameter of type 'Repository<ISession>'.

session.entity.ts

import { Column, Entity, Index, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm';
import { ISession } from 'connect-typeorm';

import { Account } from 'src/account/account.entity';

@Entity()
export class Session implements ISession {
    @Index()
    @Column('bigint')
    public expiredAt: number;

    @PrimaryColumn('varchar', { length: 255 })
    public id: string;

    @Column('text')
    public json: string;

    @OneToOne(() => Account)
    @JoinColumn()
    account: Account;

    // when add the foreign key to User table in session entity ,this error occurs:
    // src/session/session.middleware.ts:30:15 - error TS2345: Argument of type
    // 'Repository<Session>' is not assignable to parameter of type 'Repository<ISession>'.
}

session.middleware.ts

import { Request, Response, NextFunction, RequestHandler } from 'express';
import { Injectable, NestMiddleware } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import * as ExpressSession from 'express-session';
import { TypeormStore } from 'connect-typeorm';
import { Repository } from 'typeorm';

import { Session } from './session.entity';

@Injectable()
export class SessionMiddleware implements NestMiddleware {
    private readonly sessionHandler: RequestHandler;
    private exp = 60 * 1000;

    constructor(
        @InjectRepository(Session)
        private sessionRepository: Repository<Session>
    ) {
        this.sessionHandler = ExpressSession({
            secret: process.env.SESSION_SECRET,
            resave: false,
            saveUninitialized: false,
            cookie: {
                maxAge: this.exp,
                httpOnly: true
            },
            store: new TypeormStore({
                cleanupLimit: 0,
                ttl: this.exp
            }).connect(this.sessionRepository)
        });
    }

    use(req: Request, res: Response, next: NextFunction) {
        this.sessionHandler(req, res, next);
    }
}

session.module.ts

import { TypeOrmModule } from '@nestjs/typeorm';
import { Module } from '@nestjs/common';

import { SessionMiddleware } from './session.middleware';
import { Session } from './session.entity';

@Module({
    imports: [TypeOrmModule.forFeature([Session])],
    providers: [SessionMiddleware],
    exports: [SessionMiddleware]
})
export class SessionModule {}

The ERROR!

src/session/session.middleware.ts:30:15 - error TS2345: Argument of type 'Repository<Session>' is not assignable to parameter of type 'Repository<ISession>'.
  The types of 'createQueryBuilder(...).setFindOptions' are incompatible between these types.
    Type '(findOptions: FindManyOptions<Session>) => SelectQueryBuilder<Session>' is not assignable to type '(findOptions: FindManyOptions<ISession>) => SelectQueryBuilder<ISession>'.
      Types of parameters 'findOptions' and 'findOptions' are incompatible.
        Type 'FindManyOptions<ISession>' is not assignable to type 'FindManyOptions<Session>'.
          Types of property 'select' are incompatible.
            Type 'FindOptionsSelect<ISession> | FindOptionsSelectByString<ISession>' is not assignable to type 'FindOptionsSelect<Session> | FindOptionsSelectByString<Session>'.
              Type 'FindOptionsSelectByString<ISession>' is not assignable to type 'FindOptionsSelect<Session> | FindOptionsSelectByString<Session>'.
                Type '(keyof ISession)[]' is not assignable to type 'FindOptionsSelectByString<Session>'.
                  Type 'keyof ISession' is not assignable to type 'keyof Session'.
                    Type '"destroyedAt"' is not assignable to type 'keyof Session'.

30    }).connect(this.sessionRepository)
0 Answers
Related