Not working with mongoose 7.x
Closed this issue · 11 comments
mazenkhalil commented
Mongoose 7.x Removed LeanDocument and support for extends Document causing this library to stop working.
pryme0 commented
Any solution for this yet?
mazenkhalil commented
@pryme0 Based on my usage. Everything seems to be working fine if you override Typescript types and disable typescript errors in libs.
ypicard commented
Observing this issue too. @mazenkhalil Could you provide code snippets please?
mazenkhalil commented
Observing this issue too. @mazenkhalil Could you provide code snippets please?
Sure. Below an example from what I was working on. It may not be optimal, but it gets the job done.
// tsconfig.json
{
"compilerOptions": {
"experimentalDecorators": true,
"module": "commonjs",
"target": "es6",
"outDir": "dist",
"rootDir": ".",
"sourceMap": true,
"strict": false,
"strictNullChecks": true,
"esModuleInterop": true,
*"skipLibCheck": true*
}
}
// users.repository.ts
import { FilterQuery, HydratedDocument, InferSchemaType, Model, ObtainSchemaGeneric, PaginateOptions, PaginateResult, Schema } from 'mongoose';
import paginate from 'mongoose-paginate-v2';
import { ROLE } from '../_models';
import { DatabaseService } from '../_services/database.service';
const USERS_COLLECTION_NAME = 'users';
const schema = new Schema(
{
displayName: { type: String, required: true },
email: { type: String, required: true, unique: true },
roles: { type: [String], enum: ROLE, required: true }
},
{
timestamps: true,
query: {
byEmail(email: string) {
return this.where({ email: email });
}
},
methods: {
getId: function (): string {
return this._id.toString();
},
isAdmin: function (): boolean {
return this.roles.includes(ROLE.ADMIN);
}
}
}
);
schema.plugin(paginate);
type TSchema = typeof schema;
type QueryHelpers = ObtainSchemaGeneric<TSchema, 'TQueryHelpers'>;
type InstanceMethods = ObtainSchemaGeneric<TSchema, 'TInstanceMethods'>;
type TVirtuals = ObtainSchemaGeneric<TSchema, 'TVirtuals'>;
type StaticMethods = ObtainSchemaGeneric<TSchema, 'TStaticMethods'>;
type IUser = InferSchemaType<TSchema>;
type UserDocument = HydratedDocument<IUser, InstanceMethods & TVirtuals, QueryHelpers>;
// override mongose.PaginateModel to fix the type of PaginateDocument & define the Model type
interface PaginateModel extends StaticMethods, Model<IUser, QueryHelpers, InstanceMethods, TVirtuals, UserDocument, TSchema> {
paginate<O extends PaginateOptions>(
query?: FilterQuery<IUser>,
options?: O,
callback?: (err: any, result: PaginateResult<UserDocument>) => void
): Promise<PaginateResult<UserDocument>>;
}
const User = (tenant: string) => {
return DatabaseService.getMongooseConnection(tenant).model<IUser, PaginateModel>(USERS_COLLECTION_NAME, schema, USERS_COLLECTION_NAME);
};
export { USERS_COLLECTION_NAME, IUser, UserDocument, User };
zquintana commented
Is there a solution for this yet?
cincarnato commented
9 months last update. The maintainers are still there?
zquintana commented
maddocnc commented
Added in project root d.ts type LeanDocument<T> = T
while package is not updated.
aravindnc commented
Hope this is resolved.