def insert_create_event(self, create_event, position: int) -> None: if self.exists_query("models_lookup", "fqid=%s", [create_event.fqid]): raise ModelExists(create_event.fqid) # update max collection id if neccessary statement = dedent( """\ insert into id_sequences (collection, id) values (%s, %s) on conflict(collection) do update set id=greatest(id_sequences.id, excluded.id);""" ) arguments: List[Any] = [ collection_from_fqid(create_event.fqid), int(id_from_fqid(create_event.fqid)) + 1, ] self.connection.execute(statement, arguments) arguments = [ position, create_event.fqid, EVENT_TYPES.CREATE, self.json(create_event.field_data), ] self.insert_db_event(create_event, arguments, position) statement = "insert into models_lookup (fqid, deleted) values (%s, %s)" arguments = [create_event.fqid, False] self.connection.execute(statement, arguments)
def get_many(self, request: GetManyRequest) -> Dict[str, Dict[str, Model]]: with self.database.get_context(): mapped_fields_per_fqid: Dict[str, List[str]] = {} if isinstance(request.requests[0], GetManyRequestPart): requests = cast(List[GetManyRequestPart], request.requests) for part in requests: for id in part.ids: fqid = build_fqid(part.collection, str(id)) mapped_fields_per_fqid.setdefault( fqid, []).extend(part.mapped_fields + request.mapped_fields) else: fqfield_requests = cast(List[str], request.requests) for fqfield in fqfield_requests: fqid = fqid_from_fqfield(fqfield) mapped_fields_per_fqid.setdefault(fqid, []).append( field_from_fqfield(fqfield)) fqids = list(mapped_fields_per_fqid.keys()) if request.position: fqids = self.filter_fqids_by_deleted_status( fqids, request.position, request.get_deleted_models) result = self.database.build_models_ignore_deleted( fqids, request.position) result = self.apply_mapped_fields_multi( result, mapped_fields_per_fqid) else: result = self.database.get_many( fqids, mapped_fields_per_fqid, request.get_deleted_models, ) # change mapping fqid->model to collection->id->model final: Dict[str, Dict[str, Model]] = defaultdict(dict) for fqid, model in result.items(): final[collection_from_fqid(fqid)][id_from_fqid(fqid)] = model # add back empty collections for fqid in mapped_fields_per_fqid.keys(): collection = collection_from_fqid(fqid) if not final[collection]: final[collection] = {} return final
def get( self, fqid: str, mapped_fields: List[str] = [], get_deleted_models: DeletedModelsBehaviour = DeletedModelsBehaviour. ALL_MODELS, ) -> Model: collection = collection_from_fqid(fqid) models = self.get_many([fqid], {collection: mapped_fields}, get_deleted_models) try: return models[fqid] except KeyError: raise get_exception_for_deleted_models_behaviour( fqid, get_deleted_models)
def test_collection_from_fqid(): fqid = "a/1" assert collection_from_fqid(fqid) == "a"