Esempio n. 1
0
// GetByEmail retrieves a user by e-mail with OK indicator.
func (db *DynamoDB) GetByEmail(email string) (u *models.User, ok bool) {
	res, err := db.DB.Query(&dynamodb.QueryInput{
		// ConsistentRead: aws.Bool(true),
		TableName:              aws.String("users"),
		IndexName:              aws.String("Email"),
		Select:                 aws.String("ALL_ATTRIBUTES"),
		KeyConditionExpression: aws.String("Email = :Email"),
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":Email": {
				S: aws.String(email),
			},
		},
	})
	if err != nil {
		log.Printf("dynamodb: getbymail error: %v", err)
		return nil, false
	}

	var user models.User
	if err := dynamodbattribute.UnmarshalMap(res.Items[0], &user); err != nil {
		log.Printf("dynamodb: getbyid error: %v", err)
		return nil, false
	}

	return &user, true
}
Esempio n. 2
0
// GetByID retrieves a user by ID with OK indicator.
func (db *DynamoDB) GetByID(id string) (u *models.User, ok bool) {
	res, err := db.DB.GetItem(&dynamodb.GetItemInput{
		ConsistentRead: aws.Bool(true),
		TableName:      aws.String("users"),
		Key: map[string]*dynamodb.AttributeValue{
			"ID": {
				S: aws.String(id),
			},
		},
	})
	if err != nil {
		log.Printf("dynamodb: error: %v", err)
		return nil, false
	}
	if len(res.Item) == 0 {
		return nil, false
	}

	var user models.User
	if err := dynamodbattribute.UnmarshalMap(res.Item, &user); err != nil {
		log.Printf("dynamodb: getbyid error: %v", err)
		return nil, false
	}

	return &user, true
}
Esempio n. 3
0
// Load loads all events for the aggregate id from the database.
// Returns ErrNoEventsFound if no events can be found.
func (s *EventStore) Load(id eh.UUID) ([]eh.Event, error) {
	params := &dynamodb.QueryInput{
		TableName:              aws.String(s.config.Table),
		KeyConditionExpression: aws.String("AggregateID = :id"),
		ExpressionAttributeValues: map[string]*dynamodb.AttributeValue{
			":id": {S: aws.String(id.String())},
		},
		ConsistentRead: aws.Bool(true),
	}
	resp, err := s.service.Query(params)
	if err != nil {
		return nil, err
	}

	if len(resp.Items) == 0 {
		return []eh.Event{}, nil
	}

	eventRecords := make([]*eventRecord, len(resp.Items))
	for i, item := range resp.Items {
		record := &eventRecord{}
		if err := dynamodbattribute.UnmarshalMap(item, record); err != nil {
			return nil, err
		}
		eventRecords[i] = record
	}

	events := make([]eh.Event, len(eventRecords))
	for i, record := range eventRecords {
		// Create an event of the correct type.
		event, err := eh.CreateEvent(record.EventType)
		if err != nil {
			return nil, err
		}

		if err := dynamodbattribute.UnmarshalMap(record.Payload, event); err != nil {
			// 	return nil, ErrCouldNotUnmarshalEvent
			return nil, err
		}
		events[i] = event
	}

	return events, nil
}
Esempio n. 4
0
File: main.go Progetto: apex/go-apex
func main() {
	dynamo.HandleFunc(func(event *dynamo.Event, ctx *apex.Context) error {
		// Iterate all event records
		for _, record := range event.Records {

			// only act on INSERTs
			if record.EventName == "INSERT" {

				n := newImage{}
				// Unmarshal the data for NewImage
				dynamodbattribute.UnmarshalMap(record.Dynamodb.NewImage, &n)

				// Print the example attribute. (Don't do this in your function!)
				fmt.Println(n.ExampleKey)
			}
		}

		return nil
	})
}