mirror of https://github.com/VinGarcia/ksql.git
Update README to describe the new interface
parent
c0d7206dcc
commit
cc4e73dc62
34
README.md
34
README.md
|
@ -70,9 +70,9 @@ it with as little functions as possible, so don't expect many additions:
|
|||
```go
|
||||
// SQLProvider describes the public behavior of this ORM
|
||||
type SQLProvider interface {
|
||||
Insert(ctx context.Context, record interface{}) error
|
||||
Update(ctx context.Context, record interface{}) error
|
||||
Delete(ctx context.Context, idsOrRecords ...interface{}) error
|
||||
Insert(ctx context.Context, table Table, record interface{}) error
|
||||
Update(ctx context.Context, table Table, record interface{}) error
|
||||
Delete(ctx context.Context, table Table, idsOrRecords ...interface{}) error
|
||||
|
||||
Query(ctx context.Context, records interface{}, query string, params ...interface{}) error
|
||||
QueryOne(ctx context.Context, record interface{}, query string, params ...interface{}) error
|
||||
|
@ -128,11 +128,14 @@ type Address struct {
|
|||
City string `json:"city"`
|
||||
}
|
||||
|
||||
// UsersTable informs ksql the name of the table and that it can
|
||||
// use the default value for the primary key column name: "id"
|
||||
var UsersTable = ksql.NewTable("users")
|
||||
|
||||
func main() {
|
||||
ctx := context.Background()
|
||||
db, err := ksql.New("sqlite3", "/tmp/hello.sqlite", ksql.Config{
|
||||
MaxOpenConns: 1,
|
||||
TableName: "users",
|
||||
})
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
|
@ -157,14 +160,14 @@ func main() {
|
|||
State: "MG",
|
||||
},
|
||||
}
|
||||
err = db.Insert(ctx, &alison)
|
||||
err = db.Insert(ctx, UsersTable, &alison)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
fmt.Println("Alison ID:", alison.ID)
|
||||
|
||||
// Inserting inline:
|
||||
err = db.Insert(ctx, &User{
|
||||
err = db.Insert(ctx, UsersTable, &User{
|
||||
Name: "Cristina",
|
||||
Age: 27,
|
||||
Address: Address{
|
||||
|
@ -176,7 +179,7 @@ func main() {
|
|||
}
|
||||
|
||||
// Deleting Alison:
|
||||
err = db.Delete(ctx, alison.ID)
|
||||
err = db.Delete(ctx, UsersTable, alison.ID)
|
||||
if err != nil {
|
||||
panic(err.Error())
|
||||
}
|
||||
|
@ -191,12 +194,12 @@ func main() {
|
|||
|
||||
// Updating all fields from Cristina:
|
||||
cris.Name = "Cris"
|
||||
err = db.Update(ctx, cris)
|
||||
err = db.Update(ctx, UsersTable, cris)
|
||||
|
||||
// Changing the age of Cristina but not touching any other fields:
|
||||
|
||||
// Partial update technique 1:
|
||||
err = db.Update(ctx, struct {
|
||||
err = db.Update(ctx, UsersTable, struct {
|
||||
ID int `ksql:"id"`
|
||||
Age int `ksql:"age"`
|
||||
}{ID: cris.ID, Age: 28})
|
||||
|
@ -205,7 +208,7 @@ func main() {
|
|||
}
|
||||
|
||||
// Partial update technique 2:
|
||||
err = db.Update(ctx, PartialUpdateUser{
|
||||
err = db.Update(ctx, UsersTable, PartialUpdateUser{
|
||||
ID: cris.ID,
|
||||
Age: nullable.Int(28),
|
||||
})
|
||||
|
@ -214,14 +217,14 @@ func main() {
|
|||
}
|
||||
|
||||
// Listing first 10 users from the database
|
||||
// (each time you run this example a new "Cristina" is created)
|
||||
// (each time you run this example a new Cristina is created)
|
||||
//
|
||||
// Note: Using this function it is recommended to set a LIMIT, since
|
||||
// not doing so can load too many users on your computer's memory
|
||||
// causing an "Out Of Memory Kill".
|
||||
// not doing so can load too many users on your computer's memory or
|
||||
// cause an Out Of Memory Kill.
|
||||
//
|
||||
// If you need to query very big numbers of users we recommend using
|
||||
// the `QueryChunks` function instead.
|
||||
// the `QueryChunks` function.
|
||||
var users []User
|
||||
err = db.Query(ctx, &users, "SELECT * FROM users LIMIT 10")
|
||||
if err != nil {
|
||||
|
@ -237,7 +240,7 @@ func main() {
|
|||
return err
|
||||
}
|
||||
|
||||
err = db.Update(ctx, PartialUpdateUser{
|
||||
err = db.Update(ctx, UsersTable, PartialUpdateUser{
|
||||
ID: cris2.ID,
|
||||
Age: nullable.Int(29),
|
||||
})
|
||||
|
@ -531,3 +534,4 @@ make test
|
|||
- Consider passing the cached structInfo as argument for all the functions that use it,
|
||||
so that we don't need to get it more than once in the same call.
|
||||
- Use a cache to store all queries after they are built
|
||||
- Preload the insert method for all dialects inside `ksql.NewTable()`
|
||||
|
|
Loading…
Reference in New Issue