mongoose-plugin-paginate
Advanced tools
Comparing version 1.0.0 to 2.0.0-pre.1
@@ -1,24 +0,2 @@ | ||
import 'mongoose'; | ||
declare module 'mongoose' { | ||
interface PaginateOption<T extends Document> { | ||
query?: FilterQuery<T>; | ||
page?: number; | ||
limit?: number; | ||
populate?: PopulateOptions | PopulateOptions[]; | ||
} | ||
interface PaginateResult<T extends Document> { | ||
docs: T[]; | ||
docsCount: number; | ||
page: number; | ||
limit: number; | ||
pagesCount: number; | ||
hasPrev: boolean; | ||
hasNext: boolean; | ||
} | ||
interface PaginateModel<T extends Document> extends Model<T> { | ||
paginate(): Promise<PaginateResult<T>>; | ||
paginate(options: PaginateOption<T>): Promise<PaginateResult<T>>; | ||
} | ||
function model<T extends Document>(name: string, schema?: Schema<T>, collection?: string, skipInit?: boolean): PaginateModel<T>; | ||
} | ||
export * from './module'; | ||
export * from './plugin'; |
@@ -12,5 +12,4 @@ "use strict"; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
require("mongoose"); | ||
exports.__esModule = true; | ||
__exportStar(require("./module"), exports); | ||
__exportStar(require("./plugin"), exports); | ||
//# sourceMappingURL=index.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.mongoosePaginate = void 0; | ||
const defaultOptions = { | ||
query: {}, | ||
page: 1, | ||
limit: 10 | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
async function paginate(options = defaultOptions) { | ||
var _a; | ||
const model = this; | ||
const documentCount = await model.estimatedDocumentCount(); | ||
const query = (_a = options.query) !== null && _a !== void 0 ? _a : defaultOptions.query; | ||
const page = parse(options === null || options === void 0 ? void 0 : options.page, defaultOptions.page); | ||
const limit = parse(options === null || options === void 0 ? void 0 : options.limit, defaultOptions.limit); | ||
const pages = parse(Math.ceil(documentCount / limit)); | ||
const hasPrev = page > 1; | ||
const hasNext = page < pages; | ||
const skip = limit * (page - 1); | ||
const documentsQuery = model.find(query).skip(skip).limit(limit); | ||
if (options.populate) { | ||
if (!Array.isArray(options.populate)) | ||
options.populate = [options.populate]; | ||
options.populate.forEach(populate => { | ||
documentsQuery.populate(populate); | ||
}); | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
const documents = await documentsQuery.exec(); | ||
return { | ||
docs: documents, | ||
docsCount: documentCount, | ||
page: page, | ||
limit: limit, | ||
pagesCount: pages, | ||
hasPrev: hasPrev, | ||
hasNext: hasNext, | ||
}; | ||
exports.__esModule = true; | ||
exports.mongoosePaginate = void 0; | ||
var Paginator = /** @class */ (function () { | ||
function Paginator(model, _a) { | ||
var _b = _a.page, page = _b === void 0 ? 1 : _b, _c = _a.limit, limit = _c === void 0 ? 10 : _c; | ||
this.context = {}; | ||
this.model = model; | ||
this.context = { | ||
page: page, | ||
limit: limit, | ||
query: {} | ||
}; | ||
} | ||
Paginator.prototype.select = function (fields) { | ||
if (!Array.isArray(fields)) | ||
fields = [fields]; | ||
this.context.select = fields; | ||
return this; | ||
}; | ||
} | ||
function parse(value, defaultValue = 1) { | ||
const number = +value; | ||
if (isNaN(number) || number < 1) | ||
return defaultValue; | ||
return number; | ||
} | ||
Paginator.prototype.populate = function (options) { | ||
if (!Array.isArray(options)) | ||
options = [options]; | ||
this.context.populate = options; | ||
return this; | ||
}; | ||
Paginator.prototype.query = function (options) { | ||
this.context.query = options; | ||
return this; | ||
}; | ||
Paginator.prototype.exec = function () { | ||
var _a; | ||
return __awaiter(this, void 0, void 0, function () { | ||
var model, _b, query, page, limit, select, documentCount, pages, hasPrev, hasNext, skip, documentsQuery, documents; | ||
return __generator(this, function (_c) { | ||
switch (_c.label) { | ||
case 0: | ||
model = this.model; | ||
_b = this.context, query = _b.query, page = _b.page, limit = _b.limit, select = _b.select; | ||
return [4 /*yield*/, this.model.find(query).countDocuments()]; | ||
case 1: | ||
documentCount = _c.sent(); | ||
pages = this.parse(Math.ceil(documentCount / limit)); | ||
hasPrev = page > 1; | ||
hasNext = page < pages; | ||
skip = limit * (page - 1); | ||
documentsQuery = model.find(query).skip(skip).limit(limit); | ||
(_a = this.context.populate) === null || _a === void 0 ? void 0 : _a.forEach(function (populate) { | ||
documentsQuery.populate(populate); | ||
}); | ||
return [4 /*yield*/, documentsQuery.select(select).exec()]; | ||
case 2: | ||
documents = _c.sent(); | ||
return [2 /*return*/, { | ||
docs: documents, | ||
docsCount: documentCount, | ||
page: page, | ||
limit: limit, | ||
pagesCount: pages, | ||
hasPrev: hasPrev, | ||
hasNext: hasNext | ||
}]; | ||
} | ||
}); | ||
}); | ||
}; | ||
Paginator.prototype.parse = function (value, defaultValue) { | ||
if (defaultValue === void 0) { defaultValue = 1; } | ||
var number = +value; | ||
if (isNaN(number) || number < 1) | ||
return defaultValue; | ||
return number; | ||
}; | ||
return Paginator; | ||
}()); | ||
function mongoosePaginate(schema) { | ||
schema.statics.paginate = paginate; | ||
schema.statics.paginate = function (options) { | ||
var model = this; | ||
return new Paginator(model, options); | ||
}; | ||
} | ||
exports.mongoosePaginate = mongoosePaginate; | ||
//# sourceMappingURL=plugin.js.map |
{ | ||
"name": "mongoose-plugin-paginate", | ||
"version": "1.0.0", | ||
"version": "2.0.0-pre.1", | ||
"description": "Pagination plugin for mongoose", | ||
"main": "dist/index.js", | ||
"types": "dist/index.d.ts", | ||
"files": [ | ||
"dist/" | ||
], | ||
"scripts": { | ||
"prepare": "tsc", | ||
"build": "tsc -b tsconfig.build.json", | ||
"start": "tsc", | ||
"format": "prettier --write \"{src,test}/**/*.ts\"", | ||
"check": "prettier --check \"{src,test}/**/*.ts\"", | ||
"lint": "eslint \"{src,test}/**/*.ts\" --fix", | ||
"test": "jest", | ||
@@ -16,4 +24,14 @@ "test:watch": "jest --watch", | ||
}, | ||
"author": "CheeseGrinder", | ||
"license": "GPL-3.0", | ||
"keywords": [ | ||
"typescript", | ||
"mongo-db", | ||
"mongoose", | ||
"mongoose-plugin", | ||
"mongoose-pagination" | ||
], | ||
"author": { | ||
"name": "CheeseGrinder", | ||
"email": "devs.cheese@gmail.com" | ||
}, | ||
"license": "MIT", | ||
"bugs": { | ||
@@ -23,5 +41,12 @@ "url": "https://github.com/CheeseGrinder/mongoose-plugin-paginate/issues" | ||
"homepage": "https://github.com/CheeseGrinder/mongoose-plugin-paginate#readme", | ||
"dependencies": { | ||
"ts-node": "^10.2.1" | ||
}, | ||
"devDependencies": { | ||
"@types/jest": "^26.0.20", | ||
"@typescript-eslint/eslint-plugin": "^4.30.0", | ||
"@typescript-eslint/parser": "^4.30.0", | ||
"eslint": "^7.32.0", | ||
"jest": "^26.6.3", | ||
"prettier": "^2.3.2", | ||
"mongodb-memory-server": "^6.9.3", | ||
@@ -28,0 +53,0 @@ "mongoose": "^5.11.18", |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
0
100
178
11932
1
10
1
1
+ Addedts-node@^10.2.1
+ Added@cspotcode/source-map-support@0.8.1(transitive)
+ Added@jridgewell/resolve-uri@3.1.2(transitive)
+ Added@jridgewell/sourcemap-codec@1.5.0(transitive)
+ Added@jridgewell/trace-mapping@0.3.9(transitive)
+ Added@tsconfig/node10@1.0.11(transitive)
+ Added@tsconfig/node12@1.0.11(transitive)
+ Added@tsconfig/node14@1.0.3(transitive)
+ Added@tsconfig/node16@1.0.4(transitive)
+ Added@types/node@22.13.4(transitive)
+ Addedacorn@8.14.0(transitive)
+ Addedacorn-walk@8.3.4(transitive)
+ Addedarg@4.1.3(transitive)
+ Addedcreate-require@1.1.1(transitive)
+ Addeddiff@4.0.2(transitive)
+ Addedmake-error@1.3.6(transitive)
+ Addedts-node@10.9.2(transitive)
+ Addedtypescript@5.7.3(transitive)
+ Addedundici-types@6.20.0(transitive)
+ Addedv8-compile-cache-lib@3.0.1(transitive)
+ Addedyn@3.1.1(transitive)