-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathview_context.h
More file actions
84 lines (73 loc) · 2.44 KB
/
Copy pathview_context.h
File metadata and controls
84 lines (73 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#ifndef SPACETIMEDB_VIEW_CONTEXT_H
#define SPACETIMEDB_VIEW_CONTEXT_H
#include <spacetimedb/bsatn/types.h> // For Identity
#include <spacetimedb/bsatn/timestamp.h> // For Timestamp
#include <spacetimedb/readonly_database_context.h> // For ReadOnlyDatabaseContext
#include <array>
namespace SpacetimeDB {
/**
* @brief Context for views with caller identity
*
* ViewContext provides read-only database access along with the identity
* of the caller who invoked the view. Use this when the view needs to
* filter or customize results based on who is calling it.
*
* Key differences from ReducerContext:
* - db is ReadOnlyDatabaseContext (no mutations allowed)
* - No connection_id (views are stateless, don't track connections)
* - No rng() method (views should be deterministic)
*
* Example usage:
* @code
* SPACETIMEDB_VIEW(std::vector<Item>, get_my_items, Public, ViewContext ctx) {
* std::vector<Item> my_items;
* // Filter by caller's identity using indexed field
* for (const auto& item : ctx.db[item_owner].filter(ctx.sender())) {
* my_items.push_back(item);
* }
* return Ok(my_items);
* }
* @endcode
*/
struct ViewContext {
private:
// Caller's identity - who invoked this view
Identity sender_;
public:
// Read-only database access - no mutations allowed
ReadOnlyDatabaseContext db;
// Constructors
ViewContext() = default;
explicit ViewContext(Identity s)
: sender_(s) {}
Identity sender() const { return sender_; }
};
/**
* @brief Context for anonymous views without caller identity
*
* AnonymousViewContext provides read-only database access without
* exposing the caller's identity. Use this for views that return
* the same data regardless of who calls them.
*
* This is more efficient than ViewContext as it doesn't require
* identity information to be passed from the host.
*
* Key differences from ViewContext:
* - No sender field (caller identity not available)
* - Otherwise identical functionality
*
* Example usage:
* @code
* SPACETIMEDB_VIEW(std::optional<uint64_t>, count_users, Public, AnonymousViewContext ctx) {
* return Ok(std::optional<uint64_t>(ctx.db[user].count()));
* }
* @endcode
*/
struct AnonymousViewContext {
// Read-only database access - no mutations allowed
ReadOnlyDatabaseContext db;
// Constructors
AnonymousViewContext() = default;
};
} // namespace SpacetimeDB
#endif // SPACETIMEDB_VIEW_CONTEXT_H