Skip to content

DOCSP-41962 - Stable API #117

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
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion snooty.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ php-library = "MongoDB PHP Library"

php-library = "MongoDB PHP Library"
driver-short = "PHP library"
extension-short = "PHP extension"
mdb-server = "MongoDB Server"
driver-short = "PHP library"
stable-api = "Stable API"
api = "https://www.mongodb.com/docs/php-library/current/reference"
117 changes: 117 additions & 0 deletions source/connect/stable-api.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
.. _php-stable-api:

==============
{+stable-api+}
==============

.. contents:: On this page
:local:
:backlinks: none
:depth: 2
:class: singlecol

.. facet::
:name: genre
:values: reference

.. meta::
:keywords: compatible, backwards, upgrade

.. note::

The {+stable-api+} feature requires {+mdb-server+} 5.0 or later.

Overview
--------

In this guide, you can learn how to specify **{+stable-api+}** compatibility when
connecting to a MongoDB deployment.

The {+stable-api+} feature forces the server to run operations with behaviors compatible
with the API version you specify. When you update either your driver or server,
the API version changes, which can change the way these operations behave.
Using the {+stable-api+} ensures consistent responses from the server and
provides long-term API stability for your application.

The following sections describe how you can enable and customize {+stable-api+} for
your MongoDB client. For more information about the {+stable-api+}, including a list of
the commands it supports, see :manual:`Stable API </reference/stable-api/>` in the
{+mdb-server+} manual.

Enable the {+stable-api+}
-------------------------

To enable the {+stable-api+}, perform the following steps:

1. Construct a ``MongoDB\Driver\ServerApi`` object and pass the {+stable-api+}
version you want to use. Currently, the driver supports only version 1.
#. Construct a ``MongoDB\Client`` object. For the ``driverOptions`` parameter, pass an
array that contains the ``serverApi`` connection option. Set this option to the
``MongoDB\Driver\ServerApi`` object you created in the previous step.

The following code example shows how to specify {+stable-api+} version 1:

.. literalinclude:: /includes/connect/stable-api.php
:language: php
:copyable: true
:start-after: // start-specify-v1
:end-before: // end-specify-v1
:emphasize-lines: 5-7

.. note::

After you create a ``MongoDB\Client`` instance with
a specified API version, all commands you run with the client use the specified
version. If you need to run commands using more than one version of the

Check failure on line 65 in source/connect/stable-api.txt

View workflow job for this annotation

GitHub Actions / TDBX Vale rules

[vale] reported by reviewdog 🐶 [MongoDB.ConciseTerms] 'must' is preferred over 'need to'. Raw Output: {"message": "[MongoDB.ConciseTerms] 'must' is preferred over 'need to'.", "location": {"path": "source/connect/stable-api.txt", "range": {"start": {"line": 65, "column": 20}}}, "severity": "ERROR"}
{+stable-api+}, create a new ``MongoDB\Client`` instance.

.. _stable-api-options:

Configure the {+stable-api+}
------------------------

The following table describes the parameters of the ``MongoDB\Driver\ServerApi`` constructor.
You can use these parameters to customize the behavior of the {+stable-api+}.

.. list-table::
:header-rows: 1
:stub-columns: 1
:widths: 25,75

* - Option Name
- Description

* - strict
- | **Optional**. When ``true``, if you call a command that isn't part of
the declared API version, the library raises an exception.
|
| Default: ``false``

* - deprecationErrors
- | **Optional**. When ``true``, if you call a command that is deprecated in the
declared API version, the library raises an exception.
|
| Default: ``false``

The following code example shows how you can use these parameters when constructing a
``MongoDB\Driver\ServerApi`` object:

.. literalinclude:: /includes/connect/stable-api.php
:language: php
:copyable: true
:start-after: // start-stable-api-options
:end-before: // end-stable-api-options
:emphasize-lines: 5-7

API Documentation
-----------------

For more information about the ``MongoDB\Client`` class, see the following {+driver-short+}
API documentation:

- :ref:`MongoDB\Client <php-api-mongodbclient>`

For more information about the ``MongoDB\Driver\ServerApi`` class, see the following
{+extension-short+} API documentation:

- `MongoDB\\Driver\\ServerApi <https://www.php.net/manual/en/class.mongodb-driver-serverapi.php>`__
2 changes: 1 addition & 1 deletion source/connect/tls.txt
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,4 @@ API Documentation
To learn more about configuring TLS for the {+driver-short+},
see the following API documentation:

- :ref:`MongoDB\Client <php-mongodb-client>`
- :ref:`MongoDB\Client <php-api-mongodbclient>`
23 changes: 23 additions & 0 deletions source/includes/connect/stable-api.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// start-specify-v1
<?php

$uri = "mongodb://<hostname>:<port>";

$client = new MongoDB\Client($uri, [], [
'serverApi' => new MongoDB\Driver\ServerApi('1')
]);

?>
// end-specify-v1

// start-stable-api-options
<?php
Copy link
Member

Choose a reason for hiding this comment

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

I don't recall seeing multiple <?php tags in source files from @norareidy's previous PRs. This makes the file itself invalid, unless you intend to add a ?> above (probably between end-specify-v1 and start-stable-api-options since there's no reason to show ?> to a user (they won't use it in their files).

If these source files are ever meant to be executed for correctness (as we do for some existing sources in the PHPLIB docs before the docs team took over), that'd be even more reason to ensure the syntax within is valid.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

i wasn't sure about these. the existing, un-standardized docs (e.g., hhttps://www.mongodb.com/docs/php-library/current/tutorial/gridfs/#uploading-files-with-writable-streamsere) include them in all code samples, so i assumed it was the preferred syntax.

these files will never be executed in toto, so i'm just going to remove these tags.

Copy link
Member

@jmikola jmikola Sep 11, 2024

Choose a reason for hiding this comment

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

the existing, un-standardized docs include them in all code samples

I think that was necessary for syntax highlighting. Note that most of those code blocks are embedded in the RST and weren't being included from a common file (nor do they get automated testing). Speaking just of the GridFS examples, I'll also note that those are all full scripts that can be executed on their own (each constructs a Client).

I think it makes sense to leave <?php at the top of each of your source files. I expect it is also required for the CI workflow to check coding standards.


$uri = "mongodb://<hostname>:<port>";

$client = new MongoDB\Client($uri, [], [
'serverApi' => new MongoDB\Driver\ServerApi('1', true, true)
Copy link
Member

Choose a reason for hiding this comment

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

@alcaeus: I'm not sure what version of PHPLIB is being targeted here, but if we're bumping to PHP 8.1+ with version 1.20 I think this would be a good opportunity to use named arguments (available in PHP 8.0+) for the the booleans. Consider:

'serverApi' => new MongoDB\Driver\ServerApi('1', strict: true, deprecationErrors: true),

Ignore this suggestion if we need to remain compatible with earlier versions (back to PHP 7.4).

Also, my previous comment about a trailing comma after elements in multi-line arrays also applies here.

Copy link
Member

Choose a reason for hiding this comment

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

I think we can assume newer PHP versions, so using named arguments sounds like a great idea, especially for those boolean flags!

Copy link
Collaborator Author

@mongoKart mongoKart Sep 11, 2024

Choose a reason for hiding this comment

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

love it. helps a ton with readability.

FYI @rustagir and @norareidy

]);

?>
// end-stable-api-options
Loading