@payloadcms/drizzle 3.39.0-canary.7 → 3.39.0-internal.1c212c9

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.
Files changed (104) hide show
  1. package/dist/count.js +1 -1
  2. package/dist/count.js.map +1 -1
  3. package/dist/countGlobalVersions.js +1 -1
  4. package/dist/countGlobalVersions.js.map +1 -1
  5. package/dist/countVersions.js +1 -1
  6. package/dist/countVersions.js.map +1 -1
  7. package/dist/createGlobal.js +2 -2
  8. package/dist/createGlobal.js.map +1 -1
  9. package/dist/createGlobalVersion.d.ts +1 -1
  10. package/dist/createGlobalVersion.d.ts.map +1 -1
  11. package/dist/createGlobalVersion.js +3 -3
  12. package/dist/createGlobalVersion.js.map +1 -1
  13. package/dist/createVersion.d.ts +1 -1
  14. package/dist/createVersion.d.ts.map +1 -1
  15. package/dist/createVersion.js +1 -1
  16. package/dist/createVersion.js.map +1 -1
  17. package/dist/deleteOne.js +1 -1
  18. package/dist/deleteOne.js.map +1 -1
  19. package/dist/find/findMany.js +1 -1
  20. package/dist/find/findMany.js.map +1 -1
  21. package/dist/find/traverseFields.d.ts.map +1 -1
  22. package/dist/find/traverseFields.js +3 -4
  23. package/dist/find/traverseFields.js.map +1 -1
  24. package/dist/index.d.ts +1 -1
  25. package/dist/index.d.ts.map +1 -1
  26. package/dist/index.js +1 -1
  27. package/dist/index.js.map +1 -1
  28. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts.map +1 -1
  29. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js.map +1 -1
  30. package/dist/queries/buildOrderBy.d.ts.map +1 -1
  31. package/dist/queries/buildOrderBy.js +2 -11
  32. package/dist/queries/buildOrderBy.js.map +1 -1
  33. package/dist/queries/buildQuery.d.ts +2 -2
  34. package/dist/queries/buildQuery.d.ts.map +1 -1
  35. package/dist/queries/buildQuery.js +2 -1
  36. package/dist/queries/buildQuery.js.map +1 -1
  37. package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
  38. package/dist/queries/getTableColumnFromPath.js +3 -4
  39. package/dist/queries/getTableColumnFromPath.js.map +1 -1
  40. package/dist/queries/operatorMap.d.ts +2 -2
  41. package/dist/queries/operatorMap.d.ts.map +1 -1
  42. package/dist/queries/operatorMap.js.map +1 -1
  43. package/dist/queries/selectDistinct.d.ts.map +1 -1
  44. package/dist/queries/selectDistinct.js.map +1 -1
  45. package/dist/schema/build.d.ts +1 -2
  46. package/dist/schema/build.d.ts.map +1 -1
  47. package/dist/schema/build.js +1 -2
  48. package/dist/schema/build.js.map +1 -1
  49. package/dist/schema/buildRawSchema.d.ts.map +1 -1
  50. package/dist/schema/buildRawSchema.js +0 -4
  51. package/dist/schema/buildRawSchema.js.map +1 -1
  52. package/dist/schema/traverseFields.d.ts +1 -2
  53. package/dist/schema/traverseFields.d.ts.map +1 -1
  54. package/dist/schema/traverseFields.js +14 -26
  55. package/dist/schema/traverseFields.js.map +1 -1
  56. package/dist/transactions/commitTransaction.js +1 -1
  57. package/dist/transactions/commitTransaction.js.map +1 -1
  58. package/dist/transform/read/index.d.ts.map +1 -1
  59. package/dist/transform/read/index.js.map +1 -1
  60. package/dist/transform/read/traverseFields.d.ts.map +1 -1
  61. package/dist/transform/read/traverseFields.js +5 -3
  62. package/dist/transform/read/traverseFields.js.map +1 -1
  63. package/dist/transform/write/blocks.d.ts.map +1 -1
  64. package/dist/transform/write/blocks.js +5 -6
  65. package/dist/transform/write/blocks.js.map +1 -1
  66. package/dist/transform/write/relationships.js +1 -1
  67. package/dist/transform/write/relationships.js.map +1 -1
  68. package/dist/transform/write/traverseFields.d.ts.map +1 -1
  69. package/dist/transform/write/traverseFields.js +1 -3
  70. package/dist/transform/write/traverseFields.js.map +1 -1
  71. package/dist/transform/write/types.d.ts +1 -1
  72. package/dist/transform/write/types.js.map +1 -1
  73. package/dist/updateGlobal.d.ts +1 -1
  74. package/dist/updateGlobal.d.ts.map +1 -1
  75. package/dist/updateGlobal.js +3 -3
  76. package/dist/updateGlobal.js.map +1 -1
  77. package/dist/updateGlobalVersion.d.ts +1 -1
  78. package/dist/updateGlobalVersion.d.ts.map +1 -1
  79. package/dist/updateGlobalVersion.js +4 -4
  80. package/dist/updateGlobalVersion.js.map +1 -1
  81. package/dist/updateMany.js +1 -1
  82. package/dist/updateMany.js.map +1 -1
  83. package/dist/updateOne.js +1 -1
  84. package/dist/updateOne.js.map +1 -1
  85. package/dist/updateVersion.d.ts +1 -1
  86. package/dist/updateVersion.d.ts.map +1 -1
  87. package/dist/updateVersion.js +4 -4
  88. package/dist/updateVersion.js.map +1 -1
  89. package/dist/upsertRow/index.d.ts.map +1 -1
  90. package/dist/upsertRow/index.js +16 -14
  91. package/dist/upsertRow/index.js.map +1 -1
  92. package/dist/upsertRow/insertArrays.js +1 -1
  93. package/dist/upsertRow/insertArrays.js.map +1 -1
  94. package/dist/upsertRow/types.d.ts +1 -1
  95. package/dist/upsertRow/types.d.ts.map +1 -1
  96. package/dist/upsertRow/types.js.map +1 -1
  97. package/dist/utilities/createBlocksMap.d.ts.map +1 -1
  98. package/dist/utilities/createBlocksMap.js +1 -5
  99. package/dist/utilities/createBlocksMap.js.map +1 -1
  100. package/dist/utilities/validateExistingBlockIsIdentical.d.ts +2 -8
  101. package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
  102. package/dist/utilities/validateExistingBlockIsIdentical.js +5 -14
  103. package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
  104. package/package.json +3 -3
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/upsertRow/index.ts"],"sourcesContent":["import type { TypeWithID } from 'payload'\n\nimport { eq } from 'drizzle-orm'\nimport { ValidationError } from 'payload'\n\nimport type { BlockRowToInsert } from '../transform/write/types.js'\nimport type { Args } from './types.js'\n\nimport { buildFindManyArgs } from '../find/buildFindManyArgs.js'\nimport { transform } from '../transform/read/index.js'\nimport { transformForWrite } from '../transform/write/index.js'\nimport { deleteExistingArrayRows } from './deleteExistingArrayRows.js'\nimport { deleteExistingRowsByPath } from './deleteExistingRowsByPath.js'\nimport { insertArrays } from './insertArrays.js'\n\nexport const upsertRow = async <T extends Record<string, unknown> | TypeWithID>({\n id,\n adapter,\n data,\n db,\n fields,\n ignoreResult,\n // TODO:\n // When we support joins for write operations (create/update) - pass collectionSlug to the buildFindManyArgs\n // Make a new argument in upsertRow.ts and pass the slug from every operation.\n joinQuery: _joinQuery,\n operation,\n path = '',\n req,\n select,\n tableName,\n upsertTarget,\n where,\n}: Args): Promise<T> => {\n // Split out the incoming data into the corresponding:\n // base row, locales, relationships, blocks, and arrays\n const rowToInsert = transformForWrite({\n adapter,\n data,\n fields,\n path,\n tableName,\n })\n\n // First, we insert the main row\n let insertedRow: Record<string, unknown>\n\n try {\n if (operation === 'update') {\n const target = upsertTarget || adapter.tables[tableName].id\n\n if (id) {\n rowToInsert.row.id = id\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target },\n tableName,\n values: rowToInsert.row,\n })\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target, where },\n tableName,\n values: rowToInsert.row,\n })\n }\n } else {\n if (adapter.allowIDOnCreate && data.id) {\n rowToInsert.row.id = data.id\n }\n ;[insertedRow] = await adapter.insert({\n db,\n tableName,\n values: rowToInsert.row,\n })\n }\n\n const localesToInsert: Record<string, unknown>[] = []\n const relationsToInsert: Record<string, unknown>[] = []\n const textsToInsert: Record<string, unknown>[] = []\n const numbersToInsert: Record<string, unknown>[] = []\n const blocksToInsert: { [blockType: string]: BlockRowToInsert[] } = {}\n const selectsToInsert: { [selectTableName: string]: Record<string, unknown>[] } = {}\n\n // If there are locale rows with data, add the parent and locale to each\n if (Object.keys(rowToInsert.locales).length > 0) {\n Object.entries(rowToInsert.locales).forEach(([locale, localeRow]) => {\n localeRow._parentID = insertedRow.id\n localeRow._locale = locale\n localesToInsert.push(localeRow)\n })\n }\n\n // If there are relationships, add parent to each\n if (rowToInsert.relationships.length > 0) {\n rowToInsert.relationships.forEach((relation) => {\n relation.parent = insertedRow.id\n relationsToInsert.push(relation)\n })\n }\n\n // If there are texts, add parent to each\n if (rowToInsert.texts.length > 0) {\n rowToInsert.texts.forEach((textRow) => {\n textRow.parent = insertedRow.id\n textsToInsert.push(textRow)\n })\n }\n\n // If there are numbers, add parent to each\n if (rowToInsert.numbers.length > 0) {\n rowToInsert.numbers.forEach((numberRow) => {\n numberRow.parent = insertedRow.id\n numbersToInsert.push(numberRow)\n })\n }\n\n // If there are selects, add parent to each, and then\n // store by table name and rows\n if (Object.keys(rowToInsert.selects).length > 0) {\n Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows]) => {\n selectsToInsert[selectTableName] = []\n\n selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n\n selectsToInsert[selectTableName].push(row)\n })\n })\n }\n\n // If there are blocks, add parent to each, and then\n // store by table name and rows\n Object.keys(rowToInsert.blocks).forEach((tableName) => {\n rowToInsert.blocks[tableName].forEach((blockRow) => {\n blockRow.row._parentID = insertedRow.id\n if (!blocksToInsert[tableName]) {\n blocksToInsert[tableName] = []\n }\n if (blockRow.row.uuid) {\n delete blockRow.row.uuid\n }\n blocksToInsert[tableName].push(blockRow)\n })\n })\n\n // //////////////////////////////////\n // INSERT LOCALES\n // //////////////////////////////////\n\n if (localesToInsert.length > 0) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`]\n\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: localeTableName,\n where: eq(localeTable._parentID, insertedRow.id),\n })\n }\n\n await adapter.insert({\n db,\n tableName: localeTableName,\n values: localesToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT RELATIONSHIPS\n // //////////////////////////////////\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: [...relationsToInsert, ...rowToInsert.relationshipsToDelete],\n tableName: relationshipsTableName,\n })\n }\n\n if (relationsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: relationshipsTableName,\n values: relationsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany TEXTS\n // //////////////////////////////////\n\n const textsTableName = `${tableName}_texts`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: textsToInsert,\n tableName: textsTableName,\n })\n }\n\n if (textsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: textsTableName,\n values: textsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany NUMBERS\n // //////////////////////////////////\n\n const numbersTableName = `${tableName}_numbers`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: numbersToInsert,\n tableName: numbersTableName,\n })\n }\n\n if (numbersToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: numbersTableName,\n values: numbersToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT BLOCKS\n // //////////////////////////////////\n\n const insertedBlockRows: Record<string, Record<string, unknown>[]> = {}\n\n if (operation === 'update') {\n for (const tableName of rowToInsert.blocksToDelete) {\n const blockTable = adapter.tables[tableName]\n await adapter.deleteWhere({\n db,\n tableName,\n where: eq(blockTable._parentID, insertedRow.id),\n })\n }\n }\n\n // When versions are enabled, this is used to track mapping between blocks/arrays ObjectID to their numeric generated representation, then we use it for nested to arrays/blocks select hasMany in versions.\n const arraysBlocksUUIDMap: Record<string, number | string> = {}\n\n for (const [tableName, blockRows] of Object.entries(blocksToInsert)) {\n insertedBlockRows[tableName] = await adapter.insert({\n db,\n tableName,\n values: blockRows.map(({ row }) => row),\n })\n\n insertedBlockRows[tableName].forEach((row, i) => {\n blockRows[i].row = row\n if (\n typeof row._uuid === 'string' &&\n (typeof row.id === 'string' || typeof row.id === 'number')\n ) {\n arraysBlocksUUIDMap[row._uuid] = row.id\n }\n })\n\n const blockLocaleIndexMap: number[] = []\n\n const blockLocaleRowsToInsert = blockRows.reduce((acc, blockRow, i) => {\n if (Object.entries(blockRow.locales).length > 0) {\n Object.entries(blockRow.locales).forEach(([blockLocale, blockLocaleData]) => {\n if (Object.keys(blockLocaleData).length > 0) {\n blockLocaleData._parentID = blockRow.row.id\n blockLocaleData._locale = blockLocale\n acc.push(blockLocaleData)\n blockLocaleIndexMap.push(i)\n }\n })\n }\n\n return acc\n }, [])\n\n if (blockLocaleRowsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: `${tableName}${adapter.localesSuffix}`,\n values: blockLocaleRowsToInsert,\n })\n }\n\n await insertArrays({\n adapter,\n arrays: blockRows.map(({ arrays }) => arrays),\n db,\n parentRows: insertedBlockRows[tableName],\n uuidMap: arraysBlocksUUIDMap,\n })\n }\n\n // //////////////////////////////////\n // INSERT ARRAYS RECURSIVELY\n // //////////////////////////////////\n\n if (operation === 'update') {\n for (const arrayTableName of Object.keys(rowToInsert.arrays)) {\n await deleteExistingArrayRows({\n adapter,\n db,\n parentID: insertedRow.id,\n tableName: arrayTableName,\n })\n }\n }\n\n await insertArrays({\n adapter,\n arrays: [rowToInsert.arrays],\n db,\n parentRows: [insertedRow],\n uuidMap: arraysBlocksUUIDMap,\n })\n\n // //////////////////////////////////\n // INSERT hasMany SELECTS\n // //////////////////////////////////\n\n for (const [selectTableName, tableRows] of Object.entries(selectsToInsert)) {\n const selectTable = adapter.tables[selectTableName]\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: selectTableName,\n where: eq(selectTable.parent, insertedRow.id),\n })\n }\n\n if (Object.keys(arraysBlocksUUIDMap).length > 0) {\n tableRows.forEach((row: any) => {\n if (row.parent in arraysBlocksUUIDMap) {\n row.parent = arraysBlocksUUIDMap[row.parent]\n }\n })\n }\n\n if (tableRows.length) {\n await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\n })\n }\n }\n\n // //////////////////////////////////\n // Error Handling\n // //////////////////////////////////\n } catch (error) {\n if (error.code === '23505') {\n let fieldName: null | string = null\n // We need to try and find the right constraint for the field but if we can't we fallback to a generic message\n if (adapter.fieldConstraints?.[tableName]) {\n if (adapter.fieldConstraints[tableName]?.[error.constraint]) {\n fieldName = adapter.fieldConstraints[tableName]?.[error.constraint]\n } else {\n const replacement = `${tableName}_`\n\n if (error.constraint.includes(replacement)) {\n const replacedConstraint = error.constraint.replace(replacement, '')\n\n if (replacedConstraint && adapter.fieldConstraints[tableName]?.[replacedConstraint]) {\n fieldName = adapter.fieldConstraints[tableName][replacedConstraint]\n }\n }\n }\n }\n\n if (!fieldName) {\n // Last case scenario we extract the key and value from the detail on the error\n const detail = error.detail\n const regex = /Key \\(([^)]+)\\)=\\(([^)]+)\\)/\n const match = detail.match(regex)\n\n if (match) {\n const key = match[1]\n\n fieldName = key\n }\n }\n\n throw new ValidationError(\n {\n id,\n errors: [\n {\n message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',\n path: fieldName,\n },\n ],\n req,\n },\n req?.t,\n )\n } else {\n throw error\n }\n }\n\n if (ignoreResult === 'idOnly') {\n return { id: insertedRow.id } as T\n }\n\n if (ignoreResult) {\n return data as T\n }\n\n // //////////////////////////////////\n // RETRIEVE NEWLY UPDATED ROW\n // //////////////////////////////////\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n joinQuery: false,\n select,\n tableName,\n })\n\n findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id)\n\n const doc = await db.query[tableName].findFirst(findManyArgs)\n\n // //////////////////////////////////\n // TRANSFORM DATA\n // //////////////////////////////////\n\n const result = transform<T>({\n adapter,\n config: adapter.payload.config,\n data: doc,\n fields,\n joinQuery: false,\n tableName,\n })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","joinQuery","_joinQuery","operation","path","req","select","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","allowIDOnCreate","localesToInsert","relationsToInsert","textsToInsert","numbersToInsert","blocksToInsert","selectsToInsert","Object","keys","locales","length","entries","forEach","locale","localeRow","_parentID","_locale","push","relationships","relation","parent","texts","textRow","numbers","numberRow","selects","selectTableName","selectRows","blocks","blockRow","uuid","localeTableName","localesSuffix","localeTable","deleteWhere","relationshipsTableName","relationshipsSuffix","localeColumnName","parentColumnName","parentID","pathColumnName","rows","relationshipsToDelete","textsTableName","numbersTableName","insertedBlockRows","blocksToDelete","blockTable","arraysBlocksUUIDMap","blockRows","map","i","_uuid","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","uuidMap","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","message","t","findManyArgs","depth","doc","query","findFirst","result","config","payload"],"mappings":"AAEA,SAASA,EAAE,QAAQ,cAAa;AAChC,SAASC,eAAe,QAAQ,UAAS;AAKzC,SAASC,iBAAiB,QAAQ,+BAA8B;AAChE,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,SAASC,uBAAuB,QAAQ,+BAA8B;AACtE,SAASC,wBAAwB,QAAQ,gCAA+B;AACxE,SAASC,YAAY,QAAQ,oBAAmB;AAEhD,OAAO,MAAMC,YAAY,OAAuD,EAC9EC,EAAE,EACFC,OAAO,EACPC,IAAI,EACJC,EAAE,EACFC,MAAM,EACNC,YAAY,EACZ,QAAQ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G,gBACR;gBACEQ;gBACAsG,QAAQ;oBACN;wBACEC,SAAS7F,KAAK8F,IAAI9F,IAAI8F,CAAC,CAAC,6BAA6B;wBACrD/F,MAAMkF;oBACR;iBACD;gBACDjF;YACF,GACAA,KAAK8F;QAET,OAAO;YACL,MAAMf;QACR;IACF;IAEA,IAAIpF,iBAAiB,UAAU;QAC7B,OAAO;YAAEL,IAAIgB,YAAYhB,EAAE;QAAC;IAC9B;IAEA,IAAIK,cAAc;QAChB,OAAOH;IACT;IAEA,qCAAqC;IACrC,6BAA6B;IAC7B,qCAAqC;IAErC,MAAMuG,eAAehH,kBAAkB;QACrCQ;QACAyG,OAAO;QACPtG;QACAE,WAAW;QACXK;QACAC;IACF;IAEA6F,aAAa3F,KAAK,GAAGvB,GAAGU,QAAQiB,MAAM,CAACN,UAAU,CAACZ,EAAE,EAAEgB,YAAYhB,EAAE;IAEpE,MAAM2G,MAAM,MAAMxG,GAAGyG,KAAK,CAAChG,UAAU,CAACiG,SAAS,CAACJ;IAEhD,qCAAqC;IACrC,iBAAiB;IACjB,qCAAqC;IAErC,MAAMK,SAASpH,UAAa;QAC1BO;QACA8G,QAAQ9G,QAAQ+G,OAAO,CAACD,MAAM;QAC9B7G,MAAMyG;QACNvG;QACAE,WAAW;QACXM;IACF;IAEA,OAAOkG;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/upsertRow/index.ts"],"sourcesContent":["import type { TypeWithID } from 'payload'\n\nimport { eq } from 'drizzle-orm'\nimport { ValidationError } from 'payload'\n\nimport type { BlockRowToInsert } from '../transform/write/types.js'\nimport type { Args } from './types.js'\n\nimport { buildFindManyArgs } from '../find/buildFindManyArgs.js'\nimport { transform } from '../transform/read/index.js'\nimport { transformForWrite } from '../transform/write/index.js'\nimport { deleteExistingArrayRows } from './deleteExistingArrayRows.js'\nimport { deleteExistingRowsByPath } from './deleteExistingRowsByPath.js'\nimport { insertArrays } from './insertArrays.js'\n\nexport const upsertRow = async <T extends Record<string, unknown> | TypeWithID>({\n id,\n adapter,\n data,\n db,\n fields,\n ignoreResult,\n // TODO:\n // When we support joins for write operations (create/update) - pass collectionSlug to the buildFindManyArgs\n // Make a new argument in upsertRow.ts and pass the slug from every operation.\n joinQuery: _joinQuery,\n operation,\n path = '',\n req,\n select,\n tableName,\n upsertTarget,\n where,\n}: Args): Promise<T> => {\n // Split out the incoming data into the corresponding:\n // base row, locales, relationships, blocks, and arrays\n const rowToInsert = transformForWrite({\n adapter,\n data,\n fields,\n path,\n tableName,\n })\n\n // First, we insert the main row\n let insertedRow: Record<string, unknown>\n\n try {\n if (operation === 'update') {\n const target = upsertTarget || adapter.tables[tableName].id\n\n if (id) {\n rowToInsert.row.id = id\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target },\n tableName,\n values: rowToInsert.row,\n })\n } else {\n ;[insertedRow] = await adapter.insert({\n db,\n onConflictDoUpdate: { set: rowToInsert.row, target, where },\n tableName,\n values: rowToInsert.row,\n })\n }\n } else {\n if (adapter.allowIDOnCreate && data.id) {\n rowToInsert.row.id = data.id\n }\n ;[insertedRow] = await adapter.insert({\n db,\n tableName,\n values: rowToInsert.row,\n })\n }\n\n const localesToInsert: Record<string, unknown>[] = []\n const relationsToInsert: Record<string, unknown>[] = []\n const textsToInsert: Record<string, unknown>[] = []\n const numbersToInsert: Record<string, unknown>[] = []\n const blocksToInsert: { [blockType: string]: BlockRowToInsert[] } = {}\n const selectsToInsert: { [selectTableName: string]: Record<string, unknown>[] } = {}\n\n // If there are locale rows with data, add the parent and locale to each\n if (Object.keys(rowToInsert.locales).length > 0) {\n Object.entries(rowToInsert.locales).forEach(([locale, localeRow]) => {\n localeRow._parentID = insertedRow.id\n localeRow._locale = locale\n localesToInsert.push(localeRow)\n })\n }\n\n // If there are relationships, add parent to each\n if (rowToInsert.relationships.length > 0) {\n rowToInsert.relationships.forEach((relation) => {\n relation.parent = insertedRow.id\n relationsToInsert.push(relation)\n })\n }\n\n // If there are texts, add parent to each\n if (rowToInsert.texts.length > 0) {\n rowToInsert.texts.forEach((textRow) => {\n textRow.parent = insertedRow.id\n textsToInsert.push(textRow)\n })\n }\n\n // If there are numbers, add parent to each\n if (rowToInsert.numbers.length > 0) {\n rowToInsert.numbers.forEach((numberRow) => {\n numberRow.parent = insertedRow.id\n numbersToInsert.push(numberRow)\n })\n }\n\n // If there are selects, add parent to each, and then\n // store by table name and rows\n if (Object.keys(rowToInsert.selects).length > 0) {\n Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows]) => {\n selectsToInsert[selectTableName] = []\n\n selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n\n selectsToInsert[selectTableName].push(row)\n })\n })\n }\n\n // If there are blocks, add parent to each, and then\n // store by table name and rows\n Object.keys(rowToInsert.blocks).forEach((blockName) => {\n rowToInsert.blocks[blockName].forEach((blockRow) => {\n blockRow.row._parentID = insertedRow.id\n if (!blocksToInsert[blockName]) {\n blocksToInsert[blockName] = []\n }\n if (blockRow.row.uuid) {\n delete blockRow.row.uuid\n }\n blocksToInsert[blockName].push(blockRow)\n })\n })\n\n // //////////////////////////////////\n // INSERT LOCALES\n // //////////////////////////////////\n\n if (localesToInsert.length > 0) {\n const localeTableName = `${tableName}${adapter.localesSuffix}`\n const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`]\n\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: localeTableName,\n where: eq(localeTable._parentID, insertedRow.id),\n })\n }\n\n await adapter.insert({\n db,\n tableName: localeTableName,\n values: localesToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT RELATIONSHIPS\n // //////////////////////////////////\n\n const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: [...relationsToInsert, ...rowToInsert.relationshipsToDelete],\n tableName: relationshipsTableName,\n })\n }\n\n if (relationsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: relationshipsTableName,\n values: relationsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany TEXTS\n // //////////////////////////////////\n\n const textsTableName = `${tableName}_texts`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: textsToInsert,\n tableName: textsTableName,\n })\n }\n\n if (textsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: textsTableName,\n values: textsToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT hasMany NUMBERS\n // //////////////////////////////////\n\n const numbersTableName = `${tableName}_numbers`\n\n if (operation === 'update') {\n await deleteExistingRowsByPath({\n adapter,\n db,\n localeColumnName: 'locale',\n parentColumnName: 'parent',\n parentID: insertedRow.id,\n pathColumnName: 'path',\n rows: numbersToInsert,\n tableName: numbersTableName,\n })\n }\n\n if (numbersToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: numbersTableName,\n values: numbersToInsert,\n })\n }\n\n // //////////////////////////////////\n // INSERT BLOCKS\n // //////////////////////////////////\n\n const insertedBlockRows: Record<string, Record<string, unknown>[]> = {}\n\n if (operation === 'update') {\n for (const blockName of rowToInsert.blocksToDelete) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n const blockTable = adapter.tables[blockTableName]\n await adapter.deleteWhere({\n db,\n tableName: blockTableName,\n where: eq(blockTable._parentID, insertedRow.id),\n })\n }\n }\n\n // When versions are enabled, this is used to track mapping between blocks/arrays ObjectID to their numeric generated representation, then we use it for nested to arrays/blocks select hasMany in versions.\n const arraysBlocksUUIDMap: Record<string, number | string> = {}\n\n for (const [blockName, blockRows] of Object.entries(blocksToInsert)) {\n const blockTableName = adapter.tableNameMap.get(`${tableName}_blocks_${blockName}`)\n insertedBlockRows[blockName] = await adapter.insert({\n db,\n tableName: blockTableName,\n values: blockRows.map(({ row }) => row),\n })\n\n insertedBlockRows[blockName].forEach((row, i) => {\n blockRows[i].row = row\n if (\n typeof row._uuid === 'string' &&\n (typeof row.id === 'string' || typeof row.id === 'number')\n ) {\n arraysBlocksUUIDMap[row._uuid] = row.id\n }\n })\n\n const blockLocaleIndexMap: number[] = []\n\n const blockLocaleRowsToInsert = blockRows.reduce((acc, blockRow, i) => {\n if (Object.entries(blockRow.locales).length > 0) {\n Object.entries(blockRow.locales).forEach(([blockLocale, blockLocaleData]) => {\n if (Object.keys(blockLocaleData).length > 0) {\n blockLocaleData._parentID = blockRow.row.id\n blockLocaleData._locale = blockLocale\n acc.push(blockLocaleData)\n blockLocaleIndexMap.push(i)\n }\n })\n }\n\n return acc\n }, [])\n\n if (blockLocaleRowsToInsert.length > 0) {\n await adapter.insert({\n db,\n tableName: `${blockTableName}${adapter.localesSuffix}`,\n values: blockLocaleRowsToInsert,\n })\n }\n\n await insertArrays({\n adapter,\n arrays: blockRows.map(({ arrays }) => arrays),\n db,\n parentRows: insertedBlockRows[blockName],\n uuidMap: arraysBlocksUUIDMap,\n })\n }\n\n // //////////////////////////////////\n // INSERT ARRAYS RECURSIVELY\n // //////////////////////////////////\n\n if (operation === 'update') {\n for (const arrayTableName of Object.keys(rowToInsert.arrays)) {\n await deleteExistingArrayRows({\n adapter,\n db,\n parentID: insertedRow.id,\n tableName: arrayTableName,\n })\n }\n }\n\n await insertArrays({\n adapter,\n arrays: [rowToInsert.arrays],\n db,\n parentRows: [insertedRow],\n uuidMap: arraysBlocksUUIDMap,\n })\n\n // //////////////////////////////////\n // INSERT hasMany SELECTS\n // //////////////////////////////////\n\n for (const [selectTableName, tableRows] of Object.entries(selectsToInsert)) {\n const selectTable = adapter.tables[selectTableName]\n if (operation === 'update') {\n await adapter.deleteWhere({\n db,\n tableName: selectTableName,\n where: eq(selectTable.parent, insertedRow.id),\n })\n }\n\n if (Object.keys(arraysBlocksUUIDMap).length > 0) {\n tableRows.forEach((row: any) => {\n if (row.parent in arraysBlocksUUIDMap) {\n row.parent = arraysBlocksUUIDMap[row.parent]\n }\n })\n }\n\n if (tableRows.length) {\n await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\n })\n }\n }\n\n // //////////////////////////////////\n // Error Handling\n // //////////////////////////////////\n } catch (error) {\n if (error.code === '23505') {\n let fieldName: null | string = null\n // We need to try and find the right constraint for the field but if we can't we fallback to a generic message\n if (adapter.fieldConstraints?.[tableName]) {\n if (adapter.fieldConstraints[tableName]?.[error.constraint]) {\n fieldName = adapter.fieldConstraints[tableName]?.[error.constraint]\n } else {\n const replacement = `${tableName}_`\n\n if (error.constraint.includes(replacement)) {\n const replacedConstraint = error.constraint.replace(replacement, '')\n\n if (replacedConstraint && adapter.fieldConstraints[tableName]?.[replacedConstraint]) {\n fieldName = adapter.fieldConstraints[tableName][replacedConstraint]\n }\n }\n }\n }\n\n if (!fieldName) {\n // Last case scenario we extract the key and value from the detail on the error\n const detail = error.detail\n const regex = /Key \\(([^)]+)\\)=\\(([^)]+)\\)/\n const match = detail.match(regex)\n\n if (match) {\n const key = match[1]\n\n fieldName = key\n }\n }\n\n throw new ValidationError(\n {\n id,\n errors: [\n {\n message: req?.t ? req.t('error:valueMustBeUnique') : 'Value must be unique',\n path: fieldName,\n },\n ],\n req,\n },\n req?.t,\n )\n } else {\n throw error\n }\n }\n\n if (ignoreResult === 'idOnly') {\n return { id: insertedRow.id } as T\n }\n\n if (ignoreResult) {\n return data as T\n }\n\n // //////////////////////////////////\n // RETRIEVE NEWLY UPDATED ROW\n // //////////////////////////////////\n\n const findManyArgs = buildFindManyArgs({\n adapter,\n depth: 0,\n fields,\n joinQuery: false,\n select,\n tableName,\n })\n\n findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id)\n\n const doc = await db.query[tableName].findFirst(findManyArgs)\n\n // //////////////////////////////////\n // TRANSFORM DATA\n // //////////////////////////////////\n\n const result = transform<T>({\n adapter,\n config: adapter.payload.config,\n data: doc,\n fields,\n joinQuery: false,\n tableName,\n })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","joinQuery","_joinQuery","operation","path","req","select","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","allowIDOnCreate","localesToInsert","relationsToInsert","textsToInsert","numbersToInsert","blocksToInsert","selectsToInsert","Object","keys","locales","length","entries","forEach","locale","localeRow","_parentID","_locale","push","relationships","relation","parent","texts","textRow","numbers","numberRow","selects","selectTableName","selectRows","blocks","blockName","blockRow","uuid","localeTableName","localesSuffix","localeTable","deleteWhere","relationshipsTableName","relationshipsSuffix","localeColumnName","parentColumnName","parentID","pathColumnName","rows","relationshipsToDelete","textsTableName","numbersTableName","insertedBlockRows","blocksToDelete","blockTableName","tableNameMap","get","blockTable","arraysBlocksUUIDMap","blockRows","map","i","_uuid","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","uuidMap","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","message","t","findManyArgs","depth","doc","query","findFirst","result","config","payload"],"mappings":"AAEA,SAASA,EAAE,QAAQ,cAAa;AAChC,SAASC,eAAe,QAAQ,UAAS;AAKzC,SAASC,iBAAiB,QAAQ,+BAA8B;AAChE,SAASC,SAAS,QAAQ,6BAA4B;AACtD,SAASC,iBAAiB,QAAQ,8BAA6B;AAC/D,SAASC,uBAAuB,QAAQ,+BAA8B;AACtE,SAASC,wBAAwB,QAAQ,gCAA+B;AACxE,SAASC,YAAY,QAAQ,oBAAmB;AAEhD,OAAO,MAAMC,YAAY,OAAuD,EAC9EC,EAAE,EACFC,OAAO,EACPC,IAAI,EACJC,EAAE,EACFC,MAAM,EACNC,YAAY,EACZ,QAA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AW;gBACd,+EAA+E;gBAC/E,MAAMO,SAAST,MAAMS,MAAM;gBAC3B,MAAMC,QAAQ;gBACd,MAAMC,QAAQF,OAAOE,KAAK,CAACD;gBAE3B,IAAIC,OAAO;oBACT,MAAMC,MAAMD,KAAK,CAAC,EAAE;oBAEpBT,YAAYU;gBACd;YACF;YAEA,MAAM,IAAIjH,gBACR;gBACEQ;gBACA0G,QAAQ;oBACN;wBACEC,SAASjG,KAAKkG,IAAIlG,IAAIkG,CAAC,CAAC,6BAA6B;wBACrDnG,MAAMsF;oBACR;iBACD;gBACDrF;YACF,GACAA,KAAKkG;QAET,OAAO;YACL,MAAMf;QACR;IACF;IAEA,IAAIxF,iBAAiB,UAAU;QAC7B,OAAO;YAAEL,IAAIgB,YAAYhB,EAAE;QAAC;IAC9B;IAEA,IAAIK,cAAc;QAChB,OAAOH;IACT;IAEA,qCAAqC;IACrC,6BAA6B;IAC7B,qCAAqC;IAErC,MAAM2G,eAAepH,kBAAkB;QACrCQ;QACA6G,OAAO;QACP1G;QACAE,WAAW;QACXK;QACAC;IACF;IAEAiG,aAAa/F,KAAK,GAAGvB,GAAGU,QAAQiB,MAAM,CAACN,UAAU,CAACZ,EAAE,EAAEgB,YAAYhB,EAAE;IAEpE,MAAM+G,MAAM,MAAM5G,GAAG6G,KAAK,CAACpG,UAAU,CAACqG,SAAS,CAACJ;IAEhD,qCAAqC;IACrC,iBAAiB;IACjB,qCAAqC;IAErC,MAAMK,SAASxH,UAAa;QAC1BO;QACAkH,QAAQlH,QAAQmH,OAAO,CAACD,MAAM;QAC9BjH,MAAM6G;QACN3G;QACAE,WAAW;QACXM;IACF;IAEA,OAAOsG;AACT,EAAC"}
@@ -14,7 +14,7 @@ export const insertArrays = async ({ adapter, arrays, db, parentRows, uuidMap =
14
14
  const parentID = parentRows[parentRowIndex].id;
15
15
  // Add any sub arrays that need to be created
16
16
  // We will call this recursively below
17
- arrayRows.forEach((arrayRow)=>{
17
+ arrayRows.forEach((arrayRow, i)=>{
18
18
  if (Object.keys(arrayRow.arrays).length > 0) {
19
19
  rowsByTable[tableName].arrays.push(arrayRow.arrays);
20
20
  }
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/upsertRow/insertArrays.ts"],"sourcesContent":["import type { ArrayRowToInsert } from '../transform/write/types.js'\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n parentRows: Record<string, unknown>[]\n uuidMap?: Record<string, number | string>\n}\n\ntype RowsByTable = {\n [tableName: string]: {\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n locales: Record<string, unknown>[]\n rows: Record<string, unknown>[]\n }\n}\n\nexport const insertArrays = async ({\n adapter,\n arrays,\n db,\n parentRows,\n uuidMap = {},\n}: Args): Promise<void> => {\n // Maintain a map of flattened rows by table\n const rowsByTable: RowsByTable = {}\n\n arrays.forEach((arraysByTable, parentRowIndex) => {\n Object.entries(arraysByTable).forEach(([tableName, arrayRows]) => {\n // If the table doesn't exist in map, initialize it\n if (!rowsByTable[tableName]) {\n rowsByTable[tableName] = {\n arrays: [],\n locales: [],\n rows: [],\n }\n }\n\n const parentID = parentRows[parentRowIndex].id\n\n // Add any sub arrays that need to be created\n // We will call this recursively below\n arrayRows.forEach((arrayRow) => {\n if (Object.keys(arrayRow.arrays).length > 0) {\n rowsByTable[tableName].arrays.push(arrayRow.arrays)\n }\n\n // Set up parent IDs for both row and locale row\n arrayRow.row._parentID = parentID\n rowsByTable[tableName].rows.push(arrayRow.row)\n\n Object.entries(arrayRow.locales).forEach(([arrayRowLocale, arrayRowLocaleData]) => {\n arrayRowLocaleData._parentID = arrayRow.row.id\n arrayRowLocaleData._locale = arrayRowLocale\n rowsByTable[tableName].locales.push(arrayRowLocaleData)\n if (!arrayRow.row.id) {\n arrayRowLocaleData._getParentID = (rows: { _uuid: string; id: number }[]) => {\n const { id } = rows.find((each) => each._uuid === arrayRow.row._uuid)\n return id\n }\n }\n })\n })\n })\n })\n\n // Insert all corresponding arrays\n // (one insert per array table)\n for (const [tableName, row] of Object.entries(rowsByTable)) {\n // the nested arrays need the ID for the parentID foreign key\n let insertedRows: Args['parentRows']\n if (row.rows.length > 0) {\n insertedRows = await adapter.insert({\n db,\n tableName,\n values: row.rows,\n })\n\n insertedRows.forEach((row) => {\n if (\n typeof row._uuid === 'string' &&\n (typeof row.id === 'string' || typeof row.id === 'number')\n ) {\n uuidMap[row._uuid] = row.id\n }\n })\n }\n\n // Insert locale rows\n if (adapter.tables[`${tableName}${adapter.localesSuffix}`] && row.locales.length > 0) {\n if (!row.locales[0]._parentID) {\n row.locales = row.locales.map((localeRow) => {\n if (typeof localeRow._getParentID === 'function') {\n localeRow._parentID = localeRow._getParentID(insertedRows)\n delete localeRow._getParentID\n }\n return localeRow\n })\n }\n await adapter.insert({\n db,\n tableName: `${tableName}${adapter.localesSuffix}`,\n values: row.locales,\n })\n }\n\n // If there are sub arrays, call this function recursively\n if (row.arrays.length > 0) {\n await insertArrays({\n adapter,\n arrays: row.arrays,\n db,\n parentRows: insertedRows,\n })\n }\n }\n}\n"],"names":["insertArrays","adapter","arrays","db","parentRows","uuidMap","rowsByTable","forEach","arraysByTable","parentRowIndex","Object","entries","tableName","arrayRows","locales","rows","parentID","id","arrayRow","keys","length","push","row","_parentID","arrayRowLocale","arrayRowLocaleData","_locale","_getParentID","find","each","_uuid","insertedRows","insert","values","tables","localesSuffix","map","localeRow"],"mappings":"AAuBA,OAAO,MAAMA,eAAe,OAAO,EACjCC,OAAO,EACPC,MAAM,EACNC,EAAE,EACFC,UAAU,EACVC,UAAU,CAAC,CAAC,EACP;IACL,4CAA4C;IAC5C,MAAMC,cAA2B,CAAC;IAElCJ,OAAOK,OAAO,CAAC,CAACC,eAAeC;QAC7BC,OAAOC,OAAO,CAACH,eAAeD,OAAO,CAAC,CAAC,CAACK,WAAWC,UAAU;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GAAG;YACvBW,eAAe,MAAM9B,QAAQ+B,MAAM,CAAC;gBAClC7B;gBACAS;gBACAqB,QAAQX,IAAIP,IAAI;YAClB;YAEAgB,aAAaxB,OAAO,CAAC,CAACe;gBACpB,IACE,OAAOA,IAAIQ,KAAK,KAAK,YACpB,CAAA,OAAOR,IAAIL,EAAE,KAAK,YAAY,OAAOK,IAAIL,EAAE,KAAK,QAAO,GACxD;oBACAZ,OAAO,CAACiB,IAAIQ,KAAK,CAAC,GAAGR,IAAIL,EAAE;gBAC7B;YACF;QACF;QAEA,qBAAqB;QACrB,IAAIhB,QAAQiC,MAAM,CAAC,GAAGtB,YAAYX,QAAQkC,aAAa,EAAE,CAAC,IAAIb,IAAIR,OAAO,CAACM,MAAM,GAAG,GAAG;YACpF,IAAI,CAACE,IAAIR,OAAO,CAAC,EAAE,CAACS,SAAS,EAAE;gBAC7BD,IAAIR,OAAO,GAAGQ,IAAIR,OAAO,CAACsB,GAAG,CAAC,CAACC;oBAC7B,IAAI,OAAOA,UAAUV,YAAY,KAAK,YAAY;wBAChDU,UAAUd,SAAS,GAAGc,UAAUV,YAAY,CAACI;wBAC7C,OAAOM,UAAUV,YAAY;oBAC/B;oBACA,OAAOU;gBACT;YACF;YACA,MAAMpC,QAAQ+B,MAAM,CAAC;gBACnB7B;gBACAS,WAAW,GAAGA,YAAYX,QAAQkC,aAAa,EAAE;gBACjDF,QAAQX,IAAIR,OAAO;YACrB;QACF;QAEA,0DAA0D;QAC1D,IAAIQ,IAAIpB,MAAM,CAACkB,MAAM,GAAG,GAAG;YACzB,MAAMpB,aAAa;gBACjBC;gBACAC,QAAQoB,IAAIpB,MAAM;gBAClBC;gBACAC,YAAY2B;YACd;QACF;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/upsertRow/insertArrays.ts"],"sourcesContent":["import type { ArrayRowToInsert } from '../transform/write/types.js'\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n parentRows: Record<string, unknown>[]\n uuidMap?: Record<string, number | string>\n}\n\ntype RowsByTable = {\n [tableName: string]: {\n arrays: {\n [tableName: string]: ArrayRowToInsert[]\n }[]\n locales: Record<string, unknown>[]\n rows: Record<string, unknown>[]\n }\n}\n\nexport const insertArrays = async ({\n adapter,\n arrays,\n db,\n parentRows,\n uuidMap = {},\n}: Args): Promise<void> => {\n // Maintain a map of flattened rows by table\n const rowsByTable: RowsByTable = {}\n\n arrays.forEach((arraysByTable, parentRowIndex) => {\n Object.entries(arraysByTable).forEach(([tableName, arrayRows]) => {\n // If the table doesn't exist in map, initialize it\n if (!rowsByTable[tableName]) {\n rowsByTable[tableName] = {\n arrays: [],\n locales: [],\n rows: [],\n }\n }\n\n const parentID = parentRows[parentRowIndex].id\n\n // Add any sub arrays that need to be created\n // We will call this recursively below\n arrayRows.forEach((arrayRow, i) => {\n if (Object.keys(arrayRow.arrays).length > 0) {\n rowsByTable[tableName].arrays.push(arrayRow.arrays)\n }\n\n // Set up parent IDs for both row and locale row\n arrayRow.row._parentID = parentID\n rowsByTable[tableName].rows.push(arrayRow.row)\n\n Object.entries(arrayRow.locales).forEach(([arrayRowLocale, arrayRowLocaleData]) => {\n arrayRowLocaleData._parentID = arrayRow.row.id\n arrayRowLocaleData._locale = arrayRowLocale\n rowsByTable[tableName].locales.push(arrayRowLocaleData)\n if (!arrayRow.row.id) {\n arrayRowLocaleData._getParentID = (rows: { _uuid: string; id: number }[]) => {\n const { id } = rows.find((each) => each._uuid === arrayRow.row._uuid)\n return id\n }\n }\n })\n })\n })\n })\n\n // Insert all corresponding arrays\n // (one insert per array table)\n for (const [tableName, row] of Object.entries(rowsByTable)) {\n // the nested arrays need the ID for the parentID foreign key\n let insertedRows: Args['parentRows']\n if (row.rows.length > 0) {\n insertedRows = await adapter.insert({\n db,\n tableName,\n values: row.rows,\n })\n\n insertedRows.forEach((row) => {\n if (\n typeof row._uuid === 'string' &&\n (typeof row.id === 'string' || typeof row.id === 'number')\n ) {\n uuidMap[row._uuid] = row.id\n }\n })\n }\n\n // Insert locale rows\n if (adapter.tables[`${tableName}${adapter.localesSuffix}`] && row.locales.length > 0) {\n if (!row.locales[0]._parentID) {\n row.locales = row.locales.map((localeRow) => {\n if (typeof localeRow._getParentID === 'function') {\n localeRow._parentID = localeRow._getParentID(insertedRows)\n delete localeRow._getParentID\n }\n return localeRow\n })\n }\n await adapter.insert({\n db,\n tableName: `${tableName}${adapter.localesSuffix}`,\n values: row.locales,\n })\n }\n\n // If there are sub arrays, call this function recursively\n if (row.arrays.length > 0) {\n await insertArrays({\n adapter,\n arrays: row.arrays,\n db,\n parentRows: insertedRows,\n })\n }\n }\n}\n"],"names":["insertArrays","adapter","arrays","db","parentRows","uuidMap","rowsByTable","forEach","arraysByTable","parentRowIndex","Object","entries","tableName","arrayRows","locales","rows","parentID","id","arrayRow","i","keys","length","push","row","_parentID","arrayRowLocale","arrayRowLocaleData","_locale","_getParentID","find","each","_uuid","insertedRows","insert","values","tables","localesSuffix","map","localeRow"],"mappings":"AAuBA,OAAO,MAAMA,eAAe,OAAO,EACjCC,OAAO,EACPC,MAAM,EACNC,EAAE,EACFC,UAAU,EACVC,UAAU,CAAC,CAAC,EACP;IACL,4CAA4C;IAC5C,MAAMC,cAA2B,CAAC;IAElCJ,OAAOK,OAAO,CAAC,CAACC,eAAeC;QAC7BC,OAAOC,OAAO,CAACH,eAAeD,OAAO,CAAC,CAAC,CAACK,WAAWC,UAAU;YAC3D,mDAAmD;YACnD,IAAI,CAACP,WAAW,CAACM,UAAU,EAAE;gBAC3BN,WAAW,CAACM,UAAU,GAAG;oBACvBV,QAAQ,EAAE;oBACVY,SAAS,EAAE;oBACXC,MAAM,EAAE;gBACV;YACF;YAEA,MAAMC,WAAWZ,UAAU,CAACK,eAAe,CAACQ,EAAE;YAE9C,6CAA6C;YAC7C,sCAAsC;YACtC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QAAO,GACxD;oBACAZ,OAAO,CAACkB,IAAIQ,KAAK,CAAC,GAAGR,IAAIN,EAAE;gBAC7B;YACF;QACF;QAEA,qBAAqB;QACrB,IAAIhB,QAAQkC,MAAM,CAAC,GAAGvB,YAAYX,QAAQmC,aAAa,EAAE,CAAC,IAAIb,IAAIT,OAAO,CAACO,MAAM,GAAG,GAAG;YACpF,IAAI,CAACE,IAAIT,OAAO,CAAC,EAAE,CAACU,SAAS,EAAE;gBAC7BD,IAAIT,OAAO,GAAGS,IAAIT,OAAO,CAACuB,GAAG,CAAC,CAACC;oBAC7B,IAAI,OAAOA,UAAUV,YAAY,KAAK,YAAY;wBAChDU,UAAUd,SAAS,GAAGc,UAAUV,YAAY,CAACI;wBAC7C,OAAOM,UAAUV,YAAY;oBAC/B;oBACA,OAAOU;gBACT;YACF;YACA,MAAMrC,QAAQgC,MAAM,CAAC;gBACnB9B;gBACAS,WAAW,GAAGA,YAAYX,QAAQmC,aAAa,EAAE;gBACjDF,QAAQX,IAAIT,OAAO;YACrB;QACF;QAEA,0DAA0D;QAC1D,IAAIS,IAAIrB,MAAM,CAACmB,MAAM,GAAG,GAAG;YACzB,MAAMrB,aAAa;gBACjBC;gBACAC,QAAQqB,IAAIrB,MAAM;gBAClBC;gBACAC,YAAY4B;YACd;QACF;IACF;AACF,EAAC"}
@@ -10,7 +10,7 @@ type BaseArgs = {
10
10
  * When true, skips reading the data back from the database and returns the input data
11
11
  * @default false
12
12
  */
13
- ignoreResult?: 'idOnly' | boolean;
13
+ ignoreResult?: boolean | 'idOnly';
14
14
  joinQuery?: JoinQuery;
15
15
  path?: string;
16
16
  req?: Partial<PayloadRequest>;
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/upsertRow/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AACtC,OAAO,KAAK,EAAE,cAAc,EAAE,SAAS,EAAE,cAAc,EAAE,U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AEZ,MAAM,MAAM,IAAI,GAAG,UAAU,GAAG,UAAU,CAAA"}
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/upsertRow/types.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { FlattenedField, JoinQuery, PayloadRequest, SelectType } from 'payload'\n\nimport type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js'\n\ntype BaseArgs = {\n adapter: DrizzleAdapter\n data: Record<string, unknown>\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n fields: FlattenedField[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: 'idOnly' | boolean\n joinQuery?: JoinQuery\n path?: string\n req?: Partial<PayloadRequest>\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n joinQuery?: never\n operation: 'create'\n select?: SelectType\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n joinQuery?: JoinQuery\n operation: 'update'\n select?: SelectType\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"AAuCA,WAA0C"}
1
+ {"version":3,"sources":["../../src/upsertRow/types.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { FlattenedField, JoinQuery, PayloadRequest, SelectType } from 'payload'\n\nimport type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js'\n\ntype BaseArgs = {\n adapter: DrizzleAdapter\n data: Record<string, unknown>\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n fields: FlattenedField[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: boolean | 'idOnly'\n joinQuery?: JoinQuery\n path?: string\n req?: Partial<PayloadRequest>\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n joinQuery?: never\n operation: 'create'\n select?: SelectType\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n joinQuery?: JoinQuery\n operation: 'update'\n select?: SelectType\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"AAuCA,WAA0C"}
@@ -1 +1 @@
1
- {"version":3,"file":"createBlocksMap.d.ts","sourceRoot":"","sources":["../../src/utilities/createBlocksMap.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG;IACtB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CAC1C,CAAA;AAED,eAAO,MAAM,eAAe,SAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,SAyC/D,CAAA"}
1
+ {"version":3,"file":"createBlocksMap.d.ts","sourceRoot":"","sources":["../../src/utilities/createBlocksMap.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,GAAG;IACtB,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CAC1C,CAAA;AAED,eAAO,MAAM,eAAe,SAAU,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAG,SAqC/D,CAAA"}
@@ -2,11 +2,7 @@ export const createBlocksMap = (data)=>{
2
2
  const blocksMap = {};
3
3
  Object.entries(data).forEach(([key, rows])=>{
4
4
  if (key.startsWith('_blocks_') && Array.isArray(rows)) {
5
- let blockType = key.replace('_blocks_', '');
6
- const parsed = blockType.split('_');
7
- if (parsed.length === 2 && Number.isInteger(Number(parsed[1]))) {
8
- blockType = parsed[0];
9
- }
5
+ const blockType = key.replace('_blocks_', '');
10
6
  rows.forEach((row)=>{
11
7
  if ('_path' in row) {
12
8
  if (!(row._path in blocksMap)) {
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/createBlocksMap.ts"],"sourcesContent":["export type BlocksMap = {\n [path: string]: Record<string, unknown>[]\n}\n\nexport const createBlocksMap = (data: Record<string, unknown>): BlocksMap => {\n const blocksMap: BlocksMap = {}\n\n Object.entries(data).forEach(([key, rows]) => {\n if (key.startsWith('_blocks_') && Array.isArray(rows)) {\n let blockType = key.replace('_blocks_', '')\n const parsed = blockType.split('_')\n if (parsed.length === 2 && Number.isInteger(Number(parsed[1]))) {\n blockType = parsed[0]\n }\n\n rows.forEach((row) => {\n if ('_path' in row) {\n if (!(row._path in blocksMap)) {\n blocksMap[row._path] = []\n }\n\n row.blockType = blockType\n blocksMap[row._path].push(row)\n\n delete row._path\n }\n })\n\n delete data[key]\n }\n })\n\n Object.entries(blocksMap).reduce((sortedBlocksMap, [path, blocks]) => {\n sortedBlocksMap[path] = blocks.sort((a, b) => {\n if (typeof a._order === 'number' && typeof b._order === 'number') {\n return a._order - b._order\n }\n\n return 0\n })\n\n return sortedBlocksMap\n }, {})\n\n return blocksMap\n}\n"],"names":["createBlocksMap","data","blocksMap","Object","entries","forEach","key","rows","startsWith","Array","isArray","blockType","replace","parsed","split","length","Number","isInteger","row","_path","push","reduce","sortedBlocksMap","path","blocks","sort","a","b","_order"],"mappings":"AAIA,OAAO,MAAMA,kBAAkB,CAACC;IAC9B,MAAMC,YAAuB,CAAC;IAE9BC,OAAOC,OAAO,CAACH,MAAMI,OAAO,CAAC,CAAC,CAACC,KAAKC,KAAK;QACvC,IAAID,IAAIE,UAAU,CAAC,eAAeC,MAAMC,OAAO,CAACH,OAAO;YACrD,IAAII,YAAYL,IAAIM,OAAO,CAAC,YAAY;YACxC,MAAMC,SAASF,UAAUG,KAAK,CAAC;YAC/B,IAAID,OAAOE,MAAM,KAAK,KAAKC,OAAOC,SAAS,CAACD,OAAOH,MAAM,CAAC,EAAE,IAAI;gBAC9DF,YAAYE,MAAM,CAAC,EAAE;YACvB;YAEAN,KAAKF,OAAO,CAAC,CAACa;gBACZ,IAAI,WAAWA,KAAK;oBAClB,IAAI,CAAEA,CAAAA,IAAIC,KAAK,IAAIjB,SAAQ,GAAI;wBAC7BA,SAAS,CAACgB,IAAIC,KAAK,CAAC,GAAG,EAAE;oBAC3B;oBAEAD,IAAIP,SAAS,GAAGA;oBAChBT,SAAS,CAACgB,IAAIC,KAAK,CAAC,CAACC,IAAI,CAACF;oBAE1B,OAAOA,IAAIC,KAAK;gBAClB;YACF;YAEA,OAAOlB,IAAI,CAACK,IAAI;QAClB;IACF;IAEAH,OAAOC,OAAO,CAACF,WAAWmB,MAAM,CAAC,CAACC,iBAAiB,CAACC,MAAMC,OAAO;QAC/DF,eAAe,CAACC,KAAK,GAAGC,OAAOC,IAAI,CAAC,CAACC,GAAGC;YACtC,IAAI,OAAOD,EAAEE,MAAM,KAAK,YAAY,OAAOD,EAAEC,MAAM,KAAK,UAAU;gBAChE,OAAOF,EAAEE,MAAM,GAAGD,EAAEC,MAAM;YAC5B;YAEA,OAAO;QACT;QAEA,OAAON;IACT,GAAG,CAAC;IAEJ,OAAOpB;AACT,EAAC"}
1
+ {"version":3,"sources":["../../src/utilities/createBlocksMap.ts"],"sourcesContent":["export type BlocksMap = {\n [path: string]: Record<string, unknown>[]\n}\n\nexport const createBlocksMap = (data: Record<string, unknown>): BlocksMap => {\n const blocksMap: BlocksMap = {}\n\n Object.entries(data).forEach(([key, rows]) => {\n if (key.startsWith('_blocks_') && Array.isArray(rows)) {\n const blockType = key.replace('_blocks_', '')\n\n rows.forEach((row) => {\n if ('_path' in row) {\n if (!(row._path in blocksMap)) {\n blocksMap[row._path] = []\n }\n\n row.blockType = blockType\n blocksMap[row._path].push(row)\n\n delete row._path\n }\n })\n\n delete data[key]\n }\n })\n\n Object.entries(blocksMap).reduce((sortedBlocksMap, [path, blocks]) => {\n sortedBlocksMap[path] = blocks.sort((a, b) => {\n if (typeof a._order === 'number' && typeof b._order === 'number') {\n return a._order - b._order\n }\n\n return 0\n })\n\n return sortedBlocksMap\n }, {})\n\n return blocksMap\n}\n"],"names":["createBlocksMap","data","blocksMap","Object","entries","forEach","key","rows","startsWith","Array","isArray","blockType","replace","row","_path","push","reduce","sortedBlocksMap","path","blocks","sort","a","b","_order"],"mappings":"AAIA,OAAO,MAAMA,kBAAkB,CAACC;IAC9B,MAAMC,YAAuB,CAAC;IAE9BC,OAAOC,OAAO,CAACH,MAAMI,OAAO,CAAC,CAAC,CAACC,KAAKC,KAAK;QACvC,IAAID,IAAIE,UAAU,CAAC,eAAeC,MAAMC,OAAO,CAACH,OAAO;YACrD,MAAMI,YAAYL,IAAIM,OAAO,CAAC,YAAY;YAE1CL,KAAKF,OAAO,CAAC,CAACQ;gBACZ,IAAI,WAAWA,KAAK;oBAClB,IAAI,CAAEA,CAAAA,IAAIC,KAAK,IAAIZ,SAAQ,GAAI;wBAC7BA,SAAS,CAACW,IAAIC,KAAK,CAAC,GAAG,EAAE;oBAC3B;oBAEAD,IAAIF,SAAS,GAAGA;oBAChBT,SAAS,CAACW,IAAIC,KAAK,CAAC,CAACC,IAAI,CAACF;oBAE1B,OAAOA,IAAIC,KAAK;gBAClB;YACF;YAEA,OAAOb,IAAI,CAACK,IAAI;QAClB;IACF;IAEAH,OAAOC,OAAO,CAACF,WAAWc,MAAM,CAAC,CAACC,iBAAiB,CAACC,MAAMC,OAAO;QAC/DF,eAAe,CAACC,KAAK,GAAGC,OAAOC,IAAI,CAAC,CAACC,GAAGC;YACtC,IAAI,OAAOD,EAAEE,MAAM,KAAK,YAAY,OAAOD,EAAEC,MAAM,KAAK,UAAU;gBAChE,OAAOF,EAAEE,MAAM,GAAGD,EAAEC,MAAM;YAC5B;YAEA,OAAO;QACT;QAEA,OAAON;IACT,GAAG,CAAC;IAEJ,OAAOf;AACT,EAAC"}
@@ -1,4 +1,4 @@
1
- import type { Block, FlattenedBlock } from 'payload';
1
+ import type { Block } from 'payload';
2
2
  import type { RawTable } from '../types.js';
3
3
  type Args = {
4
4
  block: Block;
@@ -11,12 +11,6 @@ type Args = {
11
11
  table: RawTable;
12
12
  tableLocales?: RawTable;
13
13
  };
14
- /**
15
- * returns true if all the fields in a block are identical to the existing table
16
- */
17
- export declare const validateExistingBlockIsIdentical: ({ block, localized, parentIsLocalized, table, tableLocales, }: Args) => boolean;
18
- export declare const InternalBlockTableNameIndex: unique symbol;
19
- export declare const setInternalBlockIndex: (block: FlattenedBlock, index: number) => void;
20
- export declare const resolveBlockTableName: (block: FlattenedBlock, originalTableName: string) => string;
14
+ export declare const validateExistingBlockIsIdentical: ({ block, localized, parentIsLocalized, rootTableName, table, tableLocales, }: Args) => void;
21
15
  export {};
22
16
  //# sourceMappingURL=validateExistingBlockIsIdentical.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"validateExistingBlockIsIdentical.d.ts","sourceRoot":"","sources":["../../src/utilities/validateExistingBlockIsIdentical.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAS,cAAc,EAAE,MAAM,SAAS,CAAA;AAS3D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,KAAK,IAAI,GAAG;IACV,KAAK,EAAE,KAAK,CAAA;IACZ,SAAS,EAAE,OAAO,CAAA;IAClB;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,aAAa,EAAE,MAAM,CAAA;IACrB,KAAK,EAAE,QAAQ,CAAA;IACf,YAAY,CAAC,EAAE,QAAQ,CAAA;CACxB,CAAA;AA+DD;;GAEG;AACH,eAAO,MAAM,gCAAgC,kEAM1C,IAAI,KAAG,OAwBT,CAAA;AAED,eAAO,MAAM,2BAA2B,eAAwC,CAAA;AAChF,eAAO,MAAM,qBAAqB,UAAW,cAAc,SAAS,MAAM,SAEzE,CAAA;AAED,eAAO,MAAM,qBAAqB,UAAW,cAAc,qBAAqB,MAAM,WAMrF,CAAA"}
1
+ {"version":3,"file":"validateExistingBlockIsIdentical.d.ts","sourceRoot":"","sources":["../../src/utilities/validateExistingBlockIsIdentical.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAS,MAAM,SAAS,CAAA;AAU3C,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,aAAa,CAAA;AAE3C,KAAK,IAAI,GAAG;IACV,KAAK,EAAE,KAAK,CAAA;IACZ,SAAS,EAAE,OAAO,CAAA;IAClB;;OAEG;IACH,iBAAiB,CAAC,EAAE,OAAO,CAAA;IAC3B,aAAa,EAAE,MAAM,CAAA;IACrB,KAAK,EAAE,QAAQ,CAAA;IACf,YAAY,CAAC,EAAE,QAAQ,CAAA;CACxB,CAAA;AA+DD,eAAO,MAAM,gCAAgC,iFAO1C,IAAI,KAAG,IAkCT,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { InvalidConfiguration } from 'payload';
1
2
  import { fieldAffectsData, fieldHasSubFields, fieldShouldBeLocalized, tabHasName } from 'payload/shared';
2
3
  const getFlattenedFieldNames = (args)=>{
3
4
  const { fields, parentIsLocalized, prefix = '' } = args;
@@ -58,9 +59,7 @@ const getFlattenedFieldNames = (args)=>{
58
59
  return fieldsToUse;
59
60
  }, []);
60
61
  };
61
- /**
62
- * returns true if all the fields in a block are identical to the existing table
63
- */ export const validateExistingBlockIsIdentical = ({ block, localized, parentIsLocalized, table, tableLocales })=>{
62
+ export const validateExistingBlockIsIdentical = ({ block, localized, parentIsLocalized, rootTableName, table, tableLocales })=>{
64
63
  const fieldNames = getFlattenedFieldNames({
65
64
  fields: block.fields,
66
65
  parentIsLocalized: parentIsLocalized || localized
@@ -82,19 +81,11 @@ const getFlattenedFieldNames = (args)=>{
82
81
  }
83
82
  });
84
83
  if (missingField) {
85
- return false;
84
+ throw new InvalidConfiguration(`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One block includes the field ${typeof missingField === 'string' ? missingField : missingField.name}, while the other block does not.`);
86
85
  }
87
- return Boolean(localized) === Boolean(table.columns._locale);
88
- };
89
- export const InternalBlockTableNameIndex = Symbol('InternalBlockTableNameIndex');
90
- export const setInternalBlockIndex = (block, index)=>{
91
- block[InternalBlockTableNameIndex] = index;
92
- };
93
- export const resolveBlockTableName = (block, originalTableName)=>{
94
- if (!block[InternalBlockTableNameIndex]) {
95
- return originalTableName;
86
+ if (Boolean(localized) !== Boolean(table.columns._locale)) {
87
+ throw new InvalidConfiguration(`The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One is localized, but another is not. Block schemas of the same name must match exactly.`);
96
88
  }
97
- return `${originalTableName}_${block[InternalBlockTableNameIndex]}`;
98
89
  };
99
90
 
100
91
  //# sourceMappingURL=validateExistingBlockIsIdentical.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/utilities/validateExistingBlockIsIdentical.ts"],"sourcesContent":["import type { Block, Field, FlattenedBlock } from 'payload'\n\nimport {\n fieldAffectsData,\n fieldHasSubFields,\n fieldShouldBeLocalized,\n tabHasName,\n} from 'payload/shared'\n\nimport type { RawTable } from '../types.js'\n\ntype Args = {\n block: Block\n localized: boolean\n /**\n * @todo make required in v4.0. Usually you'd wanna pass this in\n */\n parentIsLocalized?: boolean\n rootTableName: string\n table: RawTable\n tableLocales?: RawTable\n}\n\nconst getFlattenedFieldNames = (args: {\n fields: Field[]\n parentIsLocalized: boolean\n prefix?: string\n}): { localized?: boolean; name: string }[] => {\n const { fields, parentIsLocalized, prefix = '' } = args\n return fields.reduce((fieldsToUse, field) => {\n let fieldPrefix = prefix\n\n if (\n ['array', 'blocks', 'relationship', 'upload'].includes(field.type) ||\n ('hasMany' in field && field.hasMany === true)\n ) {\n return fieldsToUse\n }\n\n if (fieldHasSubFields(field)) {\n fieldPrefix = 'name' in field ? `${prefix}${field.name}_` : prefix\n return [\n ...fieldsToUse,\n ...getFlattenedFieldNames({\n fields: field.fields,\n parentIsLocalized: parentIsLocalized || ('localized' in field && field.localized),\n prefix: fieldPrefix,\n }),\n ]\n }\n\n if (field.type === 'tabs') {\n return [\n ...fieldsToUse,\n ...field.tabs.reduce((tabFields, tab) => {\n fieldPrefix = 'name' in tab ? `${prefix}_${tab.name}` : prefix\n return [\n ...tabFields,\n ...(tabHasName(tab)\n ? [{ ...tab, type: 'tab' }]\n : getFlattenedFieldNames({\n fields: tab.fields,\n parentIsLocalized: parentIsLocalized || tab.localized,\n prefix: fieldPrefix,\n })),\n ]\n }, []),\n ]\n }\n\n if (fieldAffectsData(field)) {\n return [\n ...fieldsToUse,\n {\n name: `${fieldPrefix}${field.name}`,\n localized: fieldShouldBeLocalized({ field, parentIsLocalized }),\n },\n ]\n }\n\n return fieldsToUse\n }, [])\n}\n\n/**\n * returns true if all the fields in a block are identical to the existing table\n */\nexport const validateExistingBlockIsIdentical = ({\n block,\n localized,\n parentIsLocalized,\n table,\n tableLocales,\n}: Args): boolean => {\n const fieldNames = getFlattenedFieldNames({\n fields: block.fields,\n parentIsLocalized: parentIsLocalized || localized,\n })\n\n const missingField =\n // ensure every field from the config is in the matching table\n fieldNames.find(({ name, localized }) => {\n const fieldTable = localized && tableLocales ? tableLocales : table\n return Object.keys(fieldTable.columns).indexOf(name) === -1\n }) ||\n // ensure every table column is matched for every field from the config\n Object.keys(table).find((fieldName) => {\n if (!['_locale', '_order', '_parentID', '_path', '_uuid'].includes(fieldName)) {\n return fieldNames.findIndex((field) => field.name) === -1\n }\n })\n\n if (missingField) {\n return false\n }\n\n return Boolean(localized) === Boolean(table.columns._locale)\n}\n\nexport const InternalBlockTableNameIndex = Symbol('InternalBlockTableNameIndex')\nexport const setInternalBlockIndex = (block: FlattenedBlock, index: number) => {\n block[InternalBlockTableNameIndex] = index\n}\n\nexport const resolveBlockTableName = (block: FlattenedBlock, originalTableName: string) => {\n if (!block[InternalBlockTableNameIndex]) {\n return originalTableName\n }\n\n return `${originalTableName}_${block[InternalBlockTableNameIndex]}`\n}\n"],"names":["fieldAffectsData","fieldHasSubFields","fieldShouldBeLocalized","tabHasName","getFlattenedFieldNames","args","fields","parentIsLocalized","prefix","reduce","fieldsToUse","field","fieldPrefix","includes","type","hasMany","name","localized","tabs","tabFields","tab","validateExistingBlockIsIdentical","block","table","tableLocales","fieldNames","missingField","find","fieldTable","Object","keys","columns","indexOf","fieldName","findIndex","Boolean","_locale","InternalBlockTableNameIndex","Symbol","setInternalBlockIndex","index","resolveBlockTableName","originalTableName"],"mappings":"AAEA,SACEA,gBAAgB,EAChBC,iBAAiB,EACjBC,sBAAsB,EACtBC,UAAU,QACL,iBAAgB;AAgBvB,MAAMC,yBAAyB,CAACC;IAK9B,MAAM,EAAEC,MAAM,EAAEC,iBAAiB,EAAEC,SAAS,EAAE,EAAE,GAAGH;IACnD,OAAOC,OAAOG,MAAM,CAAC,CAACC,aAAaC;QACjC,IAAIC,cAAcJ;QAElB,IACE;YAAC;YAAS;YAAU;YAAgB;SAAS,CAACK,QAAQ,CAACF,MAAMG,IAAI,KAChE,aAAaH,SAASA,MAAMI,OAAO,KAAK,MACzC;YACA,OAAOL;QACT;QAEA,IAAIT,kBAAkBU,QAAQ;YAC5BC,cAAc,UAAUD,QAAQ,GAAGH,SAASG,MAAMK,IAAI,CAAC,CAAC,CAAC,GAAGR;YAC5D,OAAO;mBACFE;mBACAN,uBAAuB;oBACxBE,QAAQK,MAAML,MAAM;oBACpBC,mBAAmBA,qBAAsB,eAAeI,SAASA,MAAMM,SAAS;oBAChFT,QAAQI;gBACV;aACD;QACH;QAEA,IAAID,MAAMG,IAAI,KAAK,QAAQ;YACzB,OAAO;mBACFJ;mBACAC,MAAMO,IAAI,CAACT,MAAM,CAAC,CAACU,WAAWC;oBAC/BR,cAAc,UAAUQ,MAAM,GAAGZ,OAAO,CAAC,EAAEY,IAAIJ,IAAI,EAAE,GAAGR;oBACxD,OAAO;2BACFW;2BACChB,WAAWiB,OACX;4BAAC;gCAAE,GAAGA,GAAG;gCAAEN,MAAM;4BAAM;yBAAE,GACzBV,uBAAuB;4BACrBE,QAAQc,IAAId,MAAM;4BAClBC,mBAAmBA,qBAAqBa,IAAIH,SAAS;4BACrDT,QAAQI;wBACV;qBACL;gBACH,GAAG,EAAE;aACN;QACH;QAEA,IAAIZ,iBAAiBW,QAAQ;YAC3B,OAAO;mBACFD;gBACH;oB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,EAAE;AACrE,EAAC"}
1
+ {"version":3,"sources":["../../src/utilities/validateExistingBlockIsIdentical.ts"],"sourcesContent":["import type { Block, Field } from 'payload'\n\nimport { InvalidConfiguration } from 'payload'\nimport {\n fieldAffectsData,\n fieldHasSubFields,\n fieldShouldBeLocalized,\n tabHasName,\n} from 'payload/shared'\n\nimport type { RawTable } from '../types.js'\n\ntype Args = {\n block: Block\n localized: boolean\n /**\n * @todo make required in v4.0. Usually you'd wanna pass this in\n */\n parentIsLocalized?: boolean\n rootTableName: string\n table: RawTable\n tableLocales?: RawTable\n}\n\nconst getFlattenedFieldNames = (args: {\n fields: Field[]\n parentIsLocalized: boolean\n prefix?: string\n}): { localized?: boolean; name: string }[] => {\n const { fields, parentIsLocalized, prefix = '' } = args\n return fields.reduce((fieldsToUse, field) => {\n let fieldPrefix = prefix\n\n if (\n ['array', 'blocks', 'relationship', 'upload'].includes(field.type) ||\n ('hasMany' in field && field.hasMany === true)\n ) {\n return fieldsToUse\n }\n\n if (fieldHasSubFields(field)) {\n fieldPrefix = 'name' in field ? `${prefix}${field.name}_` : prefix\n return [\n ...fieldsToUse,\n ...getFlattenedFieldNames({\n fields: field.fields,\n parentIsLocalized: parentIsLocalized || ('localized' in field && field.localized),\n prefix: fieldPrefix,\n }),\n ]\n }\n\n if (field.type === 'tabs') {\n return [\n ...fieldsToUse,\n ...field.tabs.reduce((tabFields, tab) => {\n fieldPrefix = 'name' in tab ? `${prefix}_${tab.name}` : prefix\n return [\n ...tabFields,\n ...(tabHasName(tab)\n ? [{ ...tab, type: 'tab' }]\n : getFlattenedFieldNames({\n fields: tab.fields,\n parentIsLocalized: parentIsLocalized || tab.localized,\n prefix: fieldPrefix,\n })),\n ]\n }, []),\n ]\n }\n\n if (fieldAffectsData(field)) {\n return [\n ...fieldsToUse,\n {\n name: `${fieldPrefix}${field.name}`,\n localized: fieldShouldBeLocalized({ field, parentIsLocalized }),\n },\n ]\n }\n\n return fieldsToUse\n }, [])\n}\n\nexport const validateExistingBlockIsIdentical = ({\n block,\n localized,\n parentIsLocalized,\n rootTableName,\n table,\n tableLocales,\n}: Args): void => {\n const fieldNames = getFlattenedFieldNames({\n fields: block.fields,\n parentIsLocalized: parentIsLocalized || localized,\n })\n\n const missingField =\n // ensure every field from the config is in the matching table\n fieldNames.find(({ name, localized }) => {\n const fieldTable = localized && tableLocales ? tableLocales : table\n return Object.keys(fieldTable.columns).indexOf(name) === -1\n }) ||\n // ensure every table column is matched for every field from the config\n Object.keys(table).find((fieldName) => {\n if (!['_locale', '_order', '_parentID', '_path', '_uuid'].includes(fieldName)) {\n return fieldNames.findIndex((field) => field.name) === -1\n }\n })\n\n if (missingField) {\n throw new InvalidConfiguration(\n `The table ${rootTableName} has multiple blocks with slug ${\n block.slug\n }, but the schemas do not match. One block includes the field ${\n typeof missingField === 'string' ? missingField : missingField.name\n }, while the other block does not.`,\n )\n }\n\n if (Boolean(localized) !== Boolean(table.columns._locale)) {\n throw new InvalidConfiguration(\n `The table ${rootTableName} has multiple blocks with slug ${block.slug}, but the schemas do not match. One is localized, but another is not. Block schemas of the same name must match exactly.`,\n )\n }\n}\n"],"names":["InvalidConfiguration","fieldAffectsData","fieldHasSubFields","fieldShouldBeLocalized","tabHasName","getFlattenedFieldNames","args","fields","parentIsLocalized","prefix","reduce","fieldsToUse","field","fieldPrefix","includes","type","hasMany","name","localized","tabs","tabFields","tab","validateExistingBlockIsIdentical","block","rootTableName","table","tableLocales","fieldNames","missingField","find","fieldTable","Object","keys","columns","indexOf","fieldName","findIndex","slug","Boolean","_locale"],"mappings":"AAEA,SAASA,oBAAoB,QAAQ,UAAS;AAC9C,SACEC,gBAAgB,EAChBC,iBAAiB,EACjBC,sBAAsB,EACtBC,UAAU,QACL,iBAAgB;AAgBvB,MAAMC,yBAAyB,CAACC;IAK9B,MAAM,EAAEC,MAAM,EAAEC,iBAAiB,EAAEC,SAAS,EAAE,EAAE,GAAGH;IACnD,OAAOC,OAAOG,MAAM,CAAC,CAACC,aAAaC;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;QACpBC,mBAAmBA,qBAAqBU;IAC1C;IAEA,MAAMU,eACJ,8DAA8D;IAC9DD,WAAWE,IAAI,CAAC,CAAC,EAAEZ,IAAI,EAAEC,SAAS,EAAE;QAClC,MAAMY,aAAaZ,aAAaQ,eAAeA,eAAeD;QAC9D,OAAOM,OAAOC,IAAI,CAACF,WAAWG,OAAO,EAAEC,OAAO,CAACjB,UAAU,CAAC;IAC5D,MACA,uEAAuE;IACvEc,OAAOC,IAAI,CAACP,OAAOI,IAAI,CAAC,CAACM;QACvB,IAAI,CAAC;YAAC;YAAW;YAAU;YAAa;YAAS;SAAQ,CAACrB,QAAQ,CAACqB,YAAY;YAC7E,OAAOR,WAAWS,SAAS,CAAC,CAACxB,QAAUA,MAAMK,IAAI,MAAM,CAAC;QAC1D;IACF;IAEF,IAAIW,cAAc;QAChB,MAAM,IAAI5B,qBACR,CAAC,UAAU,EAAEwB,cAAc,+BAA+B,EACxDD,MAAMc,IAAI,CACX,6DAA6D,EAC5D,OAAOT,iBAAiB,WAAWA,eAAeA,aAAaX,IAAI,CACpE,iCAAiC,CAAC;IAEvC;IAEA,IAAIqB,QAAQpB,eAAeoB,QAAQb,MAAMQ,OAAO,CAACM,OAAO,GAAG;QACzD,MAAM,IAAIvC,qBACR,CAAC,UAAU,EAAEwB,cAAc,+BAA+B,EAAED,MAAMc,IAAI,CAAC,wHAAwH,CAAC;IAEpM;AACF,EAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payloadcms/drizzle",
3
- "version": "3.39.0-canary.7",
3
+ "version": "3.39.0-internal.1c212c9",
4
4
  "description": "A library of shared functions used by different payload database adapters",
5
5
  "homepage": "https://payloadcms.com",
6
6
  "repository": {
@@ -54,10 +54,10 @@
54
54
  "@types/pg": "8.10.2",
55
55
  "@types/to-snake-case": "1.0.0",
56
56
  "@payloadcms/eslint-config": "3.28.0",
57
- "payload": "3.39.0-canary.7"
57
+ "payload": "3.39.0-internal.1c212c9"
58
58
  },
59
59
  "peerDependencies": {
60
- "payload": "3.39.0-canary.7"
60
+ "payload": "3.39.0-internal.1c212c9"
61
61
  },
62
62
  "scripts": {
63
63
  "build": "pnpm build:swc && pnpm build:types",