neosqlite.binary module

class neosqlite.binary.Binary(data: bytes | bytearray | memoryview, subtype: int = 0)[source]

Bases: bytes

A BSON Binary-like class for representing binary data in neosqlite.

This class provides a PyMongo-compatible interface for storing binary data directly in documents (outside of GridFS). The data is automatically encoded/decoded when stored in SQLite JSON documents.

Example usage:

# Create binary data binary_data = Binary(b”some binary content”)

# Store in document collection.insert_one({“data”: binary_data})

# Retrieve and use doc = collection.find_one({}) retrieved_data = doc[“data”] # Returns Binary instance raw_bytes = bytes(retrieved_data) # Convert to bytes if needed

_subtype: int
BINARY_SUBTYPE = 0
FUNCTION_SUBTYPE = 1
OLD_BINARY_SUBTYPE = 2
UUID_SUBTYPE = 4
MD5_SUBTYPE = 5
COLUMN_SUBTYPE = 7
SENSITIVE_SUBTYPE = 8
VECTOR_SUBTYPE = 9
USER_DEFINED_SUBTYPE = 128
property subtype: int

Get the binary subtype.

encode_for_storage() dict[source]

Encode the binary data for JSON storage.

Returns:

A dictionary representation for JSON storage

classmethod decode_from_storage(encoded_data: dict) Binary[source]

Decode binary data from JSON storage.

Parameters:

encoded_data – Dictionary representation from JSON storage

Returns:

A Binary instance

classmethod from_uuid(uuid_value, uuid_representation=None)[source]

Create a Binary instance from a UUID.

Parameters:
  • uuid_value – A UUID instance

  • uuid_representation – UUID representation (ignored in neosqlite)

Returns:

A Binary instance with UUID_SUBTYPE

as_uuid(uuid_representation=None)[source]

Convert this Binary instance to a UUID.

Returns:

A UUID instance

Raises:

ValueError – If the subtype is not UUID_SUBTYPE