No description
This repository has been archived on 2024-12-19. You can view files and clone it, but you cannot make any changes to its state, such as pushing and creating new issues, pull requests or comments.
Find a file
2022-05-01 17:33:45 +02:00
.idea Cleanup repo 2022-05-01 12:53:37 +02:00
.yarn/releases Cleanup repo 2022-05-01 12:53:37 +02:00
dist chore(release): 1.2.0 [skip ci] 2022-05-01 15:06:31 +00:00
src Cleanup + updated readme [skip ci] 2022-05-01 17:31:43 +02:00
.gitattributes Initial commit 2017-11-06 22:19:39 +02:00
.gitignore Add lockfile 2022-05-01 16:34:48 +02:00
.gitlab-ci.yml fix(build): Change cache policy to push for build stage 2022-05-01 16:39:29 +02:00
.npmignore feat(build): Add semantic release 2022-05-01 16:32:25 +02:00
.releaserc feat(build): Add semantic release 2022-05-01 16:32:25 +02:00
.yarnrc.yml Cleanup repo 2022-05-01 12:53:37 +02:00
CHANGELOG.md chore(release): 1.2.0 [skip ci] 2022-05-01 15:06:31 +00:00
LICENSE Package: Add Binki to contributors 2018-06-05 19:45:31 +03:00
package.json chore(release): 1.2.0 [skip ci] 2022-05-01 15:06:31 +00:00
README.md Cleanup + updated readme [skip ci] 2022-05-01 17:31:43 +02:00
tsconfig.json Implement @jojoxd/tsconfig 2022-05-01 15:32:56 +02:00
yarn.lock fix(build): Update yarn lockfile, remove pre-commit as it broke during semantic-release install 2022-05-01 17:01:07 +02:00

@jojoxd/connect-typeorm

pipeline status coverage report Latest Release

A TypeORM-based session store.

Usage

Configure TypeORM with back end of your choice:

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

Implement the Session entity:

// src/domain/Session/Session.ts

import { ISession } from "@jojoxd/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 "@jojoxd/connect-typeorm";
import Express from "express";
import 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,
        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.

  • 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