Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Option to disable creation of initial connection #12965

Closed
2 tasks done
titanism opened this issue Jan 31, 2023 · 3 comments · Fixed by #13021
Closed
2 tasks done

Feature: Option to disable creation of initial connection #12965

titanism opened this issue Jan 31, 2023 · 3 comments · Fixed by #13021
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@titanism
Copy link

titanism commented Jan 31, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

If you manage multiple custom connections, a typical pattern is to simply create new connections with createConnection(uri, options). Doing so, it adds by default to the mongoose singleton of mongoose.connections, so you can iterate over mongoose.connections to see properties such as readyState.

The default connection however is always present. The only way to delete it (regardless of custom Mongoose instances) is to manually call mongoose.connection.destroy();. This isn't such a good pattern, as a better option might be new Mongoose({ createInitialConnection: false }); or something. Adding this feature would require some refactoring though.

@titanism titanism added enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class labels Jan 31, 2023
@titanism
Copy link
Author

This might not actually be too hard, but we could be wrong:

  // default global options
  this.options = Object.assign({
    pluralization: true,
    autoIndex: true,
-    autoCreate: true
+    autoCreate: true,
+    createInitialConnection: false
  }, options);
+
+  if (this.options.createInitialConnection) {
+    const conn = this.createConnection(); // default connection
+    conn.models = this.models;
+  }

-  const conn = this.createConnection(); // default connection
-  conn.models = this.models;

@titanism
Copy link
Author

There might also be another bug here we discovered, whereas id is not unique.

if (typeof base === 'undefined' || !base.connections.length) {
this.id = 0;
} else {
this.id = base.connections.length;
}

If you create multiple connections, then call .destroy() on them, and then create more connections, multiple of the connections will have the same id property because .length does not check for uniqueness.

cc @vkarpov15

@titanism
Copy link
Author

Confirmed this is a bug!

const mongoose = require('mongoose');
mongoose.createConnection()
mongoose.createConnection()
mongoose.connections[0].destroy()
mongoose.createConnection()
mongoose.createConnection()
mongoose.createConnection()
mongoose.connections.map(c => c.id) // outputs: [ 1, 2, 3, 3, 4 ]

You can see 3 appears twice.

@vkarpov15 vkarpov15 added this to the 6.9.2 milestone Feb 10, 2023
lpizzinidev added a commit to lpizzinidev/mongoose that referenced this issue Feb 11, 2023
vkarpov15 added a commit that referenced this issue Feb 13, 2023
feat(index): added createInitialConnection option to Mongoose constructor
@vkarpov15 vkarpov15 modified the milestones: 6.9.2, 6.9.4, 6.10.0 Feb 16, 2023
Say383 added a commit to Say383/cloud-code-samples that referenced this issue Mar 30, 2023
<p>This PR was automatically created by Snyk using the credentials of a
real user.</p><br /><h3>Snyk has created this PR to upgrade mongoose
from 6.8.0 to 6.10.2.</h3>

:information_source: Keep your dependencies up-to-date. This makes it
easier to fix existing vulnerabilities and to more quickly identify and
fix newly disclosed vulnerabilities when they affect your project.
<hr/>

- The recommended version is **11 versions** ahead of your current
version.
- The recommended version was released **23 days ago**, on 2023-03-07.


<details>
<summary><b>Release notes</b></summary>
<br/>
  <details>
    <summary>Package name: <b>mongoose</b></summary>
    <ul>
      <li>
<b>6.10.2</b> - <a
href="https://snyk.io/redirect/github/Automattic/mongoose/releases/tag/6.10.2">2023-03-07</a></br><p>chore:
release 6.10.2</p>
      </li>
      <li>
<b>6.10.1</b> - <a
href="https://snyk.io/redirect/github/Automattic/mongoose/releases/tag/6.10.1">2023-03-03</a></br><h1>6.10.1
/ 2023-03-03</h1>
<ul>
<li>fix: avoid removing empty query filters in <code>$and</code> and
<code>$or</code> <a class="issue-link js-issue-link"
data-error-text="Failed to load title" data-id="1600029622"
data-permission-text="Title is private"
data-url="Automattic/mongoose#13086"
data-hovercard-type="issue"
data-hovercard-url="/Automattic/mongoose/issues/13086/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/issues/13086">#13086</a>
<a class="issue-link js-issue-link" data-error-text="Failed to load
title" data-id="1527162711" data-permission-text="Title is private"
data-url="Automattic/mongoose#12898"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/12898/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/12898">#12898</a></li>
<li>fix(schematype): fixed validation for required UUID field <a
class="issue-link js-issue-link" data-error-text="Failed to load title"
data-id="1580768824" data-permission-text="Title is private"
data-url="Automattic/mongoose#13018"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13018/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13018">#13018</a>
<a
href="https://snyk.io/redirect/github/lpizzinidev">lpizzinidev</a></li>
<li>fix(types): add missing Paths generic param to
<code>Model.populate()</code> <a class="issue-link js-issue-link"
data-error-text="Failed to load title" data-id="1594857812"
data-permission-text="Title is private"
data-url="Automattic/mongoose#13070"
data-hovercard-type="issue"
data-hovercard-url="/Automattic/mongoose/issues/13070/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/issues/13070">#13070</a></li>
<li>docs(migrating_to_6): added info about removal of reconnectTries and
reconnectInterval options <a class="issue-link js-issue-link"
data-error-text="Failed to load title" data-id="1598402460"
data-permission-text="Title is private"
data-url="Automattic/mongoose#13083"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13083/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13083">#13083</a>
<a
href="https://snyk.io/redirect/github/lpizzinidev">lpizzinidev</a></li>
<li>docs: fix code in headers for migrating_to_5 <a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="1596634502" data-permission-text="Title is private"
data-url="Automattic/mongoose#13077"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13077/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13077">#13077</a>
<a href="https://snyk.io/redirect/github/hasezoey">hasezoey</a></li>
<li>docs: backport misc documentation changes into 6.x <a
class="issue-link js-issue-link" data-error-text="Failed to load title"
data-id="1600929090" data-permission-text="Title is private"
data-url="Automattic/mongoose#13091"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13091/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13091">#13091</a>
<a href="https://snyk.io/redirect/github/hasezoey">hasezoey</a></li>
</ul>
      </li>
      <li>
<b>6.10.0</b> - <a
href="https://snyk.io/redirect/github/Automattic/mongoose/releases/tag/6.10.0">2023-02-22</a></br><h1>6.10.0
/ 2023-02-22</h1>
<ul>
<li>feat: upgrade to mongodb driver 4.14.0 <a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="1585923903" data-permission-text="Title is private"
data-url="Automattic/mongoose#13036"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13036/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13036">#13036</a></li>
<li>feat: added Schema.prototype.omit() function <a class="issue-link
js-issue-link" data-error-text="Failed to load title"
data-id="1554538683" data-permission-text="Title is private"
data-url="Automattic/mongoose#12939"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/12939/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/12939">#12939</a>
<a class="issue-link js-issue-link" data-error-text="Failed to load
title" data-id="1552196285" data-permission-text="Title is private"
data-url="Automattic/mongoose#12931"
data-hovercard-type="issue"
data-hovercard-url="/Automattic/mongoose/issues/12931/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/issues/12931">#12931</a>
<a
href="https://snyk.io/redirect/github/lpizzinidev">lpizzinidev</a></li>
<li>feat(index): added createInitialConnection option to Mongoose
constructor <a class="issue-link js-issue-link" data-error-text="Failed
to load title" data-id="1580784101" data-permission-text="Title is
private" data-url="Automattic/mongoose#13021"
data-hovercard-type="pull_request"
data-hovercard-url="/Automattic/mongoose/pull/13021/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/pull/13021">#13021</a>
<a class="issue-link js-issue-link" data-error-text="Failed to load
title" data-id="1564821311" data-permission-text="Title is private"
data-url="Automattic/mongoose#12965"
data-hovercard-type="issue"
data-hovercard-url="/Automattic/mongoose/issues/12965/hovercard"
href="https://snyk.io/redirect/github/Automattic/mongoose/issues/12965">#12965</a>
<a
href="https://snyk.io/redirect/github/lpizzinidev">lpizzinidev</a></li>
</ul>
      </li>
      <li>
        <b>6.9.3</b> - 2023-02-22
      </li>
      <li>
        <b>6.9.2</b> - 2023-02-16
      </li>
      <li>
        <b>6.9.1</b> - 2023-02-06
      </li>
      <li>
        <b>6.9.0</b> - 2023-01-25
      </li>
      <li>
        <b>6.8.4</b> - 2023-01-17
      </li>
      <li>
        <b>6.8.3</b> - 2023-01-06
      </li>
      <li>
        <b>6.8.2</b> - 2022-12-28
      </li>
      <li>
        <b>6.8.1</b> - 2022-12-19
      </li>
      <li>
        <b>6.8.0</b> - 2022-12-05
      </li>
    </ul>
from <a
href="https://snyk.io/redirect/github/Automattic/mongoose/releases">mongoose
GitHub release notes</a>
  </details>
</details>


<details>
  <summary><b>Commit messages</b></summary>
  </br>
  <details>
    <summary>Package name: <b>mongoose</b></summary>
    <ul>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/d1d09aba302559a10675c4a4bf5f1836155ddabb">d1d09ab</a>
chore: release 6.10.2</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/87592672b7cd33510124d3bcc4c88a48b3419934">8759267</a>
docs: quick improvement to &#x60;Model.init()&#x60; docs re: #13054</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/b8ed1d987093db42fe5f5d4327f690b4804ad9ac">b8ed1d9</a>
Merge pull request #13135 from Automattic/vkarpov15/gh-13003</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/978b49d2e63abb580d01169fe5bbee0688e112c3">978b49d</a>
test: make #13052 test more explicit with debug usage</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/c45f67d42048b723a89ae4cacb57c419ce4ca6e3">c45f67d</a>
test: make #13052 test consistent with others</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/5af7c8c7ce4040fea42a546a1a2cfaae3d5dfdd9">5af7c8c</a>
test: add test coverage for #13043 to show that issue is fixed by #13003
fix</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/da04458f9ed60bfa5c563dfd57150067275e7096">da04458</a>
fix(document): avoid setting array default if document array projected
out by sibling projection</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/a706bc33f78bd0a8f480e017a6987dc9dddca45c">a706bc3</a>
Merge pull request #13133 from Automattic/vkarpov15/doc-array-path</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/060c88965ebf3f6240f7794545db42bd0263c96a">060c889</a>
fix(documentarray): set correct document array path if making map of
document arrays</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/a9a838cbe153dea48fb47dbb38d5c01afba208e4">a9a838c</a>
Merge pull request #13124 from lorand-horvath/patch-9</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/7764bf7a0f0d9f24a47b0ca1d78a61766ae7a36f">7764bf7</a>
6.x should still support node 12</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/f72c26d833a3b8a4b96403d77129d1c594b8f482">f72c26d</a>
chore: release 6.10.1</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/57ecacf54d74fac1e4b8b91e92e9593235d915e0">57ecacf</a>
chore: add npm script to release 6.x</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/416784e8aab5a54da3ffce9c7d13e9d60e95b29b">416784e</a>
Merge pull request #13117 from
Automattic/IslandRhythms/paths-generic</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/66d02350553a754122832e8ee903556e1c5f7398">66d0235</a>
fix tests, fix #13070</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/b7417ba3389792b7b2ccaa89aa22274890693e60">b7417ba</a>
Update populate.test.ts</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/fd367cc11fb20a6ad2563b25bdcd9ecfa05845fb">fd367cc</a>
fix: lint</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/34a55882ef8755ec0ccefdc04abbab0eeed4a8e9">34a5588</a>
Add Path genereic</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/7d2c511475fb4a36d8ffcdd30739af45cdde4d4e">7d2c511</a>
quick fix for search generation script</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/e4f1e20880e086c5c57e8a7261b8f92b618196c7">e4f1e20</a>
chore: undo accidental package.json changes</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/e6e37cbecc631d59a2d7bc86c59ecf895377aa38">e6e37cb</a>
chore: quick script fix</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/f22be739d328c63454cec09adca728abd4b9eb7a">f22be73</a>
chore: quick script fix</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/d80e6cac04519f3ccc4742d9eb713b58ccfcb777">d80e6ca</a>
chore: quick script fix</li>
<li><a
href="https://snyk.io/redirect/github/Automattic/mongoose/commit/99238c2019267b167b92c5d420b174df4aa76be4">99238c2</a>
chore: quick script fix</li>
    </ul>

<a
href="https://snyk.io/redirect/github/Automattic/mongoose/compare/82943da92ba6db7fc27846a63a5a46cd7df049a9...d1d09aba302559a10675c4a4bf5f1836155ddabb">Compare</a>
  </details>
</details>
<hr/>

**Note:** *You are seeing this because you or someone else with access
to this repository has authorized Snyk to open upgrade PRs.*

For more information: <img
src="https://api.segment.io/v1/pixel/track?data=eyJ3cml0ZUtleSI6InJyWmxZcEdHY2RyTHZsb0lYd0dUcVg4WkFRTnNCOUEwIiwiYW5vbnltb3VzSWQiOiJiNGU3YzFlNC0zMzE2LTQ3ZmItYjczMy1jYTBjNTM4M2NlYzkiLCJldmVudCI6IlBSIHZpZXdlZCIsInByb3BlcnRpZXMiOnsicHJJZCI6ImI0ZTdjMWU0LTMzMTYtNDdmYi1iNzMzLWNhMGM1MzgzY2VjOSJ9fQ=="
width="0" height="0"/>

🧐 [View latest project
report](https://app.snyk.io/org/say383/project/063a08f4-06ac-4efa-a1ca-f6fa23cf21b4?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🛠 [Adjust upgrade PR
settings](https://app.snyk.io/org/say383/project/063a08f4-06ac-4efa-a1ca-f6fa23cf21b4/settings/integration?utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr)

🔕 [Ignore this dependency or unsubscribe from future upgrade
PRs](https://app.snyk.io/org/say383/project/063a08f4-06ac-4efa-a1ca-f6fa23cf21b4/settings/integration?pkg&#x3D;mongoose&amp;utm_source&#x3D;github&amp;utm_medium&#x3D;referral&amp;page&#x3D;upgrade-pr#auto-dep-upgrades)

<!---
(snyk:metadata:{"prId":"b4e7c1e4-3316-47fb-b733-ca0c5383cec9","prPublicId":"b4e7c1e4-3316-47fb-b733-ca0c5383cec9","dependencies":[{"name":"mongoose","from":"6.8.0","to":"6.10.2"}],"packageManager":"npm","type":"auto","projectUrl":"https://app.snyk.io/org/say383/project/063a08f4-06ac-4efa-a1ca-f6fa23cf21b4?utm_source=github&utm_medium=referral&page=upgrade-pr","projectPublicId":"063a08f4-06ac-4efa-a1ca-f6fa23cf21b4","env":"prod","prType":"upgrade","vulns":[],"issuesToFix":[],"upgrade":[],"upgradeInfo":{"versionsDiff":11,"publishedDate":"2023-03-07T20:47:44.757Z"},"templateVariants":[],"hasFixes":false,"isMajorUpgrade":false,"isBreakingChange":false,"priorityScoreList":[]})
--->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants