@payloadcms/drizzle 4.0.0-internal.934870d → 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.
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +25 -3
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.js +8 -3
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +29 -4
- package/dist/countVersions.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +17 -2
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +12 -1
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteBranchGlobal.d.ts +10 -0
- package/dist/deleteBranchGlobal.d.ts.map +1 -0
- package/dist/deleteBranchGlobal.js +27 -0
- package/dist/deleteBranchGlobal.js.map +1 -0
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +25 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/exports/postgres.d.ts +1 -1
- package/dist/exports/postgres.d.ts.map +1 -1
- package/dist/exports/postgres.js +1 -1
- package/dist/exports/postgres.js.map +1 -1
- package/dist/exports/sqlite.d.ts +1 -1
- package/dist/exports/sqlite.d.ts.map +1 -1
- package/dist/exports/sqlite.js +1 -1
- package/dist/exports/sqlite.js.map +1 -1
- package/dist/find/buildFindManyArgs.d.ts +3 -2
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +2 -1
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/buildPolymorphicJoinQuery.d.ts +5 -3
- package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -1
- package/dist/find/buildPolymorphicJoinQuery.js +43 -15
- package/dist/find/buildPolymorphicJoinQuery.js.map +1 -1
- package/dist/find/buildPolymorphicJoinWhere.js +1 -1
- package/dist/find/buildPolymorphicJoinWhere.js.map +1 -1
- package/dist/find/createPolymorphicJoinWherePlan.d.ts +2 -1
- package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -1
- package/dist/find/createPolymorphicJoinWherePlan.js +14 -5
- package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -1
- package/dist/find/findMany.d.ts +6 -1
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +16 -3
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +3 -2
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +64 -8
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +32 -4
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +104 -8
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +24 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.js +8 -3
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +30 -3
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +32 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/postgres/insert.d.ts.map +1 -1
- package/dist/postgres/insert.js +4 -2
- package/dist/postgres/insert.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/postgres/requireDrizzleKit.d.ts +4 -0
- package/dist/postgres/requireDrizzleKit.d.ts.map +1 -1
- package/dist/postgres/requireDrizzleKit.js +16 -10
- package/dist/postgres/requireDrizzleKit.js.map +1 -1
- package/dist/postgres/types.d.ts +4 -0
- package/dist/postgres/types.d.ts.map +1 -1
- package/dist/postgres/types.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +3 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildBranchVisibility.d.ts +11 -0
- package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
- package/dist/queries/buildBranchVisibility.js +48 -0
- package/dist/queries/buildBranchVisibility.js.map +1 -0
- package/dist/queries/buildOrderBy.d.ts +3 -2
- package/dist/queries/buildOrderBy.d.ts.map +1 -1
- package/dist/queries/buildOrderBy.js +2 -1
- package/dist/queries/buildOrderBy.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +3 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +4 -2
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +3 -2
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +167 -29
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts +3 -2
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +4 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +27 -5
- package/dist/queryDrafts.js.map +1 -1
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +28 -20
- package/dist/schema/build.js.map +1 -1
- package/dist/sqlite/createRequireDrizzleKit.d.ts +6 -0
- package/dist/sqlite/createRequireDrizzleKit.d.ts.map +1 -0
- package/dist/sqlite/createRequireDrizzleKit.js +15 -0
- package/dist/sqlite/createRequireDrizzleKit.js.map +1 -0
- package/dist/sqlite/execute.js.map +1 -1
- package/dist/sqlite/insert.d.ts.map +1 -1
- package/dist/sqlite/insert.js +5 -3
- package/dist/sqlite/insert.js.map +1 -1
- package/dist/sqlite/types.d.ts +4 -0
- package/dist/sqlite/types.d.ts.map +1 -1
- package/dist/sqlite/types.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +13 -5
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +2 -5
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/array.d.ts +3 -2
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/array.js +2 -1
- package/dist/transform/write/array.js.map +1 -1
- package/dist/transform/write/blocks.d.ts +3 -2
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/blocks.js +2 -1
- package/dist/transform/write/blocks.js.map +1 -1
- package/dist/transform/write/index.d.ts.map +1 -1
- package/dist/transform/write/index.js +2 -0
- package/dist/transform/write/index.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts +2 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +43 -7
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/transform/write/types.d.ts +7 -0
- package/dist/transform/write/types.d.ts.map +1 -1
- package/dist/transform/write/types.js.map +1 -1
- package/dist/types.d.ts +3 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/updateGlobal.d.ts +5 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +96 -4
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +54 -5
- package/dist/updateOne.js.map +1 -1
- package/dist/upsertBranchGlobalChange.d.ts +3 -0
- package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
- package/dist/upsertBranchGlobalChange.js +35 -0
- package/dist/upsertBranchGlobalChange.js.map +1 -0
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.js +17 -12
- package/dist/upsertRow/deleteExistingRowsByPath.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 +132 -42
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/types.d.ts +3 -0
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/blocksToJsonMigrator.d.ts.map +1 -1
- package/dist/utilities/blocksToJsonMigrator.js +7 -18
- package/dist/utilities/blocksToJsonMigrator.js.map +1 -1
- package/dist/utilities/buildCreateMigration.js +1 -1
- package/dist/utilities/buildCreateMigration.js.map +1 -1
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js +1 -1
- package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js.map +1 -1
- package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.js +7 -12
- package/dist/utilities/createSchemaGenerator.js.map +1 -1
- package/dist/utilities/executeSchemaHooks.js +1 -1
- package/dist/utilities/executeSchemaHooks.js.map +1 -1
- package/dist/utilities/formatGeneratedFile.d.ts +10 -0
- package/dist/utilities/formatGeneratedFile.d.ts.map +1 -0
- package/dist/utilities/formatGeneratedFile.js +58 -0
- package/dist/utilities/formatGeneratedFile.js.map +1 -0
- package/dist/utilities/getTransaction.js.map +1 -1
- package/dist/utilities/pushDevSchema.js +1 -1
- package/dist/utilities/pushDevSchema.js.map +1 -1
- package/package.json +17 -19
- package/dist/sqlite/requireDrizzleKit.d.ts +0 -3
- package/dist/sqlite/requireDrizzleKit.d.ts.map +0 -1
- package/dist/sqlite/requireDrizzleKit.js +0 -12
- package/dist/sqlite/requireDrizzleKit.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
|
-
|
|
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:
|
|
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,
|
|
@@ -321,8 +393,15 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
321
393
|
tableName: relationshipTableName
|
|
322
394
|
}).newAliasTable;
|
|
323
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);
|
|
324
403
|
joins.push({
|
|
325
|
-
condition:
|
|
404
|
+
condition: branchCondition ? and(relationshipCondition, branchCondition) : relationshipCondition,
|
|
326
405
|
queryPath: `${constraintPath}${field.name}`,
|
|
327
406
|
table: relationshipTable
|
|
328
407
|
});
|
|
@@ -338,6 +417,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
338
417
|
// A join reads from a different collection, so localization does not carry over
|
|
339
418
|
parentIsLocalized: false,
|
|
340
419
|
pathSegments: pathSegments.slice(1),
|
|
420
|
+
req,
|
|
341
421
|
rootTableName: relationshipTableName,
|
|
342
422
|
selectFields,
|
|
343
423
|
selectLocale,
|
|
@@ -368,13 +448,29 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
368
448
|
table: arrayAliasTable
|
|
369
449
|
});
|
|
370
450
|
joins.push({
|
|
371
|
-
condition:
|
|
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
|
+
})(),
|
|
372
461
|
queryPath: `${constraintPath}${field.name}`,
|
|
373
462
|
table: newAliasTable
|
|
374
463
|
});
|
|
375
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
|
+
});
|
|
376
472
|
joins.push({
|
|
377
|
-
condition:
|
|
473
|
+
condition: branchCondition ? and(relationshipCondition, branchCondition) : relationshipCondition,
|
|
378
474
|
queryPath: `${constraintPath}${field.name}`,
|
|
379
475
|
table: newAliasTable
|
|
380
476
|
});
|
|
@@ -403,6 +499,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
403
499
|
// A join reads from a different collection, so localization does not carry over
|
|
404
500
|
parentIsLocalized: false,
|
|
405
501
|
pathSegments: pathSegments.slice(1),
|
|
502
|
+
req,
|
|
406
503
|
selectFields,
|
|
407
504
|
tableName: newTableName,
|
|
408
505
|
value
|
|
@@ -463,6 +560,8 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
463
560
|
case 'upload':
|
|
464
561
|
{
|
|
465
562
|
const newCollectionPath = pathSegments.slice(1).join('.');
|
|
563
|
+
const relationshipQueryPath = `${getTableName(tableContainingField)}.${fieldStoragePath}._rels`;
|
|
564
|
+
const targetQueryPath = `${getTableName(tableContainingField)}.${fieldStoragePath}._target${isFieldLocalized && locale ? `.${locale}` : ''}`;
|
|
466
565
|
if (Array.isArray(field.relationTo) || field.hasMany) {
|
|
467
566
|
const relationshipPath = resolveRelationshipPath({
|
|
468
567
|
adapter,
|
|
@@ -477,7 +576,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
477
576
|
}
|
|
478
577
|
let relationshipFields;
|
|
479
578
|
const relationTableName = `${rootTableName}${adapter.relationshipsSuffix}`;
|
|
480
|
-
const existingJoin = joins.find((e)=>e.queryPath ===
|
|
579
|
+
const existingJoin = joins.find((e)=>e.queryPath === relationshipQueryPath);
|
|
481
580
|
let aliasRelationshipTable;
|
|
482
581
|
let aliasRelationshipTableName;
|
|
483
582
|
if (existingJoin) {
|
|
@@ -504,7 +603,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
504
603
|
addJoinTable({
|
|
505
604
|
condition: and(...conditions),
|
|
506
605
|
joins,
|
|
507
|
-
queryPath:
|
|
606
|
+
queryPath: relationshipQueryPath,
|
|
508
607
|
table: aliasRelationshipTable
|
|
509
608
|
});
|
|
510
609
|
} else {
|
|
@@ -512,7 +611,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
512
611
|
addJoinTable({
|
|
513
612
|
condition: and(eq(tableContainingField.id, aliasRelationshipTable.parent), like(aliasRelationshipTable.path, relationshipPath.path)),
|
|
514
613
|
joins,
|
|
515
|
-
queryPath:
|
|
614
|
+
queryPath: relationshipQueryPath,
|
|
516
615
|
table: aliasRelationshipTable
|
|
517
616
|
});
|
|
518
617
|
}
|
|
@@ -523,15 +622,31 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
523
622
|
newTableName = adapter.tableNameMap.get(toSnakeCase(relationshipConfig.slug));
|
|
524
623
|
// parent to relationship join table
|
|
525
624
|
relationshipFields = relationshipConfig.flattenedFields;
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
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) {
|
|
535
650
|
return {
|
|
536
651
|
columnName: `${field.relationTo}ID`,
|
|
537
652
|
constraints,
|
|
@@ -634,21 +749,23 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
634
749
|
// A relationship jumps to a different collection, so localization does not carry over
|
|
635
750
|
parentIsLocalized: false,
|
|
636
751
|
pathSegments: pathSegments.slice(1),
|
|
752
|
+
req,
|
|
637
753
|
rootTableName: newTableName,
|
|
638
754
|
selectFields,
|
|
639
755
|
selectLocale,
|
|
640
756
|
tableName: newTableName,
|
|
641
757
|
value
|
|
642
758
|
});
|
|
643
|
-
} 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))) {
|
|
644
760
|
// simple relationships
|
|
645
761
|
const columnName = `${columnPrefix}${field.name}`;
|
|
646
762
|
const newTableName = adapter.tableNameMap.get(toSnakeCase(adapter.payload.collections[field.relationTo].config.slug));
|
|
647
|
-
const
|
|
763
|
+
const existingTargetJoin = joins.find((join)=>join.queryPath === targetQueryPath);
|
|
764
|
+
const newAliasTable = existingTargetJoin?.table ?? getTableAlias({
|
|
648
765
|
adapter,
|
|
649
766
|
tableName: newTableName
|
|
650
|
-
});
|
|
651
|
-
if (isFieldLocalized && adapter.payload.config.localization) {
|
|
767
|
+
}).newAliasTable;
|
|
768
|
+
if (!existingTargetJoin && isFieldLocalized && adapter.payload.config.localization) {
|
|
652
769
|
const { newAliasTable: aliasLocaleTable } = getTableAlias({
|
|
653
770
|
adapter,
|
|
654
771
|
tableName: `${rootTableName}${adapter.localesSuffix}`
|
|
@@ -662,19 +779,37 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
662
779
|
if (locale !== 'all') {
|
|
663
780
|
condtions.push(eq(aliasLocaleTable._locale, locale));
|
|
664
781
|
}
|
|
665
|
-
const localesTable = adapter.tables[`${rootTableName}${adapter.localesSuffix}`];
|
|
666
782
|
addJoinTable({
|
|
667
783
|
condition: and(...condtions),
|
|
668
784
|
joins,
|
|
669
|
-
table:
|
|
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
|
|
670
794
|
});
|
|
671
795
|
joins.push({
|
|
672
|
-
condition
|
|
796
|
+
condition,
|
|
797
|
+
queryPath: targetQueryPath,
|
|
673
798
|
table: newAliasTable
|
|
674
799
|
});
|
|
675
|
-
} 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
|
+
});
|
|
676
810
|
joins.push({
|
|
677
|
-
condition
|
|
811
|
+
condition,
|
|
812
|
+
queryPath: targetQueryPath,
|
|
678
813
|
table: newAliasTable
|
|
679
814
|
});
|
|
680
815
|
}
|
|
@@ -690,6 +825,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
690
825
|
// A relationship jumps to a different collection, so localization does not carry over
|
|
691
826
|
parentIsLocalized: false,
|
|
692
827
|
pathSegments: pathSegments.slice(1),
|
|
828
|
+
req,
|
|
693
829
|
selectFields,
|
|
694
830
|
tableName: newTableName,
|
|
695
831
|
value
|
|
@@ -746,6 +882,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
746
882
|
locale,
|
|
747
883
|
parentIsLocalized: parentIsLocalized || field.localized,
|
|
748
884
|
pathSegments: pathSegments.slice(1),
|
|
885
|
+
req,
|
|
749
886
|
rootTableName,
|
|
750
887
|
selectFields,
|
|
751
888
|
selectLocale,
|
|
@@ -766,6 +903,7 @@ import { appendFieldToStoragePath, resolveRelationshipPath } from './resolveRela
|
|
|
766
903
|
locale,
|
|
767
904
|
parentIsLocalized: parentIsLocalized || field.localized,
|
|
768
905
|
pathSegments: pathSegments.slice(1),
|
|
906
|
+
req,
|
|
769
907
|
rootTableName,
|
|
770
908
|
selectFields,
|
|
771
909
|
selectLocale,
|