Comments (4)
Almost 3 years still not TypeScript support , sad :(
from mongo-cursor-pagination.
For any of you that are using Typegoose, I created the following:
import { Model, DocumentQuery } from "mongoose";
import { InstanceType } from "typegoose";
export interface IPaginateOptions {
query?: Object;
limit?: number;
fields?: Object;
paginatedField?: string;
sortAscending?: Boolean;
next?: string;
previous?: string;
}
export interface IPaginateResult<T> {
hasNext: boolean;
hasPrevious: boolean;
next?: string;
previous?: string;
results: T[];
}
export interface IPaginateModel<T> extends Model<InstanceType<T>> {
paginate(options: IPaginateOptions): DocumentQuery<IPaginateResult<T>, InstanceType<T>>;
}
and export your typegoose model as follows:
export const UserModel = new User().getModelForClass(User) as IPaginateModel<InstanceType<User>> & User & typeof User;
Now you can use paginate()
anywhere as you would find()
:
const query = {}; // The query that you would normally use
const users = await UserModel.paginate({ query: query, limit: 10, paginatedField: "email", sortAscending: true });
from mongo-cursor-pagination.
I agree. Being new to ts as well made it a bit of a challenge. But I did some stack overflow research and one of them led to this:
declare module 'mongoose' {
export interface IPaginateOptions {
query: Object;
limit?: number;
next?: string;
}
export interface PaginateResult<T> {
results: Array<T>;
next: string;
}
export interface IPaginateModel<T extends Document> extends Model<T> {
paginate(options: IPaginateOptions): Promise<PaginateResult<T>>;
}
export function model<T extends Document>(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean,
): IPaginateModel<T>;
export function model<T extends Document, U extends IPaginateModel<T>>(
name: string,
schema?: Schema,
collection?: string,
skipInit?: boolean,
): U;
}
declare module "mongo-cursor-pagination" {
import mongoose from 'mongoose';
export function mongoosePlugin(schema: mongoose.Schema): void;
}
I don't think it's perfect though. Just something that got it working. I definitely would appreciate if any advanced TS folks can help to sort out the typedef for mongo-cursor-pagination
. :)
from mongo-cursor-pagination.
Building on top of @ExtraBB's answer w/ Typegoose v7.2.0 and Mongoose 5.9.20 as of writing.
I created a BaseModelSchema
class which has a static paginate()
function to be inherited by other domain-specific schema.
import { plugin, defaultClasses, DocumentType } from '@typegoose/typegoose';
import { DocumentQuery } from 'mongoose';
const MongoCursorPagination = require('mongo-cursor-pagination');
@plugin(MongoCursorPagination.mongoosePlugin) // https://github.com/mixmaxhq/mongo-cursor-pagination#with-mongoose
export class BaseModelSchema extends defaultClasses.Base {
get id(): string {
return this._id.toHexString();
}
static paginate<T>(this: T, options: IPaginateOptions): DocumentQuery<IPaginateResult<T>, DocumentType<T>> {
return (this as any).paginate(options);
}
}
interface IPaginateOptions {
query?: object;
limit?: number;
fields?: object;
paginatedField?: string;
sortAscending?: boolean;
next?: string;
previous?: string;
}
interface IPaginateResult<T> {
hasNext: boolean;
hasPrevious: boolean;
next?: string;
previous?: string;
results: T[];
}
Then usage:
class UserModelSchema extends BaseModelSchema {
// your custom props
}
const UserModel = getModelForClass(UserModelSchema);
const users = await UserModel.paginate({ query: query, limit: 10, paginatedField: "email", sortAscending: true });
from mongo-cursor-pagination.
Related Issues (20)
- Populate with Mongoose HOT 2
- The automated release is failing 🚨
- find() fields straight up don't work HOT 2
- Performance issues. HOT 1
- MAX_LIMIT can be bypassed with params.limit = NaN
- Where is _id, cmon :( ? HOT 2
- Mongoose plugin does not cast the query before execution HOT 3
- Fields projection not working on .find method HOT 1
- Bump object-path from 0.11.5 to 0.11.8
- mongoosePlugin throws "collatedQuery.sort" in paginate method
- sort and limit queries in aggregate are sliced instead of pushed HOT 3
- The automated release is failing 🚨
- The automated release is failing 🚨 HOT 1
- mistake
- Importing MongoPaging results in declaration file for module error HOT 1
- Missing @mixmaxhq/semantic-release-config npm devDependencies HOT 2
- Unsupported BSON version, bson types must be from bson 5.0 or later when used with mongoose 7.0.0 HOT 7
- TypeError: collatedQuery.sort is not a function on Vercel HOT 1
- Action Required: Fix Renovate Configuration
- ...
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mongo-cursor-pagination.