Socket
Socket
Sign inDemoInstall

mongoose

Package Overview
Dependencies
221
Maintainers
4
Versions
868
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 8.3.5 to 8.4.0

lib/helpers/omitUndefined.js

5

lib/aggregate.js

@@ -1025,2 +1025,7 @@ 'use strict';

const asyncLocalStorage = this.model()?.db?.base.transactionAsyncLocalStorage?.getStore();
if (!this.options.hasOwnProperty('session') && asyncLocalStorage?.session != null) {
this.options.session = asyncLocalStorage.session;
}
if (this.options && this.options.cursor) {

@@ -1027,0 +1032,0 @@ return new AggregationCursor(this);

71

lib/connection.js

@@ -401,7 +401,3 @@ 'use strict';

if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
await new Promise(resolve => {
this._queue.push({ fn: resolve });
});
}
await this._waitForConnect();

@@ -498,7 +494,3 @@ return this.db.createCollection(collection, options);

if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
await new Promise(resolve => {
this._queue.push({ fn: resolve });
});
}
await this._waitForConnect();

@@ -544,3 +536,3 @@ const session = this.client.startSession(options);

session[sessionNewDocuments] = new Map();
return session.withTransaction(() => _wrapUserTransaction(fn, session), options).
return session.withTransaction(() => _wrapUserTransaction(fn, session, this.base), options).
then(res => {

@@ -564,5 +556,12 @@ delete session[sessionNewDocuments];

async function _wrapUserTransaction(fn, session) {
async function _wrapUserTransaction(fn, session, mongoose) {
try {
const res = await fn(session);
const res = mongoose.transactionAsyncLocalStorage == null
? await fn(session)
: await new Promise(resolve => {
mongoose.transactionAsyncLocalStorage.run(
{ session },
() => resolve(fn(session))
);
});
return res;

@@ -625,2 +624,15 @@ } catch (err) {

await this._waitForConnect();
return this.db.dropCollection(collection);
};
/**
* Waits for connection to be established, so the connection has a `client`
*
* @return Promise
* @api private
*/
Connection.prototype._waitForConnect = async function _waitForConnect() {
if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {

@@ -631,4 +643,2 @@ await new Promise(resolve => {

}
return this.db.dropCollection(collection);
};

@@ -646,7 +656,3 @@

Connection.prototype.listCollections = async function listCollections() {
if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
await new Promise(resolve => {
this._queue.push({ fn: resolve });
});
}
await this._waitForConnect();

@@ -658,2 +664,21 @@ const cursor = this.db.listCollections();

/**
* Helper for MongoDB Node driver's `listDatabases()`.
* Returns an object with a `databases` property that contains an
* array of database objects.
*
* #### Example:
* const { databases } = await mongoose.connection.listDatabases();
* databases; // [{ name: 'mongoose_test', sizeOnDisk: 0, empty: false }]
*
* @method listCollections
* @return {Promise<{ databases: Array<{ name: string }> }>}
* @api public
*/
Connection.prototype.listDatabases = async function listDatabases() {
// Implemented in `lib/drivers/node-mongodb-native/connection.js`
throw new MongooseError('listDatabases() not implemented by driver');
};
/**
* Helper for `dropDatabase()`. Deletes the given database, including all

@@ -678,7 +703,3 @@ * collections, documents, and indexes.

if ((this.readyState === STATES.connecting || this.readyState === STATES.disconnected) && this._shouldBufferCommands()) {
await new Promise(resolve => {
this._queue.push({ fn: resolve });
});
}
await this._waitForConnect();

@@ -685,0 +706,0 @@ // If `dropDatabase()` is called, this model's collection will not be

@@ -200,2 +200,15 @@ /*!

/**
* Implementation of `listDatabases()` for MongoDB driver
*
* @return Promise
* @api public
*/
NativeConnection.prototype.listDatabases = async function listDatabases() {
await this._waitForConnect();
return await this.db.admin().listDatabases();
};
/*!

@@ -202,0 +215,0 @@ * ignore

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@

@@ -78,3 +78,2 @@ 'use strict';

setModel(model) {
this.model = model;
this.message = formatMessage(model, this.kind, this.value, this.path,

@@ -81,0 +80,0 @@ this.messageFormat, this.valueType);

@@ -8,3 +8,3 @@

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -11,0 +11,0 @@ class DivergentArrayError extends MongooseError {

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@

@@ -8,3 +8,3 @@

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -11,0 +11,0 @@ class InvalidSchemaOptionError extends MongooseError {

@@ -8,3 +8,3 @@

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -11,0 +11,0 @@ class MissingSchemaError extends MongooseError {

@@ -7,3 +7,3 @@ 'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');
const util = require('util');

@@ -10,0 +10,0 @@

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@ class ObjectParameterError extends MongooseError {

@@ -8,3 +8,3 @@

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -11,0 +11,0 @@

@@ -7,3 +7,3 @@ 'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@ class ParallelSaveError extends MongooseError {

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@ class StrictPopulateError extends MongooseError {

@@ -7,3 +7,3 @@ /*!

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@

@@ -7,3 +7,3 @@ 'use strict';

const MongooseError = require('./');
const MongooseError = require('./mongooseError');

@@ -10,0 +10,0 @@ class VersionError extends MongooseError {

@@ -6,2 +6,3 @@ 'use strict';

const castNumber = require('../../cast/number');
const omitUndefined = require('../omitUndefined');

@@ -129,3 +130,3 @@ const booleanComparison = new Set(['$and', '$or']);

_omitUndefined(val);
omitUndefined(val);

@@ -135,9 +136,2 @@ return val;

function _omitUndefined(val) {
const keys = Object.keys(val);
for (let i = 0, len = keys.length; i < len; ++i) {
(val[keys[i]] === void 0) && delete val[keys[i]];
}
}
// { $op: <number> }

@@ -144,0 +138,0 @@ function castNumberOperator(val) {

@@ -7,8 +7,12 @@ 'use strict';

require('./driver').set(require('./drivers/node-mongodb-native'));
const mongodbDriver = require('./drivers/node-mongodb-native');
require('./driver').set(mongodbDriver);
const mongoose = require('./mongoose');
mongoose.setDriver(mongodbDriver);
mongoose.Mongoose.prototype.mongo = require('mongodb');
module.exports = mongoose;

@@ -41,2 +41,4 @@ 'use strict';

const { AsyncLocalStorage } = require('node:async_hooks');
/**

@@ -72,4 +74,4 @@ * Mongoose constructor.

}, options);
const createInitialConnection = utils.getOption('createInitialConnection', this.options);
if (createInitialConnection == null || createInitialConnection) {
const createInitialConnection = utils.getOption('createInitialConnection', this.options) ?? true;
if (createInitialConnection && this.__driver != null) {
const conn = this.createConnection(); // default connection

@@ -106,2 +108,6 @@ conn.models = this.models;

if (options?.transactionAsyncLocalStorage) {
this.transactionAsyncLocalStorage = new AsyncLocalStorage();
}
Object.defineProperty(this, 'plugins', {

@@ -175,2 +181,5 @@ configurable: false,

_mongoose.connections[0].models = _mongoose.models;
if (oldDefaultConnection == null) {
return _mongoose;
}

@@ -274,3 +283,3 @@ // Update all models that pointed to the old default connection to

if (optionValue) {
Object.defineProperty(mongoose.Types.ObjectId.prototype, '_id', {
Object.defineProperty(_mongoose.Types.ObjectId.prototype, '_id', {
enumerable: false,

@@ -283,4 +292,10 @@ configurable: true,

} else {
delete mongoose.Types.ObjectId.prototype._id;
delete _mongoose.Types.ObjectId.prototype._id;
}
} else if (optionKey === 'transactionAsyncLocalStorage') {
if (optionValue && !_mongoose.transactionAsyncLocalStorage) {
_mongoose.transactionAsyncLocalStorage = new AsyncLocalStorage();
} else if (!optionValue && _mongoose.transactionAsyncLocalStorage) {
delete _mongoose.transactionAsyncLocalStorage;
}
}

@@ -1288,2 +1303,24 @@ }

/**
* Takes in an object and deletes any keys from the object whose values
* are strictly equal to `undefined`.
* This function is useful for query filters because Mongoose treats
* `TestModel.find({ name: undefined })` as `TestModel.find({ name: null })`.
*
* #### Example:
*
* const filter = { name: 'John', age: undefined, status: 'active' };
* mongoose.omitUndefined(filter); // { name: 'John', status: 'active' }
* filter; // { name: 'John', status: 'active' }
*
* await UserModel.findOne(mongoose.omitUndefined(filter));
*
* @method omitUndefined
* @param {Object} [val] the object to remove undefined keys from
* @returns {Object} the object passed in
* @api public
*/
Mongoose.prototype.omitUndefined = require('./helpers/omitUndefined');
/**
* The exports object is an instance of Mongoose.

@@ -1290,0 +1327,0 @@ *

@@ -35,2 +35,3 @@

'toObject',
'transactionAsyncLocalStorage',
'translateAliases'

@@ -37,0 +38,0 @@ ]);

{
"name": "mongoose",
"description": "Mongoose MongoDB ODM",
"version": "8.3.5",
"version": "8.4.0",
"author": "Guillermo Rauch <guillermo@learnboost.com>",

@@ -22,5 +22,5 @@ "keywords": [

"dependencies": {
"bson": "^6.5.0",
"bson": "^6.7.0",
"kareem": "2.6.3",
"mongodb": "6.5.0",
"mongodb": "6.6.2",
"mpath": "0.9.0",

@@ -27,0 +27,0 @@ "mquery": "5.0.0",

@@ -136,2 +136,8 @@ declare module 'mongoose' {

/**
* Helper for MongoDB Node driver's `listDatabases()`.
* Returns an array of database names.
*/
listDatabases(): Promise<mongodb.ListDatabasesResult>;
/**
* A [POJO](https://masteringjs.io/tutorials/fundamentals/pojo) containing

@@ -138,0 +144,0 @@ * a map from model names to models. Contains all models that have been

@@ -19,3 +19,3 @@ declare module 'mongoose' {

*/
class Document<T = any, TQueryHelpers = any, DocType = any> {
class Document<T = unknown, TQueryHelpers = any, DocType = any> {
constructor(doc?: any);

@@ -22,0 +22,0 @@

@@ -31,3 +31,2 @@ declare class NativeError extends global.Error { }

reason?: NativeError | null;
model?: any;

@@ -34,0 +33,0 @@ constructor(type: string, value: any, path: string, reason?: NativeError, schemaType?: SchemaType);

@@ -23,2 +23,3 @@ /// <reference path="./aggregate.d.ts" />

/// <reference path="./validation.d.ts" />
/// <reference path="./inferrawdoctype.d.ts" />
/// <reference path="./inferschematype.d.ts" />

@@ -72,2 +73,4 @@ /// <reference path="./virtuals.d.ts" />

export function omitUndefined<T extends Record<string, any>>(val: T): T;
/* ! ignore */

@@ -74,0 +77,0 @@ export type CompileModelOptions = {

@@ -572,2 +572,8 @@ declare module 'mongoose' {

/**
* List all [Atlas search indexes](https://www.mongodb.com/docs/atlas/atlas-search/create-index/) on this model's collection.
* This function only works when connected to MongoDB Atlas.
*/
listSearchIndexes(options?: mongodb.ListSearchIndexesOptions): Promise<Array<{ name: string }>>;
/** The name of the model */

@@ -574,0 +580,0 @@ modelName: string;

@@ -207,2 +207,9 @@ declare module 'mongoose' {

/**
* Set to true to make Mongoose use Node.js' built-in AsyncLocalStorage (Node >= 16.0.0)
* to set `session` option on all operations within a `connection.transaction(fn)` call
* by default. Defaults to false.
*/
transactionAsyncLocalStorage?: boolean;
/**
* If `true`, convert any aliases in filter, projection, update, and distinct

@@ -209,0 +216,0 @@ * to their database property names. Defaults to false.

@@ -123,2 +123,6 @@ declare module 'mongoose' {

read?: string;
/**
* Set a default readConcern for all queries at the schema level
*/
readConcern?: { level: 'local' | 'available' | 'majority' | 'snapshot' | 'linearizable' }
/** Allows setting write concern at the schema level. */

@@ -125,0 +129,0 @@ writeConcern?: WriteConcern;

@@ -86,3 +86,3 @@

class Subdocument<IdType = any, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
class Subdocument<IdType = unknown, TQueryHelpers = any, DocType = any> extends Document<IdType, TQueryHelpers, DocType> {
$isSingleNested: true;

@@ -89,0 +89,0 @@

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc