@payloadcms/db-mongodb 4.0.0-internal.e55ccef → 4.0.0-internal.ea6e8f8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +9 -2
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +33 -4
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts.map +1 -1
- package/dist/countGlobalVersions.js +1 -0
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +36 -4
- package/dist/countVersions.js.map +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +2 -1
- package/dist/create.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +23 -1
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +9 -2
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +17 -4
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteBranchGlobal.d.ts +10 -0
- package/dist/deleteBranchGlobal.d.ts.map +1 -0
- package/dist/deleteBranchGlobal.js +22 -0
- package/dist/deleteBranchGlobal.js.map +1 -0
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +1 -0
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +20 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +1 -0
- package/dist/deleteVersions.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +2 -0
- package/dist/exports/migration-utils.d.ts.map +1 -1
- package/dist/exports/migration-utils.js +2 -0
- package/dist/exports/migration-utils.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +44 -8
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +115 -12
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +34 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +10 -2
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +43 -5
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +49 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +89 -67
- package/dist/init.js.map +1 -1
- package/dist/migrateFresh.d.ts +4 -2
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +21 -6
- package/dist/migrateFresh.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +8 -2
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +4 -2
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +5 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +34 -3
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +4 -0
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +94 -36
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/models/schemaBuildContext.d.ts +21 -0
- package/dist/models/schemaBuildContext.d.ts.map +1 -0
- package/dist/models/schemaBuildContext.js +29 -0
- package/dist/models/schemaBuildContext.js.map +1 -0
- package/dist/predefinedMigrations/branching.d.ts +4 -0
- package/dist/predefinedMigrations/branching.d.ts.map +1 -0
- package/dist/predefinedMigrations/branching.js +9 -0
- package/dist/predefinedMigrations/branching.js.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts +15 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.js +88 -0
- package/dist/predefinedMigrations/migrateBranching.js.map +1 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +4 -4
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/predefinedMigrations/migrateSizesToVariants.d.ts +13 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.js +368 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.js.map +1 -0
- package/dist/predefinedMigrations/sizes-to-variants.d.ts +5 -0
- package/dist/predefinedMigrations/sizes-to-variants.d.ts.map +1 -0
- package/dist/predefinedMigrations/sizes-to-variants.js +8 -0
- package/dist/predefinedMigrations/sizes-to-variants.js.map +1 -0
- package/dist/queries/buildAndOrConditions.d.ts +4 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +3 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildBranchVisibility.d.ts +11 -0
- package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
- package/dist/queries/buildBranchVisibility.js +204 -0
- package/dist/queries/buildBranchVisibility.js.map +1 -0
- package/dist/queries/buildQuery.d.ts +4 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +3 -1
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +4 -2
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +338 -24
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/getBuildQueryPlugin.d.ts +3 -1
- package/dist/queries/getBuildQueryPlugin.d.ts.map +1 -1
- package/dist/queries/getBuildQueryPlugin.js +3 -1
- package/dist/queries/getBuildQueryPlugin.js.map +1 -1
- package/dist/queries/parseParams.d.ts +4 -2
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +5 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +38 -7
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +55 -7
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js +10 -9
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +89 -9
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +3 -2
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +71 -14
- package/dist/updateJobs.js.map +1 -1
- package/dist/updateMany.d.ts.map +1 -1
- package/dist/updateMany.js +3 -2
- package/dist/updateMany.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +30 -4
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +3 -2
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertBranchGlobalChange.d.ts +3 -0
- package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
- package/dist/upsertBranchGlobalChange.js +28 -0
- package/dist/upsertBranchGlobalChange.js.map +1 -0
- package/dist/utilities/aggregatePaginate.d.ts +2 -1
- package/dist/utilities/aggregatePaginate.d.ts.map +1 -1
- package/dist/utilities/aggregatePaginate.js +21 -5
- package/dist/utilities/aggregatePaginate.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +4 -2
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +107 -8
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/resolveJoins.d.ts +6 -2
- package/dist/utilities/resolveJoins.d.ts.map +1 -1
- package/dist/utilities/resolveJoins.js +114 -26
- package/dist/utilities/resolveJoins.js.map +1 -1
- package/dist/utilities/transform.d.ts.map +1 -1
- package/dist/utilities/transform.js +15 -0
- package/dist/utilities/transform.js.map +1 -1
- package/package.json +6 -6
- package/dist/queries/buildSortParam.spec.js +0 -123
- package/dist/queries/buildSortParam.spec.js.map +0 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +0 -215
- package/dist/queries/getLocalizedSortProperty.spec.js.map +0 -1
- package/dist/utilities/transform.spec.js +0 -479
- package/dist/utilities/transform.spec.js.map +0 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ClientSession } from 'mongoose';
|
|
2
|
+
import type { Payload } from 'payload';
|
|
3
|
+
type BranchingMigrationArgs = {
|
|
4
|
+
payload: Payload;
|
|
5
|
+
session?: ClientSession;
|
|
6
|
+
};
|
|
7
|
+
/**
|
|
8
|
+
* Backfills branch identity before replacing legacy indexes with the current schema indexes.
|
|
9
|
+
*
|
|
10
|
+
* Run this generated migration after enabling branching. Backfill errors stop the migration
|
|
11
|
+
* before any indexes are removed, so uniqueness conflicts remain visible and recoverable.
|
|
12
|
+
*/
|
|
13
|
+
export declare const migrateBranching: ({ payload, session, }: BranchingMigrationArgs) => Promise<void>;
|
|
14
|
+
export {};
|
|
15
|
+
//# sourceMappingURL=migrateBranching.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrateBranching.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateBranching.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAC7C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAA;AAOtC,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,OAAO,CAAA;IAChB,OAAO,CAAC,EAAE,aAAa,CAAA;CACxB,CAAA;AAQD;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,0BAG1B,sBAAsB,KAAG,OAAO,CAAC,IAAI,CAqEvC,CAAA"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import { branchField, MAIN_BRANCH } from 'payload';
|
|
2
|
+
const legacyMainFilter = {
|
|
3
|
+
$or: [
|
|
4
|
+
{
|
|
5
|
+
[branchField]: {
|
|
6
|
+
$exists: false
|
|
7
|
+
}
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
[branchField]: null
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Backfills branch identity before replacing legacy indexes with the current schema indexes.
|
|
16
|
+
*
|
|
17
|
+
* Run this generated migration after enabling branching. Backfill errors stop the migration
|
|
18
|
+
* before any indexes are removed, so uniqueness conflicts remain visible and recoverable.
|
|
19
|
+
*/ export const migrateBranching = async ({ payload, session })=>{
|
|
20
|
+
const branching = payload.config.branching;
|
|
21
|
+
if (!branching?.enabled) {
|
|
22
|
+
payload.logger.info({
|
|
23
|
+
msg: 'Branching is not enabled, skipping branching migration'
|
|
24
|
+
});
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const adapter = payload.db;
|
|
28
|
+
const modelsToSync = new Set();
|
|
29
|
+
const updateOptions = session ? {
|
|
30
|
+
session
|
|
31
|
+
} : {};
|
|
32
|
+
payload.logger.info({
|
|
33
|
+
msg: 'Starting branching data and index migration'
|
|
34
|
+
});
|
|
35
|
+
for (const collectionSlug of branching.branchableCollections){
|
|
36
|
+
const collectionModel = adapter.collections[collectionSlug];
|
|
37
|
+
const versionModel = adapter.versions[collectionSlug];
|
|
38
|
+
if (!collectionModel) {
|
|
39
|
+
throw new Error(`Could not find MongoDB model for branchable collection "${collectionSlug}".`);
|
|
40
|
+
}
|
|
41
|
+
await collectionModel.collection.updateMany(legacyMainFilter, {
|
|
42
|
+
$set: {
|
|
43
|
+
[branchField]: MAIN_BRANCH
|
|
44
|
+
}
|
|
45
|
+
}, updateOptions);
|
|
46
|
+
modelsToSync.add(collectionModel);
|
|
47
|
+
if (versionModel) {
|
|
48
|
+
await versionModel.collection.updateMany(legacyMainFilter, {
|
|
49
|
+
$set: {
|
|
50
|
+
[branchField]: MAIN_BRANCH
|
|
51
|
+
}
|
|
52
|
+
}, updateOptions);
|
|
53
|
+
modelsToSync.add(versionModel);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
if (branching.branchableGlobals.size) {
|
|
57
|
+
await adapter.globals.collection.updateMany({
|
|
58
|
+
...legacyMainFilter,
|
|
59
|
+
globalType: {
|
|
60
|
+
$in: Array.from(branching.branchableGlobals)
|
|
61
|
+
}
|
|
62
|
+
}, {
|
|
63
|
+
$set: {
|
|
64
|
+
[branchField]: MAIN_BRANCH
|
|
65
|
+
}
|
|
66
|
+
}, updateOptions);
|
|
67
|
+
modelsToSync.add(adapter.globals);
|
|
68
|
+
for (const globalSlug of branching.branchableGlobals){
|
|
69
|
+
const versionModel = adapter.versions[globalSlug];
|
|
70
|
+
if (versionModel) {
|
|
71
|
+
await versionModel.collection.updateMany(legacyMainFilter, {
|
|
72
|
+
$set: {
|
|
73
|
+
[branchField]: MAIN_BRANCH
|
|
74
|
+
}
|
|
75
|
+
}, updateOptions);
|
|
76
|
+
modelsToSync.add(versionModel);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
for (const model of modelsToSync){
|
|
81
|
+
await model.syncIndexes();
|
|
82
|
+
}
|
|
83
|
+
payload.logger.info({
|
|
84
|
+
msg: 'Branching data and index migration completed successfully'
|
|
85
|
+
});
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
//# sourceMappingURL=migrateBranching.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateBranching.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\nimport type { Payload } from 'payload'\n\nimport { branchField, MAIN_BRANCH } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\nimport type { CollectionModel, GlobalModel } from '../types.js'\n\ntype BranchingMigrationArgs = {\n payload: Payload\n session?: ClientSession\n}\n\ntype SyncableModel = CollectionModel | GlobalModel\n\nconst legacyMainFilter = {\n $or: [{ [branchField]: { $exists: false } }, { [branchField]: null }],\n}\n\n/**\n * Backfills branch identity before replacing legacy indexes with the current schema indexes.\n *\n * Run this generated migration after enabling branching. Backfill errors stop the migration\n * before any indexes are removed, so uniqueness conflicts remain visible and recoverable.\n */\nexport const migrateBranching = async ({\n payload,\n session,\n}: BranchingMigrationArgs): Promise<void> => {\n const branching = payload.config.branching\n\n if (!branching?.enabled) {\n payload.logger.info({ msg: 'Branching is not enabled, skipping branching migration' })\n return\n }\n\n const adapter = payload.db as MongooseAdapter\n const modelsToSync = new Set<SyncableModel>()\n const updateOptions = session ? { session } : {}\n\n payload.logger.info({ msg: 'Starting branching data and index migration' })\n\n for (const collectionSlug of branching.branchableCollections) {\n const collectionModel = adapter.collections[collectionSlug]\n const versionModel = adapter.versions[collectionSlug]\n\n if (!collectionModel) {\n throw new Error(`Could not find MongoDB model for branchable collection \"${collectionSlug}\".`)\n }\n\n await collectionModel.collection.updateMany(\n legacyMainFilter,\n { $set: { [branchField]: MAIN_BRANCH } },\n updateOptions,\n )\n modelsToSync.add(collectionModel)\n\n if (versionModel) {\n await versionModel.collection.updateMany(\n legacyMainFilter,\n { $set: { [branchField]: MAIN_BRANCH } },\n updateOptions,\n )\n modelsToSync.add(versionModel)\n }\n }\n\n if (branching.branchableGlobals.size) {\n await adapter.globals.collection.updateMany(\n {\n ...legacyMainFilter,\n globalType: { $in: Array.from(branching.branchableGlobals) },\n },\n { $set: { [branchField]: MAIN_BRANCH } },\n updateOptions,\n )\n modelsToSync.add(adapter.globals)\n\n for (const globalSlug of branching.branchableGlobals) {\n const versionModel = adapter.versions[globalSlug]\n\n if (versionModel) {\n await versionModel.collection.updateMany(\n legacyMainFilter,\n { $set: { [branchField]: MAIN_BRANCH } },\n updateOptions,\n )\n modelsToSync.add(versionModel)\n }\n }\n }\n\n for (const model of modelsToSync) {\n await model.syncIndexes()\n }\n\n payload.logger.info({ msg: 'Branching data and index migration completed successfully' })\n}\n"],"names":["branchField","MAIN_BRANCH","legacyMainFilter","$or","$exists","migrateBranching","payload","session","branching","config","enabled","logger","info","msg","adapter","db","modelsToSync","Set","updateOptions","collectionSlug","branchableCollections","collectionModel","collections","versionModel","versions","Error","collection","updateMany","$set","add","branchableGlobals","size","globals","globalType","$in","Array","from","globalSlug","model","syncIndexes"],"mappings":"AAGA,SAASA,WAAW,EAAEC,WAAW,QAAQ,UAAS;AAYlD,MAAMC,mBAAmB;IACvBC,KAAK;QAAC;YAAE,CAACH,YAAY,EAAE;gBAAEI,SAAS;YAAM;QAAE;QAAG;YAAE,CAACJ,YAAY,EAAE;QAAK;KAAE;AACvE;AAEA;;;;;CAKC,GACD,OAAO,MAAMK,mBAAmB,OAAO,EACrCC,OAAO,EACPC,OAAO,EACgB;IACvB,MAAMC,YAAYF,QAAQG,MAAM,CAACD,SAAS;IAE1C,IAAI,CAACA,WAAWE,SAAS;QACvBJ,QAAQK,MAAM,CAACC,IAAI,CAAC;YAAEC,KAAK;QAAyD;QACpF;IACF;IAEA,MAAMC,UAAUR,QAAQS,EAAE;IAC1B,MAAMC,eAAe,IAAIC;IACzB,MAAMC,gBAAgBX,UAAU;QAAEA;IAAQ,IAAI,CAAC;IAE/CD,QAAQK,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK;IAA8C;IAEzE,KAAK,MAAMM,kBAAkBX,UAAUY,qBAAqB,CAAE;QAC5D,MAAMC,kBAAkBP,QAAQQ,WAA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-1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAE,MAAM,SAAS,CAAA;AA4GpD,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CAqGhB"}
|
|
@@ -28,8 +28,9 @@ const migrateModelWithBatching = async ({ batchSize, db, fields, Model, parentIs
|
|
|
28
28
|
parentIsLocalized
|
|
29
29
|
});
|
|
30
30
|
}
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
// Writing through the driver collection skips Mongoose timestamps entirely,
|
|
32
|
+
// which is what we want here - timestamps are added by the write transform above
|
|
33
|
+
await Model.collection.bulkWrite(docs.map((doc)=>({
|
|
33
34
|
updateOne: {
|
|
34
35
|
filter: {
|
|
35
36
|
_id: doc._id
|
|
@@ -39,8 +40,7 @@ const migrateModelWithBatching = async ({ batchSize, db, fields, Model, parentIs
|
|
|
39
40
|
}
|
|
40
41
|
}
|
|
41
42
|
})), {
|
|
42
|
-
session
|
|
43
|
-
timestamps: false
|
|
43
|
+
session
|
|
44
44
|
});
|
|
45
45
|
skip += batchSize;
|
|
46
46
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getCollection, getGlobal } from '../utilities/getEntity.js'\nimport { getSession } from '../utilities/getSession.js'\nimport { transform } from '../utilities/transform.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n db,\n fields,\n Model,\n parentIsLocalized,\n session,\n}: {\n batchSize: number\n db: MongooseAdapter\n fields: Field[]\n Model: Model<any>\n parentIsLocalized: boolean\n session?: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n transform({ adapter: db, data: doc, fields, operation: 'write', parentIsLocalized })\n }\n\n
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getCollection, getGlobal } from '../utilities/getEntity.js'\nimport { getSession } from '../utilities/getSession.js'\nimport { transform } from '../utilities/transform.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n db,\n fields,\n Model,\n parentIsLocalized,\n session,\n}: {\n batchSize: number\n db: MongooseAdapter\n fields: Field[]\n Model: Model<any>\n parentIsLocalized: boolean\n session?: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n transform({ adapter: db, data: doc, fields, operation: 'write', parentIsLocalized })\n }\n\n // Writing through the driver collection skips Mongoose timestamps entirely,\n // which is what we want here - timestamps are added by the write transform above\n await Model.collection.bulkWrite(\n docs.map((doc) => ({\n updateOne: {\n filter: { _id: doc._id },\n update: {\n $set: doc,\n },\n },\n })),\n { session },\n )\n\n skip += batchSize\n }\n}\n\nconst hasRelationshipOrUploadField = ({ fields }: { fields: Field[] }): boolean => {\n for (const field of fields) {\n if (field.type === 'relationship' || field.type === 'upload') {\n return true\n }\n\n if ('fields' in field) {\n if (hasRelationshipOrUploadField({ fields: field.fields })) {\n return true\n }\n }\n\n if ('blocks' in field) {\n for (const block of field.blocks) {\n if (typeof block === 'string') {\n // Skip - string blocks have been added in v3 and thus don't need to be migrated\n continue\n }\n if (hasRelationshipOrUploadField({ fields: block.fields })) {\n return true\n }\n }\n }\n\n if ('tabs' in field) {\n for (const tab of field.tabs) {\n if (hasRelationshipOrUploadField({ fields: tab.fields })) {\n return true\n }\n }\n }\n }\n\n return false\n}\n\nexport async function migrateRelationshipsV2_V3({\n batchSize,\n req,\n}: {\n batchSize: number\n req: PayloadRequest\n}): Promise<void> {\n const { payload } = req\n const db = payload.db as MongooseAdapter\n const config = payload.config\n\n const session = await getSession(db, req)\n\n for (const collection of payload.config.collections) {\n if (hasRelationshipOrUploadField(collection)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n const { Model } = getCollection({ adapter: db, collectionSlug: collection.slug })\n\n await migrateModelWithBatching({\n batchSize,\n db,\n fields: collection.fields,\n Model,\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n }\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n const { Model } = getCollection({\n adapter: db,\n collectionSlug: collection.slug,\n versions: true,\n })\n\n await migrateModelWithBatching({\n batchSize,\n db,\n fields: buildVersionCollectionFields(config, collection),\n Model,\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated collection versions \"${collection.slug}\"`)\n }\n }\n\n const { globals: GlobalsModel } = db\n\n for (const global of payload.config.globals) {\n if (hasRelationshipOrUploadField(global)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = (await GlobalsModel.findOne(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )) as null | Record<string, unknown>\n\n // in case if the global doesn't exist in the database yet (not saved)\n if (doc) {\n transform({\n adapter: db,\n data: doc,\n fields: global.fields,\n operation: 'write',\n })\n\n await GlobalsModel.collection.updateOne(\n {\n globalType: global.slug,\n },\n { $set: doc },\n { session },\n )\n }\n\n payload.logger.info(`Migrated global \"${global.slug}\"`)\n }\n\n if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n const { Model } = getGlobal({ adapter: db, globalSlug: global.slug, versions: true })\n\n await migrateModelWithBatching({\n batchSize,\n db,\n fields: buildVersionGlobalFields(config, global),\n Model,\n parentIsLocalized: false,\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","getCollection","getGlobal","getSession","transform","migrateModelWithBatching","batchSize","db","fields","Model","parentIsLocalized","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","adapter","data","operation","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","config","collections","logger","info","slug","collectionSlug","versions","globals","GlobalsModel","global","findOne","globalType","$eq","globalSlug"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,wBAAwB,QAAQ,UAAS;AAIhF,SAASC,aAAa,EAAEC,SAAS,QAAQ,4BAA2B;AACpE,SAASC,UAAU,QAAQ,6BAA4B;AACvD,SAASC,SAAS,QAAQ,4BAA2B;AAErD,MAAMC,2BAA2B,OAAO,EACtCC,SAAS,EACTC,EAAE,EACFC,MAAM,EACNC,KAAK,EACLC,iBAAiB,EAC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ACC,IAAI,CAAC,CAAC,0BAA0B,EAAEM,OAAOL,IAAI,CAAC,CAAC,CAAC;QACjE;IACF;AACF"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Payload, PayloadRequest } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Moves every upload collection's stored image sizes between the legacy `sizes` field and
|
|
4
|
+
* `variants`, on each collection and its versions collection (`version.sizes`). Uses `$rename`,
|
|
5
|
+
* so no document is rewritten beyond the renamed key, and re-keys the indexes on those fields.
|
|
6
|
+
* Documents that are already renamed are left as they are, so it's safe to re-run.
|
|
7
|
+
*/
|
|
8
|
+
export declare function migrateSizesToVariants({ direction, payload, req, }: {
|
|
9
|
+
direction?: 'down' | 'up';
|
|
10
|
+
payload: Payload;
|
|
11
|
+
req?: Partial<PayloadRequest>;
|
|
12
|
+
}): Promise<void>;
|
|
13
|
+
//# sourceMappingURL=migrateSizesToVariants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrateSizesToVariants.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateSizesToVariants.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAqBtD;;;;;GAKG;AACH,wBAAsB,sBAAsB,CAAC,EAC3C,SAAgB,EAChB,OAAO,EACP,GAAG,GACJ,EAAE;IACD,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAA;IACzB,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,CAAC,EAAE,OAAO,CAAC,cAAc,CAAC,CAAA;CAC9B,GAAG,OAAO,CAAC,IAAI,CAAC,CA2EhB"}
|
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
import { isDeepStrictEqual } from 'node:util';
|
|
2
|
+
import { getSession } from '../utilities/getSession.js';
|
|
3
|
+
/** Mongo error codes for an index that conflicts with an existing index. */ const INDEX_CONFLICT_CODES = new Set([
|
|
4
|
+
85,
|
|
5
|
+
86
|
|
6
|
+
]);
|
|
7
|
+
/** Mongo error code for a collection that hasn't been created yet. */ const NAMESPACE_NOT_FOUND_CODE = 26;
|
|
8
|
+
const GENERATED_VARIANT_METADATA_FIELDS = new Set([
|
|
9
|
+
'filename',
|
|
10
|
+
'filesize',
|
|
11
|
+
'height',
|
|
12
|
+
'mimeType',
|
|
13
|
+
'url',
|
|
14
|
+
'width'
|
|
15
|
+
]);
|
|
16
|
+
/**
|
|
17
|
+
* Moves every upload collection's stored image sizes between the legacy `sizes` field and
|
|
18
|
+
* `variants`, on each collection and its versions collection (`version.sizes`). Uses `$rename`,
|
|
19
|
+
* so no document is rewritten beyond the renamed key, and re-keys the indexes on those fields.
|
|
20
|
+
* Documents that are already renamed are left as they are, so it's safe to re-run.
|
|
21
|
+
*/ export async function migrateSizesToVariants({ direction = 'up', payload, req }) {
|
|
22
|
+
const adapter = payload.db;
|
|
23
|
+
const session = await getSession(adapter, req);
|
|
24
|
+
const [from, to] = direction === 'up' ? [
|
|
25
|
+
'sizes',
|
|
26
|
+
'variants'
|
|
27
|
+
] : [
|
|
28
|
+
'variants',
|
|
29
|
+
'sizes'
|
|
30
|
+
];
|
|
31
|
+
const targets = [];
|
|
32
|
+
for (const collection of payload.config.collections){
|
|
33
|
+
if (!collection.upload) {
|
|
34
|
+
continue;
|
|
35
|
+
}
|
|
36
|
+
const upload = typeof collection.upload === 'object' ? collection.upload : undefined;
|
|
37
|
+
const expectedVariantNames = (upload?.variants ?? []).map(({ name })=>name);
|
|
38
|
+
targets.push({
|
|
39
|
+
collection: adapter.collections[collection.slug].collection,
|
|
40
|
+
expectedVariantNames,
|
|
41
|
+
fromPath: from,
|
|
42
|
+
toPath: to
|
|
43
|
+
});
|
|
44
|
+
if (collection.versions && adapter.versions[collection.slug]) {
|
|
45
|
+
targets.push({
|
|
46
|
+
collection: adapter.versions[collection.slug].collection,
|
|
47
|
+
expectedVariantNames,
|
|
48
|
+
fromPath: `version.${from}`,
|
|
49
|
+
toPath: `version.${to}`
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
for (const { collection, expectedVariantNames, fromPath, toPath } of targets){
|
|
54
|
+
const [sourceData, directCollision] = await Promise.all([
|
|
55
|
+
collection.findOne({
|
|
56
|
+
[fromPath]: {
|
|
57
|
+
$exists: true
|
|
58
|
+
}
|
|
59
|
+
}, {
|
|
60
|
+
projection: {
|
|
61
|
+
_id: 1
|
|
62
|
+
}
|
|
63
|
+
}),
|
|
64
|
+
collection.findOne({
|
|
65
|
+
[fromPath]: {
|
|
66
|
+
$exists: true
|
|
67
|
+
},
|
|
68
|
+
[toPath]: {
|
|
69
|
+
$exists: true
|
|
70
|
+
}
|
|
71
|
+
}, {
|
|
72
|
+
projection: {
|
|
73
|
+
_id: 1
|
|
74
|
+
}
|
|
75
|
+
})
|
|
76
|
+
]);
|
|
77
|
+
if (sourceData && (directCollision || await findUnexpectedDestinationData({
|
|
78
|
+
collection,
|
|
79
|
+
expectedVariantNames,
|
|
80
|
+
toPath
|
|
81
|
+
}))) {
|
|
82
|
+
throw new Error(`Cannot run the sizes-to-variants migration because collection "${collection.collectionName}" contains both "${fromPath}" and "${toPath}" data. Move or rename the existing destination data before running this migration.`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
// Index changes can't be part of a multi-document transaction, and one made after the
|
|
86
|
+
// migration's transaction has written to a collection aborts it. A transaction's snapshot starts
|
|
87
|
+
// at its first operation, so re-keying every index before any transactional write is safe.
|
|
88
|
+
for (const target of targets){
|
|
89
|
+
await rekeyIndexes(target);
|
|
90
|
+
}
|
|
91
|
+
for (const { collection, fromPath, toPath } of targets){
|
|
92
|
+
const result = await collection.updateMany({
|
|
93
|
+
[fromPath]: {
|
|
94
|
+
$exists: true
|
|
95
|
+
},
|
|
96
|
+
[toPath]: {
|
|
97
|
+
$exists: false
|
|
98
|
+
}
|
|
99
|
+
}, {
|
|
100
|
+
$rename: {
|
|
101
|
+
[fromPath]: toPath
|
|
102
|
+
}
|
|
103
|
+
}, {
|
|
104
|
+
session
|
|
105
|
+
});
|
|
106
|
+
payload.logger.info({
|
|
107
|
+
msg: `sizes-to-variants (${direction}): renamed ${fromPath} on ${result.modifiedCount} document(s) in "${collection.collectionName}"`
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
async function findUnexpectedDestinationData({ collection, expectedVariantNames, toPath }) {
|
|
112
|
+
const expectedVariantNameSet = new Set(expectedVariantNames);
|
|
113
|
+
const cursor = collection.find({
|
|
114
|
+
[toPath]: {
|
|
115
|
+
$exists: true
|
|
116
|
+
}
|
|
117
|
+
}, {
|
|
118
|
+
projection: {
|
|
119
|
+
_id: 1,
|
|
120
|
+
[toPath]: 1
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
try {
|
|
124
|
+
for await (const document of cursor){
|
|
125
|
+
const destinationData = getDocumentPath({
|
|
126
|
+
document,
|
|
127
|
+
path: toPath
|
|
128
|
+
});
|
|
129
|
+
if (hasUnexpectedVariantData({
|
|
130
|
+
destinationData,
|
|
131
|
+
expectedVariantNameSet
|
|
132
|
+
})) {
|
|
133
|
+
return document;
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
} finally{
|
|
137
|
+
await cursor.close();
|
|
138
|
+
}
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
function getDocumentPath({ document, path }) {
|
|
142
|
+
return path.split('.').reduce((value, pathSegment)=>{
|
|
143
|
+
if (!isRecord(value)) {
|
|
144
|
+
return undefined;
|
|
145
|
+
}
|
|
146
|
+
return value[pathSegment];
|
|
147
|
+
}, document);
|
|
148
|
+
}
|
|
149
|
+
function hasUnexpectedVariantData({ destinationData, expectedVariantNameSet }) {
|
|
150
|
+
if (!isRecord(destinationData)) {
|
|
151
|
+
return destinationData !== null && destinationData !== undefined;
|
|
152
|
+
}
|
|
153
|
+
for (const [variantName, variantData] of Object.entries(destinationData)){
|
|
154
|
+
if (!expectedVariantNameSet.has(variantName)) {
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
if (variantData === null || variantData === undefined) {
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
if (!isRecord(variantData) || Object.keys(variantData).some((metadataFieldName)=>!GENERATED_VARIANT_METADATA_FIELDS.has(metadataFieldName))) {
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
function isRecord(value) {
|
|
167
|
+
return value !== null && typeof value === 'object' && !Array.isArray(value);
|
|
168
|
+
}
|
|
169
|
+
async function rekeyIndexes({ collection, fromPath, toPath }) {
|
|
170
|
+
let indexes;
|
|
171
|
+
try {
|
|
172
|
+
indexes = await collection.indexes();
|
|
173
|
+
} catch (err) {
|
|
174
|
+
if (err.code === NAMESPACE_NOT_FOUND_CODE) {
|
|
175
|
+
return;
|
|
176
|
+
}
|
|
177
|
+
throw err;
|
|
178
|
+
}
|
|
179
|
+
for (const index of indexes){
|
|
180
|
+
const sourceKeys = getIndexKeysForCreate({
|
|
181
|
+
fromPath,
|
|
182
|
+
index,
|
|
183
|
+
toPath: fromPath
|
|
184
|
+
});
|
|
185
|
+
if (!Object.keys(sourceKeys).some((key)=>isPathAtOrBelow({
|
|
186
|
+
key,
|
|
187
|
+
path: fromPath
|
|
188
|
+
}))) {
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const rekeyedKeys = getIndexKeysForCreate({
|
|
192
|
+
fromPath,
|
|
193
|
+
index,
|
|
194
|
+
toPath
|
|
195
|
+
});
|
|
196
|
+
const destinationOptions = getDestinationIndexOptions({
|
|
197
|
+
fromPath,
|
|
198
|
+
index,
|
|
199
|
+
toPath
|
|
200
|
+
});
|
|
201
|
+
if (index.weights && '_fts' in index.key && '_ftsx' in index.key) {
|
|
202
|
+
await rekeyTextIndex({
|
|
203
|
+
collection,
|
|
204
|
+
destinationOptions,
|
|
205
|
+
index,
|
|
206
|
+
rekeyedKeys,
|
|
207
|
+
sourceKeys
|
|
208
|
+
});
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
try {
|
|
212
|
+
await collection.createIndex(rekeyedKeys, destinationOptions);
|
|
213
|
+
} catch (err) {
|
|
214
|
+
if (!INDEX_CONFLICT_CODES.has(err.code ?? 0)) {
|
|
215
|
+
throw err;
|
|
216
|
+
}
|
|
217
|
+
const destinationIndex = (await collection.indexes()).find((candidate)=>isDeepStrictEqual(getIndexKeysForCreate({
|
|
218
|
+
fromPath: toPath,
|
|
219
|
+
index: candidate,
|
|
220
|
+
toPath
|
|
221
|
+
}), rekeyedKeys));
|
|
222
|
+
const existingDestinationOptions = destinationIndex ? getDestinationIndexOptions({
|
|
223
|
+
fromPath: toPath,
|
|
224
|
+
index: destinationIndex,
|
|
225
|
+
toPath
|
|
226
|
+
}) : undefined;
|
|
227
|
+
if (!destinationIndex || !isDeepStrictEqual(existingDestinationOptions, destinationOptions)) {
|
|
228
|
+
throw err;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
await collection.dropIndex(index.name);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
async function rekeyTextIndex({ collection, destinationOptions, index, rekeyedKeys, sourceKeys }) {
|
|
235
|
+
if (!index.name) {
|
|
236
|
+
throw new Error('Cannot re-key an unnamed MongoDB text index.');
|
|
237
|
+
}
|
|
238
|
+
const sourceOptions = {
|
|
239
|
+
...getDestinationIndexOptions({
|
|
240
|
+
fromPath: '',
|
|
241
|
+
index,
|
|
242
|
+
toPath: ''
|
|
243
|
+
}),
|
|
244
|
+
name: index.name
|
|
245
|
+
};
|
|
246
|
+
await collection.dropIndex(index.name);
|
|
247
|
+
try {
|
|
248
|
+
await collection.createIndex(rekeyedKeys, destinationOptions);
|
|
249
|
+
} catch (err) {
|
|
250
|
+
try {
|
|
251
|
+
await collection.createIndex(sourceKeys, sourceOptions);
|
|
252
|
+
} catch (restoreErr) {
|
|
253
|
+
throw new AggregateError([
|
|
254
|
+
err,
|
|
255
|
+
restoreErr
|
|
256
|
+
], `Failed to create the replacement for MongoDB text index "${index.name}" and failed to restore the source index.`);
|
|
257
|
+
}
|
|
258
|
+
throw err;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
function getIndexKeysForCreate({ fromPath, index, toPath }) {
|
|
262
|
+
const keys = [];
|
|
263
|
+
const hasInternalTextKeys = '_fts' in index.key && '_ftsx' in index.key;
|
|
264
|
+
for (const [key, value] of Object.entries(index.key)){
|
|
265
|
+
if (hasInternalTextKeys && key === '_fts') {
|
|
266
|
+
for (const textPath of Object.keys(index.weights ?? {})){
|
|
267
|
+
keys.push([
|
|
268
|
+
rekeyPath({
|
|
269
|
+
fromPath,
|
|
270
|
+
path: textPath,
|
|
271
|
+
toPath
|
|
272
|
+
}),
|
|
273
|
+
'text'
|
|
274
|
+
]);
|
|
275
|
+
}
|
|
276
|
+
continue;
|
|
277
|
+
}
|
|
278
|
+
if (hasInternalTextKeys && key === '_ftsx') {
|
|
279
|
+
continue;
|
|
280
|
+
}
|
|
281
|
+
keys.push([
|
|
282
|
+
rekeyPath({
|
|
283
|
+
fromPath,
|
|
284
|
+
path: key,
|
|
285
|
+
toPath
|
|
286
|
+
}),
|
|
287
|
+
value
|
|
288
|
+
]);
|
|
289
|
+
}
|
|
290
|
+
return Object.fromEntries(keys);
|
|
291
|
+
}
|
|
292
|
+
function getDestinationIndexOptions({ fromPath, index, toPath }) {
|
|
293
|
+
const options = {
|
|
294
|
+
'2dsphereIndexVersion': index['2dsphereIndexVersion'],
|
|
295
|
+
background: index.background,
|
|
296
|
+
bits: index.bits,
|
|
297
|
+
bucketSize: index.bucketSize,
|
|
298
|
+
collation: index.collation,
|
|
299
|
+
default_language: index.default_language,
|
|
300
|
+
expireAfterSeconds: index.expireAfterSeconds,
|
|
301
|
+
hidden: index.hidden,
|
|
302
|
+
language_override: index.language_override,
|
|
303
|
+
max: index.max,
|
|
304
|
+
min: index.min,
|
|
305
|
+
partialFilterExpression: index.partialFilterExpression ? rekeyDocumentPaths({
|
|
306
|
+
document: index.partialFilterExpression,
|
|
307
|
+
fromPath,
|
|
308
|
+
toPath
|
|
309
|
+
}) : undefined,
|
|
310
|
+
sparse: index.sparse,
|
|
311
|
+
storageEngine: index.storageEngine,
|
|
312
|
+
textIndexVersion: index.textIndexVersion,
|
|
313
|
+
unique: index.unique,
|
|
314
|
+
weights: index.weights ? rekeyDocumentPaths({
|
|
315
|
+
document: index.weights,
|
|
316
|
+
fromPath,
|
|
317
|
+
toPath
|
|
318
|
+
}) : undefined,
|
|
319
|
+
wildcardProjection: index.wildcardProjection ? rekeyDocumentPaths({
|
|
320
|
+
document: index.wildcardProjection,
|
|
321
|
+
fromPath,
|
|
322
|
+
toPath
|
|
323
|
+
}) : undefined
|
|
324
|
+
};
|
|
325
|
+
return Object.fromEntries(Object.entries(options).filter(([, value])=>value !== undefined));
|
|
326
|
+
}
|
|
327
|
+
function rekeyDocumentPaths({ document, fromPath, toPath }) {
|
|
328
|
+
return Object.fromEntries(Object.entries(document).map(([key, value])=>[
|
|
329
|
+
rekeyPath({
|
|
330
|
+
fromPath,
|
|
331
|
+
path: key,
|
|
332
|
+
toPath
|
|
333
|
+
}),
|
|
334
|
+
rekeyDocumentValue({
|
|
335
|
+
fromPath,
|
|
336
|
+
toPath,
|
|
337
|
+
value
|
|
338
|
+
})
|
|
339
|
+
]));
|
|
340
|
+
}
|
|
341
|
+
function isPathAtOrBelow({ key, path }) {
|
|
342
|
+
return key === path || key.startsWith(`${path}.`);
|
|
343
|
+
}
|
|
344
|
+
function rekeyPath({ fromPath, path, toPath }) {
|
|
345
|
+
return isPathAtOrBelow({
|
|
346
|
+
key: path,
|
|
347
|
+
path: fromPath
|
|
348
|
+
}) ? `${toPath}${path.slice(fromPath.length)}` : path;
|
|
349
|
+
}
|
|
350
|
+
function rekeyDocumentValue({ fromPath, toPath, value }) {
|
|
351
|
+
if (Array.isArray(value)) {
|
|
352
|
+
return value.map((item)=>rekeyDocumentValue({
|
|
353
|
+
fromPath,
|
|
354
|
+
toPath,
|
|
355
|
+
value: item
|
|
356
|
+
}));
|
|
357
|
+
}
|
|
358
|
+
if (value !== null && typeof value === 'object' && (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)) {
|
|
359
|
+
return rekeyDocumentPaths({
|
|
360
|
+
document: value,
|
|
361
|
+
fromPath,
|
|
362
|
+
toPath
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
return value;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
//# sourceMappingURL=migrateSizesToVariants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateSizesToVariants.ts"],"sourcesContent":["import type {\n Collection,\n CreateIndexesOptions,\n Document,\n IndexDescriptionInfo,\n IndexDirection,\n} from 'mongodb'\nimport type { Payload, PayloadRequest } from 'payload'\n\nimport { isDeepStrictEqual } from 'node:util'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../utilities/getSession.js'\n\n/** Mongo error codes for an index that conflicts with an existing index. */\nconst INDEX_CONFLICT_CODES = new Set([85, 86])\n/** Mongo error code for a collection that hasn't been created yet. */\nconst NAMESPACE_NOT_FOUND_CODE = 26\nconst GENERATED_VARIANT_METADATA_FIELDS = new Set([\n 'filename',\n 'filesize',\n 'height',\n 'mimeType',\n 'url',\n 'width',\n])\n\n/**\n * Moves every upload collection's stored image sizes between the legacy `sizes` field and\n * `variants`, on each collection and its versions collection (`version.sizes`). Uses `$rename`,\n * so no document is rewritten beyond the renamed key, and re-keys the indexes on those fields.\n * Documents that are already renamed are left as they are, so it's safe to re-run.\n */\nexport async function migrateSizesToVariants({\n direction = 'up',\n payload,\n req,\n}: {\n direction?: 'down' | 'up'\n payload: Payload\n req?: Partial<PayloadRequest>\n}): Promise<void> {\n const adapter = payload.db as unknown as MongooseAdapter\n const session = await getSession(adapter, req)\n const [from, to] = direction === 'up' ? ['sizes', 'variants'] : ['variants', 'sizes']\n\n const targets: {\n collection: Collection\n expectedVariantNames: string[]\n fromPath: string\n toPath: string\n }[] = []\n\n for (const collection of payload.config.collections) {\n if (!collection.upload) {\n continue\n }\n\n const upload = typeof collection.upload === 'object' ? collection.upload : undefined\n const expectedVariantNames = (upload?.variants ?? []).map(({ name }) => name)\n\n targets.push({\n collection: adapter.collections[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: from,\n toPath: to,\n })\n\n if (collection.versions && adapter.versions[collection.slug]) {\n targets.push({\n collection: adapter.versions[collection.slug]!.collection,\n expectedVariantNames,\n fromPath: `version.${from}`,\n toPath: `version.${to}`,\n })\n }\n }\n\n for (const { collection, expectedVariantNames, fromPath, toPath } of targets) {\n const [sourceData, directCollision] = await Promise.all([\n collection.findOne({ [fromPath]: { $exists: true } }, { projection: { _id: 1 } }),\n collection.findOne(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: true } },\n { projection: { _id: 1 } },\n ),\n ])\n\n if (\n sourceData &&\n (directCollision ||\n (await findUnexpectedDestinationData({ collection, expectedVariantNames, toPath })))\n ) {\n throw new Error(\n `Cannot run the sizes-to-variants migration because collection \"${collection.collectionName}\" contains both \"${fromPath}\" and \"${toPath}\" data. Move or rename the existing destination data before running this migration.`,\n )\n }\n }\n\n // Index changes can't be part of a multi-document transaction, and one made after the\n // migration's transaction has written to a collection aborts it. A transaction's snapshot starts\n // at its first operation, so re-keying every index before any transactional write is safe.\n for (const target of targets) {\n await rekeyIndexes(target)\n }\n\n for (const { collection, fromPath, toPath } of targets) {\n const result = await collection.updateMany(\n { [fromPath]: { $exists: true }, [toPath]: { $exists: false } },\n { $rename: { [fromPath]: toPath } },\n { session },\n )\n\n payload.logger.info({\n msg: `sizes-to-variants (${direction}): renamed ${fromPath} on ${result.modifiedCount} document(s) in \"${collection.collectionName}\"`,\n })\n }\n}\n\nasync function findUnexpectedDestinationData({\n collection,\n expectedVariantNames,\n toPath,\n}: {\n collection: Collection\n expectedVariantNames: string[]\n toPath: string\n}): Promise<Document | null> {\n const expectedVariantNameSet = new Set(expectedVariantNames)\n const cursor = collection.find(\n { [toPath]: { $exists: true } },\n { projection: { _id: 1, [toPath]: 1 } },\n )\n\n try {\n for await (const document of cursor) {\n const destinationData = getDocumentPath({ document, path: toPath })\n\n if (hasUnexpectedVariantData({ destinationData, expectedVariantNameSet })) {\n return document\n }\n }\n } finally {\n await cursor.close()\n }\n\n return null\n}\n\nfunction getDocumentPath({ document, path }: { document: Document; path: string }): unknown {\n return path.split('.').reduce<unknown>((value, pathSegment) => {\n if (!isRecord(value)) {\n return undefined\n }\n\n return value[pathSegment]\n }, document)\n}\n\nfunction hasUnexpectedVariantData({\n destinationData,\n expectedVariantNameSet,\n}: {\n destinationData: unknown\n expectedVariantNameSet: Set<string>\n}): boolean {\n if (!isRecord(destinationData)) {\n return destinationData !== null && destinationData !== undefined\n }\n\n for (const [variantName, variantData] of Object.entries(destinationData)) {\n if (!expectedVariantNameSet.has(variantName)) {\n return true\n }\n\n if (variantData === null || variantData === undefined) {\n continue\n }\n\n if (\n !isRecord(variantData) ||\n Object.keys(variantData).some(\n (metadataFieldName) => !GENERATED_VARIANT_METADATA_FIELDS.has(metadataFieldName),\n )\n ) {\n return true\n }\n }\n\n return false\n}\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return value !== null && typeof value === 'object' && !Array.isArray(value)\n}\n\nasync function rekeyIndexes({\n collection,\n fromPath,\n toPath,\n}: {\n collection: Collection\n fromPath: string\n toPath: string\n}): Promise<void> {\n let indexes: IndexDescriptionInfo[]\n\n try {\n indexes = await collection.indexes()\n } catch (err) {\n if ((err as { code?: number }).code === NAMESPACE_NOT_FOUND_CODE) {\n return\n }\n\n throw err\n }\n\n for (const index of indexes) {\n const sourceKeys = getIndexKeysForCreate({ fromPath, index, toPath: fromPath })\n\n if (!Object.keys(sourceKeys).some((key) => isPathAtOrBelow({ key, path: fromPath }))) {\n continue\n }\n\n const rekeyedKeys = getIndexKeysForCreate({ fromPath, index, toPath })\n const destinationOptions = getDestinationIndexOptions({ fromPath, index, toPath })\n\n if (index.weights && '_fts' in index.key && '_ftsx' in index.key) {\n await rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n })\n continue\n }\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n if (!INDEX_CONFLICT_CODES.has((err as { code?: number }).code ?? 0)) {\n throw err\n }\n\n const destinationIndex = (await collection.indexes()).find((candidate) =>\n isDeepStrictEqual(\n getIndexKeysForCreate({ fromPath: toPath, index: candidate, toPath }),\n rekeyedKeys,\n ),\n )\n const existingDestinationOptions = destinationIndex\n ? getDestinationIndexOptions({\n fromPath: toPath,\n index: destinationIndex,\n toPath,\n })\n : undefined\n\n if (!destinationIndex || !isDeepStrictEqual(existingDestinationOptions, destinationOptions)) {\n throw err\n }\n }\n\n await collection.dropIndex(index.name!)\n }\n}\n\nasync function rekeyTextIndex({\n collection,\n destinationOptions,\n index,\n rekeyedKeys,\n sourceKeys,\n}: {\n collection: Collection\n destinationOptions: CreateIndexesOptions\n index: IndexDescriptionInfo\n rekeyedKeys: Record<string, IndexDirection>\n sourceKeys: Record<string, IndexDirection>\n}): Promise<void> {\n if (!index.name) {\n throw new Error('Cannot re-key an unnamed MongoDB text index.')\n }\n\n const sourceOptions: CreateIndexesOptions = {\n ...getDestinationIndexOptions({ fromPath: '', index, toPath: '' }),\n name: index.name,\n }\n\n await collection.dropIndex(index.name)\n\n try {\n await collection.createIndex(rekeyedKeys, destinationOptions)\n } catch (err) {\n try {\n await collection.createIndex(sourceKeys, sourceOptions)\n } catch (restoreErr) {\n throw new AggregateError(\n [err, restoreErr],\n `Failed to create the replacement for MongoDB text index \"${index.name}\" and failed to restore the source index.`,\n )\n }\n\n throw err\n }\n}\n\nfunction getIndexKeysForCreate({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): Record<string, IndexDirection> {\n const keys: [string, IndexDirection][] = []\n const hasInternalTextKeys = '_fts' in index.key && '_ftsx' in index.key\n\n for (const [key, value] of Object.entries(index.key)) {\n if (hasInternalTextKeys && key === '_fts') {\n for (const textPath of Object.keys(index.weights ?? {})) {\n keys.push([rekeyPath({ fromPath, path: textPath, toPath }), 'text'])\n }\n continue\n }\n\n if (hasInternalTextKeys && key === '_ftsx') {\n continue\n }\n\n keys.push([rekeyPath({ fromPath, path: key, toPath }), value])\n }\n\n return Object.fromEntries(keys)\n}\n\nfunction getDestinationIndexOptions({\n fromPath,\n index,\n toPath,\n}: {\n fromPath: string\n index: IndexDescriptionInfo\n toPath: string\n}): CreateIndexesOptions {\n const options: CreateIndexesOptions = {\n '2dsphereIndexVersion': index['2dsphereIndexVersion'],\n background: index.background,\n bits: index.bits,\n bucketSize: index.bucketSize,\n collation: index.collation,\n default_language: index.default_language,\n expireAfterSeconds: index.expireAfterSeconds,\n hidden: index.hidden,\n language_override: index.language_override,\n max: index.max,\n min: index.min,\n partialFilterExpression: index.partialFilterExpression\n ? rekeyDocumentPaths({ document: index.partialFilterExpression, fromPath, toPath })\n : undefined,\n sparse: index.sparse,\n storageEngine: index.storageEngine,\n textIndexVersion: index.textIndexVersion,\n unique: index.unique,\n weights: index.weights\n ? rekeyDocumentPaths({ document: index.weights, fromPath, toPath })\n : undefined,\n wildcardProjection: index.wildcardProjection\n ? rekeyDocumentPaths({ document: index.wildcardProjection, fromPath, toPath })\n : undefined,\n }\n\n return Object.fromEntries(\n Object.entries(options).filter(([, value]) => value !== undefined),\n ) as CreateIndexesOptions\n}\n\nfunction rekeyDocumentPaths({\n document,\n fromPath,\n toPath,\n}: {\n document: Document\n fromPath: string\n toPath: string\n}): Document {\n return Object.fromEntries(\n Object.entries(document).map(([key, value]) => [\n rekeyPath({ fromPath, path: key, toPath }),\n rekeyDocumentValue({ fromPath, toPath, value }),\n ]),\n )\n}\n\nfunction isPathAtOrBelow({ key, path }: { key: string; path: string }): boolean {\n return key === path || key.startsWith(`${path}.`)\n}\n\nfunction rekeyPath({\n fromPath,\n path,\n toPath,\n}: {\n fromPath: string\n path: string\n toPath: string\n}): string {\n return isPathAtOrBelow({ key: path, path: fromPath })\n ? `${toPath}${path.slice(fromPath.length)}`\n : path\n}\n\nfunction rekeyDocumentValue({\n fromPath,\n toPath,\n value,\n}: {\n fromPath: string\n toPath: string\n value: unknown\n}): unknown {\n if (Array.isArray(value)) {\n return value.map((item) => rekeyDocumentValue({ fromPath, toPath, value: item }))\n }\n\n if (\n value !== null &&\n typeof value === 'object' &&\n (Object.getPrototypeOf(value) === Object.prototype || Object.getPrototypeOf(value) === null)\n ) {\n return rekeyDocumentPaths({ document: value as Document, fromPath, toPath })\n }\n\n return value\n}\n"],"names":["isDeepStrictEqual","getSession","INDEX_CONFLICT_CODES","Set","NAMESPACE_NOT_FOUND_CODE","GENERATED_VARIANT_METADATA_FIELDS","migrateSizesToVariants","direction","payload","req","adapter","db","session","from","to","targets","collection","config","collections","upload","undefined","expectedVariantNames","variants","map","name","push","slug","fromPath","toPath","versions","sourceData","directCollision","Promise","all","findOne","$exists","projection","_id","findUnexpectedDestinationData","Error","collectionName","target","rekeyIndexes","result","updateMany","$rename","logger","info","msg","modifiedCount","expectedVariantNameSet","cursor","find","document","destinationData","getDocumentPath","path","hasUnexpectedVariantData","close","split","reduce","value","pathSegment","isRecord","variantName","variantData","Object","entries","has","keys","some","metadataFieldName","Array","isArray","indexes","err","code","index","sourceKeys","getIndexKeysForCreate","key","isPathAtOrBelow","rekeyedKeys","destinationOptions","getDestinationIndexOptions","weights","rekeyTextIndex","createIndex","destinationIndex","candidate","existingDestinationOptions","dropIndex","sourceOptions","restoreErr","AggregateError","hasInternalTextKeys","textPath","rekeyPath","fromEntries","options","background","bits","bucketSize","collation","default_language","expireAfterSeconds","hidden","language_override","max","min","partialFilterExpression","rekeyDocumentPaths","sparse","storageEngine","textIndexVersion","unique","wildcardProjection","filter","rekeyDocumentValue","startsWith","slice","length","item","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,CAAC9D,WAAW,IAAG,GAC1F;QACA,OAAOkD,mBAAmB;YAAE1D,UAAUQ;YAAmBlC;YAAUC;QAAO;IAC5E;IAEA,OAAOiC;AACT"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
declare const imports = "import { migrateSizesToVariants } from '@payloadcms/db-mongodb/migration-utils'";
|
|
2
|
+
declare const upSQL = " await migrateSizesToVariants({ payload, req })\n";
|
|
3
|
+
declare const downSQL = " await migrateSizesToVariants({ direction: 'down', payload, req })\n";
|
|
4
|
+
export { downSQL, imports, upSQL };
|
|
5
|
+
//# sourceMappingURL=sizes-to-variants.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sizes-to-variants.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/sizes-to-variants.ts"],"names":[],"mappings":"AAAA,QAAA,MAAM,OAAO,oFAAoF,CAAA;AACjG,QAAA,MAAM,KAAK,uDACV,CAAA;AACD,QAAA,MAAM,OAAO,0EACZ,CAAA;AAED,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,KAAK,EAAE,CAAA"}
|