6767from pymongo .asynchronous .pool import AsyncBaseConnection
6868from pymongo .common import CONNECT_TIMEOUT
6969from pymongo .daemon import _spawn_daemon
70- from pymongo .encryption_options import AutoEncryptionOpts , RangeOpts
70+ from pymongo .encryption_options import AutoEncryptionOpts , RangeOpts , TextOpts
7171from pymongo .errors import (
7272 ConfigurationError ,
7373 EncryptedCollectionError ,
@@ -516,6 +516,11 @@ class Algorithm(str, enum.Enum):
516516
517517 .. versionadded:: 4.4
518518 """
519+ TEXTPREVIEW = "TextPreview"
520+ """**BETA** - TextPreview.
521+
522+ .. versionadded:: 4.15
523+ """
519524
520525
521526class QueryType (str , enum .Enum ):
@@ -541,6 +546,24 @@ class QueryType(str, enum.Enum):
541546 .. versionadded:: 4.4
542547 """
543548
549+ PREFIXPREVIEW = "prefixPreview"
550+ """**BETA** - Used to encrypt a value for a prefixPreview query.
551+
552+ .. versionadded:: 4.15
553+ """
554+
555+ SUFFIXPREVIEW = "suffixPreview"
556+ """**BETA** - Used to encrypt a value for a suffixPreview query.
557+
558+ .. versionadded:: 4.15
559+ """
560+
561+ SUBSTRINGPREVIEW = "substringPreview"
562+ """**BETA** - Used to encrypt a value for a substringPreview query.
563+
564+ .. versionadded:: 4.15
565+ """
566+
544567
545568def _create_mongocrypt_options (** kwargs : Any ) -> MongoCryptOptions :
546569 # For compat with pymongocrypt <1.13, avoid setting the default key_expiration_ms.
@@ -876,6 +899,7 @@ async def _encrypt_helper(
876899 contention_factor : Optional [int ] = None ,
877900 range_opts : Optional [RangeOpts ] = None ,
878901 is_expression : bool = False ,
902+ text_opts : Optional [TextOpts ] = None ,
879903 ) -> Any :
880904 self ._check_closed ()
881905 if isinstance (key_id , uuid .UUID ):
@@ -895,6 +919,12 @@ async def _encrypt_helper(
895919 range_opts .document ,
896920 codec_options = self ._codec_options ,
897921 )
922+ text_opts_bytes = None
923+ if text_opts :
924+ text_opts_bytes = encode (
925+ text_opts .document ,
926+ codec_options = self ._codec_options ,
927+ )
898928 with _wrap_encryption_errors ():
899929 encrypted_doc = await self ._encryption .encrypt (
900930 value = doc ,
@@ -905,6 +935,7 @@ async def _encrypt_helper(
905935 contention_factor = contention_factor ,
906936 range_opts = range_opts_bytes ,
907937 is_expression = is_expression ,
938+ text_opts = text_opts_bytes ,
908939 )
909940 return decode (encrypted_doc )["v" ]
910941
@@ -917,6 +948,7 @@ async def encrypt(
917948 query_type : Optional [str ] = None ,
918949 contention_factor : Optional [int ] = None ,
919950 range_opts : Optional [RangeOpts ] = None ,
951+ text_opts : Optional [TextOpts ] = None ,
920952 ) -> Binary :
921953 """Encrypt a BSON value with a given key and algorithm.
922954
@@ -937,9 +969,14 @@ async def encrypt(
937969 used.
938970 :param range_opts: Index options for `range` queries. See
939971 :class:`RangeOpts` for some valid options.
972+ :param text_opts: Index options for `textPreview` queries. See
973+ :class:`TextOpts` for some valid options.
940974
941975 :return: The encrypted value, a :class:`~bson.binary.Binary` with subtype 6.
942976
977+ .. versionchanged:: 4.9
978+ Added the `text_opts` parameter.
979+
943980 .. versionchanged:: 4.9
944981 Added the `range_opts` parameter.
945982
@@ -960,6 +997,7 @@ async def encrypt(
960997 contention_factor = contention_factor ,
961998 range_opts = range_opts ,
962999 is_expression = False ,
1000+ text_opts = text_opts ,
9631001 ),
9641002 )
9651003
0 commit comments