@payloadcms/db-mongodb 3.0.0-canary.98e0119 → 3.0.0-canary.99cbde3
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/index.d.ts +10 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/init.js +4 -4
- package/dist/init.js.map +1 -1
- package/dist/models/buildCollectionSchema.d.ts +2 -2
- package/dist/models/buildCollectionSchema.d.ts.map +1 -1
- package/dist/models/buildCollectionSchema.js +4 -4
- package/dist/models/buildCollectionSchema.js.map +1 -1
- package/dist/models/buildGlobalModel.d.ts +2 -2
- package/dist/models/buildGlobalModel.d.ts.map +1 -1
- package/dist/models/buildGlobalModel.js +4 -4
- package/dist/models/buildGlobalModel.js.map +1 -1
- package/dist/models/buildSchema.d.ts +2 -2
- package/dist/models/buildSchema.d.ts.map +1 -1
- package/dist/models/buildSchema.js +82 -61
- 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/predefinedMigrations/relationships-v2-v3.js +1 -1
- package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -1
- package/dist/predefinedMigrations/versions-v1-v2.js +1 -1
- package/dist/predefinedMigrations/versions-v1-v2.js.map +1 -1
- package/dist/queries/buildSearchParams.js +1 -0
- package/dist/queries/buildSearchParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +3 -2
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +145 -36
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +2 -1
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +1 -1
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +2 -1
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +2 -1
- package/dist/updateVersion.js.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.d.ts.map +1 -1
- package/dist/utilities/buildProjectionFromSelect.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.js +1 -0
- package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.spec.js +408 -0
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -0
- package/package.json +3 -4
- package/dist/queries/mock.js +0 -2
- package/dist/queries/mock.js.map +0 -1
package/dist/updateVersion.js
CHANGED
@@ -2,7 +2,7 @@ import { buildVersionCollectionFields } from 'payload';
|
|
2
2
|
import { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js';
|
3
3
|
import { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js';
|
4
4
|
import { withSession } from './withSession.js';
|
5
|
-
export const updateVersion = async function updateVersion({ id, collection, locale, req = {}, select, versionData, where }) {
|
5
|
+
export const updateVersion = async function updateVersion({ id, collection, locale, options: optionsArgs = {}, req = {}, select, versionData, where }) {
|
6
6
|
const VersionModel = this.versions[collection];
|
7
7
|
const whereToUse = where || {
|
8
8
|
id: {
|
@@ -11,6 +11,7 @@ export const updateVersion = async function updateVersion({ id, collection, loca
|
|
11
11
|
};
|
12
12
|
const fields = buildVersionCollectionFields(this.payload.config, this.payload.collections[collection].config);
|
13
13
|
const options = {
|
14
|
+
...optionsArgs,
|
14
15
|
...await withSession(this, req),
|
15
16
|
lean: true,
|
16
17
|
new: true,
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import { buildVersionCollectionFields, type PayloadRequest, type UpdateVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'\nimport { withSession } from './withSession.js'\n\nexport const updateVersion: UpdateVersion = async function updateVersion(\n this: MongooseAdapter,\n {
|
1
|
+
{"version":3,"sources":["../src/updateVersion.ts"],"sourcesContent":["import type { QueryOptions } from 'mongoose'\n\nimport { buildVersionCollectionFields, type PayloadRequest, type UpdateVersion } from 'payload'\n\nimport type { MongooseAdapter } from './index.js'\n\nimport { buildProjectionFromSelect } from './utilities/buildProjectionFromSelect.js'\nimport { sanitizeRelationshipIDs } from './utilities/sanitizeRelationshipIDs.js'\nimport { withSession } from './withSession.js'\n\nexport const updateVersion: UpdateVersion = async function updateVersion(\n this: MongooseAdapter,\n {\n id,\n collection,\n locale,\n options: optionsArgs = {},\n req = {} as PayloadRequest,\n select,\n versionData,\n where,\n },\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 options: QueryOptions = {\n ...optionsArgs,\n ...(await withSession(this, req)),\n lean: true,\n new: true,\n projection: buildProjectionFromSelect({ adapter: this, fields, select }),\n }\n\n const query = await VersionModel.buildQuery({\n locale,\n payload: this.payload,\n where: whereToUse,\n })\n\n const sanitizedData = sanitizeRelationshipIDs({\n config: this.payload.config,\n data: versionData,\n fields,\n })\n\n const doc = await VersionModel.findOneAndUpdate(query, sanitizedData, options)\n\n const result = JSON.parse(JSON.stringify(doc))\n\n const verificationToken = doc._verificationToken\n\n // custom id type reset\n result.id = result._id\n if (verificationToken) {\n result._verificationToken = verificationToken\n }\n return result\n}\n"],"names":["buildVersionCollectionFields","buildProjectionFromSelect","sanitizeRelationshipIDs","withSession","updateVersion","id","collection","locale","options","optionsArgs","req","select","versionData","where","VersionModel","versions","whereToUse","equals","fields","payload","config","collections","lean","new","projection","adapter","query","buildQuery","sanitizedData","data","doc","findOneAndUpdate","result","JSON","parse","stringify","verificationToken","_verificationToken","_id"],"mappings":"AAEA,SA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AAOJ;AACT,EAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"buildProjectionFromSelect.d.ts","sourceRoot":"","sources":["../../src/utilities/buildProjectionFromSelect.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,KAAK,EAGV,KAAK,UAAU,EAEhB,MAAM,SAAS,CAAA;AAGhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;
|
1
|
+
{"version":3,"file":"buildProjectionFromSelect.d.ts","sourceRoot":"","sources":["../../src/utilities/buildProjectionFromSelect.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,KAAK,EAGV,KAAK,UAAU,EAEhB,MAAM,SAAS,CAAA;AAGhB,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AA4MlD,eAAO,MAAM,yBAAyB,iCAInC;IACD,OAAO,EAAE,eAAe,CAAA;IACxB,MAAM,EAAE,KAAK,EAAE,CAAA;IACf,MAAM,CAAC,EAAE,UAAU,CAAA;CACpB,KAAG,MAAM,CAAC,MAAM,EAAE,IAAI,CAAC,GAAG,SAmB1B,CAAA"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/buildProjectionFromSelect.ts"],"sourcesContent":["import {\n deepCopyObjectSimple,\n type Field,\n type FieldAffectingData,\n type SelectMode,\n type SelectType,\n type TabAsField,\n} from 'payload'\nimport { fieldAffectsData, getSelectMode, tabHasName } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\n\nconst addFieldToProjection = ({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath: string\n field: FieldAffectingData\n projection: Record<string, true>\n withinLocalizedField: boolean\n}) => {\n const { config } = adapter.payload\n\n if (withinLocalizedField && config.localization) {\n for (const locale of config.localization.localeCodes) {\n const localeDatabaseSchemaPath = databaseSchemaPath.replace('<locale>', locale)\n projection[`${localeDatabaseSchemaPath}${field.name}`] = true\n }\n } else {\n projection[`${databaseSchemaPath}${field.name}`] = true\n }\n}\n\nconst traverseFields = ({\n adapter,\n databaseSchemaPath = '',\n fields,\n projection,\n select,\n selectAllOnCurrentLevel = false,\n selectMode,\n withinLocalizedField = false,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath?: string\n fields: (Field | TabAsField)[]\n projection: Record<string, true>\n select: SelectType\n selectAllOnCurrentLevel?: boolean\n selectMode: SelectMode\n withinLocalizedField?: boolean\n}) => {\n for (const field of fields) {\n if (fieldAffectsData(field)) {\n if (selectMode === 'include') {\n if (select[field.name] === true || selectAllOnCurrentLevel) {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (!select[field.name]) {\n continue\n }\n }\n\n if (selectMode === 'exclude') {\n if (typeof select[field.name] === 'undefined') {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (select[field.name] === false) {\n continue\n }\n }\n }\n\n let fieldDatabaseSchemaPath = databaseSchemaPath\n let fieldWithinLocalizedField = withinLocalizedField\n\n if (fieldAffectsData(field)) {\n fieldDatabaseSchemaPath = `${databaseSchemaPath}${field.name}.`\n\n if (field.localized) {\n fieldDatabaseSchemaPath = `${fieldDatabaseSchemaPath}<locale>.`\n fieldWithinLocalizedField = true\n }\n }\n\n switch (field.type) {\n case 'array':\n case 'group':\n\n case 'tab': {\n let fieldSelect: SelectType\n\n if (field.type === 'tab' && !tabHasName(field)) {\n fieldSelect = select\n } else {\n fieldSelect = select[field.name] as SelectType\n }\n\n if (field.type === 'array' && selectMode === 'include') {\n fieldSelect['id'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: field.fields,\n projection,\n select: fieldSelect,\n selectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n\n break\n }\n\n case 'blocks': {\n const blocksSelect = select[field.name] as SelectType\n\n for (const block of field.blocks) {\n if (\n (selectMode === 'include' && blocksSelect[block.slug] === true) ||\n (selectMode === 'exclude' && typeof blocksSelect[block.slug] === 'undefined')\n ) {\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: {},\n selectAllOnCurrentLevel: true,\n selectMode: 'include',\n withinLocalizedField: fieldWithinLocalizedField,\n })\n continue\n }\n\n let blockSelectMode = selectMode\n\n if (selectMode === 'exclude' && blocksSelect[block.slug] === false) {\n blockSelectMode = 'include'\n }\n\n if (typeof blocksSelect[block.slug] !== 'object') {\n blocksSelect[block.slug] = {}\n }\n\n if (blockSelectMode === 'include') {\n blocksSelect[block.slug]['id'] = true\n blocksSelect[block.slug]['blockType'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: blocksSelect[block.slug] as SelectType,\n selectMode: blockSelectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n }\n\n break\n }\n case 'collapsible':\n case 'row':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.fields,\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n case 'tabs':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n default:\n break\n }\n }\n}\n\nexport const buildProjectionFromSelect = ({\n adapter,\n fields,\n select,\n}: {\n adapter: MongooseAdapter\n fields: Field[]\n select?: SelectType\n}): Record<string, true> | undefined => {\n if (!select) {\n return\n }\n\n const projection: Record<string, true> = {\n _id: true,\n }\n\n traverseFields({\n adapter,\n fields,\n projection,\n // Clone to safely mutate it later\n select: deepCopyObjectSimple(select),\n selectMode: getSelectMode(select),\n })\n\n return projection\n}\n"],"names":["deepCopyObjectSimple","fieldAffectsData","getSelectMode","tabHasName","addFieldToProjection","adapter","databaseSchemaPath","field","projection","withinLocalizedField","config","payload","localization","locale","localeCodes","localeDatabaseSchemaPath","replace","name","traverseFields","fields","select","selectAllOnCurrentLevel","selectMode","fieldDatabaseSchemaPath","fieldWithinLocalizedField","localized","type","fieldSelect","blocksSelect","block","blocks","slug","blockSelectMode","tabs","map","tab","buildProjectionFromSelect","_id"],"mappings":"AAAA,SACEA,oBAAoB,QAMf,UAAS;AAChB,SAASC,gBAAgB,EAAEC,aAAa,EAAEC,UAAU,QAAQ,iBAAgB;AAI5E,MAAMC,uBAAuB,CAAC,EAC5BC,OAAO,EACPC,kBAAkB,EAClBC,KAAK,EACLC,UAAU,EACVC,oBAAoB,EAOrB;IACC,MAAM,EAAEC,MAAM,EAAE,GAAGL,QAAQM,OAAO;IAElC,IAAIF,wBAAwBC,OAAOE,YAAY,EAAE;QAC/C,KAAK,MAAMC,UAAUH,OAAOE,YAAY,CAACE,WAAW,CAAE;YACpD,MAAMC,2BAA2BT,mBAAmBU,OAAO,CAAC,YAAYH;YACxEL,UAAU,CAAC,CAAC,EAAEO,yBAAyB,EAAER,MAAMU,IAAI,CAAC,CAAC,CAAC,GAAG;QAC3D;IACF,OAAO;QACLT,UAAU,CAAC,CAAC,EAAEF,mBAAmB,EAAEC,MAAMU,IAAI,CAAC,CAAC,CAAC,GAAG;IACrD;AACF;AAEA,MAAMC,iBAAiB,CAAC,EACtBb,OAAO,EACPC,qBAAqB,EAAE,EACvBa,MAAM,EACNX,UAAU,EACVY,MAAM,EACNC,0BAA0B,KAAK,EAC/BC,UAAU,EACVb,uBAAuB,KAAK,EAU7B;IACC,KAAK,MAAMF,SAASY,OAAQ;QAC1B,IAAIlB,iBAAiBM,QAAQ;YAC3B,IAAIe,eAAe,WAAW;gBAC5B,IAAIF,MAAM,CAACb,MAAMU,IAAI,CAAC,KAAK,QAAQI,yBAAyB;oBAC1DjB,qBAAqB;wBACnBC;wBACAC;wBACAC;wBACAC;wBACAC;oBACF;oBACA;gBACF;gBAEA,IAAI,CAACW,MAAM,CA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AAI,CAACA,QAAQ;QACX;IACF;IAEA,MAAMZ,aAAmC;QACvC6B,KAAK;IACP;IAEAnB,eAAe;QACbb;QACAc;QACAX;QACA,kCAAkC;QAClCY,QAAQpB,qBAAqBoB;QAC7BE,YAAYpB,cAAckB;IAC5B;IAEA,OAAOZ;AACT,EAAC"}
|
1
|
+
{"version":3,"sources":["../../src/utilities/buildProjectionFromSelect.ts"],"sourcesContent":["import {\n deepCopyObjectSimple,\n type Field,\n type FieldAffectingData,\n type SelectMode,\n type SelectType,\n type TabAsField,\n} from 'payload'\nimport { fieldAffectsData, getSelectMode, tabHasName } from 'payload/shared'\n\nimport type { MongooseAdapter } from '../index.js'\n\nconst addFieldToProjection = ({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath: string\n field: FieldAffectingData\n projection: Record<string, true>\n withinLocalizedField: boolean\n}) => {\n const { config } = adapter.payload\n\n if (withinLocalizedField && config.localization) {\n for (const locale of config.localization.localeCodes) {\n const localeDatabaseSchemaPath = databaseSchemaPath.replace('<locale>', locale)\n projection[`${localeDatabaseSchemaPath}${field.name}`] = true\n }\n } else {\n projection[`${databaseSchemaPath}${field.name}`] = true\n }\n}\n\nconst traverseFields = ({\n adapter,\n databaseSchemaPath = '',\n fields,\n projection,\n select,\n selectAllOnCurrentLevel = false,\n selectMode,\n withinLocalizedField = false,\n}: {\n adapter: MongooseAdapter\n databaseSchemaPath?: string\n fields: (Field | TabAsField)[]\n projection: Record<string, true>\n select: SelectType\n selectAllOnCurrentLevel?: boolean\n selectMode: SelectMode\n withinLocalizedField?: boolean\n}) => {\n for (const field of fields) {\n if (fieldAffectsData(field)) {\n if (selectMode === 'include') {\n if (select[field.name] === true || selectAllOnCurrentLevel) {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (!select[field.name]) {\n continue\n }\n }\n\n if (selectMode === 'exclude') {\n if (typeof select[field.name] === 'undefined') {\n addFieldToProjection({\n adapter,\n databaseSchemaPath,\n field,\n projection,\n withinLocalizedField,\n })\n continue\n }\n\n if (select[field.name] === false) {\n continue\n }\n }\n }\n\n let fieldDatabaseSchemaPath = databaseSchemaPath\n let fieldWithinLocalizedField = withinLocalizedField\n\n if (fieldAffectsData(field)) {\n fieldDatabaseSchemaPath = `${databaseSchemaPath}${field.name}.`\n\n if (field.localized) {\n fieldDatabaseSchemaPath = `${fieldDatabaseSchemaPath}<locale>.`\n fieldWithinLocalizedField = true\n }\n }\n\n switch (field.type) {\n case 'array':\n case 'group':\n case 'tab': {\n let fieldSelect: SelectType\n\n if (field.type === 'tab' && !tabHasName(field)) {\n fieldSelect = select\n } else {\n fieldSelect = select[field.name] as SelectType\n }\n\n if (field.type === 'array' && selectMode === 'include') {\n fieldSelect['id'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: field.fields,\n projection,\n select: fieldSelect,\n selectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n\n break\n }\n\n case 'blocks': {\n const blocksSelect = select[field.name] as SelectType\n\n for (const block of field.blocks) {\n if (\n (selectMode === 'include' && blocksSelect[block.slug] === true) ||\n (selectMode === 'exclude' && typeof blocksSelect[block.slug] === 'undefined')\n ) {\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: {},\n selectAllOnCurrentLevel: true,\n selectMode: 'include',\n withinLocalizedField: fieldWithinLocalizedField,\n })\n continue\n }\n\n let blockSelectMode = selectMode\n\n if (selectMode === 'exclude' && blocksSelect[block.slug] === false) {\n blockSelectMode = 'include'\n }\n\n if (typeof blocksSelect[block.slug] !== 'object') {\n blocksSelect[block.slug] = {}\n }\n\n if (blockSelectMode === 'include') {\n blocksSelect[block.slug]['id'] = true\n blocksSelect[block.slug]['blockType'] = true\n }\n\n traverseFields({\n adapter,\n databaseSchemaPath: fieldDatabaseSchemaPath,\n fields: block.fields,\n projection,\n select: blocksSelect[block.slug] as SelectType,\n selectMode: blockSelectMode,\n withinLocalizedField: fieldWithinLocalizedField,\n })\n }\n\n break\n }\n case 'collapsible':\n case 'row':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.fields,\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n case 'tabs':\n traverseFields({\n adapter,\n databaseSchemaPath,\n fields: field.tabs.map((tab) => ({ ...tab, type: 'tab' })),\n projection,\n select,\n selectMode,\n withinLocalizedField,\n })\n break\n\n default:\n break\n }\n }\n}\n\nexport const buildProjectionFromSelect = ({\n adapter,\n fields,\n select,\n}: {\n adapter: MongooseAdapter\n fields: Field[]\n select?: SelectType\n}): Record<string, true> | undefined => {\n if (!select) {\n return\n }\n\n const projection: Record<string, true> = {\n _id: true,\n }\n\n traverseFields({\n adapter,\n fields,\n projection,\n // Clone to safely mutate it later\n select: deepCopyObjectSimple(select),\n selectMode: getSelectMode(select),\n })\n\n return projection\n}\n"],"names":["deepCopyObjectSimple","fieldAffectsData","getSelectMode","tabHasName","addFieldToProjection","adapter","databaseSchemaPath","field","projection","withinLocalizedField","config","payload","localization","locale","localeCodes","localeDatabaseSchemaPath","replace","name","traverseFields","fields","select","selectAllOnCurrentLevel","selectMode","fieldDatabaseSchemaPath","fieldWithinLocalizedField","localized","type","fieldSelect","blocksSelect","block","blocks","slug","blockSelectMode","tabs","map","tab","buildProjectionFromSelect","_id"],"mappings":"AAAA,SACEA,oBAAoB,QAMf,UAAS;AAChB,SAASC,gBAAgB,EAAEC,aAAa,EAAEC,UAAU,QAAQ,iBAAgB;AAI5E,MAAMC,uBAAuB,CAAC,EAC5BC,OAAO,EACPC,kBAAkB,EAClBC,KAAK,EACLC,UAAU,EACVC,oBAAoB,EAOrB;IACC,MAAM,EAAEC,MAAM,EAAE,GAAGL,QAAQM,OAAO;IAElC,IAAIF,wBAAwBC,OAAOE,YAAY,EAAE;QAC/C,KAAK,MAAMC,UAAU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,CAAC,CAAC,KAAK,GAAG;4BACjCH,YAAY,CAACC,MAAME,IAAI,CAAC,CAAC,YAAY,GAAG;wBAC1C;wBAEAb,eAAe;4BACbb;4BACAC,oBAAoBiB;4BACpBJ,QAAQU,MAAMV,MAAM;4BACpBX;4BACAY,QAAQQ,YAAY,CAACC,MAAME,IAAI,CAAC;4BAChCT,YAAYU;4BACZvB,sBAAsBe;wBACxB;oBACF;oBAEA;gBACF;YACA,KAAK;YACL,KAAK;gBACHN,eAAe;oBACbb;oBACAC;oBACAa,QAAQZ,MAAMY,MAAM;oBACpBX;oBACAY;oBACAE;oBACAb;gBACF;gBACA;YAEF,KAAK;gBACHS,eAAe;oBACbb;oBACAC;oBACAa,QAAQZ,MAAM0B,IAAI,CAACC,GAAG,CAAC,CAACC,MAAS,CAAA;4BAAE,GAAGA,GAAG;4BAAET,MAAM;wBAAM,CAAA;oBACvDlB;oBACAY;oBACAE;oBACAb;gBACF;gBACA;YAEF;gBACE;QACJ;IACF;AACF;AAEA,OAAO,MAAM2B,4BAA4B,CAAC,EACxC/B,OAAO,EACPc,MAAM,EACNC,MAAM,EAKP;IACC,IAAI,CAACA,QAAQ;QACX;IACF;IAEA,MAAMZ,aAAmC;QACvC6B,KAAK;IACP;IAEAnB,eAAe;QACbb;QACAc;QACAX;QACA,kCAAkC;QAClCY,QAAQpB,qBAAqBoB;QAC7BE,YAAYpB,cAAckB;IAC5B;IAEA,OAAOZ;AACT,EAAC"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"sourcesContent":["import type { CollectionConfig, Field, SanitizedConfig, TraverseFieldsCallback } from 'payload'\n\nimport { Types } from 'mongoose'\nimport { APIError, 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}): number | string | Types.ObjectId => {\n const customIDField = relatedCollection.fields.find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n\n if (!customIDField) {\n try {\n return new Types.ObjectId(value)\n } catch (error) {\n throw new APIError(\n `Failed to create ObjectId from value: ${value}. Error: ${error.message}`,\n 400,\n )\n }\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":["Types","APIError","traverseFields","fieldAffectsData","isValidRelationObject","value","convertValue","relatedCollection","customIDField","fields","find","field","name","ObjectId","error","message","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,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,QAAQ,EAAEC,cAAc,QAAQ,UAAS;AAClD,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,IAAI;YACF,OAAO,IAAIR,MAAMa,QAAQ,CAACR;QAC5B,EAAE,OAAOS,OAAO;YACd,MAAM,IAAIb,SACR,CAAC,sCAAsC,EAAEI,MAAM,SAAS,EAAES,MAAMC,OAAO,CAAC,CAAC,EACzE;QAEJ;IACF;IAEA,OAAOV;AACT;AAEA,MAAMW,uBAAuB,CAAC,EAAEC,MAAM,EAAEN,KA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Fe,SAASd,aAAa;YACpBC;YACAF;QACF;IACF;IACA,IAAIa,QAAQ;QACVC,GAAG,CAACD,OAAO,GAAGE;IAChB,OAAO;QACLD,GAAG,CAACR,MAAMC,IAAI,CAAC,GAAGQ;IACpB;AACF;AAEA,OAAO,MAAMU,0BAA0B,CAAC,EACtCb,MAAM,EACNc,IAAI,EACJtB,MAAM,EACD;IACL,MAAMuB,WAAmC,CAAC,EAAErB,KAAK,EAAEQ,GAAG,EAAE;QACtD,IAAIR,MAAMsB,IAAI,KAAK,kBAAkBtB,MAAMsB,IAAI,KAAK,UAAU;YAC5D,iCAAiC;YACjC,IAAIhB,OAAOiB,YAAY,IAAIvB,MAAMwB,SAAS,EAAE;gBAC1C,MAAMC,UAAUnB,OAAOiB,YAAY,CAACE,OAAO;gBAC3C,MAAMC,WAAWlB,GAAG,CAACR,MAAMC,IAAI,CAAC;gBAChC,KAAK,MAAM,EAAE0B,IAAI,EAAE,IAAIF,QAAS;oBAC9B,IAAIjB,GAAG,CAACR,MAAMC,IAAI,CAAC,EAAE,CAAC0B,KAAK,EAAE;wBAC3B,MAAMjC,QAAQc,GAAG,CAACR,MAAMC,IAAI,CAAC,CAAC0B,KAAK;wBACnCtB,qBAAqB;4BAAEC;4BAAQN;4BAAOO,QAAQoB;4BAAMnB,KAAKkB;4BAAUhC;wBAAM;oBAC3E;gBACF;YACF,OAAO;gBACL,qCAAqC;gBACrCW,qBAAqB;oBACnBC;oBACAN;oBACAO,QAAQqB;oBACRpB;oBACAd,OAAOc,GAAG,CAACR,MAAMC,IAAI,CAAC;gBACxB;YACF;QACF;IACF;IAEAV,eAAe;QAAEsC,UAAUR;QAAUvB;
|
1
|
+
{"version":3,"sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"sourcesContent":["import type { CollectionConfig, Field, SanitizedConfig, TraverseFieldsCallback } from 'payload'\n\nimport { Types } from 'mongoose'\nimport { APIError, 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}): number | string | Types.ObjectId => {\n const customIDField = relatedCollection.fields.find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n\n if (!customIDField) {\n try {\n return new Types.ObjectId(value)\n } catch (error) {\n throw new APIError(\n `Failed to create ObjectId from value: ${value}. Error: ${error.message}`,\n 400,\n )\n }\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, fillEmpty: false, ref: data })\n\n return data\n}\n"],"names":["Types","APIError","traverseFields","fieldAffectsData","isValidRelationObject","value","convertValue","relatedCollection","customIDField","fields","find","field","name","ObjectId","error","message","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","fillEmpty"],"mappings":"AAEA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAASC,QAAQ,EAAEC,cAAc,QAAQ,UAAS;AAClD,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,IAAI;YACF,OAAO,IAAIR,MAAMa,QAAQ,CAACR;QAC5B,EAAE,OAAOS,OAAO;YACd,MAAM,IAAIb,SACR,CAAC,sCAAsC,EAAEI,M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,0 +1,408 @@
|
|
1
|
+
import { Types } from 'mongoose';
|
2
|
+
import { sanitizeRelationshipIDs } from './sanitizeRelationshipIDs.js';
|
3
|
+
const flattenRelationshipValues = (obj, prefix = '')=>{
|
4
|
+
return Object.keys(obj).reduce((acc, key)=>{
|
5
|
+
const fullKey = prefix ? `${prefix}.${key}` : key;
|
6
|
+
const value = obj[key];
|
7
|
+
if (value && typeof value === 'object' && !(value instanceof Types.ObjectId)) {
|
8
|
+
Object.assign(acc, flattenRelationshipValues(value, fullKey));
|
9
|
+
// skip relationTo and blockType
|
10
|
+
} else if (!fullKey.endsWith('relationTo') && !fullKey.endsWith('blockType')) {
|
11
|
+
acc[fullKey] = value;
|
12
|
+
}
|
13
|
+
return acc;
|
14
|
+
}, {});
|
15
|
+
};
|
16
|
+
const relsFields = [
|
17
|
+
{
|
18
|
+
name: 'rel_1',
|
19
|
+
type: 'relationship',
|
20
|
+
relationTo: 'rels'
|
21
|
+
},
|
22
|
+
{
|
23
|
+
name: 'rel_1_l',
|
24
|
+
type: 'relationship',
|
25
|
+
localized: true,
|
26
|
+
relationTo: 'rels'
|
27
|
+
},
|
28
|
+
{
|
29
|
+
name: 'rel_2',
|
30
|
+
type: 'relationship',
|
31
|
+
hasMany: true,
|
32
|
+
relationTo: 'rels'
|
33
|
+
},
|
34
|
+
{
|
35
|
+
name: 'rel_2_l',
|
36
|
+
type: 'relationship',
|
37
|
+
hasMany: true,
|
38
|
+
localized: true,
|
39
|
+
relationTo: 'rels'
|
40
|
+
},
|
41
|
+
{
|
42
|
+
name: 'rel_3',
|
43
|
+
type: 'relationship',
|
44
|
+
relationTo: [
|
45
|
+
'rels'
|
46
|
+
]
|
47
|
+
},
|
48
|
+
{
|
49
|
+
name: 'rel_3_l',
|
50
|
+
type: 'relationship',
|
51
|
+
localized: true,
|
52
|
+
relationTo: [
|
53
|
+
'rels'
|
54
|
+
]
|
55
|
+
},
|
56
|
+
{
|
57
|
+
name: 'rel_4',
|
58
|
+
type: 'relationship',
|
59
|
+
hasMany: true,
|
60
|
+
relationTo: [
|
61
|
+
'rels'
|
62
|
+
]
|
63
|
+
},
|
64
|
+
{
|
65
|
+
name: 'rel_4_l',
|
66
|
+
type: 'relationship',
|
67
|
+
hasMany: true,
|
68
|
+
localized: true,
|
69
|
+
relationTo: [
|
70
|
+
'rels'
|
71
|
+
]
|
72
|
+
}
|
73
|
+
];
|
74
|
+
const config = {
|
75
|
+
collections: [
|
76
|
+
{
|
77
|
+
slug: 'docs',
|
78
|
+
fields: [
|
79
|
+
...relsFields,
|
80
|
+
{
|
81
|
+
name: 'array',
|
82
|
+
type: 'array',
|
83
|
+
fields: [
|
84
|
+
{
|
85
|
+
name: 'array',
|
86
|
+
type: 'array',
|
87
|
+
fields: relsFields
|
88
|
+
},
|
89
|
+
{
|
90
|
+
name: 'blocks',
|
91
|
+
type: 'blocks',
|
92
|
+
blocks: [
|
93
|
+
{
|
94
|
+
slug: 'block',
|
95
|
+
fields: relsFields
|
96
|
+
}
|
97
|
+
]
|
98
|
+
},
|
99
|
+
...relsFields
|
100
|
+
]
|
101
|
+
},
|
102
|
+
{
|
103
|
+
name: 'arrayLocalized',
|
104
|
+
type: 'array',
|
105
|
+
fields: [
|
106
|
+
{
|
107
|
+
name: 'array',
|
108
|
+
type: 'array',
|
109
|
+
fields: relsFields
|
110
|
+
},
|
111
|
+
{
|
112
|
+
name: 'blocks',
|
113
|
+
type: 'blocks',
|
114
|
+
blocks: [
|
115
|
+
{
|
116
|
+
slug: 'block',
|
117
|
+
fields: relsFields
|
118
|
+
}
|
119
|
+
]
|
120
|
+
},
|
121
|
+
...relsFields
|
122
|
+
],
|
123
|
+
localized: true
|
124
|
+
},
|
125
|
+
{
|
126
|
+
name: 'blocks',
|
127
|
+
type: 'blocks',
|
128
|
+
blocks: [
|
129
|
+
{
|
130
|
+
slug: 'block',
|
131
|
+
fields: [
|
132
|
+
...relsFields,
|
133
|
+
{
|
134
|
+
name: 'group',
|
135
|
+
type: 'group',
|
136
|
+
fields: relsFields
|
137
|
+
},
|
138
|
+
{
|
139
|
+
name: 'array',
|
140
|
+
type: 'array',
|
141
|
+
fields: relsFields
|
142
|
+
}
|
143
|
+
]
|
144
|
+
}
|
145
|
+
]
|
146
|
+
},
|
147
|
+
{
|
148
|
+
name: 'group',
|
149
|
+
type: 'group',
|
150
|
+
fields: [
|
151
|
+
...relsFields,
|
152
|
+
{
|
153
|
+
name: 'array',
|
154
|
+
type: 'array',
|
155
|
+
fields: relsFields
|
156
|
+
}
|
157
|
+
]
|
158
|
+
},
|
159
|
+
{
|
160
|
+
name: 'groupLocalized',
|
161
|
+
type: 'group',
|
162
|
+
fields: [
|
163
|
+
...relsFields,
|
164
|
+
{
|
165
|
+
name: 'array',
|
166
|
+
type: 'array',
|
167
|
+
fields: relsFields
|
168
|
+
}
|
169
|
+
],
|
170
|
+
localized: true
|
171
|
+
},
|
172
|
+
{
|
173
|
+
name: 'groupAndRow',
|
174
|
+
type: 'group',
|
175
|
+
fields: [
|
176
|
+
{
|
177
|
+
type: 'row',
|
178
|
+
fields: [
|
179
|
+
...relsFields,
|
180
|
+
{
|
181
|
+
type: 'array',
|
182
|
+
name: 'array',
|
183
|
+
fields: relsFields
|
184
|
+
}
|
185
|
+
]
|
186
|
+
}
|
187
|
+
]
|
188
|
+
},
|
189
|
+
{
|
190
|
+
type: 'tabs',
|
191
|
+
tabs: [
|
192
|
+
{
|
193
|
+
name: 'tab',
|
194
|
+
fields: relsFields
|
195
|
+
},
|
196
|
+
{
|
197
|
+
name: 'tabLocalized',
|
198
|
+
fields: relsFields,
|
199
|
+
localized: true
|
200
|
+
}
|
201
|
+
]
|
202
|
+
}
|
203
|
+
]
|
204
|
+
},
|
205
|
+
{
|
206
|
+
slug: 'rels',
|
207
|
+
fields: []
|
208
|
+
}
|
209
|
+
],
|
210
|
+
localization: {
|
211
|
+
defaultLocale: 'en',
|
212
|
+
localeCodes: [
|
213
|
+
'en',
|
214
|
+
'es'
|
215
|
+
],
|
216
|
+
locales: [
|
217
|
+
{
|
218
|
+
code: 'en',
|
219
|
+
label: 'EN'
|
220
|
+
},
|
221
|
+
{
|
222
|
+
code: 'es',
|
223
|
+
label: 'ES'
|
224
|
+
}
|
225
|
+
]
|
226
|
+
}
|
227
|
+
};
|
228
|
+
const relsData = {
|
229
|
+
rel_1: new Types.ObjectId().toHexString(),
|
230
|
+
rel_1_l: {
|
231
|
+
en: new Types.ObjectId().toHexString(),
|
232
|
+
es: new Types.ObjectId().toHexString()
|
233
|
+
},
|
234
|
+
rel_2: [
|
235
|
+
new Types.ObjectId().toHexString()
|
236
|
+
],
|
237
|
+
rel_2_l: {
|
238
|
+
en: [
|
239
|
+
new Types.ObjectId().toHexString()
|
240
|
+
],
|
241
|
+
es: [
|
242
|
+
new Types.ObjectId().toHexString()
|
243
|
+
]
|
244
|
+
},
|
245
|
+
rel_3: {
|
246
|
+
relationTo: 'rels',
|
247
|
+
value: new Types.ObjectId().toHexString()
|
248
|
+
},
|
249
|
+
rel_3_l: {
|
250
|
+
en: {
|
251
|
+
relationTo: 'rels',
|
252
|
+
value: new Types.ObjectId().toHexString()
|
253
|
+
},
|
254
|
+
es: {
|
255
|
+
relationTo: 'rels',
|
256
|
+
value: new Types.ObjectId().toHexString()
|
257
|
+
}
|
258
|
+
},
|
259
|
+
rel_4: [
|
260
|
+
{
|
261
|
+
relationTo: 'rels',
|
262
|
+
value: new Types.ObjectId().toHexString()
|
263
|
+
}
|
264
|
+
],
|
265
|
+
rel_4_l: {
|
266
|
+
en: [
|
267
|
+
{
|
268
|
+
relationTo: 'rels',
|
269
|
+
value: new Types.ObjectId().toHexString()
|
270
|
+
}
|
271
|
+
],
|
272
|
+
es: [
|
273
|
+
{
|
274
|
+
relationTo: 'rels',
|
275
|
+
value: new Types.ObjectId().toHexString()
|
276
|
+
}
|
277
|
+
]
|
278
|
+
}
|
279
|
+
};
|
280
|
+
describe('sanitizeRelationshipIDs', ()=>{
|
281
|
+
it('should sanitize relationships', ()=>{
|
282
|
+
const data = {
|
283
|
+
...relsData,
|
284
|
+
array: [
|
285
|
+
{
|
286
|
+
...relsData,
|
287
|
+
array: [
|
288
|
+
{
|
289
|
+
...relsData
|
290
|
+
}
|
291
|
+
],
|
292
|
+
blocks: [
|
293
|
+
{
|
294
|
+
blockType: 'block',
|
295
|
+
...relsData
|
296
|
+
}
|
297
|
+
]
|
298
|
+
}
|
299
|
+
],
|
300
|
+
arrayLocalized: {
|
301
|
+
en: [
|
302
|
+
{
|
303
|
+
...relsData,
|
304
|
+
array: [
|
305
|
+
{
|
306
|
+
...relsData
|
307
|
+
}
|
308
|
+
],
|
309
|
+
blocks: [
|
310
|
+
{
|
311
|
+
blockType: 'block',
|
312
|
+
...relsData
|
313
|
+
}
|
314
|
+
]
|
315
|
+
}
|
316
|
+
],
|
317
|
+
es: [
|
318
|
+
{
|
319
|
+
...relsData,
|
320
|
+
array: [
|
321
|
+
{
|
322
|
+
...relsData
|
323
|
+
}
|
324
|
+
],
|
325
|
+
blocks: [
|
326
|
+
{
|
327
|
+
blockType: 'block',
|
328
|
+
...relsData
|
329
|
+
}
|
330
|
+
]
|
331
|
+
}
|
332
|
+
]
|
333
|
+
},
|
334
|
+
blocks: [
|
335
|
+
{
|
336
|
+
blockType: 'block',
|
337
|
+
...relsData,
|
338
|
+
array: [
|
339
|
+
{
|
340
|
+
...relsData
|
341
|
+
}
|
342
|
+
],
|
343
|
+
group: {
|
344
|
+
...relsData
|
345
|
+
}
|
346
|
+
}
|
347
|
+
],
|
348
|
+
group: {
|
349
|
+
...relsData,
|
350
|
+
array: [
|
351
|
+
{
|
352
|
+
...relsData
|
353
|
+
}
|
354
|
+
]
|
355
|
+
},
|
356
|
+
groupAndRow: {
|
357
|
+
...relsData,
|
358
|
+
array: [
|
359
|
+
{
|
360
|
+
...relsData
|
361
|
+
}
|
362
|
+
]
|
363
|
+
},
|
364
|
+
groupLocalized: {
|
365
|
+
en: {
|
366
|
+
...relsData,
|
367
|
+
array: [
|
368
|
+
{
|
369
|
+
...relsData
|
370
|
+
}
|
371
|
+
]
|
372
|
+
},
|
373
|
+
es: {
|
374
|
+
...relsData,
|
375
|
+
array: [
|
376
|
+
{
|
377
|
+
...relsData
|
378
|
+
}
|
379
|
+
]
|
380
|
+
}
|
381
|
+
},
|
382
|
+
tab: {
|
383
|
+
...relsData
|
384
|
+
},
|
385
|
+
tabLocalized: {
|
386
|
+
en: {
|
387
|
+
...relsData
|
388
|
+
},
|
389
|
+
es: {
|
390
|
+
...relsData
|
391
|
+
}
|
392
|
+
}
|
393
|
+
};
|
394
|
+
const flattenValuesBefore = Object.values(flattenRelationshipValues(data));
|
395
|
+
sanitizeRelationshipIDs({
|
396
|
+
config,
|
397
|
+
data,
|
398
|
+
fields: config.collections[0].fields
|
399
|
+
});
|
400
|
+
const flattenValuesAfter = Object.values(flattenRelationshipValues(data));
|
401
|
+
flattenValuesAfter.forEach((value, i)=>{
|
402
|
+
expect(value).toBeInstanceOf(Types.ObjectId);
|
403
|
+
expect(flattenValuesBefore[i]).toBe(value.toHexString());
|
404
|
+
});
|
405
|
+
});
|
406
|
+
});
|
407
|
+
|
408
|
+
//# sourceMappingURL=sanitizeRelationshipIDs.spec.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/sanitizeRelationshipIDs.spec.ts"],"sourcesContent":["import type { Field, SanitizedConfig } from 'payload'\n\nimport { Types } from 'mongoose'\n\nimport { sanitizeRelationshipIDs } from './sanitizeRelationshipIDs.js'\n\nconst flattenRelationshipValues = (obj: Record<string, any>, prefix = ''): Record<string, any> => {\n return Object.keys(obj).reduce(\n (acc, key) => {\n const fullKey = prefix ? `${prefix}.${key}` : key\n const value = obj[key]\n\n if (value && typeof value === 'object' && !(value instanceof Types.ObjectId)) {\n Object.assign(acc, flattenRelationshipValues(value, fullKey))\n // skip relationTo and blockType\n } else if (!fullKey.endsWith('relationTo') && !fullKey.endsWith('blockType')) {\n acc[fullKey] = value\n }\n\n return acc\n },\n {} as Record<string, any>,\n )\n}\n\nconst relsFields: Field[] = [\n {\n name: 'rel_1',\n type: 'relationship',\n relationTo: 'rels',\n },\n {\n name: 'rel_1_l',\n type: 'relationship',\n localized: true,\n relationTo: 'rels',\n },\n {\n name: 'rel_2',\n type: 'relationship',\n hasMany: true,\n relationTo: 'rels',\n },\n {\n name: 'rel_2_l',\n type: 'relationship',\n hasMany: true,\n localized: true,\n relationTo: 'rels',\n },\n {\n name: 'rel_3',\n type: 'relationship',\n relationTo: ['rels'],\n },\n {\n name: 'rel_3_l',\n type: 'relationship',\n localized: true,\n relationTo: ['rels'],\n },\n {\n name: 'rel_4',\n type: 'relationship',\n hasMany: true,\n relationTo: ['rels'],\n },\n {\n name: 'rel_4_l',\n type: 'relationship',\n hasMany: true,\n localized: true,\n relationTo: ['rels'],\n },\n]\n\nconst config = {\n collections: [\n {\n slug: 'docs',\n fields: [\n ...relsFields,\n {\n name: 'array',\n type: 'array',\n fields: [\n {\n name: 'array',\n type: 'array',\n fields: relsFields,\n },\n {\n name: 'blocks',\n type: 'blocks',\n blocks: [{ slug: 'block', fields: relsFields }],\n },\n ...relsFields,\n ],\n },\n {\n name: 'arrayLocalized',\n type: 'array',\n fields: [\n {\n name: 'array',\n type: 'array',\n fields: relsFields,\n },\n {\n name: 'blocks',\n type: 'blocks',\n blocks: [{ slug: 'block', fields: relsFields }],\n },\n ...relsFields,\n ],\n localized: true,\n },\n {\n name: 'blocks',\n type: 'blocks',\n blocks: [\n {\n slug: 'block',\n fields: [\n ...relsFields,\n {\n name: 'group',\n type: 'group',\n fields: relsFields,\n },\n {\n name: 'array',\n type: 'array',\n fields: relsFields,\n },\n ],\n },\n ],\n },\n {\n name: 'group',\n type: 'group',\n fields: [\n ...relsFields,\n {\n name: 'array',\n type: 'array',\n fields: relsFields,\n },\n ],\n },\n {\n name: 'groupLocalized',\n type: 'group',\n fields: [\n ...relsFields,\n {\n name: 'array',\n type: 'array',\n fields: relsFields,\n },\n ],\n localized: true,\n },\n {\n name: 'groupAndRow',\n type: 'group',\n fields: [\n {\n type: 'row',\n fields: [\n ...relsFields,\n {\n type: 'array',\n name: 'array',\n fields: relsFields,\n },\n ],\n },\n ],\n },\n {\n type: 'tabs',\n tabs: [\n {\n name: 'tab',\n fields: relsFields,\n },\n {\n name: 'tabLocalized',\n fields: relsFields,\n localized: true,\n },\n ],\n },\n ],\n },\n {\n slug: 'rels',\n fields: [],\n },\n ],\n localization: {\n defaultLocale: 'en',\n localeCodes: ['en', 'es'],\n locales: [\n { code: 'en', label: 'EN' },\n { code: 'es', label: 'ES' },\n ],\n },\n} as SanitizedConfig\n\nconst relsData = {\n rel_1: new Types.ObjectId().toHexString(),\n rel_1_l: {\n en: new Types.ObjectId().toHexString(),\n es: new Types.ObjectId().toHexString(),\n },\n rel_2: [new Types.ObjectId().toHexString()],\n rel_2_l: {\n en: [new Types.ObjectId().toHexString()],\n es: [new Types.ObjectId().toHexString()],\n },\n rel_3: {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n rel_3_l: {\n en: {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n es: {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n },\n rel_4: [\n {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n ],\n rel_4_l: {\n en: [\n {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n ],\n es: [\n {\n relationTo: 'rels',\n value: new Types.ObjectId().toHexString(),\n },\n ],\n },\n}\n\ndescribe('sanitizeRelationshipIDs', () => {\n it('should sanitize relationships', () => {\n const data = {\n ...relsData,\n array: [\n {\n ...relsData,\n array: [{ ...relsData }],\n blocks: [\n {\n blockType: 'block',\n ...relsData,\n },\n ],\n },\n ],\n arrayLocalized: {\n en: [\n {\n ...relsData,\n array: [{ ...relsData }],\n blocks: [\n {\n blockType: 'block',\n ...relsData,\n },\n ],\n },\n ],\n es: [\n {\n ...relsData,\n array: [{ ...relsData }],\n blocks: [\n {\n blockType: 'block',\n ...relsData,\n },\n ],\n },\n ],\n },\n blocks: [\n {\n blockType: 'block',\n ...relsData,\n array: [{ ...relsData }],\n group: { ...relsData },\n },\n ],\n group: {\n ...relsData,\n array: [{ ...relsData }],\n },\n groupAndRow: {\n ...relsData,\n array: [{ ...relsData }],\n },\n groupLocalized: {\n en: {\n ...relsData,\n array: [{ ...relsData }],\n },\n es: {\n ...relsData,\n array: [{ ...relsData }],\n },\n },\n tab: { ...relsData },\n tabLocalized: {\n en: { ...relsData },\n es: { ...relsData },\n },\n }\n const flattenValuesBefore = Object.values(flattenRelationshipValues(data))\n\n sanitizeRelationshipIDs({ config, data, fields: config.collections[0].fields })\n const flattenValuesAfter = Object.values(flattenRelationshipValues(data))\n\n flattenValuesAfter.forEach((value, i) => {\n expect(value).toBeInstanceOf(Types.ObjectId)\n expect(flattenValuesBefore[i]).toBe(value.toHexString())\n })\n })\n})\n"],"names":["Types","sanitizeRelationshipIDs","flattenRelationshipValues","obj","prefix","Object","keys","reduce","acc","key","fullKey","value","ObjectId","assign","endsWith","relsFields","name","type","relationTo","localized","hasMany","config","collections","slug","fields","blocks","tabs","localization","defaultLocale","localeCodes","locales","code","label","relsData","rel_1","toHexString","rel_1_l","en","es","rel_2","rel_2_l","rel_3","rel_3_l","rel_4","rel_4_l","describe","it","data","array","blockType","arrayLocalized","group","groupAndRow","groupLocalized","tab","tabLocalized","flattenValuesBefore","values","flattenValuesAfter","forEach","i","expect","toBeInstanceOf","toBe"],"mappings":"AAEA,SAASA,KAAK,QAAQ,WAAU;AAEhC,SAASC,uBAAuB,QAAQ,+BAA8B;AAEtE,MAAMC,4BAA4B,CAACC,KAA0BC,SAAS,EAAE;IACtE,OAAOC,OAAOC,IAAI,CAACH,KAAKI,MAAM,CAC5B,CAACC,KAAKC;QACJ,MAAMC,UAAUN,SAAS,CAAC,EAAEA,OAAO,CAAC,EAAEK,IAAI,CAAC,GAAGA;QAC9C,MAAME,QAAQR,GAAG,CAACM,IAAI;Q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mBAAmBC,OAAO,CAAC,CAAChD,OAAOiD;YACjCC,OAAOlD,OAAOmD,cAAc,CAAC9D,MAAMY,QAAQ;YAC3CiD,OAAOL,mBAAmB,CAACI,EAAE,EAAEG,IAAI,CAACpD,MAAMwB,WAAW;QACvD;IACF;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.99cbde3",
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
5
5
|
"homepage": "https://payloadcms.com",
|
6
6
|
"repository": {
|
@@ -34,7 +34,6 @@
|
|
34
34
|
"types": "./dist/index.d.ts",
|
35
35
|
"files": [
|
36
36
|
"dist",
|
37
|
-
"mock.js",
|
38
37
|
"predefinedMigrations"
|
39
38
|
],
|
40
39
|
"dependencies": {
|
@@ -50,10 +49,10 @@
|
|
50
49
|
"mongodb": "6.10.0",
|
51
50
|
"mongodb-memory-server": "^9",
|
52
51
|
"@payloadcms/eslint-config": "3.0.0-beta.112",
|
53
|
-
"payload": "3.0.0-canary.
|
52
|
+
"payload": "3.0.0-canary.99cbde3"
|
54
53
|
},
|
55
54
|
"peerDependencies": {
|
56
|
-
"payload": "3.0.0-canary.
|
55
|
+
"payload": "3.0.0-canary.99cbde3"
|
57
56
|
},
|
58
57
|
"scripts": {
|
59
58
|
"build": "pnpm build:types && pnpm build:swc",
|
package/dist/queries/mock.js
DELETED
package/dist/queries/mock.js.map
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
{"version":3,"sources":["../../src/queries/mock.js"],"names":[],"mappings":""}
|