|
7 | 7 |
|
8 | 8 | import pickle |
9 | 9 | from enum import Enum |
10 | | -from typing import Dict, Union, Any, Type |
| 10 | +from typing import Dict, Union, Any, Type, List |
11 | 11 | import warnings |
12 | 12 |
|
13 | 13 | import pandas as pd |
@@ -105,17 +105,32 @@ def read_query(self, |
105 | 105 | return df.to_dict() |
106 | 106 | raise TypeError("Unknown output format") |
107 | 107 |
|
108 | | - def insert_query(self) -> None: |
109 | | - """Placeholder for bulk insert""" |
110 | | - #accept dataframe & model |
111 | | - #use pandas to_sql method to perform insert |
112 | | - #if except return false or maybe raise error |
113 | | - #else return true |
114 | | - raise NotImplementedError |
115 | | - |
116 | | - def create_object(self, obj:object) -> Union[int, str]: |
117 | | - pkey_name = obj.get_pkey_name() |
118 | | - setattr(obj, pkey_name, None) |
| 108 | + def insert_query(self, objs:List[object]) -> None: |
| 109 | + with self.session_maker() as session: |
| 110 | + session.add_all(objs) |
| 111 | + session.commit() |
| 112 | + |
| 113 | + def create_object(self, obj:object, preserve_pkey:bool=False) -> Union[int, str]: |
| 114 | + """ Accepts an ORM mapped model and created a corresponding database record |
| 115 | +
|
| 116 | + Accepts on one of the ORM mapped models and creates the corresponding database |
| 117 | + record, returning the primary key of the newly created record. |
| 118 | +
|
| 119 | + Arguments: |
| 120 | + obj:object - The ORM mapped model |
| 121 | + preserve_pkey:bool - Default=False - flag indicating if the primary key for |
| 122 | + the object should be preserved. Avoid in general use cases where database has |
| 123 | + a serial that auto assigned primary key, however this can be set to True to |
| 124 | + specify you own the primary key value. |
| 125 | +
|
| 126 | + Returns: |
| 127 | + primary key: Union[int, str] |
| 128 | +
|
| 129 | + """ |
| 130 | + |
| 131 | + if not perserve_pkey: |
| 132 | + pkey_name = obj.get_pkey_name() |
| 133 | + setattr(obj, pkey_name, None) |
119 | 134 |
|
120 | 135 | with self.session_maker() as session: |
121 | 136 | session.add(obj) |
|
0 commit comments