diff --git a/dist/Mongo.js b/dist/Mongo.js index 06bb88d..2311a9c 100644 --- a/dist/Mongo.js +++ b/dist/Mongo.js @@ -20,13 +20,10 @@ class MongoConnect { this.emitter = emitter; this.userConfig = userConfig; this.config = { - keepAlive: true, - poolSize: 5, + maxPoolSize: 5, connectTimeoutMS: 30000, socketTimeoutMS: 30000, serverSelectionTimeoutMS: 10000, - useUnifiedTopology: true, - connectWithNoPrimary: false, readPreference: mongodb_1.ReadPreference.SECONDARY_PREFERRED, }; this.config.authSource = (userConfig.auth || {}).authSource; @@ -72,9 +69,7 @@ class MongoConnect { return joiner.join(""); } static isValidError(err) { - return (err instanceof mongodb_1.MongoServerSelectionError || - err instanceof mongodb_1.MongoNetworkError || - err instanceof mongodb_1.MongoTimeoutError); + return err instanceof mongodb_1.MongoError; } getClient() { return this.mongoClient; @@ -110,10 +105,6 @@ class MongoConnect { } this.client = this.mongoClient.db(this.userConfig.db); this.success(`Successfully connected in ${this.mode} mode`); - mongodb_1.Logger.setLevel("info"); - mongodb_1.Logger.setCurrentLogger((msg, context) => { - this.log(msg, context); - }); if (oldMongoClient instanceof mongodb_1.MongoClient) { // Do NOT wait. If you wait, this might block indefinitely due to the older server being out of action. oldMongoClient.close(); @@ -125,8 +116,7 @@ exports.MongoConnect = MongoConnect; async function handleMongoError(err, mongo) { if (MongoConnect.isValidError(err)) { if (mongo.reconnecting === null) { - mongo.reconnecting = mongo.connect() - .then(() => { + mongo.reconnecting = mongo.connect().then(() => { return null; }); } @@ -142,7 +132,7 @@ var MODES; MODES["SERVER"] = "server"; MODES["REPLSET"] = "replset"; MODES["SHARD"] = "shard"; -})(MODES = exports.MODES || (exports.MODES = {})); +})(MODES || (exports.MODES = MODES = {})); function MongoFactory(mode, name, emitter, config) { switch (mode) { case MODES.SERVER: @@ -213,4 +203,4 @@ function castToObjectId(value) { return mongodb_1.ObjectId.createFromHexString(value); } exports.castToObjectId = castToObjectId; -//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9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 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@quizizz/mongo", - "version": "0.1.1", + "version": "0.2.2", "description": "A simple wrapper around mongo native nodejs", "main": "dist/Mongo.js", "types": "dist/Mongo.d.ts", @@ -8,7 +8,7 @@ "/dist" ], "scripts": { - "compile": "npx tsc", + "compile-ts": "npx tsc", "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { @@ -22,9 +22,10 @@ ], "license": "ISC", "dependencies": { - "mongodb": "^3.6.9" + "mongodb": "6.3.0" }, "devDependencies": { - "@types/mongodb": "^3.6.17" + "@types/node": "^20.10.1", + "typescript": "^5.3.2" } -} \ No newline at end of file +} diff --git a/src/Mongo.ts b/src/Mongo.ts index 725e6e7..baccf47 100644 --- a/src/Mongo.ts +++ b/src/Mongo.ts @@ -3,12 +3,9 @@ import { Db, MongoClient, MongoClientOptions, - Logger as MongoLogger, + MongoError, ObjectId, ReadPreference, - MongoServerSelectionError, - MongoNetworkError, - MongoTimeoutError, } from "mongodb"; export interface Server { @@ -59,13 +56,10 @@ export class MongoConnect implements Mongo { this.emitter = emitter; this.userConfig = userConfig; this.config = { - keepAlive: true, - poolSize: 5, + maxPoolSize: 5, connectTimeoutMS: 30000, socketTimeoutMS: 30000, serverSelectionTimeoutMS: 10000, - useUnifiedTopology: true, - connectWithNoPrimary: false, readPreference: ReadPreference.SECONDARY_PREFERRED, }; this.config.authSource = (userConfig.auth || {}).authSource; @@ -124,15 +118,11 @@ export class MongoConnect implements Mongo { } static isValidError(err: Error) { - return ( - err instanceof MongoServerSelectionError || - err instanceof MongoNetworkError || - err instanceof MongoTimeoutError - ); + return err instanceof MongoError; } getClient(): MongoClient { - return this.mongoClient; + return this.mongoClient; } async connect(): Promise { @@ -164,10 +154,6 @@ export class MongoConnect implements Mongo { } this.client = this.mongoClient.db(this.userConfig.db); this.success(`Successfully connected in ${this.mode} mode`); - MongoLogger.setLevel("info"); - MongoLogger.setCurrentLogger((msg, context) => { - this.log(msg, context); - }); if (oldMongoClient instanceof MongoClient) { // Do NOT wait. If you wait, this might block indefinitely due to the older server being out of action. oldMongoClient.close(); @@ -179,14 +165,13 @@ export class MongoConnect implements Mongo { export async function handleMongoError(err: Error, mongo: Mongo) { if (MongoConnect.isValidError(err)) { if (mongo.reconnecting === null) { - mongo.reconnecting = mongo.connect() - .then(() => { - return null; - }); + mongo.reconnecting = mongo.connect().then(() => { + return null; + }); } await (mongo.reconnecting || Promise.resolve()); mongo.reconnecting = null; - return null + return null; } return err; } @@ -225,7 +210,7 @@ export function MongoFactory( mode: MODES, name: string, emitter: events.EventEmitter, - config: ServerConfig | ReplicaConfig | ShardConfig, + config: ServerConfig | ReplicaConfig | ShardConfig ): Mongo { switch (mode) { case MODES.SERVER: @@ -243,7 +228,7 @@ class ServerMongo extends MongoConnect { constructor( name: string, emitter: events.EventEmitter, - config: ServerConfig, + config: ServerConfig ) { const { db, host, port, auth } = config; const userConfig: UserConfig = { @@ -288,8 +273,11 @@ class ShardMongo extends MongoConnect { } } - -export function MongoFactoryAuto(name: string, emitter: events.EventEmitter, config: MongoConfig) { +export function MongoFactoryAuto( + name: string, + emitter: events.EventEmitter, + config: MongoConfig +) { if ((config as ReplicaConfig).replica) { return MongoFactory(MODES.REPLSET, name, emitter, config); } else if ((config as ShardConfig).shard) { @@ -319,4 +307,3 @@ export function castToObjectId(value: string) { export type MongoConfig = ServerConfig | ReplicaConfig | ShardConfig; export { ObjectId, MongoClient, Db }; -