@payloadcms/db-mongodb 3.0.0-canary.c27a334 → 3.0.0-canary.c696728
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/README.md +1 -1
- package/dist/connect.d.ts +1 -1
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +13 -4
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts +1 -1
- package/dist/count.d.ts.map +1 -1
- package/dist/count.js +2 -2
- package/dist/count.js.map +1 -1
- package/dist/create.d.ts +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +14 -4
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +11 -6
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +14 -5
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +2 -2
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +15 -6
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +1 -1
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +2 -2
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +1 -1
- package/dist/deleteVersions.js.map +1 -1
- package/dist/destroy.d.ts +1 -1
- package/dist/destroy.d.ts.map +1 -1
- package/dist/destroy.js.map +1 -1
- package/dist/find.d.ts +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +29 -6
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +3 -3
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +12 -7
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +20 -4
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +10 -4
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +49 -35
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -4
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +11 -8
- package/dist/init.js.map +1 -1
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +2 -29
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +16 -4
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +1 -1
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +2 -2
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +2 -29
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +100 -22
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +1 -2
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +0 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +3 -4
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +2 -3
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/buildSearchParams.d.ts +1 -2
- package/dist/queries/buildSearchParams.d.ts.map +1 -1
- package/dist/queries/buildSearchParams.js +21 -13
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/buildSortParam.d.ts +2 -28
- package/dist/queries/buildSortParam.d.ts.map +1 -1
- package/dist/queries/buildSortParam.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.d.ts +1 -2
- package/dist/queries/getLocalizedSortProperty.d.ts.map +1 -1
- package/dist/queries/getLocalizedSortProperty.js +2 -2
- package/dist/queries/getLocalizedSortProperty.js.map +1 -1
- package/dist/queries/getLocalizedSortProperty.spec.js +44 -44
- package/dist/queries/getLocalizedSortProperty.spec.js.map +1 -1
- package/dist/queries/mock.js.map +1 -1
- package/dist/queries/operatorMap.js.map +1 -1
- package/dist/queries/parseParams.d.ts +1 -3
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +6 -7
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +132 -25
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.d.ts +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +10 -4
- package/dist/queryDrafts.js.map +1 -1
- package/dist/testCredentials.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +3 -1
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js +3 -0
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js +13 -7
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/types.d.ts +5 -31
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/updateGlobal.d.ts +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +9 -3
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +2 -3
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +11 -4
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +15 -5
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +9 -2
- package/dist/updateVersion.js.map +1 -1
- package/dist/utilities/buildJoinAggregation.d.ts +15 -0
- package/dist/utilities/buildJoinAggregation.d.ts.map +1 -0
- package/dist/utilities/buildJoinAggregation.js +148 -0
- package/dist/utilities/buildJoinAggregation.js.map +1 -0
- package/dist/utilities/getDBName.d.ts +1 -1
- package/dist/utilities/getDBName.d.ts.map +1 -1
- package/dist/utilities/getDBName.js +3 -1
- package/dist/utilities/getDBName.js.map +1 -1
- package/dist/utilities/handleError.d.ts +6 -2
- package/dist/utilities/handleError.d.ts.map +1 -1
- package/dist/utilities/handleError.js +12 -9
- package/dist/utilities/handleError.js.map +1 -1
- package/dist/utilities/sanitizeInternalFields.d.ts +1 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +1 -1
- package/dist/utilities/sanitizeInternalFields.js +1 -2
- package/dist/utilities/sanitizeInternalFields.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +9 -0
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -0
- package/dist/utilities/sanitizeRelationshipIDs.js +111 -0
- package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -0
- package/dist/withSession.d.ts +3 -27
- package/dist/withSession.d.ts.map +1 -1
- package/dist/withSession.js +10 -4
- package/dist/withSession.js.map +1 -1
- package/package.json +14 -12
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { buildSortParam } from '../queries/buildSortParam.js';
|
|
2
|
+
export const buildJoinAggregation = async ({ adapter, collection, collectionConfig, joins, limit, locale, query })=>{
|
|
3
|
+
if (Object.keys(collectionConfig.joins).length === 0 || joins === false) {
|
|
4
|
+
return;
|
|
5
|
+
}
|
|
6
|
+
const joinConfig = adapter.payload.collections[collection].config.joins;
|
|
7
|
+
const aggregate = [
|
|
8
|
+
{
|
|
9
|
+
$sort: {
|
|
10
|
+
createdAt: -1
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
];
|
|
14
|
+
if (query) {
|
|
15
|
+
aggregate.push({
|
|
16
|
+
$match: query
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
if (limit) {
|
|
20
|
+
aggregate.push({
|
|
21
|
+
$limit: limit
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
for (const slug of Object.keys(joinConfig)){
|
|
25
|
+
for (const join of joinConfig[slug]){
|
|
26
|
+
const joinModel = adapter.collections[join.field.collection];
|
|
27
|
+
const { limit: limitJoin = 10, sort: sortJoin, where: whereJoin } = joins?.[join.schemaPath] || {};
|
|
28
|
+
const sort = buildSortParam({
|
|
29
|
+
config: adapter.payload.config,
|
|
30
|
+
fields: adapter.payload.collections[slug].config.fields,
|
|
31
|
+
locale,
|
|
32
|
+
sort: sortJoin || collectionConfig.defaultSort,
|
|
33
|
+
timestamps: true
|
|
34
|
+
});
|
|
35
|
+
const sortProperty = Object.keys(sort)[0];
|
|
36
|
+
const sortDirection = sort[sortProperty] === 'asc' ? 1 : -1;
|
|
37
|
+
const $match = await joinModel.buildQuery({
|
|
38
|
+
locale,
|
|
39
|
+
payload: adapter.payload,
|
|
40
|
+
where: whereJoin
|
|
41
|
+
});
|
|
42
|
+
const pipeline = [
|
|
43
|
+
{
|
|
44
|
+
$match
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
$sort: {
|
|
48
|
+
[sortProperty]: sortDirection
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
];
|
|
52
|
+
if (limitJoin > 0) {
|
|
53
|
+
pipeline.push({
|
|
54
|
+
$limit: limitJoin + 1
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
if (adapter.payload.config.localization && locale === 'all') {
|
|
58
|
+
adapter.payload.config.localization.localeCodes.forEach((code)=>{
|
|
59
|
+
const as = `${join.schemaPath}${code}`;
|
|
60
|
+
aggregate.push({
|
|
61
|
+
$lookup: {
|
|
62
|
+
as: `${as}.docs`,
|
|
63
|
+
foreignField: `${join.field.on}${code}`,
|
|
64
|
+
from: slug,
|
|
65
|
+
localField: '_id',
|
|
66
|
+
pipeline
|
|
67
|
+
}
|
|
68
|
+
}, {
|
|
69
|
+
$addFields: {
|
|
70
|
+
[`${as}.docs`]: {
|
|
71
|
+
$map: {
|
|
72
|
+
as: 'doc',
|
|
73
|
+
in: '$$doc._id',
|
|
74
|
+
input: `$${as}.docs`
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
[`${as}.hasNextPage`]: {
|
|
78
|
+
$gt: [
|
|
79
|
+
{
|
|
80
|
+
$size: `$${as}.docs`
|
|
81
|
+
},
|
|
82
|
+
limitJoin || Number.MAX_VALUE
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
if (limitJoin > 0) {
|
|
88
|
+
aggregate.push({
|
|
89
|
+
$addFields: {
|
|
90
|
+
[`${as}.docs`]: {
|
|
91
|
+
$slice: [
|
|
92
|
+
`$${as}.docs`,
|
|
93
|
+
limitJoin
|
|
94
|
+
]
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
});
|
|
100
|
+
} else {
|
|
101
|
+
const localeSuffix = join.field.localized && adapter.payload.config.localization && locale ? `.${locale}` : '';
|
|
102
|
+
const as = `${join.schemaPath}${localeSuffix}`;
|
|
103
|
+
aggregate.push({
|
|
104
|
+
$lookup: {
|
|
105
|
+
as: `${as}.docs`,
|
|
106
|
+
foreignField: `${join.field.on}${localeSuffix}`,
|
|
107
|
+
from: slug,
|
|
108
|
+
localField: '_id',
|
|
109
|
+
pipeline
|
|
110
|
+
}
|
|
111
|
+
}, {
|
|
112
|
+
$addFields: {
|
|
113
|
+
[`${as}.docs`]: {
|
|
114
|
+
$map: {
|
|
115
|
+
as: 'doc',
|
|
116
|
+
in: '$$doc._id',
|
|
117
|
+
input: `$${as}.docs`
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
[`${as}.hasNextPage`]: {
|
|
121
|
+
$gt: [
|
|
122
|
+
{
|
|
123
|
+
$size: `$${as}.docs`
|
|
124
|
+
},
|
|
125
|
+
limitJoin || Number.MAX_VALUE
|
|
126
|
+
]
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
if (limitJoin > 0) {
|
|
131
|
+
aggregate.push({
|
|
132
|
+
$addFields: {
|
|
133
|
+
[`${as}.docs`]: {
|
|
134
|
+
$slice: [
|
|
135
|
+
`$${as}.docs`,
|
|
136
|
+
limitJoin
|
|
137
|
+
]
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return aggregate;
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
//# sourceMappingURL=buildJoinAggregation.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/buildJoinAggregation.ts"],"sourcesContent":["import type { PipelineStage } from 'mongoose'\nimport type { CollectionSlug, JoinQuery, SanitizedCollectionConfig, Where } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { buildSortParam } from '../queries/buildSortParam.js'\n\ntype BuildJoinAggregationArgs = {\n adapter: MongooseAdapter\n collection: CollectionSlug\n collectionConfig: SanitizedCollectionConfig\n joins: JoinQuery\n // the number of docs to get at the top collection level\n limit?: number\n locale: string\n // the where clause for the top collection\n query?: Where\n}\n\nexport const buildJoinAggregation = async ({\n adapter,\n collection,\n collectionConfig,\n joins,\n limit,\n locale,\n query,\n}: BuildJoinAggregationArgs): Promise<PipelineStage[] | undefined> => {\n if (Object.keys(collectionConfig.joins).length === 0 || joins === false) {\n return\n }\n\n const joinConfig = adapter.payload.collections[collection].config.joins\n const aggregate: PipelineStage[] = [\n {\n $sort: { createdAt: -1 },\n },\n ]\n\n if (query) {\n aggregate.push({\n $match: query,\n })\n }\n\n if (limit) {\n aggregate.push({\n $limit: limit,\n })\n }\n\n for (const slug of Object.keys(joinConfig)) {\n for (const join of joinConfig[slug]) {\n const joinModel = adapter.collections[join.field.collection]\n\n const {\n limit: limitJoin = 10,\n sort: sortJoin,\n where: whereJoin,\n } = joins?.[join.schemaPath] || {}\n\n const sort = buildSortParam({\n config: adapter.payload.config,\n fields: adapter.payload.collections[slug].config.fields,\n locale,\n sort: sortJoin || collectionConfig.defaultSort,\n timestamps: true,\n })\n const sortProperty = Object.keys(sort)[0]\n const sortDirection = sort[sortProperty] === 'asc' ? 1 : -1\n\n const $match = await joinModel.buildQuery({\n locale,\n payload: adapter.payload,\n where: whereJoin,\n })\n\n const pipeline: Exclude<PipelineStage, PipelineStage.Merge | PipelineStage.Out>[] = [\n { $match },\n {\n $sort: { [sortProperty]: sortDirection },\n },\n ]\n\n if (limitJoin > 0) {\n pipeline.push({\n $limit: limitJoin + 1,\n })\n }\n\n if (adapter.payload.config.localization && locale === 'all') {\n adapter.payload.config.localization.localeCodes.forEach((code) => {\n const as = `${join.schemaPath}${code}`\n\n aggregate.push(\n {\n $lookup: {\n as: `${as}.docs`,\n foreignField: `${join.field.on}${code}`,\n from: slug,\n localField: '_id',\n pipeline,\n },\n },\n {\n $addFields: {\n [`${as}.docs`]: {\n $map: {\n as: 'doc',\n in: '$$doc._id',\n input: `$${as}.docs`,\n },\n }, // Slicing the docs to match the limit\n [`${as}.hasNextPage`]: {\n $gt: [{ $size: `$${as}.docs` }, limitJoin || Number.MAX_VALUE],\n }, // Boolean indicating if more docs than limit\n },\n },\n )\n if (limitJoin > 0) {\n aggregate.push({\n $addFields: {\n [`${as}.docs`]: {\n $slice: [`$${as}.docs`, limitJoin],\n },\n },\n })\n }\n })\n } else {\n const localeSuffix =\n join.field.localized && adapter.payload.config.localization && locale ? `.${locale}` : ''\n const as = `${join.schemaPath}${localeSuffix}`\n\n aggregate.push(\n {\n $lookup: {\n as: `${as}.docs`,\n foreignField: `${join.field.on}${localeSuffix}`,\n from: slug,\n localField: '_id',\n pipeline,\n },\n },\n {\n $addFields: {\n [`${as}.docs`]: {\n $map: {\n as: 'doc',\n in: '$$doc._id',\n input: `$${as}.docs`,\n },\n }, // Slicing the docs to match the limit\n [`${as}.hasNextPage`]: {\n $gt: [{ $size: `$${as}.docs` }, limitJoin || Number.MAX_VALUE],\n }, // Boolean indicating if more docs than limit\n },\n },\n )\n if (limitJoin > 0) {\n aggregate.push({\n $addFields: {\n [`${as}.docs`]: {\n $slice: [`$${as}.docs`, limitJoin],\n },\n },\n })\n }\n }\n }\n }\n\n return aggregate\n}\n"],"names":["buildSortParam","buildJoinAggregation","adapter","collection","collectionConfig","joins","limit","locale","query","Object","keys","length","joinConfig","payload","collections","config","aggregate","$sort","createdAt","push","$match","$limit","slug","join","joinModel","field","limitJoin","sort","sortJoin","where","whereJoin","schemaPath","fields","defaultSort","timestamps","sortProperty","sortDirection","buildQuery","pipeline","localization","localeCodes","forEach","code","as","$lookup","foreignField","on","from","localField","$addFields","$map","in","input","$gt","$size","Number","MAX_VALUE","$slice","localeSuffix","localized"],"mappings":"AAKA,SAASA,cAAc,QAAQ,+BAA8B;AAc7D,OAAO,MAAMC,uBAAuB,OAAO,EACzCC,OAAO,EACPC,UAAU,EACVC,gBAAgB,EAChBC,KAAK,EACLC,KAAK,EACLC,MAAM,EACNC,KAAK,EACoB;IACzB,IAAIC,OAAOC,IAAI,CAACN,iBAAiBC,KAAK,EAAEM,MAAM,KAAK,KAAKN,UAAU,OAAO;QACvE;IACF;IAEA,MAAMO,aAAaV,QAAQW,OAAO,CAACC,WAAW,CAACX,WAAW,CAACY,MAAM,CAACV,KAAK;IACvE,MAAMW,YAA6B;QACjC;YACEC,OAAO;gBAAEC,WAAW,CAAC;YAAE;QACzB;KACD;IAED,IAAIV,OAAO;QACTQ,UAAUG,IAAI,CAAC;YACbC,QAAQZ;QACV;IACF;IAEA,IAAIF,OAAO;QACTU,UAAUG,IAAI,CAAC;YACbE,QAAQf;QACV;IACF;IAEA,KAAK,MAAMgB,QAAQb,OAAOC,IAAI,CAACE,YAAa;QAC1C,KAAK,MAAMW,QAAQX,UAAU,CAACU,KAAK,CAAE;YACnC,MAAME,YAAYtB,QAAQY,WAAW,CAACS,KAAKE,KAAK,CAACtB,UAAU,CAAC;YAE5D,MAAM,EACJG,OAAOoB,YAAY,EAAE,EACrBC,MAAMC,QAAQ,EACdC,OAAOC,SAAS,EACjB,GAAGzB,OAAO,CAACkB,KAAKQ,UAAU,CAAC,IAAI,CAAC;YAEjC,MAAMJ,OAAO3B,eAAe;gBAC1Be,QAAQb,QAAQW,OAAO,CAACE,MAAM;gBAC9BiB,QAAQ9B,QAAQW,OAAO,CAACC,WAAW,CAACQ,KAAK,CAACP,MAAM,CAACiB,MAAM;gBACvDzB;gBACAoB,MAAMC,YAAYxB,iBAAiB6B,WAAW;gBAC9CC,YAAY;YACd;YACA,MAAMC,eAAe1B,OAAOC,IAAI,CAACiB,KAAK,CAAC,EAAE;YACzC,MA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ACF;YACF;QACF;IACF;IAEA,OAAOV;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getDBName.d.ts","sourceRoot":"","sources":["../../src/utilities/getDBName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"getDBName.d.ts","sourceRoot":"","sources":["../../src/utilities/getDBName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAE/C,KAAK,IAAI,GAAG;IACV,MAAM,EAAE;QACN,MAAM,CAAC,EAAE,gBAAgB,CAAA;QACzB,QAAQ,CAAC,EAAE,gBAAgB,CAAA;QAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD,OAAO,CAAC,EAAE,OAAO,CAAA;IACjB,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,QAAQ,CAAC,EAAE,OAAO,CAAA;CACnB,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,SAAS,0DAKnB,IAAI,KAAG,MAmBT,CAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/getDBName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/getDBName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload'\n\ntype Args = {\n config: {\n dbName?: DBIdentifierName\n enumName?: DBIdentifierName\n name?: string\n slug?: string\n }\n locales?: boolean\n target?: 'dbName' | 'enumName'\n versions?: boolean\n}\n\n/**\n * Used to name database enums and collections\n * Returns the collection or enum name for a given entity\n */\nexport const getDBName = ({\n config: { name, slug },\n config,\n target = 'dbName',\n versions = false,\n}: Args): string => {\n let result: string\n let custom = config[target]\n\n if (!custom && target === 'enumName') {\n custom = config['dbName']\n }\n\n if (custom) {\n result = typeof custom === 'function' ? custom({}) : custom\n } else {\n result = name ?? slug\n }\n\n if (versions) {\n result = `_${result}_versions`\n }\n\n return result\n}\n"],"names":["getDBName","config","name","slug","target","versions","result","custom"],"mappings":"AAcA;;;CAGC,GACD,OAAO,MAAMA,YAAY,CAAC,EACxBC,QAAQ,EAAEC,IAAI,EAAEC,IAAI,EAAE,EACtBF,MAAM,EACNG,SAAS,QAAQ,EACjBC,WAAW,KAAK,EACX;IACL,IAAIC;IACJ,IAAIC,SAASN,MAAM,CAACG,OAAO;IAE3B,IAAI,CAACG,UAAUH,WAAW,YAAY;QACpCG,SAASN,MAAM,CAAC,SAAS;IAC3B;IAEA,IAAIM,QAAQ;QACVD,SAAS,OAAOC,WAAW,aAAaA,OAAO,CAAC,KAAKA;IACvD,OAAO;QACLD,SAASJ,QAAQC;IACnB;IAEA,IAAIE,UAAU;QACZC,SAAS,CAAC,CAAC,EAAEA,OAAO,SAAS,CAAC;IAChC;IAEA,OAAOA;AACT,EAAC"}
|
|
@@ -1,3 +1,7 @@
|
|
|
1
|
-
declare const handleError: (error
|
|
2
|
-
|
|
1
|
+
export declare const handleError: ({ collection, error, global, req, }: {
|
|
2
|
+
collection?: string;
|
|
3
|
+
error: any;
|
|
4
|
+
global?: string;
|
|
5
|
+
req: any;
|
|
6
|
+
}) => never;
|
|
3
7
|
//# sourceMappingURL=handleError.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAGA,
|
|
1
|
+
{"version":3,"file":"handleError.d.ts","sourceRoot":"","sources":["../../src/utilities/handleError.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,WAAW,wCAKrB;IACD,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,KAAK,MAAA;IACL,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,GAAG,MAAA;CACJ,UAqBA,CAAA"}
|
|
@@ -1,20 +1,23 @@
|
|
|
1
1
|
import httpStatus from 'http-status';
|
|
2
|
-
import { APIError, ValidationError } from 'payload
|
|
3
|
-
const handleError = (error, req)=>{
|
|
2
|
+
import { APIError, ValidationError } from 'payload';
|
|
3
|
+
export const handleError = ({ collection, error, global, req })=>{
|
|
4
4
|
// Handle uniqueness error from MongoDB
|
|
5
5
|
if (error.code === 11000 && error.keyValue) {
|
|
6
|
-
throw new ValidationError(
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
throw new ValidationError({
|
|
7
|
+
collection,
|
|
8
|
+
errors: [
|
|
9
|
+
{
|
|
10
|
+
field: Object.keys(error.keyValue)[0],
|
|
11
|
+
message: req.t('error:valueMustBeUnique')
|
|
12
|
+
}
|
|
13
|
+
],
|
|
14
|
+
global
|
|
15
|
+
}, req.t);
|
|
12
16
|
} else if (error.code === 11000) {
|
|
13
17
|
throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST);
|
|
14
18
|
} else {
|
|
15
19
|
throw error;
|
|
16
20
|
}
|
|
17
21
|
};
|
|
18
|
-
export default handleError;
|
|
19
22
|
|
|
20
23
|
//# sourceMappingURL=handleError.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport { APIError, ValidationError } from 'payload
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/handleError.ts"],"sourcesContent":["import httpStatus from 'http-status'\nimport { APIError, ValidationError } from 'payload'\n\nexport const handleError = ({\n collection,\n error,\n global,\n req,\n}: {\n collection?: string\n error\n global?: string\n req\n}) => {\n // Handle uniqueness error from MongoDB\n if (error.code === 11000 && error.keyValue) {\n throw new ValidationError(\n {\n collection,\n errors: [\n {\n field: Object.keys(error.keyValue)[0],\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n global,\n },\n req.t,\n )\n } else if (error.code === 11000) {\n throw new APIError(req.t('error:valueMustBeUnique'), httpStatus.BAD_REQUEST)\n } else {\n throw error\n }\n}\n"],"names":["httpStatus","APIError","ValidationError","handleError","collection","error","global","req","code","keyValue","errors","field","Object","keys","message","t","BAD_REQUEST"],"mappings":"AAAA,OAAOA,gBAAgB,cAAa;AACpC,SAASC,QAAQ,EAAEC,eAAe,QAAQ,UAAS;AAEnD,OAAO,MAAMC,cAAc,CAAC,EAC1BC,UAAU,EACVC,KAAK,EACLC,MAAM,EACNC,GAAG,EAMJ;IACC,uCAAuC;IACvC,IAAIF,MAAMG,IAAI,KAAK,SAASH,MAAMI,QAAQ,EAAE;QAC1C,MAAM,IAAIP,gBACR;YACEE;YACAM,QAAQ;gBACN;oBACEC,OAAOC,OAAOC,IAAI,CAACR,MAAMI,QAAQ,CAAC,CAAC,EAAE;oBACrCK,SAASP,IAAIQ,CAAC,CAAC;gBACjB;aACD;YACDT;QACF,GACAC,IAAIQ,CAAC;IAET,OAAO,IAAIV,MAAMG,IAAI,KAAK,OAAO;QAC/B,MAAM,IAAIP,SAASM,IAAIQ,CAAC,CAAC,4BAA4Bf,WAAWgB,WAAW;IAC7E,OAAO;QACL,MAAMX;IACR;AACF,EAAC"}
|
|
@@ -1,3 +1,2 @@
|
|
|
1
|
-
declare const sanitizeInternalFields: <T extends Record<string, unknown>>(incomingDoc: T) => T;
|
|
2
|
-
export default sanitizeInternalFields;
|
|
1
|
+
export declare const sanitizeInternalFields: <T extends Record<string, unknown>>(incomingDoc: T) => T;
|
|
3
2
|
//# sourceMappingURL=sanitizeInternalFields.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sanitizeInternalFields.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeInternalFields.ts"],"names":[],"mappings":"AAEA,
|
|
1
|
+
{"version":3,"file":"sanitizeInternalFields.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeInternalFields.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,sBAAsB,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,eAAe,CAAC,KAAG,CAiB9E,CAAA"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
const internalFields = [
|
|
2
2
|
'__v'
|
|
3
3
|
];
|
|
4
|
-
const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce((newDoc, [key, val])=>{
|
|
4
|
+
export const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce((newDoc, [key, val])=>{
|
|
5
5
|
if (key === '_id') {
|
|
6
6
|
return {
|
|
7
7
|
...newDoc,
|
|
@@ -16,6 +16,5 @@ const sanitizeInternalFields = (incomingDoc)=>Object.entries(incomingDoc).reduce
|
|
|
16
16
|
[key]: val
|
|
17
17
|
};
|
|
18
18
|
}, {});
|
|
19
|
-
export default sanitizeInternalFields;
|
|
20
19
|
|
|
21
20
|
//# sourceMappingURL=sanitizeInternalFields.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/sanitizeInternalFields.ts"],"sourcesContent":["const internalFields = ['__v']\n\
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/sanitizeInternalFields.ts"],"sourcesContent":["const internalFields = ['__v']\n\nexport const sanitizeInternalFields = <T extends Record<string, unknown>>(incomingDoc: T): T =>\n Object.entries(incomingDoc).reduce((newDoc, [key, val]): T => {\n if (key === '_id') {\n return {\n ...newDoc,\n id: val,\n }\n }\n\n if (internalFields.indexOf(key) > -1) {\n return newDoc\n }\n\n return {\n ...newDoc,\n [key]: val,\n }\n }, {} as T)\n"],"names":["internalFields","sanitizeInternalFields","incomingDoc","Object","entries","reduce","newDoc","key","val","id","indexOf"],"mappings":"AAAA,MAAMA,iBAAiB;IAAC;CAAM;AAE9B,OAAO,MAAMC,yBAAyB,CAAoCC,cACxEC,OAAOC,OAAO,CAACF,aAAaG,MAAM,CAAC,CAACC,QAAQ,CAACC,KAAKC,IAAI;QACpD,IAAID,QAAQ,OAAO;YACjB,OAAO;gBACL,GAAGD,MAAM;gBACTG,IAAID;YACN;QACF;QAEA,IAAIR,eAAeU,OAAO,CAACH,OAAO,CAAC,GAAG;YACpC,OAAOD;QACT;QAEA,OAAO;YACL,GAAGA,MAAM;YACT,CAACC,IAAI,EAAEC;QACT;IACF,GAAG,CAAC,GAAO"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Field, SanitizedConfig } from 'payload';
|
|
2
|
+
type Args = {
|
|
3
|
+
config: SanitizedConfig;
|
|
4
|
+
data: Record<string, unknown>;
|
|
5
|
+
fields: Field[];
|
|
6
|
+
};
|
|
7
|
+
export declare const sanitizeRelationshipIDs: ({ config, data, fields, }: Args) => Record<string, unknown>;
|
|
8
|
+
export {};
|
|
9
|
+
//# sourceMappingURL=sanitizeRelationshipIDs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sanitizeRelationshipIDs.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,KAAK,EAAE,eAAe,EAA0B,MAAM,SAAS,CAAA;AAM/F,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,KAAK,EAAE,CAAA;CAChB,CAAA;AAgGD,eAAO,MAAM,uBAAuB,8BAIjC,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CA6B/B,CAAA"}
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import mongoose from 'mongoose';
|
|
2
|
+
import { traverseFields } from 'payload';
|
|
3
|
+
import { fieldAffectsData } from 'payload/shared';
|
|
4
|
+
function isValidRelationObject(value) {
|
|
5
|
+
return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value;
|
|
6
|
+
}
|
|
7
|
+
const convertValue = ({ relatedCollection, value })=>{
|
|
8
|
+
const customIDField = relatedCollection.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
|
|
9
|
+
if (!customIDField) {
|
|
10
|
+
return new mongoose.Types.ObjectId(value);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
const sanitizeRelationship = ({ config, field, locale, ref, value })=>{
|
|
15
|
+
let relatedCollection;
|
|
16
|
+
let result = value;
|
|
17
|
+
const hasManyRelations = typeof field.relationTo !== 'string';
|
|
18
|
+
if (!hasManyRelations) {
|
|
19
|
+
relatedCollection = config.collections?.find(({ slug })=>slug === field.relationTo);
|
|
20
|
+
}
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
result = value.map((val)=>{
|
|
23
|
+
// Handle has many
|
|
24
|
+
if (relatedCollection && val && (typeof val === 'string' || typeof val === 'number')) {
|
|
25
|
+
return convertValue({
|
|
26
|
+
relatedCollection,
|
|
27
|
+
value: val
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
// Handle has many - polymorphic
|
|
31
|
+
if (isValidRelationObject(val)) {
|
|
32
|
+
const relatedCollectionForSingleValue = config.collections?.find(({ slug })=>slug === val.relationTo);
|
|
33
|
+
if (relatedCollectionForSingleValue) {
|
|
34
|
+
return {
|
|
35
|
+
relationTo: val.relationTo,
|
|
36
|
+
value: convertValue({
|
|
37
|
+
relatedCollection: relatedCollectionForSingleValue,
|
|
38
|
+
value: val.value
|
|
39
|
+
})
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return val;
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
// Handle has one - polymorphic
|
|
47
|
+
if (isValidRelationObject(value)) {
|
|
48
|
+
relatedCollection = config.collections?.find(({ slug })=>slug === value.relationTo);
|
|
49
|
+
if (relatedCollection) {
|
|
50
|
+
result = {
|
|
51
|
+
relationTo: value.relationTo,
|
|
52
|
+
value: convertValue({
|
|
53
|
+
relatedCollection,
|
|
54
|
+
value: value.value
|
|
55
|
+
})
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
// Handle has one
|
|
60
|
+
if (relatedCollection && value && (typeof value === 'string' || typeof value === 'number')) {
|
|
61
|
+
result = convertValue({
|
|
62
|
+
relatedCollection,
|
|
63
|
+
value
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
if (locale) {
|
|
67
|
+
ref[locale] = result;
|
|
68
|
+
} else {
|
|
69
|
+
ref[field.name] = result;
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
export const sanitizeRelationshipIDs = ({ config, data, fields })=>{
|
|
73
|
+
const sanitize = ({ field, ref })=>{
|
|
74
|
+
if (field.type === 'relationship' || field.type === 'upload') {
|
|
75
|
+
// handle localized relationships
|
|
76
|
+
if (config.localization && field.localized) {
|
|
77
|
+
const locales = config.localization.locales;
|
|
78
|
+
const fieldRef = ref[field.name];
|
|
79
|
+
for (const { code } of locales){
|
|
80
|
+
if (ref[field.name]?.[code]) {
|
|
81
|
+
const value = ref[field.name][code];
|
|
82
|
+
sanitizeRelationship({
|
|
83
|
+
config,
|
|
84
|
+
field,
|
|
85
|
+
locale: code,
|
|
86
|
+
ref: fieldRef,
|
|
87
|
+
value
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
} else {
|
|
92
|
+
// handle non-localized relationships
|
|
93
|
+
sanitizeRelationship({
|
|
94
|
+
config,
|
|
95
|
+
field,
|
|
96
|
+
locale: undefined,
|
|
97
|
+
ref,
|
|
98
|
+
value: ref[field.name]
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
traverseFields({
|
|
104
|
+
callback: sanitize,
|
|
105
|
+
fields,
|
|
106
|
+
ref: data
|
|
107
|
+
});
|
|
108
|
+
return data;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
//# sourceMappingURL=sanitizeRelationshipIDs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"sourcesContent":["import type { CollectionConfig, Field, SanitizedConfig, TraverseFieldsCallback } from 'payload'\n\nimport mongoose from 'mongoose'\nimport { traverseFields } from 'payload'\nimport { fieldAffectsData } from 'payload/shared'\n\ntype Args = {\n config: SanitizedConfig\n data: Record<string, unknown>\n fields: Field[]\n}\n\ninterface RelationObject {\n relationTo: string\n value: number | string\n}\n\nfunction isValidRelationObject(value: unknown): value is RelationObject {\n return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value\n}\n\nconst convertValue = ({\n relatedCollection,\n value,\n}: {\n relatedCollection: CollectionConfig\n value: number | string\n}): mongoose.Types.ObjectId | number | string => {\n const customIDField = relatedCollection.fields.find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n\n if (!customIDField) {\n return new mongoose.Types.ObjectId(value)\n }\n\n return value\n}\n\nconst sanitizeRelationship = ({ config, field, locale, ref, value }) => {\n let relatedCollection: CollectionConfig | undefined\n let result = value\n\n const hasManyRelations = typeof field.relationTo !== 'string'\n\n if (!hasManyRelations) {\n relatedCollection = config.collections?.find(({ slug }) => slug === field.relationTo)\n }\n\n if (Array.isArray(value)) {\n result = value.map((val) => {\n // Handle has many\n if (relatedCollection && val && (typeof val === 'string' || typeof val === 'number')) {\n return convertValue({\n relatedCollection,\n value: val,\n })\n }\n\n // Handle has many - polymorphic\n if (isValidRelationObject(val)) {\n const relatedCollectionForSingleValue = config.collections?.find(\n ({ slug }) => slug === val.relationTo,\n )\n\n if (relatedCollectionForSingleValue) {\n return {\n relationTo: val.relationTo,\n value: convertValue({\n relatedCollection: relatedCollectionForSingleValue,\n value: val.value,\n }),\n }\n }\n }\n\n return val\n })\n }\n\n // Handle has one - polymorphic\n if (isValidRelationObject(value)) {\n relatedCollection = config.collections?.find(({ slug }) => slug === value.relationTo)\n\n if (relatedCollection) {\n result = {\n relationTo: value.relationTo,\n value: convertValue({ relatedCollection, value: value.value }),\n }\n }\n }\n\n // Handle has one\n if (relatedCollection && value && (typeof value === 'string' || typeof value === 'number')) {\n result = convertValue({\n relatedCollection,\n value,\n })\n }\n if (locale) {\n ref[locale] = result\n } else {\n ref[field.name] = result\n }\n}\n\nexport const sanitizeRelationshipIDs = ({\n config,\n data,\n fields,\n}: Args): Record<string, unknown> => {\n const sanitize: TraverseFieldsCallback = ({ field, ref }) => {\n if (field.type === 'relationship' || field.type === 'upload') {\n // handle localized relationships\n if (config.localization && field.localized) {\n const locales = config.localization.locales\n const fieldRef = ref[field.name]\n for (const { code } of locales) {\n if (ref[field.name]?.[code]) {\n const value = ref[field.name][code]\n sanitizeRelationship({ config, field, locale: code, ref: fieldRef, value })\n }\n }\n } else {\n // handle non-localized relationships\n sanitizeRelationship({\n config,\n field,\n locale: undefined,\n ref,\n value: ref[field.name],\n })\n }\n }\n }\n\n traverseFields({ callback: sanitize, fields, ref: data })\n\n return data\n}\n"],"names":["mongoose","traverseFields","fieldAffectsData","isValidRelationObject","value","convertValue","relatedCollection","customIDField","fields","find","field","name","Types","ObjectId","sanitizeRelationship","config","locale","ref","result","hasManyRelations","relationTo","collections","slug","Array","isArray","map","val","relatedCollectionForSingleValue","sanitizeRelationshipIDs","data","sanitize","type","localization","localized","locales","fieldRef","code","undefined","callback"],"mappings":"AAEA,OAAOA,cAAc,WAAU;AAC/B,SAASC,cAAc,QAAQ,UAAS;AACxC,SAASC,gBAAgB,QAAQ,iBAAgB;AAajD,SAASC,sBAAsBC,KAAc;IAC3C,OAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,gBAAgBA,SAAS,WAAWA;AAC5F;AAEA,MAAMC,eAAe,CAAC,EACpBC,iBAAiB,EACjBF,KAAK,EAIN;IACC,MAAMG,gBAAgBD,kBAAkBE,MAAM,CAACC,IAAI,CACjD,CAACC,QAAUR,iBAAiBQ,UAAUA,MAAMC,IAAI,KAAK;IAGvD,IAAI,CAACJ,eAAe;QAClB,OAAO,IAAIP,SAASY,KAAK,CA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<reference types="mongoose/types/aggregate.js" />
|
|
2
|
-
/// <reference types="mongoose/types/callback.js" />
|
|
3
|
-
/// <reference types="mongoose/types/collection.js" />
|
|
4
|
-
/// <reference types="mongoose/types/connection.js" />
|
|
5
|
-
/// <reference types="mongoose/types/cursor.js" />
|
|
6
|
-
/// <reference types="mongoose/types/document.js" />
|
|
7
|
-
/// <reference types="mongoose/types/error.js" />
|
|
8
|
-
/// <reference types="mongoose/types/expressions.js" />
|
|
9
|
-
/// <reference types="mongoose/types/helpers.js" />
|
|
10
|
-
/// <reference types="mongoose/types/middlewares.js" />
|
|
11
|
-
/// <reference types="mongoose/types/indexes.js" />
|
|
12
|
-
/// <reference types="mongoose/types/models.js" />
|
|
13
|
-
/// <reference types="mongoose/types/mongooseoptions.js" />
|
|
14
|
-
/// <reference types="mongoose/types/pipelinestage.js" />
|
|
15
|
-
/// <reference types="mongoose/types/populate.js" />
|
|
16
|
-
/// <reference types="mongoose/types/query.js" />
|
|
17
|
-
/// <reference types="mongoose/types/schemaoptions.js" />
|
|
18
|
-
/// <reference types="mongoose/types/schematypes.js" />
|
|
19
|
-
/// <reference types="mongoose/types/session.js" />
|
|
20
|
-
/// <reference types="mongoose/types/types.js" />
|
|
21
|
-
/// <reference types="mongoose/types/utility.js" />
|
|
22
|
-
/// <reference types="mongoose/types/validation.js" />
|
|
23
|
-
/// <reference types="mongoose/types/virtuals.js" />
|
|
24
|
-
/// <reference types="mongoose/types/inferschematype.js" />
|
|
25
|
-
/// <reference types="mongoose-paginate-v2" />
|
|
26
1
|
import type { ClientSession } from 'mongoose';
|
|
2
|
+
import type { PayloadRequest } from 'payload';
|
|
27
3
|
import type { MongooseAdapter } from './index.js';
|
|
28
4
|
/**
|
|
29
5
|
* returns the session belonging to the transaction of the req.session if exists
|
|
30
6
|
* @returns ClientSession
|
|
31
7
|
*/
|
|
32
|
-
export declare function withSession(db: MongooseAdapter,
|
|
8
|
+
export declare function withSession(db: MongooseAdapter, req: PayloadRequest): Promise<{
|
|
33
9
|
session: ClientSession;
|
|
34
|
-
} |
|
|
10
|
+
} | Record<string, never>>;
|
|
35
11
|
//# sourceMappingURL=withSession.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"withSession.d.ts","sourceRoot":"","sources":["../src/withSession.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"withSession.d.ts","sourceRoot":"","sources":["../src/withSession.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,UAAU,CAAA;AAC7C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAEjD;;;GAGG;AACH,wBAAsB,WAAW,CAC/B,EAAE,EAAE,eAAe,EACnB,GAAG,EAAE,cAAc,GAClB,OAAO,CAAC;IAAE,OAAO,EAAE,aAAa,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC,CAU7D"}
|
package/dist/withSession.js
CHANGED
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* returns the session belonging to the transaction of the req.session if exists
|
|
3
3
|
* @returns ClientSession
|
|
4
|
-
*/ export function withSession(db,
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
4
|
+
*/ export async function withSession(db, req) {
|
|
5
|
+
let transactionID = req.transactionID;
|
|
6
|
+
if (transactionID instanceof Promise) {
|
|
7
|
+
transactionID = await req.transactionID;
|
|
8
|
+
}
|
|
9
|
+
if (req) {
|
|
10
|
+
return db.sessions[transactionID] ? {
|
|
11
|
+
session: db.sessions[transactionID]
|
|
12
|
+
} : {};
|
|
13
|
+
}
|
|
8
14
|
}
|
|
9
15
|
|
|
10
16
|
//# sourceMappingURL=withSession.js.map
|
package/dist/withSession.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/withSession.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\n\nimport type { MongooseAdapter } from './index.js'\n\n/**\n * returns the session belonging to the transaction of the req.session if exists\n * @returns ClientSession\n */\nexport function withSession(\n db: MongooseAdapter,\n
|
|
1
|
+
{"version":3,"sources":["../src/withSession.ts"],"sourcesContent":["import type { ClientSession } from 'mongoose'\nimport type { PayloadRequest } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\n/**\n * returns the session belonging to the transaction of the req.session if exists\n * @returns ClientSession\n */\nexport async function withSession(\n db: MongooseAdapter,\n req: PayloadRequest,\n): Promise<{ session: ClientSession } | Record<string, never>> {\n let transactionID = req.transactionID\n\n if (transactionID instanceof Promise) {\n transactionID = await req.transactionID\n }\n\n if (req) {\n return db.sessions[transactionID] ? { session: db.sessions[transactionID] } : {}\n }\n}\n"],"names":["withSession","db","req","transactionID","Promise","sessions","session"],"mappings":"AAKA;;;CAGC,GACD,OAAO,eAAeA,YACpBC,EAAmB,EACnBC,GAAmB;IAEnB,IAAIC,gBAAgBD,IAAIC,aAAa;IAErC,IAAIA,yBAAyBC,SAAS;QACpCD,gBAAgB,MAAMD,IAAIC,aAAa;IACzC;IAEA,IAAID,KAAK;QACP,OAAOD,GAAGI,QAAQ,CAACF,cAAc,GAAG;YAAEG,SAASL,GAAGI,QAAQ,CAACF,cAAc;QAAC,IAAI,CAAC;IACjF;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/db-mongodb",
|
|
3
|
-
"version": "3.0.0-canary.
|
|
3
|
+
"version": "3.0.0-canary.c696728",
|
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"exports": {
|
|
15
15
|
".": {
|
|
16
16
|
"import": "./dist/index.js",
|
|
17
|
-
"
|
|
18
|
-
"
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"main": "./dist/index.js",
|
|
@@ -27,27 +27,29 @@
|
|
|
27
27
|
],
|
|
28
28
|
"dependencies": {
|
|
29
29
|
"bson-objectid": "2.0.4",
|
|
30
|
-
"deepmerge": "4.3.1",
|
|
31
30
|
"http-status": "1.6.2",
|
|
32
31
|
"mongoose": "6.12.3",
|
|
32
|
+
"mongoose-aggregate-paginate-v2": "1.0.6",
|
|
33
33
|
"mongoose-paginate-v2": "1.7.22",
|
|
34
34
|
"prompts": "2.4.2",
|
|
35
|
-
"uuid": "
|
|
35
|
+
"uuid": "10.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|
|
38
|
-
"@types/mongoose-aggregate-paginate-v2": "1.0.
|
|
38
|
+
"@types/mongoose-aggregate-paginate-v2": "1.0.6",
|
|
39
39
|
"mongodb": "4.17.1",
|
|
40
40
|
"mongodb-memory-server": "^9",
|
|
41
|
-
"@payloadcms/eslint-config": "
|
|
42
|
-
"payload": "3.0.0-canary.
|
|
41
|
+
"@payloadcms/eslint-config": "3.0.0-beta.97",
|
|
42
|
+
"payload": "3.0.0-canary.c696728"
|
|
43
43
|
},
|
|
44
44
|
"peerDependencies": {
|
|
45
|
-
"payload": "3.0.0-canary.
|
|
45
|
+
"payload": "3.0.0-canary.c696728"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
|
-
"build": "pnpm build:
|
|
49
|
-
"build:swc": "swc ./src -d ./dist --config-file .swcrc-build",
|
|
48
|
+
"build": "pnpm build:types && pnpm build:swc",
|
|
49
|
+
"build:swc": "swc ./src -d ./dist --config-file .swcrc-build --strip-leading-paths",
|
|
50
50
|
"build:types": "tsc --emitDeclarationOnly --outDir dist",
|
|
51
|
-
"clean": "rimraf {dist,*.tsbuildinfo}"
|
|
51
|
+
"clean": "rimraf {dist,*.tsbuildinfo}",
|
|
52
|
+
"lint": "eslint .",
|
|
53
|
+
"lint:fix": "eslint . --fix"
|
|
52
54
|
}
|
|
53
55
|
}
|