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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
---
import DocsLayout from '../../layouts/DocsLayout.astro';
import CodeBlock from '../../components/CodeBlock.astro';

const basicUsage = `# Auto-detect seed file and run
prax db seed

# Specify seed file explicitly
prax db seed --seed-file ./my-seed.sql

# Reset before seeding (JSON/TOML seeds only: truncates the tables
# declared in the seed file; .rs/.sql seeds error with
# "--reset requires table metadata")
prax db seed --reset

# Specify environment (development, staging, production)
prax db seed --environment staging

# Force seeding in restricted environments
prax db seed --environment production --force`;

const configToml = `# prax.toml - Seed configuration
[seed]
# Path to seed file (supports .rs, .sql, .json, .toml)
script = "./seed.rs"

# Automatically run seed after migrations
auto_seed = false

# Per-environment seeding controls
[seed.environments]
development = true    # ✓ Enabled
test = true           # ✓ Enabled
staging = false       # ✗ Disabled
production = false    # ✗ Disabled (protected!)`;

const sqlSeedExample = `-- seed.sql - SQL seed file
-- Clear existing data (optional)
-- TRUNCATE TABLE users, posts CASCADE;

-- Insert users
INSERT INTO users (id, email, name, role, created_at) VALUES
    (1, 'admin@example.com', 'Admin User', 'ADMIN', CURRENT_TIMESTAMP),
    (2, 'john@example.com', 'John Doe', 'USER', CURRENT_TIMESTAMP),
    (3, 'jane@example.com', 'Jane Smith', 'USER', CURRENT_TIMESTAMP)
ON CONFLICT (id) DO NOTHING;

-- Insert posts
INSERT INTO posts (id, title, content, published, author_id) VALUES
    (1, 'Welcome to Prax', 'First post!', true, 1),
    (2, 'Getting Started', 'Learn Prax.', true, 1)
ON CONFLICT (id) DO NOTHING;`;

const jsonSeedExample = `{
  "truncate": false,
  "disable_fk_checks": true,
  "order": ["users", "posts", "comments"],
  "tables": {
    "users": [
      {
        "id": 1,
        "email": "admin@example.com",
        "name": "Admin User",
        "role": "ADMIN",
        "created_at": "now()"
      },
      {
        "id": 2,
        "email": "john@example.com",
        "name": "John Doe",
        "role": "USER",
        "created_at": "now()"
      }
    ],
    "posts": [
      {
        "id": 1,
        "title": "Welcome to Prax",
        "content": "First post using Prax ORM!",
        "published": true,
        "author_id": 1,
        "created_at": "now()"
      }
    ]
  }
}`;

const tomlSeedExample = `# seed.toml - TOML seed file
truncate = false
disable_fk_checks = true
order = ["users", "posts"]

[[tables.users]]
id = 1
email = "admin@example.com"
name = "Admin User"
role = "ADMIN"
created_at = "now()"

[[tables.users]]
id = 2
email = "john@example.com"
name = "John Doe"
role = "USER"
created_at = "now()"

[[tables.posts]]
id = 1
title = "Welcome to Prax"
content = "First post!"
published = true
author_id = 1
created_at = "now()"`;

const rustSeedExample = `// seed.rs - Rust seed script
use prax_orm::PraxClient;
use prax_postgres::{PgEngine, PgPool};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize Prax client (synchronous constructor, takes an engine)
    let pool = PgPool::builder()
        .url(std::env::var("DATABASE_URL")?)
        .build()
        .await?;
    let client = PraxClient::new(PgEngine::new(pool));

    println!("🌱 Seeding database...");

    // Create admin user (zero-arg create() with .set(...) chaining)
    let admin = client.user().create()
        .set("email", "admin@example.com")
        .set("name", "Admin User")
        .set("role", "ADMIN")
        .exec().await?;
    println!("  Created admin user: {}", admin.email);

    // Create sample posts
    for i in 1..=5 {
        client.post().create()
            .set("title", format!("Sample Post {}", i))
            .set("author_id", admin.id)
            .set("content", format!("Content for post {}", i))
            .set("published", i % 2 == 0)
            .exec().await?;
    }
    println!("  Created 5 sample posts");

    println!("✅ Database seeded successfully!");
    Ok(())
}`;

const cargoTomlBin = `# Cargo.toml - Add seed binary
[[bin]]
name = "seed"
path = "seed.rs"`;

const migrateIntegration = `# v0.11: the migrate commands do NOT seed.
#  - "prax migrate dev" without --create-only errors at the apply step
#    ("not yet implemented") before any seeding would happen
#  - "prax migrate dev --create-only" skips the seed step entirely
#  - "prax migrate reset" always errors ("not yet implemented")

# Seeding runs standalone:
prax db seed`;

const fileLocations = `# Prax looks for seed files in these locations (in order):
# 1. Configured in prax.toml [seed].script
# 2. ./seed.rs, ./seed.sql, ./seed.json, ./seed.toml
# 3. ./prax/seed.rs, ./prax/seed.sql, ...
# 4. ./prisma/seed.rs (Prisma compatibility)
# 5. ./src/seed.rs
# 6. ./seeds/seed.rs, ./seeds/seed.sql`;

const specialFunctions = `# Special functions in JSON/TOML seed files:
# now()  - Current timestamp
# uuid() - Generate UUID v4

# Example:
{
  "created_at": "now()",
  "id": "uuid()"
}

# Translates to:
# PostgreSQL: CURRENT_TIMESTAMP, gen_random_uuid()
# MySQL:      NOW(), UUID()
# SQLite:     datetime('now'), '<generated-uuid>'`;
---

<DocsLayout title="Database Seeding - 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">Database Seeding</h1>
      <p class="text-xl text-muted">
        Populate your database with initial or test data.
      </p>
    </header>

    <div class="space-y-12">
      <!-- Overview -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Overview</h2>
        <p class="text-muted mb-4">
          Prax provides flexible database seeding with support for multiple file formats.
          Use seeding to populate your database with initial data during development,
          create test fixtures, or set up demo environments.
        </p>

        <!-- Features -->
        <div class="grid md:grid-cols-2 gap-4 mb-6">
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <div class="flex items-center gap-2 mb-2">
              <svg class="w-5 h-5 text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
              </svg>
              <span class="font-medium">Multiple Formats</span>
            </div>
            <p class="text-sm text-muted">.rs, .sql, .json, and .toml seed files</p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <div class="flex items-center gap-2 mb-2">
              <svg class="w-5 h-5 text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
              </svg>
              <span class="font-medium">Environment Protection</span>
            </div>
            <p class="text-sm text-muted">Prevent accidental production seeding</p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <div class="flex items-center gap-2 mb-2">
              <svg class="w-5 h-5 text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
              </svg>
              <span class="font-medium">Standalone Seeding</span>
            </div>
            <p class="text-sm text-muted">Run any time via prax db seed</p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <div class="flex items-center gap-2 mb-2">
              <svg class="w-5 h-5 text-primary-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
              </svg>
              <span class="font-medium">Special Functions</span>
            </div>
            <p class="text-sm text-muted">now(), uuid() in declarative formats</p>
          </div>
        </div>
      </section>

      <!-- Basic Usage -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Basic Usage</h2>
        <CodeBlock code={basicUsage} lang="bash" />
      </section>

      <!-- Configuration -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Configuration</h2>
        <p class="text-muted mb-4">
          Configure seeding in your <code class="text-primary-400">prax.toml</code> file:
        </p>
        <CodeBlock code={configToml} lang="toml" />

        <div class="mt-4 p-4 bg-yellow-500/10 border border-yellow-500/30 rounded-lg">
          <div class="flex items-start gap-3">
            <svg class="w-5 h-5 text-yellow-500 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
              <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z"/>
            </svg>
            <div>
              <p class="font-medium text-yellow-500">Production Protection</p>
              <p class="text-sm text-muted mt-1">
                By default, seeding is disabled for production and staging environments.
                Use <code class="text-primary-400">--force</code> to override, but be careful!
              </p>
            </div>
          </div>
        </div>
      </section>

      <!-- Supported Formats -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Supported Formats</h2>

        <div class="overflow-x-auto mb-6">
          <table class="w-full text-sm">
            <thead>
              <tr class="border-b border-border">
                <th class="text-left py-3 px-4 font-semibold">Format</th>
                <th class="text-left py-3 px-4 font-semibold">Extension</th>
                <th class="text-left py-3 px-4 font-semibold">Description</th>
                <th class="text-left py-3 px-4 font-semibold">Best For</th>
              </tr>
            </thead>
            <tbody class="text-muted">
              <tr class="border-b border-border">
                <td class="py-3 px-4"><strong>Rust</strong></td>
                <td class="py-3 px-4"><code class="text-primary-400">.rs</code></td>
                <td class="py-3 px-4">Full programmatic control with Prax client</td>
                <td class="py-3 px-4">Complex seeding logic, computed data</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><strong>SQL</strong></td>
                <td class="py-3 px-4"><code class="text-primary-400">.sql</code></td>
                <td class="py-3 px-4">Raw SQL statements executed directly</td>
                <td class="py-3 px-4">Simple data, database-specific features</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><strong>JSON</strong></td>
                <td class="py-3 px-4"><code class="text-primary-400">.json</code></td>
                <td class="py-3 px-4">Declarative data definition</td>
                <td class="py-3 px-4">Structured data, API compatibility</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><strong>TOML</strong></td>
                <td class="py-3 px-4"><code class="text-primary-400">.toml</code></td>
                <td class="py-3 px-4">Human-readable declarative format</td>
                <td class="py-3 px-4">Configuration-like seed data</td>
              </tr>
            </tbody>
          </table>
        </div>

        <!-- SQL Example -->
        <h3 class="text-xl font-semibold mb-3 mt-8">SQL Seed File</h3>
        <p class="text-muted mb-4">
          The simplest format - raw SQL executed against your database:
        </p>
        <CodeBlock code={sqlSeedExample} lang="sql" />

        <!-- JSON Example -->
        <h3 class="text-xl font-semibold mb-3 mt-8">JSON Seed File</h3>
        <p class="text-muted mb-4">
          Declarative format with table-based structure:
        </p>
        <CodeBlock code={jsonSeedExample} lang="json" />

        <!-- TOML Example -->
        <h3 class="text-xl font-semibold mb-3 mt-8">TOML Seed File</h3>
        <p class="text-muted mb-4">
          Human-readable alternative to JSON:
        </p>
        <CodeBlock code={tomlSeedExample} lang="toml" />

        <!-- Rust Example -->
        <h3 class="text-xl font-semibold mb-3 mt-8">Rust Seed Script</h3>
        <p class="text-muted mb-4">
          Full programmatic control using the Prax client:
        </p>
        <CodeBlock code={rustSeedExample} lang="rust" />

        <p class="text-muted mt-4">
          Add a binary target to your <code class="text-primary-400">Cargo.toml</code>:
        </p>
        <CodeBlock code={cargoTomlBin} lang="toml" />
      </section>

      <!-- Special Functions -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Special Functions</h2>
        <p class="text-muted mb-4">
          JSON and TOML seed files support special functions for dynamic values:
        </p>
        <CodeBlock code={specialFunctions} lang="text" />
      </section>

      <!-- Migration Integration -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Migration Integration</h2>
        <p class="text-muted mb-4">
          In v0.11, seeding runs standalone via <code class="text-primary-400">prax db seed</code> —
          the migrate commands currently neither apply migrations nor seed:
        </p>
        <CodeBlock code={migrateIntegration} lang="bash" />
      </section>

      <!-- File Locations -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Seed File Locations</h2>
        <p class="text-muted mb-4">
          Prax automatically searches for seed files in common locations:
        </p>
        <CodeBlock code={fileLocations} lang="text" />
      </section>

      <!-- Commands Reference -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Commands Reference</h2>
        <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">Command</th>
                <th class="text-left py-3 px-4 font-semibold">Description</th>
              </tr>
            </thead>
            <tbody class="text-muted">
              <tr class="border-b border-border">
                <td class="py-3 px-4"><code class="text-primary-400">prax db seed</code></td>
                <td class="py-3 px-4">Run the seed file</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><code class="text-primary-400">prax db seed --seed-file &lt;path&gt;</code></td>
                <td class="py-3 px-4">Run a specific seed file</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><code class="text-primary-400">prax db seed --reset</code></td>
                <td class="py-3 px-4">Truncate the tables declared in a JSON/TOML seed file before seeding (.rs/.sql seeds error: "--reset requires table metadata")</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><code class="text-primary-400">prax db seed --environment &lt;env&gt;</code></td>
                <td class="py-3 px-4">Seed for specific environment</td>
              </tr>
              <tr class="border-b border-border">
                <td class="py-3 px-4"><code class="text-primary-400">prax db seed --force</code></td>
                <td class="py-3 px-4">Force seeding even if disabled for environment</td>
              </tr>
            </tbody>
          </table>
        </div>
      </section>

      <!-- Best Practices -->
      <section>
        <h2 class="text-2xl font-semibold mb-4">Best Practices</h2>
        <div class="space-y-4">
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <h3 class="font-medium mb-2">🎯 Use idempotent seeds</h3>
            <p class="text-sm text-muted">
              Write seeds that can run multiple times safely. Use <code class="text-primary-400">ON CONFLICT DO NOTHING</code>
              in SQL or check for existing records in Rust.
            </p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <h3 class="font-medium mb-2">🔒 Protect production</h3>
            <p class="text-sm text-muted">
              Keep <code class="text-primary-400">production = false</code> in your seed configuration.
              Never seed production accidentally.
            </p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <h3 class="font-medium mb-2">📦 Use Rust for complex logic</h3>
            <p class="text-sm text-muted">
              When you need computed values, relationships, or conditional logic,
              use a Rust seed script for full control.
            </p>
          </div>
          <div class="p-4 bg-surface-elevated rounded-lg border border-border">
            <h3 class="font-medium mb-2">📋 Specify table order</h3>
            <p class="text-sm text-muted">
              In JSON/TOML files, use the <code class="text-primary-400">order</code> array
              to ensure tables are seeded in the correct order for foreign key constraints.
            </p>
          </div>
        </div>
      </section>

      <!-- Next Steps -->
      <section class="mt-12 p-6 bg-surface-elevated rounded-lg border border-border">
        <h2 class="text-xl font-semibold mb-4">Next Steps</h2>
        <ul class="space-y-2">
          <li>
            <a href="/database/migrations" class="text-primary-400 hover:underline">
              → Learn about migrations
            </a>
          </li>
          <li>
            <a href="/queries/crud" class="text-primary-400 hover:underline">
              → CRUD operations for seeding
            </a>
          </li>
          <li>
            <a href="/configuration" class="text-primary-400 hover:underline">
              → Full configuration reference
            </a>
          </li>
        </ul>
      </section>
    </div>
  </article>
</DocsLayout>