Documentation ¶
Overview ¶
Package base is the Deta Base service package.
The following is a simple Put operation example.
import ( "fmt" "os" "time" "github.com/deta/deta-go/deta" "github.com/deta/deta-go/service/base" ) // User an example user struct type User struct { // json struct tag 'key' used to denote the key Key string `json:"key"` Username string `json:"username"` Active bool `json:"active"` Age int `json:"age"` Likes []string `json:"likes"` // json struct tag '__expires' for expiration timestamp // 'omitempty' to prevent default 0 value Expires int64 `json:"__expires,omitempty"` } func main() { // Create a new Deta instance with a project key d, err := deta.New(deta.WithProjectKey("project_key")) if err != nil { fmt.Fprintf(os.Stderr, "failed to create new Deta instance: %v\n", err) os.Exit(1) } // Create a new Base instance called "users", provide the previously created Deta instance users, err := base.New(d, "users") if err != nil { fmt.Fprintf(os.Stderr, "failed to create new Base instance: %v\n", err) os.Exit(1) } // Put "jimmy" to the "users" Base key, err := users.Put(&User{ Key: "jimmy_neutron", Username: "jimmy", Active: true, Age: 20, Likes: []string{"science"}, }) if err != nil { fmt.Fprintf(os.Stderr, "failed to put item: %v\n", err) os.Exit(1) } fmt.Printf("successfully put item with key %s\n", key) // A map can also be used jimmy := map[string]interface{}{ "key": "jimmy_neutron", "username": "jimmy", "active": true, "age": 20, "likes": []string{"science"}, } key, err = users.Put(jimmy) if err != nil { fmt.Fprintf(os.Stderr, "failed to put item: %v\n", err) os.Exit(1) } fmt.Printf("successfully put item with key: %s\n", key) // Put with expiration timestamp eu := &User{ Key: "tmp_user_key", Username: "test_user", Expires: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), } key, err = users.Put(eu) if err != nil { fmt.Fprintf(os.Stderr, "failed to put item: %v\n", err) os.Exit(1) } fmt.Printf("successfully put expiring item with key: %s\n", key) // Put map with expiration timestamp tmp := map[string]interface{}{ "key": "tmp_user_key", "username": "test_user", // use `__expires` as the key for expiration timestamp "__expires": time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC).Unix(), } key, err = users.Put(tmp) if err != nil { fmt.Fprintf(os.Stderr, "failed to put item: %v\n", err) os.Exit(1) } fmt.Printf("successfully put expiring item with key: %s\n", key) }
More examples and complete documentation on https://docs.deta.sh/docs/base/sdk
Index ¶
- type Base
- func (b *Base) Delete(key string) error
- func (b *Base) Fetch(i *FetchInput) (string, error)
- func (b *Base) Get(key string, dest interface{}) error
- func (b *Base) Insert(item interface{}) (string, error)
- func (b *Base) Put(item interface{}) (string, error)
- func (b *Base) PutMany(items interface{}) ([]string, error)
- func (b *Base) Update(key string, updates Updates) error
- type FetchInput
- type Query
- type Updates
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Base ¶
type Base struct { // base utilities Util *util // contains filtered or unexported fields }
Base is a Deta Base service client that offers the API to make requests to Deta Base
func (*Base) Delete ¶
Delete an item from the database.
If the key does not exist, a nil error is returned.
func (*Base) Fetch ¶
func (b *Base) Fetch(i *FetchInput) (string, error)
Fetch items from the database.
Fetch is paginated, returns the last key fetched if further pages are left. Provide the last key in the subsequent fetch operation to fetch remaining pages.
func (*Base) Insert ¶
Insert an item in the database only if an item with the same key does not exist.
The item is treated similarly as the input to the Put operation. Returns the key of the item inserted in the database.
func (*Base) Put ¶
Put an item in the database.
If the item is a struct, denote the key of the item with a json struct tag "key". If the item is a map, provide the key of the item in the map under "key". If an item with the same key already exists in the database, the existing item is overwritten. If the 'key' is provided in the item, a key is autogenerated. Returns the key of the item that was put in the database.
type FetchInput ¶
type FetchInput struct { // filters to apply to items // A nil value applies no queries and fetches all items Q Query // the destination to store the results Dest interface{} // the maximum number of items to fetch // value of 0 or less applies no limit Limit int // the last key evaluated in a paginated response // leave empty if not a subsequent fetch request LastKey string // descending order Desc bool }
FetchInput input to Fetch operation