@payloadcms/drizzle 3.0.0-beta.107 → 3.0.0-beta.109
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/count.js +1 -1
- package/dist/count.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +1 -1
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +4 -2
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createVersion.d.ts +1 -1
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +9 -7
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +5 -3
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.js +1 -1
- package/dist/deleteVersions.js.map +1 -1
- package/dist/find/buildFindManyArgs.d.ts +9 -3
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +4 -1
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/findMany.d.ts +1 -1
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +6 -3
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +6 -3
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +69 -2
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +2 -1
- package/dist/find.js.map +1 -1
- package/dist/findGlobalVersions.js +1 -1
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -1
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +2 -1
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.js +1 -1
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/postgres/countDistinct.d.ts.map +1 -1
- package/dist/postgres/countDistinct.js +3 -3
- package/dist/postgres/countDistinct.js.map +1 -1
- package/dist/postgres/init.d.ts.map +1 -1
- package/dist/postgres/init.js +12 -3
- package/dist/postgres/init.js.map +1 -1
- package/dist/postgres/schema/build.js +4 -3
- package/dist/postgres/schema/build.js.map +1 -1
- package/dist/postgres/schema/traverseFields.d.ts.map +1 -1
- package/dist/postgres/schema/traverseFields.js +24 -3
- package/dist/postgres/schema/traverseFields.js.map +1 -1
- package/dist/postgres/types.d.ts +17 -3
- package/dist/postgres/types.d.ts.map +1 -1
- package/dist/postgres/types.js.map +1 -1
- package/dist/queries/buildAndOrConditions.d.ts +1 -1
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +2 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildOrderBy.d.ts +18 -0
- package/dist/queries/buildOrderBy.d.ts.map +1 -0
- package/dist/queries/buildOrderBy.js +52 -0
- package/dist/queries/buildOrderBy.js.map +1 -0
- package/dist/queries/buildQuery.d.ts +4 -4
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +12 -47
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +3 -2
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +38 -8
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +4 -2
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +3 -2
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +47 -13
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queryDrafts.js +1 -1
- package/dist/queryDrafts.js.map +1 -1
- package/dist/transform/read/index.d.ts +3 -2
- package/dist/transform/read/index.d.ts.map +1 -1
- package/dist/transform/read/index.js +2 -1
- package/dist/transform/read/index.js.map +1 -1
- package/dist/transform/read/traverseFields.d.ts +6 -2
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +49 -3
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +7 -1
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/update.d.ts.map +1 -1
- package/dist/update.js +3 -2
- package/dist/update.js.map +1 -1
- package/dist/updateGlobalVersion.js +2 -2
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateVersion.js +2 -2
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +4 -2
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js +4 -1
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/types.d.ts +4 -1
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/executeSchemaHooks.d.ts +24 -0
- package/dist/utilities/executeSchemaHooks.d.ts.map +1 -0
- package/dist/utilities/executeSchemaHooks.js +21 -0
- package/dist/utilities/executeSchemaHooks.js.map +1 -0
- package/dist/utilities/extendDrizzleTable.d.ts +19 -0
- package/dist/utilities/extendDrizzleTable.d.ts.map +1 -0
- package/dist/utilities/extendDrizzleTable.js +38 -0
- package/dist/utilities/extendDrizzleTable.js.map +1 -0
- package/dist/utilities/getCollectionIdType.d.ts +7 -0
- package/dist/utilities/getCollectionIdType.d.ts.map +1 -0
- package/dist/utilities/getCollectionIdType.js +11 -0
- package/dist/utilities/getCollectionIdType.js.map +1 -0
- package/dist/utilities/isPolymorphicRelationship.d.ts +6 -0
- package/dist/utilities/isPolymorphicRelationship.d.ts.map +1 -0
- package/dist/utilities/isPolymorphicRelationship.js +5 -0
- package/dist/utilities/isPolymorphicRelationship.js.map +1 -0
- 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 operation,\n path = '',\n req,\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 ;[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 selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n if (!selectsToInsert[selectTableName]) {\n selectsToInsert[selectTableName] = []\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 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 })\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 })\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 })\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 await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\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 field: fieldName,\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n },\n req.t,\n )\n } else {\n throw error\n }\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 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 })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","operation","path","req","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","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","blockRows","map","i","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","field","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,EACZC,SAAS,EACTC,OAAO,EAAE,EACTC,GAAG,EACHC,SAAS,EACTC,YAAY,EACZC,KAAK,EACA;IACL,sDAAsD;IACtD,uDAAuD;IACvD,MAAMC,cAAcjB,kBAAkB;QACpCM;QACAC;QACAE;QACAG;QACA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A4G,QAAQ5G,QAAQ6G,OAAO,CAACD,MAAM;QAC9B3G,MAAMuG;QACNrG;IACF;IAEA,OAAOwG;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 joinQuery,\n operation,\n path = '',\n req,\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 ;[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 selectRows.forEach((row) => {\n if (typeof row.parent === 'undefined') {\n row.parent = insertedRow.id\n }\n if (!selectsToInsert[selectTableName]) {\n selectsToInsert[selectTableName] = []\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 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 })\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 })\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 })\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 await adapter.insert({\n db,\n tableName: selectTableName,\n values: tableRows,\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 field: fieldName,\n message: req.t('error:valueMustBeUnique'),\n },\n ],\n },\n req.t,\n )\n } else {\n throw error\n }\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,\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,\n })\n\n return result\n}\n"],"names":["eq","ValidationError","buildFindManyArgs","transform","transformForWrite","deleteExistingArrayRows","deleteExistingRowsByPath","insertArrays","upsertRow","id","adapter","data","db","fields","ignoreResult","joinQuery","operation","path","req","tableName","upsertTarget","where","rowToInsert","insertedRow","target","tables","row","insert","onConflictDoUpdate","set","values","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","blockRows","map","i","blockLocaleIndexMap","blockLocaleRowsToInsert","reduce","acc","blockLocale","blockLocaleData","arrays","parentRows","arrayTableName","tableRows","selectTable","error","code","fieldName","fieldConstraints","constraint","replacement","includes","replacedConstraint","replace","detail","regex","match","key","errors","field","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,EACZC,SAAS,EACTC,SAAS,EACTC,OAAO,EAAE,EACTC,GAAG,EACHC,SAAS,EACTC,YAAY,EACZC,KAAK,EACA;IACL,sDAAsD;IACtD,uDAAuD;IACvD,MAAMC,cAAclB,kBAAkB;QACpCM;QACAC;QACAE;QACAI;QAC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1BO;QACA6G,QAAQ7G,QAAQ8G,OAAO,CAACD,MAAM;QAC9B5G,MAAMwG;QACNtG;QACAE;IACF;IAEA,OAAOuG;AACT,EAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"insertArrays.d.ts","sourceRoot":"","sources":["../../src/upsertRow/insertArrays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErE,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,cAAc,CAAA;IACvB,MAAM,EAAE;QACN,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAA;KACxC,EAAE,CAAA;IACH,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CACtC,CAAA;AAYD,eAAO,MAAM,YAAY,wCAA+C,IAAI,KAAG,OAAO,CAAC,IAAI,
|
|
1
|
+
{"version":3,"file":"insertArrays.d.ts","sourceRoot":"","sources":["../../src/upsertRow/insertArrays.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAA;AACnE,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAErE,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,cAAc,CAAA;IACvB,MAAM,EAAE;QACN,CAAC,SAAS,EAAE,MAAM,GAAG,gBAAgB,EAAE,CAAA;KACxC,EAAE,CAAA;IACH,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,CAAA;CACtC,CAAA;AAYD,eAAO,MAAM,YAAY,wCAA+C,IAAI,KAAG,OAAO,CAAC,IAAI,CAoF1F,CAAA"}
|
|
@@ -26,7 +26,10 @@ export const insertArrays = async ({ adapter, arrays, db, parentRows })=>{
|
|
|
26
26
|
arrayRowLocaleData._locale = arrayRowLocale;
|
|
27
27
|
rowsByTable[tableName].locales.push(arrayRowLocaleData);
|
|
28
28
|
if (!arrayRow.row.id) {
|
|
29
|
-
arrayRowLocaleData._getParentID = (rows)=>
|
|
29
|
+
arrayRowLocaleData._getParentID = (rows)=>{
|
|
30
|
+
const { id } = rows.find((each)=>each._uuid === arrayRow.row._uuid);
|
|
31
|
+
return id;
|
|
32
|
+
};
|
|
30
33
|
}
|
|
31
34
|
});
|
|
32
35
|
});
|
|
@@ -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}\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 ({ adapter, arrays, db, parentRows }: 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) => rows
|
|
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}\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 ({ adapter, arrays, db, parentRows }: 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\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","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":"AAsBA,OAAO,MAAMA,eAAe,OAAO,EAAEC,OAAO,EAAEC,MAAM,EAAEC,EAAE,EAAEC,UAAU,EAAQ;IAC1E,4CAA4C;IAC5C,MAAMC,cAA2B,CAAC;IAElCH,OAAOI,OAAO,CAAC,CAACC,eAAeC;QAC7BC,OAAOC,OAAO,CAACH,eAAe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,IAAIT,IAAIR,IAAI,CAACM,MAAM,GAAG,GAAG;YACvBW,eAAe,MAAM9B,QAAQ+B,MAAM,CAAC;gBAClC7B;gBACAQ;gBACAsB,QAAQX,IAAIR,IAAI;YAClB;QACF;QAEA,qBAAqB;QACrB,IAAIb,QAAQiC,MAAM,CAAC,CAAC,EAAEvB,UAAU,EAAEV,QAAQkC,aAAa,CAAC,CAAC,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,MAAMpC,QAAQ+B,MAAM,CAAC;gBACnB7B;gBACAQ,WAAW,CAAC,EAAEA,UAAU,EAAEV,QAAQkC,aAAa,CAAC,CAAC;gBACjDF,QAAQX,IAAIT,OAAO;YACrB;QACF;QAEA,0DAA0D;QAC1D,IAAIS,IAAIpB,MAAM,CAACkB,MAAM,GAAG,GAAG;YACzB,MAAMpB,aAAa;gBACjBC;gBACAC,QAAQoB,IAAIpB,MAAM;gBAClBC;gBACAC,YAAY2B;YACd;QACF;IACF;AACF,EAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { SQL } from 'drizzle-orm';
|
|
2
|
-
import type { Field, PayloadRequest } from 'payload';
|
|
2
|
+
import type { Field, JoinQuery, PayloadRequest } from 'payload';
|
|
3
3
|
import type { DrizzleAdapter, DrizzleTransaction, GenericColumn } from '../types.js';
|
|
4
4
|
type BaseArgs = {
|
|
5
5
|
adapter: DrizzleAdapter;
|
|
@@ -11,18 +11,21 @@ type BaseArgs = {
|
|
|
11
11
|
* @default false
|
|
12
12
|
*/
|
|
13
13
|
ignoreResult?: boolean;
|
|
14
|
+
joinQuery?: JoinQuery;
|
|
14
15
|
path?: string;
|
|
15
16
|
req: PayloadRequest;
|
|
16
17
|
tableName: string;
|
|
17
18
|
};
|
|
18
19
|
type CreateArgs = {
|
|
19
20
|
id?: never;
|
|
21
|
+
joinQuery?: never;
|
|
20
22
|
operation: 'create';
|
|
21
23
|
upsertTarget?: never;
|
|
22
24
|
where?: never;
|
|
23
25
|
} & BaseArgs;
|
|
24
26
|
type UpdateArgs = {
|
|
25
27
|
id?: number | string;
|
|
28
|
+
joinQuery?: JoinQuery;
|
|
26
29
|
operation: 'update';
|
|
27
30
|
upsertTarget?: GenericColumn;
|
|
28
31
|
where?: SQL<unknown>;
|
|
@@ -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,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;
|
|
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,KAAK,EAAE,SAAS,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE/D,OAAO,KAAK,EAAE,cAAc,EAAE,kBAAkB,EAAE,aAAa,EAAE,MAAM,aAAa,CAAA;AAEpF,KAAK,QAAQ,GAAG;IACd,OAAO,EAAE,cAAc,CAAA;IACvB,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAC7B,EAAE,EAAE,cAAc,CAAC,SAAS,CAAC,GAAG,kBAAkB,CAAA;IAClD,MAAM,EAAE,KAAK,EAAE,CAAA;IACf;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,GAAG,EAAE,cAAc,CAAA;IACnB,SAAS,EAAE,MAAM,CAAA;CAClB,CAAA;AAED,KAAK,UAAU,GAAG;IAChB,EAAE,CAAC,EAAE,KAAK,CAAA;IACV,SAAS,CAAC,EAAE,KAAK,CAAA;IACjB,SAAS,EAAE,QAAQ,CAAA;IACnB,YAAY,CAAC,EAAE,KAAK,CAAA;IACpB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,GAAG,QAAQ,CAAA;AAEZ,KAAK,UAAU,GAAG;IAChB,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAAA;IACpB,SAAS,CAAC,EAAE,SAAS,CAAA;IACrB,SAAS,EAAE,QAAQ,CAAA;IACnB,YAAY,CAAC,EAAE,aAAa,CAAA;IAC5B,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAA;CACrB,GAAG,QAAQ,CAAA;AAEZ,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 { Field, PayloadRequest } 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: Field[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: boolean\n path?: string\n req: PayloadRequest\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n operation: 'create'\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n operation: 'update'\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"sources":["../../src/upsertRow/types.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { Field, JoinQuery, PayloadRequest } 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: Field[]\n /**\n * When true, skips reading the data back from the database and returns the input data\n * @default false\n */\n ignoreResult?: boolean\n joinQuery?: JoinQuery\n path?: string\n req: PayloadRequest\n tableName: string\n}\n\ntype CreateArgs = {\n id?: never\n joinQuery?: never\n operation: 'create'\n upsertTarget?: never\n where?: never\n} & BaseArgs\n\ntype UpdateArgs = {\n id?: number | string\n joinQuery?: JoinQuery\n operation: 'update'\n upsertTarget?: GenericColumn\n where?: SQL<unknown>\n} & BaseArgs\n\nexport type Args = CreateArgs | UpdateArgs\n"],"names":[],"mappings":"AAqCA,WAA0C"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
2
|
+
import { extendDrizzleTable } from './extendDrizzleTable.js';
|
|
3
|
+
type DatabaseSchema = {
|
|
4
|
+
enums?: DrizzleAdapter['enums'];
|
|
5
|
+
relations: Record<string, any>;
|
|
6
|
+
tables: DrizzleAdapter['tables'];
|
|
7
|
+
};
|
|
8
|
+
type Adapter = {
|
|
9
|
+
afterSchemaInit: DatabaseSchemaHook[];
|
|
10
|
+
beforeSchemaInit: DatabaseSchemaHook[];
|
|
11
|
+
} & DatabaseSchema;
|
|
12
|
+
type DatabaseSchemaHookArgs = {
|
|
13
|
+
adapter: Record<string, unknown>;
|
|
14
|
+
extendTable: typeof extendDrizzleTable;
|
|
15
|
+
schema: DatabaseSchema;
|
|
16
|
+
};
|
|
17
|
+
type DatabaseSchemaHook = (args: DatabaseSchemaHookArgs) => DatabaseSchema | Promise<DatabaseSchema>;
|
|
18
|
+
type Args = {
|
|
19
|
+
adapter: Adapter;
|
|
20
|
+
type: 'afterSchemaInit' | 'beforeSchemaInit';
|
|
21
|
+
};
|
|
22
|
+
export declare const executeSchemaHooks: ({ type, adapter }: Args) => Promise<void>;
|
|
23
|
+
export {};
|
|
24
|
+
//# sourceMappingURL=executeSchemaHooks.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"executeSchemaHooks.d.ts","sourceRoot":"","sources":["../../src/utilities/executeSchemaHooks.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAEjD,OAAO,EAAE,kBAAkB,EAAE,MAAM,yBAAyB,CAAA;AAE5D,KAAK,cAAc,GAAG;IACpB,KAAK,CAAC,EAAE,cAAc,CAAC,OAAO,CAAC,CAAA;IAC/B,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;IAC9B,MAAM,EAAE,cAAc,CAAC,QAAQ,CAAC,CAAA;CACjC,CAAA;AAED,KAAK,OAAO,GAAG;IACb,eAAe,EAAE,kBAAkB,EAAE,CAAA;IACrC,gBAAgB,EAAE,kBAAkB,EAAE,CAAA;CACvC,GAAG,cAAc,CAAA;AAElB,KAAK,sBAAsB,GAAG;IAC5B,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;IAChC,WAAW,EAAE,OAAO,kBAAkB,CAAA;IACtC,MAAM,EAAE,cAAc,CAAA;CACvB,CAAA;AAED,KAAK,kBAAkB,GAAG,CAAC,IAAI,EAAE,sBAAsB,KAAK,cAAc,GAAG,OAAO,CAAC,cAAc,CAAC,CAAA;AAEpG,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,OAAO,CAAA;IAChB,IAAI,EAAE,iBAAiB,GAAG,kBAAkB,CAAA;CAC7C,CAAA;AAED,eAAO,MAAM,kBAAkB,sBAA6B,IAAI,KAAG,OAAO,CAAC,IAAI,CAkB9E,CAAA"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { extendDrizzleTable } from './extendDrizzleTable.js';
|
|
2
|
+
export const executeSchemaHooks = async ({ type, adapter })=>{
|
|
3
|
+
for (const hook of adapter[type]){
|
|
4
|
+
const result = await hook({
|
|
5
|
+
adapter,
|
|
6
|
+
extendTable: extendDrizzleTable,
|
|
7
|
+
schema: {
|
|
8
|
+
enums: adapter.enums,
|
|
9
|
+
relations: adapter.relations,
|
|
10
|
+
tables: adapter.tables
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
if (result.enums) {
|
|
14
|
+
adapter.enums = result.enums;
|
|
15
|
+
}
|
|
16
|
+
adapter.tables = result.tables;
|
|
17
|
+
adapter.relations = result.relations;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
//# sourceMappingURL=executeSchemaHooks.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/executeSchemaHooks.ts"],"sourcesContent":["import type { DrizzleAdapter } from '../types.js'\n\nimport { extendDrizzleTable } from './extendDrizzleTable.js'\n\ntype DatabaseSchema = {\n enums?: DrizzleAdapter['enums']\n relations: Record<string, any>\n tables: DrizzleAdapter['tables']\n}\n\ntype Adapter = {\n afterSchemaInit: DatabaseSchemaHook[]\n beforeSchemaInit: DatabaseSchemaHook[]\n} & DatabaseSchema\n\ntype DatabaseSchemaHookArgs = {\n adapter: Record<string, unknown>\n extendTable: typeof extendDrizzleTable\n schema: DatabaseSchema\n}\n\ntype DatabaseSchemaHook = (args: DatabaseSchemaHookArgs) => DatabaseSchema | Promise<DatabaseSchema>\n\ntype Args = {\n adapter: Adapter\n type: 'afterSchemaInit' | 'beforeSchemaInit'\n}\n\nexport const executeSchemaHooks = async ({ type, adapter }: Args): Promise<void> => {\n for (const hook of adapter[type]) {\n const result = await hook({\n adapter,\n extendTable: extendDrizzleTable,\n schema: {\n enums: adapter.enums,\n relations: adapter.relations,\n tables: adapter.tables,\n },\n })\n if (result.enums) {\n adapter.enums = result.enums\n }\n\n adapter.tables = result.tables\n adapter.relations = result.relations\n }\n}\n"],"names":["extendDrizzleTable","executeSchemaHooks","type","adapter","hook","result","extendTable","schema","enums","relations","tables"],"mappings":"AAEA,SAASA,kBAAkB,QAAQ,0BAAyB;AA0B5D,OAAO,MAAMC,qBAAqB,OAAO,EAAEC,IAAI,EAAEC,OAAO,EAAQ;IAC9D,KAAK,MAAMC,QAAQD,OAAO,CAACD,KAAK,CAAE;QAChC,MAAMG,SAAS,MAAMD,KAAK;YACxBD;YACAG,aAAaN;YACbO,QAAQ;gBACNC,OAAOL,QAAQK,KAAK;gBACpBC,WAAWN,QAAQM,SAAS;gBAC5BC,QAAQP,QAAQO,MAAM;YACxB;QACF;QACA,IAAIL,OAAOG,KAAK,EAAE;YAChBL,QAAQK,KAAK,GAAGH,OAAOG,KAAK;QAC9B;QAEAL,QAAQO,MAAM,GAAGL,OAAOK,MAAM;QAC9BP,QAAQM,SAAS,GAAGJ,OAAOI,SAAS;IACtC;AACF,EAAC"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implemented from:
|
|
3
|
+
* https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/table.ts#L73
|
|
4
|
+
* Drizzle uses @internal JSDoc to remove their internal methods from types, for example
|
|
5
|
+
* Table.Symbol, columnBuilder.build - but they actually exist.
|
|
6
|
+
*/
|
|
7
|
+
import type { ColumnBuilderBase } from 'drizzle-orm';
|
|
8
|
+
import { Table } from 'drizzle-orm';
|
|
9
|
+
type Args = {
|
|
10
|
+
columns?: Record<string, ColumnBuilderBase<any>>;
|
|
11
|
+
extraConfig?: (self: Record<string, any>) => object;
|
|
12
|
+
table: Table;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Extends the passed table with additional columns / extra config
|
|
16
|
+
*/
|
|
17
|
+
export declare const extendDrizzleTable: ({ columns, extraConfig, table }: Args) => void;
|
|
18
|
+
export {};
|
|
19
|
+
//# sourceMappingURL=extendDrizzleTable.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"extendDrizzleTable.d.ts","sourceRoot":"","sources":["../../src/utilities/extendDrizzleTable.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAA;AAEpD,OAAO,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAWnC,KAAK,IAAI,GAAG;IACV,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,iBAAiB,CAAC,GAAG,CAAC,CAAC,CAAA;IAChD,WAAW,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,KAAK,MAAM,CAAA;IACnD,KAAK,EAAE,KAAK,CAAA;CACb,CAAA;AAED;;GAEG;AACH,eAAO,MAAM,kBAAkB,oCAAqC,IAAI,KAAG,IAkC1E,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Implemented from:
|
|
3
|
+
* https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/table.ts#L73
|
|
4
|
+
* Drizzle uses @internal JSDoc to remove their internal methods from types, for example
|
|
5
|
+
* Table.Symbol, columnBuilder.build - but they actually exist.
|
|
6
|
+
*/ import { Table } from 'drizzle-orm';
|
|
7
|
+
import { APIError } from 'payload';
|
|
8
|
+
const { Symbol: DrizzleSymbol } = Table;
|
|
9
|
+
/**
|
|
10
|
+
* Extends the passed table with additional columns / extra config
|
|
11
|
+
*/ export const extendDrizzleTable = ({ columns, extraConfig, table })=>{
|
|
12
|
+
const InlineForeignKeys = Object.getOwnPropertySymbols(table).find((symbol)=>{
|
|
13
|
+
return symbol.description?.includes('InlineForeignKeys');
|
|
14
|
+
});
|
|
15
|
+
if (!InlineForeignKeys) {
|
|
16
|
+
throw new APIError(`Error when finding InlineForeignKeys Symbol`, 500);
|
|
17
|
+
}
|
|
18
|
+
if (columns) {
|
|
19
|
+
for (const [name, columnBuilder] of Object.entries(columns)){
|
|
20
|
+
const column = columnBuilder.build(table);
|
|
21
|
+
table[name] = column;
|
|
22
|
+
table[InlineForeignKeys].push(...columnBuilder.buildForeignKeys(column, table));
|
|
23
|
+
table[DrizzleSymbol.Columns][name] = column;
|
|
24
|
+
table[DrizzleSymbol.ExtraConfigColumns][name] = 'buildExtraConfigColumn' in columnBuilder ? columnBuilder.buildExtraConfigColumn(table) : column;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
if (extraConfig) {
|
|
28
|
+
const originalExtraConfigBuilder = table[DrizzleSymbol.ExtraConfigBuilder];
|
|
29
|
+
table[DrizzleSymbol.ExtraConfigBuilder] = (t)=>{
|
|
30
|
+
return {
|
|
31
|
+
...originalExtraConfigBuilder(t),
|
|
32
|
+
...extraConfig(t)
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
//# sourceMappingURL=extendDrizzleTable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/extendDrizzleTable.ts"],"sourcesContent":["/**\n * Implemented from:\n * https://github.com/drizzle-team/drizzle-orm/blob/main/drizzle-orm/src/pg-core/table.ts#L73\n * Drizzle uses @internal JSDoc to remove their internal methods from types, for example\n * Table.Symbol, columnBuilder.build - but they actually exist.\n */\nimport type { ColumnBuilderBase } from 'drizzle-orm'\n\nimport { Table } from 'drizzle-orm'\nimport { APIError } from 'payload'\n\nconst { Symbol: DrizzleSymbol } = Table as unknown as {\n Symbol: {\n Columns: symbol\n ExtraConfigBuilder: symbol\n ExtraConfigColumns: symbol\n }\n}\n\ntype Args = {\n columns?: Record<string, ColumnBuilderBase<any>>\n extraConfig?: (self: Record<string, any>) => object\n table: Table\n}\n\n/**\n * Extends the passed table with additional columns / extra config\n */\nexport const extendDrizzleTable = ({ columns, extraConfig, table }: Args): void => {\n const InlineForeignKeys = Object.getOwnPropertySymbols(table).find((symbol) => {\n return symbol.description?.includes('InlineForeignKeys')\n })\n\n if (!InlineForeignKeys) {\n throw new APIError(`Error when finding InlineForeignKeys Symbol`, 500)\n }\n\n if (columns) {\n for (const [name, columnBuilder] of Object.entries(columns) as [string, any][]) {\n const column = columnBuilder.build(table)\n\n table[name] = column\n table[InlineForeignKeys].push(...columnBuilder.buildForeignKeys(column, table))\n table[DrizzleSymbol.Columns][name] = column\n\n table[DrizzleSymbol.ExtraConfigColumns][name] =\n 'buildExtraConfigColumn' in columnBuilder\n ? columnBuilder.buildExtraConfigColumn(table)\n : column\n }\n }\n\n if (extraConfig) {\n const originalExtraConfigBuilder = table[DrizzleSymbol.ExtraConfigBuilder]\n\n table[DrizzleSymbol.ExtraConfigBuilder] = (t) => {\n return {\n ...originalExtraConfigBuilder(t),\n ...extraConfig(t),\n }\n }\n }\n}\n"],"names":["Table","APIError","Symbol","DrizzleSymbol","extendDrizzleTable","columns","extraConfig","table","InlineForeignKeys","Object","getOwnPropertySymbols","find","symbol","description","includes","name","columnBuilder","entries","column","build","push","buildForeignKeys","Columns","ExtraConfigColumns","buildExtraConfigColumn","originalExtraConfigBuilder","ExtraConfigBuilder","t"],"mappings":"AAAA;;;;;CAKC,GAGD,SAASA,KAAK,QAAQ,cAAa;AACnC,SAASC,QAAQ,QAAQ,UAAS;AAElC,MAAM,EAAEC,QAAQC,aAAa,EAAE,GAAGH;AAclC;;CAEC,GACD,OAAO,MAAMI,qBAAqB,CAAC,EAAEC,OAAO,EAAEC,WAAW,EAAEC,KAAK,EAAQ;IACtE,MAAMC,oBAAoBC,OAAOC,qBAAqB,CAACH,OAAOI,IAAI,CAAC,CAACC;QAClE,OAAOA,OAAOC,WAAW,EAAEC,SAAS;IACtC;IAEA,IAAI,CAACN,mBAAmB;QACtB,MAAM,IAAIP,SAAS,CAAC,2CAA2C,CAAC,EAAE;IACpE;IAEA,IAAII,SAAS;QACX,KAAK,MAAM,CAACU,MAAMC,cAAc,IAAIP,OAAOQ,OAAO,CAACZ,SAA6B;YAC9E,MAAMa,SAASF,cAAcG,KAAK,CAACZ;YAEnCA,KAAK,CAACQ,KAAK,GAAGG;YACdX,KAAK,CAACC,kBAAkB,CAACY,IAAI,IAAIJ,cAAcK,gBAAgB,CAACH,QAAQX;YACxEA,KAAK,CAACJ,cAAcmB,OAAO,CAAC,CAACP,KAAK,GAAGG;YAErCX,KAAK,CAACJ,cAAcoB,kBAAkB,CAAC,CAACR,KAAK,GAC3C,4BAA4BC,gBACxBA,cAAcQ,sBAAsB,CAACjB,SACrCW;QACR;IACF;IAEA,IAAIZ,aAAa;QACf,MAAMmB,6BAA6BlB,KAAK,CAACJ,cAAcuB,kBAAkB,CAAC;QAE1EnB,KAAK,CAACJ,cAAcuB,kBAAkB,CAAC,GAAG,CAACC;YACzC,OAAO;gBACL,GAAGF,2BAA2BE,EAAE;gBAChC,GAAGrB,YAAYqB,EAAE;YACnB;QACF;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Collection } from 'payload';
|
|
2
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
3
|
+
export declare const getCollectionIdType: ({ adapter, collection, }: {
|
|
4
|
+
adapter: DrizzleAdapter;
|
|
5
|
+
collection: Collection;
|
|
6
|
+
}) => "number" | "text";
|
|
7
|
+
//# sourceMappingURL=getCollectionIdType.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"getCollectionIdType.d.ts","sourceRoot":"","sources":["../../src/utilities/getCollectionIdType.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAEzC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AASjD,eAAO,MAAM,mBAAmB,6BAG7B;IACD,OAAO,EAAE,cAAc,CAAA;IACvB,UAAU,EAAE,UAAU,CAAA;CACvB,sBAEA,CAAA"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
const typeMap = {
|
|
2
|
+
number: 'number',
|
|
3
|
+
serial: 'number',
|
|
4
|
+
text: 'text',
|
|
5
|
+
uuid: 'text'
|
|
6
|
+
};
|
|
7
|
+
export const getCollectionIdType = ({ adapter, collection })=>{
|
|
8
|
+
return collection.customIDType ?? typeMap[adapter.idType];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
//# sourceMappingURL=getCollectionIdType.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/getCollectionIdType.ts"],"sourcesContent":["import type { Collection } from 'payload'\n\nimport type { DrizzleAdapter } from '../types.js'\n\nconst typeMap: Record<string, 'number' | 'text'> = {\n number: 'number',\n serial: 'number',\n text: 'text',\n uuid: 'text',\n}\n\nexport const getCollectionIdType = ({\n adapter,\n collection,\n}: {\n adapter: DrizzleAdapter\n collection: Collection\n}) => {\n return collection.customIDType ?? typeMap[adapter.idType]\n}\n"],"names":["typeMap","number","serial","text","uuid","getCollectionIdType","adapter","collection","customIDType","idType"],"mappings":"AAIA,MAAMA,UAA6C;IACjDC,QAAQ;IACRC,QAAQ;IACRC,MAAM;IACNC,MAAM;AACR;AAEA,OAAO,MAAMC,sBAAsB,CAAC,EAClCC,OAAO,EACPC,UAAU,EAIX;IACC,OAAOA,WAAWC,YAAY,IAAIR,OAAO,CAACM,QAAQG,MAAM,CAAC;AAC3D,EAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"isPolymorphicRelationship.d.ts","sourceRoot":"","sources":["../../src/utilities/isPolymorphicRelationship.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAE7C,eAAO,MAAM,yBAAyB,UAC7B,OAAO,KACb,KAAK,IAAI;IACV,UAAU,EAAE,cAAc,CAAA;IAC1B,KAAK,EAAE,MAAM,GAAG,MAAM,CAAA;CASvB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/utilities/isPolymorphicRelationship.ts"],"sourcesContent":["import type { CollectionSlug } from 'payload'\n\nexport const isPolymorphicRelationship = (\n value: unknown,\n): value is {\n relationTo: CollectionSlug\n value: number | string\n} => {\n return (\n value &&\n typeof value === 'object' &&\n 'relationTo' in value &&\n typeof value.relationTo === 'string' &&\n 'value' in value\n )\n}\n"],"names":["isPolymorphicRelationship","value","relationTo"],"mappings":"AAEA,OAAO,MAAMA,4BAA4B,CACvCC;IAKA,OACEA,SACA,OAAOA,UAAU,YACjB,gBAAgBA,SAChB,OAAOA,MAAMC,UAAU,KAAK,YAC5B,WAAWD;AAEf,EAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@payloadcms/drizzle",
|
|
3
|
-
"version": "3.0.0-beta.
|
|
3
|
+
"version": "3.0.0-beta.109",
|
|
4
4
|
"description": "A library of shared functions used by different payload database adapters",
|
|
5
5
|
"homepage": "https://payloadcms.com",
|
|
6
6
|
"repository": {
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"@types/pg": "8.10.2",
|
|
47
47
|
"@types/to-snake-case": "1.0.0",
|
|
48
48
|
"@payloadcms/eslint-config": "3.0.0-beta.97",
|
|
49
|
-
"payload": "3.0.0-beta.
|
|
49
|
+
"payload": "3.0.0-beta.109"
|
|
50
50
|
},
|
|
51
51
|
"peerDependencies": {
|
|
52
|
-
"payload": "3.0.0-beta.
|
|
52
|
+
"payload": "3.0.0-beta.109"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"build": "pnpm build:swc && pnpm build:types",
|