def save(self, doc):
        if doc.id:
            return self.update(doc)
        else:
            return self.insert(doc)
def encode(value):
    if isinstance(value, bytes):
        value = value.decode(errors="surrogateescape")

    for char in ("\\", "\n", "\t"):
        value = value.replace(
            char, char.encode(encoding="unicode-escape").decode()
        )

    return value