Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/digen21/dmxdev-mongoose-paginated-query
Pagination With Mongoose for NestJS
https://github.com/digen21/dmxdev-mongoose-paginated-query
mongoose pagination typescript
Last synced: 3 days ago
JSON representation
Pagination With Mongoose for NestJS
- Host: GitHub
- URL: https://github.com/digen21/dmxdev-mongoose-paginated-query
- Owner: digen21
- License: mit
- Created: 2024-12-21T20:53:49.000Z (18 days ago)
- Default Branch: main
- Last Pushed: 2024-12-22T07:55:26.000Z (17 days ago)
- Last Synced: 2024-12-22T08:24:53.377Z (17 days ago)
- Topics: mongoose, pagination, typescript
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@dmxdev/mongoose-paginated-query
- Size: 128 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: Readme.md
- License: LICENSE
Awesome Lists containing this project
README
````markdown
# Mongoose PaginateThis package provides a function to paginate Mongoose model queries.
## Installation
To install the package, run:
```bash
npm install @dmxdev/mongoose-paginated-query
```
````## Usage
### Importing the Function
First, import the `mongoosePaginate` function:
```typescript
import mongoosePaginate, {
PaginationOptions,
PaginationResult,
} from '@dmxdev/mongoose-paginated-query';
```### Defining the Schema
Define your Mongoose schema and model:
```typescript
import { Schema, model } from 'mongoose';const userSchema = new Schema({
name: String,
email: String,
});const User = model('User', userSchema);
```### Using the `mongoosePaginate` Function
Use the `mongoosePaginate` function to paginate your queries:
```typescript
const options: PaginationOptions = {
page: 1,
limit: 10,
sort: { name: 1 },
populate: [],
};const query = { name: 'John Doe' };
mongoosePaginate(User, options, query)
.then((result: PaginationResult) => {
console.log(result);
})
.catch((error) => {
console.error(error);
});
```**Explanation:**
- `mongoosePaginate(Model, options, query)`: This is the core function.
- `Model`: The Mongoose model you want to paginate (e.g., `User`).
- `options`: An object containing pagination options:
- `page`: The current page number (default: 1).
- `limit`: The number of documents per page (default: 10).
- `sort`: An object specifying the sorting order (e.g., `{ createdAt: -1 }` for descending order).
- `populate`: An array of options for populating referenced documents (optional).
- `query`: The query conditions for filtering documents.**Pagination Result:**
The `mongoosePaginate` function returns a `PaginationResult` object with the following properties:
- `docs`: An array of paginated documents.
- `totalDocs`: The total number of documents matching the query.
- `limit`: The number of documents per page.
- `page`: The current page number.
- `totalPages`: The total number of pages.
- `hasNextPage`: `true` if there is a next page, `false` otherwise.
- `hasPrevPage`: `true` if there is a previous page, `false` otherwise.## Example
```typescript
import mongoosePaginate, {
PaginationOptions,
PaginationResult,
} from '@dmxdev/mongoose-paginated-query';
import { Schema, model, connect } from 'mongoose';const userSchema = new Schema({
name: String,
email: String,
});const User = model('User', userSchema);
const run = async () => {
await connect('mongodb://localhost:27017/mydatabase');const options: PaginationOptions = {
page: 1,
limit: 10,
sort: { name: 1 },
populate: [],
};const query = { name: 'John Doe' };
try {
const result: PaginationResult = await mongoosePaginate(
User,
options,
query,
);
console.log(result);
} catch (error) {
console.error(error);
}
};run();
```## Benefits
- **Simplified Pagination:** Easily paginate Mongoose queries with a single function.
- **Flexibility:** Customize pagination options to fit your specific needs.
- **Type Safety:** Utilizes TypeScript interfaces for better code maintainability.
- **Improved Code Readability:** Encapsulates pagination logic for cleaner code.## License
This project is licensed under the MIT License. See the LICENSE file for details.