Skip to content

Commit bbd5ef7

Browse files
authored
Merge pull request #13076 from Automattic/6.10
6.10
2 parents 831009a + 6791962 commit bbd5ef7

File tree

6 files changed

+137
-3
lines changed

6 files changed

+137
-3
lines changed

lib/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ function Mongoose(options) {
7171
autoIndex: true,
7272
autoCreate: true
7373
}, options);
74-
const conn = this.createConnection(); // default connection
75-
conn.models = this.models;
74+
const createInitialConnection = utils.getOption('createInitialConnection', this.options);
75+
if (createInitialConnection == null || createInitialConnection) {
76+
const conn = this.createConnection(); // default connection
77+
conn.models = this.models;
78+
}
7679

7780
if (this.options.pluralization) {
7881
this._pluralize = legacyPluralize;

lib/schema.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,48 @@ Schema.prototype.pick = function(paths, options) {
478478
return newSchema;
479479
};
480480

481+
/**
482+
* Returns a new schema that has the `paths` from the original schema, minus the omitted ones.
483+
*
484+
* This method is analagous to [Lodash's `omit()` function](https://lodash.com/docs/#omit) for Mongoose schemas.
485+
*
486+
* #### Example:
487+
*
488+
* const schema = Schema({ name: String, age: Number });
489+
* // Creates a new schema omitting the `age` path
490+
* const newSchema = schema.omit(['age']);
491+
*
492+
* newSchema.path('name'); // SchemaString { ... }
493+
* newSchema.path('age'); // undefined
494+
*
495+
* @param {String[]} paths List of Paths to omit for the new Schema
496+
* @param {Object} [options] Options to pass to the new Schema Constructor (same as `new Schema(.., Options)`). Defaults to `this.options` if not set.
497+
* @return {Schema}
498+
* @api public
499+
*/
500+
501+
Schema.prototype.omit = function(paths, options) {
502+
const newSchema = new Schema(this, options || this.options);
503+
if (!Array.isArray(paths)) {
504+
throw new MongooseError(
505+
'Schema#omit() only accepts an array argument, ' +
506+
'got "' +
507+
typeof paths +
508+
'"'
509+
);
510+
}
511+
512+
newSchema.remove(paths);
513+
514+
for (const nested in newSchema.singleNestedPaths) {
515+
if (paths.includes(nested)) {
516+
delete newSchema.singleNestedPaths[nested];
517+
}
518+
}
519+
520+
return newSchema;
521+
};
522+
481523
/**
482524
* Returns default options for this schema, merged with `options`.
483525
*

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"dependencies": {
2222
"bson": "^4.7.0",
2323
"kareem": "2.5.1",
24-
"mongodb": "4.13.0",
24+
"mongodb": "4.14.0",
2525
"mpath": "0.9.0",
2626
"mquery": "4.0.3",
2727
"ms": "2.1.3",

test/connection.test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,4 +1545,11 @@ describe('connections:', function() {
15451545
const res = await m.connection.db.listCollections().toArray();
15461546
assert.ok(!res.map(c => c.name).includes('gh12940_Conn'));
15471547
});
1548+
1549+
it('should not create default connection with createInitialConnection = false (gh-12965)', function() {
1550+
const m = new mongoose.Mongoose({
1551+
createInitialConnection: false
1552+
});
1553+
assert.deepEqual(m.connections.length, 0);
1554+
});
15481555
});

test/schema.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,6 +2339,81 @@ describe('schema', function() {
23392339
});
23402340
});
23412341

2342+
describe('omit() (gh-12931)', function() {
2343+
it('works with nested paths', function() {
2344+
const schema = Schema({
2345+
name: {
2346+
first: {
2347+
type: String,
2348+
required: true
2349+
},
2350+
last: {
2351+
type: String,
2352+
required: true
2353+
}
2354+
},
2355+
age: {
2356+
type: Number,
2357+
index: true
2358+
}
2359+
});
2360+
assert.ok(schema.path('name.first'));
2361+
assert.ok(schema.path('name.last'));
2362+
2363+
let newSchema = schema.omit(['name.first']);
2364+
assert.ok(!newSchema.path('name.first'));
2365+
assert.ok(newSchema.path('age'));
2366+
assert.ok(newSchema.path('age').index);
2367+
2368+
newSchema = schema.omit(['age']);
2369+
assert.ok(newSchema.path('name.first'));
2370+
assert.ok(newSchema.path('name.first').required);
2371+
assert.ok(newSchema.path('name.last'));
2372+
assert.ok(newSchema.path('name.last').required);
2373+
assert.ok(!newSchema.path('age'));
2374+
2375+
newSchema = schema.omit(['name.last', 'age']);
2376+
assert.ok(newSchema.path('name.first'));
2377+
assert.ok(newSchema.path('name.first').required);
2378+
assert.ok(!newSchema.path('name.last'));
2379+
assert.ok(!newSchema.path('age'));
2380+
});
2381+
2382+
it('with single nested paths', function() {
2383+
const schema = Schema({
2384+
name: Schema({
2385+
first: {
2386+
type: String,
2387+
required: true
2388+
},
2389+
last: {
2390+
type: String,
2391+
required: true
2392+
}
2393+
}),
2394+
age: {
2395+
type: Number,
2396+
index: true
2397+
}
2398+
});
2399+
assert.ok(schema.path('name.first'));
2400+
assert.ok(schema.path('name.last'));
2401+
2402+
let newSchema = schema.omit(['age']);
2403+
assert.ok(newSchema.path('name.first'));
2404+
assert.ok(newSchema.path('name.first').required);
2405+
assert.ok(newSchema.path('name.last'));
2406+
assert.ok(newSchema.path('name.last').required);
2407+
assert.ok(!newSchema.path('age'));
2408+
2409+
newSchema = schema.omit(['name.last', 'age']);
2410+
assert.ok(newSchema.path('name.first'));
2411+
assert.ok(newSchema.path('name.first').required);
2412+
assert.ok(!newSchema.path('name.last'));
2413+
assert.ok(!newSchema.path('age'));
2414+
});
2415+
});
2416+
23422417
describe('path-level custom cast (gh-8300)', function() {
23432418
it('with numbers', function() {
23442419
const schema = Schema({

types/mongooseoptions.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ declare module 'mongoose' {
6363
*/
6464
cloneSchemas?: boolean;
6565

66+
/**
67+
* Set to `false` to disable the creation of the initial default connection.
68+
*
69+
* @default true
70+
*/
71+
createInitialConnection?: boolean;
72+
6673
/**
6774
* If `true`, prints the operations mongoose sends to MongoDB to the console.
6875
* If a writable stream is passed, it will log to that stream, without colorization.

0 commit comments

Comments
 (0)