@beecode/msh-node-session
v3.0.14
Published
[](https://beecode.semaphoreci.com/projects/msh-node-session) [ {
const { sessionStrategy } = params ?? {}
super({ sessionStrategy })
}
public getTransactionManager(): EntityManager | undefined {
try {
return this._strategy.get<EntityManager>(SessionData.TYPEORM_ENTITY_MANAGER)
} catch (_err) {
return undefined
}
}
protected _setTransactionManager(entityManager: EntityManager): void {
return this._strategy.set<EntityManager>(SessionData.TYPEORM_ENTITY_MANAGER, entityManager)
}
public async startTransaction<T>(callback: (transactionEntityManager: EntityManager) => Promise<T>): Promise<T> {
return this.createAsyncSession(() => {
const existingTransactionManager = this.getTransactionManager()
if (existingTransactionManager) return callback(existingTransactionManager)
return databaseService.getConnection().transaction<T>((newTransEntityManager: EntityManager) => {
this._setTransactionManager(newTransEntityManager)
return callback(newTransEntityManager)
})
})
}
public setAuthUser(authUser: JWTPayloadUser): void {
this._strategy.set<JWTPayloadUser>(SessionData.AUTH_USER, authUser)
}
public getAuthUser(): JWTPayloadUser {
const authUser = this._strategy.get<JWTPayloadUser>(SessionData.AUTH_USER)
if (!authUser) throw error.server.internalServerError('Missing auth user from session')
return authUser
}
/**
* Connect to existing transaction, this is only used in migrations files
* @param {EntityManager} entityManager
* @param {() => Promise<T>} callback
* @returns {Promise<T>}
*/
public async entityManagerSideCall<T>(entityManager: EntityManager, callback: () => Promise<T>): Promise<T> {
return this.createAsyncSession(async () => {
this._setTransactionManager(entityManager)
return callback()
})
}
}
export const sessionStrategy = new FastAlsStrategy()
// export const sessionStrategy = new ClsHookedStrategy()
export const sessionUtil = cacheUtil.singleton(() => new SessionUtil({ sessionStrategy }))
Fastify middleware example
import Fastify from 'fastify'
import { fastifyHelperFactory } from '@beecode/msh-node-session/dist/helpers/fastify-helper'
import { sessionStrategy } from 'src/util/session-util'
const fastify = Fastify()
const fastifyHelper = fastifyHelperFactory({sessionStrategy})
await fastify.register(fastifyHelper.beecodeSessionContextPluginFactory())
Express middleware example
FastAls
import express from 'express'
import { expressFastAlsHelperFactory } from '@beecode/msh-node-session/dist/helpers/express-fast-als-helper'
import { sessionStrategy } from 'src/util/session-util'
const expressApp = express()
const expressFastAlsHelper = expressFastAlsHelperFactory({fastAlsStrategy:sessionStrategy})
this._expressApp.use((req, res, next) => expressFastAlsHelper.expressMiddleware(req, res, next))
// other middlewares
// expressApp.use(... ClsHooked
import express from 'express'
import { expressClsHookedHelperFactory } from '@beecode/msh-node-session/dist/helpers/express-cls-hooked-helper'
import { sessionStrategy } from 'src/util/session-util'
const expressApp = express()
const expressClsHookedHelperHelper = expressClsHookedHelperFactory({fastAlsStrategy:sessionStrategy})
this._expressApp.use((req, res, next) => expressClsHookedHelperHelper.expressMiddleware(req, res, next))
this._expressApp.use((req, res, next) => expressClsHookedHelperHelper.expressMiddlewareBindEmitter(req, res, next))
// other middlewares
// expressApp.use(... 
