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