@nestpress/postgres-express-session
The PosgreSQL with Express Session Module for NestJS
Installation
$ npm install --save @nestpress/postgres-express-session
Usage
In your application main entry point, just use it like below:
import { NestFactory } from '@nestjs/core';
import { PostgresExpressSessionModule } from '@nestpress/postgres-express-session';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.get(PostgresExpressSessionModule).initialize(app, {
secret: 'secret',
username: 'root',
password: 'test',
database: 'test',
host: 'localhost',
port: 5432,
expire: 24 * 60 * 60 * 1000; // one day
});
app.listen(3000, '0.0.0.0', () => {
console.log('Ready on http://localhost:3000');
});
}
bootstrap();
Additionally, we must have session
table.
If you use TypeORM, the session entity is like this:
import {
Entity,
Column,
PrimaryColumn,
} from 'typeorm';
@Entity()
export class Session {
@PrimaryColumn({
type: 'varchar',
collation: 'default'
})
sid!: string;
@Column('json')
sess!: string;
@Column('timestamp')
expire!: number;
}
Or just use the same entity PostgresExpressSession
:
import { Entity } from 'typeorm';
import { PostgresExpressSession } from '@nestpress/postgres-express-session';
@Entity()
export class Session extends PostgresExpressSession {}
Advanced Usage
If you use dotenv, we recommend the following examples:
// session/session.module.ts
import {
Module,
INestApplication,
} from '@nestjs/common';
import { PostgresExpressSessionModule } from '@nestpress/postgres-express-session';
import { EnvModule } from '../env/env.module';
import { EnvService } from '../env/env.service';
@Module({
imports: [
EnvModule,
],
})
export class SessionModule extends PostgresExpressSessionModule {
constructor(
private readonly env: EnvService,
) {
super();
}
public initialize(app: INestApplication): void {
super.initialize(app, {
secret: this.env.get('SECRET'),
username: this.env.get('DB_USERNAME'),
password: this.env.get('DB_PASSWORD'),
database: this.env.get('DB_DATABASE'),
host: this.env.get('DB_HOST'),
port: parseInt(this.env.get('DB_PORT'), 10),
expire: parseInt(this.env.get('DB_SESSION_EXPIRE'), 10)
});
}
}
Now we can use this SessionModule
like this:
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { SessionModule } from './session/session.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.get(SessionModule).initialize(app);
app.listen(3000, '0.0.0.0', () => {
console.log('Ready on http://localhost:3000');
});
}
bootstrap();