DynamoDB的put item方法没有填充所有参数。

huangapple go评论66阅读模式
英文:

dynamoDb put item not populating all params

问题

我有两个lambda函数,它们执行完全相同的操作,但是它们使用不同的语言编写。

第一个lambda函数在node.js环境中运行,当我创建putItem的参数时,如下所示:

const args = {
    id: "my id",
    __typename: "a type name",
    _version: 1,
    _lastChangedAt: now.toISOString(),
    createdAt: now.toISOString(),
    updatedAt: fields.LastModifiedDate
}
var recParams = {
    TableName: dynamoTable,
    Key: {
        "id": Id
    },
    Item: args,
    ReturnValues: "ALL_OLD"
};

然后我使用docClient插入行。一切正常,所有属性都被填充到我的dynamo行中。

我在Golang中也有完全相同的代码:

item := RecentItem{
    Id:             "some Id",
    _version:       1,
    __typename:     "a type name",
    _lastChangedAt: currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
    createdAt:      currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
    updatedAt:      currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
}
av, err := dynamodbattribute.MarshalMap(item)
input := &dynamodb.PutItemInput{
    Item:      av,
    TableName: aws.String(tableName),
}

几乎一切正常,项目被插入,但是除了id之外,我缺少所有其他属性。

结构声明:

type RecentItem struct {
    Id             string `json:"id"`
    _version       int    `json:"_version"`
    _lastChangedAt string `json:"_lastChangedAt"`
    createdAt      string `json:"createdAt"`
    updatedAt      string `json:"updatedAt"`
}

不确定为什么在Go中我的dynamoDb行缺少属性。我是否漏掉了什么?

英文:

I have 2 lambdas that do the exact same thing, however, they are both written using different langages.

1st lambda - runs on a node.js environment, when I create my arguments to putItem, as follows:

const args = {
          id: "my id",
          __typename: "a type name",
          _version: 1,
          _lastChangedAt: now.toISOString(),
          createdAt: now.toISOString(),
          updatedAt: fields.LastModifiedDate
        }
var recParams = {
        TableName: dynamoTable,
        Key: {
        "id": Id
        },
        Item: args,
        ReturnValues: "ALL_OLD"
      };

and then I use the docClient to insert the row. Everything works fine, all the properties are populated in my dynamo row.

I have the exact same written in Golang:

    item := RecentItem{
    			Id:             "some Id",
    			_version:       1,
                __typename: "a type name",
    			_lastChangedAt: currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
    			createdAt:      currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
    			updatedAt:      currentTime.UTC().Format("2006-01-02T15:04:05-0700"),
    		}
av, err := dynamodbattribute.MarshalMap(item)
input := &dynamodb.PutItemInput{
		Item:      av,
		TableName: aws.String(tableName),
	}

Everything ALMOST works, the item is inserted, but I am missing all the properties except for the id.

Structure declaration :

type RecentItem struct {
	Id             string `json:"id"`
	_version       int    `json:"_version"`
	_lastChangedAt string `json:"_lastChangedAt"`
	createdAt      string `json:"createdAt"`
	updatedAt      string `json:"updatedAt"`
}

Not sure why in Go my dynamoDb row is missing properties. Am I missing something?

答案1

得分: 4

除了Id之外的属性必须被导出,即以大写字母开头:

type RecentItem struct {
    ID             string `dynamodbav:"id"`
    Version        int    `dynamodbav:"_version"`
    LastChangedAt  string `dynamodbav:"_lastChangedAt"`
    CreatedAt      string `dynamodbav:"createdAt"`
    UpdatedAt      string `dynamodbav:"updatedAt"`
}
英文:

Properties other than Id must be exported, i.e, started with an Upper case:



type RecentItem struct {
    ID             string `dynamodbav:"id"`
    Version        int    `dynamodbav:"_version"`
    LastChangedAt  string `dynamodbav:"_lastChangedAt"`
    CreatedAt      string `dynamodbav:"createdAt"`
    UpdatedAt      string `dynamodbav:"updatedAt"`
}

huangapple
  • 本文由 发表于 2022年1月29日 04:42:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/70900076.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定