@e22m4u/js-repository-mongodb-adapter
Advanced tools
Comparing version 0.0.16 to 0.0.17
{ | ||
"name": "@e22m4u/js-repository-mongodb-adapter", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "MongoDB адаптер для @e22m4u/js-repository", | ||
@@ -5,0 +5,0 @@ "type": "module", |
/* eslint no-unused-vars: 0 */ | ||
import {ObjectId} from 'mongodb'; | ||
import {MongoClient} from 'mongodb'; | ||
import {EventEmitter} from 'events'; | ||
import {waitAsync} from './utils/index.js'; | ||
import {isIsoDate} from './utils/index.js'; | ||
import {isObjectId} from './utils/index.js'; | ||
import {Adapter} from '@e22m4u/js-repository'; | ||
import {DataType} from '@e22m4u/js-repository'; | ||
import {isIsoDate} from './utils/is-iso-date.js'; | ||
import {capitalize} from '@e22m4u/js-repository'; | ||
@@ -74,45 +72,9 @@ import {createMongodbUrl} from './utils/index.js'; | ||
/** | ||
* Mongo client events. | ||
* 5.8.1 | ||
* | ||
* @type {string[]} | ||
*/ | ||
const MONGO_CLIENT_EVENTS = [ | ||
'connectionPoolCreated', | ||
'connectionPoolReady', | ||
'connectionPoolCleared', | ||
'connectionPoolClosed', | ||
'connectionCreated', | ||
'connectionReady', | ||
'connectionClosed', | ||
'connectionCheckOutStarted', | ||
'connectionCheckOutFailed', | ||
'connectionCheckedOut', | ||
'connectionCheckedIn', | ||
'commandStarted', | ||
'commandSucceeded', | ||
'commandFailed', | ||
'serverOpening', | ||
'serverClosed', | ||
'serverDescriptionChanged', | ||
'topologyOpening', | ||
'topologyClosed', | ||
'topologyDescriptionChanged', | ||
'error', | ||
'timeout', | ||
'close', | ||
'serverHeartbeatStarted', | ||
'serverHeartbeatSucceeded', | ||
'serverHeartbeatFailed', | ||
]; | ||
/** | ||
* Default settings. | ||
* | ||
* @type {{connectTimeoutMS: number}} | ||
* @type {object} | ||
*/ | ||
const DEFAULT_SETTINGS = { | ||
reconnectInterval: 2000, // adapter specific option | ||
connectTimeoutMS: 2000, | ||
serverSelectionTimeoutMS: 2000, | ||
// connectTimeoutMS: 2500, | ||
// serverSelectionTimeoutMS: 2500, | ||
}; | ||
@@ -127,2 +89,3 @@ | ||
* | ||
* @type {MongoClient} | ||
* @private | ||
@@ -133,67 +96,19 @@ */ | ||
/** | ||
* Collections. | ||
* Client. | ||
* | ||
* @type {Map<any, any>} | ||
* @private | ||
* @returns {MongoClient} | ||
*/ | ||
_collections = new Map(); | ||
/** | ||
* Connected. | ||
* | ||
* @type {boolean} | ||
* @private | ||
*/ | ||
_connected = false; | ||
/** | ||
* Connected. | ||
* | ||
* @return {boolean} | ||
*/ | ||
get connected() { | ||
return this._connected; | ||
get client() { | ||
return this._client; | ||
} | ||
/** | ||
* Connecting. | ||
* Collections. | ||
* | ||
* @type {boolean} | ||
* @type {Map<any, any>} | ||
* @private | ||
*/ | ||
_connecting = false; | ||
_collections = new Map(); | ||
/** | ||
* Connecting. | ||
* | ||
* @return {boolean} | ||
*/ | ||
get connecting() { | ||
return this._connecting; | ||
} | ||
/** | ||
* Event emitter. | ||
* | ||
* @private | ||
*/ | ||
_emitter; | ||
/** | ||
* Event emitter. | ||
* | ||
* @returns {EventEmitter} | ||
*/ | ||
get emitter() { | ||
if (this._emitter) return this._emitter; | ||
this._emitter = new EventEmitter(); | ||
const emit = this._emitter.emit; | ||
this._emitter.emit = function (name, ...args) { | ||
emit.call(this, '*', name, ...args); | ||
return emit.call(this, name, ...args); | ||
}; | ||
return this._emitter; | ||
} | ||
/** | ||
* Constructor. | ||
@@ -211,89 +126,8 @@ * | ||
super(container, settings); | ||
} | ||
/** | ||
* Connect. | ||
* | ||
* @return {Promise<*|undefined>} | ||
* @private | ||
*/ | ||
async connect() { | ||
if (this._connecting) { | ||
await waitAsync(500); | ||
return this.connect(); | ||
} | ||
if (this._connected) return; | ||
this._connecting = true; | ||
const options = selectObjectKeys(this.settings, MONGODB_OPTION_NAMES); | ||
const url = createMongodbUrl(this.settings); | ||
// console.log(`Connecting to ${url}`); | ||
if (this._client) { | ||
this._client.removeAllListeners(); | ||
this._client.close(true); | ||
} | ||
this._client = new MongoClient(url, options); | ||
for (const event of MONGO_CLIENT_EVENTS) { | ||
const listener = (...args) => this.emitter.emit(event, ...args); | ||
this._client.on(event, listener); | ||
} | ||
const {reconnectInterval} = this.settings; | ||
const connectFn = async () => { | ||
if (this._connecting === false) return; | ||
this.emitter.emit('connecting'); | ||
try { | ||
await this._client.connect(); | ||
} catch (e) { | ||
this.emitter.emit('error', e); | ||
console.error(e); | ||
// console.log('MongoDB connection failed!'); | ||
// console.log(`Reconnecting after ${reconnectInterval} ms.`); | ||
await waitAsync(reconnectInterval); | ||
return connectFn(); | ||
} | ||
// console.log('MongoDB is connected.'); | ||
this._connected = true; | ||
this._connecting = false; | ||
reconnectOnClose(); | ||
this.emitter.emit('connected'); | ||
}; | ||
const reconnectOnClose = () => | ||
this._client.once('serverClosed', event => { | ||
this.emitter.emit('disconnected', event); | ||
if (this._connected) { | ||
this._connected = false; | ||
this._connecting = true; | ||
// console.log('MongoDB lost connection!'); | ||
// console.log(event); | ||
// console.log(`Reconnecting after ${reconnectInterval} ms.`); | ||
setTimeout(() => connectFn(), reconnectInterval); | ||
} else { | ||
// console.log('MongoDB connection closed.'); | ||
} | ||
}); | ||
return connectFn(); | ||
} | ||
/** | ||
* Disconnect. | ||
* | ||
* @return {Promise<undefined>} | ||
*/ | ||
async disconnect() { | ||
this._connected = false; | ||
this._connecting = false; | ||
if (this._client) { | ||
const client = this._client; | ||
this._client = undefined; | ||
await client.close(); | ||
client.removeAllListeners(); | ||
} | ||
} | ||
/** | ||
* Get id prop name. | ||
@@ -334,15 +168,2 @@ * | ||
/** | ||
* Coerce iso date. | ||
* | ||
* @param value | ||
* @return {*|Date} | ||
* @private | ||
*/ | ||
_coerceIsoDate(value) { | ||
if (value === null) return value; | ||
if (isIsoDate(value)) return new Date(value); | ||
return value; | ||
} | ||
/** | ||
* To database. | ||
@@ -427,3 +248,3 @@ * | ||
this.getService(ModelDefinitionUtils).getTableNameByModelName(modelName); | ||
collection = this._client.db(this.settings.database).collection(tableName); | ||
collection = this.client.db(this.settings.database).collection(tableName); | ||
this._collections.set(modelName, collection); | ||
@@ -725,3 +546,2 @@ return collection; | ||
async create(modelName, modelData, filter = undefined) { | ||
await this.connect(); | ||
const idPropName = this._getIdPropName(modelName); | ||
@@ -762,3 +582,2 @@ const idValue = modelData[idPropName]; | ||
async replaceById(modelName, id, modelData, filter = undefined) { | ||
await this.connect(); | ||
id = this._coerceId(id); | ||
@@ -790,3 +609,2 @@ const idPropName = this._getIdPropName(modelName); | ||
async patchById(modelName, id, modelData, filter = undefined) { | ||
await this.connect(); | ||
id = this._coerceId(id); | ||
@@ -816,3 +634,2 @@ const idPropName = this._getIdPropName(modelName); | ||
async find(modelName, filter = undefined) { | ||
await this.connect(); | ||
filter = filter || {}; | ||
@@ -839,3 +656,2 @@ const query = this._buildQuery(modelName, filter.where); | ||
async findById(modelName, id, filter = undefined) { | ||
await this.connect(); | ||
id = this._coerceId(id); | ||
@@ -861,3 +677,2 @@ const table = this._getCollection(modelName); | ||
async delete(modelName, where = undefined) { | ||
await this.connect(); | ||
const table = this._getCollection(modelName); | ||
@@ -877,3 +692,2 @@ const query = this._buildQuery(modelName, where); | ||
async deleteById(modelName, id) { | ||
await this.connect(); | ||
id = this._coerceId(id); | ||
@@ -893,3 +707,2 @@ const table = this._getCollection(modelName); | ||
async exists(modelName, id) { | ||
await this.connect(); | ||
id = this._coerceId(id); | ||
@@ -909,3 +722,2 @@ const table = this._getCollection(modelName); | ||
async count(modelName, where = undefined) { | ||
await this.connect(); | ||
const query = this._buildQuery(modelName, where); | ||
@@ -912,0 +724,0 @@ const table = this._getCollection(modelName); |
@@ -1,4 +0,4 @@ | ||
export * from './wait-async.js'; | ||
export * from './is-iso-date.js'; | ||
export * from './is-object-id.js'; | ||
export * from './create-mongodb-url.js'; | ||
export * from './transform-values-deep.js'; |
Sorry, the diff of this file is too big to display
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
0
138878
26
3479