@payloadcms/drizzle 4.0.0-internal.9496957 → 4.0.0-internal.952ed66

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.
Files changed (262) hide show
  1. package/dist/count.d.ts.map +1 -1
  2. package/dist/count.js +25 -3
  3. package/dist/count.js.map +1 -1
  4. package/dist/countGlobalVersions.js +8 -3
  5. package/dist/countGlobalVersions.js.map +1 -1
  6. package/dist/countVersions.d.ts.map +1 -1
  7. package/dist/countVersions.js +29 -4
  8. package/dist/countVersions.js.map +1 -1
  9. package/dist/createGlobalVersion.d.ts.map +1 -1
  10. package/dist/createGlobalVersion.js +17 -2
  11. package/dist/createGlobalVersion.js.map +1 -1
  12. package/dist/createVersion.d.ts.map +1 -1
  13. package/dist/createVersion.js +12 -1
  14. package/dist/createVersion.js.map +1 -1
  15. package/dist/deleteBranchGlobal.d.ts +10 -0
  16. package/dist/deleteBranchGlobal.d.ts.map +1 -0
  17. package/dist/deleteBranchGlobal.js +27 -0
  18. package/dist/deleteBranchGlobal.js.map +1 -0
  19. package/dist/deleteOne.d.ts.map +1 -1
  20. package/dist/deleteOne.js +25 -1
  21. package/dist/deleteOne.js.map +1 -1
  22. package/dist/exports/postgres.d.ts +2 -1
  23. package/dist/exports/postgres.d.ts.map +1 -1
  24. package/dist/exports/postgres.js +2 -1
  25. package/dist/exports/postgres.js.map +1 -1
  26. package/dist/exports/sqlite.d.ts +2 -1
  27. package/dist/exports/sqlite.d.ts.map +1 -1
  28. package/dist/exports/sqlite.js +2 -1
  29. package/dist/exports/sqlite.js.map +1 -1
  30. package/dist/find/buildFindManyArgs.d.ts +3 -2
  31. package/dist/find/buildFindManyArgs.d.ts.map +1 -1
  32. package/dist/find/buildFindManyArgs.js +2 -1
  33. package/dist/find/buildFindManyArgs.js.map +1 -1
  34. package/dist/find/buildPolymorphicJoinQuery.d.ts +40 -0
  35. package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
  36. package/dist/find/buildPolymorphicJoinQuery.js +163 -0
  37. package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
  38. package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
  39. package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
  40. package/dist/find/buildPolymorphicJoinWhere.js +301 -0
  41. package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
  42. package/dist/find/createPolymorphicJoinWherePlan.d.ts +38 -0
  43. package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
  44. package/dist/find/createPolymorphicJoinWherePlan.js +200 -0
  45. package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
  46. package/dist/find/findMany.d.ts +6 -1
  47. package/dist/find/findMany.d.ts.map +1 -1
  48. package/dist/find/findMany.js +16 -3
  49. package/dist/find/findMany.js.map +1 -1
  50. package/dist/find/traverseFields.d.ts +3 -2
  51. package/dist/find/traverseFields.d.ts.map +1 -1
  52. package/dist/find/traverseFields.js +82 -198
  53. package/dist/find/traverseFields.js.map +1 -1
  54. package/dist/find.d.ts.map +1 -1
  55. package/dist/find.js +32 -4
  56. package/dist/find.js.map +1 -1
  57. package/dist/findDistinct.d.ts.map +1 -1
  58. package/dist/findDistinct.js +104 -8
  59. package/dist/findDistinct.js.map +1 -1
  60. package/dist/findGlobal.d.ts.map +1 -1
  61. package/dist/findGlobal.js +24 -5
  62. package/dist/findGlobal.js.map +1 -1
  63. package/dist/findGlobalVersions.js +8 -3
  64. package/dist/findGlobalVersions.js.map +1 -1
  65. package/dist/findOne.d.ts +1 -1
  66. package/dist/findOne.d.ts.map +1 -1
  67. package/dist/findOne.js +30 -3
  68. package/dist/findOne.js.map +1 -1
  69. package/dist/findVersions.d.ts.map +1 -1
  70. package/dist/findVersions.js +32 -5
  71. package/dist/findVersions.js.map +1 -1
  72. package/dist/index.d.ts +3 -8
  73. package/dist/index.d.ts.map +1 -1
  74. package/dist/index.js +3 -8
  75. package/dist/index.js.map +1 -1
  76. package/dist/migrate.d.ts.map +1 -1
  77. package/dist/migrate.js +6 -2
  78. package/dist/migrate.js.map +1 -1
  79. package/dist/migrateDown.d.ts.map +1 -1
  80. package/dist/migrateDown.js +5 -2
  81. package/dist/migrateDown.js.map +1 -1
  82. package/dist/migrateFresh.d.ts.map +1 -1
  83. package/dist/migrateFresh.js +5 -2
  84. package/dist/migrateFresh.js.map +1 -1
  85. package/dist/migrateRefresh.d.ts.map +1 -1
  86. package/dist/migrateRefresh.js +6 -2
  87. package/dist/migrateRefresh.js.map +1 -1
  88. package/dist/migrateReset.d.ts.map +1 -1
  89. package/dist/migrateReset.js +6 -2
  90. package/dist/migrateReset.js.map +1 -1
  91. package/dist/postgres/createJSONQuery/index.js +3 -3
  92. package/dist/postgres/createJSONQuery/index.js.map +1 -1
  93. package/dist/postgres/insert.d.ts.map +1 -1
  94. package/dist/postgres/insert.js +4 -2
  95. package/dist/postgres/insert.js.map +1 -1
  96. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts +35 -0
  97. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
  98. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js +185 -0
  99. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
  100. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -1
  101. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js +8 -4
  102. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -1
  103. package/dist/postgres/predefinedMigrations/v2-v3/index.js +1 -1
  104. package/dist/postgres/predefinedMigrations/v2-v3/index.js.map +1 -1
  105. package/dist/postgres/requireDrizzleKit.d.ts +4 -0
  106. package/dist/postgres/requireDrizzleKit.d.ts.map +1 -1
  107. package/dist/postgres/requireDrizzleKit.js +16 -10
  108. package/dist/postgres/requireDrizzleKit.js.map +1 -1
  109. package/dist/postgres/types.d.ts +4 -0
  110. package/dist/postgres/types.d.ts.map +1 -1
  111. package/dist/postgres/types.js.map +1 -1
  112. package/dist/queries/buildAndOrConditions.d.ts +3 -2
  113. package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
  114. package/dist/queries/buildAndOrConditions.js +2 -1
  115. package/dist/queries/buildAndOrConditions.js.map +1 -1
  116. package/dist/queries/buildBranchVisibility.d.ts +11 -0
  117. package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
  118. package/dist/queries/buildBranchVisibility.js +48 -0
  119. package/dist/queries/buildBranchVisibility.js.map +1 -0
  120. package/dist/queries/buildOrderBy.d.ts +3 -2
  121. package/dist/queries/buildOrderBy.d.ts.map +1 -1
  122. package/dist/queries/buildOrderBy.js +2 -1
  123. package/dist/queries/buildOrderBy.js.map +1 -1
  124. package/dist/queries/buildQuery.d.ts +3 -2
  125. package/dist/queries/buildQuery.d.ts.map +1 -1
  126. package/dist/queries/buildQuery.js +4 -2
  127. package/dist/queries/buildQuery.js.map +1 -1
  128. package/dist/queries/getTableColumnFromPath.d.ts +3 -2
  129. package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
  130. package/dist/queries/getTableColumnFromPath.js +176 -29
  131. package/dist/queries/getTableColumnFromPath.js.map +1 -1
  132. package/dist/queries/parseParams.d.ts +3 -2
  133. package/dist/queries/parseParams.d.ts.map +1 -1
  134. package/dist/queries/parseParams.js +63 -3
  135. package/dist/queries/parseParams.js.map +1 -1
  136. package/dist/queryDrafts.d.ts.map +1 -1
  137. package/dist/queryDrafts.js +27 -5
  138. package/dist/queryDrafts.js.map +1 -1
  139. package/dist/schema/build.d.ts.map +1 -1
  140. package/dist/schema/build.js +28 -20
  141. package/dist/schema/build.js.map +1 -1
  142. package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
  143. package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
  144. package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
  145. package/dist/sqlite/createJSONQuery/index.js +6 -5
  146. package/dist/sqlite/createJSONQuery/index.js.map +1 -1
  147. package/dist/sqlite/createRequireDrizzleKit.d.ts +6 -0
  148. package/dist/sqlite/createRequireDrizzleKit.d.ts.map +1 -0
  149. package/dist/sqlite/createRequireDrizzleKit.js +15 -0
  150. package/dist/sqlite/createRequireDrizzleKit.js.map +1 -0
  151. package/dist/sqlite/execute.js.map +1 -1
  152. package/dist/sqlite/insert.d.ts.map +1 -1
  153. package/dist/sqlite/insert.js +9 -4
  154. package/dist/sqlite/insert.js.map +1 -1
  155. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts +15 -0
  156. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
  157. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js +114 -0
  158. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
  159. package/dist/sqlite/types.d.ts +4 -0
  160. package/dist/sqlite/types.d.ts.map +1 -1
  161. package/dist/sqlite/types.js.map +1 -1
  162. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  163. package/dist/transactions/beginTransaction.js +13 -5
  164. package/dist/transactions/beginTransaction.js.map +1 -1
  165. package/dist/transactions/commitTransaction.d.ts.map +1 -1
  166. package/dist/transactions/commitTransaction.js +2 -5
  167. package/dist/transactions/commitTransaction.js.map +1 -1
  168. package/dist/transform/read/traverseFields.js.map +1 -1
  169. package/dist/transform/write/array.d.ts +3 -2
  170. package/dist/transform/write/array.d.ts.map +1 -1
  171. package/dist/transform/write/array.js +2 -1
  172. package/dist/transform/write/array.js.map +1 -1
  173. package/dist/transform/write/blocks.d.ts +3 -2
  174. package/dist/transform/write/blocks.d.ts.map +1 -1
  175. package/dist/transform/write/blocks.js +2 -1
  176. package/dist/transform/write/blocks.js.map +1 -1
  177. package/dist/transform/write/index.d.ts.map +1 -1
  178. package/dist/transform/write/index.js +2 -0
  179. package/dist/transform/write/index.js.map +1 -1
  180. package/dist/transform/write/traverseFields.d.ts +2 -1
  181. package/dist/transform/write/traverseFields.d.ts.map +1 -1
  182. package/dist/transform/write/traverseFields.js +51 -21
  183. package/dist/transform/write/traverseFields.js.map +1 -1
  184. package/dist/transform/write/types.d.ts +7 -0
  185. package/dist/transform/write/types.d.ts.map +1 -1
  186. package/dist/transform/write/types.js.map +1 -1
  187. package/dist/types.d.ts +3 -2
  188. package/dist/types.d.ts.map +1 -1
  189. package/dist/types.js.map +1 -1
  190. package/dist/updateGlobal.d.ts +5 -1
  191. package/dist/updateGlobal.d.ts.map +1 -1
  192. package/dist/updateGlobal.js +96 -4
  193. package/dist/updateGlobal.js.map +1 -1
  194. package/dist/updateOne.d.ts.map +1 -1
  195. package/dist/updateOne.js +93 -4
  196. package/dist/updateOne.js.map +1 -1
  197. package/dist/upsertBranchGlobalChange.d.ts +3 -0
  198. package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
  199. package/dist/upsertBranchGlobalChange.js +35 -0
  200. package/dist/upsertBranchGlobalChange.js.map +1 -0
  201. package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
  202. package/dist/upsertRow/deleteExistingRowsByPath.js +17 -12
  203. package/dist/upsertRow/deleteExistingRowsByPath.js.map +1 -1
  204. package/dist/upsertRow/index.d.ts +1 -1
  205. package/dist/upsertRow/index.d.ts.map +1 -1
  206. package/dist/upsertRow/index.js +156 -46
  207. package/dist/upsertRow/index.js.map +1 -1
  208. package/dist/upsertRow/types.d.ts +3 -0
  209. package/dist/upsertRow/types.d.ts.map +1 -1
  210. package/dist/upsertRow/types.js.map +1 -1
  211. package/dist/utilities/blocksToJsonMigrator.d.ts.map +1 -1
  212. package/dist/utilities/blocksToJsonMigrator.js +7 -18
  213. package/dist/utilities/blocksToJsonMigrator.js.map +1 -1
  214. package/dist/utilities/buildCreateMigration.js +1 -1
  215. package/dist/utilities/buildCreateMigration.js.map +1 -1
  216. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts +15 -0
  217. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts.map +1 -0
  218. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js +110 -0
  219. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js.map +1 -0
  220. package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
  221. package/dist/utilities/createSchemaGenerator.js +7 -12
  222. package/dist/utilities/createSchemaGenerator.js.map +1 -1
  223. package/dist/utilities/executeSchemaHooks.js +1 -1
  224. package/dist/utilities/executeSchemaHooks.js.map +1 -1
  225. package/dist/utilities/formatGeneratedFile.d.ts +10 -0
  226. package/dist/utilities/formatGeneratedFile.d.ts.map +1 -0
  227. package/dist/utilities/formatGeneratedFile.js +58 -0
  228. package/dist/utilities/formatGeneratedFile.js.map +1 -0
  229. package/dist/utilities/getSizesToVariantsRenames.d.ts +54 -0
  230. package/dist/utilities/getSizesToVariantsRenames.d.ts.map +1 -0
  231. package/dist/utilities/getSizesToVariantsRenames.js +251 -0
  232. package/dist/utilities/getSizesToVariantsRenames.js.map +1 -0
  233. package/dist/utilities/getTransaction.js.map +1 -1
  234. package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
  235. package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
  236. package/dist/utilities/parseJSONPathSegment.js +17 -0
  237. package/dist/utilities/parseJSONPathSegment.js.map +1 -0
  238. package/dist/utilities/pushDevSchema.js +1 -1
  239. package/dist/utilities/pushDevSchema.js.map +1 -1
  240. package/package.json +17 -19
  241. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.spec.js +0 -91
  242. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.spec.js.map +0 -1
  243. package/dist/postgres/query/postgresUnaccent.spec.js +0 -71
  244. package/dist/postgres/query/postgresUnaccent.spec.js.map +0 -1
  245. package/dist/queries/buildOperatorConstraint.spec.js +0 -348
  246. package/dist/queries/buildOperatorConstraint.spec.js.map +0 -1
  247. package/dist/queries/validateOperatorHandlers.spec.js +0 -142
  248. package/dist/queries/validateOperatorHandlers.spec.js.map +0 -1
  249. package/dist/schema/buildRawSchema.spec.js +0 -290
  250. package/dist/schema/buildRawSchema.spec.js.map +0 -1
  251. package/dist/sqlite/requireDrizzleKit.d.ts +0 -3
  252. package/dist/sqlite/requireDrizzleKit.d.ts.map +0 -1
  253. package/dist/sqlite/requireDrizzleKit.js +0 -12
  254. package/dist/sqlite/requireDrizzleKit.js.map +0 -1
  255. package/dist/upsertRow/handleUpsertError.spec.js +0 -42
  256. package/dist/upsertRow/handleUpsertError.spec.js.map +0 -1
  257. package/dist/utilities/checkTruncatedIdentifiers.spec.js +0 -301
  258. package/dist/utilities/checkTruncatedIdentifiers.spec.js.map +0 -1
  259. package/dist/utilities/isValidStringID.spec.js +0 -15
  260. package/dist/utilities/isValidStringID.spec.js.map +0 -1
  261. package/dist/utilities/validateIdentifierLength.spec.js +0 -19
  262. package/dist/utilities/validateIdentifierLength.spec.js.map +0 -1
@@ -1,5 +1,5 @@
1
- import { and, eq, getTableName, like, sql } from 'drizzle-orm';
2
- import { APIError, getFieldByPath } from 'payload';
1
+ import { and, eq, getTableName, isNull, like, notInArray, or, sql } from 'drizzle-orm';
2
+ import { APIError, getBranchPredicateSync, getFieldByPath, resolveBranchReadState } from 'payload';
3
3
  import { fieldShouldBeLocalized, tabHasName } from 'payload/shared';
4
4
  import toSnakeCase from 'to-snake-case';
5
5
  import { validate as uuidValidate } from 'uuid';
@@ -9,8 +9,77 @@ import { jsonBuildObject } from '../utilities/json.js';
9
9
  import { DistinctSymbol } from '../utilities/rawConstraint.js';
10
10
  import { resolveBlockTableName } from '../utilities/validateExistingBlockIsIdentical.js';
11
11
  import { addJoinTable } from './addJoinTable.js';
12
+ import { buildBranchVisibility } from './buildBranchVisibility.js';
12
13
  import { getTableAlias } from './getTableAlias.js';
13
14
  import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRelationshipPath.js';
15
+ const getRelationshipJoinCondition = ({ adapter, collectionSlug, relationshipID, req, table, value })=>{
16
+ const branchReadState = resolveBranchReadState({
17
+ collectionSlug,
18
+ req
19
+ });
20
+ const branchPredicate = branchReadState.useBranching ? null : getBranchPredicateSync({
21
+ collectionSlug,
22
+ req
23
+ });
24
+ const shouldMatchCanonicalID = Boolean(table._branchDocID && (branchReadState.useBranching || branchPredicate || value === DistinctSymbol));
25
+ const identityCondition = shouldMatchCanonicalID ? or(eq(table.id, relationshipID), eq(table._branchDocID, relationshipID)) : eq(table.id, relationshipID);
26
+ const branchCondition = getBranchTableCondition({
27
+ adapter,
28
+ collectionSlug,
29
+ req,
30
+ table
31
+ });
32
+ return branchCondition ? and(identityCondition, branchCondition) : identityCondition;
33
+ };
34
+ const getBranchTableCondition = ({ adapter, collectionSlug, req, table })=>{
35
+ const branchReadState = resolveBranchReadState({
36
+ collectionSlug,
37
+ req
38
+ });
39
+ if (branchReadState.useBranching) {
40
+ return buildBranchVisibility({
41
+ adapter,
42
+ branch: branchReadState.branch,
43
+ collectionSlug,
44
+ table
45
+ });
46
+ }
47
+ const branchPredicate = getBranchPredicateSync({
48
+ collectionSlug,
49
+ req
50
+ });
51
+ return branchPredicate ? buildBranchPredicateCondition({
52
+ table,
53
+ where: branchPredicate
54
+ }) : undefined;
55
+ };
56
+ const buildBranchPredicateCondition = ({ table, where })=>{
57
+ const conditions = [];
58
+ for (const [fieldName, condition] of Object.entries(where)){
59
+ if ((fieldName === 'and' || fieldName === 'or') && Array.isArray(condition)) {
60
+ const nestedConditions = condition.map((nestedWhere)=>buildBranchPredicateCondition({
61
+ table,
62
+ where: nestedWhere
63
+ })).filter((nestedCondition)=>Boolean(nestedCondition));
64
+ const nestedResult = fieldName === 'and' ? and(...nestedConditions) : or(...nestedConditions);
65
+ if (nestedResult) {
66
+ conditions.push(nestedResult);
67
+ }
68
+ continue;
69
+ }
70
+ const column = table[fieldName];
71
+ if (!column || !condition || typeof condition !== 'object') {
72
+ continue;
73
+ }
74
+ if ('equals' in condition) {
75
+ conditions.push(condition.equals === null ? isNull(column) : eq(column, condition.equals));
76
+ }
77
+ if ('not_in' in condition && Array.isArray(condition.not_in) && condition.not_in.length > 0) {
78
+ conditions.push(notInArray(column, condition.not_in));
79
+ }
80
+ }
81
+ return conditions.length === 1 ? conditions[0] : and(...conditions);
82
+ };
14
83
  /**
15
84
  * Resolves a field path to the database table and column that store its value.
16
85
  *
@@ -22,7 +91,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
22
91
  * to the `movies` table. The returned column is `movies.name`.
23
92
  *
24
93
  * @returns The final field and column, plus any extra constraints discovered while resolving it.
25
- */ export const getTableColumnFromPath = ({ adapter, aliasTable, collectionPath, columnPrefix = '', constraintPath: incomingConstraintPath, constraints = [], fields, joins, locale: incomingLocale, parentAliasTable, parentIsLocalized, pathSegments: incomingSegments, rootTableName: incomingRootTableName, selectFields, selectLocale, tableName, tableNameSuffix = '', value })=>{
94
+ */ export const getTableColumnFromPath = ({ adapter, aliasTable, collectionPath, columnPrefix = '', constraintPath: incomingConstraintPath, constraints = [], fields, joins, locale: incomingLocale, parentAliasTable, parentIsLocalized, pathSegments: incomingSegments, req, rootTableName: incomingRootTableName, selectFields, selectLocale, tableName, tableNameSuffix = '', value })=>{
26
95
  const fieldPath = incomingSegments[0];
27
96
  let locale = incomingLocale;
28
97
  const rootTableName = incomingRootTableName || tableName;
@@ -31,7 +100,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
31
100
  const field = fields.find((fieldToFind)=>fieldToFind.name === fieldPath);
32
101
  let newTableName = tableName;
33
102
  if (!field && fieldPath === 'id') {
34
- selectFields.id = adapter.tables[newTableName].id;
103
+ const idTable = aliasTable ?? adapter.tables[newTableName];
35
104
  return {
36
105
  columnName: 'id',
37
106
  constraints,
@@ -39,7 +108,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
39
108
  name: 'id',
40
109
  type: isUUIDType(adapter.idType) ? 'text' : 'number'
41
110
  },
42
- table: adapter.tables[newTableName]
111
+ table: idTable
43
112
  };
44
113
  }
45
114
  let localizedPathQuery = false;
@@ -106,6 +175,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
106
175
  parentAliasTable: aliasTable,
107
176
  parentIsLocalized: parentIsLocalized || field.localized,
108
177
  pathSegments: pathSegments.slice(1),
178
+ req,
109
179
  rootTableName,
110
180
  selectFields,
111
181
  selectLocale,
@@ -189,6 +259,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
189
259
  locale,
190
260
  parentIsLocalized: parentIsLocalized || field.localized,
191
261
  pathSegments: pathSegments.slice(1),
262
+ req,
192
263
  rootTableName,
193
264
  selectFields: blockSelectFields,
194
265
  selectLocale,
@@ -255,6 +326,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
255
326
  locale,
256
327
  parentIsLocalized: parentIsLocalized || field.localized,
257
328
  pathSegments: pathSegments.slice(1),
329
+ req,
258
330
  rootTableName,
259
331
  selectFields,
260
332
  selectLocale,
@@ -269,6 +341,15 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
269
341
  throw new APIError('Not supported');
270
342
  }
271
343
  const newCollectionPath = pathSegments.slice(1).join('.');
344
+ // Filtering through a polymorphic `on` relationship is not supported: it is stored in the
345
+ // `_rels` table with no direct column to correlate on, which would otherwise build invalid SQL.
346
+ const onRelationshipField = getFieldByPath({
347
+ fields: adapter.payload.collections[field.collection].config.flattenedFields,
348
+ path: field.on
349
+ })?.field;
350
+ if (onRelationshipField && (onRelationshipField.type === 'relationship' || onRelationshipField.type === 'upload') && Array.isArray(onRelationshipField.relationTo)) {
351
+ throw new APIError('Not supported');
352
+ }
272
353
  if (field.hasMany) {
273
354
  const relationTableName = `${adapter.tableNameMap.get(toSnakeCase(field.collection))}${adapter.relationshipsSuffix}`;
274
355
  const existingTable = joins.find((e)=>e.queryPath === `${constraintPath}${field.name}._rels`);
@@ -312,8 +393,15 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
312
393
  tableName: relationshipTableName
313
394
  }).newAliasTable;
314
395
  if (!existingMainTable) {
396
+ const branchCondition = getBranchTableCondition({
397
+ adapter,
398
+ collectionSlug: field.collection,
399
+ req,
400
+ table: relationshipTable
401
+ });
402
+ const relationshipCondition = eq(aliasRelationshipTable.parent, relationshipTable.id);
315
403
  joins.push({
316
- condition: eq(aliasRelationshipTable.parent, relationshipTable.id),
404
+ condition: branchCondition ? and(relationshipCondition, branchCondition) : relationshipCondition,
317
405
  queryPath: `${constraintPath}${field.name}`,
318
406
  table: relationshipTable
319
407
  });
@@ -329,6 +417,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
329
417
  // A join reads from a different collection, so localization does not carry over
330
418
  parentIsLocalized: false,
331
419
  pathSegments: pathSegments.slice(1),
420
+ req,
332
421
  rootTableName: relationshipTableName,
333
422
  selectFields,
334
423
  selectLocale,
@@ -359,13 +448,29 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
359
448
  table: arrayAliasTable
360
449
  });
361
450
  joins.push({
362
- condition: eq(newAliasTable.id, arrayAliasTable._parentID),
451
+ condition: (()=>{
452
+ const relationshipCondition = eq(newAliasTable['id'], arrayAliasTable._parentID);
453
+ const branchCondition = getBranchTableCondition({
454
+ adapter,
455
+ collectionSlug: field.collection,
456
+ req,
457
+ table: newAliasTable
458
+ });
459
+ return branchCondition ? and(relationshipCondition, branchCondition) : relationshipCondition;
460
+ })(),
363
461
  queryPath: `${constraintPath}${field.name}`,
364
462
  table: newAliasTable
365
463
  });
366
464
  } else {
465
+ const relationshipCondition = eq(newAliasTable[field.on.replaceAll('.', '_')], aliasTable ? aliasTable.id : adapter.tables[tableName].id);
466
+ const branchCondition = getBranchTableCondition({
467
+ adapter,
468
+ collectionSlug: field.collection,
469
+ req,
470
+ table: newAliasTable
471
+ });
367
472
  joins.push({
368
- condition: eq(newAliasTable[field.on.replaceAll('.', '_')], aliasTable ? aliasTable.id : adapter.tables[tableName].id),
473
+ condition: branchCondition ? and(relationshipCondition, branchCondition) : relationshipCondition,
369
474
  queryPath: `${constraintPath}${field.name}`,
370
475
  table: newAliasTable
371
476
  });
@@ -394,6 +499,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
394
499
  // A join reads from a different collection, so localization does not carry over
395
500
  parentIsLocalized: false,
396
501
  pathSegments: pathSegments.slice(1),
502
+ req,
397
503
  selectFields,
398
504
  tableName: newTableName,
399
505
  value
@@ -454,6 +560,8 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
454
560
  case 'upload':
455
561
  {
456
562
  const newCollectionPath = pathSegments.slice(1).join('.');
563
+ const relationshipQueryPath = `${getTableName(tableContainingField)}.${fieldStoragePath}._rels`;
564
+ const targetQueryPath = `${getTableName(tableContainingField)}.${fieldStoragePath}._target${isFieldLocalized && locale ? `.${locale}` : ''}`;
457
565
  if (Array.isArray(field.relationTo) || field.hasMany) {
458
566
  const relationshipPath = resolveRelationshipPath({
459
567
  adapter,
@@ -468,7 +576,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
468
576
  }
469
577
  let relationshipFields;
470
578
  const relationTableName = `${rootTableName}${adapter.relationshipsSuffix}`;
471
- const existingJoin = joins.find((e)=>e.queryPath === `${constraintPath}.${field.name}`);
579
+ const existingJoin = joins.find((e)=>e.queryPath === relationshipQueryPath);
472
580
  let aliasRelationshipTable;
473
581
  let aliasRelationshipTableName;
474
582
  if (existingJoin) {
@@ -495,7 +603,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
495
603
  addJoinTable({
496
604
  condition: and(...conditions),
497
605
  joins,
498
- queryPath: `${constraintPath}.${field.name}`,
606
+ queryPath: relationshipQueryPath,
499
607
  table: aliasRelationshipTable
500
608
  });
501
609
  } else {
@@ -503,7 +611,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
503
611
  addJoinTable({
504
612
  condition: and(eq(tableContainingField.id, aliasRelationshipTable.parent), like(aliasRelationshipTable.path, relationshipPath.path)),
505
613
  joins,
506
- queryPath: `${constraintPath}.${field.name}`,
614
+ queryPath: relationshipQueryPath,
507
615
  table: aliasRelationshipTable
508
616
  });
509
617
  }
@@ -514,15 +622,31 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
514
622
  newTableName = adapter.tableNameMap.get(toSnakeCase(relationshipConfig.slug));
515
623
  // parent to relationship join table
516
624
  relationshipFields = relationshipConfig.flattenedFields;
517
- ({ newAliasTable } = getTableAlias({
518
- adapter,
519
- tableName: newTableName
520
- }));
521
- joins.push({
522
- condition: eq(newAliasTable.id, aliasRelationshipTable[`${field.relationTo}ID`]),
523
- table: newAliasTable
524
- });
525
- if (newCollectionPath === '' || newCollectionPath === 'id') {
625
+ const existingTargetJoin = joins.find((join)=>join.queryPath === targetQueryPath);
626
+ if (existingTargetJoin) {
627
+ newAliasTable = existingTargetJoin.table;
628
+ } else {
629
+ ;
630
+ ({ newAliasTable } = getTableAlias({
631
+ adapter,
632
+ tableName: newTableName
633
+ }));
634
+ const relationshipID = aliasRelationshipTable[`${field.relationTo}ID`];
635
+ const condition = getRelationshipJoinCondition({
636
+ adapter,
637
+ collectionSlug: field.relationTo,
638
+ relationshipID,
639
+ req,
640
+ table: newAliasTable,
641
+ value
642
+ });
643
+ joins.push({
644
+ condition,
645
+ queryPath: targetQueryPath,
646
+ table: newAliasTable
647
+ });
648
+ }
649
+ if (newCollectionPath === '' || newCollectionPath === 'id' && !existingTargetJoin) {
526
650
  return {
527
651
  columnName: `${field.relationTo}ID`,
528
652
  constraints,
@@ -625,21 +749,23 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
625
749
  // A relationship jumps to a different collection, so localization does not carry over
626
750
  parentIsLocalized: false,
627
751
  pathSegments: pathSegments.slice(1),
752
+ req,
628
753
  rootTableName: newTableName,
629
754
  selectFields,
630
755
  selectLocale,
631
756
  tableName: newTableName,
632
757
  value
633
758
  });
634
- } else if (pathSegments.length > 1 && !(pathSegments.length === 2 && pathSegments[1] === 'id')) {
759
+ } else if (pathSegments.length > 1 && (!(pathSegments.length === 2 && pathSegments[1] === 'id') || joins.some((join)=>join.queryPath === targetQueryPath))) {
635
760
  // simple relationships
636
761
  const columnName = `${columnPrefix}${field.name}`;
637
762
  const newTableName = adapter.tableNameMap.get(toSnakeCase(adapter.payload.collections[field.relationTo].config.slug));
638
- const { newAliasTable } = getTableAlias({
763
+ const existingTargetJoin = joins.find((join)=>join.queryPath === targetQueryPath);
764
+ const newAliasTable = existingTargetJoin?.table ?? getTableAlias({
639
765
  adapter,
640
766
  tableName: newTableName
641
- });
642
- if (isFieldLocalized && adapter.payload.config.localization) {
767
+ }).newAliasTable;
768
+ if (!existingTargetJoin && isFieldLocalized && adapter.payload.config.localization) {
643
769
  const { newAliasTable: aliasLocaleTable } = getTableAlias({
644
770
  adapter,
645
771
  tableName: `${rootTableName}${adapter.localesSuffix}`
@@ -653,19 +779,37 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
653
779
  if (locale !== 'all') {
654
780
  condtions.push(eq(aliasLocaleTable._locale, locale));
655
781
  }
656
- const localesTable = adapter.tables[`${rootTableName}${adapter.localesSuffix}`];
657
782
  addJoinTable({
658
783
  condition: and(...condtions),
659
784
  joins,
660
- table: localesTable
785
+ table: aliasLocaleTable
786
+ });
787
+ const condition = getRelationshipJoinCondition({
788
+ adapter,
789
+ collectionSlug: field.relationTo,
790
+ relationshipID: aliasLocaleTable[columnName],
791
+ req,
792
+ table: newAliasTable,
793
+ value
661
794
  });
662
795
  joins.push({
663
- condition: eq(localesTable[columnName], newAliasTable.id),
796
+ condition,
797
+ queryPath: targetQueryPath,
664
798
  table: newAliasTable
665
799
  });
666
- } else {
800
+ } else if (!existingTargetJoin) {
801
+ const relationshipID = aliasTable ? aliasTable[columnName] : adapter.tables[tableName][columnName];
802
+ const condition = getRelationshipJoinCondition({
803
+ adapter,
804
+ collectionSlug: field.relationTo,
805
+ relationshipID,
806
+ req,
807
+ table: newAliasTable,
808
+ value
809
+ });
667
810
  joins.push({
668
- condition: eq(newAliasTable.id, aliasTable ? aliasTable[columnName] : adapter.tables[tableName][columnName]),
811
+ condition,
812
+ queryPath: targetQueryPath,
669
813
  table: newAliasTable
670
814
  });
671
815
  }
@@ -681,6 +825,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
681
825
  // A relationship jumps to a different collection, so localization does not carry over
682
826
  parentIsLocalized: false,
683
827
  pathSegments: pathSegments.slice(1),
828
+ req,
684
829
  selectFields,
685
830
  tableName: newTableName,
686
831
  value
@@ -737,6 +882,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
737
882
  locale,
738
883
  parentIsLocalized: parentIsLocalized || field.localized,
739
884
  pathSegments: pathSegments.slice(1),
885
+ req,
740
886
  rootTableName,
741
887
  selectFields,
742
888
  selectLocale,
@@ -757,6 +903,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
757
903
  locale,
758
904
  parentIsLocalized: parentIsLocalized || field.localized,
759
905
  pathSegments: pathSegments.slice(1),
906
+ req,
760
907
  rootTableName,
761
908
  selectFields,
762
909
  selectLocale,