Skip to main content

Organizing Bookmarks with Collections

To manage and organize your bookmarks into logical groups, you can use the BookmarkRepository to create, retrieve, list, and delete bookmark collections. Note that the BookmarkRepository is an in-memory implementation, meaning all data will be lost when the application restarts.

Creating a New Collection

To create a new bookmark collection, instantiate a Collection object and then use the save_collection method of the BookmarkRepository.

from app.db.repository import BookmarkRepository
from app.models.collection import Collection, CollectionType

# Initialize the repository
repo = BookmarkRepository()

# Create a new manual collection
my_collection = Collection(name="My Reading List", collection_type=CollectionType.MANUAL)
repo.save_collection(my_collection)

# Create a smart collection (e.g., for unread articles)
smart_collection = Collection(
name="Unread Articles",
collection_type=CollectionType.SMART,
filter_rule="status:unread"
)
repo.save_collection(smart_collection)

print(f"Created collection: {my_collection.name} with ID: {my_collection.id}")
print(f"Created smart collection: {smart_collection.name} with ID: {smart_collection.id}")

The save_collection method will either insert a new collection or update an existing one if a Collection object with the same ID is provided. Collections can be MANUAL, where bookmarks are explicitly added, or SMART, where bookmarks are included based on a filter_rule.

Retrieving a Collection

To retrieve a specific collection by its unique identifier, use the get_collection method.

from app.db.repository import BookmarkRepository
from app.models.collection import Collection, CollectionType

repo = BookmarkRepository()

# Assume a collection with this ID exists
collection_id_to_find = "some_collection_id" # Replace with an actual ID from your system

# First, create a collection to ensure it exists for the example
example_collection = Collection(name="Temporary Collection", id=collection_id_to_find)
repo.save_collection(example_collection)

retrieved_collection = repo.get_collection(collection_id_to_find)

if retrieved_collection:
print(f"Found collection: {retrieved_collection.name} (ID: {retrieved_collection.id})")
else:
print(f"Collection with ID {collection_id_to_find} not found.")

The get_collection method returns a Collection object if found, or None if no collection matches the provided collection_id.

Listing All Collections

To get a list of all collections currently stored in the repository, use the list_collections method.

from app.db.repository import BookmarkRepository
from app.models.collection import Collection, CollectionType

repo = BookmarkRepository()

# Ensure some collections exist for the example
repo.save_collection(Collection(name="Work Projects"))
repo.save_collection(Collection(name="Personal Reads"))

all_collections = repo.list_collections()

if all_collections:
print("All collections:")
for collection in all_collections:
print(f"- {collection.name} (ID: {collection.id}, Type: {collection.collection_type.name})")
else:
print("No collections found.")

The list_collections method returns a list of all Collection objects.

Deleting a Collection

To remove a collection from the repository, use the delete_collection method with the collection's ID.

from app.db.repository import BookmarkRepository
from app.models.collection import Collection

repo = BookmarkRepository()

# Create a collection to be deleted
collection_to_delete = Collection(name="Ephemeral Collection")
repo.save_collection(collection_to_delete)
print(f"Collection '{collection_to_delete.name}' created with ID: {collection_to_delete.id}")

# Attempt to delete the collection
was_deleted = repo.delete_collection(collection_to_delete.id)

if was_deleted:
print(f"Collection with ID {collection_to_delete.id} was successfully deleted.")
else:
print(f"Collection with ID {collection_to_delete.id} was not found or could not be deleted.")

# Verify it's gone
retrieved_after_delete = repo.get_collection(collection_to_delete.id)
if not retrieved_after_delete:
print("Verification: Collection is indeed gone.")

The delete_collection method returns True if the collection was found and deleted, and False otherwise.

Important Considerations

The BookmarkRepository is designed as an in-memory storage solution. This means that any collections created, updated, or deleted will only persist for the duration of the application's runtime. Upon application restart, all data will be lost. For persistent storage in a production environment, a database-backed repository with transaction support would be required.