@payloadcms/db-postgres 3.0.0-beta.9 → 3.0.0-canary.0d307b1
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/dist/connect.d.ts +1 -1
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +12 -7
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts +3 -0
- package/dist/count.d.ts.map +1 -0
- package/dist/count.js +41 -0
- package/dist/count.js.map +1 -0
- package/dist/create.d.ts +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +3 -5
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts +1 -2
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +3 -5
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +3 -4
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +3 -7
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +56 -35
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts +1 -2
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +11 -19
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +2 -5
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +4 -7
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +3 -7
- package/dist/deleteVersions.js.map +1 -1
- package/dist/destroy.d.ts +1 -1
- package/dist/destroy.d.ts.map +1 -1
- package/dist/destroy.js +11 -7
- package/dist/destroy.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +2 -0
- package/dist/exports/migration-utils.d.ts.map +1 -0
- package/dist/exports/migration-utils.js +3 -0
- package/dist/exports/migration-utils.js.map +1 -0
- package/dist/find/buildFindManyArgs.d.ts +6 -2
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +4 -3
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/chainMethods.d.ts.map +1 -1
- package/dist/find/findMany.d.ts +1 -2
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +4 -15
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +4 -3
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +27 -25
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +2 -5
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +2 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +3 -7
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -2
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +2 -5
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +3 -7
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -9
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +20 -23
- package/dist/init.js.map +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +1 -1
- package/dist/migrate.js.map +1 -1
- package/dist/migrateDown.js +1 -1
- package/dist/migrateDown.js.map +1 -1
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/migrateRefresh.js +1 -1
- package/dist/migrateRefresh.js.map +1 -1
- package/dist/migrateReset.js +1 -1
- package/dist/migrateReset.js.map +1 -1
- package/dist/migrateStatus.js +1 -1
- package/dist/migrateStatus.js.map +1 -1
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.mjs +11 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts +19 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js +170 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts +11 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js +191 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts +3 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js +56 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/index.d.ts +23 -0
- package/dist/predefinedMigrations/v2-v3/index.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/index.js +211 -0
- package/dist/predefinedMigrations/v2-v3/index.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts +19 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.js +57 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts +22 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.js +85 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/types.d.ts +9 -0
- package/dist/predefinedMigrations/v2-v3/types.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/types.js +6 -0
- package/dist/predefinedMigrations/v2-v3/types.js.map +1 -0
- package/dist/queries/buildAndOrConditions.d.ts +4 -5
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +1 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +2 -3
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +1 -5
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +4 -5
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +250 -146
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts +4 -5
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +9 -8
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +2 -3
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queries/selectDistinct.d.ts +3 -4
- package/dist/queries/selectDistinct.d.ts.map +1 -1
- package/dist/queries/selectDistinct.js +3 -14
- package/dist/queries/selectDistinct.js.map +1 -1
- package/dist/queryDrafts.d.ts +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +3 -8
- package/dist/queryDrafts.js.map +1 -1
- package/dist/schema/build.d.ts +12 -7
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +208 -124
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/{getTableName.d.ts → createTableName.d.ts} +6 -8
- package/dist/schema/createTableName.d.ts.map +1 -0
- package/dist/schema/createTableName.js +31 -0
- package/dist/schema/createTableName.js.map +1 -0
- package/dist/schema/idToUUID.d.ts +1 -1
- package/dist/schema/idToUUID.d.ts.map +1 -1
- package/dist/schema/idToUUID.js.map +1 -1
- package/dist/schema/setColumnID.d.ts +1 -1
- package/dist/schema/setColumnID.d.ts.map +1 -1
- package/dist/schema/setColumnID.js +2 -2
- package/dist/schema/setColumnID.js.map +1 -1
- package/dist/schema/traverseFields.d.ts +5 -7
- package/dist/schema/traverseFields.d.ts.map +1 -1
- package/dist/schema/traverseFields.js +147 -63
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.d.ts +3 -2
- package/dist/schema/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.js +15 -9
- package/dist/schema/validateExistingBlockIsIdentical.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +4 -0
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/transform/read/hasManyNumber.d.ts +1 -1
- package/dist/transform/read/hasManyNumber.d.ts.map +1 -1
- package/dist/transform/read/hasManyNumber.js.map +1 -1
- package/dist/transform/read/hasManyText.d.ts +1 -1
- package/dist/transform/read/hasManyText.d.ts.map +1 -1
- package/dist/transform/read/hasManyText.js.map +1 -1
- package/dist/transform/read/index.d.ts +5 -3
- package/dist/transform/read/index.d.ts.map +1 -1
- package/dist/transform/read/index.js +2 -1
- package/dist/transform/read/index.js.map +1 -1
- package/dist/transform/read/relationship.d.ts +1 -1
- package/dist/transform/read/relationship.d.ts.map +1 -1
- package/dist/transform/read/relationship.js +0 -4
- package/dist/transform/read/relationship.js.map +1 -1
- package/dist/transform/read/traverseFields.d.ts +8 -3
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +70 -56
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/array.d.ts +1 -1
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/array.js.map +1 -1
- package/dist/transform/write/blocks.d.ts +1 -1
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/blocks.js +1 -1
- package/dist/transform/write/blocks.js.map +1 -1
- package/dist/transform/write/index.d.ts +1 -1
- package/dist/transform/write/index.d.ts.map +1 -1
- package/dist/transform/write/index.js.map +1 -1
- package/dist/transform/write/relationships.d.ts +1 -1
- package/dist/transform/write/relationships.d.ts.map +1 -1
- package/dist/transform/write/relationships.js +1 -1
- package/dist/transform/write/relationships.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +23 -6
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/types.d.ts +7 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/update.d.ts +1 -1
- package/dist/update.d.ts.map +1 -1
- package/dist/update.js +3 -7
- package/dist/update.js.map +1 -1
- package/dist/updateGlobal.d.ts +1 -2
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +2 -5
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +1 -2
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +3 -7
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateVersion.d.ts +1 -2
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +3 -7
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertRow/deleteExistingArrayRows.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/index.d.ts +2 -2
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +10 -7
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js +3 -3
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/types.d.ts +7 -2
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/appendPrefixToKeys.d.ts.map +1 -1
- package/dist/utilities/createBlocksMap.d.ts.map +1 -1
- package/dist/utilities/createMigrationTable.d.ts.map +1 -1
- package/dist/utilities/createRelationshipMap.d.ts.map +1 -1
- package/dist/utilities/hasLocalesTable.d.ts +1 -1
- package/dist/utilities/hasLocalesTable.d.ts.map +1 -1
- package/dist/utilities/hasLocalesTable.js +1 -1
- package/dist/utilities/hasLocalesTable.js.map +1 -1
- package/dist/utilities/migrationTableExists.d.ts.map +1 -1
- package/dist/utilities/pushDevSchema.d.ts.map +1 -1
- package/dist/utilities/pushDevSchema.js +1 -1
- package/dist/utilities/pushDevSchema.js.map +1 -1
- package/package.json +34 -29
- package/dist/schema/getTableName.d.ts.map +0 -1
- package/dist/schema/getTableName.js +0 -31
- package/dist/schema/getTableName.js.map +0 -1
- package/src/index.ts +0 -156
@@ -1,16 +1,14 @@
|
|
1
1
|
/* eslint-disable no-param-reassign */ import { and, eq, like, sql } from 'drizzle-orm';
|
2
2
|
import { alias } from 'drizzle-orm/pg-core';
|
3
|
-
import { APIError } from 'payload
|
4
|
-
import { fieldAffectsData, tabHasName } from 'payload/
|
5
|
-
import { flattenTopLevelFields } from 'payload/utilities';
|
3
|
+
import { APIError, flattenTopLevelFields } from 'payload';
|
4
|
+
import { fieldAffectsData, tabHasName } from 'payload/shared';
|
6
5
|
import toSnakeCase from 'to-snake-case';
|
7
6
|
import { v4 as uuid } from 'uuid';
|
8
|
-
import { getTableName } from '../schema/getTableName.js';
|
9
7
|
/**
|
10
8
|
* Transforms path to table and column name
|
11
9
|
* Adds tables to `join`
|
12
10
|
* @returns TableColumn
|
13
|
-
*/ export const getTableColumnFromPath = ({ adapter, aliasTable, collectionPath, columnPrefix = '', constraintPath: incomingConstraintPath, constraints = [], fields,
|
11
|
+
*/ export const getTableColumnFromPath = ({ adapter, aliasTable, collectionPath, columnPrefix = '', constraintPath: incomingConstraintPath, constraints = [], fields, joins, locale: incomingLocale, pathSegments: incomingSegments, rootTableName: incomingRootTableName, selectFields, tableName, tableNameSuffix = '', value })=>{
|
14
12
|
const fieldPath = incomingSegments[0];
|
15
13
|
let locale = incomingLocale;
|
16
14
|
const rootTableName = incomingRootTableName || tableName;
|
@@ -56,7 +54,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
56
54
|
...tab,
|
57
55
|
type: 'tab'
|
58
56
|
})),
|
59
|
-
joinAliases,
|
60
57
|
joins,
|
61
58
|
locale,
|
62
59
|
pathSegments: pathSegments.slice(1),
|
@@ -78,7 +75,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
78
75
|
constraintPath: `${constraintPath}${field.name}.`,
|
79
76
|
constraints,
|
80
77
|
fields: field.fields,
|
81
|
-
joinAliases,
|
82
78
|
joins,
|
83
79
|
locale,
|
84
80
|
pathSegments: pathSegments.slice(1),
|
@@ -97,7 +93,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
97
93
|
constraintPath,
|
98
94
|
constraints,
|
99
95
|
fields: field.fields,
|
100
|
-
joinAliases,
|
101
96
|
joins,
|
102
97
|
locale,
|
103
98
|
pathSegments: pathSegments.slice(1),
|
@@ -111,14 +106,11 @@ import { getTableName } from '../schema/getTableName.js';
|
|
111
106
|
case 'group':
|
112
107
|
{
|
113
108
|
if (locale && field.localized && adapter.payload.config.localization) {
|
114
|
-
newTableName =
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
parentTableName: tableName,
|
119
|
-
prefix: `${tableName}_`
|
109
|
+
newTableName = `${tableName}${adapter.localesSuffix}`;
|
110
|
+
joins.push({
|
111
|
+
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
|
112
|
+
table: adapter.tables[newTableName]
|
120
113
|
});
|
121
|
-
joins[tableName] = eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID);
|
122
114
|
if (locale !== 'all') {
|
123
115
|
constraints.push({
|
124
116
|
columnName: '_locale',
|
@@ -135,7 +127,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
135
127
|
constraintPath: `${constraintPath}${field.name}.`,
|
136
128
|
constraints,
|
137
129
|
fields: field.fields,
|
138
|
-
joinAliases,
|
139
130
|
joins,
|
140
131
|
locale,
|
141
132
|
pathSegments: pathSegments.slice(1),
|
@@ -146,17 +137,88 @@ import { getTableName } from '../schema/getTableName.js';
|
|
146
137
|
value
|
147
138
|
});
|
148
139
|
}
|
140
|
+
case 'select':
|
141
|
+
{
|
142
|
+
if (field.hasMany) {
|
143
|
+
const newTableName = adapter.tableNameMap.get(`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`);
|
144
|
+
if (locale && field.localized && adapter.payload.config.localization) {
|
145
|
+
joins.push({
|
146
|
+
condition: and(eq(adapter.tables[tableName].id, adapter.tables[newTableName].parent), eq(adapter.tables[newTableName]._locale, locale)),
|
147
|
+
table: adapter.tables[newTableName]
|
148
|
+
});
|
149
|
+
if (locale !== 'all') {
|
150
|
+
constraints.push({
|
151
|
+
columnName: '_locale',
|
152
|
+
table: adapter.tables[newTableName],
|
153
|
+
value: locale
|
154
|
+
});
|
155
|
+
}
|
156
|
+
} else {
|
157
|
+
joins.push({
|
158
|
+
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName].parent),
|
159
|
+
table: adapter.tables[newTableName]
|
160
|
+
});
|
161
|
+
}
|
162
|
+
return {
|
163
|
+
columnName: 'value',
|
164
|
+
constraints,
|
165
|
+
field,
|
166
|
+
table: adapter.tables[newTableName]
|
167
|
+
};
|
168
|
+
}
|
169
|
+
break;
|
170
|
+
}
|
171
|
+
case 'text':
|
172
|
+
case 'number':
|
173
|
+
{
|
174
|
+
if (field.hasMany) {
|
175
|
+
let tableType = 'texts';
|
176
|
+
let columnName = 'text';
|
177
|
+
if (field.type === 'number') {
|
178
|
+
tableType = 'numbers';
|
179
|
+
columnName = 'number';
|
180
|
+
}
|
181
|
+
newTableName = `${tableName}_${tableType}`;
|
182
|
+
const joinConstraints = [
|
183
|
+
eq(adapter.tables[tableName].id, adapter.tables[newTableName].parent),
|
184
|
+
eq(adapter.tables[newTableName].path, `${constraintPath}${field.name}`)
|
185
|
+
];
|
186
|
+
if (locale && field.localized && adapter.payload.config.localization) {
|
187
|
+
joins.push({
|
188
|
+
condition: and(...joinConstraints, eq(adapter.tables[newTableName]._locale, locale)),
|
189
|
+
table: adapter.tables[newTableName]
|
190
|
+
});
|
191
|
+
if (locale !== 'all') {
|
192
|
+
constraints.push({
|
193
|
+
columnName: 'locale',
|
194
|
+
table: adapter.tables[newTableName],
|
195
|
+
value: locale
|
196
|
+
});
|
197
|
+
}
|
198
|
+
} else {
|
199
|
+
joins.push({
|
200
|
+
condition: and(...joinConstraints),
|
201
|
+
table: adapter.tables[newTableName]
|
202
|
+
});
|
203
|
+
}
|
204
|
+
return {
|
205
|
+
columnName,
|
206
|
+
constraints,
|
207
|
+
field,
|
208
|
+
table: adapter.tables[newTableName]
|
209
|
+
};
|
210
|
+
}
|
211
|
+
break;
|
212
|
+
}
|
149
213
|
case 'array':
|
150
214
|
{
|
151
|
-
newTableName =
|
152
|
-
adapter,
|
153
|
-
config: field,
|
154
|
-
parentTableName: `${tableName}_${tableNameSuffix}`,
|
155
|
-
prefix: `${tableName}_${tableNameSuffix}`
|
156
|
-
});
|
215
|
+
newTableName = adapter.tableNameMap.get(`${tableName}_${tableNameSuffix}${toSnakeCase(field.name)}`);
|
157
216
|
constraintPath = `${constraintPath}${field.name}.%.`;
|
158
217
|
if (locale && field.localized && adapter.payload.config.localization) {
|
159
|
-
joins
|
218
|
+
joins.push({
|
219
|
+
condition: and(eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID), eq(adapter.tables[newTableName]._locale, locale)),
|
220
|
+
table: adapter.tables[newTableName]
|
221
|
+
});
|
160
222
|
if (locale !== 'all') {
|
161
223
|
constraints.push({
|
162
224
|
columnName: '_locale',
|
@@ -165,7 +227,10 @@ import { getTableName } from '../schema/getTableName.js';
|
|
165
227
|
});
|
166
228
|
}
|
167
229
|
} else {
|
168
|
-
joins
|
230
|
+
joins.push({
|
231
|
+
condition: eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID),
|
232
|
+
table: adapter.tables[newTableName]
|
233
|
+
});
|
169
234
|
}
|
170
235
|
return getTableColumnFromPath({
|
171
236
|
adapter,
|
@@ -173,7 +238,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
173
238
|
constraintPath,
|
174
239
|
constraints,
|
175
240
|
fields: field.fields,
|
176
|
-
joinAliases,
|
177
241
|
joins,
|
178
242
|
locale,
|
179
243
|
pathSegments: pathSegments.slice(1),
|
@@ -195,16 +259,16 @@ import { getTableName } from '../schema/getTableName.js';
|
|
195
259
|
];
|
196
260
|
blockTypes.forEach((blockType)=>{
|
197
261
|
const block = field.blocks.find((block)=>block.slug === blockType);
|
198
|
-
newTableName =
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
|
262
|
+
newTableName = adapter.tableNameMap.get(`${tableName}_blocks_${toSnakeCase(block.slug)}`);
|
263
|
+
const newAliasTableName = toSnakeCase(uuid());
|
264
|
+
const newAliasTable = alias(adapter.tables[newTableName], newAliasTableName);
|
265
|
+
joins.push({
|
266
|
+
condition: eq(adapter.tables[tableName].id, newAliasTable._parentID),
|
267
|
+
table: newAliasTable
|
203
268
|
});
|
204
|
-
joins[newTableName] = eq(adapter.tables[tableName].id, adapter.tables[newTableName]._parentID);
|
205
269
|
constraints.push({
|
206
270
|
columnName: '_path',
|
207
|
-
table:
|
271
|
+
table: newAliasTable,
|
208
272
|
value: pathSegments[0]
|
209
273
|
});
|
210
274
|
});
|
@@ -216,12 +280,7 @@ import { getTableName } from '../schema/getTableName.js';
|
|
216
280
|
};
|
217
281
|
}
|
218
282
|
const hasBlockField = field.blocks.some((block)=>{
|
219
|
-
newTableName =
|
220
|
-
adapter,
|
221
|
-
config: block,
|
222
|
-
parentTableName: tableName,
|
223
|
-
prefix: `${tableName}_blocks_`
|
224
|
-
});
|
283
|
+
newTableName = adapter.tableNameMap.get(`${tableName}_blocks_${toSnakeCase(block.slug)}`);
|
225
284
|
constraintPath = `${constraintPath}${field.name}.%.`;
|
226
285
|
let result;
|
227
286
|
const blockConstraints = [];
|
@@ -233,7 +292,6 @@ import { getTableName } from '../schema/getTableName.js';
|
|
233
292
|
constraintPath,
|
234
293
|
constraints: blockConstraints,
|
235
294
|
fields: block.fields,
|
236
|
-
joinAliases,
|
237
295
|
joins,
|
238
296
|
locale,
|
239
297
|
pathSegments: pathSegments.slice(1),
|
@@ -255,7 +313,10 @@ import { getTableName } from '../schema/getTableName.js';
|
|
255
313
|
...blockSelectFields
|
256
314
|
};
|
257
315
|
if (field.localized && adapter.payload.config.localization) {
|
258
|
-
joins
|
316
|
+
joins.push({
|
317
|
+
condition: and(eq((aliasTable || adapter.tables[tableName]).id, adapter.tables[newTableName]._parentID), eq(adapter.tables[newTableName]._locale, locale)),
|
318
|
+
table: adapter.tables[newTableName]
|
319
|
+
});
|
259
320
|
if (locale) {
|
260
321
|
constraints.push({
|
261
322
|
columnName: '_locale',
|
@@ -264,7 +325,10 @@ import { getTableName } from '../schema/getTableName.js';
|
|
264
325
|
});
|
265
326
|
}
|
266
327
|
} else {
|
267
|
-
joins
|
328
|
+
joins.push({
|
329
|
+
condition: eq((aliasTable || adapter.tables[tableName]).id, adapter.tables[newTableName]._parentID),
|
330
|
+
table: adapter.tables[newTableName]
|
331
|
+
});
|
268
332
|
}
|
269
333
|
return true;
|
270
334
|
});
|
@@ -283,128 +347,168 @@ import { getTableName } from '../schema/getTableName.js';
|
|
283
347
|
case 'relationship':
|
284
348
|
case 'upload':
|
285
349
|
{
|
286
|
-
let relationshipFields;
|
287
|
-
const relationTableName = `${rootTableName}${adapter.relationshipsSuffix}`;
|
288
350
|
const newCollectionPath = pathSegments.slice(1).join('.');
|
289
|
-
|
290
|
-
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
296
|
-
|
297
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
351
|
+
if (Array.isArray(field.relationTo) || field.type === 'relationship' && field.hasMany) {
|
352
|
+
let relationshipFields;
|
353
|
+
const relationTableName = `${rootTableName}${adapter.relationshipsSuffix}`;
|
354
|
+
const aliasRelationshipTableName = uuid();
|
355
|
+
const aliasRelationshipTable = alias(adapter.tables[relationTableName], aliasRelationshipTableName);
|
356
|
+
// Join in the relationships table
|
357
|
+
if (locale && field.localized && adapter.payload.config.localization) {
|
358
|
+
joins.push({
|
359
|
+
condition: and(eq((aliasTable || adapter.tables[rootTableName]).id, aliasRelationshipTable.parent), eq(aliasRelationshipTable.locale, locale), like(aliasRelationshipTable.path, `${constraintPath}${field.name}`)),
|
360
|
+
table: aliasRelationshipTable
|
361
|
+
});
|
362
|
+
if (locale !== 'all') {
|
363
|
+
constraints.push({
|
364
|
+
columnName: 'locale',
|
365
|
+
table: aliasRelationshipTable,
|
366
|
+
value: locale
|
367
|
+
});
|
368
|
+
}
|
369
|
+
} else {
|
370
|
+
// Join in the relationships table
|
371
|
+
joins.push({
|
372
|
+
condition: and(eq((aliasTable || adapter.tables[rootTableName]).id, aliasRelationshipTable.parent), like(aliasRelationshipTable.path, `${constraintPath}${field.name}`)),
|
373
|
+
table: aliasRelationshipTable
|
302
374
|
});
|
303
375
|
}
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
324
|
-
|
325
|
-
|
326
|
-
|
376
|
+
selectFields[`${relationTableName}.path`] = aliasRelationshipTable.path;
|
377
|
+
let newAliasTable;
|
378
|
+
if (typeof field.relationTo === 'string') {
|
379
|
+
const relationshipConfig = adapter.payload.collections[field.relationTo].config;
|
380
|
+
newTableName = adapter.tableNameMap.get(toSnakeCase(relationshipConfig.slug));
|
381
|
+
// parent to relationship join table
|
382
|
+
relationshipFields = relationshipConfig.fields;
|
383
|
+
newAliasTable = alias(adapter.tables[newTableName], toSnakeCase(uuid()));
|
384
|
+
joins.push({
|
385
|
+
condition: eq(newAliasTable.id, aliasRelationshipTable[`${field.relationTo}ID`]),
|
386
|
+
table: newAliasTable
|
387
|
+
});
|
388
|
+
if (newCollectionPath === '' || newCollectionPath === 'id') {
|
389
|
+
return {
|
390
|
+
columnName: `${field.relationTo}ID`,
|
391
|
+
constraints,
|
392
|
+
field,
|
393
|
+
table: aliasRelationshipTable
|
394
|
+
};
|
395
|
+
}
|
396
|
+
} else if (newCollectionPath === 'value') {
|
397
|
+
const tableColumnsNames = field.relationTo.map((relationTo)=>{
|
398
|
+
const relationTableName = adapter.tableNameMap.get(toSnakeCase(adapter.payload.collections[relationTo].config.slug));
|
399
|
+
return `"${aliasRelationshipTableName}"."${relationTableName}_id"`;
|
400
|
+
});
|
327
401
|
return {
|
328
|
-
columnName: `${field.relationTo}ID`,
|
329
402
|
constraints,
|
330
403
|
field,
|
404
|
+
rawColumn: sql.raw(`COALESCE(${tableColumnsNames.join(', ')})`),
|
331
405
|
table: aliasRelationshipTable
|
332
406
|
};
|
407
|
+
} else if (newCollectionPath === 'relationTo') {
|
408
|
+
const relationTo = Array.isArray(field.relationTo) ? field.relationTo : [
|
409
|
+
field.relationTo
|
410
|
+
];
|
411
|
+
return {
|
412
|
+
constraints,
|
413
|
+
field,
|
414
|
+
getNotNullColumnByValue: (val)=>{
|
415
|
+
const matchedRelation = relationTo.find((relation)=>relation === val);
|
416
|
+
if (matchedRelation) return `${matchedRelation}ID`;
|
417
|
+
return undefined;
|
418
|
+
},
|
419
|
+
table: aliasRelationshipTable
|
420
|
+
};
|
421
|
+
} else {
|
422
|
+
throw new APIError('Not supported');
|
333
423
|
}
|
334
|
-
|
335
|
-
|
336
|
-
|
337
|
-
|
338
|
-
})}_id"`);
|
339
|
-
return {
|
424
|
+
return getTableColumnFromPath({
|
425
|
+
adapter,
|
426
|
+
aliasTable: newAliasTable,
|
427
|
+
collectionPath: newCollectionPath,
|
340
428
|
constraints,
|
341
|
-
|
342
|
-
|
343
|
-
|
344
|
-
|
345
|
-
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
|
429
|
+
fields: relationshipFields,
|
430
|
+
joins,
|
431
|
+
locale,
|
432
|
+
pathSegments: pathSegments.slice(1),
|
433
|
+
rootTableName: newTableName,
|
434
|
+
selectFields,
|
435
|
+
tableName: newTableName,
|
436
|
+
value
|
437
|
+
});
|
438
|
+
} else if (pathSegments.length > 1 && !(pathSegments.length === 2 && pathSegments[1] === 'id')) {
|
439
|
+
// simple relationships
|
440
|
+
const columnName = `${columnPrefix}${field.name}`;
|
441
|
+
const newTableName = adapter.tableNameMap.get(toSnakeCase(adapter.payload.collections[field.relationTo].config.slug));
|
442
|
+
const aliasTableName = uuid();
|
443
|
+
const newAliasTable = alias(adapter.tables[newTableName], aliasTableName);
|
444
|
+
if (field.localized && adapter.payload.config.localization) {
|
445
|
+
const aliasLocaleTableName = uuid();
|
446
|
+
const aliasLocaleTable = alias(adapter.tables[`${rootTableName}${adapter.localesSuffix}`], aliasLocaleTableName);
|
447
|
+
joins.push({
|
448
|
+
condition: and(eq(aliasLocaleTable._parentID, adapter.tables[rootTableName].id), eq(aliasLocaleTable._locale, locale)),
|
449
|
+
table: aliasLocaleTable
|
450
|
+
});
|
451
|
+
joins.push({
|
452
|
+
condition: eq(aliasLocaleTable[columnName], newAliasTable.id),
|
453
|
+
table: newAliasTable
|
454
|
+
});
|
455
|
+
} else {
|
456
|
+
joins.push({
|
457
|
+
condition: eq(newAliasTable.id, aliasTable ? aliasTable[columnName] : adapter.tables[tableName][columnName]),
|
458
|
+
table: newAliasTable
|
459
|
+
});
|
460
|
+
}
|
461
|
+
return getTableColumnFromPath({
|
462
|
+
adapter,
|
463
|
+
aliasTable: newAliasTable,
|
464
|
+
collectionPath: newCollectionPath,
|
465
|
+
constraintPath: '',
|
350
466
|
constraints,
|
351
|
-
field,
|
352
|
-
|
353
|
-
|
354
|
-
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
};
|
359
|
-
} else {
|
360
|
-
throw new APIError('Not supported');
|
467
|
+
fields: adapter.payload.collections[field.relationTo].config.fields,
|
468
|
+
joins,
|
469
|
+
locale,
|
470
|
+
pathSegments: pathSegments.slice(1),
|
471
|
+
selectFields,
|
472
|
+
tableName: newTableName,
|
473
|
+
value
|
474
|
+
});
|
361
475
|
}
|
362
|
-
|
363
|
-
adapter,
|
364
|
-
aliasTable: newAliasTable,
|
365
|
-
collectionPath: newCollectionPath,
|
366
|
-
constraints,
|
367
|
-
fields: relationshipFields,
|
368
|
-
joinAliases,
|
369
|
-
joins,
|
370
|
-
locale,
|
371
|
-
pathSegments: pathSegments.slice(1),
|
372
|
-
rootTableName: newTableName,
|
373
|
-
selectFields,
|
374
|
-
tableName: newTableName,
|
375
|
-
value
|
376
|
-
});
|
476
|
+
break;
|
377
477
|
}
|
378
478
|
default:
|
379
479
|
{
|
380
|
-
|
381
|
-
if (field.localized && adapter.payload.config.localization) {
|
382
|
-
// If localized, we go to localized table and set aliasTable to undefined
|
383
|
-
// so it is not picked up below to be used as targetTable
|
384
|
-
newTableName = `${tableName}${adapter.localesSuffix}`;
|
385
|
-
const parentTable = aliasTable || adapter.tables[tableName];
|
386
|
-
joins[newTableName] = eq(parentTable.id, adapter.tables[newTableName]._parentID);
|
387
|
-
aliasTable = undefined;
|
388
|
-
if (locale !== 'all') {
|
389
|
-
constraints.push({
|
390
|
-
columnName: '_locale',
|
391
|
-
table: adapter.tables[newTableName],
|
392
|
-
value: locale
|
393
|
-
});
|
394
|
-
}
|
395
|
-
}
|
396
|
-
const targetTable = aliasTable || adapter.tables[newTableName];
|
397
|
-
selectFields[`${newTableName}.${columnPrefix}${field.name}`] = targetTable[`${columnPrefix}${field.name}`];
|
398
|
-
return {
|
399
|
-
columnName: `${columnPrefix}${field.name}`,
|
400
|
-
constraints,
|
401
|
-
field,
|
402
|
-
pathSegments,
|
403
|
-
table: targetTable
|
404
|
-
};
|
405
|
-
}
|
480
|
+
break;
|
406
481
|
}
|
407
482
|
}
|
483
|
+
if (fieldAffectsData(field)) {
|
484
|
+
if (field.localized && adapter.payload.config.localization) {
|
485
|
+
// If localized, we go to localized table and set aliasTable to undefined
|
486
|
+
// so it is not picked up below to be used as targetTable
|
487
|
+
const parentTable = aliasTable || adapter.tables[tableName];
|
488
|
+
newTableName = `${tableName}${adapter.localesSuffix}`;
|
489
|
+
joins.push({
|
490
|
+
condition: eq(parentTable.id, adapter.tables[newTableName]._parentID),
|
491
|
+
table: adapter.tables[newTableName]
|
492
|
+
});
|
493
|
+
aliasTable = undefined;
|
494
|
+
if (locale !== 'all') {
|
495
|
+
constraints.push({
|
496
|
+
columnName: '_locale',
|
497
|
+
table: adapter.tables[newTableName],
|
498
|
+
value: locale
|
499
|
+
});
|
500
|
+
}
|
501
|
+
}
|
502
|
+
const targetTable = aliasTable || adapter.tables[newTableName];
|
503
|
+
selectFields[`${newTableName}.${columnPrefix}${field.name}`] = targetTable[`${columnPrefix}${field.name}`];
|
504
|
+
return {
|
505
|
+
columnName: `${columnPrefix}${field.name}`,
|
506
|
+
constraints,
|
507
|
+
field,
|
508
|
+
pathSegments,
|
509
|
+
table: targetTable
|
510
|
+
};
|
511
|
+
}
|
408
512
|
}
|
409
513
|
throw new APIError(`Cannot find field for path at ${fieldPath}`);
|
410
514
|
};
|