tableDB/README.md

69 lines
1.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# apigo.cc/go/tableDB
`tableDB` 是建立在 `apigo.cc/go/db` 基础上的更高层抽象工具库。提供对于动态表、数据权限隔离和 Hook 的生命周期追踪管理。
## 使用指南
### 1. 初始化与 Auth
```go
package main
import (
"apigo.cc/go/tableDB"
"apigo.cc/go/log"
)
func main() {
app := tableDB.GetDB("sqlite://local.db", log.DefaultLogger)
// 在需要忽略权限的系统级后台执行时:
systemApp := app.Auth(tableDB.SystemUserID)
// 在特定的业务上下文执行时,将强制携带 creator 约束
userApp := app.Auth("user_12345")
}
```
### 2. DSL 动态表声明与同步
`TableDB` 提供底层隐藏表机制自动补全如 `id``autoIndex`,可用于初始化表结构。
```go
schema := `
== UserGroup ==
users SD
name v50 U
age i
`
_ = app.SyncSchema(schema)
```
### 3. 生命周期 Hooks 订阅
通过注册 Hooks我们可以在发生元数据变更或行更新时进行回调拦截
```go
app.Hooks.OnUpdatingRow = func(tableName string, row map[string]any) error {
if tableName == "users" {
row["modifiedAt"] = time.Now().Unix()
}
return nil
}
```
### 4. 动态表增删改查
底层调用与 `go/db` 使用习惯对齐,不同点是操作会被权限或 Hook 钩子影响:
```go
// 设定数据,如 ID 缺省会自动生成 c10 ID
_ = userApp.Table("users").Set(map[string]any{"name": "Alice"})
// 读取
record, _ := userApp.Table("users").Get("u1xxxx")
// 统计
count, _ := userApp.Table("users").Count(map[string]any{"age >": 20})
```