How can we generate a schema from the database in meteor app. I want to generate multiple schemas from each database entry.
如何从语言应用程序的数据库中生成模式,我想从每个数据库条目中生成多个模式。
The DB used is Mongo DB.
所使用的DB是Mongo DB。
This schema will be used later to generate a form.
稍后将使用此模式生成表单。
I am using autoform to generate a form.
我正在使用autoform生成一个表单。
[1: http://autoform.meteor.com]
(1:http://autoform.meteor.com]
2 个解决方案
#1
1
I've written a small script that you can run in mongo to reverse engineer an existing (flat) collection.
我已经编写了一个小脚本,您可以在mongo中运行它来反向设计一个现有的(平面)集合。
/*
** SimpleSchema definition generator
**
** This will reverse engineer a flat collection
** only at this point. If you improve this,
** please share: {"email-left": "timstew", "at":"@", "email-right": "gmail.com"}
**
*/
var schemaName = "publisherSchema"; // Name you want to give to your simple schema
var collectionName = "publishers"; // mongodb collection name (not including 'db.')
var sampleID = "54c00f0d2b21500370a2e4c4"; // _id of a good representative document
// OK, that's all the info we need - Let's get started!
var message = eval("db." + collectionName + ".findOne({_id:\"" + sampleID +"\"})");
var count = 0;
// Hack because I can't figure out how to find out how many fields are in
var numKeys = 0;
for(var key in message) {numKeys += 1}
var index = 0;
for (var key in message) {
if (index == 0) {
print(schemaName + " = new SimpleSchema({");
}
print("\t" + key + ": {");
print("\t\ttype: " + toProper(eval("typeof db." + collectionName + ".findOne({_id:\"" + sampleID + "\"})." + key)) + ",");
print("\t\tlabel: \"" + toProper(key) + "\"");
if (index == numKeys-1) {
print("\t\t}");
print("\t})");
} else {
print("\t\t},");
}
index += 1;
}
function toProper(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
#2
0
- MongoDB is a document database, which doesn't require a schema. All you need to do is declare collections in Meteor
- MongoDB是一个文档数据库,它不需要模式。你所需要做的就是在流星中申报收藏
- If you want to be strict about the document structure, check out https://github.com/aldeed/meteor-collection2 (
meteor add aldeed:collection2
) - 如果您想严格要求文档结构,请查看https://github.com/aldeed/meteor-collection2(流星添加:collection2)
I've recently explained Meteor collections and schemas here.
我最近在这里解释了流星集合和图式。
If you want to read the schema from a collection, you can simply access it via collectionName.simpleSchema()
. For your purpose you can take this schema and convert it into your desired structure (or exclude certain configured fields).
如果希望从集合中读取模式,只需通过collection . simpleschema()访问它。出于您的目的,您可以使用此模式并将其转换为所需的结构(或排除某些配置字段)。
#1
1
I've written a small script that you can run in mongo to reverse engineer an existing (flat) collection.
我已经编写了一个小脚本,您可以在mongo中运行它来反向设计一个现有的(平面)集合。
/*
** SimpleSchema definition generator
**
** This will reverse engineer a flat collection
** only at this point. If you improve this,
** please share: {"email-left": "timstew", "at":"@", "email-right": "gmail.com"}
**
*/
var schemaName = "publisherSchema"; // Name you want to give to your simple schema
var collectionName = "publishers"; // mongodb collection name (not including 'db.')
var sampleID = "54c00f0d2b21500370a2e4c4"; // _id of a good representative document
// OK, that's all the info we need - Let's get started!
var message = eval("db." + collectionName + ".findOne({_id:\"" + sampleID +"\"})");
var count = 0;
// Hack because I can't figure out how to find out how many fields are in
var numKeys = 0;
for(var key in message) {numKeys += 1}
var index = 0;
for (var key in message) {
if (index == 0) {
print(schemaName + " = new SimpleSchema({");
}
print("\t" + key + ": {");
print("\t\ttype: " + toProper(eval("typeof db." + collectionName + ".findOne({_id:\"" + sampleID + "\"})." + key)) + ",");
print("\t\tlabel: \"" + toProper(key) + "\"");
if (index == numKeys-1) {
print("\t\t}");
print("\t})");
} else {
print("\t\t},");
}
index += 1;
}
function toProper(str)
{
return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();});
}
#2
0
- MongoDB is a document database, which doesn't require a schema. All you need to do is declare collections in Meteor
- MongoDB是一个文档数据库,它不需要模式。你所需要做的就是在流星中申报收藏
- If you want to be strict about the document structure, check out https://github.com/aldeed/meteor-collection2 (
meteor add aldeed:collection2
) - 如果您想严格要求文档结构,请查看https://github.com/aldeed/meteor-collection2(流星添加:collection2)
I've recently explained Meteor collections and schemas here.
我最近在这里解释了流星集合和图式。
If you want to read the schema from a collection, you can simply access it via collectionName.simpleSchema()
. For your purpose you can take this schema and convert it into your desired structure (or exclude certain configured fields).
如果希望从集合中读取模式,只需通过collection . simpleschema()访问它。出于您的目的,您可以使用此模式并将其转换为所需的结构(或排除某些配置字段)。