Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difficulty with typeScript model definitions and generic declarations in mongoose #12

Open
tarkantoan opened this issue Jul 14, 2023 · 2 comments

Comments

@tarkantoan
Copy link

Hello, I'm having difficulty defining models with TypeScript. Even though I've added Mongoose as a devDependency, I'm noticing some shortcomings in the generic declarations. If you'd like, I can do a little work and submit a pull request, or if you have a specific roadmap, could you please share it?

@arashsheyda
Copy link
Owner

@tarkantoan Sure! PR is welcome. Thanks

@xarenas107
Copy link

xarenas107 commented Dec 20, 2023

You can try with something like this. Is not the prettiest solution but it should work .

import { model, Schema } from 'mongoose'
import type {
	SchemaDefinition, SchemaOptions, Model, SchemaDefinitionType,
	DefaultSchemaOptions, ApplySchemaOptions, ObtainDocumentType,
	ResolveSchemaOptions, FlatRecord, InferSchemaType, ObtainSchemaGeneric, HydratedDocument
} from 'mongoose'


type Options<D,I,Q,S,V,H,O> = SchemaOptions<FlatRecord<D>, I, Q, S, V, H> | ResolveSchemaOptions<O>
type ApplyOptions<T,D,O> = ApplySchemaOptions<ObtainDocumentType<D, T, ResolveSchemaOptions<O>>,ResolveSchemaOptions<O>>
export function defineMongooseModel<T, I={}, Q={}, V={}, S={},
	M = Model<T,Q,I,V>,
	O = DefaultSchemaOptions,
	D extends ApplyOptions<T,D,O> = ApplyOptions<T,any,O>,
	H = HydratedDocument<FlatRecord<D>, V & I>,
	TSchema = Schema<T, M, I, Q, V, S, O, D, H>
	>(
	document: string | {
		name: string,
		collection?: string
	},
	schema?: SchemaDefinition<SchemaDefinitionType<T>, T> | D,
	options?: Options<D,I,Q,S,V,H,O>,
	hooks?: (schema: Schema<T, M, I, Q, V, S, O, D, H>) => void,
): Model<
  InferSchemaType<TSchema>,
  ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>,
  ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
  ObtainSchemaGeneric<TSchema, 'TVirtuals'>,
  HydratedDocument<
  InferSchemaType<TSchema>,
  ObtainSchemaGeneric<TSchema, 'TVirtuals'> & ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>,
  ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>
  >,
  TSchema
  > & ObtainSchemaGeneric<TSchema, 'TStaticMethods'>
export function defineMongooseModel<T,
	O = DefaultSchemaOptions,
	D extends ApplyOptions<T,D,O> = ApplyOptions<T,any,O>,
	H = HydratedDocument<FlatRecord<D>>
	>(
	document: string | {
		name: string,
		collection?: string
	},
	definition?: SchemaDefinition<T>,
	options?: Options<D,{},{},{},{},H,O>,
	hooks?: (schema: Schema<T>) => void,
): Model<T>
export function defineMongooseModel(...args:any[]) {
	const [document,schema,options,hooks] = args

  const name = typeof document === 'string' ? document : document?.name
  const collection = typeof document !== 'string' ? document?.collection : undefined

	const $schema = new Schema(schema, options)
	if (hooks) hooks($schema)

	const $model = model(name, $schema, collection)
	return $model
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants