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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
//! # Update and Upsert Operations
//!
//! This module provides comprehensive functionality for modifying existing records in Supabase tables.
//! It supports standard updates, upserts (insert or update), and flexible column-based targeting.
//!
//! ## 🎯 Core Features
//!
//! - **[`update`]**: Modify existing records by ID
//! - **[`update_with_column_name`]**: Update records using custom column matching
//! - **[`upsert`]**: Insert new record or update if it exists
//! - **[`upsert_without_defined_key`]**: Upsert with automatic conflict resolution
//!
//! ## 🏗️ Operation Types
//!
//! | Method | Targeting | Behavior | Return Type | Use Case |
//! |--------|-----------|----------|-------------|----------|
//! | `update` | By ID | Updates existing record | `Result<String, String>` | Standard updates |
//! | `update_with_column_name` | By custom column | Updates matching record | `Result<String, String>` | Flexible targeting |
//! | `upsert` | By ID | Insert or update | `Result<String, String>` | Idempotent operations |
//! | `upsert_without_defined_key` | Auto-detect | Insert or update | `Result<(), String>` | Conflict resolution |
//!
//! ## 🔧 Conflict Resolution
//!
//! ### Update vs Upsert Decision Matrix
//!
//! | Scenario | Recommended Method | Reason |
//! |----------|-------------------|---------|
//! | Record definitely exists | `update` | Fastest, fails fast if missing |
//! | Record may or may not exist | `upsert` | Handles both cases gracefully |
//! | Bulk operations with mixed states | `upsert_without_defined_key` | Automatic conflict handling |
//! | Need to update by non-ID field | `update_with_column_name` | Flexible targeting |
//!
//! ## 📖 Usage Examples
//!
//! ### Basic Update Operations
//!
//! ```rust,no_run
//! use supabase_rs::SupabaseClient;
//! use serde_json::json;
//!
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Update by ID (most common)
//! let updated_id = client.update("users", "123", json!({
//! "name": "Alice Smith",
//! "last_login": "2024-01-15T10:30:00Z",
//! "login_count": 42
//! })).await?;
//!
//! println!("Updated user with ID: {}", updated_id);
//! # Ok(())
//! # }
//! ```
//!
//! ### Update by Custom Column
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::json;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Update user by email instead of ID
//! client.update_with_column_name(
//! "users",
//! "email", // Column to match on
//! "alice@example.com", // Value to match
//! json!({
//! "verified": true,
//! "verification_date": "2024-01-15T10:30:00Z"
//! })
//! ).await?;
//!
//! // Update session by token
//! client.update_with_column_name(
//! "sessions",
//! "token",
//! "abc123xyz",
//! json!({ "last_accessed": "2024-01-15T10:30:00Z" })
//! ).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ### Upsert Operations
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::json;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // Upsert with explicit ID
//! let result_id = client.upsert("user_preferences", "user_123", json!({
//! "theme": "dark",
//! "language": "en",
//! "notifications": true
//! })).await?;
//!
//! // Upsert without predefined key (uses Supabase's conflict resolution)
//! client.upsert_without_defined_key("analytics", json!({
//! "user_id": "123",
//! "event": "page_view",
//! "timestamp": "2024-01-15T10:30:00Z",
//! "page": "/dashboard"
//! })).await?;
//! # Ok(())
//! # }
//! ```
//!
//! ## ⚡ Performance Best Practices
//!
//! ### Efficient Update Patterns
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::json;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! // ✅ Good: Update only changed fields
//! client.update("users", "123", json!({
//! "last_login": "2024-01-15T10:30:00Z" // Only update what changed
//! })).await?;
//!
//! // ✅ Good: Use upsert for idempotent operations
//! client.upsert("settings", "user_123", json!({
//! "theme": "dark" // Safe to run multiple times
//! })).await?;
//!
//! // ⚠️ Consider: Batch updates when possible
//! // For multiple updates, consider using transactions or bulk operations
//! # Ok(())
//! # }
//! ```
//!
//! ## 🚨 Error Handling Strategies
//!
//! ```rust,no_run
//! # use supabase_rs::SupabaseClient;
//! # use serde_json::json;
//! # async fn example() -> Result<(), String> {
//! # let client = SupabaseClient::new("url".to_string(), "key".to_string()).unwrap();
//! match client.update("users", "123", json!({"name": "New Name"})).await {
//! Ok(id) => println!("✅ Updated user {}", id),
//! Err(err) => {
//! if err.contains("404") {
//! println!("⚠️ User not found, consider using upsert");
//! } else if err.contains("403") {
//! println!("🚫 Permission denied, check RLS policies");
//! } else {
//! println!("❌ Update failed: {}", err);
//! }
//! }
//! }
//! # Ok(())
//! # }
//! ```
use crateHeadersTypes;
use crateSupabaseClient;
use Response;
use ;