Organizing data with user and folder scoping in Morphik
Morphik provides powerful mechanisms to organize and isolate your data through user scoping and folder scoping. These features allow you to create logical boundaries for different projects or user groups while maintaining a unified database.
Folders in Morphik allow you to organize documents into logical groups, similar to directories in a file system, but for your unstructured data. Operations performed within a folder scope only affect documents within that folder.
from morphik import Morphikwith Morphik() as db: # Create or get a folder folder = db.create_folder("project_x") # or folder = db.get_folder("project_x") # Operations are scoped to this folder doc = folder.ingest_text("This document belongs to Project X") chunks = folder.retrieve_chunks("project") docs = folder.list_documents() # Only lists documents in this folder
User scoping allows multi-tenant applications to isolate data per end user, ensuring each user only sees their own documents. This is particularly useful for applications where privacy between users is important.
from morphik import Morphikwith Morphik() as db: # Create a user scope user_scope = db.signin("user123") # Operations are scoped to this user doc = user_scope.ingest_text("This belongs to user123 only") docs = user_scope.list_documents() # Only lists documents for this user completion = user_scope.query("What documents do I have?") # Only searches user123's documents
For the maximum level of organization, you can combine both scopes to organize documents by both user and folder.
Copy
Ask AI
from morphik import Morphikwith Morphik() as db: # First get a folder folder = db.get_folder("project_x") # Then scope to a specific user within that folder user_folder_scope = folder.signin("user123") # Operations are scoped to both the folder and user doc = user_folder_scope.ingest_text("This belongs to user123 in project_x") chunks = user_folder_scope.retrieve_chunks("project") docs = user_folder_scope.list_documents() # Only lists user123's documents in project_x
All scoping features are also available with the asynchronous client:
Copy
Ask AI
from morphik import AsyncMorphikasync with AsyncMorphik() as db: # Create folder and user scopes folder = db.get_folder("project_x") user_scope = db.signin("user123") combined_scope = folder.signin("user123") # Use scoped operations asynchronously doc = await combined_scope.ingest_text("Document for user123 in project_x") results = await combined_scope.retrieve_docs("project")
An application serving multiple end users can use user scoping to keep each user’s data private:
Copy
Ask AI
# User 1's personal datauser1 = db.signin("user1")doc = user1.ingest_text("My private notes")# User 2's personal data (completely separate)user2 = db.signin("user2")doc = user2.ingest_text("My confidential information")