我不确定您打算用令牌存储/做什么,所以如果这不能回答您的问题,请澄清:)
我猜你的意思是:
- an 数组路径 http://mongoosejs.com/docs/api.html#schema-array-js存储在数据库中,或者
- a 虚拟财产 http://mongoosejs.com/docs/guide.html其值不被存储,而是在运行时计算。
两者都可以通过修改 mongoose 来实现schema
直接,而不是使用 Keystoneadd
方法上的List
.
要添加数组路径(例如,您可以存储通过保存过程中生成的字符串标记数组),您可以这样做:
var keystone = require('keystone'),
Types = keystone.Field.Types;
var Info = new keystone.List('Info');
Info.add({
title: { type: String, required: true, initial: true },
subtitle: { type: String, initial: true },
content: { type: Types.Markdown, height: 500, initial: true },
author: { type: Types.Relationship, ref: 'User', initial: true },
date: { type: Types.Date, default: Date.now, initial: true },
published: { type: Boolean, default: false, initial: true }
});
Info.schema.add({
tokens: { type: [String] }
});
Info.defaultColumns = 'title, author, date|15%, published|15%';
Info.register();
要创建虚拟属性,您可以使用如下的 getter 来指定它:
Info.schema.virtual('tokens', function() {
var tokens = [];
// calculate tokens somehow
return tokens;
});
通过访问架构,您可以绕过 Keystone 的列表,这意味着这些字段不会显示在管理 UI 中。有an issue https://github.com/JedWatson/keystone/issues/204在管理 UI 中添加对自定义模板的支持,这在将来将允许这样做。
还有一个问题数组字段类型 https://github.com/JedWatson/keystone/issues/13,因此,如果您现在将字符串存储在数组中,则在实现该功能后,您将能够将其包含在管理 UI 中。
与此相关的是,猫鼬提供的所有功能都可以通过模式获得,因此您也可以定义自定义方法、静态和预/后保存挂钩等内容。有关猫鼬模式的更多信息,请查看guide http://mongoosejs.com/docs/guide.html.