prax-orm 0.11.0

A next-generation, type-safe ORM for Rust inspired by Prisma
Documentation
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
---
import DocsLayout from '../../layouts/DocsLayout.astro';
import CodeBlock from '../../components/CodeBlock.astro';

const basicUpsert = `use prax_query::upsert::{Upsert, ConflictTarget};

// PostgreSQL: ON CONFLICT DO UPDATE
let sql = Upsert::new("users")
    .columns(["email", "name", "updated_at"])
    .values(["$1", "$2", "$3"])
    .on_conflict(ConflictTarget::columns(["email"]))
    .do_update(["name", "updated_at"])
    .to_postgres_sql();

// INSERT INTO users (email, name, updated_at)
// VALUES ($1, $2, $3)
// ON CONFLICT (email)
// DO UPDATE SET name = EXCLUDED.name, updated_at = EXCLUDED.updated_at`;

const doNothing = `use prax_query::upsert::{Upsert, ConflictTarget};

// INSERT or ignore on conflict
let sql = Upsert::new("users")
    .columns(["email", "name"])
    .values(["$1", "$2"])
    .on_conflict(ConflictTarget::columns(["email"]))
    .do_nothing()
    .to_postgres_sql();

// INSERT INTO users (email, name) VALUES ($1, $2) ON CONFLICT (email) DO NOTHING

// MySQL equivalent: the same .do_nothing() renders as INSERT IGNORE
let sql = Upsert::new("users")
    .columns(["email", "name"])
    .values(["?", "?"])
    .do_nothing()
    .to_mysql_sql();

// INSERT IGNORE INTO users (email, name) VALUES (?, ?)`;

const conflictTargets = `use prax_query::upsert::ConflictTarget;

// Conflict on specific columns
let by_columns = ConflictTarget::columns(["tenant_id", "email"]);

// Conflict on constraint name (renders ON CONSTRAINT <name> on Postgres)
let by_constraint = ConflictTarget::constraint("users_email_unique");

// Conflict on index expression (PostgreSQL)
let by_expression = ConflictTarget::index_expression("LOWER(email)");

// No explicit target (MySQL ON DUPLICATE KEY)
let implicit = ConflictTarget::Implicit;`;

const conditionalUpdate = `use prax_query::upsert::{Assignment, AssignmentValue, ConflictTarget, Upsert};

// Conditional update with WHERE — .where_clause() goes on the Upsert
let sql = Upsert::new("products")
    .columns(["sku", "price", "stock", "updated_at"])
    .values(["$1", "$2", "$3", "$4"])
    .on_conflict(ConflictTarget::columns(["sku"]))
    .do_update(["price", "stock", "updated_at"])
    .where_clause("products.updated_at < EXCLUDED.updated_at")  // Only update if newer
    .to_postgres_sql();

// Custom update expressions via Assignment values
let sql = Upsert::new("counters")
    .columns(["key", "value"])
    .values(["$1", "$2"])
    .on_conflict(ConflictTarget::columns(["key"]))
    .do_update_set(vec![
        Assignment {
            column: "value".into(),
            value: AssignmentValue::Expression("counters.value + EXCLUDED.value".into()),
        },  // Increment
        Assignment {
            column: "updated_at".into(),
            value: AssignmentValue::Expression("NOW()".into()),
        },
    ])
    .to_postgres_sql();`;

const mysqlUpsert = `use prax_query::upsert::{Assignment, AssignmentValue, Upsert};

// MySQL: ON DUPLICATE KEY UPDATE — the same .do_update() builder,
// rendered with MySQL syntax by .to_mysql_sql()
let sql = Upsert::new("users")
    .columns(["email", "name", "login_count"])
    .values(["?", "?", "?"])
    .do_update(["name", "login_count"])
    .to_mysql_sql();

// INSERT INTO users (email, name, login_count)
// VALUES (?, ?, ?)
// ON DUPLICATE KEY UPDATE
//   name = VALUES(name),
//   login_count = VALUES(login_count)

// With custom expressions
let sql = Upsert::new("counters")
    .columns(["key", "value"])
    .values(["?", "?"])
    .do_update_set(vec![
        Assignment {
            column: "value".into(),
            value: AssignmentValue::Expression("value + 1".into()),
        },
        Assignment {
            column: "updated_at".into(),
            value: AssignmentValue::Expression("NOW()".into()),
        },
    ])
    .to_mysql_sql();`;

const mssqlMerge = `use prax_query::upsert::{ConflictTarget, Upsert};

// MSSQL: MERGE statement — generated from the same Upsert struct
let sql = Upsert::new("users")
    .columns(["email", "name", "updated_at"])
    .values(["@P1", "@P2", "@P3"])
    .on_conflict(ConflictTarget::columns(["email"]))
    .do_update(["name", "updated_at"])
    .to_mssql_sql();

// MERGE INTO users AS target
// USING (SELECT @P1 AS email, @P2 AS name, @P3 AS updated_at) AS source
// ON target.email = source.email
// WHEN MATCHED THEN
//   UPDATE SET target.name = source.name, target.updated_at = source.updated_at
// WHEN NOT MATCHED THEN
//   INSERT (email, name, updated_at)
//   VALUES (source.email, source.name, source.updated_at);`;

const mongoUpsert = `use prax_query::upsert::mongodb::{self, BulkUpsert};

// Build a MongoDB upsert payload (no client needed — the builder
// produces the filter/update document you hand to the driver)
let upsert = mongodb::upsert()
    .filter_eq("email", "alice@example.com")
    .set("name", "Alice")
    .set("updatedAt", now)
    .set_on_insert("createdAt", now)  // Only on insert
    .inc("loginCount", 1)
    .build();

// updateOne with upsert: true
let op = upsert.to_update_one();
// { "filter": ..., "update": ..., "options": { "upsert": true } }

// findOneAndUpdate returning the post-update document
let op = upsert.to_find_one_and_update(true);

// Replace semantics (full document replacement)
let op = upsert.to_replace_one(replacement);

// Bulk upsert with bulkWrite
let mut alice = serde_json::Map::new();
alice.insert("email".to_string(), serde_json::json!("alice@example.com"));
let mut bob = serde_json::Map::new();
bob.insert("email".to_string(), serde_json::json!("bob@example.com"));

let bulk = BulkUpsert::new()
    .ordered(false)
    .add(alice, serde_json::json!({ "$set": { "name": "Alice" } }))
    .add(bob, serde_json::json!({ "$set": { "name": "Bob" } }));

let doc = bulk.to_bulk_write();
// { "operations": [
//     { "updateOne": { "filter": ..., "update": ..., "upsert": true } }, ... ],
//   "options": { "ordered": false } }`;

const bulkUpsert = `use prax_query::upsert::{ConflictTarget, Upsert};

// BulkUpsert is MongoDB-only — there is no multi-row SQL bulk upsert
// type. For SQL backends, build one Upsert per row and execute the
// statements inside a transaction.
for (sku, name, price) in rows {
    let sql = Upsert::new("products")
        .columns(["sku", "name", "price"])
        .values(["$1", "$2", "$3"])  // bind (sku, name, price) as params
        .on_conflict(ConflictTarget::columns(["sku"]))
        .do_update(["name", "price"])
        .returning(["id", "sku", "created_at"])
        .to_postgres_sql();

    // Execute sql with the row's params inside the transaction...
}`;

const praxUpsert = `// Using Prax's fluent API
let user = client
    .user()
    .upsert()
    .where(user::email::equals("alice@example.com"))
    .create_set("email", "alice@example.com")
    .create_set("name", "Alice")
    .update_set("name", "Alice Updated")
    .exec()
    .await?;

// Or apply the generated typed inputs (UserCreateInput / UserUpdateInput)
let user = client
    .user()
    .upsert()
    .where(user::email::equals("alice@example.com"))
    .with_create_input(UserCreateInput {
        email: "alice@example.com".into(),
        name: Some("Alice".into()),
        ..Default::default()
    })
    .with_update_input(UserUpdateInput {
        name: Some("Alice Updated".into()),
        ..Default::default()
    })
    .exec()
    .await?;

// create_many with skip_duplicates — ON CONFLICT DO NOTHING on
// Postgres/SQLite, INSERT IGNORE on MySQL
let count = client
    .user()
    .create_many()
    .with_create_inputs([
        UserCreateInput { email: "a@example.com".into(), ..Default::default() },
        UserCreateInput { email: "b@example.com".into(), ..Default::default() },
        UserCreateInput { email: "c@example.com".into(), ..Default::default() },
    ])
    .skip_duplicates()
    .exec()
    .await?;`;
---

<DocsLayout title="Upsert & Conflict Resolution - Prax ORM">
  <article class="max-w-4xl mx-auto px-6 py-12">
    <header class="mb-12">
      <h1 class="text-4xl font-bold mb-4">Upsert & Conflict Resolution</h1>
      <p class="text-xl text-muted">
        Insert or update records atomically with conflict handling across all databases.
      </p>
    </header>

    <div class="space-y-12">
      <!-- Introduction -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Overview</h2>
        <p class="text-muted mb-4">
          Upsert operations insert a new record or update an existing one based on conflict detection.
          Each database has its own syntax, but Prax provides a unified API.
        </p>
        <div class="overflow-x-auto">
          <table class="w-full text-sm">
            <thead>
              <tr class="border-b border-border">
                <th class="text-left py-3 px-4 font-semibold">Database</th>
                <th class="text-left py-3 px-4 font-semibold">Syntax</th>
                <th class="text-left py-3 px-4 font-semibold">Features</th>
              </tr>
            </thead>
            <tbody class="text-muted">
              <tr class="border-b border-border">
                <td class="py-3 px-4">PostgreSQL</td>
                <td class="py-3 px-4"><code>ON CONFLICT DO UPDATE/NOTHING</code></td>
                <td class="py-3 px-4">Column/constraint targets, WHERE, EXCLUDED</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4">MySQL</td>
                <td class="py-3 px-4"><code>ON DUPLICATE KEY UPDATE</code></td>
                <td class="py-3 px-4">VALUES(), INSERT IGNORE</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4">SQLite</td>
                <td class="py-3 px-4"><code>ON CONFLICT DO UPDATE</code></td>
                <td class="py-3 px-4">Column targets, excluded.column</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4">MSSQL</td>
                <td class="py-3 px-4"><code>MERGE INTO...WHEN MATCHED</code></td>
                <td class="py-3 px-4">Update/delete/insert, OUTPUT</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4">MongoDB</td>
                <td class="py-3 px-4"><code>updateOne(&#123; upsert: true &#125;)</code></td>
                <td class="py-3 px-4">$setOnInsert, bulkWrite</td>
              </tr>
            </tbody>
          </table>
        </div>
      </section>

      <!-- Basic Upsert -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Basic Upsert</h2>
        <p class="text-muted mb-4">
          Insert a record, or update if a conflict occurs on the specified columns.
        </p>
        <CodeBlock code={basicUpsert} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Do Nothing -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Insert or Ignore</h2>
        <p class="text-muted mb-4">
          Skip insertion if a conflict occurs without throwing an error.
        </p>
        <CodeBlock code={doNothing} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Conflict Targets -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Conflict Targets</h2>
        <p class="text-muted mb-4">
          Specify what constitutes a conflict: columns, constraints, or expressions.
        </p>
        <CodeBlock code={conflictTargets} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Conditional Update -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Conditional Updates</h2>
        <p class="text-muted mb-4">
          Only update when certain conditions are met, or use custom expressions.
        </p>
        <CodeBlock code={conditionalUpdate} lang="rust" filename="src/main.rs" />
      </section>

      <!-- MySQL -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">MySQL ON DUPLICATE KEY</h2>
        <p class="text-muted mb-4">
          MySQL's upsert syntax uses <code class="px-2 py-1 bg-surface-elevated rounded">ON DUPLICATE KEY UPDATE</code>.
        </p>
        <CodeBlock code={mysqlUpsert} lang="rust" filename="src/main.rs" />
      </section>

      <!-- MSSQL -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">MSSQL MERGE Statement</h2>
        <p class="text-muted mb-4">
          MSSQL uses the powerful MERGE statement for upsert operations.
        </p>
        <CodeBlock code={mssqlMerge} lang="rust" filename="src/main.rs" />
      </section>

      <!-- MongoDB -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">MongoDB Upsert</h2>
        <p class="text-muted mb-4">
          MongoDB's native upsert with updateOne and bulkWrite.
        </p>
        <CodeBlock code={mongoUpsert} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Bulk Upsert -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Bulk Upsert</h2>
        <p class="text-muted mb-4">
          <code class="px-2 py-1 bg-surface-elevated rounded">BulkUpsert</code> is MongoDB-only;
          for SQL backends, build one <code class="px-2 py-1 bg-surface-elevated rounded">Upsert</code>
          per row and execute the statements in a transaction.
        </p>
        <CodeBlock code={bulkUpsert} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Prax API -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Prax Fluent API</h2>
        <p class="text-muted mb-4">
          Use Prax's type-safe upsert methods on model clients.
        </p>
        <CodeBlock code={praxUpsert} lang="rust" filename="src/main.rs" />
      </section>

      <!-- Best Practices -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Best Practices</h2>
        <div class="grid gap-4">
          <div class="p-4 rounded-xl bg-surface border border-border">
            <h4 class="font-semibold mb-2 text-success-400">Use Unique Constraints</h4>
            <p class="text-muted text-sm">
              Upsert relies on unique constraints or indexes. Ensure your conflict target
              columns have proper constraints defined.
            </p>
          </div>
          <div class="p-4 rounded-xl bg-surface border border-border">
            <h4 class="font-semibold mb-2 text-success-400">Consider Race Conditions</h4>
            <p class="text-muted text-sm">
              Upsert is atomic, but concurrent upserts on different columns may still
              conflict. Design your conflict targets carefully.
            </p>
          </div>
          <div class="p-4 rounded-xl bg-surface border border-border">
            <h4 class="font-semibold mb-2 text-warning-400">Avoid Upsert for Large Batches</h4>
            <p class="text-muted text-sm">
              For very large batches, consider staging tables with a separate merge step
              for better performance and control.
            </p>
          </div>
          <div class="p-4 rounded-xl bg-surface border border-border">
            <h4 class="font-semibold mb-2 text-info-400">Use RETURNING/OUTPUT</h4>
            <p class="text-muted text-sm">
              Get back the inserted/updated rows to know which operation occurred
              and to retrieve generated values like IDs.
            </p>
          </div>
        </div>
      </section>
    </div>
  </article>
</DocsLayout>