UNPKG

@fxjs/db-driver

Version:

[![NPM version](https://img.shields.io/npm/v/@fxjs/db-driver.svg)](https://www.npmjs.org/package/@fxjs/db-driver) [![Build Status](https://travis-ci.org/fxjs-modules/db-driver.svg)](https://travis-ci.org/fxjs-modules/db-driver) [![Build status](https://

50 lines (49 loc) 1.39 kB
/// <reference types="fib-pool" /> Object.defineProperty(exports, "__esModule", { value: true }); const db = require("db"); const base_class_1 = require("./base.class"); class MongoDriver extends base_class_1.Driver { constructor(conn) { super(conn); this.connection = null; } switchDb(targetDb) { if (!this.connection) this.open(); const currentDb = this.connection.getName(); if (currentDb === targetDb) return; this.connection.getSiblingDB(targetDb); } reopen() { try { this.close(); } catch (error) { } return this.open(); } open() { return super.open(); } close() { if (this.connection) this.connection.close(); this.connection = null; } ping() { } command(cmd, arg) { return this.commands({ [cmd]: arg }); } commands(cmds, opts) { if (this.isPool) return this.pool(conn => conn.runCommand(cmds)); if (!this.connection) this.open(); return this.connection.runCommand(cmds); } getConnection() { // @notice it's invalid cast, you should replace one JS implementation of mongodb return db.openMongoDB(this.uri); } } exports.default = MongoDriver;