Asynchronously insert a new object into the database. :param conn: An asynchronous database connection object. :param table_name: Name of the database table where the new object will be inserted. :param object_dict: Dictionary containing the data to insert, where keys are column na
(
conn,
table_name: str,
object_dict: Dict,
primary_keys: List[str],
)
| 6 | |
| 7 | |
| 8 | async def create_object( |
| 9 | conn, |
| 10 | table_name: str, |
| 11 | object_dict: Dict, |
| 12 | primary_keys: List[str], |
| 13 | ) -> None: |
| 14 | """ |
| 15 | Asynchronously insert a new object into the database. |
| 16 | |
| 17 | :param conn: An asynchronous database connection object. |
| 18 | :param table_name: Name of the database table where the new object will be inserted. |
| 19 | :param object_dict: Dictionary containing the data to insert, where keys are column names. |
| 20 | :param primary_keys: List of primary key column names. |
| 21 | :return: None |
| 22 | """ |
| 23 | |
| 24 | if not object_dict: |
| 25 | raise ValueError("object_dict cannot be empty") |
| 26 | |
| 27 | # Cast all the dict/list to str |
| 28 | upsert_dict = prepare_db_dict(object_dict) |
| 29 | |
| 30 | # Construct the column names and placeholders for the values |
| 31 | columns = ", ".join(upsert_dict.keys()) |
| 32 | placeholders = ", ".join(f"${i + 1}" for i in range(len(upsert_dict))) |
| 33 | |
| 34 | # Construct the INSERT query |
| 35 | query = f"INSERT INTO {table_name} ({columns}) VALUES ({placeholders})" |
| 36 | |
| 37 | # Prepare the values from the pg_create_dict |
| 38 | values = list(upsert_dict.values()) |
| 39 | |
| 40 | logger.debug(f"create_object: query={query}, values={values}") |
| 41 | |
| 42 | # Attempt to execute the insert query |
| 43 | await conn.execute(query, *values) |
nothing calls this directly
no test coverage detected