Пример #1
0
    async def update_self(self, document, alias=None, upsert=False) -> UpdateResult:
        if document.is_partly_loaded:
            msg = (
                "Partly loaded document {0} can't be saved. Document should "
                "be loaded without 'only', 'exclude' or 'fields' "
                "QuerySet's modifiers"
            )
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__)
            )


        if self.validate_document(document):

            await self.ensure_indexes(alias=alias)

            self.update_field_on_save_values(document, document._id is not None)
            doc = document.to_son_changed_values()

            if document._id is not None:
                # print(f"update_self: {document._id}, {doc}")
                result = await self.coll(alias).update_one(
                    {'_id': document._id}, 
                    {'$set': doc}, 
                    upsert=upsert,
                )
                return result
            else:
                raise Exception("document can not be updated because it has no id!")
Пример #2
0
    def save(self, document, callback, alias=None):
        if document.is_partly_loaded:
            msg = (
                "Partly loaded document {0} can't be saved. Document should "
                "be loaded without 'only', 'exclude' or 'fields' "
                "QuerySet's modifiers"
            )
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__)
            )

        if self.validate_document(document):
            self.ensure_index(callback=self.indexes_saved_before_save(document, callback, alias=alias), alias=alias)
Пример #3
0
    async def save(self, document, alias=None):
        if document.is_partly_loaded:
            msg = (
                'Partly loaded document {0} can\'t be saved. Document should '
                'be loaded without \'only\', \'exclude\' or \'fields\' '
                'QuerySet\'s modifiers')
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__))

        self.update_field_on_save_values(document, document._id is not None)
        if self.validate_document(document):
            await self.ensure_index(alias=alias)
            return await self.save_document(document, alias=alias)
Пример #4
0
    def save(self, document, alias=None):
        if document.is_partly_loaded:
            msg = (
                "Partly loaded document {0} can't be saved. Document should "
                "be loaded without 'only', 'exclude' or 'fields' "
                "QuerySet's modifiers")
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__))

        self.update_field_on_save_values(document, document._id is not None)
        if self.validate_document(document):
            yield from self.ensure_index(alias=alias)
            return (yield from self.save_document(document, alias=alias))
Пример #5
0
    def save(self, document, callback, alias=None, upsert=False):
        if document.is_partly_loaded:
            msg = (
                'Partly loaded document {0} can\'t be saved. Document should '
                'be loaded without \'only\', \'exclude\' or \'fields\' '
                'QuerySet\'s modifiers')
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__))

        self.update_field_on_save_values(document, document._id is not None)
        if self.validate_document(document):
            self.ensure_index(callback=self.indexes_saved_before_save(
                document, callback, alias=alias, upsert=upsert),
                              alias=alias)
Пример #6
0
    async def save(self, document, alias=None, upsert=False) -> ObjectId:
        """Summary
        If the document is a new created one, it should have no id and will be inserted into db.
        If the documetn is the retrieved one, it should have an id and will be replaced into db.
        The replace method should be effective if most fields of the document are updated. Otherwise,
        when only few fields are updated, it will cost more I/O. 
        Args:
            document (TYPE): Description
            alias (None, optional): Description
            upsert (bool, optional): Description
        
        Returns:
            ObjectId: Description
        
        Raises:
            PartlyLoadedDocumentError: Description
        """
        if document.is_partly_loaded:
            msg = (
                "Partly loaded document {0} can't be saved. Document should "
                "be loaded without 'only', 'exclude' or 'fields' "
                "QuerySet's modifiers"
            )
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__)
            )


        if self.validate_document(document):

            await self.ensure_indexes(alias=alias)

            self.update_field_on_save_values(document, document._id is not None)
            doc = document.to_son()

            if document._id is not None:
                # print(f"xxxxxxxxx: {document._id}, {doc}")
                result = await self.coll(alias).replace_one(
                    {'_id': document._id}, 
                    doc, 
                    upsert=upsert,
                )
                return document._id if result.modified_count > 0 else None
            else:
                result = await self.coll(alias).insert_one(doc)
                document.set_id(result.inserted_id)
                return result.inserted_id
Пример #7
0
    def save(self, document, callback, alias=None):
        if document.is_partly_loaded:
            msg = (
                "Partly loaded document {0} can't be saved. Document should "
                "be loaded without 'only', 'exclude' or 'fields' "
                "QuerySet's modifiers"
            )
            raise PartlyLoadedDocumentError(
                msg.format(document.__class__.__name__)
            )

        if self.validate_document(document):
            self.update_field_on_save_values(document, document._id is not None)
            doc = document.to_son()

            if document._id is not None:
                self.coll(alias).update({'_id': document._id}, doc, callback=self.handle_update(document, callback))
            else:
                self.coll(alias).insert(doc, callback=self.handle_save(document, callback))