Skip to content

Commit f98b95e

Browse files
committed
docs: add UUID functions to README
Documents new UUID function support in feature list and examples.
1 parent 466f158 commit f98b95e

File tree

1 file changed

+71
-2
lines changed

1 file changed

+71
-2
lines changed

README.md

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ Type-safe PostgreSQL query builder for Swift. Build complex SQL queries with com
1111
## Key Features
1212

1313
- 🔒 **Type-safe query building** with compile-time validation
14-
- 🚀 **PostgreSQL-native features**: JSONB, triggers, window functions, CTEs, full-text search
14+
- 🚀 **PostgreSQL-native features**: JSONB, triggers, window functions, CTEs, full-text search, UUID generation
1515
- 🔌 **Built for [swift-records](https://github.com/coenttb/swift-records)**: The swift Postgres database package
1616
-**Swift 6.1+** with strict concurrency
17-
- 🧪 **573 tests** with SQL snapshot testing
17+
- 🧪 **880 tests** with SQL snapshot testing
1818

1919
## Quick Start
2020

@@ -298,6 +298,68 @@ Product.createTrigger(
298298
// Intercepts DELETE and converts to UPDATE with timestamp
299299
```
300300

301+
### UUID Functions
302+
303+
**Server-side UUID generation:**
304+
305+
```swift
306+
@Table
307+
struct Event {
308+
let id: UUID
309+
var title: String
310+
var timestamp: Date
311+
}
312+
313+
// Random UUID (v4) - traditional approach
314+
Event.insert {
315+
Event.Columns(id: UUID.random, title: #sql("'Login'"))
316+
}
317+
// SQL: INSERT INTO "events" ("id", "title") VALUES (gen_random_uuid(), 'Login')
318+
319+
// Time-ordered UUID (v7) - better for indexes
320+
Event.insert {
321+
Event.Columns(id: UUID.timeOrdered, title: #sql("'Purchase'"))
322+
}
323+
// SQL: INSERT INTO "events" ("id", "title") VALUES (uuidv7(), 'Purchase')
324+
```
325+
326+
**Time-shifted UUIDs for backdating:**
327+
328+
```swift
329+
// Backdate events by shifting timestamp
330+
Event.insert {
331+
($0.id, $0.title)
332+
} values: {
333+
(UUID.timeOrdered(shift: "-1 day"), "Historical Event")
334+
(UUID.timeOrdered(shift: "-2 days"), "Earlier Event")
335+
}
336+
// SQL: INSERT INTO "events" ("id", "title")
337+
// VALUES (uuidv7('-1 day'::interval), 'Historical Event'),
338+
// (uuidv7('-2 days'::interval), 'Earlier Event')
339+
```
340+
341+
**Extract version and timestamp:**
342+
343+
```swift
344+
// Filter by UUID version
345+
Event.where { $0.id.extractVersion() == 7 }
346+
// SQL: WHERE uuid_extract_version("events"."id") = 7
347+
348+
// Extract embedded timestamp from UUIDv7
349+
Event.select { $0.id.extractTimestamp() }
350+
// SQL: SELECT uuid_extract_timestamp("events"."id") FROM "events"
351+
352+
// Order by UUID creation time
353+
Event.order(by: { $0.id.extractTimestamp() })
354+
// SQL: ORDER BY uuid_extract_timestamp("events"."id")
355+
```
356+
357+
**Why UUIDv7 over UUIDv4?**
358+
- ✅ Better B-tree index performance (sequential inserts)
359+
- ✅ Natural chronological ordering
360+
- ✅ Embedded timestamp - no separate `createdAt` column needed
361+
- ✅ Reduces index fragmentation
362+
301363
### Full-Text Search
302364

303365
**Basic search on text columns with `.match()`:**
@@ -420,6 +482,13 @@ User.insert { User(id: nil, name: "Alice") }
420482
- ✅ CURRENT_TIMESTAMP, NOW()
421483
- ✅ Interval arithmetic
422484

485+
**UUID Functions (Chapter 9.14):**
486+
- ✅ gen_random_uuid(), uuidv4() - Random UUIDs
487+
- ✅ uuidv7() - Time-ordered UUIDs for better index performance
488+
- ✅ uuidv7(interval) - Time-shifted UUIDs for backdating/scheduling
489+
- ✅ uuid_extract_version() - Extract version number (1-7)
490+
- ✅ uuid_extract_timestamp() - Extract creation timestamp from v1/v7
491+
423492
**Advanced Features:**
424493
- ✅ Common Table Expressions (CTEs)
425494
- ✅ Subqueries and EXISTS

0 commit comments

Comments
 (0)