-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnosql_dynamodb.go
More file actions
95 lines (82 loc) · 3.51 KB
/
nosql_dynamodb.go
File metadata and controls
95 lines (82 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package module
import (
"context"
"fmt"
"github.com/GoCodeAlone/modular"
)
// DynamoDBNoSQLConfig holds configuration for the nosql.dynamodb module.
//
// Full AWS DynamoDB implementation would use:
// - github.com/aws/aws-sdk-go-v2/service/dynamodb
// - github.com/aws/aws-sdk-go-v2/feature/dynamodb/attributevalue
//
// When endpoint == "local" the module falls back to the in-memory backend,
// which is useful for local development and tests without a real DynamoDB endpoint.
type DynamoDBNoSQLConfig struct {
TableName string `json:"tableName" yaml:"tableName"`
Region string `json:"region" yaml:"region"`
Endpoint string `json:"endpoint" yaml:"endpoint"` // "local" => in-memory fallback
Credentials string `json:"credentials" yaml:"credentials"` // ref to cloud.account module name
}
// DynamoDBNoSQL is the nosql.dynamodb module.
// In local mode (endpoint: "local") it delegates to MemoryNoSQL.
// For a real AWS implementation, replace the backend field with a DynamoDB client
// and implement Get/Put/Delete/Query using dynamodb.GetItem, PutItem, DeleteItem, Scan.
type DynamoDBNoSQL struct {
name string
cfg DynamoDBNoSQLConfig
backend NoSQLStore // in-memory fallback when endpoint == "local"
}
// NewDynamoDBNoSQL creates a new DynamoDBNoSQL module.
func NewDynamoDBNoSQL(name string, cfg DynamoDBNoSQLConfig) *DynamoDBNoSQL {
return &DynamoDBNoSQL{name: name, cfg: cfg}
}
func (d *DynamoDBNoSQL) Name() string { return d.name }
func (d *DynamoDBNoSQL) Init(_ modular.Application) error {
if d.cfg.Endpoint == "local" || d.cfg.Endpoint == "" {
d.backend = NewMemoryNoSQL(d.name+"-mem", MemoryNoSQLConfig{Collection: d.cfg.TableName})
return nil
}
// Full AWS implementation:
// cfg, err := awsconfig.LoadDefaultConfig(ctx, awsconfig.WithRegion(d.cfg.Region))
// if err != nil { return err }
// if d.cfg.Endpoint != "" {
// cfg.EndpointResolverWithOptions = aws.EndpointResolverWithOptionsFunc(...)
// }
// d.client = dynamodb.NewFromConfig(cfg)
return fmt.Errorf("nosql.dynamodb %q: real AWS endpoint not yet implemented; use endpoint: local for testing", d.name)
}
func (d *DynamoDBNoSQL) ProvidesServices() []modular.ServiceProvider {
return []modular.ServiceProvider{
{Name: d.name, Description: "DynamoDB NoSQL store: " + d.name, Instance: d},
}
}
func (d *DynamoDBNoSQL) RequiresServices() []modular.ServiceDependency { return nil }
func (d *DynamoDBNoSQL) Get(ctx context.Context, key string) (map[string]any, error) {
if d.backend == nil {
return nil, fmt.Errorf("nosql.dynamodb %q: not initialized", d.name)
}
// Real implementation: dynamodb.GetItem with Key: {PK: {S: &key}}
return d.backend.Get(ctx, key)
}
func (d *DynamoDBNoSQL) Put(ctx context.Context, key string, item map[string]any) error {
if d.backend == nil {
return fmt.Errorf("nosql.dynamodb %q: not initialized", d.name)
}
// Real implementation: attributevalue.MarshalMap(item) then dynamodb.PutItem
return d.backend.Put(ctx, key, item)
}
func (d *DynamoDBNoSQL) Delete(ctx context.Context, key string) error {
if d.backend == nil {
return fmt.Errorf("nosql.dynamodb %q: not initialized", d.name)
}
// Real implementation: dynamodb.DeleteItem with Key: {PK: {S: &key}}
return d.backend.Delete(ctx, key)
}
func (d *DynamoDBNoSQL) Query(ctx context.Context, params map[string]any) ([]map[string]any, error) {
if d.backend == nil {
return nil, fmt.Errorf("nosql.dynamodb %q: not initialized", d.name)
}
// Real implementation: dynamodb.Scan or Query with FilterExpression
return d.backend.Query(ctx, params)
}