Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mrtenz/typeorm-store
A TypeORM-based store for express-session.
https://github.com/mrtenz/typeorm-store
express express-session nodejs typeorm typescript
Last synced: about 2 months ago
JSON representation
A TypeORM-based store for express-session.
- Host: GitHub
- URL: https://github.com/mrtenz/typeorm-store
- Owner: Mrtenz
- License: mit
- Created: 2018-04-27T18:35:09.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-02-27T17:21:32.000Z (10 months ago)
- Last Synced: 2024-09-19T19:10:50.938Z (3 months ago)
- Topics: express, express-session, nodejs, typeorm, typescript
- Language: TypeScript
- Size: 407 KB
- Stars: 16
- Watchers: 4
- Forks: 12
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# typeorm-store
A TypeORM-based store for [express-session](https://github.com/expressjs/session).## Installation
```bash
$ yarn add typeorm-store
```## Usage
First, make a new `Session` entity. Make sure to synchronize the entity to the database. `typeorm-store` will not do this for you.
```typescript
import { BaseEntity, Column, Entity, PrimaryColumn } from 'typeorm';
import { SessionEntity } from 'typeorm-store';@Entity()
export class Session extends BaseEntity implements SessionEntity {
@PrimaryColumn()
id: string;@Column()
expiresAt: number;@Column()
data: string;
}
```Use `TypeormStore` as store in `express-session`, specifying the repository of the new `Session` entity.
```typescript
import * as express from 'express';
import * as session from 'express-session';
import { getConnection } from 'typeorm';
import { TypeormStore } from 'typeorm-store';
import { Session } from './entities/session';const app = express();
// Make sure the connection is ready before doing this
const repository = getConnection().getRepository(Session);app.use(session({
secret: 'secret',
resave: false,
saveUninitialized: false,
store: new TypeormStore({ repository })
}))
```# API
`new TypeormStore(options)`## Options
* `repository` (required) - The repository of the session entity.
* `ttl` (optional) - The time to live for the session in seconds. Defaults to 86400 (1 day).
* `expirationInterval` (optional) - The interval in seconds to check for expired sessions.
Defaults to 86400 (1 minute).