# `sc_crawler.insert`

> Helpers to validate and bulk insert per-vendor records into the database.

# `sc_crawler.insert`

**Functions:**

- [**can_bulk_insert**](#can_bulk_insert) – Checks if bulk insert is supported for the engine dialect of a SQLModel session.
- [**validate_items**](#validate_items) – Validates a list of items against a [pydantic.BaseModel](https://docs.pydantic.dev/latest/api/pydantic/base_model/#pydantic.BaseModel) definition.
- [**bulk_insert_items**](#bulk_insert_items) – Bulk inserts items into a SQLModel table with `ON CONFLICT` update.
- [**insert_items**](#insert_items) – Insert items into the related database table using bulk or merge.

## func <code class="doc-name">can_bulk_insert</code> [insert.py:22–24](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/insert.py#L22-L24)

```python
can_bulk_insert(session)
```

Checks if bulk insert is supported for the engine dialect of a SQLModel session.

## func <code class="doc-name">validate_items</code> [insert.py:80–117](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/insert.py#L80-L117)

```python
validate_items(model, items, vendor=None, prefix='')
```

Validates a list of items against a [pydantic.BaseModel](https://docs.pydantic.dev/latest/api/pydantic/base_model/#pydantic.BaseModel) definition.

**Parameters:**

- **model** ([`BaseModel`](https://docs.pydantic.dev/latest/api/pydantic/base_model/#pydantic.BaseModel)) – An SQLModel model to be used for validation.
- **items** ([`List`](https://docs.python.org/3/library/typing.html#typing.List)`[`[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)`]`) – List of dictionaries to be checked against `model`.
- **vendor** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)`]`) – Optional Vendor instance used for logging and progress bar updates.
- **prefix** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – Optional extra description for the model added in front of
the model name in logs and progress bar updates.

**Returns:**

- [`List`](https://docs.python.org/3/library/typing.html#typing.List)`[`[`dict`](https://docs.python.org/3/builtins/stdtypes.html#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 <code class="doc-name">bulk_insert_items</code> [insert.py:120–178](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/insert.py#L120-L178)

```python
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`](https://docs.python.org/3/library/typing.html#typing.List)`[`[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)`]`) – List of dicts with all columns of the model.
- **vendor** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)`]`) – Optional related Vendor instance used for logging and progress bar updates.
- **session** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[Session]`) – Optional database connections. When not provided, defaults to the `vendor`'s session.
- **progress** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Progress`](https://rich.readthedocs.io/en/stable/reference/progress.html#rich.progress.Progress)`]`) – Optional progress bar to use instead of `vendor`'s progress bar.
- **prefix** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – Optional extra description for the model added in front of
the model name in logs and progress bar updates.

## func <code class="doc-name">insert_items</code> [insert.py:181–239](https://github.com/SpareCores/sc-crawler/blob/81bb61ea164dda526de4aa6de298810007274f21/src/sc_crawler/insert.py#L181-L239)

```python
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 PostgreSQL, other databases
fall back to the default session.merge (slower) approach.

**Parameters:**

- **model** (`SQLModel`) – An SQLModel table definition with primary key(s).
- **items** ([`List`](https://docs.python.org/3/library/typing.html#typing.List)`[`[`dict`](https://docs.python.org/3/builtins/stdtypes.html#dict)`]`) – List of dicts with all columns of the model.
- **vendor** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Vendor`](https://docs.sparecores.com/crawler/reference/tables.md#vendor)`]`) – Optional related Vendor instance used for database connection, logging and progress bar updates.
- **session** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[Session]`) – Optional database connections. When not provided, defaults to the `vendor`'s session.
- **progress** ([`Optional`](https://docs.python.org/3/library/typing.html#typing.Optional)`[`[`Progress`](https://rich.readthedocs.io/en/stable/reference/progress.html#rich.progress.Progress)`]`) – Optional progress bar to use instead of `vendor`'s progress bar.
- **prefix** ([`str`](https://docs.python.org/3/builtins/stdtypes.html#str)) – Optional extra description for the model added in front of
the model name in logs and progress bar updates.
