@payloadcms/db-mongodb 4.0.0-internal.e55ccef → 4.0.0-internal.ea6e8f8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +9 -2
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +33 -4
- package/dist/count.js.map +1 -1
- package/dist/countGlobalVersions.d.ts.map +1 -1
- package/dist/countGlobalVersions.js +1 -0
- package/dist/countGlobalVersions.js.map +1 -1
- package/dist/countVersions.d.ts.map +1 -1
- package/dist/countVersions.js +36 -4
- package/dist/countVersions.js.map +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +2 -1
- package/dist/create.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +23 -1
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +9 -2
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +17 -4
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteBranchGlobal.d.ts +10 -0
- package/dist/deleteBranchGlobal.d.ts.map +1 -0
- package/dist/deleteBranchGlobal.js +22 -0
- package/dist/deleteBranchGlobal.js.map +1 -0
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +1 -0
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +20 -1
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +1 -0
- package/dist/deleteVersions.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +2 -0
- package/dist/exports/migration-utils.d.ts.map +1 -1
- package/dist/exports/migration-utils.js +2 -0
- package/dist/exports/migration-utils.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +44 -8
- package/dist/find.js.map +1 -1
- package/dist/findDistinct.d.ts.map +1 -1
- package/dist/findDistinct.js +115 -12
- package/dist/findDistinct.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +34 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +10 -2
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +43 -5
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +49 -5
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +89 -67
- package/dist/init.js.map +1 -1
- package/dist/migrateFresh.d.ts +4 -2
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +21 -6
- package/dist/migrateFresh.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +8 -2
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +4 -2
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +5 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +34 -3
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +4 -0
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +94 -36
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/models/schemaBuildContext.d.ts +21 -0
- package/dist/models/schemaBuildContext.d.ts.map +1 -0
- package/dist/models/schemaBuildContext.js +29 -0
- package/dist/models/schemaBuildContext.js.map +1 -0
- package/dist/predefinedMigrations/branching.d.ts +4 -0
- package/dist/predefinedMigrations/branching.d.ts.map +1 -0
- package/dist/predefinedMigrations/branching.js +9 -0
- package/dist/predefinedMigrations/branching.js.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts +15 -0
- package/dist/predefinedMigrations/migrateBranching.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateBranching.js +88 -0
- package/dist/predefinedMigrations/migrateBranching.js.map +1 -0
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +4 -4
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/predefinedMigrations/migrateSizesToVariants.d.ts +13 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.d.ts.map +1 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.js +368 -0
- package/dist/predefinedMigrations/migrateSizesToVariants.js.map +1 -0
- package/dist/predefinedMigrations/sizes-to-variants.d.ts +5 -0
- package/dist/predefinedMigrations/sizes-to-variants.d.ts.map +1 -0
- package/dist/predefinedMigrations/sizes-to-variants.js +8 -0
- package/dist/predefinedMigrations/sizes-to-variants.js.map +1 -0
- package/dist/queries/buildAndOrConditions.d.ts +4 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +3 -1
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildBranchVisibility.d.ts +11 -0
- package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
- package/dist/queries/buildBranchVisibility.js +204 -0
- package/dist/queries/buildBranchVisibility.js.map +1 -0
- package/dist/queries/buildQuery.d.ts +4 -2
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +3 -1
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +4 -2
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +338 -24
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/getBuildQueryPlugin.d.ts +3 -1
- package/dist/queries/getBuildQueryPlugin.d.ts.map +1 -1
- package/dist/queries/getBuildQueryPlugin.js +3 -1
- package/dist/queries/getBuildQueryPlugin.js.map +1 -1
- package/dist/queries/parseParams.d.ts +4 -2
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +5 -1
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +38 -7
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +55 -7
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js +10 -9
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +89 -9
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +3 -2
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +71 -14
- package/dist/updateJobs.js.map +1 -1
- package/dist/updateMany.d.ts.map +1 -1
- package/dist/updateMany.js +3 -2
- package/dist/updateMany.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +30 -4
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +3 -2
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertBranchGlobalChange.d.ts +3 -0
- package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
- package/dist/upsertBranchGlobalChange.js +28 -0
- package/dist/upsertBranchGlobalChange.js.map +1 -0
- package/dist/utilities/aggregatePaginate.d.ts +2 -1
- package/dist/utilities/aggregatePaginate.d.ts.map +1 -1
- package/dist/utilities/aggregatePaginate.js +21 -5
- package/dist/utilities/aggregatePaginate.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +4 -2
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -1
- package/dist/utilities/buildJoinAggregation.js +107 -8
- package/dist/utilities/buildJoinAggregation.js.map +1 -1
- package/dist/utilities/resolveJoins.d.ts +6 -2
- package/dist/utilities/resolveJoins.d.ts.map +1 -1
- package/dist/utilities/resolveJoins.js +114 -26
- package/dist/utilities/resolveJoins.js.map +1 -1
- package/dist/utilities/transform.d.ts.map +1 -1
- package/dist/utilities/transform.js +15 -0
- package/dist/utilities/transform.js.map +1 -1
- package/package.json +6 -6
- package/dist/queries/buildSortParam.spec.js +0 -123
- package/dist/queries/buildSortParam.spec.js.map +0 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +0 -215
- package/dist/queries/getLocalizedSortProperty.spec.js.map +0 -1
- package/dist/utilities/transform.spec.js +0 -479
- package/dist/utilities/transform.spec.js.map +0 -1
package/dist/connect.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAa,MAAM,SAAS,CAAA;AAOjD,eAAO,MAAM,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"connect.d.ts","sourceRoot":"","sources":["../src/connect.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAa,MAAM,SAAS,CAAA;AAOjD,eAAO,MAAM,OAAO,EAAE,OAmHrB,CAAA"}
|
package/dist/connect.js
CHANGED
|
@@ -64,8 +64,15 @@ export const connect = async function connect(options = {
|
|
|
64
64
|
}
|
|
65
65
|
}
|
|
66
66
|
if (this.ensureIndexes) {
|
|
67
|
-
await Promise.all(
|
|
68
|
-
|
|
67
|
+
await Promise.all([
|
|
68
|
+
...this.payload.config.collections.flatMap((collection)=>[
|
|
69
|
+
this.collections[collection.slug],
|
|
70
|
+
this.versions[collection.slug]
|
|
71
|
+
]),
|
|
72
|
+
this.globals,
|
|
73
|
+
...this.payload.config.globals.map((global)=>this.versions[global.slug])
|
|
74
|
+
].map(async (model)=>{
|
|
75
|
+
await model?.ensureIndexes();
|
|
69
76
|
}));
|
|
70
77
|
}
|
|
71
78
|
if (process.env.NODE_ENV === 'production' && this.prodMigrations) {
|
package/dist/connect.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/connect.ts"],"sourcesContent":["import type { ConnectOptions } from 'mongoose'\nimport type { Connect, Migration } from 'payload'\n\nimport mongoose from 'mongoose'\nimport { defaultBeginTransaction } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nexport const connect: Connect = async function connect(\n this: MongooseAdapter,\n options = {\n hotReload: false,\n },\n) {\n const { hotReload } = options\n\n if (this.url === false) {\n return\n }\n\n if (typeof this.url !== 'string') {\n throw new Error('Error: missing MongoDB connection URL.')\n }\n\n const urlToConnect = this.url\n\n const connectionOptions: ConnectOptions = {\n autoIndex: true,\n ...this.connectOptions,\n }\n\n if (hotReload) {\n connectionOptions.autoIndex = false\n }\n\n try {\n if (!this.connection) {\n this.connection = await mongoose.createConnection(urlToConnect, connectionOptions).asPromise()\n if (this.afterCreateConnection) {\n await this.afterCreateConnection(this)\n }\n }\n\n await this.connection.openUri(urlToConnect, connectionOptions)\n\n if (this.afterOpenConnection) {\n await this.afterOpenConnection(this)\n }\n\n if (this.useAlternativeDropDatabase) {\n if (this.connection.db) {\n // Firestore doesn't support dropDatabase, so we monkey patch\n // dropDatabase to delete all documents from all collections instead\n this.connection.db.dropDatabase = async function (): Promise<boolean> {\n const existingCollections = await this.listCollections().toArray()\n await Promise.all(\n existingCollections.map(async (collectionInfo) => {\n const collection = this.collection(collectionInfo.name)\n await collection.deleteMany({})\n }),\n )\n return true\n }\n this.connection.dropDatabase = async function () {\n await this.db?.dropDatabase()\n }\n }\n }\n\n // If we are running a replica set with MongoDB Memory Server,\n // wait until the replica set elects a primary before proceeding\n if (this.mongoMemoryServer) {\n this.payload.logger.info(\n 'Waiting for MongoDB Memory Server replica set to elect a primary...',\n )\n await new Promise((resolve) => setTimeout(resolve, 2000))\n }\n\n if (!this.connection.getClient().options.replicaSet) {\n this.transactionOptions = false\n this.beginTransaction = defaultBeginTransaction()\n }\n\n if (!hotReload) {\n if (process.env.PAYLOAD_DROP_DATABASE === 'true') {\n this.payload.logger.info('---- DROPPING DATABASE ----')\n await this.connection.dropDatabase()\n\n this.payload.logger.info('---- DROPPED DATABASE ----')\n }\n }\n\n if (this.ensureIndexes) {\n await Promise.all(\n this.payload.config.collections.
|
|
1
|
+
{"version":3,"sources":["../src/connect.ts"],"sourcesContent":["import type { ConnectOptions } from 'mongoose'\nimport type { Connect, Migration } from 'payload'\n\nimport mongoose from 'mongoose'\nimport { defaultBeginTransaction } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nexport const connect: Connect = async function connect(\n this: MongooseAdapter,\n options = {\n hotReload: false,\n },\n) {\n const { hotReload } = options\n\n if (this.url === false) {\n return\n }\n\n if (typeof this.url !== 'string') {\n throw new Error('Error: missing MongoDB connection URL.')\n }\n\n const urlToConnect = this.url\n\n const connectionOptions: ConnectOptions = {\n autoIndex: true,\n ...this.connectOptions,\n }\n\n if (hotReload) {\n connectionOptions.autoIndex = false\n }\n\n try {\n if (!this.connection) {\n this.connection = await mongoose.createConnection(urlToConnect, connectionOptions).asPromise()\n if (this.afterCreateConnection) {\n await this.afterCreateConnection(this)\n }\n }\n\n await this.connection.openUri(urlToConnect, connectionOptions)\n\n if (this.afterOpenConnection) {\n await this.afterOpenConnection(this)\n }\n\n if (this.useAlternativeDropDatabase) {\n if (this.connection.db) {\n // Firestore doesn't support dropDatabase, so we monkey patch\n // dropDatabase to delete all documents from all collections instead\n this.connection.db.dropDatabase = async function (): Promise<boolean> {\n const existingCollections = await this.listCollections().toArray()\n await Promise.all(\n existingCollections.map(async (collectionInfo) => {\n const collection = this.collection(collectionInfo.name)\n await collection.deleteMany({})\n }),\n )\n return true\n }\n this.connection.dropDatabase = async function () {\n await this.db?.dropDatabase()\n }\n }\n }\n\n // If we are running a replica set with MongoDB Memory Server,\n // wait until the replica set elects a primary before proceeding\n if (this.mongoMemoryServer) {\n this.payload.logger.info(\n 'Waiting for MongoDB Memory Server replica set to elect a primary...',\n )\n await new Promise((resolve) => setTimeout(resolve, 2000))\n }\n\n if (!this.connection.getClient().options.replicaSet) {\n this.transactionOptions = false\n this.beginTransaction = defaultBeginTransaction()\n }\n\n if (!hotReload) {\n if (process.env.PAYLOAD_DROP_DATABASE === 'true') {\n this.payload.logger.info('---- DROPPING DATABASE ----')\n await this.connection.dropDatabase()\n\n this.payload.logger.info('---- DROPPED DATABASE ----')\n }\n }\n\n if (this.ensureIndexes) {\n await Promise.all(\n [\n ...this.payload.config.collections.flatMap((collection) => [\n this.collections[collection.slug],\n this.versions[collection.slug],\n ]),\n this.globals,\n ...this.payload.config.globals.map((global) => this.versions[global.slug]),\n ].map(async (model) => {\n await model?.ensureIndexes()\n }),\n )\n }\n\n if (process.env.NODE_ENV === 'production' && this.prodMigrations) {\n await this.migrate({ migrations: this.prodMigrations as unknown as Migration[] })\n }\n } catch (err) {\n let msg = `Error: cannot connect to MongoDB.`\n\n if (typeof err === 'object' && err && 'message' in err && typeof err.message === 'string') {\n msg = `${msg} Details: ${err.message}`\n }\n\n this.payload.logger.error({\n err,\n msg,\n })\n throw new Error(`Error: cannot connect to MongoDB: ${msg}`)\n }\n}\n"],"names":["mongoose","defaultBeginTransaction","connect","options","hotReload","url","Error","urlToConnect","connectionOptions","autoIndex","connectOptions","connection","createConnection","asPromise","afterCreateConnection","openUri","afterOpenConnection","useAlternativeDropDatabase","db","dropDatabase","existingCollections","listCollections","toArray","Promise","all","map","collectionInfo","collection","name","deleteMany","mongoMemoryServer","payload","logger","info","resolve","setTimeout","getClient","replicaSet","transactionOptions","beginTransaction","process","env","PAYLOAD_DROP_DATABASE","ensureIndexes","config","collections","flatMap","slug","versions","globals","global","model","NODE_ENV","prodMigrations","migrate","migrations","err","msg","message","error"],"mappings":"AAGA,OAAOA,cAAc,WAAU;AAC/B,SAASC,uBAAuB,QAAQ,UAAS;AAIjD,OAAO,MAAMC,UAAmB,eAAeA,QAE7CC,UAAU;IACRC,WAAW;AACb,CAAC;IAED,MAAM,EAAEA,SAAS,EAAE,GAAGD;IAEtB,IAAI,IAAI,CAACE,GAAG,KAAK,OAAO;QACtB;IACF;IAEA,IAAI,OAAO,IAAI,CAACA,GAAG,KAAK,UAAU;QAChC,MAAM,IAAIC,MAAM;IAClB;IAEA,MAAMC,eAAe,IAAI,CAACF,GAAG;IAE7B,MAAMG,oBAAoC;QACxCC,WAAW;QACX,GAAG,IAAI,CAACC,cAAc;IACxB;IAEA,IAAIN,WAAW;QACbI,kBAAkBC,SAAS,GAAG;IAChC;IAEA,IAAI;QACF,IAAI,CAAC,IAAI,CAACE,UAAU,EAAE;YACpB,IAAI,CAACA,UAAU,GAAG,MAAMX,SAASY,gBAAgB,CAACL,cAAcC,mBAAmBK,SAAS;YAC5F,IAAI,IAAI,CAACC,qBAAqB,EAAE;gBAC9B,MAAM,IAAI,CAACA,qBAAqB,CAAC,IAAI;YACvC;QACF;QAEA,MAAM,IAAI,CAACH,UAAU,CAACI,OAAO,CAACR,cAAcC;QAE5C,IAAI,IAAI,CAACQ,mBAAmB,EAAE;YAC5B,MAAM,IAAI,CAACA,mBAAmB,CAAC,IAAI;QACrC;QAEA,IAAI,IAAI,CAACC,0BAA0B,EAAE;YACnC,IAAI,IAAI,CAACN,UAAU,CAACO,EAAE,EAAE;gBACtB,6DAA6D;gBAC7D,oEAAoE;gBACpE,IAAI,CAACP,UAAU,CAACO,EAAE,CAACC,YAAY,GAAG;oBAChC,MAAMC,sBAAsB,MAAM,IAAI,CAACC,eAAe,GAAGC,OAAO;oBAChE,MAAMC,QAAQC,GAAG,CACfJ,oBAAoBK,GAAG,CAAC,OAAOC;wBAC7B,MAAMC,aAAa,IAAI,CA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O0B;gBACX,MAAMA,OAAOR;YACf;QAEJ;QAEA,IAAIH,QAAQC,GAAG,CAACW,QAAQ,KAAK,gBAAgB,IAAI,CAACC,cAAc,EAAE;YAChE,MAAM,IAAI,CAACC,OAAO,CAAC;gBAAEC,YAAY,IAAI,CAACF,cAAc;YAA2B;QACjF;IACF,EAAE,OAAOG,KAAK;QACZ,IAAIC,MAAM,CAAC,iCAAiC,CAAC;QAE7C,IAAI,OAAOD,QAAQ,YAAYA,OAAO,aAAaA,OAAO,OAAOA,IAAIE,OAAO,KAAK,UAAU;YACzFD,MAAM,GAAGA,IAAI,UAAU,EAAED,IAAIE,OAAO,EAAE;QACxC;QAEA,IAAI,CAAC3B,OAAO,CAACC,MAAM,CAAC2B,KAAK,CAAC;YACxBH;YACAC;QACF;QACA,MAAM,IAAInD,MAAM,CAAC,kCAAkC,EAAEmD,KAAK;IAC5D;AACF,EAAC"}
|
package/dist/count.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"count.d.ts","sourceRoot":"","sources":["../src/count.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"count.d.ts","sourceRoot":"","sources":["../src/count.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAgBpC,eAAO,MAAM,KAAK,EAAE,KA+EnB,CAAA"}
|
package/dist/count.js
CHANGED
|
@@ -1,12 +1,29 @@
|
|
|
1
|
-
import { flattenWhereToOperators } from 'payload';
|
|
1
|
+
import { flattenWhereToOperators, resolveBranchQuery, resolveBranchReadState, rewriteBranchIDs } from 'payload';
|
|
2
|
+
import { buildBranchVisibilityStages } from './queries/buildBranchVisibility.js';
|
|
2
3
|
import { buildQuery } from './queries/buildQuery.js';
|
|
3
4
|
import { getCollection } from './utilities/getEntity.js';
|
|
4
5
|
import { getSession } from './utilities/getSession.js';
|
|
5
|
-
export const count = async function count({ collection: collectionSlug, locale, req, where = {} }) {
|
|
6
|
+
export const count = async function count({ branch, collection: collectionSlug, locale, req, where = {} }) {
|
|
6
7
|
const { collectionConfig, Model } = getCollection({
|
|
7
8
|
adapter: this,
|
|
8
9
|
collectionSlug
|
|
9
10
|
});
|
|
11
|
+
const branchReadState = resolveBranchReadState({
|
|
12
|
+
branch,
|
|
13
|
+
collectionSlug,
|
|
14
|
+
req
|
|
15
|
+
});
|
|
16
|
+
where = branchReadState.useBranching ? rewriteBranchIDs(where) ?? {} : await resolveBranchQuery({
|
|
17
|
+
branch,
|
|
18
|
+
collectionSlug,
|
|
19
|
+
req,
|
|
20
|
+
where
|
|
21
|
+
}) ?? {};
|
|
22
|
+
const branchVisibility = branchReadState.useBranching ? buildBranchVisibilityStages({
|
|
23
|
+
adapter: this,
|
|
24
|
+
branch: branchReadState.branch,
|
|
25
|
+
collectionSlug
|
|
26
|
+
}) : [];
|
|
10
27
|
let hasNearConstraint = false;
|
|
11
28
|
if (where) {
|
|
12
29
|
const constraints = flattenWhereToOperators(where);
|
|
@@ -14,13 +31,15 @@ export const count = async function count({ collection: collectionSlug, locale,
|
|
|
14
31
|
}
|
|
15
32
|
const query = await buildQuery({
|
|
16
33
|
adapter: this,
|
|
34
|
+
branch,
|
|
17
35
|
collectionSlug,
|
|
18
36
|
fields: collectionConfig.flattenedFields,
|
|
19
37
|
locale,
|
|
38
|
+
req,
|
|
20
39
|
where
|
|
21
40
|
});
|
|
22
41
|
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
23
|
-
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0;
|
|
42
|
+
const useEstimatedCount = !branchVisibility.length && (hasNearConstraint || !query || Object.keys(query).length === 0);
|
|
24
43
|
const options = {
|
|
25
44
|
session: await getSession(this, req)
|
|
26
45
|
};
|
|
@@ -42,7 +61,17 @@ export const count = async function count({ collection: collectionSlug, locale,
|
|
|
42
61
|
};
|
|
43
62
|
}
|
|
44
63
|
let result;
|
|
45
|
-
if (
|
|
64
|
+
if (branchVisibility.length) {
|
|
65
|
+
result = await Model.aggregate([
|
|
66
|
+
{
|
|
67
|
+
$match: query
|
|
68
|
+
},
|
|
69
|
+
...branchVisibility,
|
|
70
|
+
{
|
|
71
|
+
$count: 'count'
|
|
72
|
+
}
|
|
73
|
+
], options).then((rows)=>rows[0]?.count ?? 0);
|
|
74
|
+
} else if (useEstimatedCount) {
|
|
46
75
|
result = await Model.estimatedDocumentCount({
|
|
47
76
|
session: options.session
|
|
48
77
|
});
|
package/dist/count.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/count.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { Count } from 'payload'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/count.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { Count } from 'payload'\n\nimport {\n flattenWhereToOperators,\n resolveBranchQuery,\n resolveBranchReadState,\n rewriteBranchIDs,\n} from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildBranchVisibilityStages } from './queries/buildBranchVisibility.js'\nimport { buildQuery } from './queries/buildQuery.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const count: Count = async function count(\n this: MongooseAdapter,\n { branch, collection: collectionSlug, locale, req, where = {} },\n) {\n const { collectionConfig, Model } = getCollection({ adapter: this, collectionSlug })\n const branchReadState = resolveBranchReadState({ branch, collectionSlug, req })\n\n where = branchReadState.useBranching\n ? (rewriteBranchIDs(where) ?? {})\n : ((await resolveBranchQuery({ branch, collectionSlug, req, where })) ?? {})\n const branchVisibility = branchReadState.useBranching\n ? buildBranchVisibilityStages({\n adapter: this,\n branch: branchReadState.branch,\n collectionSlug,\n })\n : []\n\n let hasNearConstraint = false\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 query = await buildQuery({\n adapter: this,\n branch,\n collectionSlug,\n fields: collectionConfig.flattenedFields,\n locale,\n req,\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 =\n !branchVisibility.length && (hasNearConstraint || !query || Object.keys(query).length === 0)\n\n const options: CountOptions = {\n session: await getSession(this, req),\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 options.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\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 options.hint = {\n _id: 1,\n }\n }\n\n let result: number\n if (branchVisibility.length) {\n result = await Model.aggregate(\n [{ $match: query }, ...branchVisibility, { $count: 'count' }],\n options,\n ).then((rows) => rows[0]?.count ?? 0)\n } else if (useEstimatedCount) {\n result = await Model.estimatedDocumentCount({ session: options.session })\n } else {\n result = await Model.countDocuments(query, options)\n }\n\n return {\n totalDocs: result,\n }\n}\n"],"names":["flattenWhereToOperators","resolveBranchQuery","resolveBranchReadState","rewriteBranchIDs","buildBranchVisibilityStages","buildQuery","getCollection","getSession","count","branch","collection","collectionSlug","locale","req","where","collectionConfig","Model","adapter","branchReadState","useBranching","branchVisibility","hasNearConstraint","constraints","some","prop","Object","keys","key","query","fields","flattenedFields","useEstimatedCount","length","options","session","collation","localizationConfig","payload","config","localization","defaultLocale","disableIndexHints","hint","_id","result","aggregate","$match","$count","then","rows","estimatedDocumentCount","countDocuments","totalDocs"],"mappings":"AAGA,SACEA,uBAAuB,EACvBC,kBA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,WAAW,IAAI,EAAEM;IAClC;IAEA,IAAI,IAAI,CAACsB,SAAS,EAAE;QAClB,MAAMC,qBAAqB,IAAI,CAACC,OAAO,CAACC,MAAM,CAACC,YAAY;QAC3D,MAAMC,gBACJ,AAAC,OAAOJ,uBAAuB,YAAYA,oBAAoBI,iBAAkB;QAEnFP,QAAQE,SAAS,GAAG;YAClBvB,QAAQA,UAAUA,WAAW,SAASA,WAAW,MAAMA,SAAS4B;YAChE,GAAG,IAAI,CAACL,SAAS;QACnB;IACF;IAEA,IAAI,CAACJ,qBAAqBN,OAAOC,IAAI,CAACE,OAAOI,MAAM,KAAK,KAAK,IAAI,CAACS,iBAAiB,KAAK,MAAM;QAC5F,mHAAmH;QACnH,qHAAqH;QACrH,mHAAmH;QACnH,4BAA4B;QAC5BR,QAAQS,IAAI,GAAG;YACbC,KAAK;QACP;IACF;IAEA,IAAIC;IACJ,IAAIxB,iBAAiBY,MAAM,EAAE;QAC3BY,SAAS,MAAM5B,MAAM6B,SAAS,CAC5B;YAAC;gBAAEC,QAAQlB;YAAM;eAAMR;YAAkB;gBAAE2B,QAAQ;YAAQ;SAAE,EAC7Dd,SACAe,IAAI,CAAC,CAACC,OAASA,IAAI,CAAC,EAAE,EAAEzC,SAAS;IACrC,OAAO,IAAIuB,mBAAmB;QAC5Ba,SAAS,MAAM5B,MAAMkC,sBAAsB,CAAC;YAAEhB,SAASD,QAAQC,OAAO;QAAC;IACzE,OAAO;QACLU,SAAS,MAAM5B,MAAMmC,cAAc,CAACvB,OAAOK;IAC7C;IAEA,OAAO;QACLmB,WAAWR;IACb;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"countGlobalVersions.d.ts","sourceRoot":"","sources":["../src/countGlobalVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAUlD,eAAO,MAAM,mBAAmB,EAAE,
|
|
1
|
+
{"version":3,"file":"countGlobalVersions.d.ts","sourceRoot":"","sources":["../src/countGlobalVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAUlD,eAAO,MAAM,mBAAmB,EAAE,mBA2DjC,CAAA"}
|
|
@@ -17,6 +17,7 @@ export const countGlobalVersions = async function countGlobalVersions({ global:
|
|
|
17
17
|
adapter: this,
|
|
18
18
|
fields: buildVersionGlobalFields(this.payload.config, globalConfig, true),
|
|
19
19
|
locale,
|
|
20
|
+
req,
|
|
20
21
|
where
|
|
21
22
|
});
|
|
22
23
|
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/countGlobalVersions.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { CountGlobalVersions } from 'payload'\n\nimport { buildVersionGlobalFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const countGlobalVersions: CountGlobalVersions = async function countGlobalVersions(\n this: MongooseAdapter,\n { global: globalSlug, locale, req, where = {} },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })\n\n let hasNearConstraint = false\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 query = await buildQuery({\n adapter: this,\n fields: buildVersionGlobalFields(this.payload.config, globalConfig, true),\n locale,\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 options: CountOptions = {\n session: await getSession(this, req),\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 options.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\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 options.hint = {\n _id: 1,\n }\n }\n\n let result: number\n if (useEstimatedCount) {\n result = await Model.estimatedDocumentCount({ session: options.session })\n } else {\n result = await Model.countDocuments(query, options)\n }\n\n return {\n totalDocs: result,\n }\n}\n"],"names":["buildVersionGlobalFields","flattenWhereToOperators","buildQuery","getGlobal","getSession","countGlobalVersions","global","globalSlug","locale","req","where","globalConfig","Model","adapter","versions","hasNearConstraint","constraints","some","prop","Object","keys","key","query","fields","payload","config","useEstimatedCount","length","options","session","collation","localizationConfig","localization","defaultLocale","disableIndexHints","hint","_id","result","estimatedDocumentCount","countDocuments","totalDocs"],"mappings":"AAGA,SAASA,wBAAwB,EAAEC,uBAAuB,QAAQ,UAAS;AAI3E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,SAAS,QAAQ,2BAA0B;AACpD,SAASC,UAAU,QAAQ,4BAA2B;AAEtD,OAAO,MAAMC,sBAA2C,eAAeA,oBAErE,EAAEC,QAAQC,UAAU,EAAEC,MAAM,EAAEC,GAAG,EAAEC,QAAQ,CAAC,CAAC,EAAE;IAE/C,MAAM,EAAEC,YAAY,EAAEC,KAAK,EAAE,GAAGT,UAAU;QAAEU,SAAS,IAAI;QAAEN;QAAYO,UAAU;IAAK;IAEtF,IAAIC,oBAAoB;IAExB,IAAIL,OAAO;QACT,MAAMM,cAAcf,wBAAwBS;QAC5CK,oBAAoBC,YAAYC,IAAI,CAAC,CAACC,OAASC,OAAOC,IAAI,CAACF,MAAMD,IAAI,CAAC,CAACI,MAAQA,QAAQ;IACzF;IAEA,MAAMC,QAAQ,MAAMpB,WAAW;QAC7BW,SAAS,IAAI;QACbU,QAAQvB,yBAAyB,IAAI,CAACwB,OAAO,CAACC,MAAM,EAAEd,cAAc;QACpEH;
|
|
1
|
+
{"version":3,"sources":["../src/countGlobalVersions.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { CountGlobalVersions } from 'payload'\n\nimport { buildVersionGlobalFields, flattenWhereToOperators } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const countGlobalVersions: CountGlobalVersions = async function countGlobalVersions(\n this: MongooseAdapter,\n { global: globalSlug, locale, req, where = {} },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })\n\n let hasNearConstraint = false\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 query = await buildQuery({\n adapter: this,\n fields: buildVersionGlobalFields(this.payload.config, globalConfig, true),\n locale,\n req,\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 options: CountOptions = {\n session: await getSession(this, req),\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 options.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\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 options.hint = {\n _id: 1,\n }\n }\n\n let result: number\n if (useEstimatedCount) {\n result = await Model.estimatedDocumentCount({ session: options.session })\n } else {\n result = await Model.countDocuments(query, options)\n }\n\n return {\n totalDocs: result,\n }\n}\n"],"names":["buildVersionGlobalFields","flattenWhereToOperators","buildQuery","getGlobal","getSession","countGlobalVersions","global","globalSlug","locale","req","where","globalConfig","Model","adapter","versions","hasNearConstraint","constraints","some","prop","Object","keys","key","query","fields","payload","config","useEstimatedCount","length","options","session","collation","localizationConfig","localization","defaultLocale","disableIndexHints","hint","_id","result","estimatedDocumentCount","countDocuments","totalDocs"],"mappings":"AAGA,SAASA,wBAAwB,EAAEC,uBAAuB,QAAQ,UAAS;AAI3E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,SAAS,QAAQ,2BAA0B;AACpD,SAASC,UAAU,QAAQ,4BAA2B;AAEtD,OAAO,MAAMC,sBAA2C,eAAeA,oBAErE,EAAEC,QAAQC,UAAU,EAAEC,MAAM,EAAEC,GAAG,EAAEC,QAAQ,CAAC,CAAC,EAAE;IAE/C,MAAM,EAAEC,YAAY,EAAEC,KAAK,EAAE,GAAGT,UAAU;QAAEU,SAAS,IAAI;QAAEN;QAAYO,UAAU;IAAK;IAEtF,IAAIC,oBAAoB;IAExB,IAAIL,OAAO;QACT,MAAMM,cAAcf,wBAAwBS;QAC5CK,oBAAoBC,YAAYC,IAAI,CAAC,CAACC,OAASC,OAAOC,IAAI,CAACF,MAAMD,IAAI,CAAC,CAACI,MAAQA,QAAQ;IACzF;IAEA,MAAMC,QAAQ,MAAMpB,WAAW;QAC7BW,SAAS,IAAI;QACbU,QAAQvB,yBAAyB,IAAI,CAACwB,OAAO,CAACC,MAAM,EAAEd,cAAc;QACpEH;QACAC;QACAC;IACF;IAEA,4HAA4H;IAC5H,MAAMgB,oBAAoBX,qBAAqB,CAACO,SAASH,OAAOC,IAAI,CAACE,OAAOK,MAAM,KAAK;IAEvF,MAAMC,UAAwB;QAC5BC,SAAS,MAAMzB,WAAW,IAAI,EAAEK;IAClC;IAEA,IAAI,IAAI,CAACqB,SAAS,EAAE;QAClB,MAAMC,qBAAqB,IAAI,CAACP,OAAO,CAACC,MAAM,CAACO,YAAY;QAC3D,MAAMC,gBACJ,AAAC,OAAOF,uBAAuB,YAAYA,oBAAoBE,iBAAkB;QAEnFL,QAAQE,SAAS,GAAG;YAClBtB,QAAQA,UAAUA,WAAW,SAASA,WAAW,MAAMA,SAASyB;YAChE,GAAG,IAAI,CAACH,SAAS;QACnB;IACF;IAEA,IAAI,CAACJ,qBAAqBP,OAAOC,IAAI,CAACE,OAAOK,MAAM,KAAK,KAAK,IAAI,CAACO,iBAAiB,KAAK,MAAM;QAC5F,mHAAmH;QACnH,qHAAqH;QACrH,mHAAmH;QACnH,4BAA4B;QAC5BN,QAAQO,IAAI,GAAG;YACbC,KAAK;QACP;IACF;IAEA,IAAIC;IACJ,IAAIX,mBAAmB;QACrBW,SAAS,MAAMzB,MAAM0B,sBAAsB,CAAC;YAAET,SAASD,QAAQC,OAAO;QAAC;IACzE,OAAO;QACLQ,SAAS,MAAMzB,MAAM2B,cAAc,CAACjB,OAAOM;IAC7C;IAEA,OAAO;QACLY,WAAWH;IACb;AACF,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"countVersions.d.ts","sourceRoot":"","sources":["../src/countVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"countVersions.d.ts","sourceRoot":"","sources":["../src/countVersions.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAiB5C,eAAO,MAAM,aAAa,EAAE,aAqF3B,CAAA"}
|
package/dist/countVersions.js
CHANGED
|
@@ -1,13 +1,33 @@
|
|
|
1
|
-
import { buildVersionCollectionFields, flattenWhereToOperators } from 'payload';
|
|
1
|
+
import { buildVersionCollectionFields, flattenWhereToOperators, resolveBranchReadState, resolveBranchVersionHistoryQuery, rewriteBranchVersionParents } from 'payload';
|
|
2
|
+
import { buildBranchVisibilityStages } from './queries/buildBranchVisibility.js';
|
|
2
3
|
import { buildQuery } from './queries/buildQuery.js';
|
|
3
4
|
import { getCollection } from './utilities/getEntity.js';
|
|
4
5
|
import { getSession } from './utilities/getSession.js';
|
|
5
|
-
export const countVersions = async function countVersions({ collection: collectionSlug, locale, req, where = {} }) {
|
|
6
|
+
export const countVersions = async function countVersions({ branch, collection: collectionSlug, locale, req, where = {} }) {
|
|
6
7
|
const { collectionConfig, Model } = getCollection({
|
|
7
8
|
adapter: this,
|
|
8
9
|
collectionSlug,
|
|
9
10
|
versions: true
|
|
10
11
|
});
|
|
12
|
+
const branchReadState = resolveBranchReadState({
|
|
13
|
+
branch,
|
|
14
|
+
collectionSlug,
|
|
15
|
+
req
|
|
16
|
+
});
|
|
17
|
+
// Shares the list's predicate so the count in the Versions tab can never
|
|
18
|
+
// disagree with the rows the Versions view actually renders.
|
|
19
|
+
where = branchReadState.useBranching ? rewriteBranchVersionParents(where) ?? {} : await resolveBranchVersionHistoryQuery({
|
|
20
|
+
branch,
|
|
21
|
+
collectionSlug,
|
|
22
|
+
req,
|
|
23
|
+
where
|
|
24
|
+
}) ?? {};
|
|
25
|
+
const branchVisibility = branchReadState.useBranching ? buildBranchVisibilityStages({
|
|
26
|
+
adapter: this,
|
|
27
|
+
branch: branchReadState.branch,
|
|
28
|
+
collectionSlug,
|
|
29
|
+
mode: 'history'
|
|
30
|
+
}) : [];
|
|
11
31
|
let hasNearConstraint = false;
|
|
12
32
|
if (where) {
|
|
13
33
|
const constraints = flattenWhereToOperators(where);
|
|
@@ -15,12 +35,14 @@ export const countVersions = async function countVersions({ collection: collecti
|
|
|
15
35
|
}
|
|
16
36
|
const query = await buildQuery({
|
|
17
37
|
adapter: this,
|
|
38
|
+
branch,
|
|
18
39
|
fields: buildVersionCollectionFields(this.payload.config, collectionConfig, true),
|
|
19
40
|
locale,
|
|
41
|
+
req,
|
|
20
42
|
where
|
|
21
43
|
});
|
|
22
44
|
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
23
|
-
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0;
|
|
45
|
+
const useEstimatedCount = !branchVisibility.length && (hasNearConstraint || !query || Object.keys(query).length === 0);
|
|
24
46
|
const options = {
|
|
25
47
|
session: await getSession(this, req)
|
|
26
48
|
};
|
|
@@ -42,7 +64,17 @@ export const countVersions = async function countVersions({ collection: collecti
|
|
|
42
64
|
};
|
|
43
65
|
}
|
|
44
66
|
let result;
|
|
45
|
-
if (
|
|
67
|
+
if (branchVisibility.length) {
|
|
68
|
+
result = await Model.aggregate([
|
|
69
|
+
{
|
|
70
|
+
$match: query
|
|
71
|
+
},
|
|
72
|
+
...branchVisibility,
|
|
73
|
+
{
|
|
74
|
+
$count: 'count'
|
|
75
|
+
}
|
|
76
|
+
], options).then((rows)=>rows[0]?.count ?? 0);
|
|
77
|
+
} else if (useEstimatedCount) {
|
|
46
78
|
result = await Model.estimatedDocumentCount({
|
|
47
79
|
session: options.session
|
|
48
80
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/countVersions.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { CountVersions } from 'payload'\n\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/countVersions.ts"],"sourcesContent":["import type { CountOptions } from 'mongodb'\nimport type { CountVersions } from 'payload'\n\nimport {\n buildVersionCollectionFields,\n flattenWhereToOperators,\n resolveBranchReadState,\n resolveBranchVersionHistoryQuery,\n rewriteBranchVersionParents,\n} from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildBranchVisibilityStages } from './queries/buildBranchVisibility.js'\nimport { buildQuery } from './queries/buildQuery.js'\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\n\nexport const countVersions: CountVersions = async function countVersions(\n this: MongooseAdapter,\n { branch, collection: collectionSlug, locale, req, where = {} },\n) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug,\n versions: true,\n })\n const branchReadState = resolveBranchReadState({ branch, collectionSlug, req })\n\n // Shares the list's predicate so the count in the Versions tab can never\n // disagree with the rows the Versions view actually renders.\n where = branchReadState.useBranching\n ? (rewriteBranchVersionParents(where) ?? {})\n : ((await resolveBranchVersionHistoryQuery({ branch, collectionSlug, req, where })) ?? {})\n const branchVisibility = branchReadState.useBranching\n ? buildBranchVisibilityStages({\n adapter: this,\n branch: branchReadState.branch,\n collectionSlug,\n mode: 'history',\n })\n : []\n\n let hasNearConstraint = false\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 query = await buildQuery({\n adapter: this,\n branch,\n fields: buildVersionCollectionFields(this.payload.config, collectionConfig, true),\n locale,\n req,\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 =\n !branchVisibility.length && (hasNearConstraint || !query || Object.keys(query).length === 0)\n\n const options: CountOptions = {\n session: await getSession(this, req),\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 options.collation = {\n locale: locale && locale !== 'all' && locale !== '*' ? locale : defaultLocale,\n ...this.collation,\n }\n }\n\n if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {\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 options.hint = {\n _id: 1,\n }\n }\n\n let result: number\n if (branchVisibility.length) {\n result = await Model.aggregate(\n [{ $match: query }, ...branchVisibility, { $count: 'count' }],\n options,\n ).then((rows) => rows[0]?.count ?? 0)\n } else if (useEstimatedCount) {\n result = await Model.estimatedDocumentCount({ session: options.session })\n } else {\n result = await Model.countDocuments(query, options)\n }\n\n return {\n totalDocs: result,\n }\n}\n"],"names":["buildVersionCollectionFields","flattenWhereToOperators","resolveBranchReadState","resolveBranchVersionHistoryQuery","rewriteBranchVersionParents","buildBranchVisibilityStages","buildQuery","getCollection","getSession","countVersions","branch","collection","collectionSlug","locale","req","where","collectionConfig","Model","adapter","versions","branchReadState","useBranching","branchVisibility","mode","hasNearConstraint","constraints","some","prop","Object","keys","key","query","fields","payload","config","useEstimatedCount","length","options","session","collation","localizationConfig","localization","defaultLocale","disableIndexHints","hint","_id","result","aggregate","$match","$count","then","rows","count","estimatedDocumentCount","countDocuments","totalDocs"],"mappings":"AAGA,SACEA,4BAA4B,EAC5BC,uBAAuB,EACvBC,sBAAsB,EACtBC,gCAAgC,EAChCC,2BAA2B,QACtB,UAAS;AAIhB,SAASC,2BAA2B,QAAQ,qCAAoC;AAChF,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AAEtD,OAAO,MAAMC,gBAA+B,eAAeA,cAEzD,EAAEC,MAAM,EAAEC,YAAYC,cAAc,EAAEC,MAAM,EAAEC,GAAG,EAAEC,QAAQ,CAAC,CAAC,EAAE;IAE/D,MAAM,EAAEC,gBAAgB,EAAEC,KAAK,EAAE,GAAGV,cAAc;QAChDW,SAAS,IAAI;QACbN;QACAO,UAAU;IACZ;IACA,MAAMC,kBAAkBlB,uBAAuB;QAAEQ;QAAQE;QAAgBE;IAAI;IAE7E,yEAAyE;IACzE,6DAA6D;IAC7DC,QAAQK,gBAAgBC,YAAY,GAC/BjB,4BAA4BW,UAAU,CAAC,IACvC,AAAC,MAAMZ,iCAAiC;QAAEO;QAAQE;QAAgBE;QAAKC;IAAM,MAAO,CAAC;IAC1F,MAAMO,mBAAmBF,gBAAg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SAAS,MAAM7B,MAAM8B,SAAS,CAC5B;YAAC;gBAAEC,QAAQjB;YAAM;eAAMT;YAAkB;gBAAE2B,QAAQ;YAAQ;SAAE,EAC7DZ,SACAa,IAAI,CAAC,CAACC,OAASA,IAAI,CAAC,EAAE,EAAEC,SAAS;IACrC,OAAO,IAAIjB,mBAAmB;QAC5BW,SAAS,MAAM7B,MAAMoC,sBAAsB,CAAC;YAAEf,SAASD,QAAQC,OAAO;QAAC;IACzE,OAAO;QACLQ,SAAS,MAAM7B,MAAMqC,cAAc,CAACvB,OAAOM;IAC7C;IAEA,OAAO;QACLkB,WAAWT;IACb;AACF,EAAC"}
|
package/dist/create.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAWrC,eAAO,MAAM,MAAM,EAAE,
|
|
1
|
+
{"version":3,"file":"create.d.ts","sourceRoot":"","sources":["../src/create.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,SAAS,CAAA;AAWrC,eAAO,MAAM,MAAM,EAAE,MAgEpB,CAAA"}
|
package/dist/create.js
CHANGED
|
@@ -21,7 +21,8 @@ export const create = async function create({ collection: collectionSlug, custom
|
|
|
21
21
|
if (customIDType) {
|
|
22
22
|
data._id = data.id;
|
|
23
23
|
} else if (customID) {
|
|
24
|
-
|
|
24
|
+
// Mongoose 9 no longer treats numbers as valid ObjectIDs, so only strings are worth casting
|
|
25
|
+
if (typeof customID === 'string' && Types.ObjectId.isValid(customID)) {
|
|
25
26
|
data._id = new Types.ObjectId(customID);
|
|
26
27
|
} else {
|
|
27
28
|
data._id = customID;
|
package/dist/create.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/create.ts"],"sourcesContent":["import type { Create } from 'payload'\n\nimport { type CreateOptions, Types } from 'mongoose'\n\nimport type { MongooseAdapter } from './index.js'\n\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 create: Create = async function create(\n this: MongooseAdapter,\n { collection: collectionSlug, customID, data, req, returning },\n) {\n const { collectionConfig, customIDType, Model } = getCollection({ adapter: this, collectionSlug })\n\n let doc\n\n if (!data.createdAt) {\n data.createdAt = new Date().toISOString()\n }\n\n transform({\n adapter: this,\n data,\n fields: collectionConfig.fields,\n operation: 'write',\n })\n\n if (customIDType) {\n data._id = data.id\n } else if (customID) {\n if (Types.ObjectId.isValid(customID)) {\n data._id = new Types.ObjectId(customID)\n } else {\n data._id = customID\n }\n } else if (this.allowIDOnCreate && data.id) {\n try {\n data._id = new Types.ObjectId(data.id as string)\n } catch (error) {\n this.payload.logger.error(\n `It appears you passed ID to create operation data but it cannot be sanitized to ObjectID, value - ${JSON.stringify(data.id)}`,\n )\n throw error\n }\n }\n\n const options: CreateOptions = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n }\n\n try {\n ;[doc] = await Model.create([data], options)\n } catch (error) {\n handleError({ collection: collectionSlug, error, req })\n }\n if (returning === false) {\n return null\n }\n\n doc = doc.toObject()\n\n transform({\n adapter: this,\n data: doc,\n fields: collectionConfig.fields,\n operation: 'read',\n })\n\n return doc\n}\n"],"names":["Types","getCollection","getSession","handleError","transform","create","collection","collectionSlug","customID","data","req","returning","collectionConfig","customIDType","Model","adapter","doc","createdAt","Date","toISOString","fields","operation","_id","id","ObjectId","isValid","allowIDOnCreate","error","payload","logger","JSON","stringify","options","session","timestamps","toObject"],"mappings":"AAEA,SAA6BA,KAAK,QAAQ,WAAU;AAIpD,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,WAAW,QAAQ,6BAA4B;AACxD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,SAAiB,eAAeA,OAE3C,EAAEC,YAAYC,cAAc,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,GAAG,EAAEC,SAAS,EAAE;IAE9D,MAAM,EAAEC,gBAAgB,EAAEC,YAAY,EAAEC,KAAK,EAAE,GAAGb,cAAc;QAAEc,SAAS,IAAI;QAAER;IAAe;IAEhG,IAAIS;IAEJ,IAAI,CAACP,KAAKQ,SAAS,EAAE;QACnBR,KAAKQ,SAAS,GAAG,IAAIC,OAAOC,WAAW;IACzC;IAEAf,UAAU;QACRW,SAAS,IAAI;QACbN;QACAW,QAAQR,iBAAiBQ,MAAM;QAC/BC,WAAW;IACb;IAEA,IAAIR,cAAc;QAChBJ,KAAKa,GAAG,GAAGb,KAAKc,EAAE;IACpB,OAAO,IAAIf,UAAU;QACnB,
|
|
1
|
+
{"version":3,"sources":["../src/create.ts"],"sourcesContent":["import type { Create } from 'payload'\n\nimport { type CreateOptions, Types } from 'mongoose'\n\nimport type { MongooseAdapter } from './index.js'\n\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 create: Create = async function create(\n this: MongooseAdapter,\n { collection: collectionSlug, customID, data, req, returning },\n) {\n const { collectionConfig, customIDType, Model } = getCollection({ adapter: this, collectionSlug })\n\n let doc\n\n if (!data.createdAt) {\n data.createdAt = new Date().toISOString()\n }\n\n transform({\n adapter: this,\n data,\n fields: collectionConfig.fields,\n operation: 'write',\n })\n\n if (customIDType) {\n data._id = data.id\n } else if (customID) {\n // Mongoose 9 no longer treats numbers as valid ObjectIDs, so only strings are worth casting\n if (typeof customID === 'string' && Types.ObjectId.isValid(customID)) {\n data._id = new Types.ObjectId(customID)\n } else {\n data._id = customID\n }\n } else if (this.allowIDOnCreate && data.id) {\n try {\n data._id = new Types.ObjectId(data.id as string)\n } catch (error) {\n this.payload.logger.error(\n `It appears you passed ID to create operation data but it cannot be sanitized to ObjectID, value - ${JSON.stringify(data.id)}`,\n )\n throw error\n }\n }\n\n const options: CreateOptions = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n }\n\n try {\n ;[doc] = await Model.create([data], options)\n } catch (error) {\n handleError({ collection: collectionSlug, error, req })\n }\n if (returning === false) {\n return null\n }\n\n doc = doc.toObject()\n\n transform({\n adapter: this,\n data: doc,\n fields: collectionConfig.fields,\n operation: 'read',\n })\n\n return doc\n}\n"],"names":["Types","getCollection","getSession","handleError","transform","create","collection","collectionSlug","customID","data","req","returning","collectionConfig","customIDType","Model","adapter","doc","createdAt","Date","toISOString","fields","operation","_id","id","ObjectId","isValid","allowIDOnCreate","error","payload","logger","JSON","stringify","options","session","timestamps","toObject"],"mappings":"AAEA,SAA6BA,KAAK,QAAQ,WAAU;AAIpD,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,WAAW,QAAQ,6BAA4B;AACxD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,SAAiB,eAAeA,OAE3C,EAAEC,YAAYC,cAAc,EAAEC,QAAQ,EAAEC,IAAI,EAAEC,GAAG,EAAEC,SAAS,EAAE;IAE9D,MAAM,EAAEC,gBAAgB,EAAEC,YAAY,EAAEC,KAAK,EAAE,GAAGb,cAAc;QAAEc,SAAS,IAAI;QAAER;IAAe;IAEhG,IAAIS;IAEJ,IAAI,CAACP,KAAKQ,SAAS,EAAE;QACnBR,KAAKQ,SAAS,GAAG,IAAIC,OAAOC,WAAW;IACzC;IAEAf,UAAU;QACRW,SAAS,IAAI;QACbN;QACAW,QAAQR,iBAAiBQ,MAAM;QAC/BC,WAAW;IACb;IAEA,IAAIR,cAAc;QAChBJ,KAAKa,GAAG,GAAGb,KAAKc,EAAE;IACpB,OAAO,IAAIf,UAAU;QACnB,4FAA4F;QAC5F,IAAI,OAAOA,aAAa,YAAYR,MAAMwB,QAAQ,CAACC,OAAO,CAACjB,WAAW;YACpEC,KAAKa,GAAG,GAAG,IAAItB,MAAMwB,QAAQ,CAAChB;QAChC,OAAO;YACLC,KAAKa,GAAG,GAAGd;QACb;IACF,OAAO,IAAI,IAAI,CAACkB,eAAe,IAAIjB,KAAKc,EAAE,EAAE;QAC1C,IAAI;YACFd,KAAKa,GAAG,GAAG,IAAItB,MAAMwB,QAAQ,CAACf,KAAKc,EAAE;QACvC,EAAE,OAAOI,OAAO;YACd,IAAI,CAACC,OAAO,CAACC,MAAM,CAACF,KAAK,CACvB,CAAC,kGAAkG,EAAEG,KAAKC,SAAS,CAACtB,KAAKc,EAAE,GAAG;YAEhI,MAAMI;QACR;IACF;IAEA,MAAMK,UAAyB;QAC7BC,SAAS,MAAM/B,WAAW,IAAI,EAAEQ;QAChC,uDAAuD;QACvDwB,YAAY;IACd;IAEA,IAAI;;QACD,CAAClB,IAAI,GAAG,MAAMF,MAAMT,MAAM,CAAC;YAACI;SAAK,EAAEuB;IACtC,EAAE,OAAOL,OAAO;QACdxB,YAAY;YAAEG,YAAYC;YAAgBoB;YAAOjB;QAAI;IACvD;IACA,IAAIC,cAAc,OAAO;QACvB,OAAO;IACT;IAEAK,MAAMA,IAAImB,QAAQ;IAElB/B,UAAU;QACRW,SAAS,IAAI;QACbN,MAAMO;QACNI,QAAQR,iBAAiBQ,MAAM;QAC/BC,WAAW;IACb;IAEA,OAAOL;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createGlobalVersion.d.ts","sourceRoot":"","sources":["../src/createGlobalVersion.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"createGlobalVersion.d.ts","sourceRoot":"","sources":["../src/createGlobalVersion.ts"],"names":[],"mappings":"AACA,OAAO,EAA4B,KAAK,mBAAmB,EAAE,MAAM,SAAS,CAAA;AAQ5E,eAAO,MAAM,mBAAmB,EAAE,mBA6FjC,CAAA"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { MAIN_BRANCH, resolveBranch } from 'payload';
|
|
1
2
|
import { buildVersionGlobalFields } from 'payload';
|
|
2
3
|
import { getGlobal } from './utilities/getEntity.js';
|
|
3
4
|
import { getSession } from './utilities/getSession.js';
|
|
@@ -8,7 +9,9 @@ export const createGlobalVersion = async function createGlobalVersion({ autosave
|
|
|
8
9
|
globalSlug,
|
|
9
10
|
versions: true
|
|
10
11
|
});
|
|
12
|
+
const versionBranch = req?.payload?.config?.branching?.branchableGlobals?.has(globalSlug) ? resolveBranch(req) : MAIN_BRANCH;
|
|
11
13
|
const data = {
|
|
14
|
+
_branch: versionBranch,
|
|
12
15
|
autosave,
|
|
13
16
|
createdAt,
|
|
14
17
|
latest: true,
|
|
@@ -34,7 +37,10 @@ export const createGlobalVersion = async function createGlobalVersion({ autosave
|
|
|
34
37
|
};
|
|
35
38
|
let [doc] = await Model.create([
|
|
36
39
|
data
|
|
37
|
-
], options
|
|
40
|
+
], options);
|
|
41
|
+
// Scoped by `_branch`. Unlike collection versions, global versions have no
|
|
42
|
+
// `parent` to separate streams by — every version of a global shares one — so
|
|
43
|
+
// without this an unscoped clear would wipe main's latest flag from a branch.
|
|
38
44
|
await Model.updateMany({
|
|
39
45
|
$and: [
|
|
40
46
|
{
|
|
@@ -46,6 +52,22 @@ export const createGlobalVersion = async function createGlobalVersion({ autosave
|
|
|
46
52
|
latest: {
|
|
47
53
|
$eq: true
|
|
48
54
|
}
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
$or: [
|
|
58
|
+
{
|
|
59
|
+
_branch: {
|
|
60
|
+
$eq: versionBranch
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
...versionBranch === MAIN_BRANCH ? [
|
|
64
|
+
{
|
|
65
|
+
_branch: {
|
|
66
|
+
$exists: false
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
] : []
|
|
70
|
+
]
|
|
49
71
|
}
|
|
50
72
|
]
|
|
51
73
|
}, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createGlobalVersion.ts"],"sourcesContent":["import { buildVersionGlobalFields, type CreateGlobalVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(\n this: MongooseAdapter,\n {\n autosave,\n createdAt,\n globalSlug,\n publishedLocale,\n req,\n returning,\n snapshot,\n updatedAt,\n versionData,\n },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })\n\n const data = {\n autosave,\n createdAt,\n latest: true,\n publishedLocale,\n snapshot,\n updatedAt,\n version: versionData,\n }\n if (!data.createdAt) {\n data.createdAt = new Date().toISOString()\n }\n\n const fields = buildVersionGlobalFields(this.payload.config, globalConfig)\n\n transform({\n adapter: this,\n data,\n fields,\n operation: 'write',\n })\n\n const options = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n }\n\n let [doc] = await Model.create([data], options,
|
|
1
|
+
{"version":3,"sources":["../src/createGlobalVersion.ts"],"sourcesContent":["import { MAIN_BRANCH, resolveBranch } from 'payload'\nimport { buildVersionGlobalFields, type CreateGlobalVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getGlobal } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const createGlobalVersion: CreateGlobalVersion = async function createGlobalVersion(\n this: MongooseAdapter,\n {\n autosave,\n createdAt,\n globalSlug,\n publishedLocale,\n req,\n returning,\n snapshot,\n updatedAt,\n versionData,\n },\n) {\n const { globalConfig, Model } = getGlobal({ adapter: this, globalSlug, versions: true })\n\n const versionBranch = req?.payload?.config?.branching?.branchableGlobals?.has(globalSlug)\n ? resolveBranch(req as never)\n : MAIN_BRANCH\n\n const data = {\n _branch: versionBranch,\n autosave,\n createdAt,\n latest: true,\n publishedLocale,\n snapshot,\n updatedAt,\n version: versionData,\n }\n if (!data.createdAt) {\n data.createdAt = new Date().toISOString()\n }\n\n const fields = buildVersionGlobalFields(this.payload.config, globalConfig)\n\n transform({\n adapter: this,\n data,\n fields,\n operation: 'write',\n })\n\n const options = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n }\n\n let [doc] = await Model.create([data], options)\n\n // Scoped by `_branch`. Unlike collection versions, global versions have no\n // `parent` to separate streams by — every version of a global shares one — so\n // without this an unscoped clear would wipe main's latest flag from a branch.\n await Model.updateMany(\n {\n $and: [\n {\n _id: {\n $ne: doc._id,\n },\n },\n {\n latest: {\n $eq: true,\n },\n },\n {\n $or: [\n { _branch: { $eq: versionBranch } },\n ...(versionBranch === MAIN_BRANCH ? [{ _branch: { $exists: false } }] : []),\n ],\n },\n ],\n },\n { $unset: { latest: 1 } },\n options,\n )\n\n if (returning === false) {\n return null\n }\n\n doc = doc.toObject()\n\n transform({\n adapter: this,\n data: doc,\n fields,\n operation: 'read',\n })\n\n return doc\n}\n"],"names":["MAIN_BRANCH","resolveBranch","buildVersionGlobalFields","getGlobal","getSession","transform","createGlobalVersion","autosave","createdAt","globalSlug","publishedLocale","req","returning","snapshot","updatedAt","versionData","globalConfig","Model","adapter","versions","versionBranch","payload","config","branching","branchableGlobals","has","data","_branch","latest","version","Date","toISOString","fields","operation","options","session","timestamps","doc","create","updateMany","$and","_id","$ne","$eq","$or","$exists","$unset","toObject"],"mappings":"AAAA,SAASA,WAAW,EAAEC,aAAa,QAAQ,UAAS;AACpD,SAASC,wBAAwB,QAAkC,UAAS;AAI5E,SAASC,SAAS,QAAQ,2BAA0B;AACpD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,sBAA2C,eAAeA,oBAErE,EACEC,QAAQ,EACRC,SAAS,EACTC,UAAU,EACVC,eAAe,EACfC,GAAG,EACH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AAMA,IAAIU,QAAQ;IAElB1C,UAAU;QACRa,SAAS,IAAI;QACbQ,MAAMW;QACNL;QACAC,WAAW;IACb;IAEA,OAAOI;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createMigration.d.ts","sourceRoot":"","sources":["../src/createMigration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAyB,MAAM,SAAS,CAAA;AAqBrE,eAAO,MAAM,eAAe,EAAE,
|
|
1
|
+
{"version":3,"file":"createMigration.d.ts","sourceRoot":"","sources":["../src/createMigration.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAyB,MAAM,SAAS,CAAA;AAqBrE,eAAO,MAAM,eAAe,EAAE,eA4C7B,CAAA"}
|
package/dist/createMigration.js
CHANGED
|
@@ -36,15 +36,22 @@ export const createMigration = async function createMigration({ file, migrationN
|
|
|
36
36
|
const formattedName = migrationName?.replace(/\W/g, '_');
|
|
37
37
|
const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`;
|
|
38
38
|
const filePath = `${dir}/${fileName}`;
|
|
39
|
-
if (
|
|
40
|
-
|
|
39
|
+
if (skipEmpty) {
|
|
40
|
+
return {
|
|
41
|
+
created: false
|
|
42
|
+
};
|
|
41
43
|
}
|
|
44
|
+
fs.writeFileSync(filePath, migrationFileContent);
|
|
42
45
|
writeMigrationIndex({
|
|
43
46
|
migrationsDir: payload.db.migrationDir
|
|
44
47
|
});
|
|
45
48
|
payload.logger.info({
|
|
46
49
|
msg: `Migration created at ${filePath}`
|
|
47
50
|
});
|
|
51
|
+
return {
|
|
52
|
+
created: true,
|
|
53
|
+
path: filePath
|
|
54
|
+
};
|
|
48
55
|
};
|
|
49
56
|
|
|
50
57
|
//# sourceMappingURL=createMigration.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createMigration.ts"],"sourcesContent":["import type { CreateMigration, MigrationTemplateArgs } from 'payload'\n\nimport fs from 'fs'\nimport path from 'path'\nimport { getPredefinedMigration, writeMigrationIndex } from 'payload'\nimport { fileURLToPath } from 'url'\n\nconst migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {\n MigrateDownArgs,\n MigrateUpArgs,\n} from '@payloadcms/db-mongodb'\n${imports ?? ''}\nexport async function up({ payload, req, session }: MigrateUpArgs): Promise<void> {\n${upSQL ?? ` // Migration code`}\n}\n\nexport async function down({ payload, req, session }: MigrateDownArgs): Promise<void> {\n${downSQL ?? ` // Migration code`}\n}\n`\n\nexport const createMigration: CreateMigration = async function createMigration({\n file,\n migrationName,\n payload,\n skipEmpty,\n}) {\n const filename = fileURLToPath(import.meta.url)\n const dirname = path.dirname(filename)\n\n const dir = payload.db.migrationDir\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir)\n }\n const predefinedMigration = await getPredefinedMigration({\n dirname,\n file,\n migrationName,\n payload,\n })\n\n const migrationFileContent = migrationTemplate(predefinedMigration)\n\n const [yyymmdd, hhmmss] = new Date().toISOString().split('T')\n\n const formattedDate = yyymmdd!.replace(/\\D/g, '')\n const formattedTime = hhmmss!.split('.')[0]!.replace(/\\D/g, '')\n\n const timestamp = `${formattedDate}_${formattedTime}`\n\n const formattedName = migrationName?.replace(/\\W/g, '_')\n const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`\n const filePath = `${dir}/${fileName}`\n\n if (
|
|
1
|
+
{"version":3,"sources":["../src/createMigration.ts"],"sourcesContent":["import type { CreateMigration, MigrationTemplateArgs } from 'payload'\n\nimport fs from 'fs'\nimport path from 'path'\nimport { getPredefinedMigration, writeMigrationIndex } from 'payload'\nimport { fileURLToPath } from 'url'\n\nconst migrationTemplate = ({ downSQL, imports, upSQL }: MigrationTemplateArgs): string => `import {\n MigrateDownArgs,\n MigrateUpArgs,\n} from '@payloadcms/db-mongodb'\n${imports ?? ''}\nexport async function up({ payload, req, session }: MigrateUpArgs): Promise<void> {\n${upSQL ?? ` // Migration code`}\n}\n\nexport async function down({ payload, req, session }: MigrateDownArgs): Promise<void> {\n${downSQL ?? ` // Migration code`}\n}\n`\n\nexport const createMigration: CreateMigration = async function createMigration({\n file,\n migrationName,\n payload,\n skipEmpty,\n}) {\n const filename = fileURLToPath(import.meta.url)\n const dirname = path.dirname(filename)\n\n const dir = payload.db.migrationDir\n if (!fs.existsSync(dir)) {\n fs.mkdirSync(dir)\n }\n const predefinedMigration = await getPredefinedMigration({\n dirname,\n file,\n migrationName,\n payload,\n })\n\n const migrationFileContent = migrationTemplate(predefinedMigration)\n\n const [yyymmdd, hhmmss] = new Date().toISOString().split('T')\n\n const formattedDate = yyymmdd!.replace(/\\D/g, '')\n const formattedTime = hhmmss!.split('.')[0]!.replace(/\\D/g, '')\n\n const timestamp = `${formattedDate}_${formattedTime}`\n\n const formattedName = migrationName?.replace(/\\W/g, '_')\n const fileName = migrationName ? `${timestamp}_${formattedName}.ts` : `${timestamp}_migration.ts`\n const filePath = `${dir}/${fileName}`\n\n if (skipEmpty) {\n return { created: false }\n }\n\n fs.writeFileSync(filePath, migrationFileContent)\n\n writeMigrationIndex({ migrationsDir: payload.db.migrationDir })\n\n payload.logger.info({ msg: `Migration created at ${filePath}` })\n\n return { created: true, path: filePath }\n}\n"],"names":["fs","path","getPredefinedMigration","writeMigrationIndex","fileURLToPath","migrationTemplate","downSQL","imports","upSQL","createMigration","file","migrationName","payload","skipEmpty","filename","url","dirname","dir","db","migrationDir","existsSync","mkdirSync","predefinedMigration","migrationFileContent","yyymmdd","hhmmss","Date","toISOString","split","formattedDate","replace","formattedTime","timestamp","formattedName","fileName","filePath","created","writeFileSync","migrationsDir","logger","info","msg"],"mappings":"AAEA,OAAOA,QAAQ,KAAI;AACnB,OAAOC,UAAU,OAAM;AACvB,SAASC,sBAAsB,EAAEC,mBAAmB,QAAQ,UAAS;AACrE,SAASC,aAAa,QAAQ,MAAK;AAEnC,MAAMC,oBAAoB,CAAC,EA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;IAE7DP,QAAQ2B,MAAM,CAACC,IAAI,CAAC;QAAEC,KAAK,CAAC,qBAAqB,EAAEN,UAAU;IAAC;IAE9D,OAAO;QAAEC,SAAS;QAAMnC,MAAMkC;IAAS;AACzC,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createVersion.d.ts","sourceRoot":"","sources":["../src/createVersion.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"createVersion.d.ts","sourceRoot":"","sources":["../src/createVersion.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,aAAa,EAEnB,MAAM,SAAS,CAAA;AAQhB,eAAO,MAAM,aAAa,EAAE,aAkH3B,CAAA"}
|
package/dist/createVersion.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { buildVersionCollectionFields } from 'payload';
|
|
1
|
+
import { buildVersionCollectionFields, resolveBranchVersionParent } from 'payload';
|
|
2
2
|
import { getCollection } from './utilities/getEntity.js';
|
|
3
3
|
import { getSession } from './utilities/getSession.js';
|
|
4
4
|
import { transform } from './utilities/transform.js';
|
|
@@ -8,15 +8,25 @@ export const createVersion = async function createVersion({ autosave, collection
|
|
|
8
8
|
collectionSlug,
|
|
9
9
|
versions: true
|
|
10
10
|
});
|
|
11
|
+
const branched = await resolveBranchVersionParent({
|
|
12
|
+
collectionSlug,
|
|
13
|
+
parent,
|
|
14
|
+
req,
|
|
15
|
+
versionData: versionData
|
|
16
|
+
});
|
|
11
17
|
const data = {
|
|
12
18
|
autosave,
|
|
13
19
|
createdAt,
|
|
14
20
|
latest: true,
|
|
15
|
-
parent,
|
|
21
|
+
parent: branched.parent,
|
|
16
22
|
publishedLocale,
|
|
17
23
|
snapshot,
|
|
18
24
|
updatedAt,
|
|
19
|
-
version: versionData
|
|
25
|
+
version: versionData,
|
|
26
|
+
...branched.versionData._branch ? {
|
|
27
|
+
_branch: branched.versionData._branch,
|
|
28
|
+
_branchParent: branched.versionData._branchParent
|
|
29
|
+
} : {}
|
|
20
30
|
};
|
|
21
31
|
if (!data.createdAt) {
|
|
22
32
|
data.createdAt = new Date().toISOString();
|
|
@@ -35,7 +45,10 @@ export const createVersion = async function createVersion({ autosave, collection
|
|
|
35
45
|
};
|
|
36
46
|
let [doc] = await Model.create([
|
|
37
47
|
data
|
|
38
|
-
], options
|
|
48
|
+
], options);
|
|
49
|
+
// No branch scoping needed here: a branch version's `parent` is the shadow
|
|
50
|
+
// row's own primary key, so main's chain and the branch's chain are already
|
|
51
|
+
// disjoint and `latest` clearing cannot cross between them.
|
|
39
52
|
const parentQuery = {
|
|
40
53
|
$or: [
|
|
41
54
|
{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createVersion.ts"],"sourcesContent":["import {
|
|
1
|
+
{"version":3,"sources":["../src/createVersion.ts"],"sourcesContent":["import {\n buildVersionCollectionFields,\n type CreateVersion,\n resolveBranchVersionParent,\n} from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { getCollection } from './utilities/getEntity.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const createVersion: CreateVersion = async function createVersion(\n this: MongooseAdapter,\n {\n autosave,\n collectionSlug,\n createdAt,\n parent,\n publishedLocale,\n req,\n returning,\n snapshot,\n updatedAt,\n versionData,\n },\n) {\n const { collectionConfig, Model } = getCollection({\n adapter: this,\n collectionSlug,\n versions: true,\n })\n\n const branched = await resolveBranchVersionParent({\n collectionSlug,\n parent,\n req,\n versionData: versionData as Record<string, unknown>,\n })\n\n const data = {\n autosave,\n createdAt,\n latest: true,\n parent: branched.parent,\n publishedLocale,\n snapshot,\n updatedAt,\n version: versionData,\n ...(branched.versionData._branch\n ? { _branch: branched.versionData._branch, _branchParent: branched.versionData._branchParent }\n : {}),\n }\n if (!data.createdAt) {\n data.createdAt = new Date().toISOString()\n }\n\n const fields = buildVersionCollectionFields(this.payload.config, collectionConfig)\n\n transform({\n adapter: this,\n data,\n fields,\n operation: 'write',\n })\n\n const options = {\n session: await getSession(this, req),\n // Timestamps are manually added by the write transform\n timestamps: false,\n }\n\n let [doc] = await Model.create([data], options)\n\n // No branch scoping needed here: a branch version's `parent` is the shadow\n // row's own primary key, so main's chain and the branch's chain are already\n // disjoint and `latest` clearing cannot cross between them.\n const parentQuery = {\n $or: [\n {\n parent: {\n $eq: data.parent,\n },\n },\n ],\n }\n\n await Model.updateMany(\n {\n $and: [\n {\n _id: {\n $ne: doc._id,\n },\n },\n parentQuery,\n {\n latest: {\n $eq: true,\n },\n },\n {\n updatedAt: {\n $lt: new Date(doc.updatedAt),\n },\n },\n ],\n },\n { $unset: { latest: 1 } },\n options,\n )\n\n if (returning === false) {\n return null\n }\n\n doc = doc.toObject()\n\n transform({\n adapter: this,\n data: doc,\n fields,\n operation: 'read',\n })\n\n return doc\n}\n"],"names":["buildVersionCollectionFields","resolveBranchVersionParent","getCollection","getSession","transform","createVersion","autosave","collectionSlug","createdAt","parent","publishedLocale","req","returning","snapshot","updatedAt","versionData","collectionConfig","Model","adapter","versions","branched","data","latest","version","_branch","_branchParent","Date","toISOString","fields","payload","config","operation","options","session","timestamps","doc","create","parentQuery","$or","$eq","updateMany","$and","_id","$ne","$lt","$unset","toObject"],"mappings":"AAAA,SACEA,4BAA4B,EAE5BC,0BAA0B,QACrB,UAAS;AAIhB,SAASC,aAAa,QAAQ,2BAA0B;AACxD,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,gBAA+B,eAAeA,cAEzD,EACEC,QAAQ,EACRC,cAAc,EACdC,SAAS,EACTC,MAAM,EACNC,eAAe,EACfC,GAAG,EACHC,SAAS,EACTC,QAAQ,EACRC,SAAS,EACTC,WAAW,EACZ;IAED,MAAM,EAAEC,gBAAgB,EAAEC,KAAK,EAAE,GAAGf,cAAc;QAChDgB,SAAS,IAAI;QACbX;QACAY,UAAU;IACZ;IAEA,MAAMC,WAAW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1C,UAAU;QACRc,SAAS,IAAI;QACbG,MAAMc;QACNP;QACAG,WAAW;IACb;IAEA,OAAOI;AACT,EAAC"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { DeleteBranchGlobal } from 'payload';
|
|
2
|
+
/**
|
|
3
|
+
* Drops a branch's copy of a global.
|
|
4
|
+
*
|
|
5
|
+
* Every global lives in one discriminated collection here, so the branch's copy is a
|
|
6
|
+
* sibling row identified by `globalType` and `_branch` — addressed directly rather than
|
|
7
|
+
* through any generic delete, since no other part of Payload deletes a global.
|
|
8
|
+
*/
|
|
9
|
+
export declare const deleteBranchGlobal: DeleteBranchGlobal;
|
|
10
|
+
//# sourceMappingURL=deleteBranchGlobal.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deleteBranchGlobal.d.ts","sourceRoot":"","sources":["../src/deleteBranchGlobal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AAOjD;;;;;;GAMG;AACH,eAAO,MAAM,kBAAkB,EAAE,kBAUhC,CAAA"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getGlobal } from './utilities/getEntity.js';
|
|
2
|
+
import { getSession } from './utilities/getSession.js';
|
|
3
|
+
/**
|
|
4
|
+
* Drops a branch's copy of a global.
|
|
5
|
+
*
|
|
6
|
+
* Every global lives in one discriminated collection here, so the branch's copy is a
|
|
7
|
+
* sibling row identified by `globalType` and `_branch` — addressed directly rather than
|
|
8
|
+
* through any generic delete, since no other part of Payload deletes a global.
|
|
9
|
+
*/ export const deleteBranchGlobal = async function deleteBranchGlobal({ branch, globalSlug, req }) {
|
|
10
|
+
const { Model } = getGlobal({
|
|
11
|
+
adapter: this,
|
|
12
|
+
globalSlug
|
|
13
|
+
});
|
|
14
|
+
await Model.deleteOne({
|
|
15
|
+
_branch: branch,
|
|
16
|
+
globalType: globalSlug
|
|
17
|
+
}, {
|
|
18
|
+
session: await getSession(this, req)
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=deleteBranchGlobal.js.map
|