Beispiel #1
0
    async def insert(self, collection: str, record: Record) -> Id:
        # Make sure we have an id
        record = dict(record)
        id_ = record.pop('id', None)
        if id_ is None:
            id_ = self._get_next_id(collection)

        key = self._make_record_key(collection, id_)
        set_key = self._make_set_key(collection)

        # Check for duplicates
        if self._client.sismember(set_key, id_):
            raise DuplicateRecordId(id_)

        # Adapt the record to db
        db_record = self._record_to_db(record)

        # Actually insert the record, but only if it's not empty
        if db_record:
            self._client.hset(key, mapping=db_record)

        # Add the id to set
        self._client.sadd(set_key, id_)

        return id_
Beispiel #2
0
    async def insert(self, collection: str, record: Record) -> Id:
        record = dict(record)
        if 'id' in record:
            record['_id'] = self._id_to_db(record.pop('id'))

        return self._id_from_db(
            self._db[collection].insert_one(record).inserted_id)
Beispiel #3
0
    def _record_to_update_clause(record: Record, params: List[Any]) -> str:
        update_clause = []

        id_ = record.pop('id', None)
        if id_ is not None:
            update_clause.append(f'id = ${len(params) + 1}')
            params.append(id_)

        if record:
            update_clause.append(f'content = content || ${len(params) + 1}')
            params.append(record)

        update_clause = ', '.join(update_clause)

        return update_clause
Beispiel #4
0
    async def update(self, collection: str, record_part: Record,
                     filt: Dict[str, Any]) -> int:
        if 'id' in record_part:
            record_part = dict(record_part)
            record_part['_id'] = self._id_to_db(record_part.pop('id'))

        if 'id' in filt:
            filt = dict(filt)
            filt['_id'] = self._id_to_db_rec(filt.pop('id'))

        db_filt = self._filt_to_db(filt)

        return self._db[collection].update_many(db_filt, {
            '$set': record_part
        },
                                                upsert=False).modified_count
Beispiel #5
0
    async def insert(self, collection: str, record: Record) -> Id:
        coll = self._data.setdefault(collection, {})

        id_ = record.get('id')
        if id_ is None:
            id_ = self._find_next_id(coll)
            record = dict(record, id=id_)

        elif id_ in coll:
            raise DuplicateRecordId(id_)

        coll[id_] = record

        self._save(self._unindex(self._data))

        return id_
Beispiel #6
0
 def _record_to_db(cls, record: Record) -> GenericJSONDict:
     return {k: (cls._value_to_db(v) if k != 'id' else v) for k, v in record.items()}
Beispiel #7
0
 def _record_from_db(self, record: Record) -> Record:
     return {k: self.value_from_db(v) for k, v in record.items()}