@payloadcms/db-mongodb 4.0.0-internal.543da65 → 4.0.0-internal.567a487
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 +8 -2
- package/dist/count.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +10 -2
- package/dist/countVersions.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +22 -0
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +16 -3
- 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/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +18 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +24 -5
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +10 -2
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +26 -4
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +8 -2
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +24 -3
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +17 -3
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.spec.js +1 -1
- package/dist/queries/buildSortParam.spec.js.map +1 -1
- package/dist/queries/getBuildQueryPlugin.d.ts.map +1 -1
- package/dist/queries/getLocalizedSortProperty.d.ts.map +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
- package/dist/queries/operatorMap.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +16 -4
- package/dist/queryDrafts.js.map +1 -1
- package/dist/testCredentials.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +0 -1
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +68 -7
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +70 -13
- package/dist/updateJobs.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +26 -2
- package/dist/updateOne.js.map +1 -1
- package/dist/utilities/aggregatePaginate.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +3 -2
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +31 -4
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
- package/dist/utilities/getDBName.d.ts.map +1 -1
- package/dist/utilities/getEntity.d.ts +1 -1
- package/dist/utilities/getEntity.d.ts.map +1 -1
- package/dist/utilities/handleError.d.ts.map +1 -1
- package/dist/utilities/isObjectID.d.ts.map +1 -1
- package/dist/utilities/resolveJoins.d.ts +4 -2
- package/dist/utilities/resolveJoins.d.ts.map +1 -1
- package/dist/utilities/resolveJoins.js +16 -2
- 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/dist/utilities/transform.spec.js +67 -0
- package/dist/utilities/transform.spec.js.map +1 -1
- package/package.json +4 -4
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.spec.ts"],"sourcesContent":["import { describe, beforeAll, it, expect } from 'vitest'\nimport type { Config, SanitizedConfig } from 'payload'\nimport { flattenAllFields, sanitizeConfig } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\nlet config: SanitizedConfig\n\ndescribe('get localized sort property', () => {\n beforeAll(async () => {\n config =
|
|
1
|
+
{"version":3,"sources":["../../src/queries/getLocalizedSortProperty.spec.ts"],"sourcesContent":["import { describe, beforeAll, it, expect } from 'vitest'\nimport type { Config, SanitizedConfig } from 'payload'\nimport { flattenAllFields, sanitizeConfig } from 'payload'\n\nimport { getLocalizedSortProperty } from './getLocalizedSortProperty.js'\n\nlet config: SanitizedConfig\n\ndescribe('get localized sort property', () => {\n beforeAll(async () => {\n config = sanitizeConfig({\n localization: {\n defaultLocale: 'en',\n fallback: true,\n locales: ['en', 'es'],\n },\n } as Config)\n })\n it('passes through a non-localized sort property', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: [\n {\n name: 'title',\n type: 'text',\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title')\n })\n\n it('properly localizes an un-localized sort property', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n\n it('keeps specifically asked-for localized sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n locale: 'en',\n segments: ['title', 'es'],\n })\n\n expect(result).toStrictEqual('title.es')\n })\n\n it('properly localizes nested sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: flattenAllFields({\n fields: [\n {\n name: 'group',\n type: 'group',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n }),\n locale: 'en',\n segments: ['group', 'title'],\n })\n\n expect(result).toStrictEqual('group.title.en')\n })\n\n it('keeps requested locale with nested sort properties', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: flattenAllFields({\n fields: [\n {\n name: 'group',\n type: 'group',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n }),\n locale: 'en',\n segments: ['group', 'title', 'es'],\n })\n\n expect(result).toStrictEqual('group.title.es')\n })\n\n it('properly localizes field within row', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: flattenAllFields({\n fields: [\n {\n type: 'row',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n }),\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n\n it('properly localizes field within named tab', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: flattenAllFields({\n fields: [\n {\n type: 'tabs',\n tabs: [\n {\n name: 'tab',\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n },\n ],\n },\n ],\n }),\n locale: 'en',\n segments: ['tab', 'title'],\n })\n\n expect(result).toStrictEqual('tab.title.en')\n })\n\n it('properly localizes field within unnamed tab', () => {\n const result = getLocalizedSortProperty({\n config,\n parentIsLocalized: false,\n fields: flattenAllFields({\n fields: [\n {\n type: 'tabs',\n tabs: [\n {\n fields: [\n {\n name: 'title',\n type: 'text',\n localized: true,\n },\n ],\n label: 'Tab',\n },\n ],\n },\n ],\n }),\n locale: 'en',\n segments: ['title'],\n })\n\n expect(result).toStrictEqual('title.en')\n })\n})\n"],"names":["describe","beforeAll","it","expect","flattenAllFields","sanitizeConfig","getLocalizedSortProperty","config","localization","defaultLocale","fallback","locales","result","parentIsLocalized","fields","name","type","locale","segments","toStrictEqual","localized","tabs","label"],"mappings":"AAAA,SAASA,QAAQ,EAAEC,SAAS,EAAEC,EAAE,EAAEC,MAAM,QAAQ,SAAQ;AAExD,SAASC,gBAAgB,EAAEC,cAAc,QAAQ,UAAS;AAE1D,SAASC,wBAAwB,QAAQ,gCAA+B;AAExE,IAAIC;AAEJP,SAAS,+BAA+B;IACtCC,UAAU;QACRM,SAASF,eAAe;YACtBG,cAAc;gBACZC,eAAe;gBACfC,UAAU;gBACVC,SAAS;oBAAC;oBAAM;iBAAK;YACvB;QACF;IACF;IACAT,GAAG,gDAAgD;QACjD,MAAMU,SAASN,yBAAyB;YACtCC;YACAM,mBAAmB;YACnBC,QAAQ;gBACN;oBACEC,MAAM;oBACNC,MAAM;gBACR;aACD;YACDC,QAAQ;YACRC,UAAU;gBAAC;aAAQ;QACrB;QAEAf,OAAOS,QAAQO,aAAa,CAAC;IAC/B;IAEAjB,GAAG,oDAAoD;QACrD,MAAMU,SAASN,yBAAyB;YACtCC;YACAM,mBAAmB;YACnBC,QAAQ;gBACN;oBACEC,MAAM;oBACNC,MAAM;oBACNI,WAAW;gBACb;aACD;YACDH,QAAQ;YACRC,UAAU;gBAAC;aAAQ;QACrB;QAEAf,OAAOS,QAAQO,aAAa,CAAC;IAC/B;IAEAjB,GAAG,0DAA0D;QAC3D,MAAMU,SAASN,yBAAyB;YACtCC;YACAM,mBAAmB;YACnBC,QAAQ;gBACN;oBACEC,MAAM;oBACNC,MAAM;oBACNI,WAAW;gBACb;aACD;YACDH,QAAQ;YACRC,UAAU;gBAAC;gBAAS;aAAK;QAC3B;QAEAf,OAAOS,QAAQO,aAAa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H;iBACD;YACH;YACAH,QAAQ;YACRC,UAAU;gBAAC;gBAAO;aAAQ;QAC5B;QAEAf,OAAOS,QAAQO,aAAa,CAAC;IAC/B;IAEAjB,GAAG,+CAA+C;QAChD,MAAMU,SAASN,yBAAyB;YACtCC;YACAM,mBAAmB;YACnBC,QAAQV,iBAAiB;gBACvBU,QAAQ;oBACN;wBACEE,MAAM;wBACNK,MAAM;4BACJ;gCACEP,QAAQ;oCACN;wCACEC,MAAM;wCACNC,MAAM;wCACNI,WAAW;oCACb;iCACD;gCACDE,OAAO;4BACT;yBACD;oBACH;iBACD;YACH;YACAL,QAAQ;YACRC,UAAU;gBAAC;aAAQ;QACrB;QAEAf,OAAOS,QAAQO,aAAa,CAAC;IAC/B;AACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operatorMap.d.ts","sourceRoot":"","sources":["../../src/queries/operatorMap.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,OAAO,WAAW,CAAA;AAErD,eAAO,MAAM,WAAW
|
|
1
|
+
{"version":3,"file":"operatorMap.d.ts","sourceRoot":"","sources":["../../src/queries/operatorMap.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,MAAM,OAAO,WAAW,CAAA;AAErD,eAAO,MAAM,WAAW;IACtB,GAAG;IACH,MAAM;IACN,MAAM;IACN,YAAY;IACZ,kBAAkB;IAClB,EAAE;IACF,UAAU;IACV,SAAS;IACT,eAAe;IACf,IAAI;IACJ,UAAU;IACV,MAAM;IACN,MAAM;CACP,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,cAAc,EAAE,QAAQ,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAA;AAMnG,KAAK,sBAAsB,GAAG;IAC5B,KAAK,EAAE,cAAc,CAAA;IACrB,WAAW,EAAE,OAAO,CAAA;IACpB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,QAAQ,CAAA;IAClB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAmED,eAAO,MAAM,kBAAkB,
|
|
1
|
+
{"version":3,"file":"sanitizeQueryValue.d.ts","sourceRoot":"","sources":["../../src/queries/sanitizeQueryValue.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,cAAc,EAAE,QAAQ,EAAE,OAAO,EAAqB,MAAM,SAAS,CAAA;AAMnG,KAAK,sBAAsB,GAAG;IAC5B,KAAK,EAAE,cAAc,CAAA;IACrB,WAAW,EAAE,OAAO,CAAA;IACpB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,QAAQ,CAAA;IAClB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,OAAO,CAAA;IAChB,GAAG,EAAE,GAAG,CAAA;CACT,CAAA;AAmED,eAAO,MAAM,kBAAkB,qFAS5B,sBAAsB,KACrB;IACE,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,GAAG,CAAC,EAAE,OAAO,CAAA;CACd,GACD,SAycH,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"queryDrafts.d.ts","sourceRoot":"","sources":["../src/queryDrafts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"queryDrafts.d.ts","sourceRoot":"","sources":["../src/queryDrafts.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,SAAS,CAAA;AAuB1C,eAAO,MAAM,WAAW,EAAE,WAwLzB,CAAA"}
|
package/dist/queryDrafts.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildVersionCollectionFields, combineQueries, flattenWhereToOperators } from 'payload';
|
|
1
|
+
import { buildVersionCollectionFields, combineQueries, flattenWhereToOperators, projectBranchVersionParent, resolveBranchVersionQuery, withBranchVersionSelect } from 'payload';
|
|
2
2
|
import { buildQuery } from './queries/buildQuery.js';
|
|
3
3
|
import { buildSortParam } from './queries/buildSortParam.js';
|
|
4
4
|
import { aggregatePaginate } from './utilities/aggregatePaginate.js';
|
|
@@ -34,11 +34,16 @@ export const queryDrafts = async function queryDrafts({ collection: collectionSl
|
|
|
34
34
|
versions: true
|
|
35
35
|
});
|
|
36
36
|
}
|
|
37
|
+
const branchedWhere = await resolveBranchVersionQuery({
|
|
38
|
+
collectionSlug,
|
|
39
|
+
req,
|
|
40
|
+
where
|
|
41
|
+
});
|
|
37
42
|
const combinedWhere = combineQueries({
|
|
38
43
|
latest: {
|
|
39
44
|
equals: true
|
|
40
45
|
}
|
|
41
|
-
},
|
|
46
|
+
}, branchedWhere ?? {});
|
|
42
47
|
const versionQuery = await buildQuery({
|
|
43
48
|
adapter: this,
|
|
44
49
|
fields,
|
|
@@ -48,7 +53,11 @@ export const queryDrafts = async function queryDrafts({ collection: collectionSl
|
|
|
48
53
|
const projection = buildProjectionFromSelect({
|
|
49
54
|
adapter: this,
|
|
50
55
|
fields,
|
|
51
|
-
select
|
|
56
|
+
select: withBranchVersionSelect({
|
|
57
|
+
collectionSlug,
|
|
58
|
+
req,
|
|
59
|
+
select
|
|
60
|
+
})
|
|
52
61
|
});
|
|
53
62
|
const session = await getSession(this, req);
|
|
54
63
|
const options = {
|
|
@@ -103,6 +112,7 @@ export const queryDrafts = async function queryDrafts({ collection: collectionSl
|
|
|
103
112
|
locale,
|
|
104
113
|
projection,
|
|
105
114
|
query: versionQuery,
|
|
115
|
+
req,
|
|
106
116
|
versions: true
|
|
107
117
|
});
|
|
108
118
|
if (aggregate || sortAggregation.length > 0) {
|
|
@@ -141,7 +151,9 @@ export const queryDrafts = async function queryDrafts({ collection: collectionSl
|
|
|
141
151
|
operation: 'read'
|
|
142
152
|
});
|
|
143
153
|
for(let i = 0; i < result.docs.length; i++){
|
|
144
|
-
|
|
154
|
+
// A branch version's `parent` is the shadow row's primary key, so the
|
|
155
|
+
// canonical document ID comes from `_branchParent` when present.
|
|
156
|
+
const id = projectBranchVersionParent(result.docs[i]);
|
|
145
157
|
result.docs[i] = result.docs[i].version ?? {};
|
|
146
158
|
result.docs[i].id = id;
|
|
147
159
|
}
|
package/dist/queryDrafts.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/queryDrafts.ts"],"sourcesContent":["import type { PaginateOptions, PipelineStage, QueryOptions } from 'mongoose'\nimport type { QueryDrafts } from 'payload'\n\nimport { buildVersionCollectionFields, combineQueries, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { aggregatePaginate } from './utilities/aggregatePaginate.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { resolveJoins } from './utilities/resolveJoins.js'\nimport { transform } from './utilities/transform.js'\n\nexport const queryDrafts: QueryDrafts = async function queryDrafts(\n this: MongooseAdapter,\n {\n collection: collectionSlug,\n joins,\n limit,\n locale,\n page,\n pagination,\n req,\n select,\n sort: sortArg,\n where = {},\n },\n) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug,\n versions: true,\n })\n\n let hasNearConstraint\n let sort\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n\n const sortAggregation: PipelineStage[] = []\n if (!hasNearConstraint) {\n sort = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields,\n locale,\n sort: sortArg || collectionConfig.defaultSort,\n sortAggregation,\n timestamps: true,\n versions: true,\n })\n }\n\n const combinedWhere = combineQueries({ latest: { equals: true } }, where)\n\n const versionQuery = await buildQuery({\n adapter: this,\n fields,\n locale,\n where: combinedWhere,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields,\n select,\n })\n\n const session = await getSession(this, req)\n const options: QueryOptions = {\n session,\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 =\n hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n options,\n page,\n pagination,\n projection,\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const localizationConfig = this.payload.config.localization\n const defaultLocale =\n (typeof localizationConfig === 'object' && localizationConfig?.defaultLocale) || 'en'\n\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (\n !useEstimatedCount &&\n Object.keys(versionQuery).length === 0 &&\n this.disableIndexHints !== true\n ) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(versionQuery, {\n collation: paginationOptions.collation,\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit && limit > 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n\n paginationOptions.options!.limit = limit\n }\n\n let result\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection: collectionSlug,\n collectionConfig,\n joins,\n locale,\n projection,\n query: versionQuery,\n versions: true,\n })\n\n if (aggregate || sortAggregation.length > 0) {\n result = await aggregatePaginate({\n adapter: this,\n collation: paginationOptions.collation,\n joinAggregation: aggregate,\n limit: paginationOptions.limit,\n Model,\n page: paginationOptions.page,\n pagination: paginationOptions.pagination,\n projection: paginationOptions.projection,\n query: versionQuery,\n session: paginationOptions.options?.session ?? undefined,\n sort: paginationOptions.sort as object,\n sortAggregation,\n useEstimatedCount: paginationOptions.useEstimatedCount,\n })\n } else {\n result = await Model.paginate(versionQuery, paginationOptions)\n }\n\n if (!this.useJoinAggregations) {\n await resolveJoins({\n adapter: this,\n collectionSlug,\n docs: result.docs as Record<string, unknown>[],\n joins,\n locale,\n versions: true,\n })\n }\n\n transform({\n adapter: this,\n data: result.docs,\n fields: buildVersionCollectionFields(this.payload.config, collectionConfig),\n operation: 'read',\n })\n\n for (let i = 0; i < result.docs.length; i++) {\n const id = result.docs[i].parent\n result.docs[i] = result.docs[i].version ?? {}\n result.docs[i].id = id\n }\n\n return result\n}\n"],"names":["buildVersionCollectionFields","combineQueries","flattenWhereToOperators","buildQuery","buildSortParam","aggregatePaginate","buildJoinAggregation","buildProjectionFromSelect","getCollection","getSession","resolveJoins","transform","queryDrafts","collection","collectionSlug","joins","limit","locale","page","pagination","req","select","sort","sortArg","where","collectionConfig","Model","adapter","versions","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","payload","config","sortAggregation","defaultSort","timestamps","combinedWhere","latest","equals","versionQuery","projection","session","options","useEstimatedCount","length","paginationOptions","lean","leanWithId","collation","localizationConfig","localization","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","aggregate","query","joinAggregation","undefined","paginate","useJoinAggregations","docs","data","operation","i","id","parent","version"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,cAAc,EAAEC,uBAAuB,QAAQ,UAAS;AAI/F,SAASC,UAAU,QAAQ,0BAAyB;AACpD,S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ent":["import type { PaginateOptions, PipelineStage, QueryOptions } from 'mongoose'\nimport type { QueryDrafts } from 'payload'\n\nimport {\n buildVersionCollectionFields,\n combineQueries,\n flattenWhereToOperators,\n projectBranchVersionParent,\n resolveBranchVersionQuery,\n withBranchVersionSelect,\n} from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { aggregatePaginate } from './utilities/aggregatePaginate.js'\nimport { buildJoinAggregation } from './utilities/buildJoinAggregation.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { resolveJoins } from './utilities/resolveJoins.js'\nimport { transform } from './utilities/transform.js'\n\nexport const queryDrafts: QueryDrafts = async function queryDrafts(\n this: MongooseAdapter,\n {\n collection: collectionSlug,\n joins,\n limit,\n locale,\n page,\n pagination,\n req,\n select,\n sort: sortArg,\n where = {},\n },\n) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug,\n versions: true,\n })\n\n let hasNearConstraint\n let sort\n\n if (where) {\n const constraints = flattenWhereToOperators(where)\n hasNearConstraint = constraints.some((prop) => Object.keys(prop).some((key) => key === 'near'))\n }\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig, true)\n\n const sortAggregation: PipelineStage[] = []\n if (!hasNearConstraint) {\n sort = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields,\n locale,\n sort: sortArg || collectionConfig.defaultSort,\n sortAggregation,\n timestamps: true,\n versions: true,\n })\n }\n\n const branchedWhere = await resolveBranchVersionQuery({\n collectionSlug,\n req,\n where,\n })\n\n const combinedWhere = combineQueries({ latest: { equals: true } }, branchedWhere ?? {})\n\n const versionQuery = await buildQuery({\n adapter: this,\n fields,\n locale,\n where: combinedWhere,\n })\n\n const projection = buildProjectionFromSelect({\n adapter: this,\n fields,\n select: withBranchVersionSelect({ collectionSlug, req, select }),\n })\n\n const session = await getSession(this, req)\n const options: QueryOptions = {\n session,\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 =\n hasNearConstraint || !versionQuery || Object.keys(versionQuery).length === 0\n const paginationOptions: PaginateOptions = {\n lean: true,\n leanWithId: true,\n options,\n page,\n pagination,\n projection,\n sort,\n useEstimatedCount,\n }\n\n if (this.collation) {\n const localizationConfig = this.payload.config.localization\n const defaultLocale =\n (typeof localizationConfig === 'object' && localizationConfig?.defaultLocale) || 'en'\n\n paginationOptions.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (\n !useEstimatedCount &&\n Object.keys(versionQuery).length === 0 &&\n this.disableIndexHints !== true\n ) {\n // Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding\n // a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,\n // which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses\n // the correct indexed field\n paginationOptions.useCustomCountFn = () => {\n return Promise.resolve(\n Model.countDocuments(versionQuery, {\n collation: paginationOptions.collation,\n hint: { _id: 1 },\n session,\n }),\n )\n }\n }\n\n if (limit && limit > 0) {\n paginationOptions.limit = limit\n // limit must also be set here, it's ignored when pagination is false\n\n paginationOptions.options!.limit = limit\n }\n\n let result\n\n const aggregate = await buildJoinAggregation({\n adapter: this,\n collection: collectionSlug,\n collectionConfig,\n joins,\n locale,\n projection,\n query: versionQuery,\n req,\n versions: true,\n })\n\n if (aggregate || sortAggregation.length > 0) {\n result = await aggregatePaginate({\n adapter: this,\n collation: paginationOptions.collation,\n joinAggregation: aggregate,\n limit: paginationOptions.limit,\n Model,\n page: paginationOptions.page,\n pagination: paginationOptions.pagination,\n projection: paginationOptions.projection,\n query: versionQuery,\n session: paginationOptions.options?.session ?? undefined,\n sort: paginationOptions.sort as object,\n sortAggregation,\n useEstimatedCount: paginationOptions.useEstimatedCount,\n })\n } else {\n result = await Model.paginate(versionQuery, paginationOptions)\n }\n\n if (!this.useJoinAggregations) {\n await resolveJoins({\n adapter: this,\n collectionSlug,\n docs: result.docs as Record<string, unknown>[],\n joins,\n locale,\n versions: true,\n })\n }\n\n transform({\n adapter: this,\n data: result.docs,\n fields: buildVersionCollectionFields(this.payload.config, collectionConfig),\n operation: 'read',\n })\n\n for (let i = 0; i < result.docs.length; i++) {\n // A branch version's `parent` is the shadow row's primary key, so the\n // canonical document ID comes from `_branchParent` when present.\n const id = projectBranchVersionParent(result.docs[i] as Record<string, unknown>)\n result.docs[i] = result.docs[i].version ?? {}\n result.docs[i].id = id\n }\n\n return result\n}\n"],"names":["buildVersionCollectionFields","combineQueries","flattenWhereToOperators","projectBranchVersionParent","resolveBranchVersionQuery","withBranchVersionSelect","buildQuery","buildSortParam","aggregatePaginate","buildJoinAggregation","buildProjectionFromSelect","getCollection","getSession","resolveJoins","transform","queryDrafts","collection","collectionSlug","joins","limit","locale","page","pagination","req","select","sort","sortArg","where","collectionConfig","Model","adapter","versions","hasNearConstraint","constraints","some","prop","Object","keys","key","fields","payload","config","sortAggregation","defaultSort","timestamps","branchedWhere","combinedWhere","latest","equals","versionQuery","projection","session","options","useEstimatedCount","length","paginationOptions","lean","leanWithId","collation","localizationConfig","localization","defaultLocale","disableIndexHints","useCustomCountFn","Promise","resolve","countDocuments","hint","_id","result","aggregate","query","joinAggregation","undefined","paginate","useJoinAggregations","docs","data","operation","i","id","version"],"mappings":"AAGA,SACEA,4BAA4B,EAC5BC,cAAc,EACdC,uBAAuB,EACvBC,0BAA0B,EAC1BC,yBAAyB,EACzBC,uBAAuB,QAClB,UAAS;AAIhB,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,iBAAiB,QAAQ,mCAAkC;AACpE,SAASC,oBAAoB,QAAQ,sCAAqC;AAC1E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,aA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testCredentials.d.ts","sourceRoot":"","sources":["../src/testCredentials.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,kBAAkB,CAAA;AACpC,eAAO,MAAM,QAAQ,YAAY,CAAA;AACjC,eAAO,MAAM,UAAU
|
|
1
|
+
{"version":3,"file":"testCredentials.d.ts","sourceRoot":"","sources":["../src/testCredentials.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,kBAAkB,CAAA;AACpC,eAAO,MAAM,QAAQ,YAAY,CAAA;AACjC,eAAO,MAAM,UAAU;IACrB,IAAI;IACJ,IAAI;IACJ,GAAG;CACJ,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"beginTransaction.d.ts","sourceRoot":"","sources":["../../src/transactions/beginTransaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAU/C,eAAO,MAAM,gBAAgB,EAAE,
|
|
1
|
+
{"version":3,"file":"beginTransaction.d.ts","sourceRoot":"","sources":["../../src/transactions/beginTransaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAU/C,eAAO,MAAM,gBAAgB,EAAE,gBAqB9B,CAAA"}
|
|
@@ -10,7 +10,6 @@ export const beginTransaction = async function beginTransaction(options) {
|
|
|
10
10
|
const client = this.connection.getClient();
|
|
11
11
|
const id = uuid();
|
|
12
12
|
if (!this.sessions[id]) {
|
|
13
|
-
// @ts-expect-error BaseDatabaseAdapter and MongoosAdapter (that extends Base) sessions aren't compatible.
|
|
14
13
|
this.sessions[id] = client.startSession();
|
|
15
14
|
}
|
|
16
15
|
if (this.sessions[id]?.inTransaction()) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/transactions/beginTransaction.ts"],"sourcesContent":["import type { TransactionOptions } from 'mongodb'\nimport type { BeginTransaction } from 'payload'\n\nimport { APIError } from 'payload'\nimport { v4 as uuid } from 'uuid'\n\nimport type { MongooseAdapter } from '../index.js'\n\n// Needs await to fulfill the interface\n// @ts-expect-error TransactionOptions isn't compatible with BeginTransaction of the DatabaseAdapter interface.\n// eslint-disable-next-line @typescript-eslint/require-await\nexport const beginTransaction: BeginTransaction = async function beginTransaction(\n this: MongooseAdapter,\n options: TransactionOptions,\n) {\n if (!this.connection) {\n throw new APIError('beginTransaction called while no connection to the database exists')\n }\n\n const client = this.connection.getClient()\n const id = uuid()\n\n if (!this.sessions[id]) {\n
|
|
1
|
+
{"version":3,"sources":["../../src/transactions/beginTransaction.ts"],"sourcesContent":["import type { TransactionOptions } from 'mongodb'\nimport type { BeginTransaction } from 'payload'\n\nimport { APIError } from 'payload'\nimport { v4 as uuid } from 'uuid'\n\nimport type { MongooseAdapter } from '../index.js'\n\n// Needs await to fulfill the interface\n// @ts-expect-error TransactionOptions isn't compatible with BeginTransaction of the DatabaseAdapter interface.\n// eslint-disable-next-line @typescript-eslint/require-await\nexport const beginTransaction: BeginTransaction = async function beginTransaction(\n this: MongooseAdapter,\n options: TransactionOptions,\n) {\n if (!this.connection) {\n throw new APIError('beginTransaction called while no connection to the database exists')\n }\n\n const client = this.connection.getClient()\n const id = uuid()\n\n if (!this.sessions[id]) {\n this.sessions[id] = client.startSession()\n }\n if (this.sessions[id]?.inTransaction()) {\n this.payload.logger.warn('beginTransaction called while transaction already exists')\n } else {\n this.sessions[id]?.startTransaction(options || (this.transactionOptions as TransactionOptions))\n }\n\n return id\n}\n"],"names":["APIError","v4","uuid","beginTransaction","options","connection","client","getClient","id","sessions","startSession","inTransaction","payload","logger","warn","startTransaction","transactionOptions"],"mappings":"AAGA,SAASA,QAAQ,QAAQ,UAAS;AAClC,SAASC,MAAMC,IAAI,QAAQ,OAAM;AAIjC,uCAAuC;AACvC,+GAA+G;AAC/G,4DAA4D;AAC5D,OAAO,MAAMC,mBAAqC,eAAeA,iBAE/DC,OAA2B;IAE3B,IAAI,CAAC,IAAI,CAACC,UAAU,EAAE;QACpB,MAAM,IAAIL,SAAS;IACrB;IAEA,MAAMM,SAAS,IAAI,CAACD,UAAU,CAACE,SAAS;IACxC,MAAMC,KAAKN;IAEX,IAAI,CAAC,IAAI,CAACO,QAAQ,CAACD,GAAG,EAAE;QACtB,IAAI,CAACC,QAAQ,CAACD,GAAG,GAAGF,OAAOI,YAAY;IACzC;IACA,IAAI,IAAI,CAACD,QAAQ,CAACD,GAAG,EAAEG,iBAAiB;QACtC,IAAI,CAACC,OAAO,CAACC,MAAM,CAACC,IAAI,CAAC;IAC3B,OAAO;QACL,IAAI,CAACL,QAAQ,CAACD,GAAG,EAAEO,iBAAiBX,WAAY,IAAI,CAACY,kBAAkB;IACzE;IAEA,OAAOR;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateGlobal.d.ts","sourceRoot":"","sources":["../src/updateGlobal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"updateGlobal.d.ts","sourceRoot":"","sources":["../src/updateGlobal.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,MAAM,SAAS,CAAA;AAW3C,eAAO,MAAM,YAAY,EAAE,YAgF1B,CAAA"}
|
package/dist/updateGlobal.js
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { recordBranchGlobalChange, resolveBranchGlobalWrite } from 'payload';
|
|
1
2
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
2
3
|
import { getGlobal } from './utilities/getEntity.js';
|
|
3
4
|
import { getSession } from './utilities/getSession.js';
|
|
4
5
|
import { transform } from './utilities/transform.js';
|
|
5
|
-
export const updateGlobal = async function updateGlobal({ slug: globalSlug, data, options: optionsArgs = {}, req, returning, select }) {
|
|
6
|
+
export const updateGlobal = async function updateGlobal({ slug: globalSlug, branch, data, options: optionsArgs = {}, req, returning, select }) {
|
|
6
7
|
const { globalConfig, Model } = getGlobal({
|
|
7
8
|
adapter: this,
|
|
8
9
|
globalSlug
|
|
@@ -31,15 +32,75 @@ export const updateGlobal = async function updateGlobal({ slug: globalSlug, data
|
|
|
31
32
|
select
|
|
32
33
|
})
|
|
33
34
|
};
|
|
35
|
+
const writeBranch = resolveBranchGlobalWrite({
|
|
36
|
+
branch,
|
|
37
|
+
globalSlug,
|
|
38
|
+
req
|
|
39
|
+
});
|
|
40
|
+
// On a branch the write targets that branch's own row, upserting it from
|
|
41
|
+
// main's current content the first time the global is touched.
|
|
42
|
+
const filter = writeBranch ? {
|
|
43
|
+
_branch: {
|
|
44
|
+
$eq: writeBranch
|
|
45
|
+
},
|
|
46
|
+
globalType: globalSlug
|
|
47
|
+
} : {
|
|
48
|
+
globalType: globalSlug
|
|
49
|
+
};
|
|
50
|
+
if (writeBranch) {
|
|
51
|
+
const existing = await Model.findOne(filter, {}, baseOptions).lean();
|
|
52
|
+
if (!existing) {
|
|
53
|
+
const mainDoc = await Model.findOne({
|
|
54
|
+
_branch: {
|
|
55
|
+
$eq: 'main'
|
|
56
|
+
},
|
|
57
|
+
globalType: globalSlug
|
|
58
|
+
}, {}, baseOptions).lean();
|
|
59
|
+
const { _id, __v, ...mainData } = mainDoc ?? {};
|
|
60
|
+
await Model.create([
|
|
61
|
+
{
|
|
62
|
+
...mainData,
|
|
63
|
+
...data,
|
|
64
|
+
_branch: writeBranch
|
|
65
|
+
}
|
|
66
|
+
], baseOptions, req);
|
|
67
|
+
await recordBranchGlobalChange({
|
|
68
|
+
branch: writeBranch,
|
|
69
|
+
globalSlug,
|
|
70
|
+
req
|
|
71
|
+
});
|
|
72
|
+
if (returning === false) {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
const created = await Model.findOne(filter, findOptions.projection, findOptions);
|
|
76
|
+
transform({
|
|
77
|
+
adapter: this,
|
|
78
|
+
data: created,
|
|
79
|
+
fields,
|
|
80
|
+
globalSlug,
|
|
81
|
+
operation: 'read'
|
|
82
|
+
});
|
|
83
|
+
return created;
|
|
84
|
+
}
|
|
85
|
+
await recordBranchGlobalChange({
|
|
86
|
+
branch: writeBranch,
|
|
87
|
+
globalSlug,
|
|
88
|
+
req
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
// `_branch` is forced rather than taken from `data`. The incoming document is a
|
|
92
|
+
// round-trip of a read, and `_branch` is stripped from reads — so the field's
|
|
93
|
+
// `main` default silently refills it, and writing that back would flip the
|
|
94
|
+
// branch's row onto main and leave two rows claiming to be production.
|
|
95
|
+
const writeData = writeBranch ? {
|
|
96
|
+
...data,
|
|
97
|
+
_branch: writeBranch
|
|
98
|
+
} : data;
|
|
34
99
|
if (returning === false) {
|
|
35
|
-
await Model.updateOne(
|
|
36
|
-
globalType: globalSlug
|
|
37
|
-
}, data, baseOptions);
|
|
100
|
+
await Model.updateOne(filter, writeData, baseOptions);
|
|
38
101
|
return null;
|
|
39
102
|
}
|
|
40
|
-
const result = await Model.findOneAndUpdate(
|
|
41
|
-
globalType: globalSlug
|
|
42
|
-
}, data, findOptions);
|
|
103
|
+
const result = await Model.findOneAndUpdate(filter, writeData, findOptions);
|
|
43
104
|
transform({
|
|
44
105
|
adapter: this,
|
|
45
106
|
data: result,
|
package/dist/updateGlobal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateGlobal.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateGlobal } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateGlobal: UpdateGlobal = async function updateGlobal(\n this: MongooseAdapter,\n { slug: globalSlug, data, options: optionsArgs = {}, req, returning, select },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug })\n\n const fields = globalConfig.fields\n\n transform({ adapter: this, data, fields, globalSlug, operation: 'write' })\n\n const baseOptions = {\n ...optionsArgs,\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n } satisfies QueryOptions\n\n const findOptions: QueryOptions = {\n ...baseOptions,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: globalConfig.flattenedFields,\n select,\n }),\n }\n\n
|
|
1
|
+
{"version":3,"sources":["../src/updateGlobal.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateGlobal } from 'payload'\n\nimport { recordBranchGlobalChange, resolveBranchGlobalWrite } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateGlobal: UpdateGlobal = async function updateGlobal(\n this: MongooseAdapter,\n { slug: globalSlug, branch, data, options: optionsArgs = {}, req, returning, select },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug })\n\n const fields = globalConfig.fields\n\n transform({ adapter: this, data, fields, globalSlug, operation: 'write' })\n\n const baseOptions = {\n ...optionsArgs,\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n } satisfies QueryOptions\n\n const findOptions: QueryOptions = {\n ...baseOptions,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: globalConfig.flattenedFields,\n select,\n }),\n }\n\n const writeBranch = resolveBranchGlobalWrite({ branch, globalSlug, req })\n\n // On a branch the write targets that branch's own row, upserting it from\n // main's current content the first time the global is touched.\n const filter = writeBranch\n ? { _branch: { $eq: writeBranch }, globalType: globalSlug }\n : { globalType: globalSlug }\n\n if (writeBranch) {\n const existing = await Model.findOne(filter, {}, baseOptions).lean()\n\n if (!existing) {\n const mainDoc: any = await Model.findOne(\n { _branch: { $eq: 'main' }, globalType: globalSlug },\n {},\n baseOptions,\n ).lean()\n const { _id, __v, ...mainData } = (mainDoc ?? {}) as Record<string, unknown>\n\n await Model.create([{ ...mainData, ...data, _branch: writeBranch }], baseOptions, req)\n await recordBranchGlobalChange({ branch: writeBranch, globalSlug, req })\n\n if (returning === false) {\n return null\n }\n\n const created: any = await Model.findOne(filter, findOptions.projection, findOptions)\n\n transform({ adapter: this, data: created, fields, globalSlug, operation: 'read' })\n\n return created\n }\n\n await recordBranchGlobalChange({ branch: writeBranch, globalSlug, req })\n }\n\n // `_branch` is forced rather than taken from `data`. The incoming document is a\n // round-trip of a read, and `_branch` is stripped from reads — so the field's\n // `main` default silently refills it, and writing that back would flip the\n // branch's row onto main and leave two rows claiming to be production.\n const writeData = writeBranch ? { ...(data as object), _branch: writeBranch } : data\n\n if (returning === false) {\n await Model.updateOne(filter, writeData, baseOptions)\n return null\n }\n\n const result: any = await Model.findOneAndUpdate(filter, writeData, findOptions)\n\n transform({ adapter: this, data: result, fields, globalSlug, operation: 'read' })\n\n return result\n}\n"],"names":["recordBranchGlobalChange","resolveBranchGlobalWrite","buildProjectionFromSelect","getGlobal","getSession","transform","updateGlobal","slug","globalSlug","branch","data","options","optionsArgs","req","returning","select","globalConfig","Model","adapter","fields","operation","baseOptions","session","timestamps","findOptions","lean","new","projection","flattenedFields","writeBranch","filter","_branch","$eq","globalType","existing","findOne","mainDoc","_id","__v","mainData","create","created","writeData","updateOne","result","findOneAndUpdate"],"mappings":"AAGA,SAASA,wBAAwB,EAAEC,wBAAwB,QAAQ,UAAS;AAI5E,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,SAAS,QAAQ,2BAA0B;AACpD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,eAA6B,eAAeA,aAEvD,EAAEC,MAAMC,UAAU,EAAEC,MAAM,EAAEC,IAAI,EAAEC,SAASC,cAAc,CAAC,CAAC,EAAEC,GAAG,EAAEC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AAO;gBACvB,OAAO;YACT;YAEA,MAAM2B,UAAe,MAAMxB,MAAMkB,OAAO,CAACL,QAAQN,YAAYG,UAAU,EAAEH;YAEzEnB,UAAU;gBAAEa,SAAS,IAAI;gBAAER,MAAM+B;gBAAStB;gBAAQX;gBAAYY,WAAW;YAAO;YAEhF,OAAOqB;QACT;QAEA,MAAMzC,yBAAyB;YAAES,QAAQoB;YAAarB;YAAYK;QAAI;IACxE;IAEA,gFAAgF;IAChF,8EAA8E;IAC9E,2EAA2E;IAC3E,uEAAuE;IACvE,MAAM6B,YAAYb,cAAc;QAAE,GAAInB,IAAI;QAAaqB,SAASF;IAAY,IAAInB;IAEhF,IAAII,cAAc,OAAO;QACvB,MAAMG,MAAM0B,SAAS,CAACb,QAAQY,WAAWrB;QACzC,OAAO;IACT;IAEA,MAAMuB,SAAc,MAAM3B,MAAM4B,gBAAgB,CAACf,QAAQY,WAAWlB;IAEpEnB,UAAU;QAAEa,SAAS,IAAI;QAAER,MAAMkC;QAAQzB;QAAQX;QAAYY,WAAW;IAAO;IAE/E,OAAOwB;AACT,EAAC"}
|
package/dist/updateJobs.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateJobs.d.ts","sourceRoot":"","sources":["../src/updateJobs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAO,UAAU,EAAS,MAAM,SAAS,CAAA;AAWrD,eAAO,MAAM,UAAU,EAAE,
|
|
1
|
+
{"version":3,"file":"updateJobs.d.ts","sourceRoot":"","sources":["../src/updateJobs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAO,UAAU,EAAS,MAAM,SAAS,CAAA;AAWrD,eAAO,MAAM,UAAU,EAAE,UAiLxB,CAAA"}
|
package/dist/updateJobs.js
CHANGED
|
@@ -24,7 +24,7 @@ export const updateJobs = async function updateMany({ id, data, limit, req, retu
|
|
|
24
24
|
sort: sortArg || collectionConfig.defaultSort,
|
|
25
25
|
timestamps: true
|
|
26
26
|
});
|
|
27
|
-
|
|
27
|
+
const query = await buildQuery({
|
|
28
28
|
adapter: this,
|
|
29
29
|
collectionSlug: collectionConfig.slug,
|
|
30
30
|
fields: collectionConfig.flattenedFields,
|
|
@@ -90,25 +90,82 @@ export const updateJobs = async function updateMany({ id, data, limit, req, retu
|
|
|
90
90
|
doc
|
|
91
91
|
] : [];
|
|
92
92
|
}
|
|
93
|
-
} else {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
93
|
+
} else if (limit === 1) {
|
|
94
|
+
/**
|
|
95
|
+
* Select, update, and return one job atomically in a single database call.
|
|
96
|
+
* We can only do this with limit === 1, which can be expressed as findOne.
|
|
97
|
+
*/ const doc = await Model.findOneAndUpdate(query, updateData, {
|
|
98
|
+
...findOptions,
|
|
99
|
+
projection: returning === false ? {
|
|
100
|
+
_id: 1
|
|
101
|
+
} : undefined,
|
|
102
|
+
sort
|
|
103
|
+
});
|
|
104
|
+
if (returning === false) {
|
|
105
|
+
return null;
|
|
106
|
+
}
|
|
107
|
+
result = doc ? [
|
|
108
|
+
doc
|
|
109
|
+
] : [];
|
|
110
|
+
} else if (typeof limit === 'number' && limit > 1) {
|
|
111
|
+
const candidates = await Model.find(query, {}, {
|
|
112
|
+
...findOptions,
|
|
113
|
+
limit,
|
|
114
|
+
projection: {
|
|
115
|
+
_id: 1
|
|
116
|
+
},
|
|
117
|
+
sort
|
|
118
|
+
});
|
|
119
|
+
if (candidates.length === 0) {
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
const candidateIDs = candidates.map((candidate)=>candidate._id);
|
|
123
|
+
if (typeof data.processingToken === 'string') {
|
|
124
|
+
/**
|
|
125
|
+
* `processingToken` identifies this claim update. `processing: true` cannot, because every
|
|
126
|
+
* worker writes the same value. The token lets us reliably find the jobs this update won.
|
|
127
|
+
*/ const claimQuery = {
|
|
128
|
+
$and: [
|
|
129
|
+
query,
|
|
130
|
+
{
|
|
131
|
+
_id: {
|
|
132
|
+
$in: candidateIDs
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
]
|
|
136
|
+
};
|
|
137
|
+
await Model.updateMany(claimQuery, updateData, baseOptions);
|
|
138
|
+
if (returning === false) {
|
|
139
|
+
return null;
|
|
140
|
+
}
|
|
141
|
+
const claimedJobsQuery = {
|
|
142
|
+
_id: {
|
|
143
|
+
$in: candidateIDs
|
|
100
144
|
},
|
|
145
|
+
processingToken: {
|
|
146
|
+
$eq: data.processingToken
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
result = await Model.find(claimedJobsQuery, {}, {
|
|
150
|
+
...findOptions,
|
|
101
151
|
sort
|
|
102
152
|
});
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
}
|
|
106
|
-
query = {
|
|
153
|
+
} else {
|
|
154
|
+
const limitedUpdateQuery = {
|
|
107
155
|
_id: {
|
|
108
|
-
$in:
|
|
156
|
+
$in: candidateIDs
|
|
109
157
|
}
|
|
110
158
|
};
|
|
159
|
+
await Model.updateMany(limitedUpdateQuery, updateData, baseOptions);
|
|
160
|
+
if (returning === false) {
|
|
161
|
+
return null;
|
|
162
|
+
}
|
|
163
|
+
result = await Model.find(limitedUpdateQuery, {}, {
|
|
164
|
+
...findOptions,
|
|
165
|
+
sort
|
|
166
|
+
});
|
|
111
167
|
}
|
|
168
|
+
} else {
|
|
112
169
|
await Model.updateMany(query, updateData, baseOptions);
|
|
113
170
|
if (returning === false) {
|
|
114
171
|
return null;
|
package/dist/updateJobs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateJobs.ts"],"sourcesContent":["import type { QueryOptions, UpdateQuery } from 'mongoose'\nimport type { Job, UpdateJobs, Where } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateJobs: UpdateJobs = async function updateMany(\n this: MongooseAdapter,\n { id, data, limit, req, returning, sort: sortArg, where: whereArg },\n) {\n if (\n !(data?.log as object[])?.length &&\n !(data.log && typeof data.log === 'object' && '$push' in data.log)\n ) {\n delete data.log\n }\n\n const where = id ? { id: { equals: id } } : (whereArg as Where)\n\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug: 'payload-jobs',\n })\n\n const sort: Record<string, unknown> | undefined = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n sort: sortArg || collectionConfig.defaultSort,\n timestamps: true,\n })\n\n
|
|
1
|
+
{"version":3,"sources":["../src/updateJobs.ts"],"sourcesContent":["import type { QueryOptions, UpdateQuery } from 'mongoose'\nimport type { Job, UpdateJobs, Where } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildSortParam } from './queries/buildSortParam.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateJobs: UpdateJobs = async function updateMany(\n this: MongooseAdapter,\n { id, data, limit, req, returning, sort: sortArg, where: whereArg },\n) {\n if (\n !(data?.log as object[])?.length &&\n !(data.log && typeof data.log === 'object' && '$push' in data.log)\n ) {\n delete data.log\n }\n\n const where = id ? { id: { equals: id } } : (whereArg as Where)\n\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug: 'payload-jobs',\n })\n\n const sort: Record<string, unknown> | undefined = buildSortParam({\n adapter: this,\n config: this.payload.config,\n fields: collectionConfig.flattenedFields,\n sort: sortArg || collectionConfig.defaultSort,\n timestamps: true,\n })\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: collectionConfig.slug,\n fields: collectionConfig.flattenedFields,\n where,\n })\n\n let updateData: UpdateQuery<any> = data\n\n const $inc: Record<string, number> = {}\n const $push: Record<string, { $each: any[] } | any> = {}\n const $addToSet: Record<string, { $each: any[] } | any> = {}\n const $pull: Record<string, { $in: any[] } | any> = {}\n\n transform({\n $addToSet,\n $inc,\n $pull,\n $push,\n adapter: this,\n data,\n fields: collectionConfig.fields,\n operation: 'write',\n })\n\n const updateOps: UpdateQuery<any> = {}\n\n if (Object.keys($inc).length) {\n updateOps.$inc = $inc\n }\n if (Object.keys($push).length) {\n updateOps.$push = $push\n }\n if (Object.keys($addToSet).length) {\n updateOps.$addToSet = $addToSet\n }\n if (Object.keys($pull).length) {\n updateOps.$pull = $pull\n }\n if (Object.keys(updateOps).length) {\n updateOps.$set = updateData\n updateData = updateOps\n }\n\n const baseOptions = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n } satisfies QueryOptions\n\n const findOptions: QueryOptions = {\n ...baseOptions,\n lean: true,\n new: true,\n }\n\n let result: Job[] = []\n\n try {\n if (id) {\n if (returning === false) {\n await Model.updateOne(query, updateData, baseOptions)\n transform({ adapter: this, data, fields: collectionConfig.fields, operation: 'read' })\n\n return null\n } else {\n const doc = await Model.findOneAndUpdate(query, updateData, findOptions)\n result = doc ? [doc] : []\n }\n } else if (limit === 1) {\n /**\n * Select, update, and return one job atomically in a single database call.\n * We can only do this with limit === 1, which can be expressed as findOne.\n */\n const doc = await Model.findOneAndUpdate(query, updateData, {\n ...findOptions,\n projection: returning === false ? { _id: 1 } : undefined,\n sort,\n })\n\n if (returning === false) {\n return null\n }\n\n result = doc ? [doc] : []\n } else if (typeof limit === 'number' && limit > 1) {\n const candidates = await Model.find(\n query,\n {},\n { ...findOptions, limit, projection: { _id: 1 }, sort },\n )\n\n if (candidates.length === 0) {\n return null\n }\n\n const candidateIDs = candidates.map((candidate) => candidate._id)\n\n if (typeof data.processingToken === 'string') {\n /**\n * `processingToken` identifies this claim update. `processing: true` cannot, because every\n * worker writes the same value. The token lets us reliably find the jobs this update won.\n */\n const claimQuery = {\n $and: [query, { _id: { $in: candidateIDs } }],\n }\n\n await Model.updateMany(claimQuery, updateData, baseOptions)\n\n if (returning === false) {\n return null\n }\n\n const claimedJobsQuery = {\n _id: { $in: candidateIDs },\n processingToken: { $eq: data.processingToken },\n }\n\n result = await Model.find(claimedJobsQuery, {}, { ...findOptions, sort })\n } else {\n const limitedUpdateQuery = { _id: { $in: candidateIDs } }\n\n await Model.updateMany(limitedUpdateQuery, updateData, baseOptions)\n\n if (returning === false) {\n return null\n }\n\n result = await Model.find(limitedUpdateQuery, {}, { ...findOptions, sort })\n }\n } else {\n await Model.updateMany(query, updateData, baseOptions)\n\n if (returning === false) {\n return null\n }\n\n result = await Model.find(query, {}, { ...findOptions, sort })\n }\n } catch (error) {\n handleError({ collection: collectionConfig.slug, error, req })\n }\n\n transform({\n adapter: this,\n data: result,\n fields: collectionConfig.fields,\n operation: 'read',\n })\n\n return result\n}\n"],"names":["buildQuery","buildSortParam","getCollection","getSession","handleError","transform","updateJobs","updateMany","id","data","limit","req","returning","sort","sortArg","where","whereArg","log","length","equals","collectionConfig","Model","adapter","collectionSlug","config","payload","fields","flattenedFields","defaultSort","timestamps","query","slug","updateData","$inc","$push","$addToSet","$pull","operation","updateOps","Object","keys","$set","baseOptions","session","findOptions","lean","new","result","updateOne","doc","findOneAndUpdate","projection","_id","undefined","candidates","find","candidateIDs","map","candidate","processingToken","claimQuery","$and","$in","claimedJobsQuery","$eq","limitedUpdateQuery","error","collection"],"mappings":"AAKA,SAASA,UAAU,QAAQ,0BAAyB;AACpD,SAASC,cAAc,QAAQ,8BAA6B;AAC5D,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,WAAW,QAAQ,6BAA4B;AACxD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,aAAyB,eAAeC,WAEnD,EAAEC,EAAE,EAAEC,IAAI,EAAEC,KAAK,EAAEC,GAA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AiBW,IAAI;YAAEmC;YAAOvD;QAAI;IAC9D;IAEAN,UAAU;QACRiB,SAAS,IAAI;QACbb,MAAMsC;QACNrB,QAAQN,iBAAiBM,MAAM;QAC/BW,WAAW;IACb;IAEA,OAAOU;AACT,EAAC"}
|
package/dist/updateOne.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateOne.d.ts","sourceRoot":"","sources":["../src/updateOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"updateOne.d.ts","sourceRoot":"","sources":["../src/updateOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAaxC,eAAO,MAAM,SAAS,EAAE,SAqHvB,CAAA"}
|
package/dist/updateOne.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
+
import { applyBranchIDProjection, resolveBranchRowID, withBranchIDSelect } from 'payload';
|
|
1
2
|
import { buildQuery } from './queries/buildQuery.js';
|
|
2
3
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
3
4
|
import { getCollection } from './utilities/getEntity.js';
|
|
4
5
|
import { getSession } from './utilities/getSession.js';
|
|
5
6
|
import { handleError } from './utilities/handleError.js';
|
|
6
7
|
import { transform } from './utilities/transform.js';
|
|
7
|
-
export const updateOne = async function updateOne({ id, collection: collectionSlug, data, locale, options: optionsArgs = {}, req, returning, select, where: whereArg = {} }) {
|
|
8
|
+
export const updateOne = async function updateOne({ id, branch, collection: collectionSlug, data, locale, options: optionsArgs = {}, req, returning, select, where: whereArg = {} }) {
|
|
8
9
|
const { collectionConfig, Model } = getCollection({
|
|
9
10
|
adapter: this,
|
|
10
11
|
collectionSlug
|
|
11
12
|
});
|
|
13
|
+
if (id !== undefined && id !== null) {
|
|
14
|
+
id = await resolveBranchRowID({
|
|
15
|
+
id,
|
|
16
|
+
branch,
|
|
17
|
+
collectionSlug,
|
|
18
|
+
req
|
|
19
|
+
});
|
|
20
|
+
}
|
|
12
21
|
const where = id ? {
|
|
13
22
|
id: {
|
|
14
23
|
equals: id
|
|
@@ -68,7 +77,12 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
|
|
|
68
77
|
projection: buildProjectionFromSelect({
|
|
69
78
|
adapter: this,
|
|
70
79
|
fields: collectionConfig.flattenedFields,
|
|
71
|
-
select
|
|
80
|
+
select: withBranchIDSelect({
|
|
81
|
+
branch,
|
|
82
|
+
collectionSlug,
|
|
83
|
+
req,
|
|
84
|
+
select
|
|
85
|
+
})
|
|
72
86
|
})
|
|
73
87
|
};
|
|
74
88
|
try {
|
|
@@ -100,6 +114,16 @@ export const updateOne = async function updateOne({ id, collection: collectionSl
|
|
|
100
114
|
fields,
|
|
101
115
|
operation: 'read'
|
|
102
116
|
});
|
|
117
|
+
// The row written on a branch is the shadow row, so the document it returns
|
|
118
|
+
// carries that row's primary key rather than the document's canonical ID.
|
|
119
|
+
applyBranchIDProjection({
|
|
120
|
+
branch,
|
|
121
|
+
collectionSlug,
|
|
122
|
+
docs: [
|
|
123
|
+
result
|
|
124
|
+
],
|
|
125
|
+
req
|
|
126
|
+
});
|
|
103
127
|
return result;
|
|
104
128
|
};
|
|
105
129
|
|
package/dist/updateOne.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateOne.ts"],"sourcesContent":["import type { QueryOptions, UpdateQuery } from 'mongoose'\nimport type { UpdateOne } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: MongooseAdapter,\n {\n id,\n collection: collectionSlug,\n data,\n locale,\n options: optionsArgs = {},\n req,\n returning,\n select,\n where: whereArg = {},\n },\n) {\n const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug })\n const where = id ? { id: { equals: id } } : whereArg\n const fields = collectionConfig.fields\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug,\n fields: collectionConfig.flattenedFields,\n locale,\n where,\n })\n\n let result\n\n let updateData: UpdateQuery<any> = data\n\n const $inc: Record<string, number> = {}\n const $push: Record<string, { $each: any[] } | any> = {}\n const $addToSet: Record<string, { $each: any[] } | any> = {}\n const $pull: Record<string, { $in: any[] } | any> = {}\n\n transform({\n $addToSet,\n $inc,\n $pull,\n $push,\n adapter: this,\n data,\n fields,\n operation: 'write',\n })\n\n const updateOps: UpdateQuery<any> = {}\n\n if (Object.keys($inc).length) {\n updateOps.$inc = $inc\n }\n if (Object.keys($push).length) {\n updateOps.$push = $push\n }\n if (Object.keys($addToSet).length) {\n updateOps.$addToSet = $addToSet\n }\n if (Object.keys($pull).length) {\n updateOps.$pull = $pull\n }\n if (Object.keys(updateOps).length) {\n updateOps.$set = updateData\n updateData = updateOps\n }\n\n const baseOptions = {\n ...optionsArgs,\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n } satisfies QueryOptions\n\n const findOptions: QueryOptions = {\n ...baseOptions,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n select,\n }),\n }\n\n try {\n if (returning === false) {\n await Model.updateOne(query, updateData, baseOptions)\n transform({ adapter: this, data, fields, operation: 'read' })\n return null\n } else {\n result = await Model.findOneAndUpdate(query, updateData, findOptions)\n }\n } catch (error) {\n handleError({ collection: collectionSlug, error, req })\n }\n\n if (!result) {\n return null\n }\n\n transform({ adapter: this, data: result, fields, operation: 'read' })\n\n return result\n}\n"],"names":["buildQuery","buildProjectionFromSelect","getCollection","getSession","handleError","transform","updateOne","id","collection","collectionSlug","data","locale","options","optionsArgs","req","returning","select","where","whereArg","collectionConfig","Model","adapter","equals","fields","query","flattenedFields","result","updateData","$inc","$push","$addToSet","$pull","operation","updateOps","Object","keys","length","$set","baseOptions","session","timestamps","findOptions","lean","new","projection","findOneAndUpdate","error"],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../src/updateOne.ts"],"sourcesContent":["import type { QueryOptions, UpdateQuery } from 'mongoose'\nimport type { UpdateOne } from 'payload'\n\nimport { applyBranchIDProjection, resolveBranchRowID, withBranchIDSelect } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: MongooseAdapter,\n {\n id,\n branch,\n collection: collectionSlug,\n data,\n locale,\n options: optionsArgs = {},\n req,\n returning,\n select,\n where: whereArg = {},\n },\n) {\n const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug })\n\n if (id !== undefined && id !== null) {\n id = await resolveBranchRowID({ id, branch, collectionSlug, req })\n }\n\n const where = id ? { id: { equals: id } } : whereArg\n const fields = collectionConfig.fields\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug,\n fields: collectionConfig.flattenedFields,\n locale,\n where,\n })\n\n let result\n\n let updateData: UpdateQuery<any> = data\n\n const $inc: Record<string, number> = {}\n const $push: Record<string, { $each: any[] } | any> = {}\n const $addToSet: Record<string, { $each: any[] } | any> = {}\n const $pull: Record<string, { $in: any[] } | any> = {}\n\n transform({\n $addToSet,\n $inc,\n $pull,\n $push,\n adapter: this,\n data,\n fields,\n operation: 'write',\n })\n\n const updateOps: UpdateQuery<any> = {}\n\n if (Object.keys($inc).length) {\n updateOps.$inc = $inc\n }\n if (Object.keys($push).length) {\n updateOps.$push = $push\n }\n if (Object.keys($addToSet).length) {\n updateOps.$addToSet = $addToSet\n }\n if (Object.keys($pull).length) {\n updateOps.$pull = $pull\n }\n if (Object.keys(updateOps).length) {\n updateOps.$set = updateData\n updateData = updateOps\n }\n\n const baseOptions = {\n ...optionsArgs,\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n } satisfies QueryOptions\n\n const findOptions: QueryOptions = {\n ...baseOptions,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: collectionConfig.flattenedFields,\n select: withBranchIDSelect({ branch, collectionSlug, req, select }),\n }),\n }\n\n try {\n if (returning === false) {\n await Model.updateOne(query, updateData, baseOptions)\n transform({ adapter: this, data, fields, operation: 'read' })\n return null\n } else {\n result = await Model.findOneAndUpdate(query, updateData, findOptions)\n }\n } catch (error) {\n handleError({ collection: collectionSlug, error, req })\n }\n\n if (!result) {\n return null\n }\n\n transform({ adapter: this, data: result, fields, operation: 'read' })\n\n // The row written on a branch is the shadow row, so the document it returns\n // carries that row's primary key rather than the document's canonical ID.\n applyBranchIDProjection({\n branch,\n collectionSlug,\n docs: [result as Record<string, unknown>],\n req,\n })\n\n return result\n}\n"],"names":["applyBranchIDProjection","resolveBranchRowID","withBranchIDSelect","buildQuery","buildProjectionFromSelect","getCollection","getSession","handleError","transform","updateOne","id","branch","collection","collectionSlug","data","locale","options","optionsArgs","req","returning","select","where","whereArg","collectionConfig","Model","adapter","undefined","equals","fields","query","flattenedFields","result","updateData","$inc","$push","$addToSet","$pull","operation","updateOps","Object","keys","length","$set","baseOptions","session","timestamps","findOptions","lean","new","projection","findOneAndUpdate","error","docs"],"mappings":"AAGA,SAASA,uBAAuB,EAAEC,kBAAkB,EAAEC,kBAAkB,QAAQ,UAAS;AAIzF,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,WAAW,QAAQ,6BAA4B;A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OK,MAAM,EAAE;QAC7BH,UAAUF,KAAK,GAAGA;IACpB;IACA,IAAIG,OAAOC,IAAI,CAACF,WAAWG,MAAM,EAAE;QACjCH,UAAUI,IAAI,GAAGV;QACjBA,aAAaM;IACf;IAEA,MAAMK,cAAc;QAClB,GAAG1B,WAAW;QACd2B,SAAS,MAAMtC,WAAW,IAAI,EAAEY;QAChC,uDAAuD;QACvD2B,YAAY;IACd;IAEA,MAAMC,cAA4B;QAChC,GAAGH,WAAW;QACdI,MAAM;QACNC,KAAK;QACLC,YAAY7C,0BAA0B;YACpCqB,SAAS,IAAI;YACbG,QAAQL,iBAAiBO,eAAe;YACxCV,QAAQlB,mBAAmB;gBAAES;gBAAQE;gBAAgBK;gBAAKE;YAAO;QACnE;IACF;IAEA,IAAI;QACF,IAAID,cAAc,OAAO;YACvB,MAAMK,MAAMf,SAAS,CAACoB,OAAOG,YAAYW;YACzCnC,UAAU;gBAAEiB,SAAS,IAAI;gBAAEX;gBAAMc;gBAAQS,WAAW;YAAO;YAC3D,OAAO;QACT,OAAO;YACLN,SAAS,MAAMP,MAAM0B,gBAAgB,CAACrB,OAAOG,YAAYc;QAC3D;IACF,EAAE,OAAOK,OAAO;QACd5C,YAAY;YAAEK,YAAYC;YAAgBsC;YAAOjC;QAAI;IACvD;IAEA,IAAI,CAACa,QAAQ;QACX,OAAO;IACT;IAEAvB,UAAU;QAAEiB,SAAS,IAAI;QAAEX,MAAMiB;QAAQH;QAAQS,WAAW;IAAO;IAEnE,4EAA4E;IAC5E,0EAA0E;IAC1ErC,wBAAwB;QACtBW;QACAE;QACAuC,MAAM;YAACrB;SAAkC;QACzCb;IACF;IAEA,OAAOa;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"aggregatePaginate.d.ts","sourceRoot":"","sources":["../../src/utilities/aggregatePaginate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAC/C,OAAO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PipelineStage } from 'mongoose';
|
|
2
|
-
import { type CollectionSlug, type JoinQuery, type SanitizedCollectionConfig } from 'payload';
|
|
2
|
+
import { type CollectionSlug, type JoinQuery, type PayloadRequest, type SanitizedCollectionConfig } from 'payload';
|
|
3
3
|
import type { MongooseAdapter } from '../index.js';
|
|
4
4
|
type BuildJoinAggregationArgs = {
|
|
5
5
|
adapter: MongooseAdapter;
|
|
@@ -10,9 +10,10 @@ type BuildJoinAggregationArgs = {
|
|
|
10
10
|
locale?: string;
|
|
11
11
|
projection?: Record<string, true>;
|
|
12
12
|
query?: Record<string, unknown>;
|
|
13
|
+
req?: Partial<PayloadRequest>;
|
|
13
14
|
/** whether the query is from drafts */
|
|
14
15
|
versions?: boolean;
|
|
15
16
|
};
|
|
16
|
-
export declare const buildJoinAggregation: ({ adapter, collection, collectionConfig, draftsEnabled, joins, locale, projection, versions, }: BuildJoinAggregationArgs) => Promise<PipelineStage[] | undefined>;
|
|
17
|
+
export declare const buildJoinAggregation: ({ adapter, collection, collectionConfig, draftsEnabled, joins, locale, projection, req, versions, }: BuildJoinAggregationArgs) => Promise<PipelineStage[] | undefined>;
|
|
17
18
|
export {};
|
|
18
19
|
//# sourceMappingURL=buildJoinAggregation.d.ts.map
|