UNPKG

nest-typeorm-sharding-repository

Version:

This module extends TypeORM capabilities to support distributed database environments with List and Range-based sharding strategies in a NestJS application.

156 lines (155 loc) 7.58 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; 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 __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var ShardingManager_1; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShardingManager = void 0; const typeorm_1 = require("typeorm"); const typeorm_sharding_type_1 = require("./types/typeorm-sharding.type"); const common_1 = require("@nestjs/common"); let ShardingManager = ShardingManager_1 = class ShardingManager { constructor(options) { this.options = options; this.dataSources = []; this._destroyed = false; } get destroyed() { return this._destroyed; } static createInstance(options) { return __awaiter(this, void 0, void 0, function* () { const manager = new ShardingManager_1(options); yield manager.init(); return manager; }); } init() { return __awaiter(this, void 0, void 0, function* () { const _a = this.options, { shards } = _a, config = __rest(_a, ["shards"]); this.dataSources.push(...(yield Promise.all( //initialize all datasource this.options.shards.map((opt) => { return new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () { try { const { onInitialize } = opt, options = __rest(opt, ["onInitialize"]); const dataSource = yield new typeorm_1.DataSource(Object.assign(Object.assign({}, options), config)).initialize(); if (onInitialize) yield onInitialize(dataSource); resolve(dataSource); } catch (e) { reject(e); } })); })))); this.dataSources.forEach((ds) => { ds.entityMetadatas.forEach((meta) => { const shardingType = Reflect.getMetadata('SHARDING_TYPE', meta.target); if (shardingType !== this.options.shardingType) throw new Error(`ShardingManager: @ShardingEntity({type}) doesn't match ShardingManager.options.shardingType`); Reflect.defineMetadata('SHARDING_MANAGER', this, meta.target); }); }); return this; }); } static init(options) { return __awaiter(this, void 0, void 0, function* () { return new ShardingManager_1(options).init(); }); } getDataSource(entity, shardingFunc) { /* if (this.options.shardingType === ShardingType.MODULAR) { throw new Error('ShardingManager: Modular type is not supported yet.'); } else */ if (this.options.shardingType === typeorm_sharding_type_1.ShardingType.RANGE) { const idx = this.options.shards.findIndex((item) => shardingFunc(entity, item.minKey, item.maxKey)); if (idx < 0) { const defaultIndex = this.options.shards.findIndex((item) => !!item.default); if (defaultIndex < 0) return this.dataSources[this.dataSources.length - 1]; return this.dataSources[defaultIndex]; } return this.dataSources[idx]; } else if (this.options.shardingType === typeorm_sharding_type_1.ShardingType.LIST) { const idx = this.options.shards.findIndex((item) => shardingFunc(entity, item.key)); if (idx < 0) { const defaultIndex = this.options.shards.findIndex((item) => !!item.default); if (defaultIndex < 0) return this.dataSources[this.dataSources.length - 1]; return this.dataSources[defaultIndex]; } return this.dataSources[idx]; } else throw new Error('ShardingManager: Unsupported sharding type'); } getDataSourceById(id, shardingFuncById) { if (this.options.shardingType === typeorm_sharding_type_1.ShardingType.RANGE) { const idx = this.options.shards.findIndex((item) => shardingFuncById(id, item.minKey, item.maxKey)); if (idx < 0) { const defaultIndex = this.options.shards.findIndex((item) => !!item.default); if (defaultIndex < 0) return this.dataSources[this.dataSources.length - 1]; return this.dataSources[defaultIndex]; } return this.dataSources[idx]; } else throw new Error('ShardingManager: Unsupported sharding type'); } getDataSourceByShardingKey(key) { if (this.options.shardingType === typeorm_sharding_type_1.ShardingType.LIST) { const idx = this.options.shards.findIndex((item) => item.key === key); if (idx < 0) { const defaultIndex = this.options.shards.findIndex((item) => !!item.default); if (defaultIndex < 0) return this.dataSources[this.dataSources.length - 1]; return this.dataSources[defaultIndex]; } return this.dataSources[idx]; } else throw new Error('ShardingManager: Unsupported sharding type'); } destroy() { return __awaiter(this, void 0, void 0, function* () { this._destroyed = true; yield Promise.all(this.dataSources.map((dataSource) => dataSource.destroy())); }); } }; ShardingManager = ShardingManager_1 = __decorate([ (0, common_1.Injectable)(), __metadata("design:paramtypes", [Object]) ], ShardingManager); exports.ShardingManager = ShardingManager;