@payloadcms/drizzle 4.0.0-internal.af6aad0 → 4.0.0-internal.b8a92d2
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/find/buildPolymorphicJoinQuery.d.ts +38 -0
- package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinQuery.js +135 -0
- package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.js +301 -0
- package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts +37 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.js +191 -0
- package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +19 -191
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +35 -13
- package/dist/migrate.js.map +1 -1
- package/dist/migrateDown.d.ts +2 -1
- package/dist/migrateDown.d.ts.map +1 -1
- package/dist/migrateDown.js +12 -2
- package/dist/migrateDown.js.map +1 -1
- package/dist/migrateFresh.d.ts +4 -2
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +21 -7
- package/dist/migrateFresh.js.map +1 -1
- package/dist/migrateRefresh.d.ts +2 -1
- package/dist/migrateRefresh.d.ts.map +1 -1
- package/dist/migrateRefresh.js +14 -3
- package/dist/migrateRefresh.js.map +1 -1
- package/dist/migrateReset.d.ts +2 -1
- package/dist/migrateReset.d.ts.map +1 -1
- package/dist/migrateReset.js +12 -2
- package/dist/migrateReset.js.map +1 -1
- package/dist/migrateStatus.d.ts +2 -1
- package/dist/migrateStatus.d.ts.map +1 -1
- package/dist/migrateStatus.js +11 -6
- package/dist/migrateStatus.js.map +1 -1
- package/dist/postgres/createJSONQuery/index.js +3 -3
- package/dist/postgres/createJSONQuery/index.js.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +12 -3
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +59 -27
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +210 -5
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/resolveRelationshipPath.d.ts +36 -0
- package/dist/queries/resolveRelationshipPath.d.ts.map +1 -0
- package/dist/queries/resolveRelationshipPath.js +66 -0
- package/dist/queries/resolveRelationshipPath.js.map +1 -0
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
- package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/index.js +6 -5
- package/dist/sqlite/createJSONQuery/index.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +8 -14
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +41 -1
- package/dist/updateOne.js.map +1 -1
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +25 -5
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/utilities/buildCreateMigration.d.ts.map +1 -1
- package/dist/utilities/buildCreateMigration.js +12 -8
- package/dist/utilities/buildCreateMigration.js.map +1 -1
- package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.js +3 -0
- package/dist/utilities/createSchemaGenerator.js.map +1 -1
- package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
- package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
- package/dist/utilities/parseJSONPathSegment.js +17 -0
- package/dist/utilities/parseJSONPathSegment.js.map +1 -0
- package/package.json +3 -3
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.spec.js +0 -91
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.spec.js.map +0 -1
- package/dist/postgres/query/postgresUnaccent.spec.js +0 -71
- package/dist/postgres/query/postgresUnaccent.spec.js.map +0 -1
- package/dist/queries/buildOperatorConstraint.spec.js +0 -348
- package/dist/queries/buildOperatorConstraint.spec.js.map +0 -1
- package/dist/queries/validateOperatorHandlers.spec.js +0 -142
- package/dist/queries/validateOperatorHandlers.spec.js.map +0 -1
- package/dist/schema/buildRawSchema.spec.js +0 -290
- package/dist/schema/buildRawSchema.spec.js.map +0 -1
- package/dist/upsertRow/handleUpsertError.spec.js +0 -42
- package/dist/upsertRow/handleUpsertError.spec.js.map +0 -1
- package/dist/utilities/checkTruncatedIdentifiers.spec.js +0 -301
- package/dist/utilities/checkTruncatedIdentifiers.spec.js.map +0 -1
- package/dist/utilities/isValidStringID.spec.js +0 -15
- package/dist/utilities/isValidStringID.spec.js.map +0 -1
- package/dist/utilities/validateIdentifierLength.spec.js +0 -19
- package/dist/utilities/validateIdentifierLength.spec.js.map +0 -1
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SQL } from 'drizzle-orm';
|
|
2
|
+
import { type FlattenedJoinField, type Where } from 'payload';
|
|
3
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
4
|
+
type BuildPolymorphicJoinQueryArgs = {
|
|
5
|
+
adapter: DrizzleAdapter;
|
|
6
|
+
currentTableName: string;
|
|
7
|
+
field: FlattenedJoinField;
|
|
8
|
+
limit: number;
|
|
9
|
+
locale?: string;
|
|
10
|
+
page?: number;
|
|
11
|
+
path: string;
|
|
12
|
+
shouldCount: boolean;
|
|
13
|
+
sort?: string | string[];
|
|
14
|
+
where?: Where;
|
|
15
|
+
};
|
|
16
|
+
type PolymorphicJoinQuery = {
|
|
17
|
+
columnName: string;
|
|
18
|
+
count?: SQL.Aliased;
|
|
19
|
+
documents: SQL.Aliased;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Builds the correlated document and optional count projections for a join that targets multiple
|
|
23
|
+
* collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort
|
|
24
|
+
* columns before the branches are combined. A missing sort field resolves to SQL NULL so targets
|
|
25
|
+
* with different schemas can still share one ordered union.
|
|
26
|
+
*
|
|
27
|
+
* `field.collection` must contain at least one configured collection. Each mapped table must have
|
|
28
|
+
* an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can
|
|
29
|
+
* include an extra look-ahead row when they calculate pagination.
|
|
30
|
+
*
|
|
31
|
+
* @returns The stable projection name, the document projection, and the count projection when
|
|
32
|
+
* `shouldCount` is true.
|
|
33
|
+
* @throws If the join definition, target collection, mapped table, required columns, or sort shape
|
|
34
|
+
* cannot be represented by the polymorphic query.
|
|
35
|
+
*/
|
|
36
|
+
export declare const buildPolymorphicJoinQuery: ({ adapter, currentTableName, field, limit, locale, page, path, shouldCount, sort, where, }: BuildPolymorphicJoinQueryArgs) => PolymorphicJoinQuery;
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=buildPolymorphicJoinQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildPolymorphicJoinQuery.d.ts","sourceRoot":"","sources":["../../src/find/buildPolymorphicJoinQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAKtC,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAG7D,OAAO,KAAK,EAAE,cAAc,EAAgB,MAAM,aAAa,CAAA;AAO/D,KAAK,6BAA6B,GAAG;IACnC,OAAO,EAAE,cAAc,CAAA;IACvB,gBAAgB,EAAE,MAAM,CAAA;IACxB,KAAK,EAAE,kBAAkB,CAAA;IACzB,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,OAAO,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IACxB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED,KAAK,oBAAoB,GAAG;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAA;IACnB,SAAS,EAAE,GAAG,CAAC,OAAO,CAAA;CACvB,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,yBAAyB,+FAWnC,6BAA6B,KAAG,oBA6HlC,CAAA"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { asc, count, desc, eq, sql } from 'drizzle-orm';
|
|
2
|
+
import toSnakeCase from 'to-snake-case';
|
|
3
|
+
import { jsonAggBuildObject } from '../utilities/json.js';
|
|
4
|
+
import { buildPolymorphicJoinWhere } from './buildPolymorphicJoinWhere.js';
|
|
5
|
+
import { createPolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js';
|
|
6
|
+
/**
|
|
7
|
+
* Builds the correlated document and optional count projections for a join that targets multiple
|
|
8
|
+
* collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort
|
|
9
|
+
* columns before the branches are combined. A missing sort field resolves to SQL NULL so targets
|
|
10
|
+
* with different schemas can still share one ordered union.
|
|
11
|
+
*
|
|
12
|
+
* `field.collection` must contain at least one configured collection. Each mapped table must have
|
|
13
|
+
* an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can
|
|
14
|
+
* include an extra look-ahead row when they calculate pagination.
|
|
15
|
+
*
|
|
16
|
+
* @returns The stable projection name, the document projection, and the count projection when
|
|
17
|
+
* `shouldCount` is true.
|
|
18
|
+
* @throws If the join definition, target collection, mapped table, required columns, or sort shape
|
|
19
|
+
* cannot be represented by the polymorphic query.
|
|
20
|
+
*/ export const buildPolymorphicJoinQuery = ({ adapter, currentTableName, field, limit, locale, page, path, shouldCount, sort, where })=>{
|
|
21
|
+
if (!Array.isArray(field.collection)) {
|
|
22
|
+
throw new Error('A polymorphic join requires a collection array');
|
|
23
|
+
}
|
|
24
|
+
if (field.collection.length === 0) {
|
|
25
|
+
throw new Error('A polymorphic join requires at least one collection');
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(sort)) {
|
|
28
|
+
throw new Error('Polymorphic joins do not support multiple sort fields');
|
|
29
|
+
}
|
|
30
|
+
const collections = field.collection;
|
|
31
|
+
const columnName = `${path.replaceAll('.', '_')}${field.name}`;
|
|
32
|
+
const db = adapter.drizzle;
|
|
33
|
+
const onPath = field.on.split('.').join('_');
|
|
34
|
+
const collectionTables = collections.map((collection)=>{
|
|
35
|
+
if (!adapter.payload.collections[collection]) {
|
|
36
|
+
throw new Error(`Unknown polymorphic join collection "${collection}"`);
|
|
37
|
+
}
|
|
38
|
+
const tableName = adapter.tableNameMap.get(toSnakeCase(collection));
|
|
39
|
+
const table = tableName ? adapter.tables[tableName] : undefined;
|
|
40
|
+
if (!table) {
|
|
41
|
+
throw new Error(`Polymorphic join collection "${collection}" has no database table`);
|
|
42
|
+
}
|
|
43
|
+
if (!table['id']) {
|
|
44
|
+
throw new Error(`Polymorphic join collection "${collection}" has no "id" column`);
|
|
45
|
+
}
|
|
46
|
+
if (!table[onPath]) {
|
|
47
|
+
throw new Error(`Polymorphic join collection "${collection}" has no "${onPath}" column`);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
collection,
|
|
51
|
+
table
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
const sortField = getSortField({
|
|
55
|
+
adapter,
|
|
56
|
+
collections,
|
|
57
|
+
sort
|
|
58
|
+
});
|
|
59
|
+
const sortOrder = sortField.startsWith('-') ? desc : asc;
|
|
60
|
+
const sortPath = sortField.replace('-', '').split('.').join('_');
|
|
61
|
+
const wherePlan = where ? createPolymorphicJoinWherePlan({
|
|
62
|
+
adapter,
|
|
63
|
+
collections,
|
|
64
|
+
where
|
|
65
|
+
}) : new Map();
|
|
66
|
+
let unionQuery = null;
|
|
67
|
+
for (const { collection, table } of collectionTables){
|
|
68
|
+
const sortColumn = table[sortPath];
|
|
69
|
+
const selectFields = {
|
|
70
|
+
id: table['id'],
|
|
71
|
+
parent: sql`${table[onPath]}`.as(onPath),
|
|
72
|
+
relationTo: sql`${collection}`.as('relationTo'),
|
|
73
|
+
sortPath: sql`${sortColumn ?? null}`.as('sortPath')
|
|
74
|
+
};
|
|
75
|
+
const collectionWhere = where && Object.keys(where).length > 0 ? buildPolymorphicJoinWhere({
|
|
76
|
+
adapter,
|
|
77
|
+
collection,
|
|
78
|
+
locale,
|
|
79
|
+
table,
|
|
80
|
+
where,
|
|
81
|
+
wherePlan
|
|
82
|
+
}) : undefined;
|
|
83
|
+
let collectionQuery = db.select(selectFields).from(table).$dynamic();
|
|
84
|
+
if (collectionWhere) {
|
|
85
|
+
collectionQuery = collectionQuery.where(collectionWhere);
|
|
86
|
+
}
|
|
87
|
+
if (unionQuery === null) {
|
|
88
|
+
unionQuery = collectionQuery;
|
|
89
|
+
} else {
|
|
90
|
+
unionQuery = unionQuery.unionAll(collectionQuery);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const subQueryAlias = `${columnName}_subquery`;
|
|
94
|
+
const parentConstraint = eq(sql.raw(`"${currentTableName}"."id"`), sql.raw(`"${subQueryAlias}"."${onPath}"`));
|
|
95
|
+
const countExpression = shouldCount ? sql`${db.select({
|
|
96
|
+
count: count()
|
|
97
|
+
}).from(sql`${unionQuery.as(subQueryAlias)}`).where(parentConstraint)}`.as(`${columnName}_count`) : undefined;
|
|
98
|
+
unionQuery = unionQuery.orderBy(sortOrder(sql`"sortPath"`));
|
|
99
|
+
const sortedUnionAlias = `${columnName}_sorted`;
|
|
100
|
+
let limitOffsetSQL = sql.empty();
|
|
101
|
+
if (limit) {
|
|
102
|
+
limitOffsetSQL = sql` LIMIT ${limit}`;
|
|
103
|
+
}
|
|
104
|
+
if (page && limit !== 0) {
|
|
105
|
+
const offset = (page - 1) * limit;
|
|
106
|
+
if (offset > 0) {
|
|
107
|
+
limitOffsetSQL = sql`${limitOffsetSQL} OFFSET ${offset}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const parentWhere = sql.raw(`"${sortedUnionAlias}"."${onPath}" = "${currentTableName}"."id"`);
|
|
111
|
+
const documents = sql`(
|
|
112
|
+
SELECT ${jsonAggBuildObject(adapter, {
|
|
113
|
+
id: sql.raw(`"${subQueryAlias}"."id"`),
|
|
114
|
+
relationTo: sql.raw(`"${subQueryAlias}"."relationTo"`)
|
|
115
|
+
})}
|
|
116
|
+
FROM (
|
|
117
|
+
SELECT * FROM ${sql`${unionQuery.as(sortedUnionAlias)}`}
|
|
118
|
+
WHERE ${parentWhere}${limitOffsetSQL}
|
|
119
|
+
) AS ${sql.raw(`"${subQueryAlias}"`)}
|
|
120
|
+
)`.as(columnName);
|
|
121
|
+
return {
|
|
122
|
+
columnName,
|
|
123
|
+
count: countExpression,
|
|
124
|
+
documents
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
const getSortField = ({ adapter, collections, sort })=>{
|
|
128
|
+
if (sort) {
|
|
129
|
+
return sort;
|
|
130
|
+
}
|
|
131
|
+
const hasCreatedAt = collections.some((collection)=>adapter.payload.collections[collection].config.fields.some((field)=>field.type === 'date' && field.name === 'createdAt'));
|
|
132
|
+
return hasCreatedAt ? '-createdAt' : 'id';
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=buildPolymorphicJoinQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/find/buildPolymorphicJoinQuery.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { LibSQLDatabase } from 'drizzle-orm/libsql'\nimport type { SQLiteSelect } from 'drizzle-orm/sqlite-core'\n\nimport { asc, count, desc, eq, sql } from 'drizzle-orm'\nimport { type FlattenedJoinField, type Where } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter, GenericTable } from '../types.js'\nimport type { PolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js'\n\nimport { jsonAggBuildObject } from '../utilities/json.js'\nimport { buildPolymorphicJoinWhere } from './buildPolymorphicJoinWhere.js'\nimport { createPolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js'\n\ntype BuildPolymorphicJoinQueryArgs = {\n adapter: DrizzleAdapter\n currentTableName: string\n field: FlattenedJoinField\n limit: number\n locale?: string\n page?: number\n path: string\n shouldCount: boolean\n sort?: string | string[]\n where?: Where\n}\n\ntype PolymorphicJoinQuery = {\n columnName: string\n count?: SQL.Aliased\n documents: SQL.Aliased\n}\n\n/**\n * Builds the correlated document and optional count projections for a join that targets multiple\n * collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort\n * columns before the branches are combined. A missing sort field resolves to SQL NULL so targets\n * with different schemas can still share one ordered union.\n *\n * `field.collection` must contain at least one configured collection. Each mapped table must have\n * an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can\n * include an extra look-ahead row when they calculate pagination.\n *\n * @returns The stable projection name, the document projection, and the count projection when\n * `shouldCount` is true.\n * @throws If the join definition, target collection, mapped table, required columns, or sort shape\n * cannot be represented by the polymorphic query.\n */\nexport const buildPolymorphicJoinQuery = ({\n adapter,\n currentTableName,\n field,\n limit,\n locale,\n page,\n path,\n shouldCount,\n sort,\n where,\n}: BuildPolymorphicJoinQueryArgs): PolymorphicJoinQuery => {\n if (!Array.isArray(field.collection)) {\n throw new Error('A polymorphic join requires a collection array')\n }\n\n if (field.collection.length === 0) {\n throw new Error('A polymorphic join requires at least one collection')\n }\n\n if (Array.isArray(sort)) {\n throw new Error('Polymorphic joins do not support multiple sort fields')\n }\n\n const collections = field.collection\n const columnName = `${path.replaceAll('.', '_')}${field.name}`\n const db = adapter.drizzle as LibSQLDatabase\n const onPath = field.on.split('.').join('_')\n const collectionTables = collections.map((collection) => {\n if (!adapter.payload.collections[collection]) {\n throw new Error(`Unknown polymorphic join collection \"${collection}\"`)\n }\n\n const tableName = adapter.tableNameMap.get(toSnakeCase(collection))\n const table = tableName ? (adapter.tables[tableName] as GenericTable | undefined) : undefined\n\n if (!table) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no database table`)\n }\n\n if (!table['id']) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no \"id\" column`)\n }\n\n if (!table[onPath]) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no \"${onPath}\" column`)\n }\n\n return { collection, table }\n })\n const sortField = getSortField({ adapter, collections, sort })\n const sortOrder = sortField.startsWith('-') ? desc : asc\n const sortPath = sortField.replace('-', '').split('.').join('_')\n const wherePlan: PolymorphicJoinWherePlan = where\n ? createPolymorphicJoinWherePlan({ adapter, collections, where })\n : new Map()\n let unionQuery: null | SQLiteSelect = null\n\n for (const { collection, table } of collectionTables) {\n const sortColumn = table[sortPath]\n const selectFields = {\n id: table['id'],\n parent: sql`${table[onPath]}`.as(onPath),\n relationTo: sql`${collection}`.as('relationTo'),\n sortPath: sql`${sortColumn ?? null}`.as('sortPath'),\n }\n const collectionWhere =\n where && Object.keys(where).length > 0\n ? buildPolymorphicJoinWhere({\n adapter,\n collection,\n locale,\n table,\n where,\n wherePlan,\n })\n : undefined\n let collectionQuery = db.select(selectFields).from(table).$dynamic()\n\n if (collectionWhere) {\n collectionQuery = collectionQuery.where(collectionWhere)\n }\n\n if (unionQuery === null) {\n unionQuery = collectionQuery as unknown as SQLiteSelect\n } else {\n unionQuery = unionQuery.unionAll(collectionQuery)\n }\n }\n\n const subQueryAlias = `${columnName}_subquery`\n const parentConstraint = eq(\n sql.raw(`\"${currentTableName}\".\"id\"`),\n sql.raw(`\"${subQueryAlias}\".\"${onPath}\"`),\n )\n const countExpression = shouldCount\n ? sql`${db\n .select({ count: count() })\n .from(sql`${unionQuery.as(subQueryAlias)}`)\n .where(parentConstraint)}`.as(`${columnName}_count`)\n : undefined\n\n unionQuery = unionQuery.orderBy(sortOrder(sql`\"sortPath\"`))\n\n const sortedUnionAlias = `${columnName}_sorted`\n let limitOffsetSQL = sql.empty()\n\n if (limit) {\n limitOffsetSQL = sql` LIMIT ${limit}`\n }\n\n if (page && limit !== 0) {\n const offset = (page - 1) * limit\n\n if (offset > 0) {\n limitOffsetSQL = sql`${limitOffsetSQL} OFFSET ${offset}`\n }\n }\n\n const parentWhere = sql.raw(`\"${sortedUnionAlias}\".\"${onPath}\" = \"${currentTableName}\".\"id\"`)\n const documents = sql`(\n SELECT ${jsonAggBuildObject(adapter, {\n id: sql.raw(`\"${subQueryAlias}\".\"id\"`),\n relationTo: sql.raw(`\"${subQueryAlias}\".\"relationTo\"`),\n })}\n FROM (\n SELECT * FROM ${sql`${unionQuery.as(sortedUnionAlias)}`}\n WHERE ${parentWhere}${limitOffsetSQL}\n ) AS ${sql.raw(`\"${subQueryAlias}\"`)}\n )`.as(columnName)\n\n return {\n columnName,\n count: countExpression,\n documents,\n }\n}\n\nconst getSortField = ({\n adapter,\n collections,\n sort,\n}: {\n adapter: DrizzleAdapter\n collections: string[]\n sort?: string\n}): string => {\n if (sort) {\n return sort\n }\n\n const hasCreatedAt = collections.some((collection) =>\n adapter.payload.collections[collection].config.fields.some(\n (field) => field.type === 'date' && field.name === 'createdAt',\n ),\n )\n\n return hasCreatedAt ? '-createdAt' : 'id'\n}\n"],"names":["asc","count","desc","eq","sql","toSnakeCase","jsonAggBuildObject","buildPolymorphicJoinWhere","createPolymorphicJoinWherePlan","buildPolymorphicJoinQuery","adapter","currentTableName","field","limit","locale","page","path","shouldCount","sort","where","Array","isArray","collection","Error","length","collections","columnName","replaceAll","name","db","drizzle","onPath","on","split","join","collectionTables","map","payload","tableName","tableNameMap","get","table","tables","undefined","sortField","getSortField","sortOrder","startsWith","sortPath","replace","wherePlan","Map","unionQuery","sortColumn","selectFields","id","parent","as","relationTo","collectionWhere","Object","keys","collectionQuery","select","from","$dynamic","unionAll","subQueryAlias","parentConstraint","raw","countExpression","orderBy","sortedUnionAlias","limitOffsetSQL","empty","offset","parentWhere","documents","hasCreatedAt","some","config","fields","type"],"mappings":"AAIA,SAASA,GAAG,EAAEC,KAAK,EAAEC,IAAI,EAAEC,EAAE,EAAEC,GAAG,QAAQ,cAAa;AAEvD,OAAOC,iBAAiB,gBAAe;AAKvC,SAASC,kBAAkB,QAAQ,uBAAsB;AACzD,SAASC,yBAAyB,QAAQ,iCAAgC;AAC1E,SAASC,8BAA8B,QAAQ,sCAAqC;AAqBpF;;;;;;;;;;;;;;CAcC,GACD,OAAO,MAAMC,4BAA4B,CAAC,EACxCC,OAAO,EACPC,gBAAgB,EAChBC,KAAK,EACLC,KAAK,EACLC,MAAM,EACNC,IAAI,EACJC,IAAI,EACJC,WAAW,EACXC,IAAI,EACJC,KAAK,EACyB;IAC9B,IAAI,CAACC,MAAMC,OAAO,CAACT,MAAMU,UAAU,GAAG;QACpC,MAAM,IAAIC,MAAM;IAClB;IAEA,IAAIX,MAAMU,UAAU,CAACE,MAAM,KAAK,GAAG;QACjC,MAAM,IAAID,MAAM;IAClB;IAEA,IAAIH,MAAMC,OAAO,CAACH,OAAO;QACvB,MAAM,IAAIK,MAAM;IAClB;IAEA,MAAME,cAAcb,MAAMU,UAAU;IACpC,MAAMI,aAAa,GAAGV,KAAKW,UAAU,CAAC,KAAK,OAAOf,MAAMgB,IAAI,EAAE;IAC9D,MAAMC,KAAKnB,QAAQoB,OAAO;IAC1B,MAAMC,SAASnB,MAAMoB,EAAE,CAACC,KAAK,CAAC,KAAKC,IAAI,CAAC;IACxC,MAA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|
|
1
|
+
import type { SQL } from 'drizzle-orm';
|
|
2
|
+
import { type Where } from 'payload';
|
|
3
|
+
import type { DrizzleAdapter, GenericTable } from '../types.js';
|
|
4
|
+
import type { PolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js';
|
|
5
|
+
type BuildPolymorphicJoinWhereArgs = {
|
|
6
|
+
adapter: DrizzleAdapter;
|
|
7
|
+
collection: string;
|
|
8
|
+
locale?: string;
|
|
9
|
+
table: GenericTable;
|
|
10
|
+
where: Where;
|
|
11
|
+
wherePlan: PolymorphicJoinWherePlan;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Builds a where clause for one collection before the polymorphic branches are combined. A field
|
|
15
|
+
* that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current
|
|
16
|
+
* collection slug.
|
|
17
|
+
*/
|
|
18
|
+
export declare const buildPolymorphicJoinWhere: ({ adapter, collection, locale, table, where, wherePlan, }: BuildPolymorphicJoinWhereArgs) => SQL | undefined;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=buildPolymorphicJoinWhere.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildPolymorphicJoinWhere.d.ts","sourceRoot":"","sources":["../../src/find/buildPolymorphicJoinWhere.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,GAAG,EAAE,MAAM,aAAa,CAAA;AAG9C,OAAO,EAAkD,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAIpF,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/D,OAAO,KAAK,EACV,wBAAwB,EAGzB,MAAM,qCAAqC,CAAA;AAO5C,KAAK,6BAA6B,GAAG;IACnC,OAAO,EAAE,cAAc,CAAA;IACvB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,YAAY,CAAA;IACnB,KAAK,EAAE,KAAK,CAAA;IACZ,SAAS,EAAE,wBAAwB,CAAA;CACpC,CAAA;AAqBD;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,8DAOnC,6BAA6B,KAAG,GAAG,GAAG,SAsMxC,CAAA"}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { and, getTableName, isNotNull, isNull, or, sql } from 'drizzle-orm';
|
|
2
|
+
import { QueryError } from 'payload';
|
|
3
|
+
import toSnakeCase from 'to-snake-case';
|
|
4
|
+
import { buildOperatorConstraint } from '../queries/buildOperatorConstraint.js';
|
|
5
|
+
import { sanitizeQueryValue } from '../queries/sanitizeQueryValue.js';
|
|
6
|
+
const supportedJSONQueryOperators = new Set([
|
|
7
|
+
'contains',
|
|
8
|
+
'equals',
|
|
9
|
+
'exists',
|
|
10
|
+
'in',
|
|
11
|
+
'like'
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* Builds a where clause for one collection before the polymorphic branches are combined. A field
|
|
15
|
+
* that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current
|
|
16
|
+
* collection slug.
|
|
17
|
+
*/ export const buildPolymorphicJoinWhere = ({ adapter, collection, locale, table, where, wherePlan })=>{
|
|
18
|
+
const constraints = [];
|
|
19
|
+
for(const key in where){
|
|
20
|
+
if ([
|
|
21
|
+
'AND',
|
|
22
|
+
'OR'
|
|
23
|
+
].includes(key.toUpperCase())) {
|
|
24
|
+
if (!Array.isArray(where[key])) {
|
|
25
|
+
throw new QueryError([
|
|
26
|
+
{
|
|
27
|
+
path: key
|
|
28
|
+
}
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
const combine = key.toUpperCase() === 'AND' ? and : or;
|
|
32
|
+
const nestedConstraints = where[key].map((nestedWhere)=>buildPolymorphicJoinWhere({
|
|
33
|
+
adapter,
|
|
34
|
+
collection,
|
|
35
|
+
locale,
|
|
36
|
+
table,
|
|
37
|
+
where: nestedWhere,
|
|
38
|
+
wherePlan
|
|
39
|
+
}));
|
|
40
|
+
const nestedCondition = combine(...nestedConstraints);
|
|
41
|
+
if (nestedCondition) {
|
|
42
|
+
constraints.push(nestedCondition);
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const resolvedPath = resolveWherePath({
|
|
47
|
+
adapter,
|
|
48
|
+
collection,
|
|
49
|
+
schemaPath: key,
|
|
50
|
+
table,
|
|
51
|
+
wherePlan
|
|
52
|
+
});
|
|
53
|
+
for (let payloadOperator of Object.keys(where[key])){
|
|
54
|
+
const originalOperator = payloadOperator;
|
|
55
|
+
let value = where[key][payloadOperator];
|
|
56
|
+
if (resolvedPath.type === 'invalid') {
|
|
57
|
+
throw new QueryError([
|
|
58
|
+
{
|
|
59
|
+
path: `${key}.${payloadOperator}`
|
|
60
|
+
}
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
if (resolvedPath.type === 'hasManySelect') {
|
|
64
|
+
if (!supportedJSONQueryOperators.has(payloadOperator)) {
|
|
65
|
+
throw new QueryError([
|
|
66
|
+
{
|
|
67
|
+
path: `${key}.${payloadOperator}`
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
72
|
+
adapter,
|
|
73
|
+
field: resolvedPath.fieldContext,
|
|
74
|
+
isUUID: false,
|
|
75
|
+
operator: payloadOperator,
|
|
76
|
+
relationOrPath: key,
|
|
77
|
+
val: value
|
|
78
|
+
});
|
|
79
|
+
if (sanitizedQueryValue === null) {
|
|
80
|
+
throw new QueryError([
|
|
81
|
+
{
|
|
82
|
+
path: `${key}.${originalOperator}`
|
|
83
|
+
}
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
if (sanitizedQueryValue.columns) {
|
|
87
|
+
throw new QueryError([
|
|
88
|
+
{
|
|
89
|
+
path: `${key}.${originalOperator}`
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
value = sanitizedQueryValue.value;
|
|
94
|
+
if (payloadOperator === 'in' && Array.isArray(value) && value.length === 0) {
|
|
95
|
+
constraints.push(sql.raw('false'));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const jsonQueryOperator = payloadOperator === 'exists' ? 'exists' : sanitizedQueryValue.operator;
|
|
99
|
+
constraints.push(sql.raw(adapter.createJSONQuery({
|
|
100
|
+
column: resolvedPath.jsonExpression,
|
|
101
|
+
operator: jsonQueryOperator,
|
|
102
|
+
pathSegments: [
|
|
103
|
+
resolvedPath.pathPlan.columnPath
|
|
104
|
+
],
|
|
105
|
+
value
|
|
106
|
+
})));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (payloadOperator === '$raw') {
|
|
110
|
+
if (typeof value !== 'string') {
|
|
111
|
+
throw new QueryError([
|
|
112
|
+
{
|
|
113
|
+
path: `${key}.${payloadOperator}`
|
|
114
|
+
}
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
constraints.push(sql.raw(value));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const { column, fieldContext, queryValueContext } = resolvedPath;
|
|
121
|
+
if (payloadOperator === 'like' && (queryValueContext.field.type === 'number' || queryValueContext.field.type === 'relationship' || queryValueContext.field.type === 'upload' || queryValueContext.isUUID)) {
|
|
122
|
+
payloadOperator = 'equals';
|
|
123
|
+
}
|
|
124
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
125
|
+
adapter,
|
|
126
|
+
field: queryValueContext.field,
|
|
127
|
+
isUUID: queryValueContext.isUUID,
|
|
128
|
+
operator: payloadOperator,
|
|
129
|
+
relationOrPath: key,
|
|
130
|
+
val: value
|
|
131
|
+
});
|
|
132
|
+
if (sanitizedQueryValue === null) {
|
|
133
|
+
throw new QueryError([
|
|
134
|
+
{
|
|
135
|
+
path: `${key}.${originalOperator}`
|
|
136
|
+
}
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
if (sanitizedQueryValue.columns) {
|
|
140
|
+
throw new QueryError([
|
|
141
|
+
{
|
|
142
|
+
path: `${key}.${originalOperator}`
|
|
143
|
+
}
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
payloadOperator = sanitizedQueryValue.operator;
|
|
147
|
+
value = sanitizedQueryValue.value;
|
|
148
|
+
if (!(payloadOperator in adapter.operators)) {
|
|
149
|
+
throw new QueryError([
|
|
150
|
+
{
|
|
151
|
+
path: `${key}.${originalOperator}`
|
|
152
|
+
}
|
|
153
|
+
]);
|
|
154
|
+
}
|
|
155
|
+
const resolvedOperator = payloadOperator;
|
|
156
|
+
const operator = adapter.operators[resolvedOperator];
|
|
157
|
+
const buildColumnConstraint = (constraintValue)=>{
|
|
158
|
+
if (!fieldContext) {
|
|
159
|
+
return operator(column, constraintValue);
|
|
160
|
+
}
|
|
161
|
+
return buildOperatorConstraint({
|
|
162
|
+
adapter,
|
|
163
|
+
column,
|
|
164
|
+
field: fieldContext.field,
|
|
165
|
+
locale,
|
|
166
|
+
originalOperator,
|
|
167
|
+
path: key,
|
|
168
|
+
resolvedOperator,
|
|
169
|
+
value: constraintValue
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
if (originalOperator === 'equals' && value === null) {
|
|
173
|
+
constraints.push(isNull(column));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (originalOperator === 'not_equals') {
|
|
177
|
+
if (value === null) {
|
|
178
|
+
constraints.push(isNotNull(column));
|
|
179
|
+
} else {
|
|
180
|
+
const notEqualsConstraint = or(isNull(column), buildColumnConstraint(value));
|
|
181
|
+
if (notEqualsConstraint) {
|
|
182
|
+
constraints.push(notEqualsConstraint);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (originalOperator === 'in' && Array.isArray(value) && value.includes(null)) {
|
|
188
|
+
const nonNullValues = value.filter((item)=>item !== null);
|
|
189
|
+
const inConstraint = buildColumnConstraint(nonNullValues);
|
|
190
|
+
const nullAwareInConstraint = or(isNull(column), inConstraint);
|
|
191
|
+
if (nullAwareInConstraint) {
|
|
192
|
+
constraints.push(nullAwareInConstraint);
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (payloadOperator === 'like' && typeof value === 'string') {
|
|
197
|
+
const wordConstraints = value.split(' ').map((word)=>buildColumnConstraint(`%${word}%`));
|
|
198
|
+
const wordCondition = and(...wordConstraints);
|
|
199
|
+
if (wordCondition) {
|
|
200
|
+
constraints.push(wordCondition);
|
|
201
|
+
}
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
constraints.push(buildColumnConstraint(value));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return and(...constraints);
|
|
208
|
+
};
|
|
209
|
+
const resolveWherePath = ({ adapter, collection, schemaPath, table, wherePlan })=>{
|
|
210
|
+
const pathPlan = wherePlan.get(schemaPath);
|
|
211
|
+
if (!pathPlan) {
|
|
212
|
+
throw new QueryError([
|
|
213
|
+
{
|
|
214
|
+
path: schemaPath
|
|
215
|
+
}
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
if (pathPlan.type === 'invalid') {
|
|
219
|
+
return {
|
|
220
|
+
type: 'invalid',
|
|
221
|
+
pathPlan
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (schemaPath === 'relationTo') {
|
|
225
|
+
return {
|
|
226
|
+
type: 'scalar',
|
|
227
|
+
column: sql`${collection}`,
|
|
228
|
+
pathPlan,
|
|
229
|
+
queryValueContext: {
|
|
230
|
+
type: 'scalar',
|
|
231
|
+
field: {
|
|
232
|
+
name: 'relationTo',
|
|
233
|
+
type: 'text'
|
|
234
|
+
},
|
|
235
|
+
isUUID: false
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
if (pathPlan.type === 'hasManySelect') {
|
|
240
|
+
const fieldContext = [
|
|
241
|
+
...pathPlan.fieldsByCollection.values()
|
|
242
|
+
].find((field)=>field.type === 'hasManySelect')?.field;
|
|
243
|
+
if (!fieldContext) {
|
|
244
|
+
return {
|
|
245
|
+
type: 'invalid',
|
|
246
|
+
pathPlan
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
type: 'hasManySelect',
|
|
251
|
+
fieldContext,
|
|
252
|
+
jsonExpression: getHasManySelectExpression({
|
|
253
|
+
adapter,
|
|
254
|
+
collection,
|
|
255
|
+
pathPlan,
|
|
256
|
+
table
|
|
257
|
+
}),
|
|
258
|
+
pathPlan
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const collectionField = pathPlan.fieldsByCollection.get(collection);
|
|
262
|
+
const fieldContext = collectionField?.type === 'scalar' ? collectionField : undefined;
|
|
263
|
+
const queryValueContext = fieldContext ?? [
|
|
264
|
+
...pathPlan.fieldsByCollection.values()
|
|
265
|
+
].find((field)=>field.type === 'scalar');
|
|
266
|
+
if (!queryValueContext) {
|
|
267
|
+
return {
|
|
268
|
+
type: 'invalid',
|
|
269
|
+
pathPlan
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
type: 'scalar',
|
|
274
|
+
column: fieldContext ? table[pathPlan.columnPath] : sql`null`,
|
|
275
|
+
fieldContext,
|
|
276
|
+
pathPlan,
|
|
277
|
+
queryValueContext
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const getHasManySelectExpression = ({ adapter, collection, pathPlan, table })=>{
|
|
281
|
+
if (pathPlan.fieldsByCollection.get(collection)?.type !== 'hasManySelect') {
|
|
282
|
+
return adapter.name === 'postgres' ? `'[]'::jsonb` : `'[]'`;
|
|
283
|
+
}
|
|
284
|
+
const selectTablePath = pathPlan.schemaPath.split('.').map((pathSegment)=>toSnakeCase(pathSegment)).join('_');
|
|
285
|
+
const valuesTableName = adapter.tableNameMap.get(`${toSnakeCase(collection)}_${selectTablePath}`);
|
|
286
|
+
if (!valuesTableName) {
|
|
287
|
+
throw new Error(`Polymorphic join collection "${collection}" has no value table for "${pathPlan.schemaPath}"`);
|
|
288
|
+
}
|
|
289
|
+
let qualifiedValuesTableName = valuesTableName;
|
|
290
|
+
let parentTableName = getTableName(table);
|
|
291
|
+
if (adapter.schemaName) {
|
|
292
|
+
qualifiedValuesTableName = `"${adapter.schemaName}"."${valuesTableName}"`;
|
|
293
|
+
parentTableName = `"${adapter.schemaName}"."${parentTableName}"`;
|
|
294
|
+
}
|
|
295
|
+
if (adapter.name === 'postgres') {
|
|
296
|
+
return `(select coalesce(jsonb_agg(${qualifiedValuesTableName}.value), '[]'::jsonb) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
297
|
+
}
|
|
298
|
+
return `(select json_group_array(${qualifiedValuesTableName}.value) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
//# sourceMappingURL=buildPolymorphicJoinWhere.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/find/buildPolymorphicJoinWhere.ts"],"sourcesContent":["import type { Column, SQL } from 'drizzle-orm'\n\nimport { and, getTableName, isNotNull, isNull, or, sql } from 'drizzle-orm'\nimport { type FlattenedField, type Operator, QueryError, type Where } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleResolvedOperator } from '../queries/operatorMap.js'\nimport type { DrizzleAdapter, GenericTable } from '../types.js'\nimport type {\n PolymorphicJoinWherePlan,\n ScalarWhereField,\n WherePathPlan,\n} from './createPolymorphicJoinWherePlan.js'\n\nimport { buildOperatorConstraint } from '../queries/buildOperatorConstraint.js'\nimport { sanitizeQueryValue } from '../queries/sanitizeQueryValue.js'\n\nconst supportedJSONQueryOperators = new Set(['contains', 'equals', 'exists', 'in', 'like'])\n\ntype BuildPolymorphicJoinWhereArgs = {\n adapter: DrizzleAdapter\n collection: string\n locale?: string\n table: GenericTable\n where: Where\n wherePlan: PolymorphicJoinWherePlan\n}\n\ntype ResolvedWherePath =\n | {\n column: Column | SQL\n fieldContext?: ScalarWhereField\n pathPlan: WherePathPlan\n queryValueContext: ScalarWhereField\n type: 'scalar'\n }\n | {\n fieldContext: FlattenedField\n jsonExpression: string\n pathPlan: WherePathPlan\n type: 'hasManySelect'\n }\n | {\n pathPlan: WherePathPlan\n type: 'invalid'\n }\n\n/**\n * Builds a where clause for one collection before the polymorphic branches are combined. A field\n * that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current\n * collection slug.\n */\nexport const buildPolymorphicJoinWhere = ({\n adapter,\n collection,\n locale,\n table,\n where,\n wherePlan,\n}: BuildPolymorphicJoinWhereArgs): SQL | undefined => {\n const constraints: SQL[] = []\n\n for (const key in where) {\n if (['AND', 'OR'].includes(key.toUpperCase())) {\n if (!Array.isArray(where[key])) {\n throw new QueryError([{ path: key }])\n }\n\n const combine = key.toUpperCase() === 'AND' ? and : or\n const nestedConstraints = where[key].map((nestedWhere) =>\n buildPolymorphicJoinWhere({\n adapter,\n collection,\n locale,\n table,\n where: nestedWhere,\n wherePlan,\n }),\n )\n const nestedCondition = combine(...nestedConstraints)\n\n if (nestedCondition) {\n constraints.push(nestedCondition)\n }\n\n continue\n }\n\n const resolvedPath = resolveWherePath({\n adapter,\n collection,\n schemaPath: key,\n table,\n wherePlan,\n })\n\n for (let payloadOperator of Object.keys(where[key])) {\n const originalOperator = payloadOperator as Operator\n let value = where[key][payloadOperator]\n\n if (resolvedPath.type === 'invalid') {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n if (resolvedPath.type === 'hasManySelect') {\n if (!supportedJSONQueryOperators.has(payloadOperator)) {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n const sanitizedQueryValue = sanitizeQueryValue({\n adapter,\n field: resolvedPath.fieldContext,\n isUUID: false,\n operator: payloadOperator,\n relationOrPath: key,\n val: value,\n })\n\n if (sanitizedQueryValue === null) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n if (sanitizedQueryValue.columns) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n value = sanitizedQueryValue.value\n\n if (payloadOperator === 'in' && Array.isArray(value) && value.length === 0) {\n constraints.push(sql.raw('false'))\n continue\n }\n\n const jsonQueryOperator =\n payloadOperator === 'exists' ? 'exists' : sanitizedQueryValue.operator\n\n constraints.push(\n sql.raw(\n adapter.createJSONQuery({\n column: resolvedPath.jsonExpression,\n operator: jsonQueryOperator,\n pathSegments: [resolvedPath.pathPlan.columnPath],\n value,\n }),\n ),\n )\n continue\n }\n\n if (payloadOperator === '$raw') {\n if (typeof value !== 'string') {\n throw new QueryError([{ path: `${key}.${payloadOperator}` }])\n }\n\n constraints.push(sql.raw(value))\n continue\n }\n\n const { column, fieldContext, queryValueContext } = resolvedPath\n\n if (\n payloadOperator === 'like' &&\n (queryValueContext.field.type === 'number' ||\n queryValueContext.field.type === 'relationship' ||\n queryValueContext.field.type === 'upload' ||\n queryValueContext.isUUID)\n ) {\n payloadOperator = 'equals'\n }\n\n const sanitizedQueryValue = sanitizeQueryValue({\n adapter,\n field: queryValueContext.field,\n isUUID: queryValueContext.isUUID,\n operator: payloadOperator,\n relationOrPath: key,\n val: value,\n })\n\n if (sanitizedQueryValue === null) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n if (sanitizedQueryValue.columns) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n payloadOperator = sanitizedQueryValue.operator\n value = sanitizedQueryValue.value\n\n if (!(payloadOperator in adapter.operators)) {\n throw new QueryError([{ path: `${key}.${originalOperator}` }])\n }\n\n const resolvedOperator = payloadOperator as DrizzleResolvedOperator\n const operator = adapter.operators[resolvedOperator]\n const buildColumnConstraint = (constraintValue: unknown) => {\n if (!fieldContext) {\n return operator(column, constraintValue)\n }\n\n return buildOperatorConstraint({\n adapter,\n column,\n field: fieldContext.field,\n locale,\n originalOperator,\n path: key,\n resolvedOperator,\n value: constraintValue,\n })\n }\n\n if (originalOperator === 'equals' && value === null) {\n constraints.push(isNull(column))\n continue\n }\n\n if (originalOperator === 'not_equals') {\n if (value === null) {\n constraints.push(isNotNull(column))\n } else {\n const notEqualsConstraint = or(isNull(column), buildColumnConstraint(value))\n\n if (notEqualsConstraint) {\n constraints.push(notEqualsConstraint)\n }\n }\n continue\n }\n\n if (originalOperator === 'in' && Array.isArray(value) && value.includes(null)) {\n const nonNullValues = value.filter((item) => item !== null)\n const inConstraint = buildColumnConstraint(nonNullValues)\n const nullAwareInConstraint = or(isNull(column), inConstraint)\n\n if (nullAwareInConstraint) {\n constraints.push(nullAwareInConstraint)\n }\n continue\n }\n\n if (payloadOperator === 'like' && typeof value === 'string') {\n const wordConstraints = value.split(' ').map((word) => buildColumnConstraint(`%${word}%`))\n const wordCondition = and(...wordConstraints)\n\n if (wordCondition) {\n constraints.push(wordCondition)\n }\n continue\n }\n\n constraints.push(buildColumnConstraint(value))\n }\n }\n\n return and(...constraints)\n}\n\nconst resolveWherePath = ({\n adapter,\n collection,\n schemaPath,\n table,\n wherePlan,\n}: {\n schemaPath: string\n} & Omit<BuildPolymorphicJoinWhereArgs, 'locale' | 'where'>): ResolvedWherePath => {\n const pathPlan = wherePlan.get(schemaPath)\n\n if (!pathPlan) {\n throw new QueryError([{ path: schemaPath }])\n }\n\n if (pathPlan.type === 'invalid') {\n return { type: 'invalid', pathPlan }\n }\n\n if (schemaPath === 'relationTo') {\n return {\n type: 'scalar',\n column: sql`${collection}`,\n pathPlan,\n queryValueContext: {\n type: 'scalar',\n field: { name: 'relationTo', type: 'text' },\n isUUID: false,\n },\n }\n }\n\n if (pathPlan.type === 'hasManySelect') {\n const fieldContext = [...pathPlan.fieldsByCollection.values()].find(\n (field) => field.type === 'hasManySelect',\n )?.field\n\n if (!fieldContext) {\n return { type: 'invalid', pathPlan }\n }\n\n return {\n type: 'hasManySelect',\n fieldContext,\n jsonExpression: getHasManySelectExpression({\n adapter,\n collection,\n pathPlan,\n table,\n }),\n pathPlan,\n }\n }\n\n const collectionField = pathPlan.fieldsByCollection.get(collection)\n const fieldContext = collectionField?.type === 'scalar' ? collectionField : undefined\n const queryValueContext =\n fieldContext ??\n [...pathPlan.fieldsByCollection.values()].find(\n (field): field is ScalarWhereField => field.type === 'scalar',\n )\n\n if (!queryValueContext) {\n return { type: 'invalid', pathPlan }\n }\n\n return {\n type: 'scalar',\n column: fieldContext ? table[pathPlan.columnPath] : sql`null`,\n fieldContext,\n pathPlan,\n queryValueContext,\n }\n}\n\nconst getHasManySelectExpression = ({\n adapter,\n collection,\n pathPlan,\n table,\n}: {\n adapter: DrizzleAdapter\n collection: string\n pathPlan: WherePathPlan\n table: GenericTable\n}): string => {\n if (pathPlan.fieldsByCollection.get(collection)?.type !== 'hasManySelect') {\n return adapter.name === 'postgres' ? `'[]'::jsonb` : `'[]'`\n }\n\n const selectTablePath = pathPlan.schemaPath\n .split('.')\n .map((pathSegment) => toSnakeCase(pathSegment))\n .join('_')\n const valuesTableName = adapter.tableNameMap.get(`${toSnakeCase(collection)}_${selectTablePath}`)\n\n if (!valuesTableName) {\n throw new Error(\n `Polymorphic join collection \"${collection}\" has no value table for \"${pathPlan.schemaPath}\"`,\n )\n }\n\n let qualifiedValuesTableName = valuesTableName\n let parentTableName = getTableName(table)\n\n if (adapter.schemaName) {\n qualifiedValuesTableName = `\"${adapter.schemaName}\".\"${valuesTableName}\"`\n parentTableName = `\"${adapter.schemaName}\".\"${parentTableName}\"`\n }\n\n if (adapter.name === 'postgres') {\n return `(select coalesce(jsonb_agg(${qualifiedValuesTableName}.value), '[]'::jsonb) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`\n }\n\n return `(select json_group_array(${qualifiedValuesTableName}.value) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`\n}\n"],"names":["and","getTableName","isNotNull","isNull","or","sql","QueryError","toSnakeCase","buildOperatorConstraint","sanitizeQueryValue","supportedJSONQueryOperators","Set","buildPolymorphicJoinWhere","adapter","collection","locale","table","where","wherePlan","constraints","key","includes","toUpperCase","Array","isArray","path","combine","nestedConstraints","map","nestedWhere","nestedCondition","push","resolvedPath","resolveWherePath","schemaPath","payloadOperator","Object","keys","originalOperator","value","type","has","sanitizedQueryValue","field","fieldContext","isUUID","operator","relationOrPath","val","columns","length","raw","jsonQueryOperator","createJSONQuery","column","jsonExpression","pathSegments","pathPlan","columnPath","queryValueContext","operators","resolvedOperator","buildColumnConstraint","constraintValue","notEqualsConstraint","nonNullValues","filter","item","inConstraint","nullAwareInConstraint","wordConstraints","split","word","wordCondition","get","name","fieldsByCollection","values","find","getHasManySelectExpression","collectionField","undefined","selectTablePath","pathSegment","join","valuesTableName","tableNameMap","Error","qualifiedValuesTableName","parentTableName","schemaName"],"mappings":"AAEA,SAASA,GAAG,EAAEC,YAAY,EAAEC,SAAS,EAAEC,MAAM,EAAEC,EAAE,EAAEC,GAAG,QAAQ,cAAa;AAC3E,SAA6CC,UAAU,QAAoB,UAAS;AACpF,OAAOC,iBAAiB,gBAAe;AAUvC,SAASC,uBAAuB,QAAQ,wCAAuC;AAC/E,SAASC,kBAAkB,QAAQ,mCAAkC;AAErE,MAAMC,8BAA8B,IAAIC,IAAI;IAAC;IAAY;IAAU;IAAU;IAAM;CAAO;AA8B1F;;;;CAIC,GACD,OAAO,MAAMC,4BAA4B,CAAC,EACxCC,OAAO,EACPC,UAAU,EACVC,MAAM,EACNC,KAAK,EACLC,KAAK,EACLC,SAAS,EACqB;IAC9B,MAAMC,cAAqB,EAAE;IAE7B,IAAK,MAAMC,OAAOH,MAAO;QACvB,IAAI;YAAC;YAAO;SAAK,CAACI,QAAQ,CAACD,IAAIE,WAAW,KAAK;YAC7C,IAAI,CAACC,MAAMC,OAAO,CAACP,KAAK,CAACG,IAAI,GAAG;gBAC9B,MAAM,IAAId,WAAW;oBAAC;wBAAEmB,MAAML;oBAAI;iBAAE;YACtC;YAEA,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,WAAW,0BAA0B,EAAE2C,SAASvB,UAAU,CAAC,CAAC,CAAC;IAEjG;IAEA,IAAIsD,2BAA2BH;IAC/B,IAAII,kBAAkBxF,aAAae;IAEnC,IAAIH,QAAQ6E,UAAU,EAAE;QACtBF,2BAA2B,CAAC,CAAC,EAAE3E,QAAQ6E,UAAU,CAAC,GAAG,EAAEL,gBAAgB,CAAC,CAAC;QACzEI,kBAAkB,CAAC,CAAC,EAAE5E,QAAQ6E,UAAU,CAAC,GAAG,EAAED,gBAAgB,CAAC,CAAC;IAClE;IAEA,IAAI5E,QAAQ8D,IAAI,KAAK,YAAY;QAC/B,OAAO,CAAC,2BAA2B,EAAEa,yBAAyB,2BAA2B,EAAEA,yBAAyB,OAAO,EAAEA,yBAAyB,aAAa,EAAEC,gBAAgB,IAAI,CAAC;IAC5L;IAEA,OAAO,CAAC,yBAAyB,EAAED,yBAAyB,aAAa,EAAEA,yBAAyB,OAAO,EAAEA,yBAAyB,aAAa,EAAEC,gBAAgB,IAAI,CAAC;AAC5K"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { type FlattenedField, type Where } from 'payload';
|
|
2
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
3
|
+
export type ScalarWhereField = {
|
|
4
|
+
field: FlattenedField;
|
|
5
|
+
isUUID: boolean;
|
|
6
|
+
type: 'scalar';
|
|
7
|
+
};
|
|
8
|
+
type CollectionWhereField = {
|
|
9
|
+
field: FlattenedField;
|
|
10
|
+
type: 'hasManySelect';
|
|
11
|
+
} | ScalarWhereField;
|
|
12
|
+
export type WherePathPlan = {
|
|
13
|
+
columnPath: string;
|
|
14
|
+
fieldsByCollection: Map<string, CollectionWhereField>;
|
|
15
|
+
schemaPath: string;
|
|
16
|
+
type: 'hasManySelect' | 'invalid' | 'scalar';
|
|
17
|
+
};
|
|
18
|
+
export type PolymorphicJoinWherePlan = Map<string, WherePathPlan>;
|
|
19
|
+
/**
|
|
20
|
+
* Analyzes every leaf path in a polymorphic join where clause before SQL is built. Each plan entry
|
|
21
|
+
* records the flattened column path, the compatible field shape for each target collection, and
|
|
22
|
+
* whether the path uses a scalar column or a has-many select value table. A field may be absent
|
|
23
|
+
* from some target collections; those branches compare against SQL NULL or an empty JSON array.
|
|
24
|
+
*
|
|
25
|
+
* Paths are marked as invalid when their storage shapes conflict, require localized or separate-row
|
|
26
|
+
* traversal, have no matching field, lack a required column, or collide after path flattening.
|
|
27
|
+
*
|
|
28
|
+
* @returns A plan keyed by each unique schema path in `where`.
|
|
29
|
+
* @throws If a target collection or its mapped database table is not available in the adapter.
|
|
30
|
+
*/
|
|
31
|
+
export declare const createPolymorphicJoinWherePlan: ({ adapter, collections, where, }: {
|
|
32
|
+
adapter: DrizzleAdapter;
|
|
33
|
+
collections: string[];
|
|
34
|
+
where: Where;
|
|
35
|
+
}) => PolymorphicJoinWherePlan;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=createPolymorphicJoinWherePlan.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createPolymorphicJoinWherePlan.d.ts","sourceRoot":"","sources":["../../src/find/createPolymorphicJoinWherePlan.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,cAAc,EAAkB,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAGzE,OAAO,KAAK,EAAE,cAAc,EAAgB,MAAM,aAAa,CAAA;AAK/D,MAAM,MAAM,gBAAgB,GAAG;IAC7B,KAAK,EAAE,cAAc,CAAA;IACrB,MAAM,EAAE,OAAO,CAAA;IACf,IAAI,EAAE,QAAQ,CAAA;CACf,CAAA;AAED,KAAK,oBAAoB,GAAG;IAAE,KAAK,EAAE,cAAc,CAAC;IAAC,IAAI,EAAE,eAAe,CAAA;CAAE,GAAG,gBAAgB,CAAA;AAE/F,MAAM,MAAM,aAAa,GAAG;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,kBAAkB,EAAE,GAAG,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAA;IACrD,UAAU,EAAE,MAAM,CAAA;IAClB,IAAI,EAAE,eAAe,GAAG,SAAS,GAAG,QAAQ,CAAA;CAC7C,CAAA;AAED,MAAM,MAAM,wBAAwB,GAAG,GAAG,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;AAEjE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,8BAA8B,qCAIxC;IACD,OAAO,EAAE,cAAc,CAAA;IACvB,WAAW,EAAE,MAAM,EAAE,CAAA;IACrB,KAAK,EAAE,KAAK,CAAA;CACb,KAAG,wBAoIH,CAAA"}
|