mongoose#SchemaOptions TypeScript Examples

The following examples show how to use mongoose#SchemaOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: generateModel.ts    From davinci with MIT License 6 votes vote down vote up
/**
 * Create an instance of a Mongoose model
 * @param classSchema
 * @param modelName
 * @param collectionName
 * @param options
 */
export function generateModel<T>(
	classSchema: ClassType,
	modelName = classSchema.name,
	collectionName?,
	options?: SchemaOptions
) {
	const schema = generateSchema(classSchema, options);
	return model(modelName, schema, collectionName) as T & ModelType<T>;
}
Example #2
Source File: index.ts    From davinci with MIT License 5 votes vote down vote up
/**
 * Decorator that annotates a schema, allowing to pass options to the mongoose 'Schema' constructor
 */
export function schema(options?: SchemaOptions): ClassDecorator {
	return (target: Function): void => {
		Reflector.defineMetadata('davinci:mongoose:schemaOptions', options, target);
	};
}
Example #3
Source File: generateModel.ts    From davinci with MIT License 5 votes vote down vote up
createMongooseSchema = (classSchema: ClassType, definition: SchemaDefinition, options: SchemaOptions) => {
	const allMethods = Reflector.getMetadata('davinci:mongoose:methods', classSchema.prototype.constructor) || [];

	// get methods
	const methods = allMethods
		.filter(({ isPrototype }) => isPrototype)
		.filter(({ name }) => !EXCLUDED_INSTANCE_METHODS.includes(name))
		.reduce((acc, { name, handler }) => ({ ...acc, [name]: handler }), {});

	// get statics
	const statics = allMethods
		.filter(({ isStatic }) => isStatic)
		.filter(({ name }) => !EXCLUDED_STATIC_METHODS.includes(name))
		.reduce((acc, { name, handler }) => ({ ...acc, [name]: handler }), {});

	// get indexes
	const indexes = Reflector.getMetadata('davinci:mongoose:indexes', classSchema) || [];

	// get virtual fields that allow population
	const populates = Reflector.getMetadata('davinci:mongoose:populates', classSchema.prototype.constructor) || [];

	// get virtual fields
	const virtuals = Reflector.getMetadata('davinci:mongoose:virtuals', classSchema.prototype.constructor) || [];

	// get schema options
	const decoratorOptions = Reflector.getMetadata('davinci:mongoose:schemaOptions', classSchema.prototype.constructor);

	const schema = new Schema(definition, options ?? decoratorOptions);
	schema.methods = methods;
	schema.statics = statics;
	if (indexes.length > 0) {
		indexes.forEach(({ index, options: o }) => schema.index(index, o));
	}
	virtuals.forEach(({ name, options: opts, handler }) => {
		const virtual = schema.virtual(name, opts);
		if (typeof handler === 'function') {
			virtual.get(handler);
		}
	});
	populates.forEach(({ name, options: o }) => schema.virtual(name, o));

	return schema;
}
Example #4
Source File: generateModel.ts    From davinci with MIT License 5 votes vote down vote up
generateSchema = (
	classSchema: ClassType,
	options: SchemaOptions = { timestamps: true, toJSON: { virtuals: true }, toObject: { virtuals: true } },
	returnMongooseSchema = true
) => {
	const props: IPropDecoratorMetadata[] =
		Reflector.getMetadata('davinci:mongoose:props', classSchema.prototype.constructor) || [];

	// loop over the variable decorated as props
	const definition = props.reduce((acc, { key, optsFactory }) => {
		const opts = optsFactory();
		// the type can be explicitly passed as option, or can be inferred
		// it's important to note that not in all the situations
		// the type can be retrieved with reflect-metadata, for example:
		// - arrays: [string] or [object] or [MyClass]
		// - objects
		let type: TypeValue = opts?.type;
		let isRawType = false;

		if (!type) {
			if (typeof opts?.typeFactory === 'function') {
				type = opts.typeFactory();
			} else {
				type = Reflector.getMetadata('design:type', classSchema.prototype, key);
			}
		}

		// explicit mongoose type passing, we can return
		if (opts?.rawType) {
			type = opts?.rawType;
			isRawType = true;
		}

		const isArray = Array.isArray(type);
		if (isArray && (type as any[]).length > 0) {
			// eslint-disable-next-line prefer-destructuring
			type = type[0];
		}

		const isFunction =
			!([String, Number, Object, Boolean, Date, Schema.Types.ObjectId, Schema.Types.Mixed] as unknown[]).includes(
				type
			) &&
			typeof type === 'function' &&
			!isRawType;

		// if the type is a function, we need to recursively get the schema definition
		if (isFunction) {
			const classType = type as ClassType;
			if (classType.name !== 'ObjectId' && classType.name !== 'Mixed') {
				// passing null to avoid setting the options recursively to sub-schemas
				type = generateSchema(classType, null, returnMongooseSchema);
			}
		}

		const prop = {
			...opts,
			type
		};

		return {
			...acc,
			[key]: isArray ? [prop] : prop
		};
	}, {});

	return returnMongooseSchema ? createMongooseSchema(classSchema, definition, options) : definition;
}
Example #5
Source File: schema-explorer.ts    From pebula-node with MIT License 5 votes vote down vote up
hasOptionValue<TOptKey extends keyof SchemaOptions>(key: TOptKey, value: SchemaOptions[TOptKey]): this  {
    expect(this.container.getSchemaOptions(key)).toEqual(value);
    return this;
  }