@payloadcms/db-postgres 3.0.0-canary.f6e77b8 → 3.0.0-canary.f83d96a
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 +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +1 -10
- package/dist/count.js.map +1 -1
- package/dist/create.d.ts +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts +2 -3
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +2 -3
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +1 -1
- 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 +3 -7
- 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.map +1 -1
- package/dist/deleteOne.d.ts +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +2 -2
- 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 +1 -1
- 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 +4 -0
- 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 +2 -1
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/chainMethods.d.ts.map +1 -1
- package/dist/find/chainMethods.js.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 +23 -5
- 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.map +1 -1
- package/dist/findGlobal.d.ts +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- 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 +1 -1
- 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.map +1 -1
- package/dist/findVersions.d.ts +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +1 -1
- 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 +10 -1
- 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 +15 -20
- 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 +169 -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/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
- package/dist/queries/createJSONQuery/formatJSONPathSegment.js.map +1 -1
- package/dist/queries/createJSONQuery/index.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 +170 -99
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/operatorMap.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 +1 -2
- package/dist/queryDrafts.js.map +1 -1
- package/dist/schema/build.d.ts +9 -5
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +161 -112
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/createIndex.js.map +1 -1
- package/dist/schema/createTableName.d.ts +1 -1
- package/dist/schema/createTableName.d.ts.map +1 -1
- package/dist/schema/createTableName.js +1 -1
- package/dist/schema/createTableName.js.map +1 -1
- 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/parentIDColumnMap.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 +105 -42
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.d.ts +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.js +2 -2
- 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 +4 -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.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/numbers.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/selects.js.map +1 -1
- package/dist/transform/write/texts.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 +21 -4
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/transform/write/types.js.map +1 -1
- package/dist/types.d.ts +4 -3
- 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 +1 -2
- package/dist/update.js.map +1 -1
- package/dist/updateGlobal.d.ts +2 -3
- package/dist/updateGlobal.d.ts.map +1 -1
- 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 +1 -1
- 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 +1 -1
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertRow/deleteExistingArrayRows.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingArrayRows.js.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.js.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 +12 -8
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/types.d.ts +6 -1
- 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/appendPrefixToKeys.js.map +1 -1
- package/dist/utilities/createBlocksMap.d.ts.map +1 -1
- package/dist/utilities/createBlocksMap.js.map +1 -1
- package/dist/utilities/createMigrationTable.d.ts.map +1 -1
- package/dist/utilities/createMigrationTable.js.map +1 -1
- package/dist/utilities/createRelationshipMap.d.ts.map +1 -1
- package/dist/utilities/createRelationshipMap.js.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/isArrayOfRows.js.map +1 -1
- package/dist/utilities/migrationTableExists.d.ts.map +1 -1
- package/dist/utilities/migrationTableExists.js.map +1 -1
- package/dist/utilities/parseError.js.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 +20 -11
@@ -0,0 +1,211 @@
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
2
|
+
import fs from 'fs';
|
3
|
+
import { createRequire } from 'module';
|
4
|
+
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload';
|
5
|
+
import toSnakeCase from 'to-snake-case';
|
6
|
+
import { groupUpSQLStatements } from './groupUpSQLStatements.js';
|
7
|
+
import { migrateRelationships } from './migrateRelationships.js';
|
8
|
+
import { traverseFields } from './traverseFields.js';
|
9
|
+
const require = createRequire(import.meta.url);
|
10
|
+
/**
|
11
|
+
* Moves upload and relationship columns from the join table and into the tables while moving data
|
12
|
+
* This is done in the following order:
|
13
|
+
* ADD COLUMNs
|
14
|
+
* -- manipulate data to move relationships to new columns
|
15
|
+
* ADD CONSTRAINTs
|
16
|
+
* NOT NULLs
|
17
|
+
* DROP TABLEs
|
18
|
+
* DROP CONSTRAINTs
|
19
|
+
* DROP COLUMNs
|
20
|
+
* @param debug
|
21
|
+
* @param payload
|
22
|
+
* @param req
|
23
|
+
*/ export const migratePostgresV2toV3 = async ({ debug, payload, req })=>{
|
24
|
+
const adapter = payload.db;
|
25
|
+
const db = adapter.sessions[req.transactionID]?.db;
|
26
|
+
const dir = payload.db.migrationDir;
|
27
|
+
// get the drizzle migrateUpSQL from drizzle using the last schema
|
28
|
+
const { generateDrizzleJson, generateMigration } = require('drizzle-kit/payload');
|
29
|
+
const drizzleJsonAfter = generateDrizzleJson(adapter.schema);
|
30
|
+
// Get the previous migration snapshot
|
31
|
+
const previousSnapshot = fs.readdirSync(dir).filter((file)=>file.endsWith('.json') && !file.endsWith('relationships_v2_v3.json')).sort().reverse()?.[0];
|
32
|
+
if (!previousSnapshot) {
|
33
|
+
throw new Error(`No previous migration schema file found! A prior migration from v2 is required to migrate to v3.`);
|
34
|
+
}
|
35
|
+
const drizzleJsonBefore = JSON.parse(fs.readFileSync(`${dir}/${previousSnapshot}`, 'utf8'));
|
36
|
+
const generatedSQL = await generateMigration(drizzleJsonBefore, drizzleJsonAfter);
|
37
|
+
if (!generatedSQL.length) {
|
38
|
+
payload.logger.info(`No schema changes needed.`);
|
39
|
+
process.exit(0);
|
40
|
+
}
|
41
|
+
const sqlUpStatements = groupUpSQLStatements(generatedSQL);
|
42
|
+
const addColumnsStatement = sqlUpStatements.addColumn.join('\n');
|
43
|
+
if (debug) {
|
44
|
+
payload.logger.info('CREATING NEW RELATIONSHIP COLUMNS');
|
45
|
+
payload.logger.info(addColumnsStatement);
|
46
|
+
}
|
47
|
+
await db.execute(sql.raw(addColumnsStatement));
|
48
|
+
for (const collection of payload.config.collections){
|
49
|
+
const tableName = adapter.tableNameMap.get(toSnakeCase(collection.slug));
|
50
|
+
const pathsToQuery = new Set();
|
51
|
+
traverseFields({
|
52
|
+
adapter,
|
53
|
+
collectionSlug: collection.slug,
|
54
|
+
columnPrefix: '',
|
55
|
+
db,
|
56
|
+
disableNotNull: false,
|
57
|
+
fields: collection.fields,
|
58
|
+
isVersions: false,
|
59
|
+
newTableName: tableName,
|
60
|
+
parentTableName: tableName,
|
61
|
+
path: '',
|
62
|
+
pathsToQuery,
|
63
|
+
payload,
|
64
|
+
rootTableName: tableName
|
65
|
+
});
|
66
|
+
await migrateRelationships({
|
67
|
+
adapter,
|
68
|
+
collectionSlug: collection.slug,
|
69
|
+
db,
|
70
|
+
debug,
|
71
|
+
fields: collection.fields,
|
72
|
+
isVersions: false,
|
73
|
+
pathsToQuery,
|
74
|
+
payload,
|
75
|
+
req,
|
76
|
+
tableName
|
77
|
+
});
|
78
|
+
if (collection.versions) {
|
79
|
+
const versionsTableName = adapter.tableNameMap.get(`_${toSnakeCase(collection.slug)}${adapter.versionsSuffix}`);
|
80
|
+
const versionFields = buildVersionCollectionFields(collection);
|
81
|
+
const versionPathsToQuery = new Set();
|
82
|
+
traverseFields({
|
83
|
+
adapter,
|
84
|
+
collectionSlug: collection.slug,
|
85
|
+
columnPrefix: '',
|
86
|
+
db,
|
87
|
+
disableNotNull: true,
|
88
|
+
fields: versionFields,
|
89
|
+
isVersions: true,
|
90
|
+
newTableName: versionsTableName,
|
91
|
+
parentTableName: versionsTableName,
|
92
|
+
path: '',
|
93
|
+
pathsToQuery: versionPathsToQuery,
|
94
|
+
payload,
|
95
|
+
rootTableName: versionsTableName
|
96
|
+
});
|
97
|
+
await migrateRelationships({
|
98
|
+
adapter,
|
99
|
+
collectionSlug: collection.slug,
|
100
|
+
db,
|
101
|
+
debug,
|
102
|
+
fields: versionFields,
|
103
|
+
isVersions: true,
|
104
|
+
pathsToQuery: versionPathsToQuery,
|
105
|
+
payload,
|
106
|
+
req,
|
107
|
+
tableName: versionsTableName
|
108
|
+
});
|
109
|
+
}
|
110
|
+
}
|
111
|
+
for (const global of payload.config.globals){
|
112
|
+
const tableName = adapter.tableNameMap.get(toSnakeCase(global.slug));
|
113
|
+
const pathsToQuery = new Set();
|
114
|
+
traverseFields({
|
115
|
+
adapter,
|
116
|
+
columnPrefix: '',
|
117
|
+
db,
|
118
|
+
disableNotNull: false,
|
119
|
+
fields: global.fields,
|
120
|
+
globalSlug: global.slug,
|
121
|
+
isVersions: false,
|
122
|
+
newTableName: tableName,
|
123
|
+
parentTableName: tableName,
|
124
|
+
path: '',
|
125
|
+
pathsToQuery,
|
126
|
+
payload,
|
127
|
+
rootTableName: tableName
|
128
|
+
});
|
129
|
+
await migrateRelationships({
|
130
|
+
adapter,
|
131
|
+
db,
|
132
|
+
debug,
|
133
|
+
fields: global.fields,
|
134
|
+
globalSlug: global.slug,
|
135
|
+
isVersions: false,
|
136
|
+
pathsToQuery,
|
137
|
+
payload,
|
138
|
+
req,
|
139
|
+
tableName
|
140
|
+
});
|
141
|
+
if (global.versions) {
|
142
|
+
const versionsTableName = adapter.tableNameMap.get(`_${toSnakeCase(global.slug)}${adapter.versionsSuffix}`);
|
143
|
+
const versionFields = buildVersionGlobalFields(global);
|
144
|
+
const versionPathsToQuery = new Set();
|
145
|
+
traverseFields({
|
146
|
+
adapter,
|
147
|
+
columnPrefix: '',
|
148
|
+
db,
|
149
|
+
disableNotNull: true,
|
150
|
+
fields: versionFields,
|
151
|
+
globalSlug: global.slug,
|
152
|
+
isVersions: true,
|
153
|
+
newTableName: versionsTableName,
|
154
|
+
parentTableName: versionsTableName,
|
155
|
+
path: '',
|
156
|
+
pathsToQuery: versionPathsToQuery,
|
157
|
+
payload,
|
158
|
+
rootTableName: versionsTableName
|
159
|
+
});
|
160
|
+
await migrateRelationships({
|
161
|
+
adapter,
|
162
|
+
db,
|
163
|
+
debug,
|
164
|
+
fields: versionFields,
|
165
|
+
globalSlug: global.slug,
|
166
|
+
isVersions: true,
|
167
|
+
pathsToQuery: versionPathsToQuery,
|
168
|
+
payload,
|
169
|
+
req,
|
170
|
+
tableName: versionsTableName
|
171
|
+
});
|
172
|
+
}
|
173
|
+
}
|
174
|
+
// ADD CONSTRAINT
|
175
|
+
const addConstraintsStatement = sqlUpStatements.addConstraint.join('\n');
|
176
|
+
if (debug) {
|
177
|
+
payload.logger.info('ADDING CONSTRAINTS');
|
178
|
+
payload.logger.info(addConstraintsStatement);
|
179
|
+
}
|
180
|
+
await db.execute(sql.raw(addConstraintsStatement));
|
181
|
+
// NOT NULL
|
182
|
+
const notNullStatements = sqlUpStatements.notNull.join('\n');
|
183
|
+
if (debug) {
|
184
|
+
payload.logger.info('NOT NULL CONSTRAINTS');
|
185
|
+
payload.logger.info(notNullStatements);
|
186
|
+
}
|
187
|
+
await db.execute(sql.raw(notNullStatements));
|
188
|
+
// DROP TABLE
|
189
|
+
const dropTablesStatement = sqlUpStatements.dropTable.join('\n');
|
190
|
+
if (debug) {
|
191
|
+
payload.logger.info('DROPPING TABLES');
|
192
|
+
payload.logger.info(dropTablesStatement);
|
193
|
+
}
|
194
|
+
await db.execute(sql.raw(dropTablesStatement));
|
195
|
+
// DROP CONSTRAINT
|
196
|
+
const dropConstraintsStatement = sqlUpStatements.dropConstraint.join('\n');
|
197
|
+
if (debug) {
|
198
|
+
payload.logger.info('DROPPING CONSTRAINTS');
|
199
|
+
payload.logger.info(dropConstraintsStatement);
|
200
|
+
}
|
201
|
+
await db.execute(sql.raw(dropConstraintsStatement));
|
202
|
+
// DROP COLUMN
|
203
|
+
const dropColumnsStatement = sqlUpStatements.dropColumn.join('\n');
|
204
|
+
if (debug) {
|
205
|
+
payload.logger.info('DROPPING COLUMNS');
|
206
|
+
payload.logger.info(dropColumnsStatement);
|
207
|
+
}
|
208
|
+
await db.execute(sql.raw(dropColumnsStatement));
|
209
|
+
};
|
210
|
+
|
211
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/predefinedMigrations/v2-v3/index.ts"],"sourcesContent":["import type { DrizzleSnapshotJSON } from 'drizzle-kit/payload'\nimport type { Payload, PayloadRequestWithData } from 'payload'\n\nimport { sql } from 'drizzle-orm'\nimport fs from 'fs'\nimport { createRequire } from 'module'\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from '../../types.js'\nimport type { PathsToQuery } from './types.js'\n\nimport { groupUpSQLStatements } from './groupUpSQLStatements.js'\nimport { migrateRelationships } from './migrateRelationships.js'\nimport { traverseFields } from './traverseFields.js'\n\nconst require = createRequire(import.meta.url)\n\ntype Args = {\n debug?: boolean\n payload: Payload\n req?: Partial<PayloadRequestWithData>\n}\n\n/**\n * Moves upload and relationship columns from the join table and into the tables while moving data\n * This is done in the following order:\n * ADD COLUMNs\n * -- manipulate data to move relationships to new columns\n * ADD CONSTRAINTs\n * NOT NULLs\n * DROP TABLEs\n * DROP CONSTRAINTs\n * DROP COLUMNs\n * @param debug\n * @param payload\n * @param req\n */\nexport const migratePostgresV2toV3 = async ({ debug, payload, req }: Args) => {\n const adapter = payload.db as PostgresAdapter\n const db = adapter.sessions[req.transactionID]?.db\n const dir = payload.db.migrationDir\n\n // get the drizzle migrateUpSQL from drizzle using the last schema\n const { generateDrizzleJson, generateMigration } = require('drizzle-kit/payload')\n const drizzleJsonAfter = generateDrizzleJson(adapter.schema)\n\n // Get the previous migration snapshot\n const previousSnapshot = fs\n .readdirSync(dir)\n .filter((file) => file.endsWith('.json') && !file.endsWith('relationships_v2_v3.json'))\n .sort()\n .reverse()?.[0]\n\n if (!previousSnapshot) {\n throw new Error(\n `No previous migration schema file found! A prior migration from v2 is required to migrate to v3.`,\n )\n }\n\n const drizzleJsonBefore = JSON.parse(\n fs.readFileSync(`${dir}/${previousSnapshot}`, 'utf8'),\n ) as DrizzleSnapshotJSON\n\n const generatedSQL = await generateMigration(drizzleJsonBefore, drizzleJsonAfter)\n\n if (!generatedSQL.length) {\n payload.logger.info(`No schema changes needed.`)\n process.exit(0)\n }\n\n const sqlUpStatements = groupUpSQLStatements(generatedSQL)\n\n const addColumnsStatement = sqlUpStatements.addColumn.join('\\n')\n\n if (debug) {\n payload.logger.info('CREATING NEW RELATIONSHIP COLUMNS')\n payload.logger.info(addColumnsStatement)\n }\n\n await db.execute(sql.raw(addColumnsStatement))\n\n for (const collection of payload.config.collections) {\n const tableName = adapter.tableNameMap.get(toSnakeCase(collection.slug))\n const pathsToQuery: PathsToQuery = new Set()\n\n traverseFields({\n adapter,\n collectionSlug: collection.slug,\n columnPrefix: '',\n db,\n disableNotNull: false,\n fields: collection.fields,\n isVersions: false,\n newTableName: tableName,\n parentTableName: tableName,\n path: '',\n pathsToQuery,\n payload,\n rootTableName: tableName,\n })\n\n await migrateRelationships({\n adapter,\n collectionSlug: collection.slug,\n db,\n debug,\n fields: collection.fields,\n isVersions: false,\n pathsToQuery,\n payload,\n req,\n tableName,\n })\n\n if (collection.versions) {\n const versionsTableName = adapter.tableNameMap.get(\n `_${toSnakeCase(collection.slug)}${adapter.versionsSuffix}`,\n )\n const versionFields = buildVersionCollectionFields(collection)\n const versionPathsToQuery: PathsToQuery = new Set()\n\n traverseFields({\n adapter,\n collectionSlug: collection.slug,\n columnPrefix: '',\n db,\n disableNotNull: true,\n fields: versionFields,\n isVersions: true,\n newTableName: versionsTableName,\n parentTableName: versionsTableName,\n path: '',\n pathsToQuery: versionPathsToQuery,\n payload,\n rootTableName: versionsTableName,\n })\n\n await migrateRelationships({\n adapter,\n collectionSlug: collection.slug,\n db,\n debug,\n fields: versionFields,\n isVersions: true,\n pathsToQuery: versionPathsToQuery,\n payload,\n req,\n tableName: versionsTableName,\n })\n }\n }\n\n for (const global of payload.config.globals) {\n const tableName = adapter.tableNameMap.get(toSnakeCase(global.slug))\n\n const pathsToQuery: PathsToQuery = new Set()\n\n traverseFields({\n adapter,\n columnPrefix: '',\n db,\n disableNotNull: false,\n fields: global.fields,\n globalSlug: global.slug,\n isVersions: false,\n newTableName: tableName,\n parentTableName: tableName,\n path: '',\n pathsToQuery,\n payload,\n rootTableName: tableName,\n })\n\n await migrateRelationships({\n adapter,\n db,\n debug,\n fields: global.fields,\n globalSlug: global.slug,\n isVersions: false,\n pathsToQuery,\n payload,\n req,\n tableName,\n })\n\n if (global.versions) {\n const versionsTableName = adapter.tableNameMap.get(\n `_${toSnakeCase(global.slug)}${adapter.versionsSuffix}`,\n )\n\n const versionFields = buildVersionGlobalFields(global)\n\n const versionPathsToQuery: PathsToQuery = new Set()\n\n traverseFields({\n adapter,\n columnPrefix: '',\n db,\n disableNotNull: true,\n fields: versionFields,\n globalSlug: global.slug,\n isVersions: true,\n newTableName: versionsTableName,\n parentTableName: versionsTableName,\n path: '',\n pathsToQuery: versionPathsToQuery,\n payload,\n rootTableName: versionsTableName,\n })\n\n await migrateRelationships({\n adapter,\n db,\n debug,\n fields: versionFields,\n globalSlug: global.slug,\n isVersions: true,\n pathsToQuery: versionPathsToQuery,\n payload,\n req,\n tableName: versionsTableName,\n })\n }\n }\n\n // ADD CONSTRAINT\n const addConstraintsStatement = sqlUpStatements.addConstraint.join('\\n')\n\n if (debug) {\n payload.logger.info('ADDING CONSTRAINTS')\n payload.logger.info(addConstraintsStatement)\n }\n\n await db.execute(sql.raw(addConstraintsStatement))\n\n // NOT NULL\n const notNullStatements = sqlUpStatements.notNull.join('\\n')\n\n if (debug) {\n payload.logger.info('NOT NULL CONSTRAINTS')\n payload.logger.info(notNullStatements)\n }\n\n await db.execute(sql.raw(notNullStatements))\n\n // DROP TABLE\n const dropTablesStatement = sqlUpStatements.dropTable.join('\\n')\n\n if (debug) {\n payload.logger.info('DROPPING TABLES')\n payload.logger.info(dropTablesStatement)\n }\n\n await db.execute(sql.raw(dropTablesStatement))\n\n // DROP CONSTRAINT\n const dropConstraintsStatement = sqlUpStatements.dropConstraint.join('\\n')\n\n if (debug) {\n payload.logger.info('DROPPING CONSTRAINTS')\n payload.logger.info(dropConstraintsStatement)\n }\n\n await db.execute(sql.raw(dropConstraintsStatement))\n\n // DROP COLUMN\n const dropColumnsStatement = sqlUpStatements.dropColumn.join('\\n')\n\n if (debug) {\n payload.logger.info('DROPPING COLUMNS')\n payload.logger.info(dropColumnsStatement)\n }\n\n await db.execute(sql.raw(dropColumnsStatement))\n}\n"],"names":["sql","fs","createRequire","buildVersionCollectionFields","buildVersionGlobalFields","toSnakeCase","groupUpSQLStatements","migrateRelationships","traverseFields","require","url","migratePostgresV2toV3","debug","payload","req","adapter","db","sessions","transactionID","dir","migrationDir","generateDrizzleJson","generateMigration","drizzleJsonAfter","schema","previousSnapshot","readdirSync","filter","file","endsWith","sort","reverse","Error","drizzleJsonBefore","JSON","parse","readFileSync","generatedSQL","length","logger","info","process","exit","sqlUpStatements","addColumnsStatement","addColumn","join","execute","raw","collection","config","collections","tableName","tableNameMap","get","slug","pathsToQuery","Set","collectionSlug","columnPrefix","disableNotNull","fields","isVersions","newTableName","parentTableName","path","rootTableName","versions","versionsTableName","versionsSuffix","versionFields","versionPathsToQuery","global","globals","globalSlug","addConstraintsStatement","addConstraint","notNullStatements","notNull","dropTablesStatement","dropTable","dropConstraintsStatement","dropConstraint","dropColumnsStatement","dropColumn"],"mappings":"AAGA,SAASA,GAAG,QAAQ,cAAa;AACjC,OAAOC,QAAQ,KAAI;AACnB,SAASC,aAAa,QAAQ,SAAQ;AACtC,SAASC,4BAA4B,EAAEC,wBAAwB,QAAQ,UAAS;AAChF,OAAOC,iBAAiB,gBAAe;AAKvC,SAASC,oBAAoB,QAAQ,4BAA2B;AAChE,SAASC,oBAAoB,QAAQ,4BAA2B;AAChE,SAASC,cAAc,QAAQ,sBAAqB;AAEpD,MAAMC,UAAUP,cAAc,YAAYQ,GAAG;AAQ7C;;;;;;;;;;;;;CAaC,GACD,OAAO,MAAMC,wBAAwB,OAAO,EAAEC,KAAK,EAAEC,OAAO,EAAEC,GAAG,EAAQ;IACvE,MAAMC,UAAUF,QAAQG,EAAE;IAC1B,MAAMA,KAAKD,QAAQE,QAAQ,CAACH,IAAII,aAAa,CAAC,EAAEF;IAChD,MAAMG,MAAMN,QAAQG,EAAE,CAACI,YAAY;IAEnC,kEAAkE;IAClE,MAAM,EAAEC,mBAAmB,EAAEC,iBAAiB,EAAE,GAAGb,QAAQ;IAC3D,MAAMc,mBAAmBF,oBAAoBN,QAAQS,MAAM;IAE3D,sCAAsC;IACtC,MAAMC,mBAAmBxB,GACtByB,WAAW,CAACP,KACZQ,MAAM,CAAC,CAACC,OAASA,KAAKC,QAAQ,CAAC,YAAY,CAACD,KAAKC,QAAQ,CAAC,6BAC1DC,IAAI,GACJC,OAAO,IAAI,CAAC,EAAE;IAEjB,IAAI,CAACN,kBAAkB;QACrB,MAAM,IAAIO,MACR,CAAC,gGAAgG,CAAC;IAEtG;IAEA,MAAMC,oBAAoBC,KAAKC,KAAK,CAClClC,GAAGmC,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AACyC;IACtB;IAEA,MAAMjE,GAAG+B,OAAO,CAAC/C,IAAIgD,GAAG,CAACiC;IAEzB,cAAc;IACd,MAAME,uBAAuBxC,gBAAgByC,UAAU,CAACtC,IAAI,CAAC;IAE7D,IAAIlC,OAAO;QACTC,QAAQ0B,MAAM,CAACC,IAAI,CAAC;QACpB3B,QAAQ0B,MAAM,CAACC,IAAI,CAAC2C;IACtB;IAEA,MAAMnE,GAAG+B,OAAO,CAAC/C,IAAIgD,GAAG,CAACmC;AAC3B,EAAC"}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { Field, Payload, PayloadRequestWithData } from 'payload';
|
2
|
+
import type { DrizzleTransaction, PostgresAdapter } from '../../types.js';
|
3
|
+
import type { PathsToQuery } from './types.js';
|
4
|
+
type Args = {
|
5
|
+
adapter: PostgresAdapter;
|
6
|
+
collectionSlug?: string;
|
7
|
+
db: DrizzleTransaction;
|
8
|
+
debug: boolean;
|
9
|
+
fields: Field[];
|
10
|
+
globalSlug?: string;
|
11
|
+
isVersions: boolean;
|
12
|
+
pathsToQuery: PathsToQuery;
|
13
|
+
payload: Payload;
|
14
|
+
req?: Partial<PayloadRequestWithData>;
|
15
|
+
tableName: string;
|
16
|
+
};
|
17
|
+
export declare const migrateRelationships: ({ adapter, collectionSlug, db, debug, fields, globalSlug, isVersions, pathsToQuery, payload, req, tableName, }: Args) => Promise<void>;
|
18
|
+
export {};
|
19
|
+
//# sourceMappingURL=migrateRelationships.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"migrateRelationships.d.ts","sourceRoot":"","sources":["../../../src/predefinedMigrations/v2-v3/migrateRelationships.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAA;AAIrE,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AACzE,OAAO,KAAK,EAAgB,YAAY,EAAE,MAAM,YAAY,CAAA;AAI5D,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,EAAE,EAAE,kBAAkB,CAAA;IACtB,KAAK,EAAE,OAAO,CAAA;IACd,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,OAAO,CAAA;IACnB,YAAY,EAAE,YAAY,CAAA;IAC1B,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC,sBAAsB,CAAC,CAAA;IACrC,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,eAAO,MAAM,oBAAoB,mHAY9B,IAAI,kBAkEN,CAAA"}
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { sql } from 'drizzle-orm';
|
2
|
+
import { fetchAndResave } from './fetchAndResave/index.js';
|
3
|
+
export const migrateRelationships = async ({ adapter, collectionSlug, db, debug, fields, globalSlug, isVersions, pathsToQuery, payload, req, tableName })=>{
|
4
|
+
if (pathsToQuery.size === 0) return;
|
5
|
+
let offset = 0;
|
6
|
+
let paginationResult;
|
7
|
+
const where = Array.from(pathsToQuery).reduce((statement, path, i)=>{
|
8
|
+
return statement += `
|
9
|
+
"${tableName}${adapter.relationshipsSuffix}"."path" LIKE '${path}'${pathsToQuery.size !== i + 1 ? ' OR' : ''}
|
10
|
+
`;
|
11
|
+
}, '');
|
12
|
+
while(typeof paginationResult === 'undefined' || paginationResult.rows.length > 0){
|
13
|
+
const paginationStatement = `SELECT DISTINCT parent_id FROM ${tableName}${adapter.relationshipsSuffix} WHERE
|
14
|
+
${where} ORDER BY parent_id LIMIT 500 OFFSET ${offset * 500};
|
15
|
+
`;
|
16
|
+
paginationResult = await adapter.drizzle.execute(sql.raw(`${paginationStatement}`));
|
17
|
+
if (paginationResult.rows.length === 0) return;
|
18
|
+
offset += 1;
|
19
|
+
const statement = `SELECT * FROM ${tableName}${adapter.relationshipsSuffix} WHERE
|
20
|
+
(${where}) AND parent_id IN (${paginationResult.rows.map((row)=>row.parent_id).join(', ')});
|
21
|
+
`;
|
22
|
+
if (debug) {
|
23
|
+
payload.logger.info('FINDING ROWS TO MIGRATE');
|
24
|
+
payload.logger.info(statement);
|
25
|
+
}
|
26
|
+
const result = await adapter.drizzle.execute(sql.raw(`${statement}`));
|
27
|
+
const docsToResave = {};
|
28
|
+
result.rows.forEach((row)=>{
|
29
|
+
const parentID = row.parent_id;
|
30
|
+
if (typeof parentID === 'string' || typeof parentID === 'number') {
|
31
|
+
if (!docsToResave[parentID]) docsToResave[parentID] = [];
|
32
|
+
docsToResave[parentID].push(row);
|
33
|
+
}
|
34
|
+
});
|
35
|
+
await fetchAndResave({
|
36
|
+
adapter,
|
37
|
+
collectionSlug,
|
38
|
+
db,
|
39
|
+
debug,
|
40
|
+
docsToResave,
|
41
|
+
fields,
|
42
|
+
globalSlug,
|
43
|
+
isVersions,
|
44
|
+
payload,
|
45
|
+
req: req,
|
46
|
+
tableName
|
47
|
+
});
|
48
|
+
}
|
49
|
+
const deleteStatement = `DELETE FROM ${tableName}${adapter.relationshipsSuffix} WHERE ${where}`;
|
50
|
+
if (debug) {
|
51
|
+
payload.logger.info('DELETING ROWS');
|
52
|
+
payload.logger.info(deleteStatement);
|
53
|
+
}
|
54
|
+
await db.execute(sql.raw(`${deleteStatement}`));
|
55
|
+
};
|
56
|
+
|
57
|
+
//# sourceMappingURL=migrateRelationships.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/predefinedMigrations/v2-v3/migrateRelationships.ts"],"sourcesContent":["import type { Field, Payload, PayloadRequestWithData } from 'payload'\n\nimport { sql } from 'drizzle-orm'\n\nimport type { DrizzleTransaction, PostgresAdapter } from '../../types.js'\nimport type { DocsToResave, PathsToQuery } from './types.js'\n\nimport { fetchAndResave } from './fetchAndResave/index.js'\n\ntype Args = {\n adapter: PostgresAdapter\n collectionSlug?: string\n db: DrizzleTransaction\n debug: boolean\n fields: Field[]\n globalSlug?: string\n isVersions: boolean\n pathsToQuery: PathsToQuery\n payload: Payload\n req?: Partial<PayloadRequestWithData>\n tableName: string\n}\n\nexport const migrateRelationships = async ({\n adapter,\n collectionSlug,\n db,\n debug,\n fields,\n globalSlug,\n isVersions,\n pathsToQuery,\n payload,\n req,\n tableName,\n}: Args) => {\n if (pathsToQuery.size === 0) return\n\n let offset = 0\n\n let paginationResult\n\n const where = Array.from(pathsToQuery).reduce((statement, path, i) => {\n return (statement += `\n\"${tableName}${adapter.relationshipsSuffix}\".\"path\" LIKE '${path}'${pathsToQuery.size !== i + 1 ? ' OR' : ''}\n`)\n }, '')\n\n while (typeof paginationResult === 'undefined' || paginationResult.rows.length > 0) {\n const paginationStatement = `SELECT DISTINCT parent_id FROM ${tableName}${adapter.relationshipsSuffix} WHERE\n ${where} ORDER BY parent_id LIMIT 500 OFFSET ${offset * 500};\n `\n\n paginationResult = await adapter.drizzle.execute(sql.raw(`${paginationStatement}`))\n\n if (paginationResult.rows.length === 0) return\n\n offset += 1\n\n const statement = `SELECT * FROM ${tableName}${adapter.relationshipsSuffix} WHERE\n (${where}) AND parent_id IN (${paginationResult.rows.map((row) => row.parent_id).join(', ')});\n`\n if (debug) {\n payload.logger.info('FINDING ROWS TO MIGRATE')\n payload.logger.info(statement)\n }\n\n const result = await adapter.drizzle.execute(sql.raw(`${statement}`))\n\n const docsToResave: DocsToResave = {}\n\n result.rows.forEach((row) => {\n const parentID = row.parent_id\n\n if (typeof parentID === 'string' || typeof parentID === 'number') {\n if (!docsToResave[parentID]) docsToResave[parentID] = []\n docsToResave[parentID].push(row)\n }\n })\n\n await fetchAndResave({\n adapter,\n collectionSlug,\n db,\n debug,\n docsToResave,\n fields,\n globalSlug,\n isVersions,\n payload,\n req: req as unknown as PayloadRequestWithData,\n tableName,\n })\n }\n\n const deleteStatement = `DELETE FROM ${tableName}${adapter.relationshipsSuffix} WHERE ${where}`\n if (debug) {\n payload.logger.info('DELETING ROWS')\n payload.logger.info(deleteStatement)\n }\n await db.execute(sql.raw(`${deleteStatement}`))\n}\n"],"names":["sql","fetchAndResave","migrateRelationships","adapter","collectionSlug","db","debug","fields","globalSlug","isVersions","pathsToQuery","payload","req","tableName","size","offset","paginationResult","where","Array","from","reduce","statement","path","i","relationshipsSuffix","rows","length","paginationStatement","drizzle","execute","raw","map","row","parent_id","join","logger","info","result","docsToResave","forEach","parentID","push","deleteStatement"],"mappings":"AAEA,SAASA,GAAG,QAAQ,cAAa;AAKjC,SAASC,cAAc,QAAQ,4BAA2B;AAgB1D,OAAO,MAAMC,uBAAuB,OAAO,EACzCC,OAAO,EACPC,cAAc,EACdC,EAAE,EACFC,KAAK,EACLC,MAAM,EACNC,UAAU,EACVC,UAAU,EACVC,YAAY,EACZC,OAAO,EACPC,GAAG,EACHC,SAAS,EACJ;IACL,IAAIH,aAAaI,IAAI,KAAK,GAAG;IAE7B,IAAIC,SAAS;IAEb,IAAIC;IAEJ,MAAMC,QAAQC,MAAMC,IAAI,CAACT,cAAcU,MAAM,CAAC,CAACC,WAAWC,MAAMC;QAC9D,OAAQF,aAAa,CAAC;CACzB,EAAER,UAAU,EAAEV,QAAQqB,mBAAmB,CAAC,eAAe,EAAEF,KAAK,CAAC,EAAEZ,aAAaI,IAAI,KAAKS,IAAI,IAAI,QAAQ,GAAG;AAC7G,CAAC;IACC,GAAG;IAEH,MAAO,OAAOP,qBAAqB,eAAeA,iBAAiBS,IAAI,CAACC,MAAM,GAAG,EAAG;QAClF,MAAMC,sBAAsB,CAAC,+BAA+B,EAAEd,UAAU,EAAEV,QAAQqB,mBAAmB,CAAC;IACtG,EAAEP,MAAM,qCAAqC,EAAEF,SAAS,IAAI;EAC9D,CAAC;QAECC,mBAAmB,MAAMb,QAAQyB,OAAO,CAACC,OAAO,CAAC7B,IAAI8B,GAAG,CAAC,CAAC,EAAEH,oBAAoB,CAAC;QAEjF,IAAIX,iBAAiBS,IAAI,CAACC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izzleTransaction, PostgresAdapter } from '../../types.js';
|
3
|
+
import type { PathsToQuery } from './types.js';
|
4
|
+
type Args = {
|
5
|
+
adapter: PostgresAdapter;
|
6
|
+
collectionSlug?: string;
|
7
|
+
columnPrefix: string;
|
8
|
+
db: DrizzleTransaction;
|
9
|
+
disableNotNull: boolean;
|
10
|
+
fields: Field[];
|
11
|
+
globalSlug?: string;
|
12
|
+
isVersions: boolean;
|
13
|
+
newTableName: string;
|
14
|
+
parentTableName: string;
|
15
|
+
path: string;
|
16
|
+
pathsToQuery: PathsToQuery;
|
17
|
+
payload: Payload;
|
18
|
+
rootTableName: string;
|
19
|
+
};
|
20
|
+
export declare const traverseFields: (args: Args) => void;
|
21
|
+
export {};
|
22
|
+
//# sourceMappingURL=traverseFields.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../../../src/predefinedMigrations/v2-v3/traverseFields.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAK7C,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAA;AACzE,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAA;AAE9C,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,YAAY,EAAE,MAAM,CAAA;IACpB,EAAE,EAAE,kBAAkB,CAAA;IACtB,cAAc,EAAE,OAAO,CAAA;IACvB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,UAAU,EAAE,OAAO,CAAA;IACnB,YAAY,EAAE,MAAM,CAAA;IACpB,eAAe,EAAE,MAAM,CAAA;IACvB,IAAI,EAAE,MAAM,CAAA;IACZ,YAAY,EAAE,YAAY,CAAA;IAC1B,OAAO,EAAE,OAAO,CAAA;IAChB,aAAa,EAAE,MAAM,CAAA;CACtB,CAAA;AAED,eAAO,MAAM,cAAc,SAAU,IAAI,SA0FxC,CAAA"}
|
@@ -0,0 +1,85 @@
|
|
1
|
+
import { tabHasName } from 'payload/shared';
|
2
|
+
import toSnakeCase from 'to-snake-case';
|
3
|
+
export const traverseFields = (args)=>{
|
4
|
+
args.fields.forEach((field)=>{
|
5
|
+
switch(field.type){
|
6
|
+
case 'group':
|
7
|
+
{
|
8
|
+
let newTableName = `${args.newTableName}_${toSnakeCase(field.name)}`;
|
9
|
+
if (field.localized && args.payload.config.localization) {
|
10
|
+
newTableName += args.adapter.localesSuffix;
|
11
|
+
}
|
12
|
+
return traverseFields({
|
13
|
+
...args,
|
14
|
+
columnPrefix: `${args.columnPrefix}${toSnakeCase(field.name)}_`,
|
15
|
+
fields: field.fields,
|
16
|
+
newTableName,
|
17
|
+
path: `${args.path ? `${args.path}.` : ''}${field.name}`
|
18
|
+
});
|
19
|
+
}
|
20
|
+
case 'row':
|
21
|
+
case 'collapsible':
|
22
|
+
{
|
23
|
+
return traverseFields({
|
24
|
+
...args,
|
25
|
+
fields: field.fields
|
26
|
+
});
|
27
|
+
}
|
28
|
+
case 'array':
|
29
|
+
{
|
30
|
+
const newTableName = args.adapter.tableNameMap.get(`${args.newTableName}_${toSnakeCase(field.name)}`);
|
31
|
+
return traverseFields({
|
32
|
+
...args,
|
33
|
+
columnPrefix: '',
|
34
|
+
fields: field.fields,
|
35
|
+
newTableName,
|
36
|
+
parentTableName: newTableName,
|
37
|
+
path: `${args.path ? `${args.path}.` : ''}${field.name}.%`
|
38
|
+
});
|
39
|
+
}
|
40
|
+
case 'blocks':
|
41
|
+
{
|
42
|
+
return field.blocks.forEach((block)=>{
|
43
|
+
const newTableName = args.adapter.tableNameMap.get(`${args.rootTableName}_blocks_${toSnakeCase(block.slug)}`);
|
44
|
+
traverseFields({
|
45
|
+
...args,
|
46
|
+
columnPrefix: '',
|
47
|
+
fields: block.fields,
|
48
|
+
newTableName,
|
49
|
+
parentTableName: newTableName,
|
50
|
+
path: `${args.path ? `${args.path}.` : ''}${field.name}.%`
|
51
|
+
});
|
52
|
+
});
|
53
|
+
}
|
54
|
+
case 'tabs':
|
55
|
+
{
|
56
|
+
return field.tabs.forEach((tab)=>{
|
57
|
+
if (tabHasName(tab)) {
|
58
|
+
args.columnPrefix = `${args.columnPrefix}_${toSnakeCase(tab.name)}_`;
|
59
|
+
args.path = `${args.path ? `${args.path}.` : ''}${tab.name}`;
|
60
|
+
args.newTableName = `${args.newTableName}_${toSnakeCase(tab.name)}`;
|
61
|
+
if (tab.localized && args.payload.config.localization) {
|
62
|
+
args.newTableName += args.adapter.localesSuffix;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
traverseFields({
|
66
|
+
...args,
|
67
|
+
fields: tab.fields
|
68
|
+
});
|
69
|
+
});
|
70
|
+
}
|
71
|
+
case 'relationship':
|
72
|
+
case 'upload':
|
73
|
+
{
|
74
|
+
if (typeof field.relationTo === 'string') {
|
75
|
+
if (field.type === 'upload' || !field.hasMany) {
|
76
|
+
args.pathsToQuery.add(`${args.path ? `${args.path}.` : ''}${field.name}`);
|
77
|
+
}
|
78
|
+
}
|
79
|
+
return null;
|
80
|
+
}
|
81
|
+
}
|
82
|
+
});
|
83
|
+
};
|
84
|
+
|
85
|
+
//# sourceMappingURL=traverseFields.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/predefinedMigrations/v2-v3/traverseFields.ts"],"sourcesContent":["import type { Field, Payload } from 'payload'\n\nimport { tabHasName } from 'payload/shared'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleTransaction, PostgresAdapter } from '../../types.js'\nimport type { PathsToQuery } from './types.js'\n\ntype Args = {\n adapter: PostgresAdapter\n collectionSlug?: string\n columnPrefix: string\n db: DrizzleTransaction\n disableNotNull: boolean\n fields: Field[]\n globalSlug?: string\n isVersions: boolean\n newTableName: string\n parentTableName: string\n path: string\n pathsToQuery: PathsToQuery\n payload: Payload\n rootTableName: string\n}\n\nexport const traverseFields = (args: Args) => {\n args.fields.forEach((field) => {\n switch (field.type) {\n case 'group': {\n let newTableName = `${args.newTableName}_${toSnakeCase(field.name)}`\n\n if (field.localized && args.payload.config.localization) {\n newTableName += args.adapter.localesSuffix\n }\n\n return traverseFields({\n ...args,\n columnPrefix: `${args.columnPrefix}${toSnakeCase(field.name)}_`,\n fields: field.fields,\n newTableName,\n path: `${args.path ? `${args.path}.` : ''}${field.name}`,\n })\n }\n\n case 'row':\n case 'collapsible': {\n return traverseFields({\n ...args,\n fields: field.fields,\n })\n }\n\n case 'array': {\n const newTableName = args.adapter.tableNameMap.get(\n `${args.newTableName}_${toSnakeCase(field.name)}`,\n )\n\n return traverseFields({\n ...args,\n columnPrefix: '',\n fields: field.fields,\n newTableName,\n parentTableName: newTableName,\n path: `${args.path ? `${args.path}.` : ''}${field.name}.%`,\n })\n }\n\n case 'blocks': {\n return field.blocks.forEach((block) => {\n const newTableName = args.adapter.tableNameMap.get(\n `${args.rootTableName}_blocks_${toSnakeCase(block.slug)}`,\n )\n\n traverseFields({\n ...args,\n columnPrefix: '',\n fields: block.fields,\n newTableName,\n parentTableName: newTableName,\n path: `${args.path ? `${args.path}.` : ''}${field.name}.%`,\n })\n })\n }\n\n case 'tabs': {\n return field.tabs.forEach((tab) => {\n if (tabHasName(tab)) {\n args.columnPrefix = `${args.columnPrefix}_${toSnakeCase(tab.name)}_`\n args.path = `${args.path ? `${args.path}.` : ''}${tab.name}`\n args.newTableName = `${args.newTableName}_${toSnakeCase(tab.name)}`\n\n if (tab.localized && args.payload.config.localization) {\n args.newTableName += args.adapter.localesSuffix\n }\n }\n\n traverseFields({\n ...args,\n fields: tab.fields,\n })\n })\n }\n\n case 'relationship':\n case 'upload': {\n if (typeof field.relationTo === 'string') {\n if (field.type === 'upload' || !field.hasMany) {\n args.pathsToQuery.add(`${args.path ? `${args.path}.` : ''}${field.name}`)\n }\n }\n\n return null\n }\n }\n })\n}\n"],"names":["tabHasName","toSnakeCase","traverseFields","args","fields","forEach","field","type","newTableName","name","localized","payload","config","localization","adapter","localesSuffix","columnPrefix","path","tableNameMap","get","parentTableName","blocks","block","rootTableName","slug","tabs","tab","relationTo","hasMany","pathsToQuery","add"],"mappings":"AAEA,SAASA,UAAU,QAAQ,iBAAgB;AAC3C,OAAOC,iBAAiB,gBAAe;AAsBvC,OAAO,MAAMC,iBAAiB,CAACC;IAC7BA,KAAKC,MAAM,CAACC,OAAO,CAAC,CAACC;QACnB,OAAQA,MAAMC,IAAI;YAChB,KAAK;gBAAS;oBACZ,IAAIC,eAAe,CAAC,EAAEL,KAAKK,YAAY,CAAC,CAAC,EAAEP,YAAYK,MAAMG,IAAI,EAAE,CAAC;oBAEpE,IAAIH,MAAMI,SAAS,IAAIP,KAAKQ,OAAO,CAACC,MAAM,CAACC,YAAY,EAAE;wBACvDL,gBAAgBL,KAAKW,OAAO,CAACC,aAAa;oBAC5C;oBAEA,OAAOb,eAAe;wBACpB,GAAGC,IAAI;wBACPa,cAAc,CAAC,EAAEb,KAAKa,YAAY,CAAC,EAAEf,YAAYK,MAAMG,IAAI,EAAE,CAAC,CAAC;wBAC/DL,QAAQE,MAAMF,MAAM;wBACpBI;wBACAS,MAAM,CAAC,EAAEd,KAAKc,IAAI,GAAG,CAAC,EAAEd,KAAKc,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,EAAEX,MAAMG,IAAI,CAAC,CAAC;oBAC1D;gBACF;YAEA,KAAK;YACL,KAAK;gBAAe;oBAClB,OAAOP,eAAe;wBACpB,GAAGC,IAAI;wBACPC,QAAQE,MAAMF,MAAM;oBACtB;gBACF;YAEA,KAAK;gBAAS;oBACZ,MAAMI,eAAeL,KAAKW,OAAO,CAACI,YAAY,CAACC,GAAG,CAChD,CAAC,EAAEhB,KAAKK,YAAY,CAAC,CAAC,EAAEP,YAAYK,MAAMG,IAAI,EAAE,CAAC;oBAGnD,OAAOP,eAAe;wBACpB,GAAGC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,IAAIP,KAAKQ,OAAO,CAACC,MAAM,CAACC,YAAY,EAAE;gCACrDV,KAAKK,YAAY,IAAIL,KAAKW,OAAO,CAACC,aAAa;4BACjD;wBACF;wBAEAb,eAAe;4BACb,GAAGC,IAAI;4BACPC,QAAQsB,IAAItB,MAAM;wBACpB;oBACF;gBACF;YAEA,KAAK;YACL,KAAK;gBAAU;oBACb,IAAI,OAAOE,MAAMqB,UAAU,KAAK,UAAU;wBACxC,IAAIrB,MAAMC,IAAI,KAAK,YAAY,CAACD,MAAMsB,OAAO,EAAE;4BAC7CzB,KAAK0B,YAAY,CAACC,GAAG,CAAC,CAAC,EAAE3B,KAAKc,IAAI,GAAG,CAAC,EAAEd,KAAKc,IAAI,CAAC,CAAC,CAAC,GAAG,GAAG,EAAEX,MAAMG,IAAI,CAAC,CAAC;wBAC1E;oBACF;oBAEA,OAAO;gBACT;QACF;IACF;AACF,EAAC"}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/**
|
2
|
+
* Set of all paths which should be moved
|
3
|
+
* This will be built up into one WHERE query
|
4
|
+
*/
|
5
|
+
export type PathsToQuery = Set<string>;
|
6
|
+
export type DocsToResave = {
|
7
|
+
[id: number | string]: Record<string, unknown>[];
|
8
|
+
};
|
9
|
+
//# sourceMappingURL=types.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../../src/predefinedMigrations/v2-v3/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,MAAM,MAAM,YAAY,GAAG,GAAG,CAAC,MAAM,CAAC,CAAA;AAEtC,MAAM,MAAM,YAAY,GAAG;IACzB,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CACjD,CAAA"}
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../../src/predefinedMigrations/v2-v3/types.ts"],"sourcesContent":["/**\n * Set of all paths which should be moved\n * This will be built up into one WHERE query\n */\nexport type PathsToQuery = Set<string>\n\nexport type DocsToResave = {\n [id: number | string]: Record<string, unknown>[]\n}\n"],"names":[],"mappings":"AAAA;;;CAGC,GAGD,WAEC"}
|
@@ -1,14 +1,13 @@
|
|
1
1
|
import type { SQL } from 'drizzle-orm';
|
2
|
-
import type { Field, Where } from 'payload
|
2
|
+
import type { Field, Where } from 'payload';
|
3
3
|
import type { GenericColumn, PostgresAdapter } from '../types.js';
|
4
|
-
import type { BuildQueryJoinAliases
|
5
|
-
export declare function buildAndOrConditions({ adapter, fields,
|
4
|
+
import type { BuildQueryJoinAliases } from './buildQuery.js';
|
5
|
+
export declare function buildAndOrConditions({ adapter, fields, joins, locale, selectFields, tableName, where, }: {
|
6
6
|
adapter: PostgresAdapter;
|
7
7
|
collectionSlug?: string;
|
8
8
|
fields: Field[];
|
9
9
|
globalSlug?: string;
|
10
|
-
|
11
|
-
joins: BuildQueryJoins;
|
10
|
+
joins: BuildQueryJoinAliases;
|
12
11
|
locale?: string;
|
13
12
|
selectFields: Record<string, GenericColumn>;
|
14
13
|
tableName: string;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"buildAndOrConditions.d.ts","sourceRoot":"","sources":["../../src/queries/buildAndOrConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"buildAndOrConditions.d.ts","sourceRoot":"","sources":["../../src/queries/buildAndOrConditions.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE3C,OAAO,KAAK,EAAE,aAAa,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AACjE,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AAI5D,wBAAsB,oBAAoB,CAAC,EACzC,OAAO,EACP,MAAM,EACN,KAAK,EACL,MAAM,EACN,YAAY,EACZ,SAAS,EACT,KAAK,GACN,EAAE;IACD,OAAO,EAAE,eAAe,CAAA;IACxB,cAAc,CAAC,EAAE,MAAM,CAAA;IACvB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,EAAE,qBAAqB,CAAA;IAC5B,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IAC3C,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,EAAE,KAAK,EAAE,CAAA;CACf,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,CAwBjB"}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import { parseParams } from './parseParams.js';
|
2
|
-
export async function buildAndOrConditions({ adapter, fields,
|
2
|
+
export async function buildAndOrConditions({ adapter, fields, joins, locale, selectFields, tableName, where }) {
|
3
3
|
const completedConditions = [];
|
4
4
|
// Loop over all AND / OR operations and add them to the AND / OR query param
|
5
5
|
// Operations should come through as an array
|
@@ -11,7 +11,6 @@ export async function buildAndOrConditions({ adapter, fields, joinAliases, joins
|
|
11
11
|
const result = await parseParams({
|
12
12
|
adapter,
|
13
13
|
fields,
|
14
|
-
joinAliases,
|
15
14
|
joins,
|
16
15
|
locale,
|
17
16
|
selectFields,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/buildAndOrConditions.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { Field, Where } from 'payload
|
1
|
+
{"version":3,"sources":["../../src/queries/buildAndOrConditions.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { Field, Where } from 'payload'\n\nimport type { GenericColumn, PostgresAdapter } from '../types.js'\nimport type { BuildQueryJoinAliases } from './buildQuery.js'\n\nimport { parseParams } from './parseParams.js'\n\nexport async function buildAndOrConditions({\n adapter,\n fields,\n joins,\n locale,\n selectFields,\n tableName,\n where,\n}: {\n adapter: PostgresAdapter\n collectionSlug?: string\n fields: Field[]\n globalSlug?: string\n joins: BuildQueryJoinAliases\n locale?: string\n selectFields: Record<string, GenericColumn>\n tableName: string\n where: Where[]\n}): Promise<SQL[]> {\n const completedConditions = []\n // Loop over all AND / OR operations and add them to the AND / OR query param\n // Operations should come through as an array\n // eslint-disable-next-line no-restricted-syntax\n for (const condition of where) {\n // If the operation is properly formatted as an object\n if (typeof condition === 'object') {\n // eslint-disable-next-line no-await-in-loop\n const result = await parseParams({\n adapter,\n fields,\n joins,\n locale,\n selectFields,\n tableName,\n where: condition,\n })\n if (result && Object.keys(result).length > 0) {\n completedConditions.push(result)\n }\n }\n }\n return completedConditions\n}\n"],"names":["parseParams","buildAndOrConditions","adapter","fields","joins","locale","selectFields","tableName","where","completedConditions","condition","result","Object","keys","length","push"],"mappings":"AAMA,SAASA,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,eAAeC,qBAAqB,EACzCC,OAAO,EACPC,MAAM,EACNC,KAAK,EACLC,MAAM,EACNC,YAAY,EACZC,SAAS,EACTC,KAAK,EAWN;IACC,MAAMC,sBAAsB,EAAE;IAC9B,6EAA6E;IAC7E,6CAA6C;IAC7C,gDAAgD;IAChD,KAAK,MAAMC,aAAaF,MAAO;QAC7B,sDAAsD;QACtD,IAAI,OAAOE,cAAc,UAAU;YACjC,4CAA4C;YAC5C,MAAMC,SAAS,MAAMX,YAAY;gBAC/BE;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC;gBACAC,OAAOE;YACT;YACA,IAAIC,UAAUC,OAAOC,IAAI,CAACF,QAAQG,MAAM,GAAG,GAAG;gBAC5CL,oBAAoBM,IAAI,CAACJ;YAC3B;QACF;IACF;IACA,OAAOF;AACT"}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { SQL } from 'drizzle-orm';
|
2
2
|
import type { PgTableWithColumns } from 'drizzle-orm/pg-core';
|
3
|
-
import type { Field, Where } from 'payload
|
3
|
+
import type { Field, Where } from 'payload';
|
4
4
|
import { asc, desc } from 'drizzle-orm';
|
5
5
|
import type { GenericColumn, GenericTable, PostgresAdapter } from '../types.js';
|
6
6
|
export type BuildQueryJoins = Record<string, SQL>;
|
@@ -17,8 +17,7 @@ type BuildQueryArgs = {
|
|
17
17
|
where: Where;
|
18
18
|
};
|
19
19
|
type Result = {
|
20
|
-
|
21
|
-
joins: BuildQueryJoins;
|
20
|
+
joins: BuildQueryJoinAliases;
|
22
21
|
orderBy: {
|
23
22
|
column: GenericColumn;
|
24
23
|
order: typeof asc | typeof desc;
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"buildQuery.d.ts","sourceRoot":"","sources":["../../src/queries/buildQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAC7D,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"buildQuery.d.ts","sourceRoot":"","sources":["../../src/queries/buildQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,qBAAqB,CAAA;AAC7D,OAAO,KAAK,EAAE,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAE3C,OAAO,EAAE,GAAG,EAAE,IAAI,EAAE,MAAM,aAAa,CAAA;AAEvC,OAAO,KAAK,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAK/E,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;AAEjD,MAAM,MAAM,qBAAqB,GAAG;IAClC,SAAS,EAAE,GAAG,CAAA;IACd,KAAK,EAAE,YAAY,GAAG,kBAAkB,CAAC,GAAG,CAAC,CAAA;CAC9C,EAAE,CAAA;AAEH,KAAK,cAAc,GAAG;IACpB,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,SAAS,EAAE,MAAM,CAAA;IACjB,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,KAAK,EAAE,qBAAqB,CAAA;IAC5B,OAAO,EAAE;QACP,MAAM,EAAE,aAAa,CAAA;QACrB,KAAK,EAAE,OAAO,GAAG,GAAG,OAAO,IAAI,CAAA;KAChC,CAAA;IACD,YAAY,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IAC3C,KAAK,EAAE,GAAG,CAAA;CACX,CAAA;AACD,QAAA,MAAM,UAAU,wEAOb,cAAc,KAAG,OAAO,CAAC,MAAM,CA2EjC,CAAA;AAED,eAAe,UAAU,CAAA"}
|
@@ -5,8 +5,7 @@ const buildQuery = async function buildQuery({ adapter, fields, locale, sort, ta
|
|
5
5
|
const selectFields = {
|
6
6
|
id: adapter.tables[tableName].id
|
7
7
|
};
|
8
|
-
const joins =
|
9
|
-
const joinAliases = [];
|
8
|
+
const joins = [];
|
10
9
|
const orderBy = {
|
11
10
|
column: null,
|
12
11
|
order: null
|
@@ -25,7 +24,6 @@ const buildQuery = async function buildQuery({ adapter, fields, locale, sort, ta
|
|
25
24
|
adapter,
|
26
25
|
collectionPath: sortPath,
|
27
26
|
fields,
|
28
|
-
joinAliases,
|
29
27
|
joins,
|
30
28
|
locale,
|
31
29
|
pathSegments: sortPath.replace(/__/g, '.').split('.'),
|
@@ -55,7 +53,6 @@ const buildQuery = async function buildQuery({ adapter, fields, locale, sort, ta
|
|
55
53
|
where = await parseParams({
|
56
54
|
adapter,
|
57
55
|
fields,
|
58
|
-
joinAliases,
|
59
56
|
joins,
|
60
57
|
locale,
|
61
58
|
selectFields,
|
@@ -64,7 +61,6 @@ const buildQuery = async function buildQuery({ adapter, fields, locale, sort, ta
|
|
64
61
|
});
|
65
62
|
}
|
66
63
|
return {
|
67
|
-
joinAliases,
|
68
64
|
joins,
|
69
65
|
orderBy,
|
70
66
|
selectFields,
|