rok-fluent 0.4.1

Eloquent-inspired async ORM for Rust (PostgreSQL, MySQL, SQLite)
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
//! Table and column builder DSL.

// ── ColumnBuilder ─────────────────────────────────────────────────────────────

/// Fluent builder for a single SQL column definition.
#[derive(Debug, Clone)]
pub struct ColumnBuilder {
    name: String,
    type_sql: String,
    nullable: bool,
    default: Option<String>,
    unique: bool,
    primary_key: bool,
    references: Option<(String, String)>, // (table, column)
}

impl ColumnBuilder {
    pub(crate) fn new(name: impl Into<String>, type_sql: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            type_sql: type_sql.into(),
            nullable: true,
            default: None,
            unique: false,
            primary_key: false,
            references: None,
        }
    }

    /// Mark the column as `NOT NULL`.
    pub fn not_null(&mut self) -> &mut Self {
        self.nullable = false;
        self
    }

    /// Explicitly mark the column as nullable (the default — useful for documentation).
    pub fn nullable(&mut self) -> &mut Self {
        self.nullable = true;
        self
    }

    /// Set a default value (raw SQL expression, e.g. `"true"`, `"NOW()"`, `"'active'"`)
    pub fn default(&mut self, expr: impl Into<String>) -> &mut Self {
        self.default = Some(expr.into());
        self
    }

    /// Add a `UNIQUE` constraint.
    pub fn unique(&mut self) -> &mut Self {
        self.unique = true;
        self
    }

    /// Mark as `PRIMARY KEY`.
    pub fn primary_key(&mut self) -> &mut Self {
        self.primary_key = true;
        self
    }

    /// Add a `REFERENCES table(column)` foreign key constraint.
    pub fn references(&mut self, table: impl Into<String>, column: impl Into<String>) -> &mut Self {
        self.references = Some((table.into(), column.into()));
        self
    }

    /// Render the column definition to SQL.
    pub(crate) fn to_sql(&self) -> String {
        let mut parts = vec![format!("\"{}\" {}", self.name, self.type_sql)];
        if self.primary_key {
            parts.push("PRIMARY KEY".to_string());
        }
        if !self.nullable {
            parts.push("NOT NULL".to_string());
        }
        if self.unique {
            parts.push("UNIQUE".to_string());
        }
        if let Some(ref def) = self.default {
            parts.push(format!("DEFAULT {def}"));
        }
        if let Some((ref tbl, ref col)) = self.references {
            parts.push(format!("REFERENCES \"{tbl}\"(\"{col}\")"));
        }
        parts.join(" ")
    }
}

// ── TableBuilder ──────────────────────────────────────────────────────────────

/// Accumulates column definitions and index statements for a `CREATE TABLE`.
#[derive(Debug, Default)]
pub struct TableBuilder {
    pub(crate) columns: Vec<ColumnBuilder>,
    pub(crate) indices: Vec<String>,
}

impl TableBuilder {
    pub(crate) fn new() -> Self {
        Self::default()
    }

    // ── Convenience helpers ──────────────────────────────────────────────

    /// `id BIGSERIAL PRIMARY KEY` — auto-increment 64-bit integer PK.
    pub fn id(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "BIGSERIAL");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `uuid_id UUID PRIMARY KEY DEFAULT gen_random_uuid()`.
    pub fn uuid_id(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "UUID");
        col.primary_key();
        col.not_null();
        col.default("gen_random_uuid()");
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `id TEXT PRIMARY KEY NOT NULL` — ULID primary key (application-generated).
    pub fn ulid_pk(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "TEXT");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `id TEXT PRIMARY KEY NOT NULL` — CUID2 primary key (application-generated).
    pub fn cuid2_pk(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "TEXT");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `id BIGINT PRIMARY KEY NOT NULL` — Snowflake ID primary key.
    pub fn snowflake_pk(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "BIGINT");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `id VARCHAR(36) PRIMARY KEY NOT NULL` — UUID v7 primary key stored as string.
    pub fn uuid_v7_pk(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "VARCHAR(36)");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// `id TEXT PRIMARY KEY NOT NULL` — NanoID primary key (application-generated).
    pub fn nanoid_pk(&mut self) -> &mut ColumnBuilder {
        let mut col = ColumnBuilder::new("id", "TEXT");
        col.primary_key();
        col.not_null();
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// Add a `TEXT` column.
    pub fn string(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "TEXT"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `VARCHAR(n)` column.
    pub fn varchar(&mut self, name: impl Into<String>, len: u32) -> &mut ColumnBuilder {
        self.columns
            .push(ColumnBuilder::new(name, format!("VARCHAR({len})")));
        self.columns.last_mut().unwrap()
    }

    /// Add a `BIGINT` column.
    pub fn big_integer(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "BIGINT"));
        self.columns.last_mut().unwrap()
    }

    /// Add an `INTEGER` column.
    pub fn integer(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "INTEGER"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `SMALLINT` column.
    pub fn small_integer(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "SMALLINT"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `BOOLEAN` column.
    pub fn boolean(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "BOOLEAN"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `DOUBLE PRECISION` (f64) column.
    pub fn double(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns
            .push(ColumnBuilder::new(name, "DOUBLE PRECISION"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `DECIMAL(precision, scale)` column.
    pub fn decimal(
        &mut self,
        name: impl Into<String>,
        precision: u8,
        scale: u8,
    ) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(
            name,
            format!("DECIMAL({precision}, {scale})"),
        ));
        self.columns.last_mut().unwrap()
    }

    /// Add a `JSONB` column.
    pub fn json(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "JSONB"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `TIMESTAMPTZ` column.
    pub fn timestamp(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "TIMESTAMPTZ"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `DATE` column.
    pub fn date(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "DATE"));
        self.columns.last_mut().unwrap()
    }

    /// Add a `BYTEA` (binary) column.
    pub fn binary(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "BYTEA"));
        self.columns.last_mut().unwrap()
    }

    /// Add an enum column using a custom enum type.
    ///
    /// The enum type must already exist (create it via [`Schema::create_enum`]).
    ///
    /// [`Schema::create_enum`]: crate::migrate::Schema::create_enum
    pub fn enum_col(&mut self, name: impl Into<String>, enum_name: &str) -> &mut ColumnBuilder {
        self.columns
            .push(ColumnBuilder::new(name, enum_name.to_string()));
        self.columns.last_mut().unwrap()
    }

    /// Add a raw column definition (type specified manually).
    pub fn column(
        &mut self,
        name: impl Into<String>,
        type_sql: impl Into<String>,
    ) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, type_sql));
        self.columns.last_mut().unwrap()
    }

    // ── Extended types ────────────────────────────────────────────────────────

    /// `NUMERIC(19, 4)` — fixed-precision currency / monetary amount.
    pub fn money(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns
            .push(ColumnBuilder::new(name, "NUMERIC(19, 4)"));
        self.columns.last_mut().unwrap()
    }

    /// `INET` — IPv4 or IPv6 address (PostgreSQL).
    pub fn ip_address(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "INET"));
        self.columns.last_mut().unwrap()
    }

    /// `MACADDR` — MAC address (PostgreSQL).
    pub fn mac_address(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "MACADDR"));
        self.columns.last_mut().unwrap()
    }

    /// `BYTEA` — binary blob, suitable for storing application-layer encrypted values.
    pub fn encrypted(&mut self, name: impl Into<String>) -> &mut ColumnBuilder {
        self.columns.push(ColumnBuilder::new(name, "BYTEA"));
        self.columns.last_mut().unwrap()
    }

    /// `vector(dims)` — pgvector embedding column (requires the pgvector extension).
    pub fn vector(&mut self, name: impl Into<String>, dims: u32) -> &mut ColumnBuilder {
        self.columns
            .push(ColumnBuilder::new(name, format!("vector({dims})")));
        self.columns.last_mut().unwrap()
    }

    /// Add `BIGINT NOT NULL REFERENCES parent_table(id)` foreign key column.
    pub fn foreign_id(
        &mut self,
        name: impl Into<String>,
        references_table: impl Into<String>,
    ) -> &mut ColumnBuilder {
        let table = references_table.into();
        let mut col = ColumnBuilder::new(name, "BIGINT");
        col.not_null();
        col.references(&table, "id");
        self.columns.push(col);
        self.columns.last_mut().unwrap()
    }

    /// Add `created_at TIMESTAMPTZ` and `updated_at TIMESTAMPTZ` columns.
    pub fn timestamps(&mut self) {
        let mut c = ColumnBuilder::new("created_at", "TIMESTAMPTZ");
        c.not_null();
        c.default("NOW()");
        self.columns.push(c);
        let mut u = ColumnBuilder::new("updated_at", "TIMESTAMPTZ");
        u.not_null();
        u.default("NOW()");
        self.columns.push(u);
    }

    /// Add `deleted_at TIMESTAMPTZ NULL` (soft-delete column).
    pub fn soft_deletes(&mut self) {
        self.columns
            .push(ColumnBuilder::new("deleted_at", "TIMESTAMPTZ"));
    }

    /// Add a raw index statement (appended after column definitions).
    pub fn index(&mut self, index_sql: impl Into<String>) {
        self.indices.push(index_sql.into());
    }
}

// ── AlterColumnBuilder ────────────────────────────────────────────────────────

/// Closure argument for `AlterTableBuilder::change_column`.
///
/// Specify the new column type and/or nullability; unset fields are left unchanged.
pub struct AlterColumnBuilder {
    pub(crate) type_sql: Option<String>,
    pub(crate) nullable: Option<bool>,
}

impl AlterColumnBuilder {
    /// Change column type to `TEXT`.
    pub fn text(&mut self) -> &mut Self {
        self.type_sql = Some("TEXT".into());
        self
    }
    /// Change column type to `TEXT`.
    pub fn string(&mut self) -> &mut Self {
        self.type_sql = Some("TEXT".into());
        self
    }
    /// Change column type to `BIGINT`.
    pub fn big_integer(&mut self) -> &mut Self {
        self.type_sql = Some("BIGINT".into());
        self
    }
    /// Change column type to `INTEGER`.
    pub fn integer(&mut self) -> &mut Self {
        self.type_sql = Some("INTEGER".into());
        self
    }
    /// Change column type to `SMALLINT`.
    pub fn small_integer(&mut self) -> &mut Self {
        self.type_sql = Some("SMALLINT".into());
        self
    }
    /// Change column type to `BOOLEAN`.
    pub fn boolean(&mut self) -> &mut Self {
        self.type_sql = Some("BOOLEAN".into());
        self
    }
    /// Change column type to `DOUBLE PRECISION`.
    pub fn double(&mut self) -> &mut Self {
        self.type_sql = Some("DOUBLE PRECISION".into());
        self
    }
    /// Change column type to `JSONB`.
    pub fn json(&mut self) -> &mut Self {
        self.type_sql = Some("JSONB".into());
        self
    }
    /// Change column type to `TIMESTAMPTZ`.
    pub fn timestamp(&mut self) -> &mut Self {
        self.type_sql = Some("TIMESTAMPTZ".into());
        self
    }
    /// Change column type to `UUID`.
    pub fn uuid(&mut self) -> &mut Self {
        self.type_sql = Some("UUID".into());
        self
    }
    /// Change column type to `DECIMAL(p, s)`.
    pub fn decimal(&mut self, p: u8, s: u8) -> &mut Self {
        self.type_sql = Some(format!("DECIMAL({p}, {s})"));
        self
    }
    /// Set a raw SQL type string.
    pub fn type_sql(&mut self, sql: impl Into<String>) -> &mut Self {
        self.type_sql = Some(sql.into());
        self
    }
    /// Mark the column as nullable.
    pub fn nullable(&mut self) -> &mut Self {
        self.nullable = Some(true);
        self
    }
    /// Mark the column as `NOT NULL`.
    pub fn not_null(&mut self) -> &mut Self {
        self.nullable = Some(false);
        self
    }
}