Collections

Overview

Collections provide a way to attach data to activities that is shared between these activities and can be updated in a single place. A typical use case is to track the status of an activity such as started, completed, or failed. Instead of having to update multiple activities when the status changes you can create a collection and add it to the activities that needs this data. When the status changes you only need to update the collection.

A collection consists of a name, an id and some data. You can think of the name as a namespace for the collection. A collection reference is a string that looks like <name>:<id> and is the unique identifier for the collection.

Data stored in collections can not be used in custom ranking or aggregation.

The batch endpoints supports up to 100 collections in a single request. Automatic deduplication will be applied, keeping the last occurrence only.

Creating Collections

The example below shows how to create a collection.

let response = try await client.createCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9
                ]
            )
        ]
    )
)

Overview of the collection model

CollectionResponse

NameTypeDescriptionConstraints
created_atnumberWhen the collection was created-
customobjectCustom data for the collection-
idstringUnique identifier for the collection within its nameRequired
namestringName/type of the collectionRequired
updated_atnumberWhen the collection was last updated-
user_idstringID of the user who owns this collection-

Updating Collections

The example below shows how to update a collection.

let response = try await client.updateCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9.5
                ]
            )
        ]
    )
)

Deleting Collections

The example below shows how to delete a collection.

let response = try await client.deleteCollections(
    request: .init(
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)

Reading Collections

The example below shows how to read a collection.

let response = try await client.readCollections(
    request: .init(
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)

Activities and Enrichment

Collections are added to activities in the form of collection references. An activity can reference up to 10 collections.

Support for adding collections directly when creating activities is coming soon.

Adding Collections to an Activity

let response = try await feed.addActivity(
    request: .init(
        type: "post",
        text: "I love this movie!",
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)

Enrichment

When you have added collection references to your activities these will automatically be enriched with the collection data when reading feeds.

// Create a collection
let createResponse = try await client.createCollections(
    request: .init(
        collections: [
            .init(
                name: "movies",
                id: "lord_of_the_rings",
                custom: [
                    "title": "Lord of the Rings",
                    "genre": "fantasy",
                    "rating": 9
                ]
            )
        ]
    )
)

// Add the reference to an activity
let feed = client.feed(group: "user", id: "jack")
let activityResponse = try await feed.addActivity(
    request: .init(
        type: "post",
        text: "I love this movie!",
        collectionRefs: ["movies:lord_of_the_rings"]
    )
)

// Read the feed and see the enriched collection data
try await feed.getOrCreate()
let activities = feed.state.activities
// The activities will contain enriched collection data in the collections field
// Example response structure:
// {
//     "activities": [
//         {
//             "type": "post",
//             "text": "I love this movie!",
//             "collections": {
//                 "movies:lord_of_the_rings": {
//                     "name": "movies",
//                     "id": "lord_of_the_rings",
//                     "custom": {
//                         "title": "Lord of the Rings",
//                         "genre": "fantasy",
//                         "rating": 9
//                     },
//                     "user_id": "jack",
//                     "created_at": "2025-01-01T00:00:00.000Z",
//                     "updated_at": "2025-01-01T00:00:00.000Z",
//                     "status": "ok"
//                 }
//             }
//         }
//     ],
//     ...
// }

The enrichment is a best effort process. Missing or deleted collections will not be enriched but will be retured with a status of notfound