zents
Version:
ZenTS is a Node.js & TypeScript MVC-Framework for building rich web applications, released as free and open-source software under the MIT License. It is designed for building web applications with modern tools and design patterns.
77 lines • 2.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.DatabaseSessionStoreAdapter = void 0;
const typeorm_1 = require("typeorm");
const enums_1 = require("../../types/enums");
const dayjs_1 = __importDefault(require("dayjs"));
const logger_1 = require("../../log/logger");
class DatabaseSessionStoreAdapter {
constructor(databaseContainer, providerOptions) {
this.repository = databaseContainer
.get(enums_1.DB_TYPE.ORM)
.getRepository(providerOptions.dbStoreEntity);
this.entity = providerOptions.dbStoreEntity;
this.expire = providerOptions.expireInMS;
}
async create(sessionId) {
const record = await this.getRecord(sessionId);
if (record) {
return;
}
const session = new this.entity();
session.id = sessionId;
session.data = JSON.stringify({});
session.created_at = new Date();
session.expired_at =
this.expire > 0 ? dayjs_1.default().add(this.expire, 'ms').toDate() : dayjs_1.default().add(7, 'day').toDate();
await this.repository.save(session);
}
async load(sessionId) {
const record = await this.getRecord(sessionId);
let data = {};
if (!record) {
return data;
}
try {
data = JSON.parse(record.data);
}
catch (e) {
logger_1.log.error(e);
}
return data;
}
async persist(sessionId, data) {
let record;
try {
record = JSON.stringify(data);
}
catch (e) {
record = '{}';
logger_1.log.error(e);
}
await this.repository.update({
id: sessionId,
}, {
data: record,
});
}
async remove(sessionId) {
await this.repository.delete({
id: sessionId,
});
}
async has(sessionId) {
return !!(await this.getRecord(sessionId));
}
async getRecord(sessionId) {
return await this.repository.findOne({
id: sessionId,
expired_at: typeorm_1.MoreThan(new Date()),
});
}
}
exports.DatabaseSessionStoreAdapter = DatabaseSessionStoreAdapter;
//# sourceMappingURL=DatabaseSessionStoreAdapter.js.map