本文整理汇总了TypeScript中mongoose.Schema.eachPath方法的典型用法代码示例。如果您正苦于以下问题:TypeScript Schema.eachPath方法的具体用法?TypeScript Schema.eachPath怎么用?TypeScript Schema.eachPath使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mongoose.Schema
的用法示例。
在下文中一共展示了Schema.eachPath方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: traversePaths
export function traversePaths(schema: Schema, prefix: string = '', paths: any = {}): { [key: string]: any } {
schema.eachPath((path, type) => {
const schemaType = type as any;
const isPathArray = schemaType.options && isArray(schemaType.options.type);
const fullPath = prefix + (prefix ? '.' : '') + path + (isPathArray ? '.0' : '');
paths[fullPath] = schemaType;
if (schemaType.schema) {
exports.traversePaths(schemaType.schema, fullPath, paths);
}
});
return paths;
}
示例2: idReferenceValidatorPlugin
export function idReferenceValidatorPlugin(schema: Schema, options: IdReferenceValidatorOpts = {}) {
options = options || {};
const fields = options.fields || [];
const message = options.message || '{PATH} references a non existing ID';
schema.eachPath((path, schemaType: any) => {
let validateFunction: (doc: Document, path: string, refModelName: string, values: any[], conditions: {[key: string]: any}) => Promise<boolean> = null;
let refModelName: string = null;
let conditions = {};
if (fields.length > 0 && !fields.includes(path)) {
return;
}
if (schemaType.options && schemaType.options.ref) {
validateFunction = validateId;
refModelName = schemaType.options.ref;
if (schemaType.options.refConditions) {
conditions = schemaType.options.refConditions;
}
} else if (schemaType.caster && schemaType.caster.instance && schemaType.caster.options && schemaType.caster.options.ref) {
validateFunction = validateIdArray;
refModelName = schemaType.caster.options.ref;
if (schemaType.caster.options.refConditions) {
conditions = schemaType.caster.options.refConditions;
}
}
if (validateFunction) {
schema.path(path).validate(async function(value: any) {
return validateFunction(this, path, refModelName, value, conditions);
}, message);
}
});
}
示例3:
ofBuffer: [Buffer],
ofBoolean: [Boolean],
ofMixed: [mongoose.Schema.Types.Mixed],
ofObjectId: [mongoose.Schema.Types.ObjectId],
nested: {
stuff: { type: String, lowercase: true, trim: true }
}
});
schema.add({
mixedArray: {
type: [mongoose.Schema.Types.Mixed],
required: true
}
}, 'prefix');
schema.eachPath(function (path, type) {
path.toLowerCase();
type.sparse(true);
}).eachPath(cb);
schema.get('path');
schema.index({
name: 1,
binary: -1
}).index({}, {});
schema.indexes().slice();
schema.method('name', cb).method({
m1: cb,
m2: cb
});
schema.path('a', mongoose.Schema.Types.Buffer).path('a');
schema.pathType('m1').toLowerCase();
schema.plugin(function (schema, opts) {
schema.get('path');