From da8074954e901d6493ddb76c79626599f2349962 Mon Sep 17 00:00:00 2001 From: Josh Wright Date: Wed, 20 Apr 2022 15:21:11 -0700 Subject: [PATCH] Add randomOrSeed() to Seedable --- Sources/Alchemy/SQL/Database/Seeding/Seeder.swift | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Sources/Alchemy/SQL/Database/Seeding/Seeder.swift b/Sources/Alchemy/SQL/Database/Seeding/Seeder.swift index da7411be..a49c4cda 100644 --- a/Sources/Alchemy/SQL/Database/Seeding/Seeder.swift +++ b/Sources/Alchemy/SQL/Database/Seeding/Seeder.swift @@ -23,6 +23,14 @@ extension Seedable where Self: Model { return try await rows.insertReturnAll() } + + public static func randomOrSeed() async throws -> Self { + guard let random = try await random() else { + return try await seed() + } + + return random + } } extension Faker {