Skip to content

Commit

Permalink
Fix default cursor options assign function
Browse files Browse the repository at this point in the history
  • Loading branch information
kolyasya committed Jan 3, 2023
1 parent d93019b commit 59238a3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .versions
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ [email protected]
[email protected]
[email protected]
[email protected]
kolyasya:[email protected].5
kolyasya:[email protected].8
[email protected]
[email protected]
[email protected]
Expand Down
2 changes: 1 addition & 1 deletion example/.meteor/versions
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ [email protected]
[email protected]
[email protected]
[email protected]
kolyasya:[email protected].6
kolyasya:[email protected].8
[email protected]
[email protected]
[email protected]
Expand Down
53 changes: 38 additions & 15 deletions example/packages/meteor-pagination/getCursorOptions.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
import handleKeepPreloaded from "./handleKeepPreloaded";
import getPublishPaginatedLogger from "./getPublishPaginatedLogger";

const getCursorOptions = ({ paginationParams, subscriptionParams }) => {
let options =
typeof paginationParams.transformCursorOptions === "function"
? paginationParams.transformCursorOptions({
paginationParams,
subscriptionParams,
})
: {};
const logger = getPublishPaginatedLogger();
let cursorOptions = {};

if (subscriptionParams.limit >= 0) {
options.limit = subscriptionParams.limit;
cursorOptions.limit = subscriptionParams.limit;
}

if (subscriptionParams.skip >= 0) {
options.skip = subscriptionParams.skip;
cursorOptions.skip = subscriptionParams.skip;
}

if (subscriptionParams.sort) {
options.sort = subscriptionParams.sort;
cursorOptions.sort = subscriptionParams.sort;
}

if (subscriptionParams.fields) {
options.fields = subscriptionParams.fields;
cursorOptions.fields = subscriptionParams.fields;
}

if (subscriptionParams.keepPreloaded) {
options = handleKeepPreloaded({
cursorOptions = handleKeepPreloaded({
options,
params: subscriptionParams,
});
}

if (subscriptionParams.transform) {
options.transform = subscriptionParams.transform;
cursorOptions.transform = subscriptionParams.transform;
}

if (typeof subscriptionParams.reactive !== "undefined") {
options.reactive = subscriptionParams.reactive;
cursorOptions.reactive = subscriptionParams.reactive;
}

return options;
if (typeof paginationParams.transformCursorOptions === "function") {
logger(
"Transforming cursor options with custom functio (transformCursorOptions)..."
);

cursorOptions = paginationParams.transformCursorOptions({
paginationParams,
subscriptionParams,
cursorOptions,
});

if (typeof cursorOptions !== "object") {
console.warn(
`"transformCursorOptions" function should return object, which will be used as Mongo Cursor options param`
);
}
}

if (
typeof cursorOptions.limit !== "number" ||
typeof cursorOptions.skip !== "number"
) {
console.warn(
`Check cursor options limit: ${cursorOptions.limit} and skip: ${cursorOptions.skip} params. They should be numbers for pagination to work properly`
);
}

return cursorOptions;
};

export default getCursorOptions;
2 changes: 1 addition & 1 deletion example/packages/meteor-pagination/package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: "kolyasya:meteor-pagination",
version: "0.0.6",
version: "0.0.8",
summary: "Trying to make it right",
git: "https://github.com/kolyasya/meteor-pagination",
documentation: "README.md",
Expand Down
53 changes: 38 additions & 15 deletions getCursorOptions.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,69 @@
import handleKeepPreloaded from "./handleKeepPreloaded";
import getPublishPaginatedLogger from "./getPublishPaginatedLogger";

const getCursorOptions = ({ paginationParams, subscriptionParams }) => {
let options =
typeof paginationParams.transformCursorOptions === "function"
? paginationParams.transformCursorOptions({
paginationParams,
subscriptionParams,
})
: {};
const logger = getPublishPaginatedLogger();
let cursorOptions = {};

if (subscriptionParams.limit >= 0) {
options.limit = subscriptionParams.limit;
cursorOptions.limit = subscriptionParams.limit;
}

if (subscriptionParams.skip >= 0) {
options.skip = subscriptionParams.skip;
cursorOptions.skip = subscriptionParams.skip;
}

if (subscriptionParams.sort) {
options.sort = subscriptionParams.sort;
cursorOptions.sort = subscriptionParams.sort;
}

if (subscriptionParams.fields) {
options.fields = subscriptionParams.fields;
cursorOptions.fields = subscriptionParams.fields;
}

if (subscriptionParams.keepPreloaded) {
options = handleKeepPreloaded({
cursorOptions = handleKeepPreloaded({
options,
params: subscriptionParams,
});
}

if (subscriptionParams.transform) {
options.transform = subscriptionParams.transform;
cursorOptions.transform = subscriptionParams.transform;
}

if (typeof subscriptionParams.reactive !== "undefined") {
options.reactive = subscriptionParams.reactive;
cursorOptions.reactive = subscriptionParams.reactive;
}

return options;
if (typeof paginationParams.transformCursorOptions === "function") {
logger(
"Transforming cursor options with custom functio (transformCursorOptions)..."
);

cursorOptions = paginationParams.transformCursorOptions({
paginationParams,
subscriptionParams,
cursorOptions,
});

if (typeof cursorOptions !== "object") {
console.warn(
`"transformCursorOptions" function should return object, which will be used as Mongo Cursor options param`
);
}
}

if (
typeof cursorOptions.limit !== "number" ||
typeof cursorOptions.skip !== "number"
) {
console.warn(
`Check cursor options limit: ${cursorOptions.limit} and skip: ${cursorOptions.skip} params. They should be numbers for pagination to work properly`
);
}

return cursorOptions;
};

export default getCursorOptions;
2 changes: 1 addition & 1 deletion package.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package.describe({
name: "kolyasya:meteor-pagination",
version: "0.0.6",
version: "0.0.8",
summary: "Trying to make it right",
git: "https://github.com/kolyasya/meteor-pagination",
documentation: "README.md",
Expand Down

0 comments on commit 59238a3

Please sign in to comment.