mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
+13
-3
@@ -1,5 +1,5 @@
|
||||
// utils/paginate.util.js
|
||||
const { Sequelize } = require('sequelize');
|
||||
const { Sequelize, Op } = require('sequelize');
|
||||
const { modelToAttributes } = require('./modelToAttributes.util');
|
||||
const { excludeJsonbPaths } = require('./excludeJSONBPaths.util');
|
||||
const { buildQuery } = require('./buildQuery.util');
|
||||
@@ -108,7 +108,10 @@ async function paginate(model, req, {
|
||||
const ALLOWED_FIELDS = attributes.map((a) => a.field);
|
||||
const computedFieldKeys = computedAttributes.map((c) => c.key);
|
||||
|
||||
const { where, order } = buildQuery(filters, sort, ALLOWED_FIELDS, computedFieldKeys);
|
||||
// Sequelize aliases the main model's table with the model's name by default
|
||||
// (e.g. `FROM "users" AS "User"`) — needed to qualify Sequelize.col()
|
||||
// references so they don't collide with same-named columns on joined tables.
|
||||
const { where, order } = buildQuery(filters, sort, ALLOWED_FIELDS, computedFieldKeys, model.name);
|
||||
|
||||
// Build attribute includes: jsonb + audit subqueries + any extra from findOptions
|
||||
const baseIncludes = jsonbAttr ? [jsonbAttr] : [];
|
||||
@@ -131,9 +134,16 @@ async function paginate(model, req, {
|
||||
|
||||
const { attributes: _attr, ...restFindOptions } = findOptions;
|
||||
|
||||
// Nest rather than shallow-spread — `where` and `restFindOptions.where` can
|
||||
// both carry an [Op.and] key (the same global Symbol), and spreading two
|
||||
// objects that share a Symbol key silently drops the first one's value.
|
||||
const combinedWhere = restFindOptions.where
|
||||
? { [Op.and]: [where, restFindOptions.where] }
|
||||
: where;
|
||||
|
||||
const { count, rows } = await model.findAndCountAll({
|
||||
...restFindOptions,
|
||||
where: { ...where, ...(restFindOptions.where ?? {}) },
|
||||
where: combinedWhere,
|
||||
order,
|
||||
limit,
|
||||
offset,
|
||||
|
||||
Reference in New Issue
Block a user