2015-04-24 53 views
2

我們如何從流星應用程序中的數據庫生成模式。 我想從每個數據庫條目生成多個模式。如何從流星中的數據庫生成簡單模式

使用的數據庫是Mongo DB。

該模式稍後將用於生成表單。

我正在使用autoform生成表單。

[1:http://autoform.meteor.com]

+0

這個答案如何反向工程MongoDB的模式可能是有用的:http://stackoverflow.com/questions/12386547/tool-to-create-a-visualization-from-existing-mongodb –

回答

0
  1. MongoDB是文檔數據庫,其不需要的模式。如果你想成爲嚴格的有關文檔結構的,檢查出https://github.com/aldeed/meteor-collection2meteor add aldeed:collection2

我最近解釋流星集合和架構here所有你需要做的是declare collections in Meteor

  • 如果您想從集合中讀取模式,您可以通過collectionName.simpleSchema()簡單訪問它。出於您的目的,您可以採用此架構並將其轉換爲您所需的結構(或排除某些配置的字段)。

  • +0

    我必須顯示對象的所有項目,你可以給任何例如? – garima

    +0

    你是說我必須只顯示集合嗎? – garima

    +0

    啊明白。延長答案。最後一段應該指出你正確的方向。 –

    1

    我寫了一個小腳本,您可以在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();}); 
    }