-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Conversation
|
||
'mongodb' => [ | ||
'driver' => 'mongodb', | ||
'dsn' => env('DB_URI', '<connection string>'), |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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,
],
There was a problem hiding this comment.
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)).
: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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
laravel-mongodb/src/Schema/Builder.php
Lines 87 to 92 in 95c5b4f
public function dropIfExists($table) | |
{ | |
if ($this->hasCollection($table)) { | |
$this->drop($table); | |
} | |
} |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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',
]
], ...
There was a problem hiding this 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.
To drop a collection idiomatically, the Laravel ``Schema`` facade provides the | ||
``drop()`` and ``dropIfExists()`` methods. The following example |
There was a problem hiding this comment.
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."
There was a problem hiding this 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.
You specify a database name when configuring a connection in your | ||
application's ``config/database.php`` file. The ``connections`` property |
There was a problem hiding this comment.
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. |
There was a problem hiding this comment.
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.
``$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 |
There was a problem hiding this comment.
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:
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(correcting my prior suggestion)
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 |
model, you can access a collection by using a facade to access the query | ||
builder class ``DB`` and calling the ``collection()`` method. The |
There was a problem hiding this comment.
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.
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 |
There was a problem hiding this comment.
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":
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. |
There was a problem hiding this comment.
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.
* DOCSP-35934: db and coll * add sections * small fixes * CC and DBX feedback * tech review fixes and CC suggestions * small fixes
JIRA
Staging
docs PR
Checklist