@payloadcms/db-mongodb 3.0.0-beta.11 → 3.0.0-beta.12
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 +3 -0
- package/dist/count.d.ts.map +1 -0
- package/dist/count.js +33 -0
- package/dist/count.js.map +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +5 -1
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/package.json +4 -4
- package/src/index.ts +0 -198
package/dist/count.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"count.d.ts","sourceRoot":"","sources":["../src/count.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,kBAAkB,CAAA;AAS7C,eAAO,MAAM,KAAK,EAAE,KAsCnB,CAAA"}
|
package/dist/count.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { flattenWhereToOperators } from 'payload/database';
|
|
2
|
+
import { withSession } from './withSession.js';
|
|
3
|
+
export const count = async function count({ collection, locale, req = {}, where }) {
|
|
4
|
+
const Model = this.collections[collection];
|
|
5
|
+
const options = withSession(this, req.transactionID);
|
|
6
|
+
let hasNearConstraint = false;
|
|
7
|
+
if (where) {
|
|
8
|
+
const constraints = flattenWhereToOperators(where);
|
|
9
|
+
hasNearConstraint = constraints.some((prop)=>Object.keys(prop).some((key)=>key === 'near'));
|
|
10
|
+
}
|
|
11
|
+
const query = await Model.buildQuery({
|
|
12
|
+
locale,
|
|
13
|
+
payload: this.payload,
|
|
14
|
+
where
|
|
15
|
+
});
|
|
16
|
+
// useEstimatedCount is faster, but not accurate, as it ignores any filters. It is thus set to true if there are no filters.
|
|
17
|
+
const useEstimatedCount = hasNearConstraint || !query || Object.keys(query).length === 0;
|
|
18
|
+
if (!useEstimatedCount && Object.keys(query).length === 0 && this.disableIndexHints !== true) {
|
|
19
|
+
// Improve the performance of the countDocuments query which is used if useEstimatedCount is set to false by adding
|
|
20
|
+
// a hint. By default, if no hint is provided, MongoDB does not use an indexed field to count the returned documents,
|
|
21
|
+
// which makes queries very slow. This only happens when no query (filter) is provided. If one is provided, it uses
|
|
22
|
+
// the correct indexed field
|
|
23
|
+
options.hint = {
|
|
24
|
+
_id: 1
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
const result = await Model.countDocuments(query, options);
|
|
28
|
+
return {
|
|
29
|
+
totalDocs: result
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
//# sourceMappingURL=count.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/count.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { Count } from 'payload/database'\nimport type { PayloadRequest } from 'payload/types'\n\nimport { flattenWhereToOperators } from 'payload/database'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { withSession } from './withSession.js'\n\nexport const count: Count = async function count(\n this: MongooseAdapter,\n { collection, locale, req = {} as PayloadRequest, where },\n) {\n const Model = this.collections[collection]\n const options: QueryOptions = withSession(this, req.transactionID)\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 Model.buildQuery({\n locale,\n payload: this.payload,\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 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 const result = await Model.countDocuments(query, options)\n\n return {\n totalDocs: result,\n }\n}\n"],"names":["flattenWhereToOperators","withSession","count","collection","locale","req","where","Model","collections","options","transactionID","hasNearConstraint","constraints","some","prop","Object","keys","key","query","buildQuery","payload","useEstimatedCount","length","disableIndexHints","hint","_id","result","countDocuments","totalDocs"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAIA,SAASA,uBAAuB,QAAQ,mBAAkB;AAI1D,SAASC,WAAW,QAAQ,mBAAkB;AAE9C,OAAO,MAAMC,QAAe,eAAeA,MAEzC,EAAEC,UAAU,EAAEC,MAAM,EAAEC,MAAM,CAAC,CAAmB,EAAEC,KAAK,EAAE;IAEzD,MAAMC,QAAQ,IAAI,CAACC,WAAW,CAACL,WAAW;IAC1C,MAAMM,UAAwBR,YAAY,IAAI,EAAEI,IAAIK,aAAa;IAEjE,IAAIC,oBAAoB;IAExB,IAAIL,OAAO;QACT,MAAMM,cAAcZ,wBAAwBM;QAC5CK,oBAAoBC,YAAYC,IAAI,CAAC,CAACC,OAASC,OAAOC,IAAI,CAACF,MAAMD,IAAI,CAAC,CAACI,MAAQA,QAAQ;IACzF;IAEA,MAAMC,QAAQ,MAAMX,MAAMY,UAAU,CAAC;QACnCf;QACAgB,SAAS,IAAI,CAACA,OAAO;QACrBd;IACF;IAEA,4HAA4H;IAC5H,MAAMe,oBAAoBV,qBAAqB,CAACO,SAASH,OAAOC,IAAI,CAACE,OAAOI,MAAM,KAAK;IAEvF,IAAI,CAACD,qBAAqBN,OAAOC,IAAI,CAACE,OAAOI,MAAM,KAAK,KAAK,IAAI,CAACC,iBAAiB,KAAK,MAAM;QAC5F,mHAAmH;QACnH,qHAAqH;QACrH,mHAAmH;QACnH,4BAA4B;QAC5Bd,QAAQe,IAAI,GAAG;YACbC,KAAK;QACP;IACF;IAEA,MAAMC,SAAS,MAAMnB,MAAMoB,cAAc,CAACT,OAAOT;IAEjD,OAAO;QACLmB,WAAWF;IACb;AACF,EAAC"}
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AACjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAEzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAO/E,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAAA,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,SAAS,CAAA;AACjD,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAA;AAC/D,OAAO,KAAK,EAAE,aAAa,EAAE,cAAc,EAAE,UAAU,EAAE,MAAM,UAAU,CAAA;AAEzE,OAAO,KAAK,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAA;AAO/E,OAAO,KAAK,EAAE,eAAe,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AA6B9D,YAAY,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,YAAY,CAAA;AAEhE,MAAM,WAAW,IAAI;IACnB,uFAAuF;IACvF,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,kCAAkC;IAClC,cAAc,CAAC,EAAE,cAAc,GAAG;QAChC,4FAA4F;QAC5F,QAAQ,CAAC,EAAE,OAAO,CAAA;KACnB,CAAA;IACD,gOAAgO;I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C7B,iBAAiB,EACjB,kBAAuB,EACvB,GAAG,GACJ,EAAE,IAAI,GAAG,kBAAkB,CAyD3B"}
|
package/dist/index.js
CHANGED
|
@@ -3,6 +3,7 @@ import mongoose from 'mongoose';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { createDatabaseAdapter } from 'payload/database';
|
|
5
5
|
import { connect } from './connect.js';
|
|
6
|
+
import { count } from './count.js';
|
|
6
7
|
import { create } from './create.js';
|
|
7
8
|
import { createGlobal } from './createGlobal.js';
|
|
8
9
|
import { createGlobalVersion } from './createGlobalVersion.js';
|
|
@@ -38,6 +39,7 @@ export function mongooseAdapter({ autoPluralization = true, connectOptions, disa
|
|
|
38
39
|
collections: {},
|
|
39
40
|
connectOptions: connectOptions || {},
|
|
40
41
|
connection: undefined,
|
|
42
|
+
count,
|
|
41
43
|
disableIndexHints,
|
|
42
44
|
globals: undefined,
|
|
43
45
|
mongoMemoryServer,
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { TransactionOptions } from 'mongodb'\nimport type { MongoMemoryReplSet } from 'mongodb-memory-server'\nimport type { ClientSession, ConnectOptions, Connection } from 'mongoose'\nimport type { Payload } from 'payload'\nimport type { BaseDatabaseAdapter, DatabaseAdapterObj } from 'payload/database'\n\nimport fs from 'fs'\nimport mongoose from 'mongoose'\nimport path from 'path'\nimport { createDatabaseAdapter } from 'payload/database'\n\nimport type { CollectionModel, GlobalModel } from './types.js'\n\nimport { connect } from './connect.js'\nimport { create } from './create.js'\nimport { createGlobal } from './createGlobal.js'\nimport { createGlobalVersion } from './createGlobalVersion.js'\nimport { createMigration } from './createMigration.js'\nimport { createVersion } from './createVersion.js'\nimport { deleteMany } from './deleteMany.js'\nimport { deleteOne } from './deleteOne.js'\nimport { deleteVersions } from './deleteVersions.js'\nimport { destroy } from './destroy.js'\nimport { find } from './find.js'\nimport { findGlobal } from './findGlobal.js'\nimport { findGlobalVersions } from './findGlobalVersions.js'\nimport { findOne } from './findOne.js'\nimport { findVersions } from './findVersions.js'\nimport { init } from './init.js'\nimport { migrateFresh } from './migrateFresh.js'\nimport { queryDrafts } from './queryDrafts.js'\nimport { beginTransaction } from './transactions/beginTransaction.js'\nimport { commitTransaction } from './transactions/commitTransaction.js'\nimport { rollbackTransaction } from './transactions/rollbackTransaction.js'\nimport { updateGlobal } from './updateGlobal.js'\nimport { updateGlobalVersion } from './updateGlobalVersion.js'\nimport { updateOne } from './updateOne.js'\nimport { updateVersion } from './updateVersion.js'\n\nexport type { MigrateDownArgs, MigrateUpArgs } from './types.js'\n\nexport interface Args {\n /** Set to false to disable auto-pluralization of collection names, Defaults to true */\n autoPluralization?: boolean\n /** Extra configuration options */\n connectOptions?: ConnectOptions & {\n /** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */\n useFacet?: boolean\n }\n /** Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false */\n disableIndexHints?: boolean\n migrationDir?: string\n /**\n * typed as any to avoid dependency\n */\n mongoMemoryServer?: MongoMemoryReplSet\n transactionOptions?: TransactionOptions | false\n /** The URL to connect to MongoDB or false to start payload and prevent connecting */\n url: false | string\n}\n\nexport type MongooseAdapter = BaseDatabaseAdapter &\n Args & {\n collections: {\n [slug: string]: CollectionModel\n }\n connection: Connection\n globals: GlobalModel\n mongoMemoryServer: MongoMemoryReplSet\n sessions: Record<number | string, ClientSession>\n versions: {\n [slug: string]: CollectionModel\n }\n }\n\ndeclare module 'payload' {\n export interface DatabaseAdapter\n extends Omit<BaseDatabaseAdapter, 'sessions'>,\n Omit<Args, 'migrationDir'> {\n collections: {\n [slug: string]: CollectionModel\n }\n connection: Connection\n globals: GlobalModel\n mongoMemoryServer: MongoMemoryReplSet\n sessions: Record<number | string, ClientSession>\n transactionOptions: TransactionOptions\n versions: {\n [slug: string]: CollectionModel\n }\n }\n}\n\nexport function mongooseAdapter({\n autoPluralization = true,\n connectOptions,\n disableIndexHints = false,\n migrationDir: migrationDirArg,\n mongoMemoryServer,\n transactionOptions = {},\n url,\n}: Args): DatabaseAdapterObj {\n function adapter({ payload }: { payload: Payload }) {\n const migrationDir = findMigrationDir(migrationDirArg)\n mongoose.set('strictQuery', false)\n\n return createDatabaseAdapter<MongooseAdapter>({\n name: 'mongoose',\n\n // Mongoose-specific\n autoPluralization,\n collections: {},\n connectOptions: connectOptions || {},\n connection: undefined,\n disableIndexHints,\n globals: undefined,\n mongoMemoryServer,\n sessions: {},\n transactionOptions: transactionOptions === false ? undefined : transactionOptions,\n url,\n versions: {},\n
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import type { TransactionOptions } from 'mongodb'\nimport type { MongoMemoryReplSet } from 'mongodb-memory-server'\nimport type { ClientSession, ConnectOptions, Connection } from 'mongoose'\nimport type { Payload } from 'payload'\nimport type { BaseDatabaseAdapter, DatabaseAdapterObj } from 'payload/database'\n\nimport fs from 'fs'\nimport mongoose from 'mongoose'\nimport path from 'path'\nimport { createDatabaseAdapter } from 'payload/database'\n\nimport type { CollectionModel, GlobalModel } from './types.js'\n\nimport { connect } from './connect.js'\nimport { count } from './count.js'\nimport { create } from './create.js'\nimport { createGlobal } from './createGlobal.js'\nimport { createGlobalVersion } from './createGlobalVersion.js'\nimport { createMigration } from './createMigration.js'\nimport { createVersion } from './createVersion.js'\nimport { deleteMany } from './deleteMany.js'\nimport { deleteOne } from './deleteOne.js'\nimport { deleteVersions } from './deleteVersions.js'\nimport { destroy } from './destroy.js'\nimport { find } from './find.js'\nimport { findGlobal } from './findGlobal.js'\nimport { findGlobalVersions } from './findGlobalVersions.js'\nimport { findOne } from './findOne.js'\nimport { findVersions } from './findVersions.js'\nimport { init } from './init.js'\nimport { migrateFresh } from './migrateFresh.js'\nimport { queryDrafts } from './queryDrafts.js'\nimport { beginTransaction } from './transactions/beginTransaction.js'\nimport { commitTransaction } from './transactions/commitTransaction.js'\nimport { rollbackTransaction } from './transactions/rollbackTransaction.js'\nimport { updateGlobal } from './updateGlobal.js'\nimport { updateGlobalVersion } from './updateGlobalVersion.js'\nimport { updateOne } from './updateOne.js'\nimport { updateVersion } from './updateVersion.js'\n\nexport type { MigrateDownArgs, MigrateUpArgs } from './types.js'\n\nexport interface Args {\n /** Set to false to disable auto-pluralization of collection names, Defaults to true */\n autoPluralization?: boolean\n /** Extra configuration options */\n connectOptions?: ConnectOptions & {\n /** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */\n useFacet?: boolean\n }\n /** Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false */\n disableIndexHints?: boolean\n migrationDir?: string\n /**\n * typed as any to avoid dependency\n */\n mongoMemoryServer?: MongoMemoryReplSet\n transactionOptions?: TransactionOptions | false\n /** The URL to connect to MongoDB or false to start payload and prevent connecting */\n url: false | string\n}\n\nexport type MongooseAdapter = BaseDatabaseAdapter &\n Args & {\n collections: {\n [slug: string]: CollectionModel\n }\n connection: Connection\n globals: GlobalModel\n mongoMemoryServer: MongoMemoryReplSet\n sessions: Record<number | string, ClientSession>\n versions: {\n [slug: string]: CollectionModel\n }\n }\n\ndeclare module 'payload' {\n export interface DatabaseAdapter\n extends Omit<BaseDatabaseAdapter, 'sessions'>,\n Omit<Args, 'migrationDir'> {\n collections: {\n [slug: string]: CollectionModel\n }\n connection: Connection\n globals: GlobalModel\n mongoMemoryServer: MongoMemoryReplSet\n sessions: Record<number | string, ClientSession>\n transactionOptions: TransactionOptions\n versions: {\n [slug: string]: CollectionModel\n }\n }\n}\n\nexport function mongooseAdapter({\n autoPluralization = true,\n connectOptions,\n disableIndexHints = false,\n migrationDir: migrationDirArg,\n mongoMemoryServer,\n transactionOptions = {},\n url,\n}: Args): DatabaseAdapterObj {\n function adapter({ payload }: { payload: Payload }) {\n const migrationDir = findMigrationDir(migrationDirArg)\n mongoose.set('strictQuery', false)\n\n return createDatabaseAdapter<MongooseAdapter>({\n name: 'mongoose',\n\n // Mongoose-specific\n autoPluralization,\n collections: {},\n connectOptions: connectOptions || {},\n connection: undefined,\n count,\n disableIndexHints,\n globals: undefined,\n mongoMemoryServer,\n sessions: {},\n transactionOptions: transactionOptions === false ? undefined : transactionOptions,\n url,\n versions: {},\n // DatabaseAdapter\n beginTransaction: transactionOptions ? beginTransaction : undefined,\n commitTransaction,\n connect,\n create,\n createGlobal,\n createGlobalVersion,\n createMigration,\n createVersion,\n defaultIDType: 'text',\n deleteMany,\n deleteOne,\n deleteVersions,\n destroy,\n find,\n findGlobal,\n findGlobalVersions,\n findOne,\n findVersions,\n init,\n migrateFresh,\n migrationDir,\n payload,\n queryDrafts,\n rollbackTransaction,\n updateGlobal,\n updateGlobalVersion,\n updateOne,\n updateVersion,\n })\n }\n\n return {\n defaultIDType: 'text',\n init: adapter,\n }\n}\n\n/**\n * Attempt to find migrations directory.\n *\n * Checks for the following directories in order:\n * - `migrationDir` argument from Payload config\n * - `src/migrations`\n * - `dist/migrations`\n * - `migrations`\n *\n * Defaults to `src/migrations`\n *\n * @param migrationDir\n * @returns\n */\nfunction findMigrationDir(migrationDir?: string): string {\n const cwd = process.cwd()\n const srcDir = path.resolve(cwd, 'src/migrations')\n const distDir = path.resolve(cwd, 'dist/migrations')\n const relativeMigrations = path.resolve(cwd, 'migrations')\n\n // Use arg if provided\n if (migrationDir) return migrationDir\n\n // Check other common locations\n if (fs.existsSync(srcDir)) {\n return srcDir\n }\n\n if (fs.existsSync(distDir)) {\n return distDir\n }\n\n if (fs.existsSync(relativeMigrations)) {\n return relativeMigrations\n }\n\n return srcDir\n}\n"],"names":["fs","mongoose","path","createDatabaseAdapter","connect","count","create","createGlobal","createGlobalVersion","createMigration","createVersion","deleteMany","deleteOne","deleteVersions","destroy","find","findGlobal","findGlobalVersions","findOne","findVersions","init","migrateFresh","queryDrafts","beginTransaction","commitTransaction","rollbackTransaction","updateGlobal","updateGlobalVersion","updateOne","updateVersion","mongooseAdapter","autoPluralization","connectOptions","disableIndexHints","migrationDir","migrationDirArg","mongoMemoryServer","transactionOptions","url","adapter","payload","findMigrationDir","set","name","collections","connection","undefined","globals","sessions","versions","defaultIDType","cwd","process","srcDir","resolve","distDir","relativeMigrations","existsSync"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAMA,OAAOA,QAAQ,KAAI;AACnB,OAAOC,cAAc,WAAU;AAC/B,OAAOC,UAAU,OAAM;AACvB,SAASC,qBAAqB,QAAQ,mBAAkB;AAIxD,SAASC,OAAO,QAAQ,eAAc;AACtC,SAASC,KAAK,QAAQ,aAAY;AAClC,SAASC,MAAM,QAAQ,cAAa;AACpC,SAASC,YAAY,QAAQ,oBAAmB;AAChD,SAASC,mBAAmB,QAAQ,2BAA0B;AAC9D,SAASC,eAAe,QAAQ,uBAAsB;AACtD,SAASC,aAAa,QAAQ,qBAAoB;AAClD,SAASC,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,SAAS,QAAQ,iBAAgB;AAC1C,SAASC,cAAc,QAAQ,sBAAqB;AACpD,SAASC,OAAO,QAAQ,eAAc;AACtC,SAASC,IAAI,QAAQ,YAAW;AAChC,SAASC,UAAU,QAAQ,kBAAiB;AAC5C,SAASC,kBAAkB,QAAQ,0BAAyB;AAC5D,SAASC,OAAO,QAAQ,eAAc;AACtC,SAASC,YAAY,QAAQ,oBAAmB;AAChD,SAASC,IAAI,QAAQ,YAAW;AAChC,SAASC,YAAY,QAAQ,oBAAmB;AAChD,SAASC,WAAW,QAAQ,mBAAkB;AAC9C,SAASC,gBAAgB,QAAQ,qCAAoC;AACrE,SAASC,iBAAiB,QAAQ,sCAAqC;AACvE,SAASC,mBAAmB,QAAQ,wCAAuC;AAC3E,SAASC,YAAY,QAAQ,oBAAmB;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J,SAAS;QACzB,OAAOA;IACT;IAEA,IAAIrD,GAAGyD,UAAU,CAACF,UAAU;QAC1B,OAAOA;IACT;IAEA,IAAIvD,GAAGyD,UAAU,CAACD,qBAAqB;QACrC,OAAOA;IACT;IAEA,OAAOH;AACT"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commitTransaction.d.ts","sourceRoot":"","sources":["../../src/transactions/commitTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,eAAO,MAAM,iBAAiB,EAAE,
|
|
1
|
+
{"version":3,"file":"commitTransaction.d.ts","sourceRoot":"","sources":["../../src/transactions/commitTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAA;AAEzD,eAAO,MAAM,iBAAiB,EAAE,iBAY/B,CAAA"}
|
|
@@ -3,7 +3,11 @@ export const commitTransaction = async function commitTransaction(id) {
|
|
|
3
3
|
return;
|
|
4
4
|
}
|
|
5
5
|
await this.sessions[id].commitTransaction();
|
|
6
|
-
|
|
6
|
+
try {
|
|
7
|
+
await this.sessions[id].endSession();
|
|
8
|
+
} catch (error) {
|
|
9
|
+
// ending sessions is only best effort and won't impact anything if it fails since the transaction was committed
|
|
10
|
+
}
|
|
7
11
|
delete this.sessions[id];
|
|
8
12
|
};
|
|
9
13
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/transactions/commitTransaction.ts"],"sourcesContent":["import type { CommitTransaction } from 'payload/database'\n\nexport const commitTransaction: CommitTransaction = async function commitTransaction(id) {\n if (!this.sessions[id]?.inTransaction()) {\n return\n }\n\n await this.sessions[id].commitTransaction()\n await this.sessions[id].endSession()\n delete this.sessions[id]\n}\n"],"names":["commitTransaction","id","sessions","inTransaction","endSession"],"rangeMappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/transactions/commitTransaction.ts"],"sourcesContent":["import type { CommitTransaction } from 'payload/database'\n\nexport const commitTransaction: CommitTransaction = async function commitTransaction(id) {\n if (!this.sessions[id]?.inTransaction()) {\n return\n }\n\n await this.sessions[id].commitTransaction()\n try {\n await this.sessions[id].endSession()\n } catch (error) {\n // ending sessions is only best effort and won't impact anything if it fails since the transaction was committed\n }\n delete this.sessions[id]\n}\n"],"names":["commitTransaction","id","sessions","inTransaction","endSession","error"],"rangeMappings":";;;;;;;;;;;","mappings":"AAEA,OAAO,MAAMA,oBAAuC,eAAeA,kBAAkBC,EAAE;IACrF,IAAI,CAAC,IAAI,CAACC,QAAQ,CAACD,GAAG,EAAEE,iBAAiB;QACvC;IACF;IAEA,MAAM,IAAI,CAACD,QAAQ,CAACD,GAAG,CAACD,iBAAiB;IACzC,IAAI;QACF,MAAM,IAAI,CAACE,QAAQ,CAACD,GAAG,CAACG,UAAU;IACpC,EAAE,OAAOC,OAAO;IACd,gHAAgH;IAClH;IACA,OAAO,IAAI,CAACH,QAAQ,CAACD,GAAG;AAC1B,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/db-mongodb",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.12",
|
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -31,11 +31,11 @@
|
|
|
31
31
|
"@types/mongoose-aggregate-paginate-v2": "1.0.9",
|
|
32
32
|
"mongodb": "4.17.1",
|
|
33
33
|
"mongodb-memory-server": "^9",
|
|
34
|
-
"
|
|
35
|
-
"
|
|
34
|
+
"payload": "3.0.0-beta.12",
|
|
35
|
+
"@payloadcms/eslint-config": "1.1.1"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"payload": "3.0.0-beta.
|
|
38
|
+
"payload": "3.0.0-beta.12"
|
|
39
39
|
},
|
|
40
40
|
"exports": {
|
|
41
41
|
".": {
|
package/src/index.ts
DELETED
|
@@ -1,198 +0,0 @@
|
|
|
1
|
-
import type { TransactionOptions } from 'mongodb'
|
|
2
|
-
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
|
|
3
|
-
import type { ClientSession, ConnectOptions, Connection } from 'mongoose'
|
|
4
|
-
import type { Payload } from 'payload'
|
|
5
|
-
import type { BaseDatabaseAdapter, DatabaseAdapterObj } from 'payload/database'
|
|
6
|
-
|
|
7
|
-
import fs from 'fs'
|
|
8
|
-
import mongoose from 'mongoose'
|
|
9
|
-
import path from 'path'
|
|
10
|
-
import { createDatabaseAdapter } from 'payload/database'
|
|
11
|
-
|
|
12
|
-
import type { CollectionModel, GlobalModel } from './types.js'
|
|
13
|
-
|
|
14
|
-
import { connect } from './connect.js'
|
|
15
|
-
import { create } from './create.js'
|
|
16
|
-
import { createGlobal } from './createGlobal.js'
|
|
17
|
-
import { createGlobalVersion } from './createGlobalVersion.js'
|
|
18
|
-
import { createMigration } from './createMigration.js'
|
|
19
|
-
import { createVersion } from './createVersion.js'
|
|
20
|
-
import { deleteMany } from './deleteMany.js'
|
|
21
|
-
import { deleteOne } from './deleteOne.js'
|
|
22
|
-
import { deleteVersions } from './deleteVersions.js'
|
|
23
|
-
import { destroy } from './destroy.js'
|
|
24
|
-
import { find } from './find.js'
|
|
25
|
-
import { findGlobal } from './findGlobal.js'
|
|
26
|
-
import { findGlobalVersions } from './findGlobalVersions.js'
|
|
27
|
-
import { findOne } from './findOne.js'
|
|
28
|
-
import { findVersions } from './findVersions.js'
|
|
29
|
-
import { init } from './init.js'
|
|
30
|
-
import { migrateFresh } from './migrateFresh.js'
|
|
31
|
-
import { queryDrafts } from './queryDrafts.js'
|
|
32
|
-
import { beginTransaction } from './transactions/beginTransaction.js'
|
|
33
|
-
import { commitTransaction } from './transactions/commitTransaction.js'
|
|
34
|
-
import { rollbackTransaction } from './transactions/rollbackTransaction.js'
|
|
35
|
-
import { updateGlobal } from './updateGlobal.js'
|
|
36
|
-
import { updateGlobalVersion } from './updateGlobalVersion.js'
|
|
37
|
-
import { updateOne } from './updateOne.js'
|
|
38
|
-
import { updateVersion } from './updateVersion.js'
|
|
39
|
-
|
|
40
|
-
export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
|
|
41
|
-
|
|
42
|
-
export interface Args {
|
|
43
|
-
/** Set to false to disable auto-pluralization of collection names, Defaults to true */
|
|
44
|
-
autoPluralization?: boolean
|
|
45
|
-
/** Extra configuration options */
|
|
46
|
-
connectOptions?: ConnectOptions & {
|
|
47
|
-
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */
|
|
48
|
-
useFacet?: boolean
|
|
49
|
-
}
|
|
50
|
-
/** Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false */
|
|
51
|
-
disableIndexHints?: boolean
|
|
52
|
-
migrationDir?: string
|
|
53
|
-
/**
|
|
54
|
-
* typed as any to avoid dependency
|
|
55
|
-
*/
|
|
56
|
-
mongoMemoryServer?: MongoMemoryReplSet
|
|
57
|
-
transactionOptions?: TransactionOptions | false
|
|
58
|
-
/** The URL to connect to MongoDB or false to start payload and prevent connecting */
|
|
59
|
-
url: false | string
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
export type MongooseAdapter = BaseDatabaseAdapter &
|
|
63
|
-
Args & {
|
|
64
|
-
collections: {
|
|
65
|
-
[slug: string]: CollectionModel
|
|
66
|
-
}
|
|
67
|
-
connection: Connection
|
|
68
|
-
globals: GlobalModel
|
|
69
|
-
mongoMemoryServer: MongoMemoryReplSet
|
|
70
|
-
sessions: Record<number | string, ClientSession>
|
|
71
|
-
versions: {
|
|
72
|
-
[slug: string]: CollectionModel
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
declare module 'payload' {
|
|
77
|
-
export interface DatabaseAdapter
|
|
78
|
-
extends Omit<BaseDatabaseAdapter, 'sessions'>,
|
|
79
|
-
Omit<Args, 'migrationDir'> {
|
|
80
|
-
collections: {
|
|
81
|
-
[slug: string]: CollectionModel
|
|
82
|
-
}
|
|
83
|
-
connection: Connection
|
|
84
|
-
globals: GlobalModel
|
|
85
|
-
mongoMemoryServer: MongoMemoryReplSet
|
|
86
|
-
sessions: Record<number | string, ClientSession>
|
|
87
|
-
transactionOptions: TransactionOptions
|
|
88
|
-
versions: {
|
|
89
|
-
[slug: string]: CollectionModel
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function mongooseAdapter({
|
|
95
|
-
autoPluralization = true,
|
|
96
|
-
connectOptions,
|
|
97
|
-
disableIndexHints = false,
|
|
98
|
-
migrationDir: migrationDirArg,
|
|
99
|
-
mongoMemoryServer,
|
|
100
|
-
transactionOptions = {},
|
|
101
|
-
url,
|
|
102
|
-
}: Args): DatabaseAdapterObj {
|
|
103
|
-
function adapter({ payload }: { payload: Payload }) {
|
|
104
|
-
const migrationDir = findMigrationDir(migrationDirArg)
|
|
105
|
-
mongoose.set('strictQuery', false)
|
|
106
|
-
|
|
107
|
-
return createDatabaseAdapter<MongooseAdapter>({
|
|
108
|
-
name: 'mongoose',
|
|
109
|
-
|
|
110
|
-
// Mongoose-specific
|
|
111
|
-
autoPluralization,
|
|
112
|
-
collections: {},
|
|
113
|
-
connectOptions: connectOptions || {},
|
|
114
|
-
connection: undefined,
|
|
115
|
-
disableIndexHints,
|
|
116
|
-
globals: undefined,
|
|
117
|
-
mongoMemoryServer,
|
|
118
|
-
sessions: {},
|
|
119
|
-
transactionOptions: transactionOptions === false ? undefined : transactionOptions,
|
|
120
|
-
url,
|
|
121
|
-
versions: {},
|
|
122
|
-
|
|
123
|
-
// DatabaseAdapter
|
|
124
|
-
beginTransaction: transactionOptions ? beginTransaction : undefined,
|
|
125
|
-
commitTransaction,
|
|
126
|
-
connect,
|
|
127
|
-
create,
|
|
128
|
-
createGlobal,
|
|
129
|
-
createGlobalVersion,
|
|
130
|
-
createMigration,
|
|
131
|
-
createVersion,
|
|
132
|
-
defaultIDType: 'text',
|
|
133
|
-
deleteMany,
|
|
134
|
-
deleteOne,
|
|
135
|
-
deleteVersions,
|
|
136
|
-
destroy,
|
|
137
|
-
find,
|
|
138
|
-
findGlobal,
|
|
139
|
-
findGlobalVersions,
|
|
140
|
-
findOne,
|
|
141
|
-
findVersions,
|
|
142
|
-
init,
|
|
143
|
-
migrateFresh,
|
|
144
|
-
migrationDir,
|
|
145
|
-
payload,
|
|
146
|
-
queryDrafts,
|
|
147
|
-
rollbackTransaction,
|
|
148
|
-
updateGlobal,
|
|
149
|
-
updateGlobalVersion,
|
|
150
|
-
updateOne,
|
|
151
|
-
updateVersion,
|
|
152
|
-
})
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
return {
|
|
156
|
-
defaultIDType: 'text',
|
|
157
|
-
init: adapter,
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
/**
|
|
162
|
-
* Attempt to find migrations directory.
|
|
163
|
-
*
|
|
164
|
-
* Checks for the following directories in order:
|
|
165
|
-
* - `migrationDir` argument from Payload config
|
|
166
|
-
* - `src/migrations`
|
|
167
|
-
* - `dist/migrations`
|
|
168
|
-
* - `migrations`
|
|
169
|
-
*
|
|
170
|
-
* Defaults to `src/migrations`
|
|
171
|
-
*
|
|
172
|
-
* @param migrationDir
|
|
173
|
-
* @returns
|
|
174
|
-
*/
|
|
175
|
-
function findMigrationDir(migrationDir?: string): string {
|
|
176
|
-
const cwd = process.cwd()
|
|
177
|
-
const srcDir = path.resolve(cwd, 'src/migrations')
|
|
178
|
-
const distDir = path.resolve(cwd, 'dist/migrations')
|
|
179
|
-
const relativeMigrations = path.resolve(cwd, 'migrations')
|
|
180
|
-
|
|
181
|
-
// Use arg if provided
|
|
182
|
-
if (migrationDir) return migrationDir
|
|
183
|
-
|
|
184
|
-
// Check other common locations
|
|
185
|
-
if (fs.existsSync(srcDir)) {
|
|
186
|
-
return srcDir
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
if (fs.existsSync(distDir)) {
|
|
190
|
-
return distDir
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
if (fs.existsSync(relativeMigrations)) {
|
|
194
|
-
return relativeMigrations
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
return srcDir
|
|
198
|
-
}
|