Node

Enrichment

LAST EDIT Jan 26 2021

Enrichment of Collection Entries

Copied!

Objects stored inside collections can be embedded inside activities or user objects. This allows you to integrate with Stream without building a complex integration with another database. Stream's collections can be used as your data source for data enrichment.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// first we add our object to the food collection 
let cheeseBurger = await client.collections.add('food', '123', { 
	name: 'Cheese Burger', 
	ingredients: ['cheese', 'burger', 'bread', 'lettuce', 'tomato'], 
}); 
 
// the object returned by .add can be embedded directly inside of an activity 
await userFeed.addActivity({ 
	actor: client.currentUser, 
	verb: 'grill', 
	object: cheeseBurger, 
}); 
 
// if we now read the feed, the activity we just added will include the entire full object 
await userFeed.get(); 
 
// we can then update the object and Stream will propagate the change to all activities 
await cheeseBurger.update({ 
	name: 'Amazing Cheese Burger', 
	ingredients: ['cheese', 'burger', 'bread', 'lettuce', 'tomato'], 
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# first we add our object to the food collection 
cheese_burger = client.collections.add( 
    "food", 
    data={ 
        "name": "Cheese Burger", 
        "ingredients": ["cheese", "burger", "bread", "lettuce", "tomato"], 
    }, 
) 
 
# then we embed a reference to the entry we created before 
user_feed.add_activity( 
    { 
        "actor": "jim", 
        "verb": "grill", 
        "object": client.collections.create_reference( 
            "food", cheese_burger["id"] 
        ), 
    } 
) 
 
# if we now read the feed, the activity we just added will include the entire full object 
user_feed.get(enrich=True)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# first we add our object to the food collection 
cheese_burger = client.collections.add( 
    "food",  
    :data => { 
        :name => "Cheese Burger", 
        :ingredients => ["cheese", "burger", "bread", "lettuce", "tomato"], 
    }, 
) 
 
# then we embed a reference to the entry we created before 
user_feed.add_activity({ 
    :actor => "jim", 
    :verb => "grill", 
    :object => client.collections.create_reference( 
        "food", cheese_burger["id"] 
    ), 
}) 
 
# if we now read the feed, the activity we just added will include the entire full object 
user_feed.get(:enrich => true)
1
2
3
4
5
6
7
8
9
10
11
12
13
// first we add our object to the food collection 
$client->collections()->add( 
     "food", ["name" => "Cheese Burger", "rating" => "4 stars"], "cheese-burger" 
); 
 
// then we embed a reference to the entry we created before 
$user_feed->addActivity( 
    ["actor" => "jim", "verb" => "grill", 
     "object" => $client->collections()->createReference("food", "cheese-burger")] 
); 
 
// if we now read the feed, the activity we just added will include the entire full object 
$user_feed->getActivities(0, 1, null, true);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// first we add our object to the food collection 
CollectionData cheeseBurger = client.collections().add("food", new CollectionData("123") 
        .set("name", "Cheese Burger") 
        .set("ingredients", Lists.newArrayList("cheese", "burger", "bread", "lettuce", "tomato"))).join(); 
 
// the object returned by .add can be embedded directly inside of an activity 
userFeed.addActivity(Activity.builder() 
        .actor(createUserReference("john-doe")) 
        .verb("grill") 
        .object(createCollectionReference(cheeseBurger.getCollection(), cheeseBurger.getID())) 
        .build()).join(); 
 
// if we now read the feed, the activity we just added will include the entire full object 
userFeed.getEnrichedActivities(); 
 
// we can then update the object and Stream will propagate the change to all activities 
client.collections().update(cheeseBurger.getCollection(), cheeseBurger 
        .set("name", "Amazing Cheese Burger") 
        .set("ingredients", Lists.newArrayList("cheese", "burger", "bread", "lettuce", "tomato"))).join();
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//first we add our object to the food collection 
obj := stream.CollectionObject{ 
	Data: map[string]interface{}{ 
		"name":        "Cheese Burger", 
		"ingredients": []string{"cheese", "burger", "bread", "lettuce", "tomato"}, 
	}, 
} 
cheeseBurger, err := client.Collections().Add("food", obj) 
 
//then we embed a reference to the entry we created before 
activity := stream.Activity{ 
	Actor:  "jim", 
	Verb:   "grill", 
	Object: client.Collections().CreateReference("food", cheeseBurger.ID), 
} 
userFeed.AddActivity(activity) 
 
//if we now read the feed, the activity we just added will include the entire full object 
enrichedResponse, err := userFeed.GetEnrichedActivities()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// first we add our object to the food collection 
let cheeseBurger = Food(name: "Cheese Burger", rating: 4, id: "cheese-burger") 
 
// setup an enriched activity type 
typealias UserFoodActivity = EnrichedActivity<user, food,="" string=""> 
 
client.add(collectionObject: cheeseBurger) { _ in 
    // the object returned by .add can be embedded directly inside of an activity 
    userFeed.add(UserFoodActivity(actor: client.currentUser!, verb: 'grill', object: cheeseBurger)) { _ in 
        // if we now read the feed, the activity we just added will include the entire full object 
        userFeed.get(typeOf: UserFoodActivity.self) { result in 
            let activities = try! result.get().results 
             
            // we can then update the object and Stream will propagate the change to all activities 
            cheeseBurger.name = "Amazing Cheese Burger" 
            client.update(collectionObject: cheeseBurger) { result in /* ... */ } 
        } 
    } 
}</user,>
1
2
3
4
5
6
7
8
9
10
11
12
// first we add our object to the food collection 
var collectionData = new GenericData(); 
collectionData.SetData("name", "Cheese Burger"); 
collectionData.SetData("ingredients", new string[] { "cheese", "burger", "bread", "lettuce", "tomato" }); 
var cheeseBurger = await client.Collections.Add("food", collectionData, "123"); 
 
// the object returned by .add can be embedded directly inside of an activity 
var activity = new Activity("jim", "grill", cheeseBurger.Ref("food")); 
await userFeed.AddActivity(activity); 
 
// if we now read the feed, the activity we just added will include the entire full object 
await userFeed.GetEnrichedFlatActivities();