[英]Firebase Firestore: Is there a way to enforce required fields for all documents in a collection?
I am manually adding data for an app I am creating.我正在为我正在创建的应用程序手动添加数据。 I have to add many documents to a collection, all of which have to contain the same 5 fields.
我必须将许多文档添加到一个集合中,所有这些文档都必须包含相同的 5 个字段。 Is there a way to automatically create these fields when I create a new document in the Firebase console?
有没有办法在Firebase控制台新建文档的时候自动创建这些字段? And to also enforce all documents have those 5 fields (so that I don't make mistakes)?
并且还强制所有文件都有这 5 个字段(这样我就不会犯错误)?
Additionally, is there a better way to manually adding this data than from the Firebase console?此外,是否有比从 Firebase 控制台手动添加此数据更好的方法? Through JSON for example?
例如通过JSON?
There are two separate questions here.这里有两个不同的问题。 Both have been answered before, but never in a single question, so I'll do that here.
之前都已经回答过这两个问题,但从来没有在一个问题中回答过,所以我会在这里回答。
This is not possible.这不可能。 You will have explicitly write those values into each new document you create.
您将明确地将这些值写入您创建的每个新文档中。
This is possible through the use of Firebase's server-side security rules.这可以通过使用 Firebase 的服务器端安全规则来实现。
For example, to ensure a document has values for field1
, field2
and field3
, you could use hasAll
:例如,要确保文档具有
field1
、 field2
和field3
,您可以使用hasAll
:
allow write: if resource.data.keys().hasAll(['editor', 'admin']);
One option for setting default values on documents is to implement a cloud function with an onCreate trigger , which will then look at each new document when it is created and add the default value if it does not exist.在文档上设置默认值的一种选择是使用onCreate 触发器实现云函数,然后在创建时查看每个新文档,如果不存在则添加默认值。 Note that this isn't perfect, as there will be some non zero time between when the object is created and when the function runs, but it may be sufficient for some cases.
请注意,这并不完美,因为在创建对象和运行函数之间会有一些非零时间,但对于某些情况可能就足够了。
Here's what one such function might look like:下面是这样一个函数的样子:
const functions = require('firebase-functions');
exports.setDefaultValueFirestore = functions.firestore.document('defaultDemo/{someDoc}')
.onCreate(async (snap, context) => {
if(!('defaultWanted' in snap.data())) {
return snap.ref.set({
'defaultWanted': 'my default value'
}, {merge: true});
} else {
return Promise.resolve();
}
});
This will set the defaultWanted
field on any document created in /defaultDemo
to my default value
.这会将
/defaultDemo
创建的任何文档上的defaultWanted
字段设置为my default value
。
However, it is likely more stable to use the security rules and have the client always supply the needed fields as @Frank suggests if you can.但是,如果可以的话,使用安全规则并让客户端始终按照@Frank 的建议提供所需的字段可能会更稳定。
There is no way to set default value as of today.截至目前,无法设置默认值。
You may utilize the is
operator to enforce required fields.您可以使用
is
运算符来强制执行必填字段。 It will ensure the fields are not null other than using the correct data type.除了使用正确的数据类型外,它将确保字段不是 null。 For more info, please refer to Enforcing field types
有关详细信息,请参阅强制字段类型
For example, to ensure a document has user_id
, origin
is map & has certain properties in origin
object, you can use the rule below:例如,要确保文档具有
user_id
, origin
为 map 并且在origin
object 中具有某些属性,您可以使用以下规则:
match /requests/{doc} {
allow create: if (request.auth != null) &&
request.resource.data.user_id is string &&
request.resource.data.origin is map &&
request.resource.data.origin.name is string &&
request.resource.data.origin.is_pinned is bool;
}
I think it is better than hasAll
since hasAll
only check if the key exist but does not prevent user from adding null
value.我认为它比
hasAll
更好,因为hasAll
只检查密钥是否存在但不会阻止用户添加null
值。
For optional fields but you still want to ensure the data type is correct, you may utilize request.resource.data.get
method.对于可选字段,但您仍想确保数据类型正确,您可以使用
request.resource.data.get
方法。 For more info, please refer to Enforcing types for optional fields有关更多信息,请参阅可选字段的强制类型
For example if phone_num
field is optional, you can use the following rule to ensure the data type is correct:例如,如果
phone_num
字段是可选的,则可以使用以下规则来确保数据类型正确:
match /requests/{doc} {
allow create: if (request.auth != null) &&
request.resource.data.user_id is string &&
request.resource.data.get('phone_num', '') is string;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.