@payloadcms/db-mongodb 3.1.1-canary.9da0158 → 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.d.ts.map +1 -1
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js +15 -12
- package/dist/predefinedMigrations/migrateRelationshipsV2_V3.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.d.ts.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.js +8 -8
- package/dist/utilities/sanitizeRelationshipIDs.js.map +1 -1
- package/dist/utilities/sanitizeRelationshipIDs.spec.js +17 -0
- package/dist/utilities/sanitizeRelationshipIDs.spec.js.map +1 -1
- package/package.json +3 -3
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AAmGrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,
|
1
|
+
{"version":3,"file":"migrateRelationshipsV2_V3.d.ts","sourceRoot":"","sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAS,cAAc,EAAmB,MAAM,SAAS,CAAA;AAmGrE,wBAAsB,yBAAyB,CAAC,EAC9C,SAAS,EACT,GAAG,GACJ,EAAE;IACD,SAAS,EAAE,MAAM,CAAA;IACjB,GAAG,EAAE,cAAc,CAAA;CACpB,GAAG,OAAO,CAAC,IAAI,CAAC,CA+EhB"}
|
@@ -111,18 +111,21 @@ export async function migrateRelationshipsV2_V3({ batchSize, req }) {
|
|
111
111
|
lean: true,
|
112
112
|
session
|
113
113
|
});
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
114
|
+
// in case if the global doesn't exist in the database yet (not saved)
|
115
|
+
if (doc) {
|
116
|
+
sanitizeRelationshipIDs({
|
117
|
+
config,
|
118
|
+
data: doc,
|
119
|
+
fields: global.fields
|
120
|
+
});
|
121
|
+
await GlobalsModel.collection.updateOne({
|
122
|
+
globalType: global.slug
|
123
|
+
}, {
|
124
|
+
$set: doc
|
125
|
+
}, {
|
126
|
+
session
|
127
|
+
});
|
128
|
+
}
|
126
129
|
payload.logger.info(`Migrated global "${global.slug}"`);
|
127
130
|
if (global.versions) {
|
128
131
|
payload.logger.info(`Migrating global versions "${global.slug}"`);
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest, SanitizedConfig } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { sanitizeRelationshipIDs } from '../utilities/sanitizeRelationshipIDs.js'\nimport { withSession } from '../withSession.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n config,\n fields,\n Model,\n session,\n}: {\n batchSize: number\n config: SanitizedConfig\n fields: Field[]\n Model: Model<any>\n session: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n sanitizeRelationshipIDs({ config, data: doc, fields })\n }\n\n await Model.collection.bulkWrite(\n docs.map((doc) => ({\n updateOne: {\n filter: { _id: doc._id },\n update: {\n $set: doc,\n },\n },\n })),\n { session },\n )\n\n skip += batchSize\n }\n}\n\nconst hasRelationshipOrUploadField = ({ fields }: { fields: Field[] }): boolean => {\n for (const field of fields) {\n if (field.type === 'relationship' || field.type === 'upload') {\n return true\n }\n\n if ('fields' in field) {\n if (hasRelationshipOrUploadField({ fields: field.fields })) {\n return true\n }\n }\n\n if ('blocks' in field) {\n for (const block of field.blocks) {\n if (hasRelationshipOrUploadField({ fields: block.fields })) {\n return true\n }\n }\n }\n\n if ('tabs' in field) {\n for (const tab of field.tabs) {\n if (hasRelationshipOrUploadField({ fields: tab.fields })) {\n return true\n }\n }\n }\n }\n\n return false\n}\n\nexport async function migrateRelationshipsV2_V3({\n batchSize,\n req,\n}: {\n batchSize: number\n req: PayloadRequest\n}): Promise<void> {\n const { payload } = req\n const db = payload.db as MongooseAdapter\n const config = payload.config\n\n const { session } = await withSession(db, req)\n\n for (const collection of payload.config.collections.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: collection.fields,\n Model: db.collections[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: buildVersionCollectionFields(config, collection),\n Model: db.versions[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection versions \"${collection.slug}\"`)\n }\n }\n\n const { globals: GlobalsModel } = db\n\n for (const global of payload.config.globals.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = await GlobalsModel.findOne<Record<string, unknown>>(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )\n\n sanitizeRelationshipIDs({ config, data: doc, fields: global.fields })\n\n
|
1
|
+
{"version":3,"sources":["../../src/predefinedMigrations/migrateRelationshipsV2_V3.ts"],"sourcesContent":["import type { ClientSession, Model } from 'mongoose'\nimport type { Field, PayloadRequest, SanitizedConfig } from 'payload'\n\nimport { buildVersionCollectionFields, buildVersionGlobalFields } from 'payload'\n\nimport type { MongooseAdapter } from '../index.js'\n\nimport { sanitizeRelationshipIDs } from '../utilities/sanitizeRelationshipIDs.js'\nimport { withSession } from '../withSession.js'\n\nconst migrateModelWithBatching = async ({\n batchSize,\n config,\n fields,\n Model,\n session,\n}: {\n batchSize: number\n config: SanitizedConfig\n fields: Field[]\n Model: Model<any>\n session: ClientSession\n}): Promise<void> => {\n let hasNext = true\n let skip = 0\n\n while (hasNext) {\n const docs = await Model.find(\n {},\n {},\n {\n lean: true,\n limit: batchSize + 1,\n session,\n skip,\n },\n )\n\n if (docs.length === 0) {\n break\n }\n\n hasNext = docs.length > batchSize\n\n if (hasNext) {\n docs.pop()\n }\n\n for (const doc of docs) {\n sanitizeRelationshipIDs({ config, data: doc, fields })\n }\n\n await Model.collection.bulkWrite(\n docs.map((doc) => ({\n updateOne: {\n filter: { _id: doc._id },\n update: {\n $set: doc,\n },\n },\n })),\n { session },\n )\n\n skip += batchSize\n }\n}\n\nconst hasRelationshipOrUploadField = ({ fields }: { fields: Field[] }): boolean => {\n for (const field of fields) {\n if (field.type === 'relationship' || field.type === 'upload') {\n return true\n }\n\n if ('fields' in field) {\n if (hasRelationshipOrUploadField({ fields: field.fields })) {\n return true\n }\n }\n\n if ('blocks' in field) {\n for (const block of field.blocks) {\n if (hasRelationshipOrUploadField({ fields: block.fields })) {\n return true\n }\n }\n }\n\n if ('tabs' in field) {\n for (const tab of field.tabs) {\n if (hasRelationshipOrUploadField({ fields: tab.fields })) {\n return true\n }\n }\n }\n }\n\n return false\n}\n\nexport async function migrateRelationshipsV2_V3({\n batchSize,\n req,\n}: {\n batchSize: number\n req: PayloadRequest\n}): Promise<void> {\n const { payload } = req\n const db = payload.db as MongooseAdapter\n const config = payload.config\n\n const { session } = await withSession(db, req)\n\n for (const collection of payload.config.collections.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating collection \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: collection.fields,\n Model: db.collections[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection \"${collection.slug}\"`)\n\n if (collection.versions) {\n payload.logger.info(`Migrating collection versions \"${collection.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: buildVersionCollectionFields(config, collection),\n Model: db.versions[collection.slug],\n session,\n })\n\n payload.logger.info(`Migrated collection versions \"${collection.slug}\"`)\n }\n }\n\n const { globals: GlobalsModel } = db\n\n for (const global of payload.config.globals.filter(hasRelationshipOrUploadField)) {\n payload.logger.info(`Migrating global \"${global.slug}\"`)\n\n const doc = await GlobalsModel.findOne<Record<string, unknown>>(\n {\n globalType: {\n $eq: global.slug,\n },\n },\n {},\n { lean: true, session },\n )\n\n // in case if the global doesn't exist in the database yet (not saved)\n if (doc) {\n sanitizeRelationshipIDs({ config, data: doc, fields: global.fields })\n\n await GlobalsModel.collection.updateOne(\n {\n globalType: global.slug,\n },\n { $set: doc },\n { session },\n )\n }\n\n payload.logger.info(`Migrated global \"${global.slug}\"`)\n\n if (global.versions) {\n payload.logger.info(`Migrating global versions \"${global.slug}\"`)\n\n await migrateModelWithBatching({\n batchSize,\n config,\n fields: buildVersionGlobalFields(config, global),\n Model: db.versions[global.slug],\n session,\n })\n\n payload.logger.info(`Migrated global versions \"${global.slug}\"`)\n }\n }\n}\n"],"names":["buildVersionCollectionFields","buildVersionGlobalFields","sanitizeRelationshipIDs","withSession","migrateModelWithBatching","batchSize","config","fields","Model","session","hasNext","skip","docs","find","lean","limit","length","pop","doc","data","collection","bulkWrite","map","updateOne","filter","_id","update","$set","hasRelationshipOrUploadField","field","type","block","blocks","tab","tabs","migrateRelationshipsV2_V3","req","payload","db","collections","logger","info","slug","versions","globals","GlobalsModel","global","findOne","globalType","$eq"],"mappings":"AAGA,SAASA,4BAA4B,EAAEC,wBAAwB,QAAQ,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AAA,OAAO,KAAK,EAAoB,KAAK,EAAE,eAAe,EAA0B,MAAM,SAAS,CAAA;AAM/F,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,KAAK,EAAE,CAAA;CAChB,CAAA;
|
1
|
+
{"version":3,"file":"sanitizeRelationshipIDs.d.ts","sourceRoot":"","sources":["../../src/utilities/sanitizeRelationshipIDs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAoB,KAAK,EAAE,eAAe,EAA0B,MAAM,SAAS,CAAA;AAM/F,KAAK,IAAI,GAAG;IACV,MAAM,EAAE,eAAe,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,MAAM,EAAE,KAAK,EAAE,CAAA;CAChB,CAAA;AAoGD,eAAO,MAAM,uBAAuB,8BAIjC,IAAI,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAyC/B,CAAA"}
|
@@ -1,19 +1,19 @@
|
|
1
1
|
import { Types } from 'mongoose';
|
2
|
-
import {
|
2
|
+
import { traverseFields } from 'payload';
|
3
3
|
import { fieldAffectsData } from 'payload/shared';
|
4
4
|
function isValidRelationObject(value) {
|
5
5
|
return typeof value === 'object' && value !== null && 'relationTo' in value && 'value' in value;
|
6
6
|
}
|
7
7
|
const convertValue = ({ relatedCollection, value })=>{
|
8
8
|
const customIDField = relatedCollection.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
|
9
|
-
if (
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
9
|
+
if (customIDField) {
|
10
|
+
return value;
|
11
|
+
}
|
12
|
+
try {
|
13
|
+
return new Types.ObjectId(value);
|
14
|
+
} catch {
|
15
|
+
return value;
|
15
16
|
}
|
16
|
-
return value;
|
17
17
|
};
|
18
18
|
const sanitizeRelationship = ({ config, field, locale, ref, value })=>{
|
19
19
|
let relatedCollection;
|
@@ -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 (
|
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 return value\n }\n\n try {\n return new Types.ObjectId(value)\n } catch {\n return value\n }\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 (!ref || typeof ref !== 'object') {\n return\n }\n\n if (field.type === 'relationship' || field.type === 'upload') {\n if (!ref[field.name]) {\n return\n }\n\n // handle localized relationships\n if (config.localization && field.localized) {\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({ 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","traverseFields","fieldAffectsData","isValidRelationObject","value","convertValue","relatedCollection","customIDField","fields","find","field","name","ObjectId","sanitizeRelationship","config","locale","ref","result","hasManyRelations","relationTo","collections","slug","Array","isArray","map","val","relatedCollectionForSingleValue","sanitizeRelationshipIDs","data","sanitize","type","localization","localized","locales","fieldRef","code","undefined","callback","fillEmpty"],"mappings":"AAEA,SAASA,KAAK,QAAQ,WAAU;AAChC,SAAmBC,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,IAAIJ,eAAe;QACjB,OAAOH;IACT;IAEA,IAAI;QACF,OAAO,IAAIJ,MAAMY,QAAQ,CAACR;IAC5B,EAAE,OAAM;QACN,OAAOA;IACT;AACF;AAEA,MAAMS,uBAAuB,CAAC,EAAEC,MAAM,EAAEJ,KAAK,EAAEK,MAAM,EAAEC,GAAG,EAAEZ,KAAK,EAAE;IACjE,IAAIE;IACJ,IAAIW,SAASb;IAEb,MAAMc,mBAAmB,OAAOR,MAAMS,UAAU,KAAK;IAErD,IAAI,CAACD,kBAAkB;QACrBZ,oBAAoBQ,OAAOM,WAAW,EAAEX,KAAK,CAAC,EAAEY,IAAI,EAAE,GAAKA,SAASX,MAAMS,UAAU;IACtF;IAEA,IAAIG,MAAMC,OAAO,CAACnB,QAAQ;QACxBa,SAASb,MAAMoB,GAAG,CAAC,CAACC;YAClB,kBAAkB;YAClB,IAAInB,qBAAqBmB,OAAQ,CAAA,OAAOA,QAAQ,YAAY,OAAOA,QAAQ,QAAO,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ACd,GAAG,CAACN,MAAMC,IAAI,CAAC,EAAE;gBACpB;YACF;YAEA,iCAAiC;YACjC,IAAIG,OAAOiB,YAAY,IAAIrB,MAAMsB,SAAS,EAAE;gBAC1C,MAAMC,UAAUnB,OAAOiB,YAAY,CAACE,OAAO;gBAC3C,MAAMC,WAAWlB,GAAG,CAACN,MAAMC,IAAI,CAAC;gBAChC,IAAI,OAAOuB,aAAa,UAAU;oBAChC;gBACF;gBAEA,KAAK,MAAM,EAAEC,IAAI,EAAE,IAAIF,QAAS;oBAC9B,MAAM7B,QAAQY,GAAG,CAACN,MAAMC,IAAI,CAAC,CAACwB,KAAK;oBACnC,IAAI/B,OAAO;wBACTS,qBAAqB;4BAAEC;4BAAQJ;4BAAOK,QAAQoB;4BAAMnB,KAAKkB;4BAAU9B;wBAAM;oBAC3E;gBACF;YACF,OAAO;gBACL,qCAAqC;gBACrCS,qBAAqB;oBACnBC;oBACAJ;oBACAK,QAAQqB;oBACRpB;oBACAZ,OAAOY,GAAG,CAACN,MAAMC,IAAI,CAAC;gBACxB;YACF;QACF;IACF;IAEAV,eAAe;QAAEoC,UAAUR;QAAUrB;QAAQ8B,WAAW;QAAOtB,KAAKY;IAAK;IAEzE,OAAOA;AACT,EAAC"}
|
@@ -197,6 +197,20 @@ const config = {
|
|
197
197
|
name: 'tabLocalized',
|
198
198
|
fields: relsFields,
|
199
199
|
localized: true
|
200
|
+
},
|
201
|
+
{
|
202
|
+
label: 'another',
|
203
|
+
fields: [
|
204
|
+
{
|
205
|
+
type: 'tabs',
|
206
|
+
tabs: [
|
207
|
+
{
|
208
|
+
name: 'nestedTab',
|
209
|
+
fields: relsFields
|
210
|
+
}
|
211
|
+
]
|
212
|
+
}
|
213
|
+
]
|
200
214
|
}
|
201
215
|
]
|
202
216
|
}
|
@@ -389,6 +403,9 @@ describe('sanitizeRelationshipIDs', ()=>{
|
|
389
403
|
es: {
|
390
404
|
...relsData
|
391
405
|
}
|
406
|
+
},
|
407
|
+
nestedTab: {
|
408
|
+
...relsData
|
392
409
|
}
|
393
410
|
};
|
394
411
|
const flattenValuesBefore = Object.values(flattenRelationshipValues(data));
|
@@ -1 +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;QAEtB,IAAIE,SAAS,OAAOA,UAAU,YAAY,CAAEA,CAAAA,iBAAiBX,MAAMY,QAAQ,AAAD,GAAI;YAC5EP,OAAOQ,MAAM,CAACL,KAAKN,0BAA0BS,OAAOD;QACpD,gCAAgC;QAClC,OAAO,IAAI,CAACA,QAAQI,QAAQ,CAAC,iBAAiB,CAACJ,QAAQI,QAAQ,CAAC,cAAc;YAC5EN,GAAG,CAACE,QAAQ,GAAGC;QACjB;QAEA,OAAOH;IACT,GACA,CAAC;AAEL;AAEA,MAAMO,aAAsB;IAC1B;QACEC,MAAM;QACNC,MAAM;QACNC,YAAY;IACd;IACA;QACEF,MAAM;QACNC,MAAM;QACNE,WAAW;QACXD,YAAY;IACd;IACA;QACEF,MAAM;QACNC,MAAM;QACNG,SAAS;QACTF,YAAY;IACd;IACA;QACEF,MAAM;QACNC,MAAM;QACNG,SAAS;QACTD,WAAW;QACXD,YAAY;IACd;IACA;QACEF,MAAM;QACNC,MAAM;QACNC,YAAY;YAAC;SAAO;IACtB;IACA;QACEF,MAAM;QACNC,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BACdhB,IAAI;oBACF,GAAGJ,QAAQ;oBACXe,OAAO;wBAAC;4BAAE,GAAGf,QAAQ;wBAAC;qBAAE;gBAC1B;gBACAK,IAAI;oBACF,GAAGL,QAAQ;oBACXe,OAAO;wBAAC;4BAAE,GAAGf,QAAQ;wBAAC;qBAAE;gBAC1B;YACF;YACAqB,KAAK;gBAAE,GAAGrB,QAAQ;YAAC;YACnBsB,cAAc;gBACZlB,IAAI;oBAAE,GAAGJ,QAAQ;gBAAC;gBAClBK,IAAI;oBAAE,GAAGL,QAAQ;gBAAC;YACpB;QACF;QACA,MAAMuB,sBAAsBnD,OAAOoD,MAAM,CAACvD,0BAA0B6C;QAEpE9C,wBAAwB;YAAEoB;YAAQ0B;YAAMvB,QAAQH,OAAOC,WAAW,CAAC,EAAE,CAACE,MAAM;QAAC;QAC7E,MAAMkC,qBAAqBrD,OAAOoD,MAAM,CAACvD,0BAA0B6C;QAEnEW,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"}
|
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 label: 'another',\n fields: [\n {\n type: 'tabs',\n tabs: [\n {\n name: 'nestedTab',\n fields: relsFields,\n },\n ],\n },\n ],\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 nestedTab: { ...relsData },\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","label","localization","defaultLocale","localeCodes","locales","code","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","nestedTab","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;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GAAGL,QAAQ;gBAAC;YACpB;YACAuB,WAAW;gBAAE,GAAGvB,QAAQ;YAAC;QAC3B;QACA,MAAMwB,sBAAsBpD,OAAOqD,MAAM,CAACxD,0BAA0B6C;QAEpE9C,wBAAwB;YAAEoB;YAAQ0B;YAAMvB,QAAQH,OAAOC,WAAW,CAAC,EAAE,CAACE,MAAM;QAAC;QAC7E,MAAMmC,qBAAqBtD,OAAOqD,MAAM,CAACxD,0BAA0B6C;QAEnEY,mBAAmBC,OAAO,CAAC,CAACjD,OAAOkD;YACjCC,OAAOnD,OAAOoD,cAAc,CAAC/D,MAAMY,QAAQ;YAC3CkD,OAAOL,mBAAmB,CAACI,EAAE,EAAEG,IAAI,CAACrD,MAAMwB,WAAW;QACvD;IACF;AACF"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@payloadcms/db-mongodb",
|
3
|
-
"version": "3.1.1
|
3
|
+
"version": "3.1.1",
|
4
4
|
"description": "The officially supported MongoDB database adapter for Payload",
|
5
5
|
"homepage": "https://payloadcms.com",
|
6
6
|
"repository": {
|
@@ -49,10 +49,10 @@
|
|
49
49
|
"mongodb": "6.10.0",
|
50
50
|
"mongodb-memory-server": "^9",
|
51
51
|
"@payloadcms/eslint-config": "3.0.0",
|
52
|
-
"payload": "3.1.1
|
52
|
+
"payload": "3.1.1"
|
53
53
|
},
|
54
54
|
"peerDependencies": {
|
55
|
-
"payload": "3.1.1
|
55
|
+
"payload": "3.1.1"
|
56
56
|
},
|
57
57
|
"scripts": {
|
58
58
|
"build": "pnpm build:types && pnpm build:swc",
|