當前位置: 首頁>>代碼示例>>TypeScript>>正文


TypeScript Schema.eachPath方法代碼示例

本文整理匯總了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;
}
開發者ID:freezy,項目名稱:node-vpdb,代碼行數:12,代碼來源:util.ts

示例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);
		}
	});
}
開發者ID:freezy,項目名稱:node-vpdb,代碼行數:36,代碼來源:id.reference.validator.plugin.ts

示例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');
開發者ID:RaySingerNZ,項目名稱:DefinitelyTyped,代碼行數:32,代碼來源:mongoose-tests.ts


注:本文中的mongoose.Schema.eachPath方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。