1 | 'use strict';
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | const EventEmitter = require('events').EventEmitter;
|
8 | const Schema = require('./schema');
|
9 | const Collection = require('./driver').get().Collection;
|
10 | const STATES = require('./connectionstate');
|
11 | const MongooseError = require('./error');
|
12 | const PromiseProvider = require('./promise_provider');
|
13 | const applyPlugins = require('./helpers/schema/applyPlugins');
|
14 | const get = require('./helpers/get');
|
15 | const mongodb = require('mongodb');
|
16 | const utils = require('./utils');
|
17 |
|
18 | const parseConnectionString = require('mongodb-core').parseConnectionString;
|
19 |
|
20 | /*!
|
21 | * A list of authentication mechanisms that don't require a password for authentication.
|
22 | * This is used by the authMechanismDoesNotRequirePassword method.
|
23 | *
|
24 | * @api private
|
25 | */
|
26 | const noPasswordAuthMechanisms = [
|
27 | 'MONGODB-X509'
|
28 | ];
|
29 |
|
30 | /**
|
31 | * Connection constructor
|
32 | *
|
33 | * For practical reasons, a Connection equals a Db.
|
34 | *
|
35 | * @param {Mongoose} base a mongoose instance
|
36 | * @inherits NodeJS EventEmitter http:
|
37 | * @event `connecting`: Emitted when `connection.openUri()` is executed on this connection.
|
38 | * @event `connected`: Emitted when this connection successfully connects to the db. May be emitted _multiple_ times in `reconnected` scenarios.
|
39 | * @event `open`: Emitted after we `connected` and `onOpen` is executed on all of this connections models.
|
40 | * @event `disconnecting`: Emitted when `connection.close()` was executed.
|
41 | * @event `disconnected`: Emitted after getting disconnected from the db.
|
42 | * @event `close`: Emitted after we `disconnected` and `onClose` executed on all of this connections models.
|
43 | * @event `reconnected`: Emitted after we `connected` and subsequently `disconnected`, followed by successfully another successfull connection.
|
44 | * @event `error`: Emitted when an error occurs on this connection.
|
45 | * @event `fullsetup`: Emitted in a replica-set scenario, when primary and at least one seconaries specified in the connection string are connected.
|
46 | * @event `all`: Emitted in a replica-set scenario, when all nodes specified in the connection string are connected.
|
47 | * @api public
|
48 | */
|
49 |
|
50 | function Connection(base) {
|
51 | this.base = base;
|
52 | this.collections = {};
|
53 | this.models = {};
|
54 | this.config = {autoIndex: true};
|
55 | this.replica = false;
|
56 | this.options = null;
|
57 | this.otherDbs = [];
|
58 | this.relatedDbs = {};
|
59 | this.states = STATES;
|
60 | this._readyState = STATES.disconnected;
|
61 | this._closeCalled = false;
|
62 | this._hasOpened = false;
|
63 | this.plugins = [];
|
64 |
|
65 | this.$internalEmitter = new EventEmitter();
|
66 | this.$internalEmitter.setMaxListeners(0);
|
67 | }
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | Connection.prototype.__proto__ = EventEmitter.prototype;
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 |
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 |
|
95 |
|
96 | Object.defineProperty(Connection.prototype, 'readyState', {
|
97 | get: function() {
|
98 | return this._readyState;
|
99 | },
|
100 | set: function(val) {
|
101 | if (!(val in STATES)) {
|
102 | throw new Error('Invalid connection state: ' + val);
|
103 | }
|
104 |
|
105 | if (this._readyState !== val) {
|
106 | this._readyState = val;
|
107 |
|
108 | for (let i = 0; i < this.otherDbs.length; i++) {
|
109 | this.otherDbs[i].readyState = val;
|
110 | }
|
111 |
|
112 |
|
113 | for (const k in this.relatedDbs) {
|
114 | this.relatedDbs[k].readyState = val;
|
115 | }
|
116 |
|
117 | if (STATES.connected === val) {
|
118 | this._hasOpened = true;
|
119 | }
|
120 |
|
121 | this.emit(STATES[val]);
|
122 | }
|
123 | }
|
124 | });
|
125 |
|
126 |
|
127 |
|
128 |
|
129 |
|
130 |
|
131 |
|
132 |
|
133 |
|
134 |
|
135 | Connection.prototype.collections;
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | Connection.prototype.name;
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | Object.defineProperty(Connection.prototype, 'plugins', {
|
170 | configurable: false,
|
171 | enumerable: true,
|
172 | writable: true
|
173 | });
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 | Object.defineProperty(Connection.prototype, 'host', {
|
190 | configurable: true,
|
191 | enumerable: true,
|
192 | writable: true
|
193 | });
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 | Object.defineProperty(Connection.prototype, 'port', {
|
210 | configurable: true,
|
211 | enumerable: true,
|
212 | writable: true
|
213 | });
|
214 |
|
215 |
|
216 |
|
217 |
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 | Object.defineProperty(Connection.prototype, 'user', {
|
229 | configurable: true,
|
230 | enumerable: true,
|
231 | writable: true
|
232 | });
|
233 |
|
234 |
|
235 |
|
236 |
|
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | Object.defineProperty(Connection.prototype, 'pass', {
|
248 | configurable: true,
|
249 | enumerable: true,
|
250 | writable: true
|
251 | });
|
252 |
|
253 |
|
254 |
|
255 |
|
256 |
|
257 |
|
258 |
|
259 |
|
260 |
|
261 |
|
262 | Connection.prototype.db;
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 |
|
269 |
|
270 |
|
271 |
|
272 |
|
273 | Connection.prototype.config;
|
274 |
|
275 |
|
276 |
|
277 |
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 | Connection.prototype.createCollection = _wrapConnHelper(function createCollection(collection, options, cb) {
|
291 | if (typeof options === 'function') {
|
292 | cb = options;
|
293 | options = {};
|
294 | }
|
295 | this.db.createCollection(collection, options, cb);
|
296 | });
|
297 |
|
298 |
|
299 |
|
300 |
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 |
|
315 |
|
316 |
|
317 |
|
318 |
|
319 |
|
320 |
|
321 |
|
322 |
|
323 | Connection.prototype.startSession = _wrapConnHelper(function startSession(options, cb) {
|
324 | if (typeof options === 'function') {
|
325 | cb = options;
|
326 | options = null;
|
327 | }
|
328 | const session = this.client.startSession(options);
|
329 | cb(null, session);
|
330 | });
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 |
|
343 | Connection.prototype.dropCollection = _wrapConnHelper(function dropCollection(collection, cb) {
|
344 | this.db.dropCollection(collection, cb);
|
345 | });
|
346 |
|
347 |
|
348 |
|
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 | Connection.prototype.dropDatabase = _wrapConnHelper(function dropDatabase(cb) {
|
364 |
|
365 |
|
366 |
|
367 |
|
368 | for (const name of Object.keys(this.models)) {
|
369 | delete this.models[name].$init;
|
370 | }
|
371 | this.db.dropDatabase(cb);
|
372 | });
|
373 |
|
374 |
|
375 |
|
376 |
|
377 |
|
378 | function _wrapConnHelper(fn) {
|
379 | return function() {
|
380 | const cb = arguments.length > 0 ? arguments[arguments.length - 1] : null;
|
381 | const argsWithoutCb = typeof cb === 'function' ?
|
382 | Array.prototype.slice.call(arguments, 0, arguments.length - 1) :
|
383 | Array.prototype.slice.call(arguments);
|
384 | return utils.promiseOrCallback(cb, cb => {
|
385 | if (this.readyState !== STATES.connected) {
|
386 | this.once('open', function() {
|
387 | fn.apply(this, argsWithoutCb.concat([cb]));
|
388 | });
|
389 | } else {
|
390 | fn.apply(this, argsWithoutCb.concat([cb]));
|
391 | }
|
392 | });
|
393 | };
|
394 | }
|
395 |
|
396 |
|
397 |
|
398 |
|
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 |
|
405 |
|
406 |
|
407 | Connection.prototype.error = function(err, callback) {
|
408 | if (callback) {
|
409 | callback(err);
|
410 | return null;
|
411 | }
|
412 | if (this.listeners('error').length > 0) {
|
413 | this.emit('error', err);
|
414 | }
|
415 | return Promise.reject(err);
|
416 | };
|
417 |
|
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 |
|
424 | Connection.prototype.onOpen = function() {
|
425 | this.readyState = STATES.connected;
|
426 |
|
427 |
|
428 |
|
429 | for (const i in this.collections) {
|
430 | if (utils.object.hasOwnProperty(this.collections, i)) {
|
431 | this.collections[i].onOpen();
|
432 | }
|
433 | }
|
434 |
|
435 | this.emit('open');
|
436 | };
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | Connection.prototype.openUri = function(uri, options, callback) {
|
449 | this.readyState = STATES.connecting;
|
450 | this._closeCalled = false;
|
451 |
|
452 | if (typeof options === 'function') {
|
453 | callback = options;
|
454 | options = null;
|
455 | }
|
456 |
|
457 | if (['string', 'number'].indexOf(typeof options) !== -1) {
|
458 | throw new MongooseError('Mongoose 5.x no longer supports ' +
|
459 | '`mongoose.connect(host, dbname, port)` or ' +
|
460 | '`mongoose.createConnection(host, dbname, port)`. See ' +
|
461 | 'http://mongoosejs.com/docs/connections.html for supported connection syntax');
|
462 | }
|
463 |
|
464 | if (typeof uri !== 'string') {
|
465 | throw new MongooseError('The `uri` parameter to `openUri()` must be a ' +
|
466 | `string, got "${typeof uri}". Make sure the first parameter to ` +
|
467 | '`mongoose.connect()` or `mongoose.createConnection()` is a string.');
|
468 | }
|
469 |
|
470 | const Promise = PromiseProvider.get();
|
471 | const _this = this;
|
472 |
|
473 | if (options) {
|
474 | options = utils.clone(options);
|
475 | const autoIndex = options.config && options.config.autoIndex != null ?
|
476 | options.config.autoIndex :
|
477 | options.autoIndex;
|
478 | if (autoIndex != null) {
|
479 | this.config.autoIndex = autoIndex !== false;
|
480 | delete options.config;
|
481 | delete options.autoIndex;
|
482 | }
|
483 |
|
484 | if ('autoCreate' in options) {
|
485 | this.config.autoCreate = !!options.autoCreate;
|
486 | delete options.autoCreate;
|
487 | }
|
488 | if ('useCreateIndex' in options) {
|
489 | this.config.useCreateIndex = !!options.useCreateIndex;
|
490 | delete options.useCreateIndex;
|
491 | }
|
492 |
|
493 | if ('useFindAndModify' in options) {
|
494 | this.config.useFindAndModify = !!options.useFindAndModify;
|
495 | delete options.useFindAndModify;
|
496 | }
|
497 |
|
498 |
|
499 | if (options.user || options.pass) {
|
500 | options.auth = options.auth || {};
|
501 | options.auth.user = options.user;
|
502 | options.auth.password = options.pass;
|
503 |
|
504 | this.user = options.user;
|
505 | this.pass = options.pass;
|
506 | }
|
507 | delete options.user;
|
508 | delete options.pass;
|
509 |
|
510 | if (options.bufferCommands != null) {
|
511 | options.bufferMaxEntries = 0;
|
512 | this.config.bufferCommands = options.bufferCommands;
|
513 | delete options.bufferCommands;
|
514 | }
|
515 |
|
516 | if (options.useMongoClient != null) {
|
517 | handleUseMongoClient(options);
|
518 | }
|
519 | } else {
|
520 | options = {};
|
521 | }
|
522 |
|
523 | this._connectionOptions = options;
|
524 | const dbName = options.dbName;
|
525 | if (dbName != null) {
|
526 | this.$dbName = dbName;
|
527 | }
|
528 | delete options.dbName;
|
529 |
|
530 | if (!('promiseLibrary' in options)) {
|
531 | options.promiseLibrary = PromiseProvider.get();
|
532 | }
|
533 | if (!('useNewUrlParser' in options)) {
|
534 | if ('useNewUrlParser' in this.base.options) {
|
535 | options.useNewUrlParser = this.base.options.useNewUrlParser;
|
536 | } else {
|
537 | options.useNewUrlParser = false;
|
538 | }
|
539 | }
|
540 |
|
541 | const parsePromise = new Promise((resolve, reject) => {
|
542 | parseConnectionString(uri, options, (err, parsed) => {
|
543 | if (err) {
|
544 | return reject(err);
|
545 | }
|
546 | this.name = dbName != null ? dbName : get(parsed, 'auth.db', null);
|
547 | this.host = get(parsed, 'hosts.0.host', 'localhost');
|
548 | this.port = get(parsed, 'hosts.0.port', 27017);
|
549 | this.user = this.user || get(parsed, 'auth.username');
|
550 | this.pass = this.pass || get(parsed, 'auth.password');
|
551 | resolve();
|
552 | });
|
553 | });
|
554 |
|
555 | const promise = new Promise((resolve, reject) => {
|
556 | const client = new mongodb.MongoClient(uri, options);
|
557 | _this.client = client;
|
558 | client.connect(function(error) {
|
559 | if (error) {
|
560 | _this.readyState = STATES.disconnected;
|
561 | return reject(error);
|
562 | }
|
563 |
|
564 | const db = dbName != null ? client.db(dbName) : client.db();
|
565 | _this.db = db;
|
566 |
|
567 |
|
568 | db.on('reconnect', function() {
|
569 |
|
570 |
|
571 |
|
572 |
|
573 | if (_this.readyState !== STATES.connected) {
|
574 | _this.readyState = STATES.connected;
|
575 | _this.emit('reconnect');
|
576 | _this.emit('reconnected');
|
577 | }
|
578 | });
|
579 | db.s.topology.on('reconnectFailed', function() {
|
580 | _this.emit('reconnectFailed');
|
581 | });
|
582 | db.s.topology.on('left', function(data) {
|
583 | _this.emit('left', data);
|
584 | });
|
585 | db.s.topology.on('joined', function(data) {
|
586 | _this.emit('joined', data);
|
587 | });
|
588 | db.s.topology.on('fullsetup', function(data) {
|
589 | _this.emit('fullsetup', data);
|
590 | });
|
591 | db.on('close', function() {
|
592 |
|
593 | _this.readyState = STATES.disconnected;
|
594 | });
|
595 | client.on('left', function() {
|
596 | if (_this.readyState === STATES.connected &&
|
597 | get(db, 's.topology.s.coreTopology.s.replicaSetState.topologyType') === 'ReplicaSetNoPrimary') {
|
598 | _this.readyState = STATES.disconnected;
|
599 | }
|
600 | });
|
601 | db.on('timeout', function() {
|
602 | _this.emit('timeout');
|
603 | });
|
604 |
|
605 | delete _this.then;
|
606 | delete _this.catch;
|
607 | _this.readyState = STATES.connected;
|
608 |
|
609 | for (const i in _this.collections) {
|
610 | if (utils.object.hasOwnProperty(_this.collections, i)) {
|
611 | _this.collections[i].onOpen();
|
612 | }
|
613 | }
|
614 |
|
615 | resolve(_this);
|
616 | _this.emit('open');
|
617 | });
|
618 | });
|
619 |
|
620 | this.$initialConnection = Promise.all([promise, parsePromise]).
|
621 | then(res => res[0]).
|
622 | catch(err => {
|
623 | if (this.listeners('error').length > 0) {
|
624 | process.nextTick(() => this.emit('error', err));
|
625 | return;
|
626 | }
|
627 | throw err;
|
628 | });
|
629 | this.then = function(resolve, reject) {
|
630 | return this.$initialConnection.then(resolve, reject);
|
631 | };
|
632 | this.catch = function(reject) {
|
633 | return this.$initialConnection.catch(reject);
|
634 | };
|
635 |
|
636 | if (callback != null) {
|
637 | this.$initialConnection = this.$initialConnection.then(
|
638 | () => callback(null, this),
|
639 | err => callback(err)
|
640 | );
|
641 | }
|
642 |
|
643 | return this;
|
644 | };
|
645 |
|
646 |
|
647 |
|
648 |
|
649 |
|
650 | const handleUseMongoClient = function handleUseMongoClient(options) {
|
651 | console.warn('WARNING: The `useMongoClient` option is no longer ' +
|
652 | 'necessary in mongoose 5.x, please remove it.');
|
653 | const stack = new Error().stack;
|
654 | console.warn(stack.substr(stack.indexOf('\n') + 1));
|
655 | delete options.useMongoClient;
|
656 | };
|
657 |
|
658 |
|
659 |
|
660 |
|
661 |
|
662 |
|
663 |
|
664 |
|
665 |
|
666 |
|
667 | Connection.prototype.close = function(force, callback) {
|
668 | if (typeof force === 'function') {
|
669 | callback = force;
|
670 | force = false;
|
671 | }
|
672 |
|
673 | this.$wasForceClosed = !!force;
|
674 |
|
675 | return utils.promiseOrCallback(callback, cb => {
|
676 | this._close(force, cb);
|
677 | });
|
678 | };
|
679 |
|
680 |
|
681 |
|
682 |
|
683 |
|
684 |
|
685 |
|
686 |
|
687 | Connection.prototype._close = function(force, callback) {
|
688 | const _this = this;
|
689 | this._closeCalled = true;
|
690 |
|
691 | switch (this.readyState) {
|
692 | case 0:
|
693 | callback();
|
694 | break;
|
695 |
|
696 | case 1:
|
697 | this.readyState = STATES.disconnecting;
|
698 | this.doClose(force, function(err) {
|
699 | if (err) {
|
700 | return callback(err);
|
701 | }
|
702 | _this.onClose(force);
|
703 | callback(null);
|
704 | });
|
705 |
|
706 | break;
|
707 | case 2:
|
708 | this.once('open', function() {
|
709 | _this.close(callback);
|
710 | });
|
711 | break;
|
712 |
|
713 | case 3:
|
714 | this.once('close', function() {
|
715 | callback();
|
716 | });
|
717 | break;
|
718 | }
|
719 |
|
720 | return this;
|
721 | };
|
722 |
|
723 |
|
724 |
|
725 |
|
726 |
|
727 |
|
728 |
|
729 | Connection.prototype.onClose = function(force) {
|
730 | this.readyState = STATES.disconnected;
|
731 |
|
732 |
|
733 |
|
734 | for (const i in this.collections) {
|
735 | if (utils.object.hasOwnProperty(this.collections, i)) {
|
736 | this.collections[i].onClose(force);
|
737 | }
|
738 | }
|
739 |
|
740 | this.emit('close', force);
|
741 | };
|
742 |
|
743 |
|
744 |
|
745 |
|
746 |
|
747 |
|
748 |
|
749 |
|
750 |
|
751 |
|
752 |
|
753 |
|
754 | Connection.prototype.collection = function(name, options) {
|
755 | options = options ? utils.clone(options) : {};
|
756 | options.$wasForceClosed = this.$wasForceClosed;
|
757 | if (!(name in this.collections)) {
|
758 | this.collections[name] = new Collection(name, this, options);
|
759 | }
|
760 | return this.collections[name];
|
761 | };
|
762 |
|
763 |
|
764 |
|
765 |
|
766 |
|
767 |
|
768 |
|
769 |
|
770 |
|
771 |
|
772 |
|
773 |
|
774 |
|
775 |
|
776 |
|
777 |
|
778 |
|
779 |
|
780 |
|
781 |
|
782 | Connection.prototype.plugin = function(fn, opts) {
|
783 | this.plugins.push([fn, opts]);
|
784 | return this;
|
785 | };
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 |
|
792 |
|
793 |
|
794 |
|
795 |
|
796 |
|
797 |
|
798 |
|
799 |
|
800 |
|
801 |
|
802 |
|
803 |
|
804 |
|
805 |
|
806 |
|
807 |
|
808 |
|
809 |
|
810 |
|
811 |
|
812 |
|
813 |
|
814 |
|
815 |
|
816 |
|
817 |
|
818 |
|
819 | Connection.prototype.model = function(name, schema, collection) {
|
820 | if (!(this instanceof Connection)) {
|
821 | throw new MongooseError('`connection.model()` should not be run with ' +
|
822 | '`new`. If you are doing `new db.model(foo)(bar)`, use ' +
|
823 | '`db.model(foo)(bar)` instead');
|
824 | }
|
825 |
|
826 | let fn;
|
827 | if (typeof name === 'function') {
|
828 | fn = name;
|
829 | name = fn.name;
|
830 | }
|
831 |
|
832 |
|
833 | if (typeof schema === 'string') {
|
834 | collection = schema;
|
835 | schema = false;
|
836 | }
|
837 |
|
838 | if (utils.isObject(schema) && !schema.instanceOfSchema) {
|
839 | schema = new Schema(schema);
|
840 | }
|
841 | if (schema && !schema.instanceOfSchema) {
|
842 | throw new Error('The 2nd parameter to `mongoose.model()` should be a ' +
|
843 | 'schema or a POJO');
|
844 | }
|
845 |
|
846 | if (this.models[name] && !collection) {
|
847 |
|
848 | if (schema && schema.instanceOfSchema && schema !== this.models[name].schema) {
|
849 | throw new MongooseError.OverwriteModelError(name);
|
850 | }
|
851 | return this.models[name];
|
852 | }
|
853 |
|
854 | const opts = {cache: false, connection: this};
|
855 | let model;
|
856 |
|
857 | if (schema && schema.instanceOfSchema) {
|
858 | applyPlugins(schema, this.plugins, null, '$connectionPluginsApplied');
|
859 |
|
860 |
|
861 | model = this.base.model(fn || name, schema, collection, opts);
|
862 |
|
863 |
|
864 |
|
865 | if (!this.models[name]) {
|
866 | this.models[name] = model;
|
867 | }
|
868 |
|
869 |
|
870 | model.init(function $modelInitNoop() {});
|
871 |
|
872 | return model;
|
873 | }
|
874 |
|
875 | if (this.models[name] && collection) {
|
876 |
|
877 | model = this.models[name];
|
878 | schema = model.prototype.schema;
|
879 | const sub = model.__subclass(this, schema, collection);
|
880 |
|
881 | return sub;
|
882 | }
|
883 |
|
884 |
|
885 | model = this.base.models[name];
|
886 |
|
887 | if (!model) {
|
888 | throw new MongooseError.MissingSchemaError(name);
|
889 | }
|
890 |
|
891 | if (this === model.prototype.db
|
892 | && (!collection || collection === model.collection.name)) {
|
893 |
|
894 |
|
895 |
|
896 |
|
897 | if (!this.models[name]) {
|
898 | this.models[name] = model;
|
899 | }
|
900 |
|
901 | return model;
|
902 | }
|
903 | this.models[name] = model.__subclass(this, schema, collection);
|
904 | return this.models[name];
|
905 | };
|
906 |
|
907 |
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 |
|
924 |
|
925 |
|
926 |
|
927 |
|
928 |
|
929 | Connection.prototype.deleteModel = function(name) {
|
930 | if (typeof name === 'string') {
|
931 | const model = this.model(name);
|
932 | if (model == null) {
|
933 | return this;
|
934 | }
|
935 | delete this.models[name];
|
936 | delete this.collections[model.collection.name];
|
937 | delete this.base.modelSchemas[name];
|
938 | } else if (name instanceof RegExp) {
|
939 | const pattern = name;
|
940 | const names = this.modelNames();
|
941 | for (const name of names) {
|
942 | if (pattern.test(name)) {
|
943 | this.deleteModel(name);
|
944 | }
|
945 | }
|
946 | } else {
|
947 | throw new Error('First parameter to `deleteModel()` must be a string ' +
|
948 | 'or regexp, got "' + name + '"');
|
949 | }
|
950 |
|
951 | return this;
|
952 | };
|
953 |
|
954 |
|
955 |
|
956 |
|
957 |
|
958 |
|
959 |
|
960 | Connection.prototype.modelNames = function() {
|
961 | return Object.keys(this.models);
|
962 | };
|
963 |
|
964 |
|
965 |
|
966 |
|
967 |
|
968 |
|
969 |
|
970 |
|
971 | Connection.prototype.shouldAuthenticate = function() {
|
972 | return this.user != null &&
|
973 | (this.pass != null || this.authMechanismDoesNotRequirePassword());
|
974 | };
|
975 |
|
976 |
|
977 |
|
978 |
|
979 |
|
980 |
|
981 |
|
982 |
|
983 | Connection.prototype.authMechanismDoesNotRequirePassword = function() {
|
984 | if (this.options && this.options.auth) {
|
985 | return noPasswordAuthMechanisms.indexOf(this.options.auth.authMechanism) >= 0;
|
986 | }
|
987 | return true;
|
988 | };
|
989 |
|
990 |
|
991 |
|
992 |
|
993 |
|
994 |
|
995 |
|
996 |
|
997 |
|
998 |
|
999 |
|
1000 | Connection.prototype.optionsProvideAuthenticationData = function(options) {
|
1001 | return (options) &&
|
1002 | (options.user) &&
|
1003 | ((options.pass) || this.authMechanismDoesNotRequirePassword());
|
1004 | };
|
1005 |
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 |
|
1016 |
|
1017 |
|
1018 |
|
1019 |
|
1020 |
|
1021 |
|
1022 | Connection.STATES = STATES;
|
1023 | module.exports = Connection;
|