def upsert_entity(data, collection, authz=None, sync=False): """Create or update an entity in the database. This has a side hustle of migrating entities created via the _bulk API or a mapper to a database entity in the event that it gets edited by the user. """ entity = None entity_id = collection.ns.sign(data.get("id")) if entity_id is not None: entity = Entity.by_id(entity_id, collection=collection) if entity is None: role_id = authz.id if authz is not None else None entity = Entity.create(data, collection, role_id=role_id) else: entity.update(data, collection) # Inline name properties from adjacent entities. See the # docstring on `inline_names` for a more detailed discussion. proxy = entity.to_proxy() entity_ids = proxy.get_type_values(registry.entity) for rel in index.entities_by_ids(entity_ids): inline_names(proxy, model.get_proxy(rel)) entity.data = proxy.properties db.session.add(entity) delete_aggregator_entity(collection, entity.id) index.index_proxy(collection, proxy, sync=sync) refresh_entity(collection, entity.id) return entity.id
def upsert_entity(data, collection, sync=False): entity = None entity_id = collection.ns.sign(data.get('id')) if entity_id is not None: entity = Entity.by_id(entity_id, collection=collection, deleted=True) # TODO: migrate softly from index. if entity is None: entity = Entity.create(data, collection) else: entity.update(data, collection) collection.touch() db.session.commit() delete_aggregator_entity(collection, entity.id) index.index_entity(entity, sync=sync) refresh_entity(entity.id, sync=sync) refresh_collection(collection.id, sync=sync) return entity.id
def delete_entity(collection, entity, deleted_at=None, sync=False): # This is recursive and will also delete any entities which # reference the given entity. Usually this is going to be child # documents, or directoships referencing a person. It's a pretty # dangerous operation, though. entity_id = collection.ns.sign(entity.get('id')) for adjacent in index.iter_adjacent(entity): log.warning("Recursive delete: %r", adjacent) delete_entity(collection, adjacent, deleted_at=deleted_at, sync=sync) flush_notifications(entity_id, clazz=Entity) obj = Entity.by_id(entity_id, collection=collection) if obj is not None: obj.delete(deleted_at=deleted_at) doc = Document.by_id(entity_id, collection=collection) if doc is not None: doc.delete(deleted_at=deleted_at) index.delete_entity(entity_id, sync=sync) delete_aggregator_entity(collection, entity_id) refresh_entity(entity_id, sync=sync) refresh_collection(collection.id, sync=sync)
def upsert_entity(data, collection, validate=True, sync=False): """Create or update an entity in the database. This has a side hustle of migrating entities created via the _bulk API or a mapper to a database entity in the event that it gets edited by the user. """ entity = None entity_id = collection.ns.sign(data.get('id')) if entity_id is not None: entity = Entity.by_id(entity_id, collection=collection, deleted=True) # TODO: migrate softly from index. if entity is None: entity = Entity.create(data, collection, validate=validate) else: entity.update(data, collection, validate=validate) collection.touch() db.session.commit() delete_aggregator_entity(collection, entity.id) index.index_entity(entity, sync=sync) refresh_entity(entity.id, sync=sync) refresh_collection(collection.id, sync=sync) return entity.id