Comments (7)
Hello,
Any comments/hints on this question?
from sequelize-cursor-pagination.
There's no direct support, but quite simple workaround would be to use the Book
model with appropriate query:
Book.paginate({
limit: 20,
where: {
authorId: author.id // or whatever the foreign key is
},
});
And this could be implemented as a method such as getPaginatedBooks()
without too much hassle:
class Author {
// ...
getPaginatedBooks(options) {
return Book.paginate({
...options,
where: {
...options?.where,
authorId: this.id,
},
});
}
}
from sequelize-cursor-pagination.
Yes, certainly, although I wanted to avoid that since it means duplicating the relation's query/conditions.
from sequelize-cursor-pagination.
GraphQL's connection fields can be transparently created on either model or relations. Time-permitting I'll see if it is easily identifiable from the code how they did it.
from sequelize-cursor-pagination.
Yes, certainly, although I wanted to avoid that since it means duplicating the relation's query/conditions.
I don't see this as a major issue. Relation conditions change very rarely and only one method needs to be changed if that happens
from sequelize-cursor-pagination.
A patch as simple as this makes it possible to paginate associations.
diff -ru sequelize-cursor-pagination/makePaginate.js sequelize-cursor-pagination/makePaginate.js
--- sequelize-cursor-pagination/makePaginate.js 2022-08-04 22:50:45.774000000 +0200
+++ sequelize-cursor-pagination/makePaginate.js 2022-08-04 22:45:08.181000000 +0200
@@ -2,10 +2,21 @@
Object.defineProperty(exports, "__esModule", { value: true });
const sequelize_1 = require("sequelize");
const utils_1 = require("./utils");
-const makePaginate = (model, options) => {
+
+function checkIsAssociation(target) {
+ return !!target.associationType;
+}
+
+const makePaginate = (target, options) => {
+ const isAssociation = checkIsAssociation(target)
+ const findFunction = isAssociation ? target.accessors.get : 'findAll'
+ const countFunction = isAssociation ? target.accessors.count : 'count'
+ const model = isAssociation ? target.target : target
+
const primaryKeyField = options?.primaryKeyField ?? (0, utils_1.getPrimaryKeyFields)(model);
const omitPrimaryKeyFromOrder = options?.omitPrimaryKeyFromOrder ?? false;
- const paginate = async (queryOptions) => {
+ const paginate = async (queryOptions, source) => {
+ if (!isAssociation) source = model;
const { order: orderOption, where, after, before, limit, ...restQueryOptions } = queryOptions;
const normalizedOrder = (0, utils_1.normalizeOrder)(orderOption, primaryKeyField, omitPrimaryKeyFromOrder);
const order = before ? (0, utils_1.reverseOrder)(normalizedOrder) : normalizedOrder;
@@ -33,9 +44,9 @@
...restQueryOptions,
};
const [instances, totalCount, cursorCount] = await Promise.all([
- model.findAll(paginationQueryOptions),
- model.count(totalCountQueryOptions),
- model.count(cursorCountQueryOptions),
+ source[findFunction](paginationQueryOptions),
+ source[countFunction](totalCountQueryOptions),
+ source[countFunction](cursorCountQueryOptions),
]);
if (before) {
instances.reverse();
Associations require an instance to start querying from, rather than just a model. Sequelize keeps track of model's associations in Model.associations
hash, along with all necessary data. Knowing that, and assuming we have Author -> Books
relation defined in Sequelize, the usage would be:
paginatedBooksFromAuthor = await makePaginate(Author.associations.books)
author = await Author.findOne()
await paginatedBooksFromAuthor( {order: 'id'}, author )
This is just an in-place patch for .js as a proof of concept showing that it works. Let me know if you'd be willing to merge if a proper PR was made.
from sequelize-cursor-pagination.
Seems like a quite clean solution. If you can turn this into a PR it would be highly appreciated!
from sequelize-cursor-pagination.
Related Issues (20)
- Can't handle one-to-many HOT 1
- On sequelize model option Paranoid: true, cursor generating different query. HOT 1
- totalCount? HOT 1
- Add UUID support HOT 1
- multiple order generates invalid query HOT 2
- Add support for HAVING clause HOT 1
- Add support for GROUP BY clause HOT 1
- UUID PK and order generates unwanted results HOT 1
- ` after: pagination.cursors.after` in the option always throw error "pagination was use before it was defined" HOT 1
- Incorrect comparison of primary key breaks pagination of common values
- composite primary keys HOT 2
- Option to override ordering parameters HOT 4
- Support ordering on associated models HOT 2
- Module '"sequelize"' has no exported member 'Attributes'. HOT 1
- Support ordering with DESC / ASC NULL FIRST HOT 1
- Using "group" option HOT 13
- Support custom model method name other than 'paginate' HOT 1
- Add option to disable count HOT 1
- nodejs.SequelizeDatabaseError: Undeclared variable: NaN
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 sequelize-cursor-pagination.