sc_crawler.insert
Helpers to validate and bulk insert per-vendor records into the database.
Functions:
- can_bulk_insert – Checks if bulk insert is supported for the engine dialect of a SQLModel session.
- validate_items – Validates a list of items against a pydantic.BaseModel definition.
- bulk_insert_items – Bulk inserts items into a SQLModel table with
ON CONFLICTupdate. - insert_items – Insert items into the related database table using bulk or merge.
func can_bulk_insert insert.py:22–24
can_bulk_insert(session)
Checks if bulk insert is supported for the engine dialect of a SQLModel session.
func validate_items insert.py:80–117
validate_items(model, items, vendor=None, prefix='')
Validates a list of items against a pydantic.BaseModel definition.
Parameters:
- model (
BaseModel) – An SQLModel model to be used for validation. - items (
List[dict]) – List of dictionaries to be checked againstmodel. - vendor (
Optional[Vendor]) – Optional Vendor instance used for logging and progress bar updates. - prefix (
str) – Optional extra description for the model added in front of the model name in logs and progress bar updates.
Returns:
List[dict]– List of validated dicts in the same order. Note that missing fields has been filled in with default values (needed for bulk inserts).
func bulk_insert_items insert.py:120–178
bulk_insert_items(model, items, vendor=None, session=None, progress=None, prefix='')
Bulk inserts items into a SQLModel table with ON CONFLICT update.
Parameters:
- model (
SQLModel) – An SQLModel table definition with primary key(s). - items (
List[dict]) – List of dicts with all columns of the model. - vendor (
Optional[Vendor]) – Optional related Vendor instance used for logging and progress bar updates. - session (
Optional[Session]) – Optional database connections. When not provided, defaults to thevendor's session. - progress (
Optional[Progress]) – Optional progress bar to use instead ofvendor's progress bar. - prefix (
str) – Optional extra description for the model added in front of the model name in logs and progress bar updates.
func insert_items insert.py:181–239
insert_items(model, items, vendor=None, session=None, progress=None, prefix='')
Insert items into the related database table using bulk or merge.
Bulk insert is only supported with SQLite and PostgreSQLPostgreSQLAn open-source object-relational database system, developed by a global community and descended from the POSTGRES project at UC Berkeley in 1986. It uses and extends SQL, with transactions, many index types, extensions, and JSON data.PostgreSQL docsGlossary, other databases fall back to the default session.merge (slower) approach.
Parameters:
- model (
SQLModel) – An SQLModel table definition with primary key(s). - items (
List[dict]) – List of dicts with all columns of the model. - vendor (
Optional[Vendor]) – Optional related Vendor instance used for database connection, logging and progress bar updates. - session (
Optional[Session]) – Optional database connections. When not provided, defaults to thevendor's session. - progress (
Optional[Progress]) – Optional progress bar to use instead ofvendor's progress bar. - prefix (
str) – Optional extra description for the model added in front of the model name in logs and progress bar updates.