@strapi/database 5.43.0 → 5.45.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/entity-manager/regular-relations.d.ts +1 -1
- package/dist/entity-manager/regular-relations.d.ts.map +1 -1
- package/dist/entity-manager/regular-relations.js +3 -4
- package/dist/entity-manager/regular-relations.js.map +1 -1
- package/dist/entity-manager/regular-relations.mjs +3 -4
- package/dist/entity-manager/regular-relations.mjs.map +1 -1
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.d.ts.map +1 -1
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.js +19 -7
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.js.map +1 -1
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.mjs +19 -7
- package/dist/migrations/internal-migrations/5.0.0-02-document-id.mjs.map +1 -1
- package/dist/query/helpers/order-by.d.ts +23 -1
- package/dist/query/helpers/order-by.d.ts.map +1 -1
- package/dist/query/helpers/order-by.js +65 -8
- package/dist/query/helpers/order-by.js.map +1 -1
- package/dist/query/helpers/order-by.mjs +65 -9
- package/dist/query/helpers/order-by.mjs.map +1 -1
- package/dist/query/helpers/populate/apply.d.ts.map +1 -1
- package/dist/query/helpers/populate/apply.js +6 -2
- package/dist/query/helpers/populate/apply.js.map +1 -1
- package/dist/query/helpers/populate/apply.mjs +6 -2
- package/dist/query/helpers/populate/apply.mjs.map +1 -1
- package/dist/query/query-builder.d.ts.map +1 -1
- package/dist/query/query-builder.js +16 -4
- package/dist/query/query-builder.js.map +1 -1
- package/dist/query/query-builder.mjs +17 -5
- package/dist/query/query-builder.mjs.map +1 -1
- package/package.json +6 -6
|
@@ -7,11 +7,41 @@ var transform = require('./transform.js');
|
|
|
7
7
|
|
|
8
8
|
const COL_STRAPI_ROW_NUMBER = '__strapi_row_number';
|
|
9
9
|
const COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';
|
|
10
|
+
/**
|
|
11
|
+
* Builds a SQL CASE expression that maps each row to a numeric status rank:
|
|
12
|
+
* 0 = draft/created (no published sibling with same documentId [+ locale])
|
|
13
|
+
* 1 = modified (draft updated_at > published updated_at for same documentId [+ locale])
|
|
14
|
+
* 2 = published
|
|
15
|
+
*
|
|
16
|
+
* @param db - Database instance (used to access knex raw)
|
|
17
|
+
* @param tableName - Actual DB table name (used in correlated subqueries)
|
|
18
|
+
* @param tableAlias - Alias of the table in the outer query (e.g. "t0")
|
|
19
|
+
* @param isI18n - When true, adds a locale condition to avoid cross-locale contamination
|
|
20
|
+
*/ const buildStatusSortExpression = (db, tableName, tableAlias, isI18n = false)=>{
|
|
21
|
+
const localeCondition = isI18n ? ` AND sub.locale = ${tableAlias}.locale` : '';
|
|
22
|
+
return db.connection.raw(`CASE WHEN NOT EXISTS(SELECT 1 FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 0 WHEN ${tableAlias}.updated_at > (SELECT MAX(sub.updated_at) FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 1 ELSE 2 END`, [
|
|
23
|
+
tableName,
|
|
24
|
+
tableName
|
|
25
|
+
]);
|
|
26
|
+
};
|
|
10
27
|
const processOrderBy = (orderBy, ctx)=>{
|
|
11
28
|
const { db, uid, qb, alias } = ctx;
|
|
12
29
|
const meta = db.metadata.get(uid);
|
|
13
30
|
const { attributes } = meta;
|
|
14
31
|
if (typeof orderBy === 'string') {
|
|
32
|
+
if (orderBy === 'status') {
|
|
33
|
+
if (!attributes.publishedAt || !attributes.documentId) {
|
|
34
|
+
throw new Error(`Cannot order by status on model ${uid}: missing publishedAt or documentId`);
|
|
35
|
+
}
|
|
36
|
+
const isI18n = 'locale' in attributes;
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
rawExpression: 'status',
|
|
40
|
+
isI18n,
|
|
41
|
+
order: undefined
|
|
42
|
+
}
|
|
43
|
+
];
|
|
44
|
+
}
|
|
15
45
|
const attribute = attributes[orderBy];
|
|
16
46
|
if (!attribute) {
|
|
17
47
|
throw new Error(`Attribute ${orderBy} not found on model ${uid}`);
|
|
@@ -29,6 +59,19 @@ const processOrderBy = (orderBy, ctx)=>{
|
|
|
29
59
|
if (_.isPlainObject(orderBy)) {
|
|
30
60
|
return Object.entries(orderBy).flatMap(([key, direction])=>{
|
|
31
61
|
const value = orderBy[key];
|
|
62
|
+
if (key === 'status') {
|
|
63
|
+
if (!attributes.publishedAt || !attributes.documentId) {
|
|
64
|
+
throw new Error(`Cannot order by status on model ${uid}: missing publishedAt or documentId`);
|
|
65
|
+
}
|
|
66
|
+
const isI18n = 'locale' in attributes;
|
|
67
|
+
return [
|
|
68
|
+
{
|
|
69
|
+
rawExpression: 'status',
|
|
70
|
+
isI18n,
|
|
71
|
+
order: direction
|
|
72
|
+
}
|
|
73
|
+
];
|
|
74
|
+
}
|
|
32
75
|
const attribute = attributes[key];
|
|
33
76
|
if (!attribute) {
|
|
34
77
|
throw new Error(`Attribute ${key} not found on model ${uid}`);
|
|
@@ -77,6 +120,9 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
77
120
|
const { tableName } = db.metadata.get(uid);
|
|
78
121
|
// The orderBy is cloned to avoid unwanted mutations of the original object
|
|
79
122
|
const orderBy = _.cloneDeep(qb.state.orderBy);
|
|
123
|
+
// Separate column-based entries from raw-expression entries (e.g. status)
|
|
124
|
+
const columnOrderBy = orderBy.filter((ob)=>'column' in ob);
|
|
125
|
+
const rawExpressionOrderBy = orderBy.filter((ob)=>'rawExpression' in ob);
|
|
80
126
|
// 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection
|
|
81
127
|
// The connection reuse the original table name (aliased if needed)
|
|
82
128
|
const resultQueryAlias = qb.getAlias();
|
|
@@ -92,16 +138,17 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
92
138
|
.clear('select')// Pagination and sorting
|
|
93
139
|
.clear('order').clear('limit').clear('offset');
|
|
94
140
|
// Override the initial select and return only the columns needed for the partitioning.
|
|
141
|
+
// Only column-based orderBy entries are included here; raw expressions are applied later.
|
|
95
142
|
baseQuery.select(// Always select the row id for future manipulation
|
|
96
143
|
prefix(qb.alias, 'id'), // Select every column used in an order by clause, but alias it for future reference
|
|
97
144
|
// i.e. if t2.name is present in an order by clause:
|
|
98
145
|
// Then, "t2.name" will become "t2.name as __strapi_order_by__t2_name"
|
|
99
|
-
...
|
|
146
|
+
...columnOrderBy.map((orderByClause)=>alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)));
|
|
100
147
|
// 2. Create a sub-query callback to extract and sort the partitions using row number
|
|
101
148
|
const partitionedQueryAlias = qb.getAlias();
|
|
102
149
|
const selectRowsAsNumberedPartitions = (partitionedQuery)=>{
|
|
103
150
|
// Transform order by clause to their alias to reference them from baseQuery
|
|
104
|
-
const prefixedOrderBy =
|
|
151
|
+
const prefixedOrderBy = columnOrderBy.map((orderByClause)=>({
|
|
105
152
|
column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),
|
|
106
153
|
order: orderByClause.order
|
|
107
154
|
}));
|
|
@@ -119,9 +166,11 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
119
166
|
}).from(baseQuery.as(baseQueryAlias)).as(partitionedQueryAlias);
|
|
120
167
|
};
|
|
121
168
|
// 3. Create the final resultQuery query, that select and sort the wanted data using T
|
|
122
|
-
|
|
123
|
-
qb.state.
|
|
124
|
-
.
|
|
169
|
+
// Filter to string-only select items before diffing (Knex.Raw items are passed through as-is)
|
|
170
|
+
const stringSelect = qb.state.select.filter((s)=>typeof s === 'string');
|
|
171
|
+
const originalSelect = _.difference(stringSelect, // Remove column-based order by columns from the initial select (raw expressions are not in select)
|
|
172
|
+
columnOrderBy.map(_.prop('column')))// Alias everything in resultQuery
|
|
173
|
+
.map((col)=>`${resultQueryAlias}.${col}`);
|
|
125
174
|
resultQuery.select(originalSelect)// Join T to resultQuery to access sorted data
|
|
126
175
|
// Notes:
|
|
127
176
|
// - Only select the first row for each partition
|
|
@@ -142,13 +191,20 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
142
191
|
if (qb.state.first) {
|
|
143
192
|
resultQuery.first();
|
|
144
193
|
}
|
|
145
|
-
// Re-apply the sort using T values
|
|
194
|
+
// Re-apply the sort using T values (column-based), then append raw expression sorts.
|
|
195
|
+
// Cast to any[] because Knex's TS types don't accept Knex.Raw in the column position,
|
|
196
|
+
// even though Knex supports it at runtime.
|
|
146
197
|
resultQuery.orderBy([
|
|
147
|
-
// Transform "order by" clause to their T alias and prefix them with T alias
|
|
148
|
-
...
|
|
198
|
+
// Transform column-based "order by" clause to their T alias and prefix them with T alias
|
|
199
|
+
...columnOrderBy.map((orderByClause)=>({
|
|
149
200
|
column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),
|
|
150
201
|
order: orderByClause.order
|
|
151
202
|
})),
|
|
203
|
+
// Rebuild raw expression entries with the correct outer alias (resultQueryAlias)
|
|
204
|
+
...rawExpressionOrderBy.map((entry)=>({
|
|
205
|
+
column: buildStatusSortExpression(db, tableName, resultQueryAlias, entry.isI18n),
|
|
206
|
+
order: entry.order
|
|
207
|
+
})),
|
|
152
208
|
// Add T.id to the order by clause to get consistent results in case several rows have the exact same order
|
|
153
209
|
{
|
|
154
210
|
column: `${partitionedQueryAlias}.id`,
|
|
@@ -161,6 +217,7 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
161
217
|
const alias = _.curry((alias, value)=>`${value} as ${alias}`);
|
|
162
218
|
const prefix = _.curry((prefix, value)=>`${prefix}.${value}`);
|
|
163
219
|
|
|
220
|
+
exports.buildStatusSortExpression = buildStatusSortExpression;
|
|
164
221
|
exports.getStrapiOrderColumnAlias = getStrapiOrderColumnAlias;
|
|
165
222
|
exports.processOrderBy = processOrderBy;
|
|
166
223
|
exports.wrapWithDeepSort = wrapWithDeepSort;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order-by.js","sources":["../../../src/query/helpers/order-by.ts"],"sourcesContent":["import _ from 'lodash/fp';\nimport knex from 'knex';\n\nimport * as types from '../../utils/types';\nimport { createJoin } from './join';\nimport { toColumnName } from './transform';\n\nimport type { Ctx } from '../types';\n\ntype OrderByCtx = Ctx & { alias?: string };\ntype OrderBy = string | { [key: string]: 'asc' | 'desc' } | OrderBy[];\ntype OrderByValue = { column: string; order?: 'asc' | 'desc' };\n\nconst COL_STRAPI_ROW_NUMBER = '__strapi_row_number';\nconst COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';\n\nexport const processOrderBy = (orderBy: OrderBy, ctx: OrderByCtx): OrderByValue[] => {\n const { db, uid, qb, alias } = ctx;\n const meta = db.metadata.get(uid);\n const { attributes } = meta;\n\n if (typeof orderBy === 'string') {\n const attribute = attributes[orderBy];\n\n if (!attribute) {\n throw new Error(`Attribute ${orderBy} not found on model ${uid}`);\n }\n\n const columnName = toColumnName(meta, orderBy);\n\n return [{ column: qb.aliasColumn(columnName, alias) }];\n }\n\n if (Array.isArray(orderBy)) {\n return orderBy.flatMap((value) => processOrderBy(value, ctx));\n }\n\n if (_.isPlainObject(orderBy)) {\n return Object.entries(orderBy).flatMap(([key, direction]) => {\n const value = orderBy[key];\n const attribute = attributes[key];\n\n if (!attribute) {\n throw new Error(`Attribute ${key} not found on model ${uid}`);\n }\n\n if (types.isScalar(attribute.type)) {\n const columnName = toColumnName(meta, key);\n\n return { column: qb.aliasColumn(columnName, alias), order: direction };\n }\n\n if (attribute.type === 'relation' && 'target' in attribute) {\n const subAlias = createJoin(ctx, {\n alias: alias || qb.alias,\n attributeName: key,\n attribute,\n });\n\n return processOrderBy(value, {\n db,\n qb,\n alias: subAlias,\n uid: attribute.target,\n });\n }\n\n throw new Error(`You cannot order on ${attribute.type} types`);\n });\n }\n\n throw new Error('Invalid orderBy syntax');\n};\n\nexport const getStrapiOrderColumnAlias = (column: string) => {\n const trimmedColumnName = column.replaceAll('.', '_');\n\n return `${COL_STRAPI_ORDER_BY_PREFIX}__${trimmedColumnName}`;\n};\n\n/**\n * Wraps the original Knex query with deep sorting functionality.\n *\n * The function takes an original query and an OrderByCtx object as parameters and returns a new Knex query with deep sorting applied.\n */\nexport const wrapWithDeepSort = (originalQuery: knex.Knex.QueryBuilder, ctx: OrderByCtx) => {\n /**\n * Notes:\n * - The generated query has the following flow: baseQuery (filtered unsorted data) -> T (partitioned/sorted data) --> resultQuery (distinct, paginated, sorted data)\n * - Pagination and selection are transferred from the original query to the outer one to avoid pruning rows too early\n * - Filtering (where) has to be done in the deepest sub query possible to avoid processing invalid rows and corrupting the final results\n * - We assume that all necessary joins are done in the original query (`originalQuery`), and every needed column is available with the right name and alias.\n */\n\n const { db, qb, uid } = ctx;\n\n const { tableName } = db.metadata.get(uid);\n\n // The orderBy is cloned to avoid unwanted mutations of the original object\n const orderBy = _.cloneDeep<OrderByValue[]>(qb.state.orderBy);\n\n // 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection\n // The connection reuse the original table name (aliased if needed)\n const resultQueryAlias = qb.getAlias();\n const aliasedTableName = qb.mustUseAlias() ? alias(resultQueryAlias, tableName) : tableName;\n\n const resultQuery = db.getConnection(aliasedTableName);\n\n // 1. Clone the original query to create the sub-query (referenced as baseQuery) and avoid any mutation on the initial object\n const baseQuery = originalQuery.clone();\n const baseQueryAlias = qb.getAlias();\n\n // Clear unwanted statements from the sub-query 'baseQuery'\n // Note: `first()` is cleared through the combination of `baseQuery.clear('limit')` and calling `baseQuery.select(...)` again\n // Note: Those statements will be re-applied when duplicates are removed from the final selection\n baseQuery\n // Columns selection\n .clear('select')\n // Pagination and sorting\n .clear('order')\n .clear('limit')\n .clear('offset');\n\n // Override the initial select and return only the columns needed for the partitioning.\n baseQuery.select(\n // Always select the row id for future manipulation\n prefix(qb.alias, 'id'),\n // Select every column used in an order by clause, but alias it for future reference\n // i.e. if t2.name is present in an order by clause:\n // Then, \"t2.name\" will become \"t2.name as __strapi_order_by__t2_name\"\n ...orderBy.map((orderByClause) =>\n alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)\n )\n );\n\n // 2. Create a sub-query callback to extract and sort the partitions using row number\n const partitionedQueryAlias = qb.getAlias();\n\n const selectRowsAsNumberedPartitions = (partitionedQuery: knex.Knex.QueryBuilder) => {\n // Transform order by clause to their alias to reference them from baseQuery\n const prefixedOrderBy = orderBy.map((orderByClause) => ({\n column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n }));\n\n // partitionedQuery select must contain every column used for sorting\n const orderByColumns = prefixedOrderBy.map<string>(_.prop('column'));\n\n partitionedQuery\n .select(\n // Always select baseQuery.id\n prefix(baseQueryAlias, 'id'),\n // Sort columns\n ...orderByColumns\n )\n // The row number is used to assign an index to every row in every partition\n .rowNumber(COL_STRAPI_ROW_NUMBER, (subQuery) => {\n for (const orderByClause of prefixedOrderBy) {\n subQuery.orderBy(orderByClause.column, orderByClause.order, 'last');\n }\n\n // And each partition/group is created based on baseQuery.id\n subQuery.partitionBy(`${baseQueryAlias}.id`);\n })\n .from(baseQuery.as(baseQueryAlias))\n .as(partitionedQueryAlias);\n };\n\n // 3. Create the final resultQuery query, that select and sort the wanted data using T\n\n const originalSelect = _.difference(\n qb.state.select,\n // Remove order by columns from the initial select\n qb.state.orderBy.map(_.prop('column'))\n )\n // Alias everything in resultQuery\n .map(prefix(resultQueryAlias));\n\n resultQuery\n .select(originalSelect)\n // Join T to resultQuery to access sorted data\n // Notes:\n // - Only select the first row for each partition\n // - Since we're applying the \"where\" statement directly on baseQuery (and not on resultQuery), we're using an inner join to avoid unwanted rows\n .innerJoin(selectRowsAsNumberedPartitions, function () {\n this\n // Only select rows that are returned by T\n .on(`${partitionedQueryAlias}.id`, `${resultQueryAlias}.id`)\n // By only selecting the rows number equal to 1, we make sure we don't have duplicate, and that\n // we're selecting rows in the correct order amongst the groups created by the \"partition by\"\n .andOnVal(`${partitionedQueryAlias}.${COL_STRAPI_ROW_NUMBER}`, '=', 1);\n });\n\n // Re-apply pagination params\n\n if (qb.state.limit) {\n resultQuery.limit(qb.state.limit);\n }\n\n if (qb.state.offset) {\n resultQuery.offset(qb.state.offset);\n }\n\n if (qb.state.first) {\n resultQuery.first();\n }\n\n // Re-apply the sort using T values\n resultQuery.orderBy([\n // Transform \"order by\" clause to their T alias and prefix them with T alias\n ...orderBy.map((orderByClause) => ({\n column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n })),\n // Add T.id to the order by clause to get consistent results in case several rows have the exact same order\n { column: `${partitionedQueryAlias}.id`, order: 'asc' },\n ]);\n\n return resultQuery;\n};\n\n// Utils\nconst alias = _.curry((alias: string, value: string) => `${value} as ${alias}`);\nconst prefix = _.curry((prefix: string, value: string) => `${prefix}.${value}`);\n"],"names":["COL_STRAPI_ROW_NUMBER","COL_STRAPI_ORDER_BY_PREFIX","processOrderBy","orderBy","ctx","db","uid","qb","alias","meta","metadata","get","attributes","attribute","Error","columnName","toColumnName","column","aliasColumn","Array","isArray","flatMap","value","_","isPlainObject","Object","entries","key","direction","types","type","order","subAlias","createJoin","attributeName","target","getStrapiOrderColumnAlias","trimmedColumnName","replaceAll","wrapWithDeepSort","originalQuery","tableName","cloneDeep","state","resultQueryAlias","getAlias","aliasedTableName","mustUseAlias","resultQuery","getConnection","baseQuery","clone","baseQueryAlias","clear","select","prefix","map","orderByClause","partitionedQueryAlias","selectRowsAsNumberedPartitions","partitionedQuery","prefixedOrderBy","orderByColumns","prop","rowNumber","subQuery","partitionBy","from","as","originalSelect","difference","innerJoin","on","andOnVal","limit","offset","first","curry"],"mappings":";;;;;;;AAaA,MAAMA,qBAAAA,GAAwB,qBAAA;AAC9B,MAAMC,0BAAAA,GAA6B,mBAAA;AAE5B,MAAMC,cAAAA,GAAiB,CAACC,OAAAA,EAAkBC,GAAAA,GAAAA;IAC/C,MAAM,EAAEC,EAAE,EAAEC,GAAG,EAAEC,EAAE,EAA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AC9E,MAAM+C,MAAAA,GAAShC,CAAAA,CAAEsD,KAAK,CAAC,CAACtB,MAAAA,EAAgBjC,KAAAA,GAAkB,CAAA,EAAGiC,MAAAA,CAAO,CAAC,EAAEjC,KAAAA,CAAAA,CAAO,CAAA;;;;;;"}
|
|
1
|
+
{"version":3,"file":"order-by.js","sources":["../../../src/query/helpers/order-by.ts"],"sourcesContent":["import _ from 'lodash/fp';\nimport knex from 'knex';\n\nimport * as types from '../../utils/types';\nimport { createJoin } from './join';\nimport { toColumnName } from './transform';\n\nimport type { Ctx } from '../types';\n\ntype OrderByCtx = Ctx & { alias?: string };\ntype OrderBy = string | { [key: string]: 'asc' | 'desc' } | OrderBy[];\ntype OrderByColumnValue = { column: string; order?: 'asc' | 'desc' };\ntype OrderByRawValue = { rawExpression: 'status'; isI18n?: boolean; order?: 'asc' | 'desc' };\nexport type OrderByValue = OrderByColumnValue | OrderByRawValue;\n\nconst COL_STRAPI_ROW_NUMBER = '__strapi_row_number';\nconst COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';\n\n/**\n * Builds a SQL CASE expression that maps each row to a numeric status rank:\n * 0 = draft/created (no published sibling with same documentId [+ locale])\n * 1 = modified (draft updated_at > published updated_at for same documentId [+ locale])\n * 2 = published\n *\n * @param db - Database instance (used to access knex raw)\n * @param tableName - Actual DB table name (used in correlated subqueries)\n * @param tableAlias - Alias of the table in the outer query (e.g. \"t0\")\n * @param isI18n - When true, adds a locale condition to avoid cross-locale contamination\n */\nexport const buildStatusSortExpression = (\n db: { connection: { raw: (sql: string, bindings?: unknown[]) => knex.Knex.Raw } },\n tableName: string,\n tableAlias: string,\n isI18n = false\n): knex.Knex.Raw => {\n const localeCondition = isI18n ? ` AND sub.locale = ${tableAlias}.locale` : '';\n\n return db.connection.raw(\n `CASE WHEN NOT EXISTS(SELECT 1 FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 0 WHEN ${tableAlias}.updated_at > (SELECT MAX(sub.updated_at) FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 1 ELSE 2 END`,\n [tableName, tableName]\n );\n};\n\nexport const processOrderBy = (orderBy: OrderBy, ctx: OrderByCtx): OrderByValue[] => {\n const { db, uid, qb, alias } = ctx;\n const meta = db.metadata.get(uid);\n const { attributes } = meta;\n\n if (typeof orderBy === 'string') {\n if (orderBy === 'status') {\n if (!attributes.publishedAt || !attributes.documentId) {\n throw new Error(\n `Cannot order by status on model ${uid}: missing publishedAt or documentId`\n );\n }\n const isI18n = 'locale' in attributes;\n return [{ rawExpression: 'status' as const, isI18n, order: undefined }];\n }\n\n const attribute = attributes[orderBy];\n\n if (!attribute) {\n throw new Error(`Attribute ${orderBy} not found on model ${uid}`);\n }\n\n const columnName = toColumnName(meta, orderBy);\n\n return [{ column: qb.aliasColumn(columnName, alias) }];\n }\n\n if (Array.isArray(orderBy)) {\n return orderBy.flatMap((value) => processOrderBy(value, ctx));\n }\n\n if (_.isPlainObject(orderBy)) {\n return Object.entries(orderBy).flatMap(([key, direction]) => {\n const value = orderBy[key];\n\n if (key === 'status') {\n if (!attributes.publishedAt || !attributes.documentId) {\n throw new Error(\n `Cannot order by status on model ${uid}: missing publishedAt or documentId`\n );\n }\n const isI18n = 'locale' in attributes;\n return [{ rawExpression: 'status' as const, isI18n, order: direction as 'asc' | 'desc' }];\n }\n\n const attribute = attributes[key];\n\n if (!attribute) {\n throw new Error(`Attribute ${key} not found on model ${uid}`);\n }\n\n if (types.isScalar(attribute.type)) {\n const columnName = toColumnName(meta, key);\n\n return { column: qb.aliasColumn(columnName, alias), order: direction };\n }\n\n if (attribute.type === 'relation' && 'target' in attribute) {\n const subAlias = createJoin(ctx, {\n alias: alias || qb.alias,\n attributeName: key,\n attribute,\n });\n\n return processOrderBy(value, {\n db,\n qb,\n alias: subAlias,\n uid: attribute.target,\n });\n }\n\n throw new Error(`You cannot order on ${attribute.type} types`);\n });\n }\n\n throw new Error('Invalid orderBy syntax');\n};\n\nexport const getStrapiOrderColumnAlias = (column: string) => {\n const trimmedColumnName = column.replaceAll('.', '_');\n\n return `${COL_STRAPI_ORDER_BY_PREFIX}__${trimmedColumnName}`;\n};\n\n/**\n * Wraps the original Knex query with deep sorting functionality.\n *\n * The function takes an original query and an OrderByCtx object as parameters and returns a new Knex query with deep sorting applied.\n */\nexport const wrapWithDeepSort = (originalQuery: knex.Knex.QueryBuilder, ctx: OrderByCtx) => {\n /**\n * Notes:\n * - The generated query has the following flow: baseQuery (filtered unsorted data) -> T (partitioned/sorted data) --> resultQuery (distinct, paginated, sorted data)\n * - Pagination and selection are transferred from the original query to the outer one to avoid pruning rows too early\n * - Filtering (where) has to be done in the deepest sub query possible to avoid processing invalid rows and corrupting the final results\n * - We assume that all necessary joins are done in the original query (`originalQuery`), and every needed column is available with the right name and alias.\n */\n\n const { db, qb, uid } = ctx;\n\n const { tableName } = db.metadata.get(uid);\n\n // The orderBy is cloned to avoid unwanted mutations of the original object\n const orderBy = _.cloneDeep<OrderByValue[]>(qb.state.orderBy);\n\n // Separate column-based entries from raw-expression entries (e.g. status)\n const columnOrderBy = orderBy.filter((ob): ob is OrderByColumnValue => 'column' in ob);\n const rawExpressionOrderBy = orderBy.filter((ob): ob is OrderByRawValue => 'rawExpression' in ob);\n\n // 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection\n // The connection reuse the original table name (aliased if needed)\n const resultQueryAlias = qb.getAlias();\n const aliasedTableName = qb.mustUseAlias() ? alias(resultQueryAlias, tableName) : tableName;\n\n const resultQuery = db.getConnection(aliasedTableName);\n\n // 1. Clone the original query to create the sub-query (referenced as baseQuery) and avoid any mutation on the initial object\n const baseQuery = originalQuery.clone();\n const baseQueryAlias = qb.getAlias();\n\n // Clear unwanted statements from the sub-query 'baseQuery'\n // Note: `first()` is cleared through the combination of `baseQuery.clear('limit')` and calling `baseQuery.select(...)` again\n // Note: Those statements will be re-applied when duplicates are removed from the final selection\n baseQuery\n // Columns selection\n .clear('select')\n // Pagination and sorting\n .clear('order')\n .clear('limit')\n .clear('offset');\n\n // Override the initial select and return only the columns needed for the partitioning.\n // Only column-based orderBy entries are included here; raw expressions are applied later.\n baseQuery.select(\n // Always select the row id for future manipulation\n prefix(qb.alias, 'id'),\n // Select every column used in an order by clause, but alias it for future reference\n // i.e. if t2.name is present in an order by clause:\n // Then, \"t2.name\" will become \"t2.name as __strapi_order_by__t2_name\"\n ...columnOrderBy.map((orderByClause) =>\n alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)\n )\n );\n\n // 2. Create a sub-query callback to extract and sort the partitions using row number\n const partitionedQueryAlias = qb.getAlias();\n\n const selectRowsAsNumberedPartitions = (partitionedQuery: knex.Knex.QueryBuilder) => {\n // Transform order by clause to their alias to reference them from baseQuery\n const prefixedOrderBy = columnOrderBy.map((orderByClause) => ({\n column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n }));\n\n // partitionedQuery select must contain every column used for sorting\n const orderByColumns = prefixedOrderBy.map<string>(_.prop('column'));\n\n partitionedQuery\n .select(\n // Always select baseQuery.id\n prefix(baseQueryAlias, 'id'),\n // Sort columns\n ...orderByColumns\n )\n // The row number is used to assign an index to every row in every partition\n .rowNumber(COL_STRAPI_ROW_NUMBER, (subQuery) => {\n for (const orderByClause of prefixedOrderBy) {\n subQuery.orderBy(orderByClause.column, orderByClause.order, 'last');\n }\n\n // And each partition/group is created based on baseQuery.id\n subQuery.partitionBy(`${baseQueryAlias}.id`);\n })\n .from(baseQuery.as(baseQueryAlias))\n .as(partitionedQueryAlias);\n };\n\n // 3. Create the final resultQuery query, that select and sort the wanted data using T\n\n // Filter to string-only select items before diffing (Knex.Raw items are passed through as-is)\n const stringSelect = qb.state.select.filter((s): s is string => typeof s === 'string');\n const originalSelect = _.difference(\n stringSelect,\n // Remove column-based order by columns from the initial select (raw expressions are not in select)\n columnOrderBy.map(_.prop('column'))\n )\n // Alias everything in resultQuery\n .map((col) => `${resultQueryAlias}.${col}`);\n\n resultQuery\n .select(originalSelect)\n // Join T to resultQuery to access sorted data\n // Notes:\n // - Only select the first row for each partition\n // - Since we're applying the \"where\" statement directly on baseQuery (and not on resultQuery), we're using an inner join to avoid unwanted rows\n .innerJoin(selectRowsAsNumberedPartitions, function () {\n this\n // Only select rows that are returned by T\n .on(`${partitionedQueryAlias}.id`, `${resultQueryAlias}.id`)\n // By only selecting the rows number equal to 1, we make sure we don't have duplicate, and that\n // we're selecting rows in the correct order amongst the groups created by the \"partition by\"\n .andOnVal(`${partitionedQueryAlias}.${COL_STRAPI_ROW_NUMBER}`, '=', 1);\n });\n\n // Re-apply pagination params\n\n if (qb.state.limit) {\n resultQuery.limit(qb.state.limit);\n }\n\n if (qb.state.offset) {\n resultQuery.offset(qb.state.offset);\n }\n\n if (qb.state.first) {\n resultQuery.first();\n }\n\n // Re-apply the sort using T values (column-based), then append raw expression sorts.\n // Cast to any[] because Knex's TS types don't accept Knex.Raw in the column position,\n // even though Knex supports it at runtime.\n resultQuery.orderBy([\n // Transform column-based \"order by\" clause to their T alias and prefix them with T alias\n ...columnOrderBy.map((orderByClause) => ({\n column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n })),\n // Rebuild raw expression entries with the correct outer alias (resultQueryAlias)\n ...rawExpressionOrderBy.map((entry) => ({\n column: buildStatusSortExpression(db, tableName, resultQueryAlias, entry.isI18n),\n order: entry.order,\n })),\n // Add T.id to the order by clause to get consistent results in case several rows have the exact same order\n { column: `${partitionedQueryAlias}.id`, order: 'asc' },\n ] as any);\n\n return resultQuery;\n};\n\n// Utils\nconst alias = _.curry((alias: string, value: string) => `${value} as ${alias}`);\nconst prefix = _.curry((prefix: string, value: string) => `${prefix}.${value}`);\n"],"names":["COL_STRAPI_ROW_NUMBER","COL_STRAPI_ORDER_BY_PREFIX","buildStatusSortExpression","db","tableName","tableAlias","isI18n","localeCondition","connection","raw","processOrderBy","orderBy","ctx","uid","qb","alias","meta","metadata","get","attributes","publishedAt","documentId","Error","rawExpression","order","undefined","attribute","columnName","toColumnName","column","aliasColumn","Array","isArray","flatMap","value","_","isPlainObject","Object","entries","key","direction","types","type","subAlias","createJoin","attributeName","target","getStrapiOrderColumnAlias","trimmedColumnName","replaceAll","wrapWithDeepSort","originalQuery","cloneDeep","state","columnOrderBy","filter","ob","rawExpressionOrderBy","resultQueryAlias","getAlias","aliasedTableName","mustUseAlias","resultQuery","getConnection","baseQuery","clone","baseQueryAlias","clear","select","prefix","map","orderByClause","partitionedQueryAlias","selectRowsAsNumberedPartitions","partitionedQuery","prefixedOrderBy","orderByColumns","prop","rowNumber","subQuery","partitionBy","from","as","stringSelect","s","originalSelect","difference","col","innerJoin","on","andOnVal","limit","offset","first","entry","curry"],"mappings":";;;;;;;AAeA,MAAMA,qBAAAA,GAAwB,qBAAA;AAC9B,MAAMC,0BAAAA,GAA6B,mBAAA;AAEnC;;;;;;;;;;UAWaC,yBAAAA,GAA4B,CACvCC,IACAC,SAAAA,EACAC,UAAAA,EACAC,SAAS,KAAK,GAAA;IAEd,MAAMC,eAAAA,GAAkBD,SAAS,CAAC,kBAAkB,EAAED,UAAAA,CAAW,OAAO,CAAC,GAAG,EAAA;IAE5E,OAAOF,EAAAA,CAAGK,UAAU,CAACC,GAAG,CACtB,CAAC,kEAAkE,EAAEJ,UAAAA,CAAW,6CAA6C,EAAEE,gBAAgB,cAAc,EAAEF,UAAAA,CAAW,8EAA8E,EAAEA,UAAAA,CAAW,6CAA6C,EAAEE,eAAAA,CAAgB,mBAAmB,CAAC,EACxV;AAACH,QAAAA,SAAAA;AAAWA,QAAAA;AAAU,KAAA,CAAA;AAE1B;AAEO,MAAMM,cAAAA,GAAiB,CAACC,OAAAA,EAAkBC,GAAAA,GAAAA;IAC/C,MAAM,EAAET,EAAE,EAAEU,GAAG,EAAEC,EAAE,EAAEC,KAAK,EAAE,GAAGH,GAAAA;AAC/B,IAAA,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A;AACA,MAAM/C,KAAAA,GAAQoB,CAAAA,CAAE4D,KAAK,CAAC,CAAChF,KAAAA,EAAemB,KAAAA,GAAkB,CAAA,EAAGA,KAAAA,CAAM,IAAI,EAAEnB,KAAAA,CAAAA,CAAO,CAAA;AAC9E,MAAMsD,MAAAA,GAASlC,CAAAA,CAAE4D,KAAK,CAAC,CAAC1B,MAAAA,EAAgBnC,KAAAA,GAAkB,CAAA,EAAGmC,MAAAA,CAAO,CAAC,EAAEnC,KAAAA,CAAAA,CAAO,CAAA;;;;;;;"}
|
|
@@ -5,11 +5,41 @@ import { toColumnName } from './transform.mjs';
|
|
|
5
5
|
|
|
6
6
|
const COL_STRAPI_ROW_NUMBER = '__strapi_row_number';
|
|
7
7
|
const COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';
|
|
8
|
+
/**
|
|
9
|
+
* Builds a SQL CASE expression that maps each row to a numeric status rank:
|
|
10
|
+
* 0 = draft/created (no published sibling with same documentId [+ locale])
|
|
11
|
+
* 1 = modified (draft updated_at > published updated_at for same documentId [+ locale])
|
|
12
|
+
* 2 = published
|
|
13
|
+
*
|
|
14
|
+
* @param db - Database instance (used to access knex raw)
|
|
15
|
+
* @param tableName - Actual DB table name (used in correlated subqueries)
|
|
16
|
+
* @param tableAlias - Alias of the table in the outer query (e.g. "t0")
|
|
17
|
+
* @param isI18n - When true, adds a locale condition to avoid cross-locale contamination
|
|
18
|
+
*/ const buildStatusSortExpression = (db, tableName, tableAlias, isI18n = false)=>{
|
|
19
|
+
const localeCondition = isI18n ? ` AND sub.locale = ${tableAlias}.locale` : '';
|
|
20
|
+
return db.connection.raw(`CASE WHEN NOT EXISTS(SELECT 1 FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 0 WHEN ${tableAlias}.updated_at > (SELECT MAX(sub.updated_at) FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 1 ELSE 2 END`, [
|
|
21
|
+
tableName,
|
|
22
|
+
tableName
|
|
23
|
+
]);
|
|
24
|
+
};
|
|
8
25
|
const processOrderBy = (orderBy, ctx)=>{
|
|
9
26
|
const { db, uid, qb, alias } = ctx;
|
|
10
27
|
const meta = db.metadata.get(uid);
|
|
11
28
|
const { attributes } = meta;
|
|
12
29
|
if (typeof orderBy === 'string') {
|
|
30
|
+
if (orderBy === 'status') {
|
|
31
|
+
if (!attributes.publishedAt || !attributes.documentId) {
|
|
32
|
+
throw new Error(`Cannot order by status on model ${uid}: missing publishedAt or documentId`);
|
|
33
|
+
}
|
|
34
|
+
const isI18n = 'locale' in attributes;
|
|
35
|
+
return [
|
|
36
|
+
{
|
|
37
|
+
rawExpression: 'status',
|
|
38
|
+
isI18n,
|
|
39
|
+
order: undefined
|
|
40
|
+
}
|
|
41
|
+
];
|
|
42
|
+
}
|
|
13
43
|
const attribute = attributes[orderBy];
|
|
14
44
|
if (!attribute) {
|
|
15
45
|
throw new Error(`Attribute ${orderBy} not found on model ${uid}`);
|
|
@@ -27,6 +57,19 @@ const processOrderBy = (orderBy, ctx)=>{
|
|
|
27
57
|
if (_.isPlainObject(orderBy)) {
|
|
28
58
|
return Object.entries(orderBy).flatMap(([key, direction])=>{
|
|
29
59
|
const value = orderBy[key];
|
|
60
|
+
if (key === 'status') {
|
|
61
|
+
if (!attributes.publishedAt || !attributes.documentId) {
|
|
62
|
+
throw new Error(`Cannot order by status on model ${uid}: missing publishedAt or documentId`);
|
|
63
|
+
}
|
|
64
|
+
const isI18n = 'locale' in attributes;
|
|
65
|
+
return [
|
|
66
|
+
{
|
|
67
|
+
rawExpression: 'status',
|
|
68
|
+
isI18n,
|
|
69
|
+
order: direction
|
|
70
|
+
}
|
|
71
|
+
];
|
|
72
|
+
}
|
|
30
73
|
const attribute = attributes[key];
|
|
31
74
|
if (!attribute) {
|
|
32
75
|
throw new Error(`Attribute ${key} not found on model ${uid}`);
|
|
@@ -75,6 +118,9 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
75
118
|
const { tableName } = db.metadata.get(uid);
|
|
76
119
|
// The orderBy is cloned to avoid unwanted mutations of the original object
|
|
77
120
|
const orderBy = _.cloneDeep(qb.state.orderBy);
|
|
121
|
+
// Separate column-based entries from raw-expression entries (e.g. status)
|
|
122
|
+
const columnOrderBy = orderBy.filter((ob)=>'column' in ob);
|
|
123
|
+
const rawExpressionOrderBy = orderBy.filter((ob)=>'rawExpression' in ob);
|
|
78
124
|
// 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection
|
|
79
125
|
// The connection reuse the original table name (aliased if needed)
|
|
80
126
|
const resultQueryAlias = qb.getAlias();
|
|
@@ -90,16 +136,17 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
90
136
|
.clear('select')// Pagination and sorting
|
|
91
137
|
.clear('order').clear('limit').clear('offset');
|
|
92
138
|
// Override the initial select and return only the columns needed for the partitioning.
|
|
139
|
+
// Only column-based orderBy entries are included here; raw expressions are applied later.
|
|
93
140
|
baseQuery.select(// Always select the row id for future manipulation
|
|
94
141
|
prefix(qb.alias, 'id'), // Select every column used in an order by clause, but alias it for future reference
|
|
95
142
|
// i.e. if t2.name is present in an order by clause:
|
|
96
143
|
// Then, "t2.name" will become "t2.name as __strapi_order_by__t2_name"
|
|
97
|
-
...
|
|
144
|
+
...columnOrderBy.map((orderByClause)=>alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)));
|
|
98
145
|
// 2. Create a sub-query callback to extract and sort the partitions using row number
|
|
99
146
|
const partitionedQueryAlias = qb.getAlias();
|
|
100
147
|
const selectRowsAsNumberedPartitions = (partitionedQuery)=>{
|
|
101
148
|
// Transform order by clause to their alias to reference them from baseQuery
|
|
102
|
-
const prefixedOrderBy =
|
|
149
|
+
const prefixedOrderBy = columnOrderBy.map((orderByClause)=>({
|
|
103
150
|
column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),
|
|
104
151
|
order: orderByClause.order
|
|
105
152
|
}));
|
|
@@ -117,9 +164,11 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
117
164
|
}).from(baseQuery.as(baseQueryAlias)).as(partitionedQueryAlias);
|
|
118
165
|
};
|
|
119
166
|
// 3. Create the final resultQuery query, that select and sort the wanted data using T
|
|
120
|
-
|
|
121
|
-
qb.state.
|
|
122
|
-
.
|
|
167
|
+
// Filter to string-only select items before diffing (Knex.Raw items are passed through as-is)
|
|
168
|
+
const stringSelect = qb.state.select.filter((s)=>typeof s === 'string');
|
|
169
|
+
const originalSelect = _.difference(stringSelect, // Remove column-based order by columns from the initial select (raw expressions are not in select)
|
|
170
|
+
columnOrderBy.map(_.prop('column')))// Alias everything in resultQuery
|
|
171
|
+
.map((col)=>`${resultQueryAlias}.${col}`);
|
|
123
172
|
resultQuery.select(originalSelect)// Join T to resultQuery to access sorted data
|
|
124
173
|
// Notes:
|
|
125
174
|
// - Only select the first row for each partition
|
|
@@ -140,13 +189,20 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
140
189
|
if (qb.state.first) {
|
|
141
190
|
resultQuery.first();
|
|
142
191
|
}
|
|
143
|
-
// Re-apply the sort using T values
|
|
192
|
+
// Re-apply the sort using T values (column-based), then append raw expression sorts.
|
|
193
|
+
// Cast to any[] because Knex's TS types don't accept Knex.Raw in the column position,
|
|
194
|
+
// even though Knex supports it at runtime.
|
|
144
195
|
resultQuery.orderBy([
|
|
145
|
-
// Transform "order by" clause to their T alias and prefix them with T alias
|
|
146
|
-
...
|
|
196
|
+
// Transform column-based "order by" clause to their T alias and prefix them with T alias
|
|
197
|
+
...columnOrderBy.map((orderByClause)=>({
|
|
147
198
|
column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),
|
|
148
199
|
order: orderByClause.order
|
|
149
200
|
})),
|
|
201
|
+
// Rebuild raw expression entries with the correct outer alias (resultQueryAlias)
|
|
202
|
+
...rawExpressionOrderBy.map((entry)=>({
|
|
203
|
+
column: buildStatusSortExpression(db, tableName, resultQueryAlias, entry.isI18n),
|
|
204
|
+
order: entry.order
|
|
205
|
+
})),
|
|
150
206
|
// Add T.id to the order by clause to get consistent results in case several rows have the exact same order
|
|
151
207
|
{
|
|
152
208
|
column: `${partitionedQueryAlias}.id`,
|
|
@@ -159,5 +215,5 @@ const getStrapiOrderColumnAlias = (column)=>{
|
|
|
159
215
|
const alias = _.curry((alias, value)=>`${value} as ${alias}`);
|
|
160
216
|
const prefix = _.curry((prefix, value)=>`${prefix}.${value}`);
|
|
161
217
|
|
|
162
|
-
export { getStrapiOrderColumnAlias, processOrderBy, wrapWithDeepSort };
|
|
218
|
+
export { buildStatusSortExpression, getStrapiOrderColumnAlias, processOrderBy, wrapWithDeepSort };
|
|
163
219
|
//# sourceMappingURL=order-by.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"order-by.mjs","sources":["../../../src/query/helpers/order-by.ts"],"sourcesContent":["import _ from 'lodash/fp';\nimport knex from 'knex';\n\nimport * as types from '../../utils/types';\nimport { createJoin } from './join';\nimport { toColumnName } from './transform';\n\nimport type { Ctx } from '../types';\n\ntype OrderByCtx = Ctx & { alias?: string };\ntype OrderBy = string | { [key: string]: 'asc' | 'desc' } | OrderBy[];\ntype OrderByValue = { column: string; order?: 'asc' | 'desc' };\n\nconst COL_STRAPI_ROW_NUMBER = '__strapi_row_number';\nconst COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';\n\nexport const processOrderBy = (orderBy: OrderBy, ctx: OrderByCtx): OrderByValue[] => {\n const { db, uid, qb, alias } = ctx;\n const meta = db.metadata.get(uid);\n const { attributes } = meta;\n\n if (typeof orderBy === 'string') {\n const attribute = attributes[orderBy];\n\n if (!attribute) {\n throw new Error(`Attribute ${orderBy} not found on model ${uid}`);\n }\n\n const columnName = toColumnName(meta, orderBy);\n\n return [{ column: qb.aliasColumn(columnName, alias) }];\n }\n\n if (Array.isArray(orderBy)) {\n return orderBy.flatMap((value) => processOrderBy(value, ctx));\n }\n\n if (_.isPlainObject(orderBy)) {\n return Object.entries(orderBy).flatMap(([key, direction]) => {\n const value = orderBy[key];\n const attribute = attributes[key];\n\n if (!attribute) {\n throw new Error(`Attribute ${key} not found on model ${uid}`);\n }\n\n if (types.isScalar(attribute.type)) {\n const columnName = toColumnName(meta, key);\n\n return { column: qb.aliasColumn(columnName, alias), order: direction };\n }\n\n if (attribute.type === 'relation' && 'target' in attribute) {\n const subAlias = createJoin(ctx, {\n alias: alias || qb.alias,\n attributeName: key,\n attribute,\n });\n\n return processOrderBy(value, {\n db,\n qb,\n alias: subAlias,\n uid: attribute.target,\n });\n }\n\n throw new Error(`You cannot order on ${attribute.type} types`);\n });\n }\n\n throw new Error('Invalid orderBy syntax');\n};\n\nexport const getStrapiOrderColumnAlias = (column: string) => {\n const trimmedColumnName = column.replaceAll('.', '_');\n\n return `${COL_STRAPI_ORDER_BY_PREFIX}__${trimmedColumnName}`;\n};\n\n/**\n * Wraps the original Knex query with deep sorting functionality.\n *\n * The function takes an original query and an OrderByCtx object as parameters and returns a new Knex query with deep sorting applied.\n */\nexport const wrapWithDeepSort = (originalQuery: knex.Knex.QueryBuilder, ctx: OrderByCtx) => {\n /**\n * Notes:\n * - The generated query has the following flow: baseQuery (filtered unsorted data) -> T (partitioned/sorted data) --> resultQuery (distinct, paginated, sorted data)\n * - Pagination and selection are transferred from the original query to the outer one to avoid pruning rows too early\n * - Filtering (where) has to be done in the deepest sub query possible to avoid processing invalid rows and corrupting the final results\n * - We assume that all necessary joins are done in the original query (`originalQuery`), and every needed column is available with the right name and alias.\n */\n\n const { db, qb, uid } = ctx;\n\n const { tableName } = db.metadata.get(uid);\n\n // The orderBy is cloned to avoid unwanted mutations of the original object\n const orderBy = _.cloneDeep<OrderByValue[]>(qb.state.orderBy);\n\n // 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection\n // The connection reuse the original table name (aliased if needed)\n const resultQueryAlias = qb.getAlias();\n const aliasedTableName = qb.mustUseAlias() ? alias(resultQueryAlias, tableName) : tableName;\n\n const resultQuery = db.getConnection(aliasedTableName);\n\n // 1. Clone the original query to create the sub-query (referenced as baseQuery) and avoid any mutation on the initial object\n const baseQuery = originalQuery.clone();\n const baseQueryAlias = qb.getAlias();\n\n // Clear unwanted statements from the sub-query 'baseQuery'\n // Note: `first()` is cleared through the combination of `baseQuery.clear('limit')` and calling `baseQuery.select(...)` again\n // Note: Those statements will be re-applied when duplicates are removed from the final selection\n baseQuery\n // Columns selection\n .clear('select')\n // Pagination and sorting\n .clear('order')\n .clear('limit')\n .clear('offset');\n\n // Override the initial select and return only the columns needed for the partitioning.\n baseQuery.select(\n // Always select the row id for future manipulation\n prefix(qb.alias, 'id'),\n // Select every column used in an order by clause, but alias it for future reference\n // i.e. if t2.name is present in an order by clause:\n // Then, \"t2.name\" will become \"t2.name as __strapi_order_by__t2_name\"\n ...orderBy.map((orderByClause) =>\n alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)\n )\n );\n\n // 2. Create a sub-query callback to extract and sort the partitions using row number\n const partitionedQueryAlias = qb.getAlias();\n\n const selectRowsAsNumberedPartitions = (partitionedQuery: knex.Knex.QueryBuilder) => {\n // Transform order by clause to their alias to reference them from baseQuery\n const prefixedOrderBy = orderBy.map((orderByClause) => ({\n column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n }));\n\n // partitionedQuery select must contain every column used for sorting\n const orderByColumns = prefixedOrderBy.map<string>(_.prop('column'));\n\n partitionedQuery\n .select(\n // Always select baseQuery.id\n prefix(baseQueryAlias, 'id'),\n // Sort columns\n ...orderByColumns\n )\n // The row number is used to assign an index to every row in every partition\n .rowNumber(COL_STRAPI_ROW_NUMBER, (subQuery) => {\n for (const orderByClause of prefixedOrderBy) {\n subQuery.orderBy(orderByClause.column, orderByClause.order, 'last');\n }\n\n // And each partition/group is created based on baseQuery.id\n subQuery.partitionBy(`${baseQueryAlias}.id`);\n })\n .from(baseQuery.as(baseQueryAlias))\n .as(partitionedQueryAlias);\n };\n\n // 3. Create the final resultQuery query, that select and sort the wanted data using T\n\n const originalSelect = _.difference(\n qb.state.select,\n // Remove order by columns from the initial select\n qb.state.orderBy.map(_.prop('column'))\n )\n // Alias everything in resultQuery\n .map(prefix(resultQueryAlias));\n\n resultQuery\n .select(originalSelect)\n // Join T to resultQuery to access sorted data\n // Notes:\n // - Only select the first row for each partition\n // - Since we're applying the \"where\" statement directly on baseQuery (and not on resultQuery), we're using an inner join to avoid unwanted rows\n .innerJoin(selectRowsAsNumberedPartitions, function () {\n this\n // Only select rows that are returned by T\n .on(`${partitionedQueryAlias}.id`, `${resultQueryAlias}.id`)\n // By only selecting the rows number equal to 1, we make sure we don't have duplicate, and that\n // we're selecting rows in the correct order amongst the groups created by the \"partition by\"\n .andOnVal(`${partitionedQueryAlias}.${COL_STRAPI_ROW_NUMBER}`, '=', 1);\n });\n\n // Re-apply pagination params\n\n if (qb.state.limit) {\n resultQuery.limit(qb.state.limit);\n }\n\n if (qb.state.offset) {\n resultQuery.offset(qb.state.offset);\n }\n\n if (qb.state.first) {\n resultQuery.first();\n }\n\n // Re-apply the sort using T values\n resultQuery.orderBy([\n // Transform \"order by\" clause to their T alias and prefix them with T alias\n ...orderBy.map((orderByClause) => ({\n column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n })),\n // Add T.id to the order by clause to get consistent results in case several rows have the exact same order\n { column: `${partitionedQueryAlias}.id`, order: 'asc' },\n ]);\n\n return resultQuery;\n};\n\n// Utils\nconst alias = _.curry((alias: string, value: string) => `${value} as ${alias}`);\nconst prefix = _.curry((prefix: string, value: string) => `${prefix}.${value}`);\n"],"names":["COL_STRAPI_ROW_NUMBER","COL_STRAPI_ORDER_BY_PREFIX","processOrderBy","orderBy","ctx","db","uid","qb","alias","meta","metadata","get","attributes","attribute","Error","columnName","toColumnName","column","aliasColumn","Array","isArray","flatMap","value","_","isPlainObject","Object","entries","key","direction","types","type","order","subAlias","createJoin","attributeName","target","getStrapiOrderColumnAlias","trimmedColumnName","replaceAll","wrapWithDeepSort","originalQuery","tableName","cloneDeep","state","resultQueryAlias","getAlias","aliasedTableName","mustUseAlias","resultQuery","getConnection","baseQuery","clone","baseQueryAlias","clear","select","prefix","map","orderByClause","partitionedQueryAlias","selectRowsAsNumberedPartitions","partitionedQuery","prefixedOrderBy","orderByColumns","prop","rowNumber","subQuery","partitionBy","from","as","originalSelect","difference","innerJoin","on","andOnVal","limit","offset","first","curry"],"mappings":";;;;;AAaA,MAAMA,qBAAAA,GAAwB,qBAAA;AAC9B,MAAMC,0BAAAA,GAA6B,mBAAA;AAE5B,MAAMC,cAAAA,GAAiB,CAACC,OAAAA,EAAkBC,GAAAA,GAAAA;IAC/C,MAAM,EAAEC,EAAE,EAAEC,GAAG,EAAEC,EAAE,EAAEC,KAAK,EAAE,GAAGJ,GAAAA;AAC/B,IAAA,MAAMK,IAAAA,GAAOJ,EAAAA,CAAGK,QAAQ,CAACC,GAAG,CAACL,GAAAA,CAAAA;IAC7B,MAAM,EAAEM,UAAU,EAAE,GAAGH,IAAAA;IAEvB,IAAI,OAAON,YAAY,QAAA,EAAU;QAC/B,MAAMU,SAAAA,GAAYD,UAAU,CAACT,OAAAA,CAAQ;AAErC,QAAA,IAAI,CAACU,SAAAA,EAAW;YACd,MAAM,IAAIC,MAAM,CAAC,UAAU,EAAEX,OAAAA,CAAQ,oBAAoB,EAAEG,GAAAA,CAAAA,CAAK,CAAA;AAClE,QAAA;QAEA,MAAMS,UAAAA,GAAaC,aAAaP,IAAAA,EAAMN,OAAAA,CAAAA;QAEtC,OAAO;AAAC,YAAA;gBAAEc,MAAAA,EAAQV,EAAAA,CAAGW,WAAW,CAACH,UAAAA,EAAYP,KAAAA;AAAO;AAAE,SAAA;AACxD,IAAA;IAEA,IAAIW,KAAAA,CAAMC,OAAO,CAACjB,OAAAA,CAAAA,EAAU;AAC1B,QAAA,OAAOA,QAAQkB,OAAO,CAAC,CAAC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qD,GAAG,CAAC,CAACC,aAAAA,IAAmB;AACjCxC,gBAAAA,MAAAA,EAAQsC,MAAAA,CAAOG,qBAAAA,EAAuBtB,yBAAAA,CAA0BqB,aAAAA,CAAcxC,MAAM,CAAA,CAAA;AACpFc,gBAAAA,KAAAA,EAAO0B,cAAc1B;aACvB,CAAA,CAAA;;AAEA,QAAA;YAAEd,MAAAA,EAAQ,CAAA,EAAGyC,qBAAAA,CAAsB,GAAG,CAAC;YAAE3B,KAAAA,EAAO;AAAM;AACvD,KAAA,CAAA;IAED,OAAOiB,WAAAA;AACT;AAEA;AACA,MAAMxC,KAAAA,GAAQe,CAAAA,CAAEsD,KAAK,CAAC,CAACrE,KAAAA,EAAec,KAAAA,GAAkB,CAAA,EAAGA,KAAAA,CAAM,IAAI,EAAEd,KAAAA,CAAAA,CAAO,CAAA;AAC9E,MAAM+C,MAAAA,GAAShC,CAAAA,CAAEsD,KAAK,CAAC,CAACtB,MAAAA,EAAgBjC,KAAAA,GAAkB,CAAA,EAAGiC,MAAAA,CAAO,CAAC,EAAEjC,KAAAA,CAAAA,CAAO,CAAA;;;;"}
|
|
1
|
+
{"version":3,"file":"order-by.mjs","sources":["../../../src/query/helpers/order-by.ts"],"sourcesContent":["import _ from 'lodash/fp';\nimport knex from 'knex';\n\nimport * as types from '../../utils/types';\nimport { createJoin } from './join';\nimport { toColumnName } from './transform';\n\nimport type { Ctx } from '../types';\n\ntype OrderByCtx = Ctx & { alias?: string };\ntype OrderBy = string | { [key: string]: 'asc' | 'desc' } | OrderBy[];\ntype OrderByColumnValue = { column: string; order?: 'asc' | 'desc' };\ntype OrderByRawValue = { rawExpression: 'status'; isI18n?: boolean; order?: 'asc' | 'desc' };\nexport type OrderByValue = OrderByColumnValue | OrderByRawValue;\n\nconst COL_STRAPI_ROW_NUMBER = '__strapi_row_number';\nconst COL_STRAPI_ORDER_BY_PREFIX = '__strapi_order_by';\n\n/**\n * Builds a SQL CASE expression that maps each row to a numeric status rank:\n * 0 = draft/created (no published sibling with same documentId [+ locale])\n * 1 = modified (draft updated_at > published updated_at for same documentId [+ locale])\n * 2 = published\n *\n * @param db - Database instance (used to access knex raw)\n * @param tableName - Actual DB table name (used in correlated subqueries)\n * @param tableAlias - Alias of the table in the outer query (e.g. \"t0\")\n * @param isI18n - When true, adds a locale condition to avoid cross-locale contamination\n */\nexport const buildStatusSortExpression = (\n db: { connection: { raw: (sql: string, bindings?: unknown[]) => knex.Knex.Raw } },\n tableName: string,\n tableAlias: string,\n isI18n = false\n): knex.Knex.Raw => {\n const localeCondition = isI18n ? ` AND sub.locale = ${tableAlias}.locale` : '';\n\n return db.connection.raw(\n `CASE WHEN NOT EXISTS(SELECT 1 FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 0 WHEN ${tableAlias}.updated_at > (SELECT MAX(sub.updated_at) FROM ?? sub WHERE sub.document_id = ${tableAlias}.document_id AND sub.published_at IS NOT NULL${localeCondition}) THEN 1 ELSE 2 END`,\n [tableName, tableName]\n );\n};\n\nexport const processOrderBy = (orderBy: OrderBy, ctx: OrderByCtx): OrderByValue[] => {\n const { db, uid, qb, alias } = ctx;\n const meta = db.metadata.get(uid);\n const { attributes } = meta;\n\n if (typeof orderBy === 'string') {\n if (orderBy === 'status') {\n if (!attributes.publishedAt || !attributes.documentId) {\n throw new Error(\n `Cannot order by status on model ${uid}: missing publishedAt or documentId`\n );\n }\n const isI18n = 'locale' in attributes;\n return [{ rawExpression: 'status' as const, isI18n, order: undefined }];\n }\n\n const attribute = attributes[orderBy];\n\n if (!attribute) {\n throw new Error(`Attribute ${orderBy} not found on model ${uid}`);\n }\n\n const columnName = toColumnName(meta, orderBy);\n\n return [{ column: qb.aliasColumn(columnName, alias) }];\n }\n\n if (Array.isArray(orderBy)) {\n return orderBy.flatMap((value) => processOrderBy(value, ctx));\n }\n\n if (_.isPlainObject(orderBy)) {\n return Object.entries(orderBy).flatMap(([key, direction]) => {\n const value = orderBy[key];\n\n if (key === 'status') {\n if (!attributes.publishedAt || !attributes.documentId) {\n throw new Error(\n `Cannot order by status on model ${uid}: missing publishedAt or documentId`\n );\n }\n const isI18n = 'locale' in attributes;\n return [{ rawExpression: 'status' as const, isI18n, order: direction as 'asc' | 'desc' }];\n }\n\n const attribute = attributes[key];\n\n if (!attribute) {\n throw new Error(`Attribute ${key} not found on model ${uid}`);\n }\n\n if (types.isScalar(attribute.type)) {\n const columnName = toColumnName(meta, key);\n\n return { column: qb.aliasColumn(columnName, alias), order: direction };\n }\n\n if (attribute.type === 'relation' && 'target' in attribute) {\n const subAlias = createJoin(ctx, {\n alias: alias || qb.alias,\n attributeName: key,\n attribute,\n });\n\n return processOrderBy(value, {\n db,\n qb,\n alias: subAlias,\n uid: attribute.target,\n });\n }\n\n throw new Error(`You cannot order on ${attribute.type} types`);\n });\n }\n\n throw new Error('Invalid orderBy syntax');\n};\n\nexport const getStrapiOrderColumnAlias = (column: string) => {\n const trimmedColumnName = column.replaceAll('.', '_');\n\n return `${COL_STRAPI_ORDER_BY_PREFIX}__${trimmedColumnName}`;\n};\n\n/**\n * Wraps the original Knex query with deep sorting functionality.\n *\n * The function takes an original query and an OrderByCtx object as parameters and returns a new Knex query with deep sorting applied.\n */\nexport const wrapWithDeepSort = (originalQuery: knex.Knex.QueryBuilder, ctx: OrderByCtx) => {\n /**\n * Notes:\n * - The generated query has the following flow: baseQuery (filtered unsorted data) -> T (partitioned/sorted data) --> resultQuery (distinct, paginated, sorted data)\n * - Pagination and selection are transferred from the original query to the outer one to avoid pruning rows too early\n * - Filtering (where) has to be done in the deepest sub query possible to avoid processing invalid rows and corrupting the final results\n * - We assume that all necessary joins are done in the original query (`originalQuery`), and every needed column is available with the right name and alias.\n */\n\n const { db, qb, uid } = ctx;\n\n const { tableName } = db.metadata.get(uid);\n\n // The orderBy is cloned to avoid unwanted mutations of the original object\n const orderBy = _.cloneDeep<OrderByValue[]>(qb.state.orderBy);\n\n // Separate column-based entries from raw-expression entries (e.g. status)\n const columnOrderBy = orderBy.filter((ob): ob is OrderByColumnValue => 'column' in ob);\n const rawExpressionOrderBy = orderBy.filter((ob): ob is OrderByRawValue => 'rawExpression' in ob);\n\n // 0. Init a new Knex query instance (referenced as resultQuery) using the DB connection\n // The connection reuse the original table name (aliased if needed)\n const resultQueryAlias = qb.getAlias();\n const aliasedTableName = qb.mustUseAlias() ? alias(resultQueryAlias, tableName) : tableName;\n\n const resultQuery = db.getConnection(aliasedTableName);\n\n // 1. Clone the original query to create the sub-query (referenced as baseQuery) and avoid any mutation on the initial object\n const baseQuery = originalQuery.clone();\n const baseQueryAlias = qb.getAlias();\n\n // Clear unwanted statements from the sub-query 'baseQuery'\n // Note: `first()` is cleared through the combination of `baseQuery.clear('limit')` and calling `baseQuery.select(...)` again\n // Note: Those statements will be re-applied when duplicates are removed from the final selection\n baseQuery\n // Columns selection\n .clear('select')\n // Pagination and sorting\n .clear('order')\n .clear('limit')\n .clear('offset');\n\n // Override the initial select and return only the columns needed for the partitioning.\n // Only column-based orderBy entries are included here; raw expressions are applied later.\n baseQuery.select(\n // Always select the row id for future manipulation\n prefix(qb.alias, 'id'),\n // Select every column used in an order by clause, but alias it for future reference\n // i.e. if t2.name is present in an order by clause:\n // Then, \"t2.name\" will become \"t2.name as __strapi_order_by__t2_name\"\n ...columnOrderBy.map((orderByClause) =>\n alias(getStrapiOrderColumnAlias(orderByClause.column), orderByClause.column)\n )\n );\n\n // 2. Create a sub-query callback to extract and sort the partitions using row number\n const partitionedQueryAlias = qb.getAlias();\n\n const selectRowsAsNumberedPartitions = (partitionedQuery: knex.Knex.QueryBuilder) => {\n // Transform order by clause to their alias to reference them from baseQuery\n const prefixedOrderBy = columnOrderBy.map((orderByClause) => ({\n column: prefix(baseQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n }));\n\n // partitionedQuery select must contain every column used for sorting\n const orderByColumns = prefixedOrderBy.map<string>(_.prop('column'));\n\n partitionedQuery\n .select(\n // Always select baseQuery.id\n prefix(baseQueryAlias, 'id'),\n // Sort columns\n ...orderByColumns\n )\n // The row number is used to assign an index to every row in every partition\n .rowNumber(COL_STRAPI_ROW_NUMBER, (subQuery) => {\n for (const orderByClause of prefixedOrderBy) {\n subQuery.orderBy(orderByClause.column, orderByClause.order, 'last');\n }\n\n // And each partition/group is created based on baseQuery.id\n subQuery.partitionBy(`${baseQueryAlias}.id`);\n })\n .from(baseQuery.as(baseQueryAlias))\n .as(partitionedQueryAlias);\n };\n\n // 3. Create the final resultQuery query, that select and sort the wanted data using T\n\n // Filter to string-only select items before diffing (Knex.Raw items are passed through as-is)\n const stringSelect = qb.state.select.filter((s): s is string => typeof s === 'string');\n const originalSelect = _.difference(\n stringSelect,\n // Remove column-based order by columns from the initial select (raw expressions are not in select)\n columnOrderBy.map(_.prop('column'))\n )\n // Alias everything in resultQuery\n .map((col) => `${resultQueryAlias}.${col}`);\n\n resultQuery\n .select(originalSelect)\n // Join T to resultQuery to access sorted data\n // Notes:\n // - Only select the first row for each partition\n // - Since we're applying the \"where\" statement directly on baseQuery (and not on resultQuery), we're using an inner join to avoid unwanted rows\n .innerJoin(selectRowsAsNumberedPartitions, function () {\n this\n // Only select rows that are returned by T\n .on(`${partitionedQueryAlias}.id`, `${resultQueryAlias}.id`)\n // By only selecting the rows number equal to 1, we make sure we don't have duplicate, and that\n // we're selecting rows in the correct order amongst the groups created by the \"partition by\"\n .andOnVal(`${partitionedQueryAlias}.${COL_STRAPI_ROW_NUMBER}`, '=', 1);\n });\n\n // Re-apply pagination params\n\n if (qb.state.limit) {\n resultQuery.limit(qb.state.limit);\n }\n\n if (qb.state.offset) {\n resultQuery.offset(qb.state.offset);\n }\n\n if (qb.state.first) {\n resultQuery.first();\n }\n\n // Re-apply the sort using T values (column-based), then append raw expression sorts.\n // Cast to any[] because Knex's TS types don't accept Knex.Raw in the column position,\n // even though Knex supports it at runtime.\n resultQuery.orderBy([\n // Transform column-based \"order by\" clause to their T alias and prefix them with T alias\n ...columnOrderBy.map((orderByClause) => ({\n column: prefix(partitionedQueryAlias, getStrapiOrderColumnAlias(orderByClause.column)),\n order: orderByClause.order,\n })),\n // Rebuild raw expression entries with the correct outer alias (resultQueryAlias)\n ...rawExpressionOrderBy.map((entry) => ({\n column: buildStatusSortExpression(db, tableName, resultQueryAlias, entry.isI18n),\n order: entry.order,\n })),\n // Add T.id to the order by clause to get consistent results in case several rows have the exact same order\n { column: `${partitionedQueryAlias}.id`, order: 'asc' },\n ] as any);\n\n return resultQuery;\n};\n\n// Utils\nconst alias = _.curry((alias: string, value: string) => `${value} as ${alias}`);\nconst prefix = _.curry((prefix: string, value: string) => `${prefix}.${value}`);\n"],"names":["COL_STRAPI_ROW_NUMBER","COL_STRAPI_ORDER_BY_PREFIX","buildStatusSortExpression","db","tableName","tableAlias","isI18n","localeCondition","connection","raw","processOrderBy","orderBy","ctx","uid","qb","alias","meta","metadata","get","attributes","publishedAt","documentId","Error","rawExpression","order","undefined","attribute","columnName","toColumnName","column","aliasColumn","Array","isArray","flatMap","value","_","isPlainObject","Object","entries","key","direction","types","type","subAlias","createJoin","attributeName","target","getStrapiOrderColumnAlias","trimmedColumnName","replaceAll","wrapWithDeepSort","originalQuery","cloneDeep","state","columnOrderBy","filter","ob","rawExpressionOrderBy","resultQueryAlias","getAlias","aliasedTableName","mustUseAlias","resultQuery","getConnection","baseQuery","clone","baseQueryAlias","clear","select","prefix","map","orderByClause","partitionedQueryAlias","selectRowsAsNumberedPartitions","partitionedQuery","prefixedOrderBy","orderByColumns","prop","rowNumber","subQuery","partitionBy","from","as","stringSelect","s","originalSelect","difference","col","innerJoin","on","andOnVal","limit","offset","first","entry","curry"],"mappings":";;;;;AAeA,MAAMA,qBAAAA,GAAwB,qBAAA;AAC9B,MAAMC,0BAAAA,GAA6B,mBAAA;AAEnC;;;;;;;;;;UAWaC,yBAAAA,GAA4B,CACvCC,IACAC,SAAAA,EACAC,UAAAA,EACAC,SAAS,KAAK,GAAA;IAEd,MAAMC,e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AC,MAAM,EAAE,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"apply.d.ts","sourceRoot":"","sources":["../../../../src/query/helpers/populate/apply.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACxD,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AASzC,KAAK,OAAO,GAAG;IACb,EAAE,EAAE,QAAQ,CAAC;IACb,EAAE,EAAE,YAAY,CAAC;IACjB,GAAG,EAAE,MAAM,CAAC;CACb,CAAC;AAoBF,KAAK,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAqpBnC,QAAA,MAAM,aAAa,YAAmB,GAAG,EAAE,YAAY,OAAO,MAAM,EAAE,GAAG,CAAC,OAAO,OAAO,+BA+DvF,CAAC;AAEF,eAAe,aAAa,CAAC"}
|
|
@@ -424,7 +424,7 @@ const morphToOne = async (input, ctx)=>{
|
|
|
424
424
|
const { attribute, attributeName, results, populateValue } = input;
|
|
425
425
|
const { db } = ctx;
|
|
426
426
|
const { morphColumn } = attribute;
|
|
427
|
-
const { idColumn, typeColumn } = morphColumn;
|
|
427
|
+
const { idColumn, typeColumn, typeField = '__type' } = morphColumn;
|
|
428
428
|
// make a map for each type what ids to return
|
|
429
429
|
// make a nested map per id
|
|
430
430
|
const idsByType = results.reduce((acc, result)=>{
|
|
@@ -465,7 +465,11 @@ const morphToOne = async (input, ctx)=>{
|
|
|
465
465
|
}
|
|
466
466
|
const matchingRows = map[type][id];
|
|
467
467
|
const fromTargetRow = (rowOrRows)=>transform.fromRow(db.metadata.get(type), rowOrRows);
|
|
468
|
-
|
|
468
|
+
const row = fromTargetRow(_.first(matchingRows));
|
|
469
|
+
result[attributeName] = row ? {
|
|
470
|
+
[typeField]: type,
|
|
471
|
+
...row
|
|
472
|
+
} : row;
|
|
469
473
|
});
|
|
470
474
|
};
|
|
471
475
|
// TODO: Omit limit & offset to avoid needing a query per result to avoid making too many queries
|