Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG]: getRepository 'typeorm' is deprecated #37

Open
jrAtAustin opened this issue Sep 16, 2022 · 8 comments
Open

[BUG]: getRepository 'typeorm' is deprecated #37

jrAtAustin opened this issue Sep 16, 2022 · 8 comments
Assignees
Labels
documentation 📖 book Improvements or additions to documentation help wanted 🤝 Extra attention is needed

Comments

@jrAtAustin
Copy link

Contact Details

No response

Bug description

The documentation for connect-typeorm shows the session store connection being set to the entity repository from the depreciated TypeORM getRepository method. Is there a replacement for getRepository or a workaround for this issue?

Steps to reproduce

See documentation (README.md) for this repository.

Additional Information

No response

@jrAtAustin jrAtAustin added the bug 🐛 bug Something isn't working label Sep 16, 2022
@jrAtAustin
Copy link
Author

I found an alternative:
app.get(DataSource).getRepository(TypeORMSession)

@jrAtAustin
Copy link
Author

Closing

@freshgiammi
Copy link
Member

freshgiammi commented Sep 18, 2022

Hi, sorry for the late reply.
You're correct, TypeORM changed how getRepository() is used since v0.3 (https://typeorm.io/data-source#creating-a-new-datasource) and I forgot to update the documentation.

I'm reopening this issue as a memo to fix it, but a correct implementation would now be (this is how I set everything up in the index file):

import { DataSource, DataSourceOptions } from 'typeorm';
import { TypeormStore } from 'connect-typeorm';
import { Session } from './entity/Session';
import express from 'express';
import ExpressSession from 'express-session';


// Define express variable
const app = express();

// Create an exportable `TypeormStore` to allow access everywhere
export const sessionStore = new TypeormStore({
    cleanupLimit: 2,
    limitSubquery: false, // If using MariaDB.
    ttl: 86400,
})

// Create an exportable `DataSource` to allow `getRepository()` access everywhere. 
// `databaseOptions` is an object of type DataSourceOptions and may depend on your configuration/needs.
export const dataSource = new DataSource(databaseOptions);

// Initialize DataSource
dataSource.initialize()
.then(async () => {

    const sessionRepository = dataSource.getRepository(Session);
    app.use(
        ExpressSession({
            resave: false,
            saveUninitialized: false,
            store: sessionStore.connect(sessionRepository),
            // Cookie settings can be added here based on your configuration/needs. 
            // See: https://expressjs.com/en/resources/middleware/session.html
            secret: 'keyboard cat' // This should not be hardcoded, an .env variable is preferred.
        })
    );

    /* Everything else required by your app: setting up routes, starting server on HTTP/S, etc... */

}).catch((error) => console.log(error));

@freshgiammi freshgiammi reopened this Sep 18, 2022
@freshgiammi freshgiammi added documentation 📖 book Improvements or additions to documentation and removed bug 🐛 bug Something isn't working labels Sep 18, 2022
@freshgiammi freshgiammi added the help wanted 🤝 Extra attention is needed label Feb 23, 2023
@srini-leanfolks
Copy link

srini-leanfolks commented Feb 23, 2023

Hi, sorry for the late reply. You're correct, TypeORM changed how getRepository() is used since v0.3 (https://typeorm.io/data-source#creating-a-new-datasource) and I forgot to update the documentation.

I'm reopening this issue as a memo to fix it, but a correct implementation would now be (this is how I set everything up in the index file):

import { DataSource, DataSourceOptions } from 'typeorm';
import { TypeormStore } from 'connect-typeorm';
import { Session } from './entity/Session';
import express from 'express';
import ExpressSession from 'express-session';


// Define express variable
const app = express();

// Create an exportable `TypeormStore` to allow access everywhere
export const sessionStore = new TypeormStore({
    cleanupLimit: 2,
    limitSubquery: false, // If using MariaDB.
    ttl: 86400,
})

// Create an exportable `DataSource` to allow `getRepository()` access everywhere. 
// `databaseOptions` is an object of type DataSourceOptions and may depend on your configuration/needs.
export const dataSource = new DataSource(databaseOptions);

// Initialize DataSource
dataSource.initialize()
.then(async () => {

    const sessionRepository = dataSource.getRepository(Session);
    app.use(
        ExpressSession({
            resave: false,
            saveUninitialized: false,
            store: sessionStore.connect(sessionRepository),
            // Cookie settings can be added here based on your configuration/needs. 
            // See: https://expressjs.com/en/resources/middleware/session.html
            secret: 'keyboard cat' // This should not be hardcoded, an .env variable is preferred.
        })
    );

    /* Everything else required by your app: setting up routes, starting server on HTTP/S, etc... */

}).catch((error) => console.log(error));

But it is not working with NestJS. If possible, please provide an example of using it in the NestJS

@freshgiammi
Copy link
Member

But it is not working with NestJS. If possible, please provide an example of using it in the NestJS

Yeah, that's an example configuration for an Express backend. I yet have to try running it in Nest. In the meanwhile, please refer to this https://docs.nestjs.com/techniques/session for how to set up a session middleware, it should point you in the right direction.

@rajamoulimallareddy
Copy link

But it is not working with NestJS. If possible, please provide an example of using it in the NestJS

Yeah, that's an example configuration for an Express backend. I yet have to try running it in Nest. In the meanwhile, please refer to this https://docs.nestjs.com/techniques/session for how to set up a session middleware, it should point you in the right direction.

tried this too its running fine with no issues but. data is not getting saved.

@freshgiammi
Copy link
Member

tried this too its running fine with no issues but. data is not getting saved.

I may have some time this week or next to look at it, could you please share a repo with the implementation?
I'll need to reproduce it and I don't currently have anything running with Nest to try quickly, so a basic template with it would be nice.

@hayzedDev
Copy link

hayzedDev commented Apr 8, 2023

If you're using it with nest.js, you can do it this way

import { TypeormStore } from 'connect-typeorm';
import * as ExpressSession from 'express-session'; 
import { NestFactory } from '@nestjs/core';
import { Session } from './entities/session.entity';
import { DataSource } from 'typeorm';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  const sessionRepo = app.get(DataSource).getRepository(Session);
  app.use(
    ExpressSession({
      secret: process.env.SESSION_SECRET,
      resave: true,
      name: process.env.SESSION_NAME,
      saveUninitialized: true,
      cookie: { maxAge: Number(process.env.COOKIE_MAX_AGE) },
      store: new TypeormStore().connect(sessionRepo),
    }),
  );

  app.use(passport.initialize());
  app.use(passport.session());

  await app.listen(Number(process.env.NODE_PORT) || 3000);
}
bootstrap();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation 📖 book Improvements or additions to documentation help wanted 🤝 Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants