tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
139 lines • 4.3 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TCache = exports.Cache = void 0;
const MemoryCache_1 = require("./MemoryCache");
const DBCache_1 = require("./DBCache");
const RedisCache_1 = require("./RedisCache");
const options_1 = __importDefault(require("../../options"));
class Cache {
constructor() {
this._driver = new MemoryCache_1.MemoryCache();
this._chooseDriver(options_1.default.CACHE);
return this;
}
provider() {
return this._driver.provider();
}
/**
* The 'driver' method is used to pick a driver for the cache
*
* @param {string} driver 'db' | 'memory' | 'redis'
* @returns {this} this
*/
driver(driver) {
return this._chooseDriver(driver);
}
/**
* The 'all' method is used get all cache
*
* @returns {Promise<array>} array
*/
all() {
return __awaiter(this, void 0, void 0, function* () {
return yield this._driver.all();
});
}
/**
* The 'exists' method is used get all cache
*
* @param {string} key
* @returns {Promise<array>} array
*/
exists(key) {
return __awaiter(this, void 0, void 0, function* () {
try {
return yield this._driver.exists(key);
}
catch (e) {
return false;
}
});
}
/**
* The 'get' method is used get cache by key
* @param {string} key
* @returns {any} any
*/
get(key) {
return __awaiter(this, void 0, void 0, function* () {
try {
const cache = yield this._driver.get(key);
if (cache == null || cache === '')
return null;
return cache;
}
catch (e) {
return null;
}
});
}
/**
* The 'set' method is used set the cache
*
* @param {string} key
* @param {unknown} value
* @param {number} ms
* @returns {Promise<void>} void
*/
set(key, value, ms) {
return __awaiter(this, void 0, void 0, function* () {
try {
yield this._driver.set(key, value, ms);
return;
}
catch (e) {
return;
}
});
}
/**
* The 'clear' method is used clear all cache
*
* @returns {Promise<void>} void
*/
clear() {
return __awaiter(this, void 0, void 0, function* () {
return yield this._driver.clear();
});
}
/**
* The 'clear' method is used delete cache by key
*
* @returns {Promise<void>} void
*/
delete(key) {
return __awaiter(this, void 0, void 0, function* () {
yield this._driver.delete(key);
return;
});
}
_chooseDriver(driver) {
if (driver === 'db') {
this._driver = new DBCache_1.DBCache();
return this;
}
if (driver != null && driver.includes('redis')) {
this._driver = new RedisCache_1.RedisCache(String(options_1.default.CACHE));
return this;
}
this._driver = new MemoryCache_1.MemoryCache();
return this;
}
}
exports.TCache = Cache;
const cacheInstance = new Cache();
exports.Cache = cacheInstance;
exports.default = cacheInstance;
//# sourceMappingURL=index.js.map