本文整理匯總了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');