@payloadcms/db-mongodb 3.2.2-canary.b072173 → 3.2.2-canary.d2399ed
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +19 -21
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts.map +1 -1
- package/dist/countGlobalVersions.js +19 -21
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +19 -21
- package/dist/countVersions.js.map +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +22 -22
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +21 -19
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +35 -30
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +35 -30
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +6 -6
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +17 -13
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +6 -6
- package/dist/deleteVersions.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +41 -74
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +19 -18
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +33 -62
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +32 -19
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +34 -62
- package/dist/findVersions.js.map +1 -1
- package/dist/{withSession.d.ts → getSession.d.ts} +2 -4
- package/dist/getSession.d.ts.map +1 -0
- package/dist/{withSession.js → getSession.js} +3 -5
- package/dist/getSession.js.map +1 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +20 -16
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js +2 -2
- package/dist/predefinedMigrations/migrateVersionsV1_V2.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +3 -1
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +2 -0
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +2 -6
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +3 -1
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +14 -10
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/buildSortParam.d.ts +1 -7
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.js +2 -2
- package/dist/queries/buildSortParam.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.js +2 -2
- package/dist/queries/getLocalizedSortProperty.js.map +1 -1
- package/dist/queries/parseParams.d.ts +3 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +2 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +16 -0
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +41 -65
- package/dist/queryDrafts.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +25 -23
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +28 -27
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +28 -24
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +28 -26
- package/dist/updateVersion.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +4 -4
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +7 -28
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.js +18 -1
- package/dist/utilities/buildProjectionFromSelect.js.map +1 -1
- package/dist/utilities/findMany.d.ts +20 -0
- package/dist/utilities/findMany.d.ts.map +1 -0
- package/dist/utilities/findMany.js +84 -0
- package/dist/utilities/findMany.js.map +1 -0
- package/dist/utilities/getHasNearConstraint.d.ts +3 -0
- package/dist/utilities/getHasNearConstraint.d.ts.map +1 -0
- package/dist/utilities/getHasNearConstraint.js +26 -0
- package/dist/utilities/getHasNearConstraint.js.map +1 -0
- package/dist/utilities/transform.d.ts +17 -0
- package/dist/utilities/transform.d.ts.map +1 -0
- package/dist/utilities/transform.js +250 -0
- package/dist/utilities/transform.js.map +1 -0
- package/dist/utilities/{sanitizeRelationshipIDs.spec.js → transform.spec.js} +23 -7
- package/dist/utilities/transform.spec.js.map +1 -0
- package/package.json +3 -3
- package/dist/utilities/sanitizeInternalFields.d.ts +0 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +0 -1
- package/dist/utilities/sanitizeInternalFields.js +0 -20
- package/dist/utilities/sanitizeInternalFields.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +0 -9
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.js +0 -125
- package/dist/utilities/sanitizeRelationshipIDs.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +0 -1
- package/dist/withSession.d.ts.map +0 -1
- package/dist/withSession.js.map +0 -1
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
import { buildVersionGlobalFields
|
|
1
|
+
import { buildVersionGlobalFields } from 'payload';
|
|
2
|
+
import { getSession } from './getSession.js';
|
|
2
3
|
import { buildSortParam } from './queries/buildSortParam.js';
|
|
3
4
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import { findMany } from './utilities/findMany.js';
|
|
6
|
+
import { getHasNearConstraint } from './utilities/getHasNearConstraint.js';
|
|
7
|
+
import { transform } from './utilities/transform.js';
|
|
6
8
|
export const findGlobalVersions = async function findGlobalVersions({ global, limit, locale, page, pagination, req = {}, select, skip, sort: sortArg, where }) {
|
|
7
9
|
const Model = this.versions[global];
|
|
8
10
|
const versionFields = buildVersionGlobalFields(this.payload.config, this.payload.globals.config.find(({ slug })=>slug === global), true);
|
|
9
|
-
const
|
|
10
|
-
...await withSession(this, req),
|
|
11
|
-
limit,
|
|
12
|
-
skip
|
|
13
|
-
};
|
|
14
|
-
let hasNearConstraint = false;
|
|
15
|
-
if (where) {
|
|
16
|
-
const constraints = flattenWhereToOperators(where);
|
|
17
|
-
hasNearConstraint = constraints.some((prop)=>Object.keys(prop).some((key)=>key === 'near'));
|
|
18
|
-
}
|
|
11
|
+
const hasNearConstraint = getHasNearConstraint(where);
|
|
19
12
|
let sort;
|
|
20
13
|
if (!hasNearConstraint) {
|
|
21
14
|
sort = buildSortParam({
|
|
@@ -26,68 +19,46 @@ export const findGlobalVersions = async function findGlobalVersions({ global, li
|
|
|
26
19
|
timestamps: true
|
|
27
20
|
});
|
|
28
21
|
}
|
|
22
|
+
const session = await getSession(this, req);
|
|
29
23
|
const query = await Model.buildQuery({
|
|
30
24
|
globalSlug: global,
|
|
31
25
|
locale,
|
|
32
26
|
payload: this.payload,
|
|
27
|
+
session,
|
|
33
28
|
where
|
|
34
29
|
});
|
|
35
30
|
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
36
31
|
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0;
|
|
37
|
-
const
|
|
38
|
-
|
|
39
|
-
|
|
32
|
+
const projection = buildProjectionFromSelect({
|
|
33
|
+
adapter: this,
|
|
34
|
+
fields: versionFields,
|
|
35
|
+
select
|
|
36
|
+
});
|
|
37
|
+
const collation = this.collation ? {
|
|
38
|
+
locale: locale && locale !== 'all' && locale !== '*' ? locale : 'en',
|
|
39
|
+
...this.collation
|
|
40
|
+
} : undefined;
|
|
41
|
+
const result = await findMany({
|
|
42
|
+
adapter: this,
|
|
43
|
+
collation,
|
|
44
|
+
collection: Model.collection,
|
|
40
45
|
limit,
|
|
41
|
-
options,
|
|
42
46
|
page,
|
|
43
47
|
pagination,
|
|
44
|
-
projection
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}),
|
|
48
|
+
projection,
|
|
49
|
+
query,
|
|
50
|
+
session,
|
|
51
|
+
skip,
|
|
49
52
|
sort,
|
|
50
53
|
useEstimatedCount
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
|
60
|
-
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
|
|
61
|
-
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
|
62
|
-
// which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses
|
|
63
|
-
// the correct indexed field
|
|
64
|
-
paginationOptions.useCustomCountFn = ()=>{
|
|
65
|
-
return Promise.resolve(Model.countDocuments(query, {
|
|
66
|
-
...options,
|
|
67
|
-
hint: {
|
|
68
|
-
_id: 1
|
|
69
|
-
}
|
|
70
|
-
}));
|
|
71
|
-
};
|
|
72
|
-
}
|
|
73
|
-
if (limit >= 0) {
|
|
74
|
-
paginationOptions.limit = limit;
|
|
75
|
-
// limit must also be set here, it's ignored when pagination is false
|
|
76
|
-
paginationOptions.options.limit = limit;
|
|
77
|
-
// Disable pagination if limit is 0
|
|
78
|
-
if (limit === 0) {
|
|
79
|
-
paginationOptions.pagination = false;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const result = await Model.paginate(query, paginationOptions);
|
|
83
|
-
const docs = JSON.parse(JSON.stringify(result.docs));
|
|
84
|
-
return {
|
|
85
|
-
...result,
|
|
86
|
-
docs: docs.map((doc)=>{
|
|
87
|
-
doc.id = doc._id;
|
|
88
|
-
return sanitizeInternalFields(doc);
|
|
89
|
-
})
|
|
90
|
-
};
|
|
54
|
+
});
|
|
55
|
+
transform({
|
|
56
|
+
adapter: this,
|
|
57
|
+
data: result.docs,
|
|
58
|
+
fields: versionFields,
|
|
59
|
+
operation: 'read'
|
|
60
|
+
});
|
|
61
|
+
return result;
|
|
91
62
|
};
|
|
92
63
|
|
|
93
64
|
//# sourceMappingURL=findGlobalVersions.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/findGlobalVersions.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/findGlobalVersions.ts"],"sourcesContent":["import type { CollationOptions } from 'mongodb'\nimport type { FindGlobalVersions, PayloadRequest } from 'payload'\n\nimport { buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getSession } from './getSession.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { findMany } from './utilities/findMany.js'\nimport { getHasNearConstraint } from './utilities/getHasNearConstraint.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findGlobalVersions: FindGlobalVersions = async function findGlobalVersions(\n this: MongooseAdapter,\n {\n global,\n limit,\n locale,\n page,\n pagination,\n req = {} as PayloadRequest,\n select,\n skip,\n sort: sortArg,\n where,\n },\n) {\n const Model = this.versions[global]\n const versionFields = buildVersionGlobalFields(\n this.payload.config,\n this.payload.globals.config.find(({ slug }) => slug === global),\n true,\n )\n\n const hasNearConstraint = getHasNearConstraint(where)\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: versionFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const session = await getSession(this, req)\n\n const query = await Model.buildQuery({\n globalSlug: global,\n locale,\n payload: this.payload,\n session,\n where,\n })\n\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n\n const projection = buildProjectionFromSelect({ adapter: this, fields: versionFields, select })\n\n const collation: CollationOptions | undefined = this.collation\n ? {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : 'en',\n ...this.collation,\n }\n : undefined\n\n const result = await findMany({\n adapter: this,\n collation,\n collection: Model.collection,\n limit,\n page,\n pagination,\n projection,\n query,\n session,\n skip,\n sort,\n useEstimatedCount,\n })\n\n transform({\n adapter: this,\n data: result.docs,\n fields: versionFields,\n operation: 'read',\n })\n\n return result\n}\n"],"names":["buildVersionGlobalFields","getSession","buildSortParam","buildProjectionFromSelect","findMany","getHasNearConstraint","transform","findGlobalVersions","global","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","Model","versions","versionFields","payload","config","globals","find","slug","hasNearConstraint","fields","timestamps","session","query","buildQuery","globalSlug","useEstimatedCount","Object","keys","length","projection","adapter","collation","undefined","result","collection","data","docs","operation"],"mappings":"AAGA,SAASA,wBAAwB,QAAQ,UAAS;AAIlD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,QAAQA,UAAUA,WAAW,SAASA,WAAW,MAAMA,SAAS;QAChE,GAAG,IAAI,CAAC8B,SAAS;IACnB,IACAC;IAEJ,MAAMC,SAAS,MAAMtC,SAAS;QAC5BmC,SAAS,IAAI;QACbC;QACAG,YAAYxB,MAAMwB,UAAU;QAC5BlC;QACAE;QACAC;QACA0B;QACAP;QACAD;QACAf;QACAC;QACAkB;IACF;IAEA5B,UAAU;QACRiC,SAAS,IAAI;QACbK,MAAMF,OAAOG,IAAI;QACjBjB,QAAQP;QACRyB,WAAW;IACb;IAEA,OAAOJ;AACT,EAAC"}
|
package/dist/findOne.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findOne.d.ts","sourceRoot":"","sources":["../src/findOne.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"findOne.d.ts","sourceRoot":"","sources":["../src/findOne.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAkB,MAAM,SAAS,CAAA;AAStD,eAAO,MAAM,OAAO,EAAE,OAkErB,CAAA"}
|
package/dist/findOne.js
CHANGED
|
@@ -1,49 +1,62 @@
|
|
|
1
|
+
import { getSession } from './getSession.js';
|
|
1
2
|
import { buildJoinAggregation } from './utilities/buildJoinAggregation.js';
|
|
2
3
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
3
|
-
import {
|
|
4
|
-
import { withSession } from './withSession.js';
|
|
4
|
+
import { transform } from './utilities/transform.js';
|
|
5
5
|
export const findOne = async function findOne({ collection, joins, locale, req = {}, select, where }) {
|
|
6
6
|
const Model = this.collections[collection];
|
|
7
7
|
const collectionConfig = this.payload.collections[collection].config;
|
|
8
|
-
const
|
|
9
|
-
...await withSession(this, req),
|
|
10
|
-
lean: true
|
|
11
|
-
};
|
|
8
|
+
const session = await getSession(this, req);
|
|
12
9
|
const query = await Model.buildQuery({
|
|
13
10
|
locale,
|
|
14
11
|
payload: this.payload,
|
|
12
|
+
session,
|
|
15
13
|
where
|
|
16
14
|
});
|
|
15
|
+
const fields = collectionConfig.flattenedFields;
|
|
17
16
|
const projection = buildProjectionFromSelect({
|
|
18
17
|
adapter: this,
|
|
19
|
-
fields
|
|
18
|
+
fields,
|
|
20
19
|
select
|
|
21
20
|
});
|
|
22
|
-
const
|
|
21
|
+
const joinAggregation = await buildJoinAggregation({
|
|
23
22
|
adapter: this,
|
|
24
23
|
collection,
|
|
25
24
|
collectionConfig,
|
|
26
25
|
joins,
|
|
27
|
-
limit: 1,
|
|
28
26
|
locale,
|
|
29
27
|
projection,
|
|
30
|
-
|
|
28
|
+
session
|
|
31
29
|
});
|
|
32
30
|
let doc;
|
|
33
|
-
if (
|
|
34
|
-
|
|
31
|
+
if (joinAggregation) {
|
|
32
|
+
const cursor = Model.collection.aggregate([
|
|
33
|
+
{
|
|
34
|
+
$match: query
|
|
35
|
+
}
|
|
36
|
+
], {
|
|
37
|
+
session
|
|
38
|
+
});
|
|
39
|
+
cursor.limit(1);
|
|
40
|
+
for (const stage of joinAggregation){
|
|
41
|
+
cursor.addStage(stage);
|
|
42
|
+
}
|
|
43
|
+
[doc] = await cursor.toArray();
|
|
35
44
|
} else {
|
|
36
|
-
|
|
37
|
-
|
|
45
|
+
doc = await Model.collection.findOne(query, {
|
|
46
|
+
projection,
|
|
47
|
+
session
|
|
48
|
+
});
|
|
38
49
|
}
|
|
39
50
|
if (!doc) {
|
|
40
51
|
return null;
|
|
41
52
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
53
|
+
transform({
|
|
54
|
+
adapter: this,
|
|
55
|
+
data: doc,
|
|
56
|
+
fields,
|
|
57
|
+
operation: 'read'
|
|
58
|
+
});
|
|
59
|
+
return doc;
|
|
47
60
|
};
|
|
48
61
|
|
|
49
62
|
//# sourceMappingURL=findOne.js.map
|
package/dist/findOne.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/findOne.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/findOne.ts"],"sourcesContent":["import type { FindOne, PayloadRequest } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getSession } from './getSession.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findOne: FindOne = async function findOne(\n this: MongooseAdapter,\n { collection, joins, locale, req = {} as PayloadRequest, select, where },\n) {\n const Model = this.collections[collection]\n const collectionConfig = this.payload.collections[collection].config\n\n const session = await getSession(this, req)\n\n const query = await Model.buildQuery({\n locale,\n payload: this.payload,\n session,\n where,\n })\n\n const fields = collectionConfig.flattenedFields\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n })\n\n const joinAggregation = await buildJoinAggregation({\n adapter: this,\n collection,\n collectionConfig,\n joins,\n locale,\n projection,\n session,\n })\n\n let doc\n if (joinAggregation) {\n const cursor = Model.collection.aggregate(\n [\n {\n $match: query,\n },\n ],\n { session },\n )\n cursor.limit(1)\n for (const stage of joinAggregation) {\n cursor.addStage(stage)\n }\n\n ;[doc] = await cursor.toArray()\n } else {\n doc = await Model.collection.findOne(query, { projection, session })\n }\n\n if (!doc) {\n return null\n }\n\n transform({\n adapter: this,\n data: doc,\n fields,\n operation: 'read',\n })\n\n return doc\n}\n"],"names":["getSession","buildJoinAggregation","buildProjectionFromSelect","transform","findOne","collection","joins","locale","req","select","where","Model","collections","collectionConfig","payload","config","session","query","buildQuery","fields","flattenedFields","projection","adapter","joinAggregation","doc","cursor","aggregate","$match","limit","stage","addStage","toArray","data","operation"],"mappings":"AAIA,SAASA,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,UAAmB,eAAeA,QAE7C,EAAEC,UAAU,EAAEC,KAAK,EAAEC,MAAM,EAAEC,MAAM,CAAC,CAAmB,EAAEC,MAAM,EAAEC,KAAK,EAAE;IAExE,MAAMC,QAAQ,IAAI,CAACC,WAAW,CAACP,WAAW;IAC1C,MAAMQ,mBAAmB,IAAI,CAACC,OAAO,CAACF,WAAW,CAACP,WAAW,CAACU,MAAM;IAEpE,MAAMC,UAAU,MAAMhB,WAAW,IAAI,EAAEQ;IAEvC,MAAMS,QAAQ,MAAMN,MAAMO,UAAU,CAAC;QACnCX;QACAO,SAAS,IAAI,CAACA,OAAO;QACrBE;QACAN;IACF;IAEA,MAAMS,SAASN,iBAAiBO,eAAe;IAE/C,MAAMC,aAAanB,0BAA0B;QAC3CoB,SAAS,IAAI;QACbH;QACAV;IACF;IAEA,MAAMc,kBAAkB,MAAMtB,qBAAqB;QACjDqB,SAAS,IAAI;QACbjB;QACAQ;QACAP;QACAC;QACAc;QACAL;IACF;IAEA,IAAIQ;IACJ,IAAID,iBAAiB;QACnB,MAAME,SAASd,MAAMN,UAAU,CAACqB,SAAS,CACvC;YACE;gBACEC,QAAQV;YACV;SACD,EACD;YAAED;QAAQ;QAEZS,OAAOG,KAAK,CAAC;QACb,KAAK,MAAMC,SAASN,gBAAiB;YACnCE,OAAOK,QAAQ,CAACD;QAClB;QAEC,CAACL,IAAI,GAAG,MAAMC,OAAOM,OAAO;IAC/B,OAAO;QACLP,MAAM,MAAMb,MAAMN,UAAU,CAACD,OAAO,CAACa,OAAO;YAAEI;YAAYL;QAAQ;IACpE;IAEA,IAAI,CAACQ,KAAK;QACR,OAAO;IACT;IAEArB,UAAU;QACRmB,SAAS,IAAI;QACbU,MAAMR;QACNL;QACAc,WAAW;IACb;IAEA,OAAOT;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"findVersions.d.ts","sourceRoot":"","sources":["../src/findVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"findVersions.d.ts","sourceRoot":"","sources":["../src/findVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAkB,MAAM,SAAS,CAAA;AAa3D,eAAO,MAAM,YAAY,EAAE,YAgF1B,CAAA"}
|
package/dist/findVersions.js
CHANGED
|
@@ -1,21 +1,15 @@
|
|
|
1
|
-
import { buildVersionCollectionFields
|
|
1
|
+
import { buildVersionCollectionFields } from 'payload';
|
|
2
|
+
import { getSession } from './getSession.js';
|
|
2
3
|
import { buildSortParam } from './queries/buildSortParam.js';
|
|
3
4
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
5
|
+
import { findMany } from './utilities/findMany.js';
|
|
6
|
+
import { getHasNearConstraint } from './utilities/getHasNearConstraint.js';
|
|
7
|
+
import { transform } from './utilities/transform.js';
|
|
6
8
|
export const findVersions = async function findVersions({ collection, limit, locale, page, pagination, req = {}, select, skip, sort: sortArg, where }) {
|
|
7
9
|
const Model = this.versions[collection];
|
|
8
10
|
const collectionConfig = this.payload.collections[collection].config;
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
limit,
|
|
12
|
-
skip
|
|
13
|
-
};
|
|
14
|
-
let hasNearConstraint = false;
|
|
15
|
-
if (where) {
|
|
16
|
-
const constraints = flattenWhereToOperators(where);
|
|
17
|
-
hasNearConstraint = constraints.some((prop)=>Object.keys(prop).some((key)=>key === 'near'));
|
|
18
|
-
}
|
|
11
|
+
const session = await getSession(this, req);
|
|
12
|
+
const hasNearConstraint = getHasNearConstraint(where);
|
|
19
13
|
let sort;
|
|
20
14
|
if (!hasNearConstraint) {
|
|
21
15
|
sort = buildSortParam({
|
|
@@ -29,64 +23,42 @@ export const findVersions = async function findVersions({ collection, limit, loc
|
|
|
29
23
|
const query = await Model.buildQuery({
|
|
30
24
|
locale,
|
|
31
25
|
payload: this.payload,
|
|
26
|
+
session,
|
|
32
27
|
where
|
|
33
28
|
});
|
|
29
|
+
const versionFields = buildVersionCollectionFields(this.payload.config, collectionConfig, true);
|
|
34
30
|
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
35
31
|
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0;
|
|
36
|
-
const
|
|
37
|
-
|
|
38
|
-
|
|
32
|
+
const projection = buildProjectionFromSelect({
|
|
33
|
+
adapter: this,
|
|
34
|
+
fields: versionFields,
|
|
35
|
+
select
|
|
36
|
+
});
|
|
37
|
+
const collation = this.collation ? {
|
|
38
|
+
locale: locale && locale !== 'all' && locale !== '*' ? locale : 'en',
|
|
39
|
+
...this.collation
|
|
40
|
+
} : undefined;
|
|
41
|
+
const result = await findMany({
|
|
42
|
+
adapter: this,
|
|
43
|
+
collation,
|
|
44
|
+
collection: Model.collection,
|
|
39
45
|
limit,
|
|
40
|
-
options,
|
|
41
46
|
page,
|
|
42
47
|
pagination,
|
|
43
|
-
projection
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}),
|
|
48
|
+
projection,
|
|
49
|
+
query,
|
|
50
|
+
session,
|
|
51
|
+
skip,
|
|
48
52
|
sort,
|
|
49
53
|
useEstimatedCount
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
|
59
|
-
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
|
|
60
|
-
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
|
61
|
-
// which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses
|
|
62
|
-
// the correct indexed field
|
|
63
|
-
paginationOptions.useCustomCountFn = ()=>{
|
|
64
|
-
return Promise.resolve(Model.countDocuments(query, {
|
|
65
|
-
...options,
|
|
66
|
-
hint: {
|
|
67
|
-
_id: 1
|
|
68
|
-
}
|
|
69
|
-
}));
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
if (limit >= 0) {
|
|
73
|
-
paginationOptions.limit = limit;
|
|
74
|
-
// limit must also be set here, it's ignored when pagination is false
|
|
75
|
-
paginationOptions.options.limit = limit;
|
|
76
|
-
// Disable pagination if limit is 0
|
|
77
|
-
if (limit === 0) {
|
|
78
|
-
paginationOptions.pagination = false;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
const result = await Model.paginate(query, paginationOptions);
|
|
82
|
-
const docs = JSON.parse(JSON.stringify(result.docs));
|
|
83
|
-
return {
|
|
84
|
-
...result,
|
|
85
|
-
docs: docs.map((doc)=>{
|
|
86
|
-
doc.id = doc._id;
|
|
87
|
-
return sanitizeInternalFields(doc);
|
|
88
|
-
})
|
|
89
|
-
};
|
|
54
|
+
});
|
|
55
|
+
transform({
|
|
56
|
+
adapter: this,
|
|
57
|
+
data: result.docs,
|
|
58
|
+
fields: versionFields,
|
|
59
|
+
operation: 'read'
|
|
60
|
+
});
|
|
61
|
+
return result;
|
|
90
62
|
};
|
|
91
63
|
|
|
92
64
|
//# sourceMappingURL=findVersions.js.map
|
package/dist/findVersions.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/findVersions.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"sources":["../src/findVersions.ts"],"sourcesContent":["import type { CollationOptions } from 'mongodb'\nimport type { FindVersions, PayloadRequest } from 'payload'\n\nimport { buildVersionCollectionFields } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getSession } from './getSession.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { findMany } from './utilities/findMany.js'\nimport { getHasNearConstraint } from './utilities/getHasNearConstraint.js'\nimport { transform } from './utilities/transform.js'\n\nexport const findVersions: FindVersions = async function findVersions(\n this: MongooseAdapter,\n {\n collection,\n limit,\n locale,\n page,\n pagination,\n req = {} as PayloadRequest,\n select,\n skip,\n sort: sortArg,\n where,\n },\n) {\n const Model = this.versions[collection]\n const collectionConfig = this.payload.collections[collection].config\n\n const session = await getSession(this, req)\n\n const hasNearConstraint = getHasNearConstraint(where)\n\n let sort\n if (!hasNearConstraint) {\n sort = buildSortParam({\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n locale,\n sort: sortArg || '-updatedAt',\n timestamps: true,\n })\n }\n\n const query = await Model.buildQuery({\n locale,\n payload: this.payload,\n session,\n where,\n })\n\n const versionFields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n // useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.\n const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields: versionFields,\n select,\n })\n\n const collation: CollationOptions | undefined = this.collation\n ? {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : 'en',\n ...this.collation,\n }\n : undefined\n\n const result = await findMany({\n adapter: this,\n collation,\n collection: Model.collection,\n limit,\n page,\n pagination,\n projection,\n query,\n session,\n skip,\n sort,\n useEstimatedCount,\n })\n\n transform({\n adapter: this,\n data: result.docs,\n fields: versionFields,\n operation: 'read',\n })\n\n return result\n}\n"],"names":["buildVersionCollectionFields","getSession","buildSortParam","buildProjectionFromSelect","findMany","getHasNearConstraint","transform","findVersions","collection","limit","locale","page","pagination","req","select","skip","sort","sortArg","where","Model","versions","collectionConfig","payload","collections","config","session","hasNearConstraint","fields","flattenedFields","timestamps","query","buildQuery","versionFields","useEstimatedCount","Object","keys","length","projection","adapter","collation","undefined","result","data","docs","operation"],"mappings":"AAGA,SAASA,4BAA4B,QAAQ,UAAS;AAItD,SAASC,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,yBAAy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AAS,MAAMrC,SAAS;QAC5BkC,SAAS,IAAI;QACbC;QACA/B,YAAYW,MAAMX,UAAU;QAC5BC;QACAE;QACAC;QACAyB;QACAP;QACAL;QACAV;QACAC;QACAiB;IACF;IAEA3B,UAAU;QACRgC,SAAS,IAAI;QACbI,MAAMD,OAAOE,IAAI;QACjBhB,QAAQK;QACRY,WAAW;IACb;IAEA,OAAOH;AACT,EAAC"}
|
|
@@ -5,7 +5,5 @@ import type { MongooseAdapter } from './index.js';
|
|
|
5
5
|
* returns the session belonging to the transaction of the req.session if exists
|
|
6
6
|
* @returns ClientSession
|
|
7
7
|
*/
|
|
8
|
-
export declare function
|
|
9
|
-
|
|
10
|
-
} | Record<string, never>>;
|
|
11
|
-
//# sourceMappingURL=withSession.d.ts.map
|
|
8
|
+
export declare function getSession(db: MongooseAdapter, req: PayloadRequest): Promise<ClientSession | undefined>;
|
|
9
|
+
//# sourceMappingURL=getSession.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getSession.d.ts","sourceRoot":"","sources":["../src/getSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD;;;GAGG;AACH,wBAAsB,UAAU,CAC9B,EAAE,EAAE,eAAe,EACnB,GAAG,EAAE,cAAc,GAClB,OAAO,CAAC,aAAa,GAAG,SAAS,CAAC,CAUpC"}
|
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* returns the session belonging to the transaction of the req.session if exists
|
|
3
3
|
* @returns ClientSession
|
|
4
|
-
*/ export async function
|
|
4
|
+
*/ export async function getSession(db, req) {
|
|
5
5
|
let transactionID = req.transactionID;
|
|
6
6
|
if (transactionID instanceof Promise) {
|
|
7
7
|
transactionID = await req.transactionID;
|
|
8
8
|
}
|
|
9
9
|
if (req) {
|
|
10
|
-
return db.sessions[transactionID]
|
|
11
|
-
session: db.sessions[transactionID]
|
|
12
|
-
} : {};
|
|
10
|
+
return db.sessions[transactionID];
|
|
13
11
|
}
|
|
14
12
|
}
|
|
15
13
|
|
|
16
|
-
//# sourceMappingURL=
|
|
14
|
+
//# sourceMappingURL=getSession.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/getSession.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\nimport type { PayloadRequest } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\n/**\n * returns the session belonging to the transaction of the req.session if exists\n * @returns ClientSession\n */\nexport async function getSession(\n db: MongooseAdapter,\n req: PayloadRequest,\n): Promise<ClientSession | undefined> {\n let transactionID = req.transactionID\n\n if (transactionID instanceof Promise) {\n transactionID = await req.transactionID\n }\n\n if (req) {\n return db.sessions[transactionID]\n }\n}\n"],"names":["getSession","db","req","transactionID","Promise","sessions"],"mappings":"AAKA;;;CAGC,GACD,OAAO,eAAeA,WACpBC,EAAmB,EACnBC,GAAmB;IAEnB,IAAIC,gBAAgBD,IAAIC,aAAa;IAErC,IAAIA,yBAAyBC,SAAS;QACpCD,gBAAgB,MAAMD,IAAIC,aAAa;IACzC;IAEA,IAAID,KAAK;QACP,OAAOD,GAAGI,QAAQ,CAACF,cAAc;IACnC;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,
|
|
1
|
+
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAyB,cAAc,EAAE,MAAM,SAAS,CAAA;AAmGpE,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,CAqFhB"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
const migrateModelWithBatching = async ({
|
|
2
|
+
import { getSession } from '../getSession.js';
|
|
3
|
+
import { transform } from '../utilities/transform.js';
|
|
4
|
+
const migrateModelWithBatching = async ({ adapter, batchSize, fields, Model, session })=>{
|
|
5
5
|
let hasNext = true;
|
|
6
6
|
let skip = 0;
|
|
7
7
|
while(hasNext){
|
|
@@ -19,10 +19,12 @@ const migrateModelWithBatching = async ({ batchSize, config, fields, Model, sess
|
|
|
19
19
|
docs.pop();
|
|
20
20
|
}
|
|
21
21
|
for (const doc of docs){
|
|
22
|
-
|
|
23
|
-
|
|
22
|
+
transform({
|
|
23
|
+
adapter,
|
|
24
24
|
data: doc,
|
|
25
|
-
fields
|
|
25
|
+
fields,
|
|
26
|
+
operation: 'update',
|
|
27
|
+
validateRelationships: false
|
|
26
28
|
});
|
|
27
29
|
}
|
|
28
30
|
await Model.collection.bulkWrite(docs.map((doc)=>({
|
|
@@ -77,13 +79,13 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
77
79
|
const { payload } = req;
|
|
78
80
|
const db = payload.db;
|
|
79
81
|
const config = payload.config;
|
|
80
|
-
const
|
|
82
|
+
const session = await getSession(db, req);
|
|
81
83
|
for (const collection of payload.config.collections.filter(hasRelationshipOrUploadField)){
|
|
82
84
|
payload.logger.info(`Migrating collection "${collection.slug}"`);
|
|
83
85
|
await migrateModelWithBatching({
|
|
86
|
+
adapter: db,
|
|
84
87
|
batchSize,
|
|
85
|
-
|
|
86
|
-
fields: collection.fields,
|
|
88
|
+
fields: collection.flattenedFields,
|
|
87
89
|
Model: db.collections[collection.slug],
|
|
88
90
|
session
|
|
89
91
|
});
|
|
@@ -91,9 +93,9 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
91
93
|
if (collection.versions) {
|
|
92
94
|
payload.logger.info(`Migrating collection versions "${collection.slug}"`);
|
|
93
95
|
await migrateModelWithBatching({
|
|
96
|
+
adapter: db,
|
|
94
97
|
batchSize,
|
|
95
|
-
config,
|
|
96
|
-
fields: buildVersionCollectionFields(config, collection),
|
|
98
|
+
fields: buildVersionCollectionFields(config, collection, true),
|
|
97
99
|
Model: db.versions[collection.slug],
|
|
98
100
|
session
|
|
99
101
|
});
|
|
@@ -113,10 +115,12 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
113
115
|
});
|
|
114
116
|
// in case if the global doesn't exist in the database yet (not saved)
|
|
115
117
|
if (doc) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
+
transform({
|
|
119
|
+
adapter: db,
|
|
118
120
|
data: doc,
|
|
119
|
-
fields: global.
|
|
121
|
+
fields: global.flattenedFields,
|
|
122
|
+
operation: 'update',
|
|
123
|
+
validateRelationships: false
|
|
120
124
|
});
|
|
121
125
|
await GlobalsModel.collection.updateOne({
|
|
122
126
|
globalType: global.slug
|
|
@@ -130,9 +134,9 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
|
130
134
|
if (global.versions) {
|
|
131
135
|
payload.logger.info(`Migrating global versions "${global.slug}"`);
|
|
132
136
|
await migrateModelWithBatching({
|
|
137
|
+
adapter: db,
|
|
133
138
|
batchSize,
|
|
134
|
-
config,
|
|
135
|
-
fields: buildVersionGlobalFields(config, global),
|
|
139
|
+
fields: buildVersionGlobalFields(config, global, true),
|
|
136
140
|
Model: db.versions[global.slug],
|
|
137
141
|
session
|
|
138
142
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field,
|
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, FlattenedField, PayloadRequest } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { getSession } from '../getSession.js'\nimport { transform } from '../utilities/transform.js'\n\nconst migrateModelWithBatching = async ({\n adapter,\n batchSize,\n fields,\n Model,\n session,\n}: {\n adapter: MongooseAdapter\n batchSize: number\n fields: FlattenedField[]\n Model: Model<any>\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, data: doc, fields, operation: 'update', validateRelationships: false })\n }\n\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 (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.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: collection.flattenedFields,\n Model: db.collections[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: buildVersionCollectionFields(config, collection, true),\n Model: db.versions[collection.slug],\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.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = await GlobalsModel.findOne<Record<string, unknown>>(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )\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.flattenedFields,\n operation: 'update',\n validateRelationships: false,\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 if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n await migrateModelWithBatching({\n adapter: db,\n batchSize,\n fields: buildVersionGlobalFields(config, global, true),\n Model: db.versions[global.slug],\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","getSession","transform","migrateModelWithBatching","adapter","batchSize","fields","Model","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","data","operation","validateRelationships","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","db","config","collections","logger","info","slug","flattenedFields","versions","globals","GlobalsModel","global","findOne","globalType","$eq"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,wBAAwB,QAAQ,UAAS;AAIhF,SAASC,UAAU,QAAQ,mBAAkB;AAC7C,SAASC,SAAS,QAAQ,4BAA2B;AAErD,MAAMC,2BAA2B,OAAO,EACtCC,OAAO,EACPC,SAAS,EACTC,MAAM,EACNC,KAAK,EACLC,OAAO,EAOR;IACC,IAAIC,UAAU;IACd,IAAIC,OAAO;IAEX,MAAOD,QAAS;QACd,MAAME,OAAO,MAAMJ,MAAMK,IAAI,CAC3B,CAAC,GACD,CAAC,GACD;YACEC,MAAM;YACNC,OAAOT,YAAY;YACnBG;YACAE;QACF;QAGF,IAAIC,KAAKI,MAAM,KAAK,GAAG;YACrB;QACF;QAEAN,UAAUE,KAAKI,MAAM,GAAGV;QAExB,IAAII,SAAS;YACXE,KAAKK,GAAG;QACV;QAEA,KAAK,MAAMC,OAAON,KAAM;YACtBT,UAAU;gBAAEE;gBAASc,MAAMD;gBAAKX;gBAAQa,WAAW;gBAAUC,uBAAuB;YAAM;QAC5F;QAEA,MAAMb,MAAMc,UAAU,CAACC,SAAS,CAC9BX,KAAKY,GAAG,CAAC,CAACN,MAAS,CAAA;gBACjBO,WAAW;oBACTC,QAAQ;wBAAEC,KAAKT,IAAIS,GAAG;oBAAC;oBACvBC,QAAQ;wBACNC,MAAMX;oBACR;gBACF;YACF,CAAA,IACA;YAAET;QAAQ;QAGZE,QAAQL;IACV;AACF;AAEA,MAAMwB,+BAA+B,CAAC,EAAEvB,MAAM,EAAuB;IACnE,KAAK,MAAMwB,SAASxB,OAAQ;QAC1B,IAAIwB,MAAMC,IAAI,KAAK,kBAAkBD,MAAMC,IAAI,KAAK,UAAU;YAC5D,OAAO;QACT;QAEA,IAAI,YAAYD,OAAO;YACrB,IAAID,6BAA6B;gBAAEvB,QAAQwB,MAAMxB,M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