在使用Firestore时,你可能会疑惑是否需要额外的往返操作来读取创建和更新的时间戳字段。答案是不需要。Firestore会自动为每个文档提供创建和更新时间戳,你可以通过引用这些字段来获取相应的时间信息。这样,你就不需要额外的操作来读取时间戳字段,可以更便捷地获取到文档的创建和更新时间。这样的设计使得在开发过程中更加高效和简化,避免了不必要的代码和请求。
问题内容
-
好的,我在
go
中有一个rest api
,它使用firestore
存储ticket
资源。为此,我使用:firestore go client -
我希望能够通过
date 创建/更新日期
对我的文档进行排序,因此按照文档,我将这 2 个字段作为时间戳存储在文档中。 -
我在这两个字段上使用标签
servertimestamp
。通过这样做,该值应该是 firestore 服务器处理请求的时间。 -
更新操作的 http 响应应包含以下正文:
{ "ticket": { "id": "af41766e-76ea-43b5-86c1-8ba382edd4dc", "title": "ticket updated title", "price": 9, "date_created": "2023-01-06 09:07:24", "date_updated": "2023-01-06 10:08:24" } }
这意味着在我更新票证文档后,除了更新的 title 或 price
之外,我还需要更新 date_updated
字段的值。
目前可行,但我很好奇我编码的方式是否就是这样做的方式。正如您在代码示例中看到的,我使用事务来更新票证。除了再次读取更新的票证之外,我没有找到检索 dateupdated
字段的更新值的方法。
域实体定义如下:
package tixer import ( "context" "time" "github.com/google/uuid" ) type ( // ticketid represents a unique identifier for a ticket. // it's a domain type. ticketid uuid.uuid // ticket represents an individual ticket in the system. // it's a domain type. ticket struct { id ticketid title string price float64 datecreated time.time dateupdated time.time } )
我将在此处附加从创建和更新角度与 firestore 的通信:
// Storer persists tickets in Firestore. type Storer struct { client *firestore.Client } func NewStorer(client *firestore.Client) *Storer { return &Storer{client} } func (s *Storer) CreateTicket(ctx context.Context, ticket *tixer.Ticket) error { writeRes, err := s.client.Collection("tickets").Doc(ticket.ID.String()).Set(ctx, createTicket{ Title: ticket.Title, Price: ticket.Price, }) // In this case writeRes.UpdateTime is the time the document was created. ticket.DateCreated = writeRes.UpdateTime return err } func (s *Storer) UpdateTicket(ctx context.Context, ticket *tixer.Ticket) error { docRef := s.client.Collection("tickets").Doc(ticket.ID.String()) err := s.client.RunTransaction(ctx, func(ctx context.Context, tx *firestore.Transaction) error { doc, err := tx.Get(docRef) if err != nil { switch { case status.Code(err) == codes.NotFound: return tixer.ErrTicketNotFound default: return err } } var t persistedTicket if err := doc.DataTo(&t); err != nil { return err } t.ID = doc.Ref.ID if ticket.Title != "" { t.Title = ticket.Title } if ticket.Price != 0 { t.Price = ticket.Price } return tx.Set(docRef, updateTicket{ Title: t.Title, Price: t.Price, DateCreated: t.DateCreated, }) }) if err != nil { return err } updatedTicket, err := s.readTicket(ctx, ticket.ID) if err != nil { return err } *ticket = updatedTicket return nil } func (s *Storer) readTicket(ctx context.Context, id tixer.TicketID) (tixer.Ticket, error) { doc, err := s.client.Collection("tickets").Doc(id.String()).Get(ctx) if err != nil { switch { case status.Code(err) == codes.NotFound: return tixer.Ticket{}, tixer.ErrTicketNotFound default: return tixer.Ticket{}, err } } var t persistedTicket if err := doc.DataTo(&t); err != nil { return tixer.Ticket{}, err } t.ID = doc.Ref.ID return toDomainTicket(t), nil } type ( // persistedTicket represents a stored ticket in Firestore. persistedTicket struct { ID string `firestore:"id"` Title string `firestore:"title"` Price float64 `firestore:"price"` DateCreated time.Time `firestore:"dateCreated"` DateUpdated time.Time `firestore:"dateUpdate"` } // createTicket contains the data needed to create a Ticket in Firestore. createTicket struct { Title string `firestore:"title"` Price float64 `firestore:"price"` DateCreated time.Time `firestore:"dateCreated,serverTimestamp"` DateUpdated time.Time `firestore:"dateUpdate,serverTimestamp"` } // updateTicket contains the data needed to update a Ticket in Firestore. updateTicket struct { Title string `firestore:"title"` Price float64 `firestore:"price"` DateCreated time.Time `firestore:"dateCreated"` DateUpdated time.Time `firestore:"dateUpdate,serverTimestamp"` } ) func toDomainTicket(t persistedTicket) tixer.Ticket { return tixer.Ticket{ ID: tixer.TicketID(uuid.MustParse(t.ID)), Title: t.Title, Price: t.Price, DateCreated: t.DateCreated, DateUpdated: t.DateUpdated, } }
解决方法
如果我理解正确的话,DateUpdated
字段是一个服务器端时间戳,这意味着它的值是由服务器在将该值写入存储层时确定的(即所谓的字段转换)。由于 Firestore SDK 中的写入操作不会返回该操作的结果数据,因此将该值返回到应用程序中的唯一方法实际上是在写入后执行额外的读取操作来获取它。
SDK 不会自动执行此读取,因为这是一个收费操作,很多情况下是不需要的。因此,通过让您的代码来执行该读取,您可以决定是否产生此成本。
以上是我是否需要额外往返 firestore 来读取创建和更新的时间戳字段?的详细内容。更多信息请关注PHP中文网其他相关文章!

本文解释了GO的软件包导入机制:命名imports(例如导入“ fmt”)和空白导入(例如导入_ fmt; fmt;)。 命名导入使包装内容可访问,而空白导入仅执行t

本文解释了Beego的NewFlash()函数,用于Web应用程序中的页间数据传输。 它专注于使用newflash()在控制器之间显示临时消息(成功,错误,警告),并利用会话机制。 Lima

本文详细介绍了MySQL查询结果的有效转换为GO结构切片。 它强调使用数据库/SQL的扫描方法来最佳性能,避免手动解析。 使用DB标签和Robus的结构现场映射的最佳实践

本文演示了创建模拟和存根进行单元测试。 它强调使用接口,提供模拟实现的示例,并讨论最佳实践,例如保持模拟集中并使用断言库。 文章

本文探讨了GO的仿制药自定义类型约束。 它详细介绍了界面如何定义通用功能的最低类型要求,从而改善了类型的安全性和代码可重复使用性。 本文还讨论了局限性和最佳实践

本文详细介绍了在GO中详细介绍有效的文件,将OS.WriteFile(适用于小文件)与OS.openfile和缓冲写入(最佳大型文件)进行比较。 它强调了使用延迟并检查特定错误的可靠错误处理。

本文使用跟踪工具探讨了GO应用程序执行流。 它讨论了手册和自动仪器技术,比较诸如Jaeger,Zipkin和Opentelemetry之类的工具,并突出显示有效的数据可视化


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

EditPlus 中文破解版
体积小,语法高亮,不支持代码提示功能

SublimeText3 Linux新版
SublimeText3 Linux最新版