KORM 是一个优雅且极其快速的 ORM,可满足您所有的并发和异步需求。受到广受欢迎的 Django 框架的启发,KORM 提供了类似的功能。
KORM 是一个优雅且极其快速的 ORM,可满足您所有的并发和异步需求。受到广受欢迎的 Django 框架的启发,KORM 提供了类似的功能。
New: When using korm.WithDashboard, now you have access to all logs in realtime (websockets) from admin dashboard when you log using lg pkg. By default only 10 last logs are keeped in memory, you can increase it using lg.SaveLogs(50) for keeping last 50 logs
New: Handle Nested or Embeded structs and slice of structs through joins, like sqlx, but sqlx doesn't handle slice of structs
New: korm.QueryNamed, QueryNamedS, korm.ExecNamed, korm.ExecContextNamed and WhereNamed(query string, args map[string]any) like :Where("email = :email",map[string]any{"email":"[email protected]"})
New: korm.LogsQueries() that log statements and time tooked by sql queries
Auto Docs with Model API and video tutoriel
Swagger Documentation and tutorial
PPROF Go profiling tool and Metrics Prometheus
Admin dashboard with ready offline and installable PWA (using /static/sw.js and /static/manifest.webmanifest). All statics mentionned in sw.js will be cached and served by the service worker, you can inspect the Network Tab in the browser to check it
Shared Network Bus allowing you to send and recv data in realtime using pubsub websockets between your ORMs, so you can decide how you data will be distributed between different databases, see Example
Built-in Authentication using korm.Auth , korm.Admin or korm.BasicAuth middlewares, whenever Auth and Admin middlewares are used, you get access to the .User model and variable .IsAuthenticated from any template html like this example admin_nav.html
Interactive Shell, to CRUD in your databases from command line, use korm.WithShell() , ⚠️ if dashboard used, korm.WithShell should be after WithDashboard
AutoMigrate directly from struct
Compatible with official database/sql, so you can do your queries yourself using sql.DB `korm.GetConnection()``, and overall a painless integration of your existing codebases using database/sql
Router/Mux accessible from the serverBus after calling korm.WithBus(...opts) or korm.WithDashboard(addr, ...opts)
Hooks : OnInsert OnSet OnDelete and OnDrop
many to many relationships
GENERATED ALWAYS AS tag added (all dialects)
Concatination and Length support for Where and for tags: check and generated (all dialects)
Support for foreign keys, indexes , checks,... See all
Kenv load env vars to struct
Python Bus Client pip install ksbus
go get -u github.com/kamalshkeir/korm@latestgo get -u github.com/kamalshkeir/sqlitedriver@latest
go get -u github.com/kamalshkeir/pgdriver@latest
go get -u github.com/kamalshkeir/mysqldriver@latest// Debug when true show extra useful logs for queries executed for migrations and queries statements
Debug = false
// FlushCacheEvery execute korm.FlushCache() every 10 min by default, you should not worry about it, but useful that you can change it
FlushCacheEvery = 10 * time.Minute
// SetCacheMaxMemory set max size of each cache cacheAllS AllM ...
korm.SetCacheMaxMemory(megaByte int) // default maximum of 50 Mb , cannot be lower
// Connection pool
MaxOpenConns = 20
MaxIdleConns = 20
MaxLifetime = 30 * time.Minute
MaxIdleTime = 15 * time.Minute// sqlite
// go get github.com/kamalshkeir/sqlitedriver
err := korm.New(korm.SQLITE, "dbName", sqlitedriver.Use()) // Connect
// postgres, cockroach
// go get github.com/kamalshkeir/pgdriver
err := korm.New(korm.POSTGRES,"dbName", pgdriver.Use(), "user:password@localhost:5432") // Connect
// mysql, maria
// go get github.com/kamalshkeir/mysqldriver
err := korm.New(korm.MYSQL,"dbName", mysqldriver.Use(), "user:password@localhost:3306") // Connect
korm.Shutdown(databasesName ...string) error…Available Tags (SQL)
SQL:
…map[string]any:…korm.PaginationPer = 10
korm.DocsUrl = "docs"
korm.EmbededDashboard = false
korm.MediaDir = "media"
korm.AssetsDir = "assets"
korm.StaticDir = path.Join(AssetsDir, "/", "static")
korm.TemplatesDir = path.Join(AssetsDir, "/", "templates")
korm.RepoUser = "kamalshkeir"
korm.RepoName = "korm-dash"
korm.adminPathNameGroup = "/admin" // korm.SetAdminPath("/another")
// so you can create a custom dashboard, upload it to your repos and change like like above korm.RepoUser and korm.RepoName…Then create admin user to connect to the dashboard
go run main.go shell
createsuperuserThen you can visit /admin
…{{define "admin_nav"}}
KORM
Home
Admin
{{if .IsAuthenticated}}
Logout
{{if .User.Email}}
Hello {{.User.Email}}
{{end}}
{{end}}
{{end}}
…KORM 1:
…KORM 2:
……// Where example
func TestConcatANDLen(t *testing.T) {
groupes, err := Model[Group]().Where("name = concat(?,'min') AND len(name) = ?", "ad", 5).Debug().All()
// translated to select * from groups WHERE name = 'ad' || 'min' AND length(name) = 5 (sqlite)
// translated to select * from groups WHERE name = concat('ad','min') AND char_length(name) = 5 (postgres, mysql)
if err != nil {
t.Error(err)
}
if len(groupes) != 1 || groupes[0].Name != "admin" {
t.Error("len(groupes) != 1 , got: ", groupes)
}
}Learn more about Ksmux
func main() {
err := korm.New(korm.SQLITE, "db", sqlitedriver.Use())
if err != nil {
log.Fatal(err)
}
serverBus := korm.WithDashboard("localhost:9313")
korm.WithShell() // ⚠️ if dashboard used, korm.WithShell should be after WithDashboard
mux := serverBus.App
// add global middlewares
mux.Use((midws ...func(http.Handler) http.Handler))
...
}
serverBus := korm.WithDashboard("localhost:9313")
// or srv := korm.WithBus()
serverBus.WithPprof(path ...string) // path is 'debug' by default
will enable:
- /debug/pprof
- /debug/profile
- /debug/heap
- /debug/traceTo execute profile cpu: go tool pprof -http=":8000" pprofbin http://localhost:9313/debug/profile?seconds=18
To execute profile memory: go tool pprof -http=":8000" pprofbin http://localhost:9313/debug/heap?seconds=18
To execute generate trace: go to endpoint http://localhost:9313/debug/trace?seconds=18 from browser , this will download the trace of 18 seconds
Then to see the trace : go tool trace path/to/trace
// or srv := korm.WithBus()
//srv.WithMetrics(httpHandler http.Handler, path ...string) path default to 'metrics'
srv.WithMetrics(promhttp.Handler())
will enable:
- /metrics// or srv := korm.WithBus()
//srv.WithMetrics(httpHandler http.Handler, path ...string) path default to 'metrics'
srv.App.Use(ksmux.Logs()) // it take an optional callback executed on each request if you want to add log to a file or send
srv.App.Use(ksmux.Logs(func(method, path, remote string, status int, took time.Duration) {
// save somewhere
}))
will enable:
- /metricskorm.OnInsert(func(database, table string, data map[string]any) error {
fmt.Println("inserting into", database, table, data)
// if error returned, it will not insert
return nil
})
korm.OnSet(func(database, table string, data map[string]any) error {
fmt.Println("set into", database, table, data)
return nil
})
korm.OnDelete(func(database, table, query string, args ...any) error {})
korm.OnDrop(func(database, table string) error {})pip install ksbus==1.1.0
# if it doesn't work , execute it again ……korm.DocsUrl = "docs" // default endpoint '/docs'
korm.BASIC_AUTH_USER = "test"
korm.BASIC_AUTH_PASS = "pass"
korm.WithDocs(generate, dirPath, korm.BasicAuth)
korm.WithDocs(true, "", korm.BasicAuth) // dirPath default to 'assets/static/docs'
korm.WithEmbededDocs(embeded embed.FS, dirPath, korm.BasicAuth)
// dirPath default to 'assets/static/docs' if empty………https://github.com/kamalshkeir/korm-vs-gorm-vs-tarantool-vs-pgx
goos: windows
goarch: amd64
pkg: github.com/kamalshkeir/korm/benchmarks
cpu: Intel(R) Core(TM) i5-7300HQ CPU @ 2.50GHzTo execute these benchmarks on your machine, very easy :
…Without parameter &n
暂无开放 Issues,或尚未同步最近议题。