[英]Can i record changes on firestore documents?
I'm working on a project with Firebase and Firestore, I have a collection that has a status attribute and I wanna record the time that a document is updated and who updated it.我正在使用 Firebase 和 Firestore 开发一个项目,我有一个具有状态属性的集合,我想记录文档更新的时间以及更新者。 There's no documentation for this Does anyone have a solution?
没有这方面的文档 有人有解决方案吗?
There is nothing built-in that can help you achieve that.没有任何内置功能可以帮助您实现这一目标。 If you want to track the changes that are made to a document and know who made them, you have to create a mechanism for that yourself.
如果您想跟踪对文档所做的更改并知道是谁做的,您必须自己创建一个机制。
Assuming that you have a document with a particular ID, you can create a subcollection in which you can add as documents the changes that are made.假设您有一个具有特定 ID 的文档,您可以创建一个子集合,您可以在其中将所做的更改作为文档添加。 A possible database schema would look like this:
可能的数据库模式如下所示:
Firestore-root
|
--- users (collection)
|
--- $uid (document)
|
--- changes (collection)
|
--- $changeId (document)
|
--- timestamp: Febrary 4, 2022 at 2:46:19 PM UTC+3
|
--- madeBy: "UidOfTheUser"
In this way you can keep a history of the changes that are made to a particular document.通过这种方式,您可以保留对特定文档所做更改的历史记录。
Something like this happened to me when i wanted to retrive live messages from Firebase so i used Stream Builder to catch all new messages in the stream.当我想从 Firebase 检索实时消息时,我遇到了这样的事情,所以我使用 Stream Builder 来捕获 stream 中的所有新消息。
Here is the code i used!这是我使用的代码!
class MessagesStream extends StatelessWidget {
@override
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: _firestore.collection('messages').orderBy('time').limitToLast(100).snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(
child: CircularProgressIndicator(
backgroundColor: Colors.lightBlueAccent,
),
);
}
final messages = snapshot.data.docs.reversed;
List<MessageBubble> messageBubbles = [];
for (var message in messages) {
final messageText = message.data()['text'];
final messageSender = message.data()['sender'];
final currentUser = loggedInUser.email;
final messageBubble = MessageBubble(
sender: messageSender,
text: messageText,
isMe: currentUser == messageSender,
);
messageBubbles.add(messageBubble);
}
return Expanded(
child: ListView(
reverse: true,
padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 20.0),
children: messageBubbles,
),
);
},
);
}
}
Instead of storing this information yourself, you can also use Firestore's built-in audit logging capability to track who access what documents.您也可以使用Firestore 的内置审核日志记录功能来跟踪谁访问了哪些文档,而不是自己存储这些信息。 Check the documentation to learn how to enable audit logging for data access .
查看文档以了解如何为数据访问启用审核日志记录。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.