MCPcopy
hub / github.com/msiemens/tinydb / insert

Method insert

tinydb/table.py:141–179  ·  view source on GitHub ↗

Insert a new document into the table. :param document: the document to insert :returns: the inserted document's ID

(self, document: Mapping)

Source from the content-addressed store, hash-verified

139 return self._storage
140
141 def insert(self, document: Mapping) -> int:
142 """
143 Insert a new document into the table.
144
145 :param document: the document to insert
146 :returns: the inserted document's ID
147 """
148
149 # Make sure the document implements the ``Mapping`` interface
150 if not isinstance(document, Mapping):
151 raise ValueError('Document is not a Mapping')
152
153 # First, we get the document ID for the new document
154 if isinstance(document, self.document_class):
155 # For a `Document` object we use the specified ID
156 doc_id = document.doc_id
157
158 # We also reset the stored next ID so the next insert won't
159 # re-use document IDs by accident when storing an old value
160 self._next_id = None
161 else:
162 # In all other cases we use the next free ID
163 doc_id = self._get_next_id()
164
165 # Now, we update the table and add the document
166 def updater(table: dict):
167 if doc_id in table:
168 raise ValueError(f'Document with ID {str(doc_id)} '
169 f'already exists')
170
171 # By calling ``dict(document)`` we convert the data we got to a
172 # ``dict`` instance even if it was a different class that
173 # implemented the ``Mapping`` interface
174 table[doc_id] = dict(document)
175
176 # See below for details on ``Table._update``
177 self._update_table(updater)
178
179 return doc_id
180
181 def insert_multiple(self, documents: Iterable[Mapping]) -> List[int]:
182 """

Callers 15

upsertMethod · 0.95
test_json_kwargsFunction · 0.80
test_json_readwriteFunction · 0.80
test_json_readFunction · 0.80
test_in_memory_closeFunction · 0.80
test_read_onceFunction · 0.80
test_yamlFunction · 0.80
test_tables_listFunction · 0.80
test_multiple_tablesFunction · 0.80
test_zero_cache_sizeFunction · 0.80
test_query_cache_sizeFunction · 0.80

Calls 2

_get_next_idMethod · 0.95
_update_tableMethod · 0.95

Tested by 15

test_json_kwargsFunction · 0.64
test_json_readwriteFunction · 0.64
test_json_readFunction · 0.64
test_in_memory_closeFunction · 0.64
test_read_onceFunction · 0.64
test_yamlFunction · 0.64
test_tables_listFunction · 0.64
test_multiple_tablesFunction · 0.64
test_zero_cache_sizeFunction · 0.64
test_query_cache_sizeFunction · 0.64
test_caching_json_writeFunction · 0.64