mirror of
https://github.com/rgrgogu/new_starr.git
synced 2026-09-27 00:12:54 +08:00
Adjusted
This commit is contained in:
@@ -147,26 +147,22 @@ function modelToAttributes(model, { jsonbSchemas = {}, exclude = [], timestampLa
|
||||
});
|
||||
}
|
||||
|
||||
// ─── Sort all fields globally by order ─────────────────────────────────────
|
||||
attributes.sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity));
|
||||
|
||||
// ─── Strip order from final output (frontend doesn't need it) ──────────────
|
||||
const sorted = attributes.map(({ order: _, ...rest }) => rest);
|
||||
|
||||
// ── Audit fields in correct sequence ────────────────────────────────────────
|
||||
for (const { field, type } of auditSequence) {
|
||||
for (const { field, type, order } of auditSequence) {
|
||||
if (exclude.includes(field)) continue;
|
||||
if (!rawAttrs[field]) continue; // skip if field doesn't exist on model
|
||||
|
||||
sorted.push({
|
||||
attributes.push({
|
||||
name: defaultTimestampLabels[field],
|
||||
type,
|
||||
field,
|
||||
order,
|
||||
options: resolveOptions(rawAttrs[field]?.type),
|
||||
});
|
||||
}
|
||||
|
||||
return sorted;
|
||||
// ── Sort by order — DO NOT strip order here, paginate.util does it ───────────
|
||||
return attributes.sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity));
|
||||
}
|
||||
|
||||
module.exports = { modelToAttributes };
|
||||
+41
-6
@@ -80,7 +80,8 @@ async function paginate(model, req, {
|
||||
jsonbSchemas = {},
|
||||
jsonbColumn = null,
|
||||
findOptions = {},
|
||||
auditOptions = null, // ← { mdl_Users, parentAlias }
|
||||
auditOptions = null, // ← { mdl_Users, parentAlias },
|
||||
computedAttributes = []
|
||||
} = {}) {
|
||||
const page = Math.max(PAGE_START, parseInt(req.query.page, 10) || PAGE_START);
|
||||
const limit = Math.min(parseInt(req.query.limit, 10) || PAGE_SIZE, MAX_LIMIT);
|
||||
@@ -104,7 +105,18 @@ async function paginate(model, req, {
|
||||
? auditInclude(auditOptions.mdl_Users, auditOptions.parentAlias).attributes
|
||||
: [];
|
||||
const extraIncludes = findOptions.attributes?.include ?? [];
|
||||
const mergedAttributeIncludes = [...baseIncludes, ...auditAttrs, ...extraIncludes];
|
||||
|
||||
// ← Convert { key, literal } objects into Sequelize [literal, alias] tuples
|
||||
const computedIncludes = computedAttributes
|
||||
.filter((c) => c.literal)
|
||||
.map((c) => [Sequelize.literal(c.literal), c.key]);
|
||||
|
||||
const mergedAttributeIncludes = [
|
||||
...baseIncludes,
|
||||
...auditAttrs,
|
||||
...extraIncludes,
|
||||
...computedIncludes, // ← now proper tuples
|
||||
];
|
||||
|
||||
const { attributes: _attr, ...restFindOptions } = findOptions;
|
||||
|
||||
@@ -122,12 +134,35 @@ async function paginate(model, req, {
|
||||
|
||||
const data = rows
|
||||
.map((row) => row.toJSON?.() ?? row)
|
||||
.map(auditIdToName);
|
||||
.map(auditIdToName)
|
||||
.map((row) => {
|
||||
// ← Coerce computed types after toJSON
|
||||
const result = { ...row };
|
||||
for (const { key, type } of computedAttributes) {
|
||||
if (result[key] !== undefined && result[key] !== null) {
|
||||
if (type === 'number') result[key] = parseInt(result[key], 10);
|
||||
if (type === 'float') result[key] = parseFloat(result[key]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
});
|
||||
|
||||
// ─── Flatten JSONB columns into dot-notation keys ──────────────────────────
|
||||
const jsonbCols = jsonbColumn ? [jsonbColumn] : [];
|
||||
const totalPages = Math.ceil(count / limit);
|
||||
|
||||
// ← Append computed metadata
|
||||
const computedMeta = computedAttributes.map(({ key, label, type, order: ord }) => ({
|
||||
name: label ?? key,
|
||||
type: type ?? 'text',
|
||||
field: key,
|
||||
order: ord ?? Infinity,
|
||||
options: {},
|
||||
}));
|
||||
|
||||
// ← Merge, sort, THEN strip order
|
||||
const mergedAttributes = [...attributes, ...computedMeta]
|
||||
.sort((a, b) => (a.order ?? Infinity) - (b.order ?? Infinity))
|
||||
.map(({ order: _, ...rest }) => rest);
|
||||
|
||||
return {
|
||||
data,
|
||||
pagination: {
|
||||
@@ -138,7 +173,7 @@ async function paginate(model, req, {
|
||||
hasPrevPage: page > PAGE_START,
|
||||
hasNextPage: page < totalPages,
|
||||
},
|
||||
attributes,
|
||||
attributes: mergedAttributes,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user