UNPKG

@furystack/mongodb-store

Version:

MongoDB Store implementation for FuryStack

32 lines 1.41 kB
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; }; import { Injectable } from '@furystack/inject'; import { MongoClient } from 'mongodb'; /** * Factory for instantiating MongoDb clients */ let MongoClientFactory = class MongoClientFactory { connections = new Map(); async [Symbol.asyncDispose]() { await Promise.all([...this.connections.values()].map((c) => c.close(true))); this.connections.clear(); } getClientFor(url, options) { const existing = this.connections.get(url); if (existing) { return existing; } const client = new MongoClient(url, options); this.connections.set(url, client); return client; } }; MongoClientFactory = __decorate([ Injectable({ lifetime: 'singleton' }) ], MongoClientFactory); export { MongoClientFactory }; //# sourceMappingURL=mongo-client-factory.js.map