@payloadcms/db-mongodb 3.25.0-canary.d7d40bf → 3.25.0-canary.fd53f68
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/create.js +15 -15
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +15 -14
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +26 -23
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +26 -31
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +8 -6
- package/dist/deleteOne.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +8 -9
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +10 -9
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +10 -10
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +8 -6
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.js +8 -9
- package/dist/findVersions.js.map +1 -1
- package/dist/models/buildSchema.js +2 -0
- package/dist/models/buildSchema.js.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +12 -7
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/queryDrafts.js +13 -13
- package/dist/queryDrafts.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +15 -15
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +13 -13
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +12 -9
- package/dist/updateOne.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +13 -13
- package/dist/updateVersion.js.map +1 -1
- package/dist/utilities/transform.d.ts +27 -0
- package/dist/utilities/transform.d.ts.map +1 -0
- package/dist/utilities/transform.js +224 -0
- package/dist/utilities/transform.js.map +1 -0
- package/dist/utilities/{sanitizeRelationshipIDs.spec.js → transform.spec.js} +11 -5
- package/dist/utilities/transform.spec.js.map +1 -0
- package/package.json +3 -3
- package/dist/utilities/sanitizeInternalFields.d.ts +0 -2
- package/dist/utilities/sanitizeInternalFields.d.ts.map +0 -1
- package/dist/utilities/sanitizeInternalFields.js +0 -20
- package/dist/utilities/sanitizeInternalFields.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts +0 -10
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.js +0 -130
- package/dist/utilities/sanitizeRelationshipIDs.js.map +0 -1
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +0 -1
package/dist/updateGlobal.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
2
2
|
import { getSession } from './utilities/getSession.js';
|
|
3
|
-
import {
|
|
4
|
-
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js';
|
|
3
|
+
import { transform } from './utilities/transform.js';
|
|
5
4
|
export const updateGlobal = async function updateGlobal({ slug, data, options: optionsArgs = {}, req, select }) {
|
|
6
5
|
const Model = this.globals;
|
|
7
6
|
const fields = this.payload.config.globals.find((global)=>global.slug === slug).fields;
|
|
@@ -16,22 +15,23 @@ export const updateGlobal = async function updateGlobal({ slug, data, options: o
|
|
|
16
15
|
}),
|
|
17
16
|
session: await getSession(this, req)
|
|
18
17
|
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
config: this.payload.config,
|
|
18
|
+
transform({
|
|
19
|
+
adapter: this,
|
|
22
20
|
data,
|
|
23
|
-
fields
|
|
21
|
+
fields,
|
|
22
|
+
globalSlug: slug,
|
|
23
|
+
operation: 'write'
|
|
24
24
|
});
|
|
25
|
-
result = await Model.findOneAndUpdate({
|
|
25
|
+
const result = await Model.findOneAndUpdate({
|
|
26
26
|
globalType: slug
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
27
|
+
}, data, options);
|
|
28
|
+
transform({
|
|
29
|
+
adapter: this,
|
|
30
|
+
data: result,
|
|
31
|
+
fields,
|
|
32
|
+
globalSlug: slug,
|
|
33
|
+
operation: 'read'
|
|
34
|
+
});
|
|
35
35
|
return result;
|
|
36
36
|
};
|
|
37
37
|
|
package/dist/updateGlobal.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateGlobal.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateGlobal } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/updateGlobal.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateGlobal } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateGlobal: UpdateGlobal = async function updateGlobal(\n this: MongooseAdapter,\n { slug, data, options: optionsArgs = {}, req, select },\n) {\n const Model = this.globals\n const fields = this.payload.config.globals.find((global) => global.slug === slug).fields\n\n const options: QueryOptions = {\n ...optionsArgs,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: this.payload.config.globals.find((global) => global.slug === slug).flattenedFields,\n select,\n }),\n session: await getSession(this, req),\n }\n\n transform({ adapter: this, data, fields, globalSlug: slug, operation: 'write' })\n\n const result: any = await Model.findOneAndUpdate({ globalType: slug }, data, options)\n\n transform({ adapter: this, data: result, fields, globalSlug: slug, operation: 'read' })\n\n return result\n}\n"],"names":["buildProjectionFromSelect","getSession","transform","updateGlobal","slug","data","options","optionsArgs","req","select","Model","globals","fields","payload","config","find","global","lean","new","projection","adapter","flattenedFields","session","globalSlug","operation","result","findOneAndUpdate","globalType"],"mappings":"AAKA,SAASA,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,eAA6B,eAAeA,aAEvD,EAAEC,IAAI,EAAEC,IAAI,EAAEC,SAASC,cAAc,CAAC,CAAC,EAAEC,GAAG,EAAEC,MAAM,EAAE;IAEtD,MAAMC,QAAQ,IAAI,CAACC,OAAO;IAC1B,MAAMC,SAAS,IAAI,CAACC,OAAO,CAACC,MAAM,CAACH,OAAO,CAACI,IAAI,CAAC,CAACC,SAAWA,OAAOZ,IAAI,KAAKA,MAAMQ,MAAM;IAExF,MAAMN,UAAwB;QAC5B,GAAGC,WAAW;QACdU,MAAM;QACNC,KAAK;QACLC,YAAYnB,0BAA0B;YACpCoB,SAAS,IAAI;YACbR,QAAQ,IAAI,CAACC,OAAO,CAACC,MAAM,CAACH,OAAO,CAACI,IAAI,CAAC,CAACC,SAAWA,OAAOZ,IAAI,KAAKA,MAAMiB,eAAe;YAC1FZ;QACF;QACAa,SAAS,MAAMrB,WAAW,IAAI,EAAEO;IAClC;IAEAN,UAAU;QAAEkB,SAAS,IAAI;QAAEf;QAAMO;QAAQW,YAAYnB;QAAMoB,WAAW;IAAQ;IAE9E,MAAMC,SAAc,MAAMf,MAAMgB,gBAAgB,CAAC;QAAEC,YAAYvB;IAAK,GAAGC,MAAMC;IAE7EJ,UAAU;QAAEkB,SAAS,IAAI;QAAEf,MAAMoB;QAAQb;QAAQW,YAAYnB;QAAMoB,WAAW;IAAO;IAErF,OAAOC;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateGlobalVersion.d.ts","sourceRoot":"","sources":["../src/updateGlobalVersion.ts"],"names":[],"mappings":"AAEA,OAAO,EAA4B,KAAK,UAAU,EAAE,KAAK,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAEjG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAOjD,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAC5D,IAAI,EAAE,eAAe,EACrB,EACE,EAAE,EACF,MAAM,EAAE,UAAU,EAClB,MAAM,EACN,OAAO,EAAE,WAAgB,EACzB,GAAG,EACH,MAAM,EACN,WAAW,EACX,KAAK,GACN,EAAE,uBAAuB,CAAC,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"updateGlobalVersion.d.ts","sourceRoot":"","sources":["../src/updateGlobalVersion.ts"],"names":[],"mappings":"AAEA,OAAO,EAA4B,KAAK,UAAU,EAAE,KAAK,uBAAuB,EAAE,MAAM,SAAS,CAAA;AAEjG,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AAOjD,wBAAsB,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAC5D,IAAI,EAAE,eAAe,EACrB,EACE,EAAE,EACF,MAAM,EAAE,UAAU,EAClB,MAAM,EACN,OAAO,EAAE,WAAgB,EACzB,GAAG,EACH,MAAM,EACN,WAAW,EACX,KAAK,GACN,EAAE,uBAAuB,CAAC,CAAC,CAAC,gBAsC9B"}
|
|
@@ -2,7 +2,7 @@ import { buildVersionGlobalFields } from 'payload';
|
|
|
2
2
|
import { buildQuery } from './queries/buildQuery.js';
|
|
3
3
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
4
4
|
import { getSession } from './utilities/getSession.js';
|
|
5
|
-
import {
|
|
5
|
+
import { transform } from './utilities/transform.js';
|
|
6
6
|
export async function updateGlobalVersion({ id, global: globalSlug, locale, options: optionsArgs = {}, req, select, versionData, where }) {
|
|
7
7
|
const VersionModel = this.versions[globalSlug];
|
|
8
8
|
const whereToUse = where || {
|
|
@@ -30,23 +30,23 @@ export async function updateGlobalVersion({ id, global: globalSlug, locale, opti
|
|
|
30
30
|
locale,
|
|
31
31
|
where: whereToUse
|
|
32
32
|
});
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
transform({
|
|
34
|
+
adapter: this,
|
|
35
35
|
data: versionData,
|
|
36
|
-
fields
|
|
36
|
+
fields,
|
|
37
|
+
operation: 'write'
|
|
37
38
|
});
|
|
38
|
-
const doc = await VersionModel.findOneAndUpdate(query,
|
|
39
|
+
const doc = await VersionModel.findOneAndUpdate(query, versionData, options);
|
|
39
40
|
if (!doc) {
|
|
40
41
|
return null;
|
|
41
42
|
}
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
return result;
|
|
43
|
+
transform({
|
|
44
|
+
adapter: this,
|
|
45
|
+
data: doc,
|
|
46
|
+
fields,
|
|
47
|
+
operation: 'read'
|
|
48
|
+
});
|
|
49
|
+
return doc;
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
//# sourceMappingURL=updateGlobalVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateGlobalVersion.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\n\nimport { buildVersionGlobalFields, type TypeWithID, type UpdateGlobalVersionArgs } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/updateGlobalVersion.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\n\nimport { buildVersionGlobalFields, type TypeWithID, type UpdateGlobalVersionArgs } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport async function updateGlobalVersion<T extends TypeWithID>(\n this: MongooseAdapter,\n {\n id,\n global: globalSlug,\n locale,\n options: optionsArgs = {},\n req,\n select,\n versionData,\n where,\n }: UpdateGlobalVersionArgs<T>,\n) {\n const VersionModel = this.versions[globalSlug]\n const whereToUse = where || { id: { equals: id } }\n\n const currentGlobal = this.payload.config.globals.find((global) => global.slug === globalSlug)\n const fields = buildVersionGlobalFields(this.payload.config, currentGlobal)\n const flattenedFields = buildVersionGlobalFields(this.payload.config, currentGlobal, true)\n const options: QueryOptions = {\n ...optionsArgs,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: flattenedFields,\n select,\n }),\n session: await getSession(this, req),\n }\n\n const query = await buildQuery({\n adapter: this,\n fields: flattenedFields,\n locale,\n where: whereToUse,\n })\n\n transform({ adapter: this, data: versionData, fields, operation: 'write' })\n\n const doc = await VersionModel.findOneAndUpdate(query, versionData, options)\n\n if (!doc) {\n return null\n }\n\n transform({ adapter: this, data: doc, fields, operation: 'read' })\n\n return doc\n}\n"],"names":["buildVersionGlobalFields","buildQuery","buildProjectionFromSelect","getSession","transform","updateGlobalVersion","id","global","globalSlug","locale","options","optionsArgs","req","select","versionData","where","VersionModel","versions","whereToUse","equals","currentGlobal","payload","config","globals","find","slug","fields","flattenedFields","lean","new","projection","adapter","session","query","data","operation","doc","findOneAndUpdate"],"mappings":"AAEA,SAASA,wBAAwB,QAAuD,UAAS;AAIjG,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,yBAAyB,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 @@
|
|
|
1
|
-
{"version":3,"file":"updateOne.d.ts","sourceRoot":"","sources":["../src/updateOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"updateOne.d.ts","sourceRoot":"","sources":["../src/updateOne.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,SAAS,CAAA;AAUxC,eAAO,MAAM,SAAS,EAAE,SA4CvB,CAAA"}
|
package/dist/updateOne.js
CHANGED
|
@@ -2,8 +2,7 @@ import { buildQuery } from './queries/buildQuery.js';
|
|
|
2
2
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
3
3
|
import { getSession } from './utilities/getSession.js';
|
|
4
4
|
import { handleError } from './utilities/handleError.js';
|
|
5
|
-
import {
|
|
6
|
-
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js';
|
|
5
|
+
import { transform } from './utilities/transform.js';
|
|
7
6
|
export const updateOne = async function updateOne({ id, collection, data, locale, options: optionsArgs = {}, req, select, where: whereArg }) {
|
|
8
7
|
const where = id ? {
|
|
9
8
|
id: {
|
|
@@ -31,13 +30,14 @@ export const updateOne = async function updateOne({ id, collection, data, locale
|
|
|
31
30
|
where
|
|
32
31
|
});
|
|
33
32
|
let result;
|
|
34
|
-
|
|
35
|
-
|
|
33
|
+
transform({
|
|
34
|
+
adapter: this,
|
|
36
35
|
data,
|
|
37
|
-
fields
|
|
36
|
+
fields,
|
|
37
|
+
operation: 'write'
|
|
38
38
|
});
|
|
39
39
|
try {
|
|
40
|
-
result = await Model.findOneAndUpdate(query,
|
|
40
|
+
result = await Model.findOneAndUpdate(query, data, options);
|
|
41
41
|
} catch (error) {
|
|
42
42
|
handleError({
|
|
43
43
|
collection,
|
|
@@ -48,9 +48,12 @@ export const updateOne = async function updateOne({ id, collection, data, locale
|
|
|
48
48
|
if (!result) {
|
|
49
49
|
return null;
|
|
50
50
|
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
51
|
+
transform({
|
|
52
|
+
adapter: this,
|
|
53
|
+
data: result,
|
|
54
|
+
fields,
|
|
55
|
+
operation: 'read'
|
|
56
|
+
});
|
|
54
57
|
return result;
|
|
55
58
|
};
|
|
56
59
|
|
package/dist/updateOne.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateOne.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateOne } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/updateOne.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\nimport type { UpdateOne } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { handleError } from './utilities/handleError.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateOne: UpdateOne = async function updateOne(\n this: MongooseAdapter,\n { id, collection, data, locale, options: optionsArgs = {}, req, select, where: whereArg },\n) {\n const where = id ? { id: { equals: id } } : whereArg\n const Model = this.collections[collection]\n const fields = this.payload.collections[collection].config.fields\n const options: QueryOptions = {\n ...optionsArgs,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: this.payload.collections[collection].config.flattenedFields,\n select,\n }),\n session: await getSession(this, req),\n }\n\n const query = await buildQuery({\n adapter: this,\n collectionSlug: collection,\n fields: this.payload.collections[collection].config.flattenedFields,\n locale,\n where,\n })\n\n let result\n\n transform({ adapter: this, data, fields, operation: 'write' })\n\n try {\n result = await Model.findOneAndUpdate(query, data, options)\n } catch (error) {\n handleError({ collection, error, req })\n }\n\n if (!result) {\n return null\n }\n\n transform({ adapter: this, data: result, fields, operation: 'read' })\n\n return result\n}\n"],"names":["buildQuery","buildProjectionFromSelect","getSession","handleError","transform","updateOne","id","collection","data","locale","options","optionsArgs","req","select","where","whereArg","equals","Model","collections","fields","payload","config","lean","new","projection","adapter","flattenedFields","session","query","collectionSlug","result","operation","findOneAndUpdate","error"],"mappings":"AAKA,SAASA,UAAU,QAAQ,0BAAyB;AACpD,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,WAAW,QAAQ,6BAA4B;AACxD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,YAAuB,eAAeA,UAEjD,EAAEC,EAAE,EAAEC,UAAU,EAAEC,IAAI,EAAEC,MAAM,EAAEC,SAASC,cAAc,CAAC,CAAC,EAAEC,GAAG,EAAEC,MAAM,EAAEC,OAAOC,QAAQ,EAAE;IAEzF,MAAMD,QAAQR,KAAK;QAAEA,IAAI;YAAEU,QAAQV;QAAG;IAAE,IAAIS;IAC5C,MAAME,QAAQ,IAAI,CAACC,WAAW,CAACX,WAAW;IAC1C,MAAMY,SAAS,IAAI,CAACC,OAAO,CAACF,WAAW,CAACX,WAAW,CAACc,MAAM,CAACF,MAAM;IACjE,MAAMT,UAAwB;QAC5B,GAAGC,WAAW;QACdW,MAAM;QACNC,KAAK;QACLC,YAAYvB,0BAA0B;YACpCwB,SAAS,IAAI;YACbN,QAAQ,IAAI,CAACC,OAAO,CAACF,WAAW,CAACX,WAAW,CAACc,MAAM,CAACK,eAAe;YACnEb;QACF;QACAc,SAAS,MAAMzB,WAAW,IAAI,EAAEU;IAClC;IAEA,MAAMgB,QAAQ,MAAM5B,WAAW;QAC7ByB,SAAS,IAAI;QACbI,gBAAgBtB;QAChBY,QAAQ,IAAI,CAACC,OAAO,CAACF,WAAW,CAACX,WAAW,CAACc,MAAM,CAACK,eAAe;QACnEjB;QACAK;IACF;IAEA,IAAIgB;IAEJ1B,UAAU;QAAEqB,SAAS,IAAI;QAAEjB;QAAMW;QAAQY,WAAW;IAAQ;IAE5D,IAAI;QACFD,SAAS,MAAMb,MAAMe,gBAAgB,CAACJ,OAAOpB,MAAME;IACrD,EAAE,OAAOuB,OAAO;QACd9B,YAAY;YAAEI;YAAY0B;YAAOrB;QAAI;IACvC;IAEA,IAAI,CAACkB,QAAQ;QACX,OAAO;IACT;IAEA1B,UAAU;QAAEqB,SAAS,IAAI;QAAEjB,MAAMsB;QAAQX;QAAQY,WAAW;IAAO;IAEnE,OAAOD;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"updateVersion.d.ts","sourceRoot":"","sources":["../src/updateVersion.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgC,KAAK,aAAa,EAAE,MAAM,SAAS,CAAA;AAS1E,eAAO,MAAM,aAAa,EAAE,
|
|
1
|
+
{"version":3,"file":"updateVersion.d.ts","sourceRoot":"","sources":["../src/updateVersion.ts"],"names":[],"mappings":"AAEA,OAAO,EAAgC,KAAK,aAAa,EAAE,MAAM,SAAS,CAAA;AAS1E,eAAO,MAAM,aAAa,EAAE,aA+C3B,CAAA"}
|
package/dist/updateVersion.js
CHANGED
|
@@ -2,7 +2,7 @@ import { buildVersionCollectionFields } from 'payload';
|
|
|
2
2
|
import { buildQuery } from './queries/buildQuery.js';
|
|
3
3
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
|
4
4
|
import { getSession } from './utilities/getSession.js';
|
|
5
|
-
import {
|
|
5
|
+
import { transform } from './utilities/transform.js';
|
|
6
6
|
export const updateVersion = async function updateVersion({ id, collection, locale, options: optionsArgs = {}, req, select, versionData, where }) {
|
|
7
7
|
const VersionModel = this.versions[collection];
|
|
8
8
|
const whereToUse = where || {
|
|
@@ -29,23 +29,23 @@ export const updateVersion = async function updateVersion({ id, collection, loca
|
|
|
29
29
|
locale,
|
|
30
30
|
where: whereToUse
|
|
31
31
|
});
|
|
32
|
-
|
|
33
|
-
|
|
32
|
+
transform({
|
|
33
|
+
adapter: this,
|
|
34
34
|
data: versionData,
|
|
35
|
-
fields
|
|
35
|
+
fields,
|
|
36
|
+
operation: 'write'
|
|
36
37
|
});
|
|
37
|
-
const doc = await VersionModel.findOneAndUpdate(query,
|
|
38
|
+
const doc = await VersionModel.findOneAndUpdate(query, versionData, options);
|
|
38
39
|
if (!doc) {
|
|
39
40
|
return null;
|
|
40
41
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
return result;
|
|
42
|
+
transform({
|
|
43
|
+
adapter: this,
|
|
44
|
+
data: doc,
|
|
45
|
+
fields,
|
|
46
|
+
operation: 'write'
|
|
47
|
+
});
|
|
48
|
+
return doc;
|
|
49
49
|
};
|
|
50
50
|
|
|
51
51
|
//# sourceMappingURL=updateVersion.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\n\nimport { buildVersionCollectionFields, type UpdateVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport {
|
|
1
|
+
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\n\nimport { buildVersionCollectionFields, type UpdateVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildQuery } from './queries/buildQuery.js'\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { getSession } from './utilities/getSession.js'\nimport { transform } from './utilities/transform.js'\n\nexport const updateVersion: UpdateVersion = async function updateVersion(\n this: MongooseAdapter,\n { id, collection, locale, options: optionsArgs = {}, req, select, versionData, where },\n) {\n const VersionModel = this.versions[collection]\n const whereToUse = where || { id: { equals: id } }\n const fields = buildVersionCollectionFields(\n this.payload.config,\n this.payload.collections[collection].config,\n )\n\n const flattenedFields = buildVersionCollectionFields(\n this.payload.config,\n this.payload.collections[collection].config,\n true,\n )\n\n const options: QueryOptions = {\n ...optionsArgs,\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({\n adapter: this,\n fields: flattenedFields,\n select,\n }),\n session: await getSession(this, req),\n }\n\n const query = await buildQuery({\n adapter: this,\n fields: flattenedFields,\n locale,\n where: whereToUse,\n })\n\n transform({ adapter: this, data: versionData, fields, operation: 'write' })\n\n const doc = await VersionModel.findOneAndUpdate(query, versionData, options)\n\n if (!doc) {\n return null\n }\n\n transform({ adapter: this, data: doc, fields, operation: 'write' })\n\n return doc\n}\n"],"names":["buildVersionCollectionFields","buildQuery","buildProjectionFromSelect","getSession","transform","updateVersion","id","collection","locale","options","optionsArgs","req","select","versionData","where","VersionModel","versions","whereToUse","equals","fields","payload","config","collections","flattenedFields","lean","new","projection","adapter","session","query","data","operation","doc","findOneAndUpdate"],"mappings":"AAEA,SAASA,4BAA4B,QAA4B,UAAS;AAI1E,SAASC,UAAU,QAAQ,0BAAyB;AACpD,SAASC,yBAAyB,QAAQ,2CAA0C;AACpF,SAASC,UAAU,QAAQ,4BAA2B;AACtD,SAASC,SAAS,QAAQ,2BAA0B;AAEpD,OAAO,MAAMC,gBAA+B,eAAeA,cAEzD,EAAEC,EAAE,EAAEC,UAAU,EAAEC,MAAM,EAAEC,SAASC,cAAc,CAAC,CAAC,EAAEC,GAAG,EAAEC,MAAM,EAAEC,WAAW,EAAEC,KAAK,EAAE;IAEtF,MAAMC,eAAe,IAAI,CAACC,QAAQ,CAACT,WAAW;IAC9C,MAAMU,aAAaH,SAAS;QAAER,IAAI;YAAEY,QAAQZ;QAAG;IAAE;IACjD,MAAMa,SAASnB,6BACb,IAAI,CAACoB,OAAO,CAACC,MAAM,EACnB,IAAI,CAACD,OAAO,CAACE,WAAW,CAACf,WAAW,CAACc,MAAM;IAG7C,MAAME,kBAAkBvB,6BACtB,IAAI,CAACoB,OAAO,CAACC,MAAM,EACnB,IAAI,CAACD,OAAO,CAACE,WAAW,CAACf,WAAW,CAACc,MAAM,EAC3C;IAGF,MAAMZ,UAAwB;QAC5B,GAAGC,WAAW;QACdc,MAAM;QACNC,KAAK;QACLC,YAAYxB,0BAA0B;YACpCyB,SAAS,IAAI;YACbR,QAAQI;YACRX;QACF;QACAgB,SAAS,MAAMzB,WAAW,IAAI,EAAEQ;IAClC;IAEA,MAAMkB,QAAQ,MAAM5B,WAAW;QAC7B0B,SAAS,IAAI;QACbR,QAAQI;QACRf;QACAM,OAAOG;IACT;IAEAb,UAAU;QAAEuB,SAAS,IAAI;QAAEG,MAAMjB;QAAaM;QAAQY,WAAW;IAAQ;IAEzE,MAAMC,MAAM,MAAMjB,aAAakB,gBAAgB,CAACJ,OAAOhB,aAAaJ;IAEpE,IAAI,CAACuB,KAAK;QACR,OAAO;IACT;IAEA5B,UAAU;QAAEuB,SAAS,IAAI;QAAEG,MAAME;QAAKb;QAAQY,WAAW;IAAQ;IAEjE,OAAOC;AACT,EAAC"}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { Field } from 'payload';
|
|
2
|
+
import type { MongooseAdapter } from '../index.js';
|
|
3
|
+
type Args = {
|
|
4
|
+
/** instance of the adapter */
|
|
5
|
+
adapter: MongooseAdapter;
|
|
6
|
+
/** data to transform, can be an array of documents or a single document */
|
|
7
|
+
data: Record<string, unknown> | Record<string, unknown>[];
|
|
8
|
+
/** fields accossiated with the data */
|
|
9
|
+
fields: Field[];
|
|
10
|
+
/** slug of the global, pass only when the operation is `write` */
|
|
11
|
+
globalSlug?: string;
|
|
12
|
+
/**
|
|
13
|
+
* Type of the operation
|
|
14
|
+
* read - sanitizes ObjectIDs, Date to strings.
|
|
15
|
+
* write - sanitizes string relationships to ObjectIDs.
|
|
16
|
+
*/
|
|
17
|
+
operation: 'read' | 'write';
|
|
18
|
+
parentIsLocalized?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* Throw errors on invalid relationships
|
|
21
|
+
* @default true
|
|
22
|
+
*/
|
|
23
|
+
validateRelationships?: boolean;
|
|
24
|
+
};
|
|
25
|
+
export declare const transform: ({ adapter, data, fields, globalSlug, operation, parentIsLocalized, validateRelationships, }: Args) => void;
|
|
26
|
+
export {};
|
|
27
|
+
//# sourceMappingURL=transform.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transform.d.ts","sourceRoot":"","sources":["../../src/utilities/transform.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAGV,KAAK,EAMN,MAAM,SAAS,CAAA;AAMhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAgMlD,KAAK,IAAI,GAAG;IACV,8BAA8B;IAC9B,OAAO,EAAE,eAAe,CAAA;IACxB,2EAA2E;IAC3E,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;IACzD,uCAAuC;IACvC,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,kEAAkE;IAClE,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;;OAIG;IACH,SAAS,EAAE,MAAM,GAAG,OAAO,CAAA;IAC3B,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAA;CAChC,CAAA;AAED,eAAO,MAAM,SAAS,gGAQnB,IAAI,SA4GN,CAAA"}
|
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
import { Types } from 'mongoose';
|
|
2
|
+
import { traverseFields } from 'payload';
|
|
3
|
+
import { fieldAffectsData, fieldShouldBeLocalized } from 'payload/shared';
|
|
4
|
+
function isValidRelationObject(value) {
|
|
5
|
+
return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value;
|
|
6
|
+
}
|
|
7
|
+
const convertRelationshipValue = ({ operation, relatedCollection, validateRelationships, value })=>{
|
|
8
|
+
const customIDField = relatedCollection.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
|
|
9
|
+
if (operation === 'read') {
|
|
10
|
+
if (value instanceof Types.ObjectId) {
|
|
11
|
+
return value.toHexString();
|
|
12
|
+
}
|
|
13
|
+
return value;
|
|
14
|
+
}
|
|
15
|
+
if (customIDField) {
|
|
16
|
+
return value;
|
|
17
|
+
}
|
|
18
|
+
if (typeof value === 'string') {
|
|
19
|
+
try {
|
|
20
|
+
return new Types.ObjectId(value);
|
|
21
|
+
} catch (e) {
|
|
22
|
+
if (validateRelationships) {
|
|
23
|
+
throw e;
|
|
24
|
+
}
|
|
25
|
+
return value;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
return value;
|
|
29
|
+
};
|
|
30
|
+
const sanitizeRelationship = ({ config, field, locale, operation, ref, validateRelationships, value })=>{
|
|
31
|
+
if (field.type === 'join') {
|
|
32
|
+
if (operation === 'read' && value && typeof value === 'object' && 'docs' in value && Array.isArray(value.docs)) {
|
|
33
|
+
for(let i = 0; i < value.docs.length; i++){
|
|
34
|
+
const item = value.docs[i];
|
|
35
|
+
if (item instanceof Types.ObjectId) {
|
|
36
|
+
value.docs[i] = item.toHexString();
|
|
37
|
+
} else if (Array.isArray(field.collection) && item) {
|
|
38
|
+
// Fields here for polymorphic joins cannot be determinted, JSON.parse needed
|
|
39
|
+
value.docs[i] = JSON.parse(JSON.stringify(value.docs[i]));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return value;
|
|
44
|
+
}
|
|
45
|
+
let relatedCollection;
|
|
46
|
+
let result = value;
|
|
47
|
+
const hasManyRelations = typeof field.relationTo !== 'string';
|
|
48
|
+
if (!hasManyRelations) {
|
|
49
|
+
relatedCollection = config.collections?.find(({ slug })=>slug === field.relationTo);
|
|
50
|
+
}
|
|
51
|
+
if (Array.isArray(value)) {
|
|
52
|
+
result = value.map((val)=>{
|
|
53
|
+
// Handle has many - polymorphic
|
|
54
|
+
if (isValidRelationObject(val)) {
|
|
55
|
+
const relatedCollectionForSingleValue = config.collections?.find(({ slug })=>slug === val.relationTo);
|
|
56
|
+
if (relatedCollectionForSingleValue) {
|
|
57
|
+
return {
|
|
58
|
+
relationTo: val.relationTo,
|
|
59
|
+
value: convertRelationshipValue({
|
|
60
|
+
operation,
|
|
61
|
+
relatedCollection: relatedCollectionForSingleValue,
|
|
62
|
+
validateRelationships,
|
|
63
|
+
value: val.value
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
if (relatedCollection) {
|
|
69
|
+
return convertRelationshipValue({
|
|
70
|
+
operation,
|
|
71
|
+
relatedCollection,
|
|
72
|
+
validateRelationships,
|
|
73
|
+
value: val
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
return val;
|
|
77
|
+
});
|
|
78
|
+
} else if (isValidRelationObject(value)) {
|
|
79
|
+
relatedCollection = config.collections?.find(({ slug })=>slug === value.relationTo);
|
|
80
|
+
if (relatedCollection) {
|
|
81
|
+
result = {
|
|
82
|
+
relationTo: value.relationTo,
|
|
83
|
+
value: convertRelationshipValue({
|
|
84
|
+
operation,
|
|
85
|
+
relatedCollection,
|
|
86
|
+
validateRelationships,
|
|
87
|
+
value: value.value
|
|
88
|
+
})
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
} else if (relatedCollection) {
|
|
92
|
+
result = convertRelationshipValue({
|
|
93
|
+
operation,
|
|
94
|
+
relatedCollection,
|
|
95
|
+
validateRelationships,
|
|
96
|
+
value
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
if (locale) {
|
|
100
|
+
ref[locale] = result;
|
|
101
|
+
} else {
|
|
102
|
+
ref[field.name] = result;
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
const sanitizeDate = ({ field, locale, ref, value })=>{
|
|
106
|
+
if (!value) {
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (value instanceof Date) {
|
|
110
|
+
value = value.toISOString();
|
|
111
|
+
}
|
|
112
|
+
if (locale) {
|
|
113
|
+
ref[locale] = value;
|
|
114
|
+
} else {
|
|
115
|
+
ref[field.name] = value;
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
export const transform = ({ adapter, data, fields, globalSlug, operation, parentIsLocalized, validateRelationships = true })=>{
|
|
119
|
+
if (Array.isArray(data)) {
|
|
120
|
+
for(let i = 0; i < data.length; i++){
|
|
121
|
+
transform({
|
|
122
|
+
adapter,
|
|
123
|
+
data: data[i],
|
|
124
|
+
fields,
|
|
125
|
+
globalSlug,
|
|
126
|
+
operation,
|
|
127
|
+
validateRelationships
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const { payload: { config } } = adapter;
|
|
133
|
+
if (operation === 'read') {
|
|
134
|
+
delete data['__v'];
|
|
135
|
+
data.id = data._id;
|
|
136
|
+
delete data['_id'];
|
|
137
|
+
if (data.id instanceof Types.ObjectId) {
|
|
138
|
+
data.id = data.id.toHexString();
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (operation === 'write' && globalSlug) {
|
|
142
|
+
data.globalType = globalSlug;
|
|
143
|
+
}
|
|
144
|
+
const sanitize = ({ field, ref })=>{
|
|
145
|
+
if (!ref || typeof ref !== 'object') {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
if (field.type === 'date' && operation === 'read' && ref[field.name]) {
|
|
149
|
+
if (config.localization && fieldShouldBeLocalized({
|
|
150
|
+
field,
|
|
151
|
+
parentIsLocalized
|
|
152
|
+
})) {
|
|
153
|
+
const fieldRef = ref[field.name];
|
|
154
|
+
if (!fieldRef || typeof fieldRef !== 'object') {
|
|
155
|
+
return;
|
|
156
|
+
}
|
|
157
|
+
for (const locale of config.localization.localeCodes){
|
|
158
|
+
sanitizeDate({
|
|
159
|
+
field,
|
|
160
|
+
ref: fieldRef,
|
|
161
|
+
value: fieldRef[locale]
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
} else {
|
|
165
|
+
sanitizeDate({
|
|
166
|
+
field,
|
|
167
|
+
ref: ref,
|
|
168
|
+
value: ref[field.name]
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
if (field.type === 'relationship' || field.type === 'upload' || operation === 'read' && field.type === 'join') {
|
|
173
|
+
if (!ref[field.name]) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
// handle localized relationships
|
|
177
|
+
if (config.localization && fieldShouldBeLocalized({
|
|
178
|
+
field,
|
|
179
|
+
parentIsLocalized
|
|
180
|
+
})) {
|
|
181
|
+
const locales = config.localization.locales;
|
|
182
|
+
const fieldRef = ref[field.name];
|
|
183
|
+
if (typeof fieldRef !== 'object') {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
for (const { code } of locales){
|
|
187
|
+
const value = ref[field.name][code];
|
|
188
|
+
if (value) {
|
|
189
|
+
sanitizeRelationship({
|
|
190
|
+
config,
|
|
191
|
+
field,
|
|
192
|
+
locale: code,
|
|
193
|
+
operation,
|
|
194
|
+
ref: fieldRef,
|
|
195
|
+
validateRelationships,
|
|
196
|
+
value
|
|
197
|
+
});
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
// handle non-localized relationships
|
|
202
|
+
sanitizeRelationship({
|
|
203
|
+
config,
|
|
204
|
+
field,
|
|
205
|
+
locale: undefined,
|
|
206
|
+
operation,
|
|
207
|
+
ref: ref,
|
|
208
|
+
validateRelationships,
|
|
209
|
+
value: ref[field.name]
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
};
|
|
214
|
+
traverseFields({
|
|
215
|
+
callback: sanitize,
|
|
216
|
+
config,
|
|
217
|
+
fields,
|
|
218
|
+
fillEmpty: false,
|
|
219
|
+
parentIsLocalized,
|
|
220
|
+
ref: data
|
|
221
|
+
});
|
|
222
|
+
};
|
|
223
|
+
|
|
224
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/transform.ts"],"sourcesContent":["import type {\n CollectionConfig,\n DateField,\n Field,\n JoinField,\n RelationshipField,\n SanitizedConfig,\n TraverseFieldsCallback,\n UploadField,\n} from 'payload'\n\nimport { Types } from 'mongoose'\nimport { traverseFields } from 'payload'\nimport { fieldAffectsData, fieldShouldBeLocalized } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\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 convertRelationshipValue = ({\n operation,\n relatedCollection,\n validateRelationships,\n value,\n}: {\n operation: Args['operation']\n relatedCollection: CollectionConfig\n validateRelationships?: boolean\n value: unknown\n}) => {\n const customIDField = relatedCollection.fields.find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n\n if (operation === 'read') {\n if (value instanceof Types.ObjectId) {\n return value.toHexString()\n }\n\n return value\n }\n\n if (customIDField) {\n return value\n }\n\n if (typeof value === 'string') {\n try {\n return new Types.ObjectId(value)\n } catch (e) {\n if (validateRelationships) {\n throw e\n }\n return value\n }\n }\n\n return value\n}\n\nconst sanitizeRelationship = ({\n config,\n field,\n locale,\n operation,\n ref,\n validateRelationships,\n value,\n}: {\n config: SanitizedConfig\n field: JoinField | RelationshipField | UploadField\n locale?: string\n operation: Args['operation']\n ref: Record<string, unknown>\n validateRelationships?: boolean\n value?: unknown\n}) => {\n if (field.type === 'join') {\n if (\n operation === 'read' &&\n value &&\n typeof value === 'object' &&\n 'docs' in value &&\n Array.isArray(value.docs)\n ) {\n for (let i = 0; i < value.docs.length; i++) {\n const item = value.docs[i]\n\n if (item instanceof Types.ObjectId) {\n value.docs[i] = item.toHexString()\n } else if (Array.isArray(field.collection) && item) {\n // Fields here for polymorphic joins cannot be determinted, JSON.parse needed\n value.docs[i] = JSON.parse(JSON.stringify(value.docs[i]))\n }\n }\n }\n\n return value\n }\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 - 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: convertRelationshipValue({\n operation,\n relatedCollection: relatedCollectionForSingleValue,\n validateRelationships,\n value: val.value,\n }),\n }\n }\n }\n\n if (relatedCollection) {\n return convertRelationshipValue({\n operation,\n relatedCollection,\n validateRelationships,\n value: val,\n })\n }\n\n return val\n })\n }\n // Handle has one - polymorphic\n else if (isValidRelationObject(value)) {\n relatedCollection = config.collections?.find(({ slug }) => slug === value.relationTo)\n\n if (relatedCollection) {\n result = {\n relationTo: value.relationTo,\n value: convertRelationshipValue({\n operation,\n relatedCollection,\n validateRelationships,\n value: value.value,\n }),\n }\n }\n }\n // Handle has one\n else if (relatedCollection) {\n result = convertRelationshipValue({\n operation,\n relatedCollection,\n validateRelationships,\n value,\n })\n }\n\n if (locale) {\n ref[locale] = result\n } else {\n ref[field.name] = result\n }\n}\n\nconst sanitizeDate = ({\n field,\n locale,\n ref,\n value,\n}: {\n field: DateField\n locale?: string\n ref: Record<string, unknown>\n value: unknown\n}) => {\n if (!value) {\n return\n }\n\n if (value instanceof Date) {\n value = value.toISOString()\n }\n\n if (locale) {\n ref[locale] = value\n } else {\n ref[field.name] = value\n }\n}\n\ntype Args = {\n /** instance of the adapter */\n adapter: MongooseAdapter\n /** data to transform, can be an array of documents or a single document */\n data: Record<string, unknown> | Record<string, unknown>[]\n /** fields accossiated with the data */\n fields: Field[]\n /** slug of the global, pass only when the operation is `write` */\n globalSlug?: string\n /**\n * Type of the operation\n * read - sanitizes ObjectIDs, Date to strings.\n * write - sanitizes string relationships to ObjectIDs.\n */\n operation: 'read' | 'write'\n parentIsLocalized?: boolean\n /**\n * Throw errors on invalid relationships\n * @default true\n */\n validateRelationships?: boolean\n}\n\nexport const transform = ({\n adapter,\n data,\n fields,\n globalSlug,\n operation,\n parentIsLocalized,\n validateRelationships = true,\n}: Args) => {\n if (Array.isArray(data)) {\n for (let i = 0; i < data.length; i++) {\n transform({ adapter, data: data[i], fields, globalSlug, operation, validateRelationships })\n }\n return\n }\n\n const {\n payload: { config },\n } = adapter\n\n if (operation === 'read') {\n delete data['__v']\n data.id = data._id\n delete data['_id']\n\n if (data.id instanceof Types.ObjectId) {\n data.id = data.id.toHexString()\n }\n }\n\n if (operation === 'write' && globalSlug) {\n data.globalType = globalSlug\n }\n\n const sanitize: TraverseFieldsCallback = ({ field, ref }) => {\n if (!ref || typeof ref !== 'object') {\n return\n }\n\n if (field.type === 'date' && operation === 'read' && ref[field.name]) {\n if (config.localization && fieldShouldBeLocalized({ field, parentIsLocalized })) {\n const fieldRef = ref[field.name]\n if (!fieldRef || typeof fieldRef !== 'object') {\n return\n }\n\n for (const locale of config.localization.localeCodes) {\n sanitizeDate({\n field,\n ref: fieldRef,\n value: fieldRef[locale],\n })\n }\n } else {\n sanitizeDate({\n field,\n ref: ref as Record<string, unknown>,\n value: ref[field.name],\n })\n }\n }\n\n if (\n field.type === 'relationship' ||\n field.type === 'upload' ||\n (operation === 'read' && field.type === 'join')\n ) {\n if (!ref[field.name]) {\n return\n }\n\n // handle localized relationships\n if (config.localization && fieldShouldBeLocalized({ field, parentIsLocalized })) {\n const locales = config.localization.locales\n const fieldRef = ref[field.name]\n if (typeof fieldRef !== 'object') {\n return\n }\n\n for (const { code } of locales) {\n const value = ref[field.name][code]\n if (value) {\n sanitizeRelationship({\n config,\n field,\n locale: code,\n operation,\n ref: fieldRef,\n validateRelationships,\n value,\n })\n }\n }\n } else {\n // handle non-localized relationships\n sanitizeRelationship({\n config,\n field,\n locale: undefined,\n operation,\n ref: ref as Record<string, unknown>,\n validateRelationships,\n value: ref[field.name],\n })\n }\n }\n }\n\n traverseFields({\n callback: sanitize,\n config,\n fields,\n fillEmpty: false,\n parentIsLocalized,\n ref: data,\n })\n}\n"],"names":["Types","traverseFields","fieldAffectsData","fieldShouldBeLocalized","isValidRelationObject","value","convertRelationshipValue","operation","relatedCollection","validateRelationships","customIDField","fields","find","field","name","ObjectId","toHexString","e","sanitizeRelationship","config","locale","ref","type","Array","isArray","docs","i","length","item","collection","JSON","parse","stringify","result","hasManyRelations","relationTo","collections","slug","map","val","relatedCollectionForSingleValue","sanitizeDate","Date","toISOString","transform","adapter","data","globalSlug","parentIsLocalized","payload","id","_id","globalType","sanitize","localization","fieldRef","localeCodes","locales","code","undefined","callback","fillEmpty"],"mappings":"AAWA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,cAAc,QAAQ,UAAS;AACxC,SAASC,gBAAgB,EAAEC,sBAAsB,QAAQ,iBAAgB;AASzE,SAASC,sBAAsBC,KAAc;IAC3C,OAAO,OAAOA,UAAU,YAAYA,UAAU,QAAQ,gBAAgBA,SAAS,WAAWA;AAC5F;AAEA,MAAMC,2BAA2B,CAAC,EAChCC,SAAS,EACTC,iBAAiB,EACjBC,qBAAqB,EACrBJ,KAAK,EAMN;IACC,MAAMK,gBAAgBF,kBAAkBG,MAAM,CAACC,IAAI,CACjD,CAACC,QAAUX,iBAAiBW,UAAUA,MAAMC,IAAI,KAAK;IAGvD,IAAIP,cAAc,QAAQ;QACxB,IAAIF,iBAAiBL,MAAMe,QAAQ,EAAE;YACnC,OAAOV,MAAMW,WAAW;QAC1B;QAEA,OAAOX;IACT;IAEA,IAAIK,eAAe;QACjB,OAAOL;IACT;IAEA,IAAI,OAAOA,UAAU,UA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5B;wBACAQ,KAAKkC;wBACLlD,OAAOkD,QAAQ,CAACnC,OAAO;oBACzB;gBACF;YACF,OAAO;gBACLqB,aAAa;oBACX5B;oBACAQ,KAAKA;oBACLhB,OAAOgB,GAAG,CAACR,MAAMC,IAAI,CAAC;gBACxB;YACF;QACF;QAEA,IACED,MAAMS,IAAI,KAAK,kBACfT,MAAMS,IAAI,KAAK,YACdf,cAAc,UAAUM,MAAMS,IAAI,KAAK,QACxC;YACA,IAAI,CAACD,GAAG,CAACR,MAAMC,IAAI,CAAC,EAAE;gBACpB;YACF;YAEA,iCAAiC;YACjC,IAAIK,OAAOmC,YAAY,IAAInD,uBAAuB;gBAAEU;gBAAOmC;YAAkB,IAAI;gBAC/E,MAAMS,UAAUtC,OAAOmC,YAAY,CAACG,OAAO;gBAC3C,MAAMF,WAAWlC,GAAG,CAACR,MAAMC,IAAI,CAAC;gBAChC,IAAI,OAAOyC,aAAa,UAAU;oBAChC;gBACF;gBAEA,KAAK,MAAM,EAAEG,IAAI,EAAE,IAAID,QAAS;oBAC9B,MAAMpD,QAAQgB,GAAG,CAACR,MAAMC,IAAI,CAAC,CAAC4C,KAAK;oBACnC,IAAIrD,OAAO;wBACTa,qBAAqB;4BACnBC;4BACAN;4BACAO,QAAQsC;4BACRnD;4BACAc,KAAKkC;4BACL9C;4BACAJ;wBACF;oBACF;gBACF;YACF,OAAO;gBACL,qCAAqC;gBACrCa,qBAAqB;oBACnBC;oBACAN;oBACAO,QAAQuC;oBACRpD;oBACAc,KAAKA;oBACLZ;oBACAJ,OAAOgB,GAAG,CAACR,MAAMC,IAAI,CAAC;gBACxB;YACF;QACF;IACF;IAEAb,eAAe;QACb2D,UAAUP;QACVlC;QACAR;QACAkD,WAAW;QACXb;QACA3B,KAAKyB;IACP;AACF,EAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { flattenAllFields } from 'payload';
|
|
2
2
|
import { Types } from 'mongoose';
|
|
3
|
-
import {
|
|
3
|
+
import { transform } from './transform.js';
|
|
4
4
|
const flattenRelationshipValues = (obj, prefix = '')=>{
|
|
5
5
|
return Object.keys(obj).reduce((acc, key)=>{
|
|
6
6
|
const fullKey = prefix ? `${prefix}.${key}` : key;
|
|
@@ -321,7 +321,7 @@ const relsData = {
|
|
|
321
321
|
]
|
|
322
322
|
}
|
|
323
323
|
};
|
|
324
|
-
describe('
|
|
324
|
+
describe('transform', ()=>{
|
|
325
325
|
it('should sanitize relationships', ()=>{
|
|
326
326
|
const data = {
|
|
327
327
|
...relsData,
|
|
@@ -453,8 +453,14 @@ describe('sanitizeRelationshipIDs', ()=>{
|
|
|
453
453
|
}
|
|
454
454
|
};
|
|
455
455
|
const flattenValuesBefore = Object.values(flattenRelationshipValues(data));
|
|
456
|
-
|
|
457
|
-
|
|
456
|
+
const mockAdapter = {
|
|
457
|
+
payload: {
|
|
458
|
+
config
|
|
459
|
+
}
|
|
460
|
+
};
|
|
461
|
+
transform({
|
|
462
|
+
adapter: mockAdapter,
|
|
463
|
+
operation: 'write',
|
|
458
464
|
data,
|
|
459
465
|
fields: config.collections[0].fields
|
|
460
466
|
});
|
|
@@ -466,4 +472,4 @@ describe('sanitizeRelationshipIDs', ()=>{
|
|
|
466
472
|
});
|
|
467
473
|
});
|
|
468
474
|
|
|
469
|
-
//# sourceMappingURL=
|
|
475
|
+
//# sourceMappingURL=transform.spec.js.map
|