2017-07-26 60 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,梯形校正可能不是你想要使用的平台,因为它需要在构建时通过设计来定义模式。