Created
June 23, 2021 23:02
-
-
Save thecoolwinter/e6f8d45852acb2fe45f7fb79a0f8dc23 to your computer and use it in GitHub Desktop.
A small extension for easily adding timestamps to models with one line in a migration. You can customize the fieldnames as well. By default adds `createdAt`, `updatedAt` and `deletedAt`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Fluent | |
extension SchemaBuilder { | |
enum TimestampTypes: Equatable { | |
case createdAt(_ fieldName: String = "createdAt") | |
case updatedAt(_ fieldName: String = "updatedAt") | |
case deletedAt(_ fieldName: String = "deletedAt") | |
} | |
func addTimestamps(_ types: [TimestampTypes] = [.createdAt(), .updatedAt(), .deletedAt()]) -> SchemaBuilder { | |
var value = self | |
for type in types { | |
switch type { | |
case .createdAt(let fieldName): | |
value = value.field(fieldName, .datetime) | |
case .updatedAt(let fieldName): | |
value = value.field(fieldName, .datetime) | |
case .deletedAt(let fieldName): | |
value = value.field(fieldName, .datetime) | |
} | |
} | |
return value | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use like so: