Track model creations, updates and deletions.
Update your Package.swift
using Vapor toolbox's provider command.
vapor provider add nodes-vapor/audit
There are two primary interfaces for reporting a model event: the static interface through Audit
and the static interface through a type of Model
.
Audit provides an extension for all types conforming to Model
that allows them to report an event without having to manually pass in type information.
try Post.audit(user, event: .created, severity: .normal)
or with a custom event:
try Post.audit(user, event: "liked a post")
There may be a time where you have an enumeration of custom events and you would like to use their identifiers in your report. Auditor makes this possible, just call the overload with the parameter eventTypeId: UInt8
.
note: values 0-3 are used by Audit. Use the values 4-255 if you want you want to be able to differentiate your events from Audit's builtin events.
enum MyEvents: UInt8 {
// start at `4` so we can tell the difference between `.something` and `Event.created`
case something = 4
case another
}
try Post.audit(user, event: "did something with a post", eventTypeId: MyEvents.something.rawValue)
try Audit.report(user, itemType: Post.self, event: .deleted, severity: .danger)
A new instance of M
was created.
An instance of M
was updated.
An instance of M
was deleted.
For custom events, you have to define the message and the event's type's identifier.
note: values 0-3 are used by Audit. Use the values 4-255 if you want you want to be able to differentiate your events from Audit's builtin events.
note: values 0-4 are used by Audit. Use the values 5-255 if you want you want to be able to differentiate your severity levels from Audit's builtin levels.
By default the generated messages use the model's name
. If you wish to override this behaviour, you can conform to the protocol AuditCustomDescribable
.
extension Post: AuditCustomDescribable {
var auditDescription: String {
return "blog post"
}
}
try Post.audit(user, event: .created) // Brett created a/an blog post
In order to report events with your custom user model, you need to conform it to Author
. Author
is a simple protocol that just expects a name.
public protocol Author: Model {
var name: String { get }
}
This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Brett.
This package is open-sourced software licensed under the MIT license.