UNPKG

typeorm-sharding-repository

Version:

TypeORM Sharding Repository: Enables TypeORM to be utilized in a distributed database environment.

101 lines (100 loc) 4.71 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 __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; }; 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"); class ShardingManager { constructor(options) { this.options = options; this.dataSources = []; this._destroyed = false; } get destroyed() { return this._destroyed; } 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(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) return this.dataSources[this.dataSources.length - 1]; 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) return this.dataSources[this.dataSources.length - 1]; 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())); }); } } exports.ShardingManager = ShardingManager;