ex_audit 开源项目教程

ex_audit 开源项目教程

ex_auditEcto auditing library that transparently tracks changes and can revert them.项目地址:https://gitcode.com/gh_mirrors/ex/ex_audit

项目介绍

ex_audit 是一个用于 Ecto 的审计库,能够透明地跟踪更改并可以还原这些更改。它适用于需要记录数据库变更历史的应用场景,支持自动跟踪和手动添加变更记录。

项目快速启动

安装依赖

首先,将 ex_audit 添加到你的项目依赖中:

def deps do
  [
    {:ex_audit, "~> 0.9"}
  ]
end

配置 Repo

在你的 Repo 模块中使用 ExAudit Repo:

defmodule MyApp.Repo do
  use Ecto.Repo, otp_app: :my_app, adapter: Ecto.Adapters.Postgres
  use ExAudit.Repo
end

配置跟踪

config/config.exs 中配置需要跟踪的 schema 和版本 schema:

config :ex_audit, ecto_repos: [MyApp.Repo],
  version_schema: MyApp.Version,
  tracked_schemas: [
    MyApp.Accounts.User,
    MyApp.BlogPost,
    MyApp.Comment
  ]

应用案例和最佳实践

自动跟踪变更

ex_audit 会自动跟踪配置中指定的 schema 的变更。例如,如果你更新了一个 MyApp.Accounts.User 记录,ex_audit 会自动记录这次变更。

手动添加变更

如果某些变更没有被自动跟踪,你可以手动添加:

ExAudit.Tracking.track_change(module, adapter, action, changeset, resulting_struct, opts)

处理特殊数据类型

你可以配置某些数据类型为“原始类型”,这样 ex_audit 会记录整个数据结构的变化,而不是深入到内部:

config :ex_audit, ecto_repos: [MyApp.Repo],
  version_schema: MyApp.Version,
  tracked_schemas: [
    MyApp.Accounts.User,
    MyApp.BlogPost,
    MyApp.Comment
  ],
  primitive_structs: [Date]

典型生态项目

ex_audit 通常与以下项目一起使用:

  • Ecto: 数据库库,用于与数据库交互。
  • Phoenix: Web 框架,用于构建 Web 应用。
  • Postgres: 数据库,ex_audit 支持 Postgres 数据库。

这些项目共同构成了一个强大的生态系统,适用于构建需要审计功能的复杂应用。

ex_auditEcto auditing library that transparently tracks changes and can revert them.项目地址:https://gitcode.com/gh_mirrors/ex/ex_audit

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

袁菲李

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值