neosqlite.collection.type_correction module

Type correction utilities for NeoSQLite to handle automatic conversion between integer IDs and ObjectIds in queries.

neosqlite.collection.type_correction.normalize_id_query(query: dict[str, Any]) dict[str, Any][source]

Public function to normalize ID types in a query.

This function is provided for backward compatibility. The actual normalization logic is implemented in QueryHelper._normalize_id_query method to avoid code duplication. This function is not actively used but kept for API compatibility.

Parameters:

query – The query dictionary to normalize

Returns:

A normalized query dictionary with corrected ID types

neosqlite.collection.type_correction._try_convert_to_int(value: str) int | str[source]

Try to convert a string to int, return original if fails.

neosqlite.collection.type_correction._is_valid_objectid_hex(value: str) bool[source]

Check if string is a valid ObjectId hex string (24 chars, valid hex).

neosqlite.collection.type_correction._convert_list_item(item: Any) Any[source]

Convert a single list item - ObjectIds to strings, recurse on dicts.

neosqlite.collection.type_correction.normalize_objectid_for_db_query(value: Any) str[source]

Normalize an ObjectId value for database queries, converting ObjectId objects to string representations and validating hex strings.

Parameters:

value – The value to normalize (ObjectId, hex string, or other)

Returns:

The normalized string representation suitable for database queries

neosqlite.collection.type_correction.normalize_id_query_for_db(query: dict[str, Any]) dict[str, Any][source]

Normalize ID types in a query dictionary to correct common mismatches.

This method automatically detects and corrects common ID type mismatches: - When ‘id’ field is queried with an ObjectId/hex string, it’s converted to ‘_id’ - When ‘id’ field is queried with an integer string, it’s converted to integer - When ‘_id’ field is queried with an integer string, it’s converted to integer - When any other field is queried with an ObjectId, it’s converted to string

Parameters:

query – The query dictionary to process

Returns:

A new query dictionary with corrected ID types

neosqlite.collection.type_correction.get_integer_id_for_oid(db_connection: Any, collection_name: str, oid: Any) int[source]

Get the integer ID for a given ObjectId or other ID type.

Parameters:
  • db_connection – SQLite database connection

  • collection_name – Name of the collection/table

  • oid – The ID value (ObjectId, int, str, etc.)

Returns:

Integer ID from the database

Raises:

ValueError – If the ID cannot be found

neosqlite.collection.type_correction.get_integer_id_for_table(db_connection: Any, table_name: str, oid: Any) int[source]

Get the integer ID for a file in GridFS tables.

This is an alias for get_integer_id_for_oid, provided for clarity when working with GridFS tables.

Parameters:
  • db_connection – SQLite database connection

  • table_name – Name of the GridFS table

  • oid – The file ID

Returns:

Integer ID from the database

Raises:

ValueError – If the ID cannot be found