Giter Club home page Giter Club logo

connect-typeorm's Introduction

connect-typeorm

A TypeORM-based session store.

Usage

Configure TypeORM with back end of your choice:

yarn add @types/express-session connect-typeorm express-session typeorm sqlite3

Implement the Session entity:

// src/domain/Session/Session.ts

import { ISession } from "connect-typeorm";
import { Column, Entity, Index, PrimaryColumn } from "typeorm";
@Entity()
export class Session implements ISession {
  @Index()
  @Column("bigint")
  public expiredAt = Date.now();

  @PrimaryColumn("varchar", { length: 255 })
  public id = "";

  @Column("text")
  public json = "";
}

Pass repository to TypeormStore:

// src/app/Api/Api.ts

import { TypeormStore } from "connect-typeorm";
import * as Express from "express";
import * as ExpressSession from "express-session";
import { Db } from "typeorm-static";
import { Session } from "../../domain/Session/Session";

export class Api {
  public sessionRepository = Db.connection.getRepository(Session);

  public express = Express().use(
    ExpressSession({
      resave: false,
      saveUninitialized: false,
      store: new TypeormStore({
        cleanupLimit: 2,
        limitSubquery: false, // If using MariaDB.
        ttl: 86400
      }).connect(this.sessionRepository),
      secret: "keyboard cat"
    })
  );
}

TypeORM uses { "bigNumberStrings": true } option by default for node-mysql, you can use a Transformer to fix this issue:

import { Bigint } from "typeorm-static";
@Column("bigint", { transformer: Bigint })

Options

Constructor receives an object. Following properties may be included:

  • cleanupLimit For every new session, remove this many expired ones. Defaults to 0, in case you need to analyze sessions retrospectively.

  • limitSubquery Select and delete expired sessions in one query. Defaults to true, you can set false to make two queries, in case you want cleanupLimit but your MariaDB version doesn't support limit in a subquery.

  • ttl Session time to live (expiration) in seconds. Defaults to session.maxAge (if set), or one day. This may also be set to a function of the form (store, sess, sessionID) => number.

  • onError Error handler for database exception. It is a function of the form (store: TypeormStore, error: Error) => void. If not set, any database error will cause the TypeormStore to be marked as "disconnected", and stop reading/writing to the database, therefore not loading sessions and causing all requests to be considered unauthenticated.

License

MIT

connect-typeorm's People

Contributors

nykula avatar yagoferrer avatar binki avatar kehrlann avatar 0x0ece avatar heartilab avatar jdhuntington avatar

Watchers

James Cloos avatar

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.