流星collections。更改模式后,如何检查无效文档?怎么修?
Meteor collections. After altering schema, how to check for invalid documents? How to fix?
我正在使用 SimpleSchema 和 Collection2。
在推出我的应用程序的新版本之前,我想检查是否有任何文档对新架构无效。有自动执行此操作的方法吗?
或命令行实用程序?
如何发布架构更改的新版本?
谢谢!
麦克
SimpleSchema 和 Collection2 都有验证方法。
对于 SimpleSchema,这里是他们的示例代码:https://github.com/aldeed/meteor-simple-schema#validating-data
对于 Collection2,检查从验证上下文开始的部分 https://github.com/aldeed/meteor-collection2#validation-contexts
从客户端或服务器上的流星应用程序(假设您可以访问要在客户端上检查的所有文档):
MyCollection.find().forEach(function(doc){
check(doc,MyCollectionSchema);
});
您可能还想在失败时记录 doc
及其 _id
,以便您可以修复它们。
我听从了@michel 的建议并制定了这条 server-side 路线来检查 collections。
这非常危险,因为它正在清理和保存您的数据。这意味着它正在擦除字段。可能您不想要这些字段,这就是您将它们从架构中删除的原因,但也可能是一个错误。所以请注意买者牛仔。显然,这不应该在生产服务器上。
可以使用这样的 url 检查它们:
http://127.0.0.1:4000/v/users
回复:
{
"ok": 1
}
路线:
skipUpdatedAt = true;
Router.route('validate', {
path: '/v/:collection',
where: 'server',
action: function() {
var res = this.response;
var collections = {
users: Meteor.users,
puppies: Puppies,
unicorns: Unicorns,
rainbows: Rainbows,
};
var c = collections[this.params.collection];
if (!c) {
res.statusCode = 404;
return res.end('not found');
}
var s = c.simpleSchema();
var ret = { ok: 0 };
c.find().forEach(function(doc){
var id = doc._id;
try {
s.clean(doc);
check(doc,s);
c.update(id, doc, {validate: false});
ret.ok += 1;
} catch (e) {
ret[id] = {
doc: doc,
error: e.toString(),
};
console.log('=============ERROR');
console.log(doc);
console.log('-------------');
console.log(e);
}
});
res.statusCode = 200;
if (Object.keys(ret).length) res.statusCode = 500;
res.end(JSON.stringify(ret, null, 2));
}
});
我正在使用 SimpleSchema 和 Collection2。
在推出我的应用程序的新版本之前,我想检查是否有任何文档对新架构无效。有自动执行此操作的方法吗?
或命令行实用程序?
如何发布架构更改的新版本?
谢谢!
麦克
SimpleSchema 和 Collection2 都有验证方法。
对于 SimpleSchema,这里是他们的示例代码:https://github.com/aldeed/meteor-simple-schema#validating-data
对于 Collection2,检查从验证上下文开始的部分 https://github.com/aldeed/meteor-collection2#validation-contexts
从客户端或服务器上的流星应用程序(假设您可以访问要在客户端上检查的所有文档):
MyCollection.find().forEach(function(doc){
check(doc,MyCollectionSchema);
});
您可能还想在失败时记录 doc
及其 _id
,以便您可以修复它们。
我听从了@michel 的建议并制定了这条 server-side 路线来检查 collections。
这非常危险,因为它正在清理和保存您的数据。这意味着它正在擦除字段。可能您不想要这些字段,这就是您将它们从架构中删除的原因,但也可能是一个错误。所以请注意买者牛仔。显然,这不应该在生产服务器上。
可以使用这样的 url 检查它们:
http://127.0.0.1:4000/v/users
回复:
{
"ok": 1
}
路线:
skipUpdatedAt = true;
Router.route('validate', {
path: '/v/:collection',
where: 'server',
action: function() {
var res = this.response;
var collections = {
users: Meteor.users,
puppies: Puppies,
unicorns: Unicorns,
rainbows: Rainbows,
};
var c = collections[this.params.collection];
if (!c) {
res.statusCode = 404;
return res.end('not found');
}
var s = c.simpleSchema();
var ret = { ok: 0 };
c.find().forEach(function(doc){
var id = doc._id;
try {
s.clean(doc);
check(doc,s);
c.update(id, doc, {validate: false});
ret.ok += 1;
} catch (e) {
ret[id] = {
doc: doc,
error: e.toString(),
};
console.log('=============ERROR');
console.log(doc);
console.log('-------------');
console.log(e);
}
});
res.statusCode = 200;
if (Object.keys(ret).length) res.statusCode = 500;
res.end(JSON.stringify(ret, null, 2));
}
});