UNPKG

simple-graphql

Version:

The simple way to generates GraphQL schemas and Sequelize models from your models definition.

91 lines 4.33 kB
"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 }); const sequelize_1 = __importDefault(require("sequelize")); const cache_1 = require("./cache"); const Cache_1 = __importDefault(require("./cache/Cache")); exports.default = { name: 'cache', defaultOptions: { prefix: 'SG', enable: true }, priority: 999, description: 'Support cache with dataLoader', applyToModel: function (model, options, models) { const self = this; if (self.cacheManager == null) { self.cacheManager = (options === null || options === void 0 ? void 0 : options.cacheManager) || new cache_1.LruCacheManager(); } const cache = new Cache_1.default({ prefix: options.prefix || 'SG', cacheManger: self.cacheManager, model: model, expire: options.expire }); Object.assign(model, { withCache: () => cache, clearCache: () => cache.clear() }); const cleanCache = (options) => __awaiter(this, void 0, void 0, function* () { let transaction = options.transaction; if (transaction === undefined && sequelize_1.default._cls) { // TODO Check if Sequelize update transaction = sequelize_1.default._cls.get('transaction'); } if (transaction) { if (transaction.clearCaches == null) { transaction.clearCaches = []; transaction.afterCommit(() => __awaiter(this, void 0, void 0, function* () { for (const c of transaction.clearCaches || []) { yield c.clear(); } transaction.clearCaches = null; })); } if (transaction.clearCaches.indexOf(cache) === -1) { transaction.clearCaches.push(cache); } } else { yield cache.clear(); } }); model.addHook('afterCreate', 'cleanCache', (instance, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterUpdate', 'cleanCache', (instance, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterDestroy', 'cleanCache', (instance, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterSave', 'cleanCache', (instance, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterUpsert', 'cleanCache', (instance, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterBulkCreate', 'cleanCache', (instances, options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterBulkDestroy', 'cleanCache', (options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); model.addHook('afterBulkUpdate', 'cleanCache', (options) => __awaiter(this, void 0, void 0, function* () { return cleanCache(options); })); } }; //# sourceMappingURL=cachePlugin.js.map