upd README.md
This commit is contained in:
49
test/example.go
Normal file
49
test/example.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
"code.uint32.ru/tiny/repo"
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
type My struct {
|
||||
A string
|
||||
B int
|
||||
}
|
||||
|
||||
func main() {
|
||||
db, err := sql.Open("sqlite3", "test.db")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
defer db.Close()
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// init repository for our type "My"
|
||||
r, err := repo.OpenOrCreate[My](ctx, db, "my_test_table")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
first := &My{
|
||||
A: "hello",
|
||||
B: 42,
|
||||
}
|
||||
|
||||
if err := r.Create(ctx, "first_record", first); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
duplicate, err := r.Read(ctx, "first_record")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
// prints Type: *main.My, Value: &{A:hello B:42}
|
||||
fmt.Printf("Type: %T, Value: %+v\n", duplicate, duplicate)
|
||||
}
|
Reference in New Issue
Block a user