simple-graphql
Version:
The simple way to generates GraphQL schemas and Sequelize models from your models definition.
126 lines • 5.88 kB
JavaScript
;
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 });
const sequelize_1 = __importDefault(require("sequelize"));
const getIncludeModeNames_1 = __importDefault(require("./getIncludeModeNames"));
const dataToInstance_1 = __importDefault(require("./dataToInstance"));
const instanceToData_1 = __importDefault(require("./instanceToData"));
class Cache {
constructor(options) {
this.prefix = options.prefix;
this.cacheManger = options.cacheManger;
this.model = options.model;
this.expire = options.expire;
}
buildCacheKey(method, options) {
const self = this;
return `${self.prefix}|${self.cacheManger.buildCacheKey(self.model, method, options)}`;
}
isCacheValid(options) {
return __awaiter(this, void 0, void 0, function* () {
options = options || {};
// 如果当前Transaction中, 关联实体的数据有改动, disable cache
const self = this;
let transaction = options.transaction;
if (transaction === undefined && sequelize_1.default._cls) {
transaction = sequelize_1.default._cls.get('transaction');
}
if (transaction && transaction.clearCaches) {
const relateModelNames = [
self.model.name,
...(0, getIncludeModeNames_1.default)(options)
];
return (transaction.clearCaches.find((cache) => relateModelNames.indexOf(cache.model.name) !== -1) == null);
}
return true;
});
}
findAll(options) {
return __awaiter(this, void 0, void 0, function* () {
const self = this;
const cacheKey = self.buildCacheKey('findAll', options);
if ((yield self.isCacheValid(options)) === false) {
return (yield self.model.findAll(options));
}
const cacheValue = yield self.cacheManger.get(cacheKey);
if (cacheValue !== undefined) {
return cacheValue.map((value) => (0, dataToInstance_1.default)(value, self.model, (options === null || options === void 0 ? void 0 : options.include) || []));
}
else {
const lockKey = cacheKey + '$LOCK';
const lockValue = Date.now();
yield self.cacheManger.set(lockKey, lockValue, 300);
const result = yield self.model.findAll(options);
if ((yield self.cacheManger.get(lockKey)) === lockValue) {
yield self.cacheManger.set(cacheKey, result.map((r) => (0, instanceToData_1.default)(r)), self.expire);
}
return result;
}
});
}
findOne(options) {
return __awaiter(this, void 0, void 0, function* () {
const self = this;
const cacheKey = self.buildCacheKey('findOne', options);
if ((yield self.isCacheValid(options)) === false) {
return (yield self.model.findOne(options));
}
const cacheValue = yield self.cacheManger.get(cacheKey);
if (cacheValue !== undefined) {
return (0, dataToInstance_1.default)(cacheValue, self.model, options ? options.include : []);
}
else {
const lockKey = cacheKey + '$LOCK';
const lockValue = Date.now();
yield self.cacheManger.set(lockKey, lockValue, 300);
const result = yield self.model.findOne(options);
if ((yield self.cacheManger.get(lockKey)) === lockValue) {
yield self.cacheManger.set(cacheKey, (0, instanceToData_1.default)(result), self.expire);
}
return result;
}
});
}
count(options) {
return __awaiter(this, void 0, void 0, function* () {
const self = this;
const cacheKey = self.buildCacheKey('count', options);
if ((yield self.isCacheValid(options)) === false) {
return self.model.count(options);
}
const cacheValue = yield self.cacheManger.get(cacheKey);
if (cacheValue !== undefined) {
return cacheValue;
}
else {
const lockKey = cacheKey + '$LOCK';
const lockValue = Date.now();
yield self.cacheManger.set(lockKey, lockValue, 300);
const result = yield self.model.count(options);
if ((yield self.cacheManger.get(lockKey)) === lockValue) {
yield self.cacheManger.set(cacheKey, result, self.expire);
}
return result;
}
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
const self = this;
yield self.cacheManger.del(`${self.prefix}|*|${self.model.name}|*`);
});
}
}
exports.default = Cache;
//# sourceMappingURL=Cache.js.map