connect-mikro-orm
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

🔗 connect-mikro-orm

A MikroORM-based session store.


npm GitHub Repo stars

Setup & Usage

Configure MikroORM with back end of your choice:

NPM

yarn add connect-mikro-orm @mikro-orm/core @mikro-orm/knex express-session
yarn add -D @types/express-session

Implement the Session entity:

// src/domain/Session/Session.ts

import type { ISession } from 'connect-mikro-orm';

import { Entity, Index, PrimaryKey, Property } from '@mikro-orm/core';

@Entity()
export class Session implements ISession {
  @PrimaryKey()
  id: string;

  @Property({ columnType: 'text' })
  json: string;

  @Index()
  @Property({ columnType: 'bigint' })
  expiredAt: number;
}

Pass repository to MikroOrmStore:

// src/app/Api/Api.ts

import { MikroOrmStore } from 'connect-mikro-orm';
import * as Express from 'express';
import * as ExpressSession from 'express-session';

import { Session } from '../../domain/Session/Session';

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

  public express = Express().use(
    ExpressSession({
      resave: false,
      saveUninitialized: false,
      store: new MikroOrmStore({
        ttl: 86400,
      }).connect(this.sessionRepository),
      secret: 'keyboard cat',
    })
  );
}

Options

Constructor receives an object. Following properties may be included:

  • 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: MikroOrmStore, error: Error) => void. If not set, any database error will cause the MikroOrmStore 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

Package Sidebar

Install

npm i connect-mikro-orm

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

21.3 kB

Total Files

16

Last publish

Collaborators

  • diogoabu