Skip to content

Commit 0f05681

Browse files
committed
Improve architecture document
1 parent f2eef78 commit 0f05681

File tree

1 file changed

+35
-27
lines changed

1 file changed

+35
-27
lines changed

src/Codec/architecture.md

Lines changed: 35 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
# Converting BSON data through codecs
22

3-
The codec system is a more advanced way to convert BSON data to native types and back, designed for libraries with more
4-
advanced use cases, e.g. object mappers. It is designed to decouple the serialisation logic from the data model,
5-
allowing for more flexible implementations.
3+
Codecs provide a more flexible way to convert BSON data to native types and back and address most of the shortcomings of
4+
the previous type map system. The codec system is designed to be used by libraries that need to convert BSON data into
5+
native types, for example object mappers. Unlike the type map system, codecs allow converting any BSON type to a native
6+
type directly when reading data from the database. Together with lazy decoding of BSON structures, this allows
7+
for a more flexible and efficient way to handle BSON data.
68

79
## Encoders and Decoders
810

9-
The codec interface is split into two smaller interfaces: encoders and decoders. Both interfaces are marked as internal,
10-
as users are only expected to interact with the Codec interface. The interfaces are typed using Psalm generics, allowing
11-
for better type checking when they are used. Without type annotations, the interfaces are equivalent to the following:
11+
The codec interface is comprised by two smaller interfaces: encoders and decoders. Both interfaces are marked as
12+
internal, as users are only expected to interact with the Codec interface. The interfaces are typed as generics through
13+
`@template` annotations, allowing for better type checking when they are used. Without type annotations, the interfaces
14+
are equivalent to the following:
1215

1316
```php
1417
namespace MongoDB\Codec;
@@ -32,7 +35,7 @@ interface Encoder
3235
}
3336
```
3437

35-
## Codec interface
38+
## Codec Interface
3639

3740
The `Codec` interface combines decoding and encoding into a single interface. This will be used for most values except
3841
for documents where a more specific `DocumentCodec` is provided.
@@ -44,23 +47,16 @@ encode or decode a value only if it is supported. If it is not supported, the or
4447
```php
4548
namespace MongoDB\Codec;
4649

47-
/**
48-
* @psalm-template BSONType
49-
* @psalm-template NativeType
50-
* @template-extends Decoder<BSONType, NativeType>
51-
* @template-extends Encoder<BSONType, NativeType>
52-
*/
5350
interface Codec extends Decoder, Encoder
5451
{
5552
}
5653
```
5754

58-
## Document codec
55+
## Document Codec
5956

6057
The document codec is special as it is guaranteed to always encode to a BSON document instance and decode to a PHP
61-
object. Document codecs can be provided to a `MongoDB\Collection` instance to have it automatically decode data read
62-
from the database. Likewise, any supported value is encoded before writing to the database in `insert` and `replace`
63-
operations.
58+
object. Document codecs will be used by `MongoDB\Collection` instances to automatically decode BSON data into PHP
59+
objects when reading data, and to encode PHP objects when inserting or replacing data.
6460

6561
```php
6662
namespace MongoDB\Codec;
@@ -76,11 +72,29 @@ interface DocumentCodec extends Codec
7672
}
7773
```
7874

79-
## Using codecs
75+
## Built-in codecs
76+
77+
By default, two codecs are provided: an `ArrayCodec` and an `ObjectCodec`. These two codecs are used to recursively
78+
encode and decode values in arrays and `stdClass` instances, respectively. When encoding or decoding an object,
79+
`ObjectCodec` only handles public properties of the object and ignores private and protected properties.
80+
81+
## Future Work
82+
83+
### Using Codecs
84+
85+
The `MongoDB\Collection` class and all operations that work with documents now take a `codec` option. This option is
86+
passed along to the various operations that already take a `typeMap` option. Collections only support a `DocumentCodec`
87+
instance to guarantee that data always encodes to a BSON document and decodes to a PHP object.
88+
89+
All operations that return documents will use the codec to decode the documents into PHP objects. This includes
90+
the various `find` and `findAndModify` operations in collections as well as the `aggregate` and `watch` operations in
91+
collections, databases, and the client object itself.
92+
93+
When writing data, any operation that takes an entire document will use the codec to automatically encode the document.
94+
This is limited to `insertOne`, `insertMany`, `replaceOne`, and `findOneAndReplace` operations in collections. `update`
95+
operations will not use the codec to encode documents, as they only support update operators and can't work with the
96+
entire document.
8097

81-
The `MongoDB\Collection` class and all operations that work with documents now take a `codec` option. This can be
82-
an instance of a `DocumentCodec` that will be used to encode documents (for insert and replace operations) and decode
83-
them into PHP objects when reading data.
8498

8599
### Codecs and type maps
86100

@@ -96,9 +110,3 @@ this case, the type map is used. The precedence order is as follows:
96110
Codecs are not inherited from the client or the database object, as they are purely used for operations that return
97111
documents. However, database- or client-level aggregation commands will take an operation-level codec option to
98112
decode the resulting documents.
99-
100-
## Built-in codecs
101-
102-
By default, two codecs are provided: an `ArrayCodec` and an `ObjectCodec`. These two codecs are used to recursively
103-
encode and decode values in arrays and `stdClass` instances, respectively. `ObjectCodec` only handles public properties
104-
in objects and ignores other properties.

0 commit comments

Comments
 (0)