Documentation
¶
Overview ¶
Example (Count) ¶
Example_count counts the ids matching a glob pattern.
package main
import (
"context"
"fmt"
"github.com/thalesfsp/dal/v2/memory"
"github.com/thalesfsp/params/v2/count"
"github.com/thalesfsp/params/v2/create"
)
// book is the document used throughout the examples below.
type book struct {
ID string `json:"id"`
Title string `json:"title"`
}
func main() {
ctx := context.Background()
s, err := memory.New(ctx)
if err != nil {
fmt.Println(err)
return
}
ids := []string{"book-dune", "book-hobbit", "movie-arrival"}
for _, id := range ids {
if _, err := s.Create(ctx, id, "", &book{ID: id}, &create.Create{}); err != nil {
fmt.Println(err)
return
}
}
n, err := s.Count(ctx, "", &count.Count{Search: "book-*"})
if err != nil {
fmt.Println(err)
return
}
fmt.Println(n)
}
Output: 2
Example (CreateAndRetrieve) ¶
Example_createAndRetrieve stores a document under an id and reads it back.
package main
import (
"context"
"fmt"
"github.com/thalesfsp/dal/v2/memory"
"github.com/thalesfsp/params/v2/create"
"github.com/thalesfsp/params/v2/retrieve"
)
// book is the document used throughout the examples below.
type book struct {
ID string `json:"id"`
Title string `json:"title"`
}
func main() {
ctx := context.Background()
s, err := memory.New(ctx)
if err != nil {
fmt.Println(err)
return
}
if _, err := s.Create(ctx, "book-dune", "", &book{ID: "book-dune", Title: "Dune"}, &create.Create{}); err != nil {
fmt.Println(err)
return
}
var got book
if err := s.Retrieve(ctx, "book-dune", "", &got, &retrieve.Retrieve{}); err != nil {
fmt.Println(err)
return
}
fmt.Println(got.ID, got.Title)
}
Output: book-dune Dune
Example (Delete) ¶
Example_delete removes a document, then shows that retrieving it fails with a not-found error.
package main
import (
"context"
"fmt"
"github.com/thalesfsp/dal/v2/memory"
"github.com/thalesfsp/params/v2/create"
"github.com/thalesfsp/params/v2/delete"
"github.com/thalesfsp/params/v2/retrieve"
)
// book is the document used throughout the examples below.
type book struct {
ID string `json:"id"`
Title string `json:"title"`
}
func main() {
ctx := context.Background()
s, err := memory.New(ctx)
if err != nil {
fmt.Println(err)
return
}
if _, err := s.Create(ctx, "book-dune", "", &book{ID: "book-dune", Title: "Dune"}, &create.Create{}); err != nil {
fmt.Println(err)
return
}
if err := s.Delete(ctx, "book-dune", "", &delete.Delete{}); err != nil {
fmt.Println(err)
return
}
var got book
err = s.Retrieve(ctx, "book-dune", "", &got, &retrieve.Retrieve{})
fmt.Println(err)
}
Output: retrieve not found
Example (List) ¶
Example_list shows that List's Search field is a glob matched against ids, not a value filter, and that results carry no guaranteed order, so callers wanting a stable order must sort the returned items themselves.
package main
import (
"context"
"fmt"
"sort"
"github.com/thalesfsp/dal/v2/memory"
"github.com/thalesfsp/params/v2/create"
"github.com/thalesfsp/params/v2/list"
)
// book is the document used throughout the examples below.
type book struct {
ID string `json:"id"`
Title string `json:"title"`
}
func main() {
ctx := context.Background()
s, err := memory.New(ctx)
if err != nil {
fmt.Println(err)
return
}
books := []*book{
{ID: "book-dune", Title: "Dune"},
{ID: "book-hobbit", Title: "The Hobbit"},
{ID: "movie-arrival", Title: "Arrival"},
}
for _, b := range books {
if _, err := s.Create(ctx, b.ID, "", b, &create.Create{}); err != nil {
fmt.Println(err)
return
}
}
var result memory.ResponseList[book]
if err := s.List(ctx, "", &result, &list.List{Search: "book-*"}); err != nil {
fmt.Println(err)
return
}
sort.Slice(result.Items, func(i, j int) bool {
return result.Items[i].ID < result.Items[j].ID
})
for _, item := range result.Items {
fmt.Println(item.ID, item.Title)
}
}
Output: book-dune Dune book-hobbit The Hobbit
Index ¶
- Constants
- func Get() storage.IStorage
- func Set(s storage.IStorage)
- type Memory
- func (s *Memory) Count(ctx context.Context, target string, prm *count.Count, ...) (int64, error)
- func (s *Memory) Create(ctx context.Context, id, target string, v any, prm *create.Create, ...) (string, error)
- func (s *Memory) Delete(ctx context.Context, id, target string, prm *delete.Delete, ...) error
- func (s *Memory) GetClient() any
- func (s *Memory) List(ctx context.Context, target string, v any, prm *list.List, ...) error
- func (s *Memory) Retrieve(ctx context.Context, id, target string, v any, prm *retrieve.Retrieve, ...) error
- func (s *Memory) Update(ctx context.Context, id, target string, v any, prm *update.Update, ...) error
- type ResponseList
Examples ¶
Constants ¶
const Name = "memory"
Name of the storage.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Memory ¶
type Memory struct {
*storage.Storage
// Target allows to set a static target. If it is empty, the target will be
// dynamic - the one set at the operation (count, create, delete, etc) time.
// Depending on the storage, target is a collection, a table, a bucket, etc.
// For ElasticSearch, for example it doesn't have a concept of a database -
// the target then is the index. Due to different cases of ElasticSearch
// usage, the target can be static or dynamic - defined at the index time,
// for example: log-{YYYY}-{MM}. For Memory, it isn't used at all.
Target string `json:"-" validate:"omitempty,gt=0"`
// contains filtered or unexported fields
}
Memory storage definition.
func (*Memory) Count ¶
func (s *Memory) Count(ctx context.Context, target string, prm *count.Count, options ...storage.Func[*count.Count]) (int64, error)
Count returns the number of items in the storage.
func (*Memory) Create ¶
func (s *Memory) Create(ctx context.Context, id, target string, v any, prm *create.Create, options ...storage.Func[*create.Create]) (string, error)
Create data.
NOTE: Not all storages returns the ID, neither all storages requires `id` to be set. You are better off setting the ID yourself.
func (*Memory) Delete ¶
func (s *Memory) Delete(ctx context.Context, id, target string, prm *delete.Delete, options ...storage.Func[*delete.Delete]) error
Delete removes data.
func (*Memory) List ¶
func (s *Memory) List(ctx context.Context, target string, v any, prm *list.List, options ...storage.Func[*list.List]) error
List data.
NOTE: It uses params.List.Search to query the data.
NOTE: Memory does not support the concept of "offset" and "limit" in the same way that a traditional SQL database does.
type ResponseList ¶
type ResponseList[T any] struct { Items []T `json:"items"` }
ResponseList is the list response.