-
Notifications
You must be signed in to change notification settings - Fork 91
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
Complete Example and explanation of usage with typescript #162
Comments
Also if possible please provide an example to integrate with Nest js. Many Thanks! |
@nickchauhan I am using like this... this works... but has this type definition problem... I define a Model like so: import * as mongoose from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate-v2';
interface MyType {
name: string;
};
const schemaOptions = {
timestamps: true,
};
const schema = new mongoose.Schema(
{
name: { type: String, trim: true, required: true },
},
schemaOptions
);
schema.plugin(mongoosePaginate);
schema.set('toObject', {
getters: true,
virtuals: true,
});
schema.set('versionKey', false);
export default mongoose.model<MyType>('MyType', schema); then I just need to cast it to any due the typescript complain about the model do not have the (this.myTypeCollection as any).paginate({}); |
@herbertpimentel Can you try as below? import * as mongoose from 'mongoose';
import * as mongoosePaginate from 'mongoose-paginate-v2';
import { Document, PaginateModel } from "mongoose";
interface MyType {
name: string;
};
const schemaOptions = {
timestamps: true,
};
const schema = new mongoose.Schema(
{
name: { type: String, trim: true, required: true },
},
schemaOptions
);
schema.plugin(mongoosePaginate);
schema.set('toObject', {
getters: true,
virtuals: true,
});
schema.set('versionKey', false);
// declare a mongoose document based on a Typescript interface representing your schema
interface MyTpyeDocument extends Document, MyType {}
// create the paginated model
const model = mongoose.model<MyTpyeDocument, PaginateModel<MyTpyeDocument>>(
"MyType",
schema,
"mytype"
);
export default model; after that, you can paginate import model from './schema';
const query = {
name : 'test'
}
model.paginate(query, {limit: 5, page: 1}).then(result => {
console.log(result);
}); I hope it helps! |
Hello @aravindnc I have this issue too but a slight difference in my request is that I want to dynamically type the items in the |
Looking at the code at paginate we can achieve this by interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}>
extends Model<T, TQueryHelpers, TMethods> {
paginate<O extends PaginateOptions = PaginateOptions, UserType = T>(
query?: FilterQuery<T>,
options?: O,
callback?: (
err: any,
result: PaginateResult<PaginateDocument<UserType, TMethods, O>>
) => void
): Promise<PaginateResult<PaginateDocument<UserType, TMethods, O>>>;
} where we use default generic parameters so a user can use it like so UserModel.paginate<PaginateOptions, TypeForDocsArrayItem>(...) They use OR we can use an overload so that the user can avoid passing in interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}>
extends Model<T, TQueryHelpers, TMethods> {
paginate<O extends PaginateOptions = PaginateOptions, UserType = T>(
query?: FilterQuery<T>,
options?: O,
callback?: (
err: any,
result: PaginateResult<PaginateDocument<UserType, TMethods, O>>
) => void
): Promise<PaginateResult<PaginateDocument<UserType, TMethods, O>>>;
}
// the overload
interface PaginateModel<T, TQueryHelpers = {}, TMethods = {}>
extends Model<T, TQueryHelpers, TMethods> {
paginate<UserType = T, O extends PaginateOptions = PaginateOptions>(
query?: FilterQuery<T>,
options?: O,
callback?: (
err: any,
result: PaginateResult<PaginateDocument<UserType, TMethods, O>>
) => void
): Promise<PaginateResult<PaginateDocument<UserType, TMethods, O>>>;
} I'll be glad to know what you think. |
I'm doing exactly this, however the
I've also tried |
Hello @tdsoundation Have you taken a look at this article - Set Custom Types for docs items in mongoose-paginate-v2 |
Is your feature request related to a problem? Please describe.
The typescript snipet provided on readme is confuse and I was no able to make it work properly
Describe the solution you'd like
Have a clear, complete and explained example of typescript usage of type definition. Including the missing imports from the reademe page.
The text was updated successfully, but these errors were encountered: