@discue/mongodb-resource-client
Advanced tools
Comparing version 0.12.1 to 0.13.0
@@ -23,6 +23,6 @@ export = exports; | ||
* @param {Object} ref the resource to be stored | ||
* @param {import('mongodb').UpdateOptions} [options=null] | ||
* @param {import('mongodb').UpdateOptions | WithMongoClient} [options=null] | ||
* @returns {Promise.<ObjectId>} | ||
*/ | ||
create(resourceIds: string | Array<string>, ref: any, options?: import('mongodb').UpdateOptions): Promise<ObjectId>; | ||
create(resourceIds: string | Array<string>, ref: any, options?: import('mongodb').UpdateOptions | WithMongoClient): Promise<ObjectId>; | ||
/** | ||
@@ -42,6 +42,6 @@ * Delete a reference | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {import('mongodb').FindOneAndUpdateOptions} [options=null] | ||
* @param {import('mongodb').FindOneAndUpdateOptions | WithMongoClient} [options=null] | ||
* @returns {Promise.<void>} | ||
*/ | ||
delete(resourceIds: string | Array<string>, options?: import('mongodb').FindOneAndUpdateOptions): Promise<void>; | ||
delete(resourceIds: string | Array<string>, options?: import('mongodb').FindOneAndUpdateOptions | WithMongoClient): Promise<void>; | ||
} | ||
@@ -48,0 +48,0 @@ declare namespace exports { |
@@ -107,3 +107,3 @@ const { EQUALS_ANY_OF, EQUALS } = require('./aggregations.js') | ||
* @param {Object} ref the resource to be stored | ||
* @param {import('mongodb').UpdateOptions} [options=null] | ||
* @param {import('mongodb').UpdateOptions | WithMongoClient} [options=null] | ||
* @returns {Promise.<ObjectId>} | ||
@@ -118,3 +118,3 @@ */ | ||
const collection = await this._getCollection() | ||
const collection = await this._getCollection(options?.client) | ||
const result = await collection.updateOne({ | ||
@@ -160,3 +160,3 @@ id: resourceIds.at(0) | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {import('mongodb').FindOneAndUpdateOptions} [options=null] | ||
* @param {import('mongodb').FindOneAndUpdateOptions | WithMongoClient} [options=null] | ||
* @returns {Promise.<void>} | ||
@@ -170,3 +170,3 @@ */ | ||
const collection = await this._getCollection() | ||
const collection = await this._getCollection(options?.client) | ||
const result = await collection.findOneAndUpdate({ | ||
@@ -178,3 +178,3 @@ id: resourceIds.at(0), | ||
} | ||
}, this._passSessionIfTransactionEnabled(options)) | ||
}, { includeResultMetadata: true, ...this._passSessionIfTransactionEnabled(options) }) | ||
@@ -181,0 +181,0 @@ const success = result.ok === 1 |
@@ -202,3 +202,5 @@ const { PROJECT, EQUALS } = require('./aggregations.js') | ||
} | ||
}, update) | ||
}, update, { | ||
includeResultMetadata: true | ||
}) | ||
@@ -246,3 +248,3 @@ const success = result.ok === 1 | ||
} | ||
}) | ||
}, { includeResultMetadata: true }) | ||
@@ -249,0 +251,0 @@ const success = result.ok === 1 |
@@ -322,5 +322,5 @@ const Base = require('./simple-resource-storage.js') | ||
async create(resourceIds, resource) { | ||
return this._withTransaction(async (session) => { | ||
const newId = await super.create(resourceIds.slice(-1), resource, { session }) | ||
await this._hostStorage.create(this._getResourceIdsForHostStorage(resourceIds), newId, { session }) | ||
return this._withTransaction(async (session, client) => { | ||
const newId = await super.create(resourceIds.slice(-1), resource, { session, client }) | ||
await this._hostStorage.create(this._getResourceIdsForHostStorage(resourceIds), newId, { session, client }) | ||
@@ -330,3 +330,3 @@ if (this._enableTwoWayReferences) { | ||
[`${this._hostStorage._collectionName}_ref`]: resourceIds.at(0) | ||
}, { session }) | ||
}, { session, client }) | ||
} | ||
@@ -350,3 +350,3 @@ | ||
async update(resourceIds, update) { | ||
return this._withTransaction(async (session) => { | ||
return this._withTransaction(async (session, client) => { | ||
const resource = await this.get(resourceIds) | ||
@@ -357,3 +357,3 @@ if (!resource) { | ||
const updateResult = await this._updateUnsafe(resourceIds.slice(-1), update, { session }) | ||
const updateResult = await this._updateUnsafe(resourceIds.slice(-1), update, { session, client }) | ||
await session.commitTransaction() | ||
@@ -378,3 +378,3 @@ | ||
async delete(resourceIds) { | ||
return this._withTransaction(async (session) => { | ||
return this._withTransaction(async (session, client) => { | ||
const resource = await this.get(resourceIds) | ||
@@ -385,4 +385,4 @@ if (!resource) { | ||
const deleteResult = await this._hostStorage.delete(this._getResourceIdsForHostStorage(resourceIds), { session }) | ||
await this._deleteUnsafe(resourceIds.slice(-1), resource, { session }) | ||
const deleteResult = await this._hostStorage.delete(this._getResourceIdsForHostStorage(resourceIds), { session, client }) | ||
await this._deleteUnsafe(resourceIds.slice(-1), resource, { session, client }) | ||
await session.commitTransaction() | ||
@@ -389,0 +389,0 @@ |
@@ -29,5 +29,7 @@ export = exports; | ||
* | ||
* @param {import('mongodb').MongoClient} [collectionName] | ||
* @param {import('mongodb').MongoClient} [givenClient] | ||
* @returns {Promise.<Collection>} | ||
*/ | ||
_getCollection(collectionName: any): Promise<Collection>; | ||
_getCollection(collectionName?: import('mongodb').MongoClient, givenClient?: import('mongodb').MongoClient): Promise<Collection>; | ||
/** | ||
@@ -43,9 +45,2 @@ * @callback WithSessionCallback | ||
_passSessionIfTransactionEnabled(options: any): any; | ||
/** | ||
* | ||
* @param {Function} callback | ||
* @param {Array.<any>} resultTransport | ||
* @returns | ||
*/ | ||
_asyncResultCollector(callback: Function, resultTransport: Array<any>): (...args: any[]) => Promise<void>; | ||
_toStringIfArray(ids: any): any; | ||
@@ -114,6 +109,6 @@ /** | ||
* @param {Object} update values that should be updated | ||
* @param {import('mongodb').DeleteOptions} options | ||
* @param {import('mongodb').DeleteOptions | WithMongoClient} options | ||
* @returns | ||
*/ | ||
_updateUnsafe(resourceIds: string | Array<string>, update: any, options: import('mongodb').DeleteOptions): Promise<void>; | ||
_updateUnsafe(resourceIds: string | Array<string>, update: any, options: import('mongodb').DeleteOptions | WithMongoClient): Promise<void>; | ||
_createUpdateMetadata(): { | ||
@@ -134,6 +129,6 @@ '_meta_data.updated_at': Timestamp; | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {import('mongodb').DeleteOptions} options | ||
* @param {import('mongodb').DeleteOptions | WithMongoClient} options | ||
* @returns | ||
*/ | ||
_deleteUnsafe(resourceIds: string | Array<string>, options: import('mongodb').DeleteOptions): Promise<void>; | ||
_deleteUnsafe(resourceIds: string | Array<string>, options: import('mongodb').DeleteOptions | WithMongoClient): Promise<void>; | ||
/** | ||
@@ -148,3 +143,3 @@ * Closes the datbase client. | ||
declare namespace exports { | ||
export { ConstructorOptions, GetOptions, Collection, MongoClient }; | ||
export { ConstructorOptions, GetOptions, WithMongoClient, Collection, MongoClient }; | ||
} | ||
@@ -164,2 +159,5 @@ import { MongoClient } from "mongodb"; | ||
import { Timestamp } from "mongodb"; | ||
type WithMongoClient = { | ||
client: import('mongodb').MongoClient; | ||
}; | ||
type ConstructorOptions = { | ||
@@ -166,0 +164,0 @@ /** |
@@ -32,3 +32,10 @@ const { MongoClient, Timestamp } = require('mongodb') | ||
/** | ||
* @name WithMongoClient | ||
* @private | ||
* @typedef WithMongoClient | ||
* @property {import('mongodb').MongoClient} client | ||
*/ | ||
/** | ||
* @private | ||
* @typedef {import('mongodb').Collection} Collection | ||
@@ -96,6 +103,12 @@ * @typedef {import('mongodb').MongoClient} MongoClient | ||
* | ||
* @param {import('mongodb').MongoClient} [collectionName] | ||
* @param {import('mongodb').MongoClient} [givenClient] | ||
* @returns {Promise.<Collection>} | ||
*/ | ||
async _getCollection(collectionName) { | ||
const client = await this._getConnectedClient() | ||
async _getCollection(collectionName, givenClient) { | ||
if (typeof collectionName === 'object') { | ||
givenClient = collectionName | ||
collectionName = null | ||
} | ||
const client = givenClient ?? await this._getConnectedClient() | ||
const db = client.db(this._databaseName) | ||
@@ -117,6 +130,4 @@ return db.collection(collectionName ?? this._collectionName) | ||
const session = client.startSession() | ||
const resultContainer = [] | ||
const resultCollectionCallback = this._asyncResultCollector(callback, resultContainer) | ||
try { | ||
await session.withTransaction(resultCollectionCallback, { | ||
return await session.withTransaction((transaction) => callback.call(this, transaction, client), { | ||
readPreference: 'primary', | ||
@@ -134,3 +145,2 @@ readConcern: { level: 'local' }, | ||
} | ||
return resultContainer.at(0) | ||
} | ||
@@ -145,15 +155,2 @@ | ||
/** | ||
* | ||
* @param {Function} callback | ||
* @param {Array.<any>} resultTransport | ||
* @returns | ||
*/ | ||
_asyncResultCollector(callback, resultTransport) { | ||
return async (...args) => { | ||
const result = await callback.apply(null, args) | ||
resultTransport.push(result) | ||
} | ||
} | ||
_toStringIfArray(ids) { | ||
@@ -295,3 +292,3 @@ if (Array.isArray(ids)) { | ||
async update(resourceIds, update) { | ||
return this._withTransaction(async (session) => { | ||
return this._withTransaction(async (session, client) => { | ||
const resource = await this.get(resourceIds) | ||
@@ -302,3 +299,3 @@ if (!resource) { | ||
const updateResult = await this._updateUnsafe(resourceIds, update, { session }) | ||
const updateResult = await this._updateUnsafe(resourceIds, update, { session, client }) | ||
await session.commitTransaction() | ||
@@ -320,3 +317,3 @@ | ||
* @param {Object} update values that should be updated | ||
* @param {import('mongodb').DeleteOptions} options | ||
* @param {import('mongodb').DeleteOptions | WithMongoClient} options | ||
* @returns | ||
@@ -345,3 +342,3 @@ */ | ||
const collection = await this._getCollection() | ||
const collection = await this._getCollection(options.client) | ||
const result = await collection.updateOne({ | ||
@@ -380,3 +377,3 @@ id: this._toStringIfArray(resourceIds) | ||
async delete(resourceIds) { | ||
return this._withTransaction(async (session) => { | ||
return this._withTransaction(async (session, client) => { | ||
const resource = await this.get(resourceIds) | ||
@@ -391,3 +388,3 @@ if (!resource) { | ||
const deleteResult = await this._deleteUnsafe(resourceIds, resource, session) | ||
const deleteResult = await this._deleteUnsafe(resourceIds, resource, { session, client }) | ||
await session.commitTransaction() | ||
@@ -402,7 +399,7 @@ return deleteResult | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {import('mongodb').DeleteOptions} options | ||
* @param {import('mongodb').DeleteOptions | WithMongoClient} options | ||
* @returns | ||
*/ | ||
async _deleteUnsafe(resourceIds, options) { | ||
const collection = await this._getCollection() | ||
const collection = await this._getCollection(options.client) | ||
const result = await collection.deleteOne({ | ||
@@ -409,0 +406,0 @@ id: this._toStringIfArray(resourceIds) |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.12.1", | ||
"version": "0.13.0", | ||
"description": "Simple wrapper around mongodb client allowing easier managing of resources", | ||
@@ -53,15 +53,15 @@ "main": "lib/index", | ||
"devDependencies": { | ||
"chai": "^4.3.7", | ||
"chai": "^4.3.8", | ||
"documentation": "^14.0.2", | ||
"eslint": "^8.46.0", | ||
"eslint": "^8.48.0", | ||
"mocha": "^10.2.0", | ||
"mongodb-memory-server": "^8.13.0", | ||
"mongodb-memory-server": "^8.15.1", | ||
"nodemon": "^3.0.1", | ||
"standard-version": "^9.5.0", | ||
"typescript": "^5.1.6", | ||
"@types/node": "^20.4.10" | ||
"typescript": "^5.2.2", | ||
"@types/node": "^20.5.7" | ||
}, | ||
"optionalDependencies": { | ||
"mongodb": "^5.7.0" | ||
"mongodb": "^6.0.0" | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
94149
2323