mongoose-paginate-v2
Advanced tools
Comparing version 1.6.3 to 1.7.0
# Changelog | ||
## v1.7.0 | ||
[2022-07-04] | ||
- Added support for sub-document pagination. | ||
[2022-02-28] | ||
- Added support for Mongoose 6 | ||
## v1.6.3 | ||
@@ -4,0 +14,0 @@ |
@@ -45,2 +45,4 @@ "use strict"; | ||
var paginateSubDocsHelper = require('./pagination-subdocs'); | ||
var defaultOptions = { | ||
@@ -294,5 +296,7 @@ customLabels: { | ||
schema.statics.paginate = paginate; | ||
schema.statics.paginateSubDocs = paginateSubDocsHelper; | ||
}; | ||
module.exports.PaginationParameters = PaginationParametersHelper; | ||
module.exports.paginateSubDocs = paginateSubDocsHelper; | ||
module.exports.paginate = paginate; |
@@ -26,8 +26,8 @@ declare module 'mongoose' { | ||
populate?: | ||
| PopulateOptions[] | ||
| string[] | ||
| PopulateOptions | ||
| string | ||
| PopulateOptions | ||
| undefined; | ||
| PopulateOptions[] | ||
| string[] | ||
| PopulateOptions | ||
| string | ||
| PopulateOptions | ||
| undefined; | ||
projection?: any; | ||
@@ -50,2 +50,28 @@ lean?: boolean | undefined; | ||
interface SubPaginateOptions { | ||
select?: object | string | undefined; | ||
populate?: | ||
| PopulateOptions[] | ||
| string[] | ||
| PopulateOptions | ||
| string | ||
| PopulateOptions | ||
| undefined; | ||
pagination?: boolean | undefined; | ||
read?: ReadOptions | undefined; | ||
pagingOptions: SubDocumentPagingOptions | undefined; | ||
} | ||
interface SubDocumentPagingOptions { | ||
populate?: | ||
| PopulateOptions[] | ||
| string[] | ||
| PopulateOptions | ||
| string | ||
| PopulateOptions | ||
| undefined; | ||
page?: number | undefined; | ||
limit?: number | undefined; | ||
} | ||
interface PaginateResult<T> { | ||
@@ -74,4 +100,4 @@ docs: T[]; | ||
? O['leanWithId'] extends true | ||
? LeanDocument<T & { id: string }> | ||
: LeanDocument<T> | ||
? LeanDocument<T & { id: string }> | ||
: LeanDocument<T> | ||
: HydratedDocument<T, TMethods, TVirtuals>; | ||
@@ -97,2 +123,3 @@ | ||
const paginate: { options: mongoose.PaginateOptions }; | ||
const paginateSubDocs: { options: mongoose.PaginateOptions }; | ||
class PaginationParameters<T, O extends mongoose.PaginateOptions> { | ||
@@ -99,0 +126,0 @@ constructor(request: { query?: Record<string, any> }); |
{ | ||
"name": "mongoose-paginate-v2", | ||
"version": "1.6.3", | ||
"version": "1.7.0", | ||
"description": "A custom pagination library for Mongoose with customizable labels.", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -55,6 +55,7 @@ ![Banner](static/banner.jpg) | ||
```ts | ||
import mongoose from 'mongoose'; | ||
import paginate from 'mongoose-paginate-v2'; | ||
// declare your schema | ||
export const institutionSchema = new Schema({ name: String }); | ||
export const institutionSchema = new mongoose.Schema({ name: String }); | ||
@@ -65,3 +66,3 @@ // paginate with this plugin | ||
// declare a mongoose document based on a Typescript interface representing your schema | ||
interface InstitutionDocument extends Document, InstitutionData {} | ||
interface InstitutionDocument extends mongoose.Document, InstitutionData {} | ||
@@ -71,3 +72,3 @@ // create the paginated model | ||
InstitutionDocument, | ||
PaginateModel<InstitutionDocument> | ||
mongoose.PaginateModel<InstitutionDocument> | ||
>('Institutions', institutionSchema, 'institutions'); | ||
@@ -366,2 +367,24 @@ ``` | ||
### Pagination for Sub-Documents | ||
If you want to paginate your sub-documents, here is the method you can use. | ||
```js | ||
var query = { name: 'John' }; | ||
var option = { | ||
select: 'name follower', | ||
pagingOptions: { | ||
// your populate option | ||
populate: { | ||
path: 'follower', | ||
}, | ||
page: 2, | ||
limit: 10, | ||
}, | ||
}; | ||
// Only one document (which object key with name John) will be return | ||
const result = await Book.paginateSubDocs(query, option); | ||
``` | ||
#### AllowDiskUse for large datasets | ||
@@ -368,0 +391,0 @@ |
@@ -24,2 +24,3 @@ /** | ||
const PaginationParametersHelper = require('./pagination-parameters'); | ||
const paginateSubDocsHelper = require('./pagination-subdocs'); | ||
@@ -292,5 +293,7 @@ const defaultOptions = { | ||
schema.statics.paginate = paginate; | ||
schema.statics.paginateSubDocs = paginateSubDocsHelper; | ||
}; | ||
module.exports.PaginationParameters = PaginationParametersHelper; | ||
module.exports.paginateSubDocs = paginateSubDocsHelper; | ||
module.exports.paginate = paginate; |
@@ -11,6 +11,14 @@ 'use strict'; | ||
let UserSchema = new mongoose.Schema({ | ||
name: String, | ||
age: Number, | ||
gender: Number, | ||
}); | ||
let AuthorSchema = new mongoose.Schema({ | ||
name: String, | ||
}); | ||
let Author = mongoose.model('Author', AuthorSchema); | ||
let User = mongoose.model('User', UserSchema); | ||
@@ -25,2 +33,8 @@ let BookSchema = new mongoose.Schema({ | ||
}, | ||
user: [ | ||
{ | ||
type: mongoose.Schema.ObjectId, | ||
ref: 'User', | ||
}, | ||
], | ||
loc: Object, | ||
@@ -92,3 +106,3 @@ }); | ||
before(function () { | ||
before(async function () { | ||
let book, | ||
@@ -98,2 +112,14 @@ books = []; | ||
// create users | ||
let users = []; | ||
for (let i = 0; i < 10; ++i) { | ||
const user = new User({ | ||
name: randomString(), | ||
gender: 1, | ||
age: i, | ||
}); | ||
const newUser = await User.create(user); | ||
users.push(newUser); | ||
} | ||
return Author.create({ | ||
@@ -109,2 +135,3 @@ name: 'Arthur Conan Doyle', | ||
author: author._id, | ||
user: users, | ||
loc: { | ||
@@ -513,2 +540,29 @@ type: 'Point', | ||
}); | ||
it('Sub documents pagination', () => { | ||
var query = { title: 'Book #1' }; | ||
var option = { | ||
pagingOptions: { | ||
populate: { | ||
path: 'user', | ||
}, | ||
page: 2, | ||
limit: 3, | ||
}, | ||
}; | ||
return Book.paginateSubDocs(query, option).then((result) => { | ||
expect(result.user.docs).to.have.length(3); | ||
expect(result.user.totalPages).to.equal(4); | ||
expect(result.user.page).to.equal(2); | ||
expect(result.user.limit).to.equal(3); | ||
expect(result.user.hasPrevPage).to.equal(true); | ||
expect(result.user.hasNextPage).to.equal(true); | ||
expect(result.user.prevPage).to.equal(1); | ||
expect(result.user.nextPage).to.equal(3); | ||
expect(result.user.pagingCounter).to.equal(4); | ||
expect(result.user.docs[0].age).to.equal(3); | ||
}); | ||
}); | ||
/* | ||
@@ -719,1 +773,16 @@ it('2dsphere', function () { | ||
}); | ||
function randomString(strLength, charSet) { | ||
var result = []; | ||
strLength = strLength || 5; | ||
charSet = | ||
charSet || 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | ||
while (strLength--) { | ||
// (note, fixed typo) | ||
result.push(charSet.charAt(Math.floor(Math.random() * charSet.length))); | ||
} | ||
return result.join(''); | ||
} |
324933
23
1952
453