base

package
v1.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 13, 2023 License: MIT Imports: 8 Imported by: 13

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

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 New

func New(d *deta.Deta, baseName string) (*Base, error)

New returns a pointer to a new Base

func (*Base) Delete

func (b *Base) Delete(key string) error

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) Get

func (b *Base) Get(key string, dest interface{}) error

Get an item from the database.

The item is scanned onto `dest`.

func (*Base) Insert

func (b *Base) Insert(item interface{}) (string, error)

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

func (b *Base) Put(item interface{}) (string, error)

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.

func (*Base) PutMany

func (b *Base) PutMany(items interface{}) ([]string, error)

PutMany puts multiple items in the database.

Puts at most 25 items in a single request. The items should be a slice. Each item in the slice is treated similarly as the input to the Put operation. Returns the slice of keys of the items put in the database.

func (*Base) Update

func (b *Base) Update(key string, updates Updates) error

Update an existing item in the database.

Updates according to the the provided 'updates'.

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

type Query

type Query []map[string]interface{}

Query is a datatype to provide a query to a Fetch operation.

Items in the query are ORed.

q := Query{
	{"active": true},
	{"age?lt": 32},
}

The example above fetches items where 'active' is true OR 'age' is less than 32.

type Updates

type Updates map[string]interface{}

Updates is a datatype to provide updates to an item in an Update operation

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL