8000 DOCSP-35934: databases and collections by rustagir · Pull Request #2816 · mongodb/laravel-mongodb · GitHub
[go: up one dir, main page]

Skip to content

DOCSP-35934: databases and collections #2816

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

Merged
merged 8 commits into from
Apr 15, 2024

Conversation

rustagir
Copy link
Contributor
@rustagir rustagir commented Apr 2, 2024

JIRA
Staging
docs PR

Checklist

  • Add tests and ensure they pass
  • Add an entry to the CHANGELOG.md file
  • Update documentation for new features

@rustagir rustagir requested a review from a team as a code owner April 2, 2024 21:02
@rustagir rustagir requested a review from jmikola April 2, 2024 21:02
@GromNaN GromNaN requested review from GromNaN and removed request for jmikola April 3, 2024 10:10

'mongodb' => [
'driver' => 'mongodb',
'dsn' => env('DB_URI', '<connection string>'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The config/dabase.php file is committed in the git repository of each project. The MongoDB DSN usually contains credentials, it should be in the .env file.
The 2nd argument of env() is the default value. Typically mongodb://localhost:27017 for the MongoDB DSN.

The application can contain a .env.example but in local the developers must create the file .env. For production, there is many ways to set the environment variables, from the .env file to server or container config.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is content that will be better suited for the Connection Guide

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong. When a "dsn" is provided, the database name is read from the "dsn", and the "database" config is ignored.

Your example should look like this, so we don't bother with env vars.

       'mongodb' => [
           'driver' => 'mongodb',
           'dsn' => 'mongodb://localhost:27017/animals,
        ],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When a "dsn" is provided, the database name is read from the "dsn", and the "database" config is ignored.

Outside the scope of this PR, but is that something we should consider changing in some future release. The "auth database" component in the DSN should ideally never be specified since it was made obsolete by the authSource URI option (as I explained in symfony-cli/symfony-cli#458 (comment)).

@rustagir rustagir requested a review from a team as a code owner April 10, 2024 14:50
@rustagir rustagir requested a review from GromNaN April 10, 2024 15:11
:ref:`laravel-eloquent-migrations` section in the Schema Builder guide.

To drop a collection idiomatically, the Laravel ``Schema`` facade provides the
``drop()`` and ``dropIfExists()`` methods. The following example
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GromNaN: Since dropping a nonexistent collection is a NOP in PHP, is there any practical difference between these two methods? Does it make sense to talk about both of them given the driver behavior?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the doc should tell both methods have the same effect and they exist for compatibility with the SQL Eloquent.

We could remove the collection existence check from this method?

public function dropIfExists($table)
{
if ($this->hasCollection($table)) {
$this->drop($table);
}
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct. I see the Blueprint::drop() method calls Collection::drop(). That NOPs if the namespace doesn't exist.

The MongoDB PHP driver reuses the same connection when
you create two clients with the same connection string. There is no
overhead in using two connections for two distinct databases, so you
do not need to optimize your connections.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll note that if users expect to change the database name for separate connections, that would result in the driver not re-using the same connection since the connection string (i.e. dsn option) will technically differ.

This relates to my suggestion in https://github.com/mongodb/laravel-mongodb/pull/2816/files#r1559726362 to encourage users not to specify any "auth database" in the connection string, which the Laravel library just so happens to use as a "default" database.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the information! Since i modified the multiple connections example to specify the default db in the connection strings, should I remove this note then?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking @GromNaN above, but I very much dislike the example being presented here. If there's any way we can change this to avoid using the "auth database" URI component, I'd like to see it.

I can confirm that this paragraph is no longer relevant given the current state of the PR, but the fact that it's now creating multiple database connections is very disagreeable to me. This doesn't feel like something we should be conveying so simply in docs that some users are bound to copy/paste as reference.

Copy link
Member
@GromNaN GromNaN Apr 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The correct configuration should be to extract the database into a dedicated config:

   'connections' => [

       'mongodb' => [
           'driver' => 'mongodb',
           'dsn' => 'mongodb://localhost:27017/',
           'database' => 'animals',
       ],

       'mongodb_alt' => [
           'driver' => 'mongodb',
           'dsn' => 'mongodb://localhost:27017/',
           'database' => 'plants',
       ]

   ], ...

Copy link
Contributor
@ccho-mongodb ccho-mongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, the content looks good. As mentioned in my comments, I think there could be some structural changes that make the info more focused and easier to locate, but would be ok if you choose not to take them.
I'll also leave it up to you whether you want me to take another look.

Comment on lines 192 to 193
To drop a collection idiomatically, the Laravel ``Schema`` facade provides the
``drop()`` and ``dropIfExists()`` methods. The following example
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think this sentence structure could be simplified and made more concise. E.g.

"The Laravel Schema facade offers the drop() and dropIfExists() methods to drop a collection."

Or, if you end up suggesting just one, maybe something like the following:

"You can drop a collection by calling the "drop()" method of the Laravel Schema facade."

@rustagir rustagir requested a review from ccho-mongodb April 11, 2024 19:38
Copy link
Contributor
@ccho-mongodb ccho-mongodb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, left a couple small suggestions.

Comment on lines 40 to 41
You specify a database name when configuring a connection in your
application's ``config/database.php`` file. The ``connections`` property
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think this sentence specifies a time ("when"), but I don't think it's connected to any sequence of instructions we provide. I think the following sentence could avoid associating it with an order:

You can configure the name of the database that a connection uses in your application's config/database.php configuration file.


If your application contains multiple database connections and you want
to store your model in a database other than the default, override the
``$connection`` property when creating a ``Model`` class.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think it's only necessary to mention where to make the change.

Suggested change
``$connection`` property when creating a ``Model`` class.
``$connection`` property in your ``Model`` class.


When you create model class that extends
``MongoDB\Laravel\Eloquent\Model``, {+odm-short+} stores the model data
in a collection with a name that is the snake case plural form of your
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

No change necessary, but I just thought this wording might flow better:

Suggested change
in a collection with a name that is the snake case plural form of your
in a collection with a name formatted as the snake case plural form of your


Flower::where('name', 'Water Lily')->get()

If you are not able to accomplish your operation by using an Eloquent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(correcting my prior suggestion)

Suggested change
If you are not able to accomplish your operation by using an Eloquent
If you are unable to accomplish your operation by using an Eloquent

Comment on lines 146 to 147
model, you can access a collection by using a facade to access the query
builder class ``DB`` and calling the ``collection()`` method. The
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:

I think it can be more concise to use the "DB facade" nomenclature which the Laravel docs use.

Suggested change
model, you can access a collection by using a facade to access the query
builder class ``DB`` and calling the ``collection()`` method. The
model, you can access the query builder by calling the ``collection()`` method on the ``DB`` facade. The

Model Class guide.

We generally recommend that you use the Eloquent ORM to access a collection
implicitly for code readability and maintainability. The following
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion:
I think there's only one way to reference collections by using Eloquent ORM (through the models), so it could be confusing to include "implicitly":

Suggested change
implicitly for code readability and maintainability. The following
for code readability and maintainability. The following

The MongoDB PHP driver reuses the same connection when
you create two clients with the same connection string. There is no
overhead in using two connections for two distinct databases, so you
do not need to optimize your connections.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm asking @GromNaN above, but I very much dislike the example being presented here. If there's any way we can change this to avoid using the "auth database" URI component, I'd like to see it.

I can confirm that this paragraph is no longer relevant given the current state of the PR, but the fact that it's now creating multiple database connections is very disagreeable to me. This doesn't feel like something we should be conveying so simply in docs that some users are bound to copy/paste as reference.

@rustagir rustagir requested a review from GromNaN April 15, 2024 20:17
@rustagir rustagir merged commit 93727cd into mongodb:4.1 Apr 15, 2024
15 checks passed
This was referenced Apr 15, 2024
ccho-mongodb pushed a commit to ccho-mongodb/laravel-mongodb that referenced this pull request Apr 15, 2024
* DOCSP-35934: db and coll

* add sections

* small fixes

* CC and DBX feedback

* tech review fixes and CC suggestions

* small fixes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0