GithubHelp home page GithubHelp logo

mrtenz / typeorm-store Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 12.0 417 KB

A TypeORM-based store for express-session.

License: MIT License

TypeScript 100.00%
express-session typeorm typescript nodejs express

typeorm-store's Introduction

typeorm-store

A TypeORM-based store for express-session.

Installation

$ 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.

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.

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).

typeorm-store's People

Contributors

bigplayalman avatar dependabot[bot] avatar manzato avatar mrtenz avatar royheeffer avatar tlamarre91 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

typeorm-store's Issues

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters

Error: Argument passed in must be a single String of 12 bytes or a string of 24 hex characters
at new ObjectID (/home/container/node_modules/bson/lib/bson/objectid.js:59:11)
at MongoEntityManager. (/home/container/node_modules/typeorm/entity-manager/MongoEntityManager.js:183:84)
at step (/home/container/node_modules/tslib/tslib.js:143:27)
at Object.next (/home/container/node_modules/tslib/tslib.js:124:57)
at /home/container/node_modules/tslib/tslib.js:117:75
at new Promise ()
at __awaiter (/home/container/node_modules/tslib/tslib.js:113:16)
at MongoEntityManager.findOne (/home/container/node_modules/typeorm/entity-manager/MongoEntityManager.js:173:38)
at MongoRepository.findOne (/home/container/node_modules/typeorm/repository/MongoRepository.js:57:29)
at e.TypeormStore.get (/home/container/node_modules/typeorm-store/lib/typeormStore.js:1:1636)

expirationInterval is defined in milliseconds, not seconds

The expirationInterval doc comment says:

The interval between checking for expired sessions in seconds. Defaults to 86400 (1 day).

But, the value is actually passed as-is to node's setInterval function, without first multiplying by 1000 (https://github.com/Mrtenz/typeorm-store/blob/master/src/store.ts#L189):

this.expirationIntervalId = setInterval(this.clearExpiredSessions, interval);

This means clearExpiredSessions() is getting called 1000x more times than would be expected given the documentation. :)

Type error when setting up connection

Type 'Repository<Session>' is not assignable to type 'Repository<SessionEntity>'.
  Types of property 'findOne' are incompatible.
    Type '{ (id?: string | number | Date | ObjectID, options?: FindOneOptions<Session>): Promise<Session>; (options?: FindOneOptions<Session>): Promise<Session>; (conditions?: FindConditions<Session>, options?: FindOneOptions<...>): Promise<...>; }' is not assignable to type '{ (id?: string | number | Date | ObjectID, options?: FindOneOptions<SessionEntity>): Promise<SessionEntity>; (options?: FindOneOptions<SessionEntity>): Promise<SessionEntity>; (conditions?: FindConditions<...>, options?: FindOneOptions<...>): Promise<...>; }'.

Wrong units in documentation

Hello,

the documentation (README) says that ttl configuration is in millisecods.
However looking at the code, it expects seconds. The undocumented expirationInterval also expects 'seconds'.

This is just to prevent others to make the mistake i did.


BTW: Thanks @Mrtenz for writting this!

Is there a way to add custom metadata to the session table?

Lets say I want to associate a user ID with my session entity, so I can easily retrieve all sessions from a user for a feature such as "logout from all devices".

Something like this:

@Entity()
export class Session extends BaseEntity implements SessionEntity {
  @Index()
  @Column('bigint')
  expiresAt: number;

  @PrimaryColumn()
  id: string;

  @Column('json')
  data: string;

  @ManyToOne(() => User, (user) => user.sessions)
  @JoinColumn({name: 'userId'})
  user: User;
}

How can I make it so, whenever a new session is created, this column is populated with the associated user ID?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.