2017-07-26 97 views
0

我已經在顯示的keystone.js中創建了一個新模型'Product.js'。在keystone.js中更新模型數據庫模式

var keystone = require('keystone'); 
var Types = keystone.Field.Types; 

var Product = new keystone.List('Product', { 
    map: {name: 'title'}, 
    singular: 'Product', 
    plural: 'Products', 
    autokey: {path: 'slug', from: 'title', unique: true} 
}); 

Product.add({ 
    title: {type: String, required: true}, 
    price: {type: Number}, 
    qty: { type: Number }, 
    description: {type: Types.Html, wysiwyg: true, height: 300}, 
    image: {type: Types.CloudinaryImage}, 
    publishedDate: {type: Date, default: Date.now} 
}); 

Product.register(); 

我在管理界面看到這些屬性。現在我想更新它並動態地添加一個新的屬性(不更改此文件中的任何內容)。在路由文件product.js中,我可以創建新的實例(以下文章this)來填充像這樣的現有屬性。

keystone.createItems({ 
    Product: [{ 
     title: 'Product 3', 
     price: 34, 
     qty: 4, 
     description: "sldfkjslkfjlskdjfsdfj",  
     __ref: 'prod' 
    }] 
}, function (err, stats) { 
    stats && console.log(stats.message); 
}); 

是否有像這樣的功能可以更改產品架構?

我想在這個模型中在mongoose db中添加一個新屬性。我該怎麼做? keystone.js在Windows中存儲數據庫文件的位置?

回答

0

架構並不意味着在運行時進行修改,尤其是在Keystone中利用架構來生成管理界面的方式。

下面是關於使用蒙戈沒有模式的一個有趣的文章:MongoDB: To Use Schemas or Not?

這裏是貓鼬隊採取了類似的問題:Dynamically adding fields in the schema doesn't permit to add fields as usual

如果你打算「無模式」或使用Mixed Types,梯形校正可能不是你想要使用的平臺,因爲它需要在構建時通過設計來定義模式。