@payloadcms/drizzle 4.0.0-internal.e55ccef → 4.0.0-internal.ea6e8f8

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 (280) hide show
  1. package/dist/count.d.ts.map +1 -1
  2. package/dist/count.js +25 -3
  3. package/dist/count.js.map +1 -1
  4. package/dist/countVersions.d.ts.map +1 -1
  5. package/dist/countVersions.js +28 -4
  6. package/dist/countVersions.js.map +1 -1
  7. package/dist/createGlobalVersion.d.ts.map +1 -1
  8. package/dist/createGlobalVersion.js +15 -2
  9. package/dist/createGlobalVersion.js.map +1 -1
  10. package/dist/createTableName.d.ts.map +1 -1
  11. package/dist/createTableName.js +2 -7
  12. package/dist/createTableName.js.map +1 -1
  13. package/dist/createVersion.d.ts.map +1 -1
  14. package/dist/createVersion.js +12 -1
  15. package/dist/createVersion.js.map +1 -1
  16. package/dist/deleteBranchGlobal.d.ts +10 -0
  17. package/dist/deleteBranchGlobal.d.ts.map +1 -0
  18. package/dist/deleteBranchGlobal.js +27 -0
  19. package/dist/deleteBranchGlobal.js.map +1 -0
  20. package/dist/deleteOne.d.ts.map +1 -1
  21. package/dist/deleteOne.js +18 -1
  22. package/dist/deleteOne.js.map +1 -1
  23. package/dist/exports/postgres.d.ts +3 -0
  24. package/dist/exports/postgres.d.ts.map +1 -1
  25. package/dist/exports/postgres.js +3 -0
  26. package/dist/exports/postgres.js.map +1 -1
  27. package/dist/exports/sqlite.d.ts +1 -0
  28. package/dist/exports/sqlite.d.ts.map +1 -1
  29. package/dist/exports/sqlite.js +1 -0
  30. package/dist/exports/sqlite.js.map +1 -1
  31. package/dist/find/buildFindManyArgs.d.ts +3 -2
  32. package/dist/find/buildFindManyArgs.d.ts.map +1 -1
  33. package/dist/find/buildFindManyArgs.js +2 -1
  34. package/dist/find/buildFindManyArgs.js.map +1 -1
  35. package/dist/find/buildPolymorphicJoinQuery.d.ts +39 -0
  36. package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
  37. package/dist/find/buildPolymorphicJoinQuery.js +152 -0
  38. package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
  39. package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
  40. package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
  41. package/dist/find/buildPolymorphicJoinWhere.js +301 -0
  42. package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
  43. package/dist/find/createPolymorphicJoinWherePlan.d.ts +37 -0
  44. package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
  45. package/dist/find/createPolymorphicJoinWherePlan.js +191 -0
  46. package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
  47. package/dist/find/findMany.d.ts +6 -1
  48. package/dist/find/findMany.d.ts.map +1 -1
  49. package/dist/find/findMany.js +16 -3
  50. package/dist/find/findMany.js.map +1 -1
  51. package/dist/find/traverseFields.d.ts +3 -2
  52. package/dist/find/traverseFields.d.ts.map +1 -1
  53. package/dist/find/traverseFields.js +72 -197
  54. package/dist/find/traverseFields.js.map +1 -1
  55. package/dist/find.d.ts.map +1 -1
  56. package/dist/find.js +32 -4
  57. package/dist/find.js.map +1 -1
  58. package/dist/findDistinct.d.ts.map +1 -1
  59. package/dist/findDistinct.js +107 -8
  60. package/dist/findDistinct.js.map +1 -1
  61. package/dist/findGlobal.d.ts.map +1 -1
  62. package/dist/findGlobal.js +22 -5
  63. package/dist/findGlobal.js.map +1 -1
  64. package/dist/findGlobalVersions.js +8 -3
  65. package/dist/findGlobalVersions.js.map +1 -1
  66. package/dist/findOne.d.ts +1 -1
  67. package/dist/findOne.d.ts.map +1 -1
  68. package/dist/findOne.js +30 -3
  69. package/dist/findOne.js.map +1 -1
  70. package/dist/findVersions.d.ts.map +1 -1
  71. package/dist/findVersions.js +32 -5
  72. package/dist/findVersions.js.map +1 -1
  73. package/dist/index.d.ts +6 -2
  74. package/dist/index.d.ts.map +1 -1
  75. package/dist/index.js +4 -0
  76. package/dist/index.js.map +1 -1
  77. package/dist/migrate.d.ts.map +1 -1
  78. package/dist/migrate.js +41 -15
  79. package/dist/migrate.js.map +1 -1
  80. package/dist/migrateDown.d.ts +2 -1
  81. package/dist/migrateDown.d.ts.map +1 -1
  82. package/dist/migrateDown.js +17 -4
  83. package/dist/migrateDown.js.map +1 -1
  84. package/dist/migrateFresh.d.ts +4 -2
  85. package/dist/migrateFresh.d.ts.map +1 -1
  86. package/dist/migrateFresh.js +26 -9
  87. package/dist/migrateFresh.js.map +1 -1
  88. package/dist/migrateRefresh.d.ts +2 -1
  89. package/dist/migrateRefresh.d.ts.map +1 -1
  90. package/dist/migrateRefresh.js +20 -5
  91. package/dist/migrateRefresh.js.map +1 -1
  92. package/dist/migrateReset.d.ts +2 -1
  93. package/dist/migrateReset.d.ts.map +1 -1
  94. package/dist/migrateReset.js +18 -4
  95. package/dist/migrateReset.js.map +1 -1
  96. package/dist/migrateStatus.d.ts +2 -1
  97. package/dist/migrateStatus.d.ts.map +1 -1
  98. package/dist/migrateStatus.js +11 -6
  99. package/dist/migrateStatus.js.map +1 -1
  100. package/dist/postgres/createJSONQuery/index.js +3 -3
  101. package/dist/postgres/createJSONQuery/index.js.map +1 -1
  102. package/dist/postgres/init.d.ts.map +1 -1
  103. package/dist/postgres/init.js +6 -0
  104. package/dist/postgres/init.js.map +1 -1
  105. package/dist/postgres/insert.d.ts.map +1 -1
  106. package/dist/postgres/insert.js +4 -2
  107. package/dist/postgres/insert.js.map +1 -1
  108. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts +35 -0
  109. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
  110. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js +185 -0
  111. package/dist/postgres/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
  112. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -1
  113. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js +8 -4
  114. package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -1
  115. package/dist/postgres/predefinedMigrations/v2-v3/index.js +1 -1
  116. package/dist/postgres/predefinedMigrations/v2-v3/index.js.map +1 -1
  117. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts +15 -0
  118. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts.map +1 -0
  119. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js +25 -0
  120. package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js.map +1 -0
  121. package/dist/postgres/query/postgresUnaccent.d.ts +13 -0
  122. package/dist/postgres/query/postgresUnaccent.d.ts.map +1 -0
  123. package/dist/postgres/query/postgresUnaccent.js +40 -0
  124. package/dist/postgres/query/postgresUnaccent.js.map +1 -0
  125. package/dist/postgres/types.d.ts +17 -2
  126. package/dist/postgres/types.d.ts.map +1 -1
  127. package/dist/postgres/types.js.map +1 -1
  128. package/dist/queries/buildAndOrConditions.d.ts +3 -2
  129. package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
  130. package/dist/queries/buildAndOrConditions.js +2 -1
  131. package/dist/queries/buildAndOrConditions.js.map +1 -1
  132. package/dist/queries/buildBranchVisibility.d.ts +11 -0
  133. package/dist/queries/buildBranchVisibility.d.ts.map +1 -0
  134. package/dist/queries/buildBranchVisibility.js +48 -0
  135. package/dist/queries/buildBranchVisibility.js.map +1 -0
  136. package/dist/queries/buildOperatorConstraint.d.ts +22 -0
  137. package/dist/queries/buildOperatorConstraint.d.ts.map +1 -0
  138. package/dist/queries/buildOperatorConstraint.js +63 -0
  139. package/dist/queries/buildOperatorConstraint.js.map +1 -0
  140. package/dist/queries/buildOrderBy.d.ts +3 -2
  141. package/dist/queries/buildOrderBy.d.ts.map +1 -1
  142. package/dist/queries/buildOrderBy.js +2 -1
  143. package/dist/queries/buildOrderBy.js.map +1 -1
  144. package/dist/queries/buildQuery.d.ts +3 -2
  145. package/dist/queries/buildQuery.d.ts.map +1 -1
  146. package/dist/queries/buildQuery.js +4 -2
  147. package/dist/queries/buildQuery.js.map +1 -1
  148. package/dist/queries/getTableColumnFromPath.d.ts +15 -5
  149. package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
  150. package/dist/queries/getTableColumnFromPath.js +225 -54
  151. package/dist/queries/getTableColumnFromPath.js.map +1 -1
  152. package/dist/queries/operatorMap.d.ts +7 -3
  153. package/dist/queries/operatorMap.d.ts.map +1 -1
  154. package/dist/queries/operatorMap.js.map +1 -1
  155. package/dist/queries/parseParams.d.ts +3 -2
  156. package/dist/queries/parseParams.d.ts.map +1 -1
  157. package/dist/queries/parseParams.js +266 -11
  158. package/dist/queries/parseParams.js.map +1 -1
  159. package/dist/queries/resolveRelationshipPath.d.ts +36 -0
  160. package/dist/queries/resolveRelationshipPath.d.ts.map +1 -0
  161. package/dist/queries/resolveRelationshipPath.js +66 -0
  162. package/dist/queries/resolveRelationshipPath.js.map +1 -0
  163. package/dist/queries/validateOperatorHandlers.d.ts +8 -0
  164. package/dist/queries/validateOperatorHandlers.d.ts.map +1 -0
  165. package/dist/queries/validateOperatorHandlers.js +30 -0
  166. package/dist/queries/validateOperatorHandlers.js.map +1 -0
  167. package/dist/queryDrafts.d.ts.map +1 -1
  168. package/dist/queryDrafts.js +23 -4
  169. package/dist/queryDrafts.js.map +1 -1
  170. package/dist/schema/build.d.ts.map +1 -1
  171. package/dist/schema/build.js +20 -17
  172. package/dist/schema/build.js.map +1 -1
  173. package/dist/schema/traverseFields.js +5 -5
  174. package/dist/schema/traverseFields.js.map +1 -1
  175. package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
  176. package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
  177. package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
  178. package/dist/sqlite/createJSONQuery/index.js +6 -5
  179. package/dist/sqlite/createJSONQuery/index.js.map +1 -1
  180. package/dist/sqlite/insert.d.ts.map +1 -1
  181. package/dist/sqlite/insert.js +9 -4
  182. package/dist/sqlite/insert.js.map +1 -1
  183. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts +15 -0
  184. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.d.ts.map +1 -0
  185. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js +114 -0
  186. package/dist/sqlite/predefinedMigrations/sizes-to-variants/index.js.map +1 -0
  187. package/dist/sqlite/types.d.ts +4 -0
  188. package/dist/sqlite/types.d.ts.map +1 -1
  189. package/dist/sqlite/types.js.map +1 -1
  190. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  191. package/dist/transactions/beginTransaction.js +13 -5
  192. package/dist/transactions/beginTransaction.js.map +1 -1
  193. package/dist/transactions/commitTransaction.d.ts.map +1 -1
  194. package/dist/transactions/commitTransaction.js +1 -5
  195. package/dist/transactions/commitTransaction.js.map +1 -1
  196. package/dist/transform/write/array.d.ts +3 -2
  197. package/dist/transform/write/array.d.ts.map +1 -1
  198. package/dist/transform/write/array.js +2 -1
  199. package/dist/transform/write/array.js.map +1 -1
  200. package/dist/transform/write/blocks.d.ts +3 -2
  201. package/dist/transform/write/blocks.d.ts.map +1 -1
  202. package/dist/transform/write/blocks.js +2 -1
  203. package/dist/transform/write/blocks.js.map +1 -1
  204. package/dist/transform/write/index.d.ts.map +1 -1
  205. package/dist/transform/write/index.js +2 -0
  206. package/dist/transform/write/index.js.map +1 -1
  207. package/dist/transform/write/traverseFields.d.ts +2 -1
  208. package/dist/transform/write/traverseFields.d.ts.map +1 -1
  209. package/dist/transform/write/traverseFields.js +40 -20
  210. package/dist/transform/write/traverseFields.js.map +1 -1
  211. package/dist/transform/write/types.d.ts +7 -0
  212. package/dist/transform/write/types.d.ts.map +1 -1
  213. package/dist/transform/write/types.js.map +1 -1
  214. package/dist/types.d.ts +51 -2
  215. package/dist/types.d.ts.map +1 -1
  216. package/dist/types.js.map +1 -1
  217. package/dist/updateGlobal.d.ts +5 -1
  218. package/dist/updateGlobal.d.ts.map +1 -1
  219. package/dist/updateGlobal.js +96 -4
  220. package/dist/updateGlobal.js.map +1 -1
  221. package/dist/updateJobs.d.ts.map +1 -1
  222. package/dist/updateJobs.js +37 -0
  223. package/dist/updateJobs.js.map +1 -1
  224. package/dist/updateOne.d.ts.map +1 -1
  225. package/dist/updateOne.js +86 -4
  226. package/dist/updateOne.js.map +1 -1
  227. package/dist/upsertBranchGlobalChange.d.ts +3 -0
  228. package/dist/upsertBranchGlobalChange.d.ts.map +1 -0
  229. package/dist/upsertBranchGlobalChange.js +35 -0
  230. package/dist/upsertBranchGlobalChange.js.map +1 -0
  231. package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
  232. package/dist/upsertRow/deleteExistingRowsByPath.js +17 -12
  233. package/dist/upsertRow/deleteExistingRowsByPath.js.map +1 -1
  234. package/dist/upsertRow/index.d.ts +1 -1
  235. package/dist/upsertRow/index.d.ts.map +1 -1
  236. package/dist/upsertRow/index.js +156 -46
  237. package/dist/upsertRow/index.js.map +1 -1
  238. package/dist/upsertRow/types.d.ts +3 -0
  239. package/dist/upsertRow/types.d.ts.map +1 -1
  240. package/dist/upsertRow/types.js.map +1 -1
  241. package/dist/utilities/buildCreateMigration.d.ts.map +1 -1
  242. package/dist/utilities/buildCreateMigration.js +12 -8
  243. package/dist/utilities/buildCreateMigration.js.map +1 -1
  244. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts +15 -0
  245. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.d.ts.map +1 -0
  246. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js +110 -0
  247. package/dist/utilities/buildDynamicPredefinedSizesToVariantsMigration.js.map +1 -0
  248. package/dist/utilities/buildForeignKeyName.d.ts.map +1 -1
  249. package/dist/utilities/buildForeignKeyName.js +3 -2
  250. package/dist/utilities/buildForeignKeyName.js.map +1 -1
  251. package/dist/utilities/buildIndexName.d.ts.map +1 -1
  252. package/dist/utilities/buildIndexName.js +3 -2
  253. package/dist/utilities/buildIndexName.js.map +1 -1
  254. package/dist/utilities/checkTruncatedIdentifiers.d.ts +13 -0
  255. package/dist/utilities/checkTruncatedIdentifiers.d.ts.map +1 -0
  256. package/dist/utilities/checkTruncatedIdentifiers.js +56 -0
  257. package/dist/utilities/checkTruncatedIdentifiers.js.map +1 -0
  258. package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
  259. package/dist/utilities/createSchemaGenerator.js +3 -0
  260. package/dist/utilities/createSchemaGenerator.js.map +1 -1
  261. package/dist/utilities/getSizesToVariantsRenames.d.ts +54 -0
  262. package/dist/utilities/getSizesToVariantsRenames.d.ts.map +1 -0
  263. package/dist/utilities/getSizesToVariantsRenames.js +251 -0
  264. package/dist/utilities/getSizesToVariantsRenames.js.map +1 -0
  265. package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
  266. package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
  267. package/dist/utilities/parseJSONPathSegment.js +17 -0
  268. package/dist/utilities/parseJSONPathSegment.js.map +1 -0
  269. package/dist/utilities/validateExistingBlockIsIdentical.d.ts +1 -5
  270. package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
  271. package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
  272. package/dist/utilities/validateIdentifierLength.d.ts +12 -0
  273. package/dist/utilities/validateIdentifierLength.d.ts.map +1 -0
  274. package/dist/utilities/validateIdentifierLength.js +18 -0
  275. package/dist/utilities/validateIdentifierLength.js.map +1 -0
  276. package/package.json +3 -3
  277. package/dist/upsertRow/handleUpsertError.spec.js +0 -42
  278. package/dist/upsertRow/handleUpsertError.spec.js.map +0 -1
  279. package/dist/utilities/isValidStringID.spec.js +0 -15
  280. package/dist/utilities/isValidStringID.spec.js.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import { and, eq, inArray } from 'drizzle-orm';
2
2
  export const deleteExistingRowsByPath = async ({ adapter, db, localeColumnName = '_locale', parentColumnName = '_parentID', parentID, pathColumnName = '_path', rows, tableName })=>{
3
- const localizedPathsToDelete = new Set();
3
+ const localizedPathsToDelete = new Map();
4
4
  const pathsToDelete = new Set();
5
5
  const table = adapter.tables[tableName];
6
6
  rows.forEach((row)=>{
@@ -8,24 +8,29 @@ export const deleteExistingRowsByPath = async ({ adapter, db, localeColumnName =
8
8
  const localeData = row[localeColumnName];
9
9
  if (typeof path === 'string') {
10
10
  if (typeof localeData === 'string') {
11
- localizedPathsToDelete.add(path);
11
+ const localePaths = localizedPathsToDelete.get(localeData) ?? new Set();
12
+ localePaths.add(path);
13
+ localizedPathsToDelete.set(localeData, localePaths);
12
14
  } else {
13
15
  pathsToDelete.add(path);
14
16
  }
15
17
  }
16
18
  });
17
19
  if (localizedPathsToDelete.size > 0) {
18
- const whereConstraints = [
19
- eq(table[parentColumnName], parentID)
20
- ];
21
- if (pathColumnName) {
22
- whereConstraints.push(inArray(table[pathColumnName], Array.from(localizedPathsToDelete)));
20
+ for (const [locale, localePaths] of localizedPathsToDelete){
21
+ const whereConstraints = [
22
+ eq(table[parentColumnName], parentID)
23
+ ];
24
+ if (pathColumnName) {
25
+ whereConstraints.push(inArray(table[pathColumnName], Array.from(localePaths)));
26
+ }
27
+ whereConstraints.push(eq(table[localeColumnName], locale));
28
+ await adapter.deleteWhere({
29
+ db,
30
+ tableName,
31
+ where: and(...whereConstraints)
32
+ });
23
33
  }
24
- await adapter.deleteWhere({
25
- db,
26
- tableName,
27
- where: and(...whereConstraints)
28
- });
29
34
  }
30
35
  if (pathsToDelete.size > 0) {
31
36
  const whereConstraints = [
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/upsertRow/deleteExistingRowsByPath.ts"],"sourcesContent":["import { and, eq, inArray } from 'drizzle-orm'\n\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n localeColumnName?: string\n parentColumnName?: string\n parentID: unknown\n pathColumnName?: string\n rows: Record<string, unknown>[]\n tableName: string\n}\n\nexport const deleteExistingRowsByPath = async ({\n adapter,\n db,\n localeColumnName = '_locale',\n parentColumnName = '_parentID',\n parentID,\n pathColumnName = '_path',\n rows,\n tableName,\n}: Args): Promise<void> => {\n const localizedPathsToDelete = new Set<string>()\n const pathsToDelete = new Set<string>()\n const table = adapter.tables[tableName]\n\n rows.forEach((row) => {\n const path = row[pathColumnName]\n const localeData = row[localeColumnName]\n if (typeof path === 'string') {\n if (typeof localeData === 'string') {\n localizedPathsToDelete.add(path)\n } else {\n pathsToDelete.add(path)\n }\n }\n })\n\n if (localizedPathsToDelete.size > 0) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(localizedPathsToDelete)))\n }\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n\n if (pathsToDelete.size > 0) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(pathsToDelete)))\n }\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n}\n"],"names":["and","eq","inArray","deleteExistingRowsByPath","adapter","db","localeColumnName","parentColumnName","parentID","pathColumnName","rows","tableName","localizedPathsToDelete","Set","pathsToDelete","table","tables","forEach","row","path","localeData","add","size","whereConstraints","push","Array","from","deleteWhere","where"],"mappings":"AAAA,SAASA,GAAG,EAAEC,EAAE,EAAEC,OAAO,QAAQ,cAAa;AAe9C,OAAO,MAAMC,2BAA2B,OAAO,EAC7CC,OAAO,EACPC,EAAE,EACFC,mBAAmB,SAAS,EAC5BC,mBAAmB,WAAW,EAC9BC,QAAQ,EACRC,iBAAiB,OAAO,EACxBC,IAAI,EACJC,SAAS,EACJ;IACL,MAAMC,yBAAyB,IAAIC;IACnC,MAAMC,gBAAgB,IAAID;IAC1B,MAAME,QAAQX,QAAQY,MAAM,CAACL,UAAU;IAEvCD,KAAKO,OAAO,CAAC,CAACC;QACZ,MAAMC,OAAOD,GAAG,CAACT,eAAe;QAChC,MAAMW,aAAaF,GAAG,CAACZ,iBAAiB;QACxC,IAAI,OAAOa,SAAS,UAAU;YAC5B,IAAI,OAAOC,eAAe,UAAU;gBAClCR,uBAAuBS,GAAG,CAACF;YAC7B,OAAO;gBACLL,cAAcO,GAAG,CAACF;YACpB;QACF;IACF;IAEA,IAAIP,uBAAuBU,IAAI,GAAG,GAAG;QACnC,MAAMC,mBAAmB;YAACtB,GAAGc,KAAK,CAACR,iBAAiB,EAAEC;SAAU;QAEhE,IAAIC,gBAAgB;YAClBc,iBAAiBC,IAAI,CAACtB,QAAQa,KAAK,CAACN,eAAe,EAAEgB,MAAMC,IAAI,CAACd;QAClE;QAEA,MAAMR,QAAQuB,WAAW,CAAC;YACxBtB;YACAM;YACAiB,OAAO5B,OAAOuB;QAChB;IACF;IAEA,IAAIT,cAAcQ,IAAI,GAAG,GAAG;QAC1B,MAAMC,mBAAmB;YAACtB,GAAGc,KAAK,CAACR,iBAAiB,EAAEC;SAAU;QAEhE,IAAIC,gBAAgB;YAClBc,iBAAiBC,IAAI,CAACtB,QAAQa,KAAK,CAACN,eAAe,EAAEgB,MAAMC,IAAI,CAACZ;QAClE;QAEA,MAAMV,QAAQuB,WAAW,CAAC;YACxBtB;YACAM;YACAiB,OAAO5B,OAAOuB;QAChB;IACF;AACF,EAAC"}
1
+ {"version":3,"sources":["../../src/upsertRow/deleteExistingRowsByPath.ts"],"sourcesContent":["import { and, eq, inArray } from 'drizzle-orm'\n\nimport type { DrizzleAdapter, DrizzleTransaction } from '../types.js'\n\ntype Args = {\n adapter: DrizzleAdapter\n db: DrizzleAdapter['drizzle'] | DrizzleTransaction\n localeColumnName?: string\n parentColumnName?: string\n parentID: unknown\n pathColumnName?: string\n rows: Record<string, unknown>[]\n tableName: string\n}\n\nexport const deleteExistingRowsByPath = async ({\n adapter,\n db,\n localeColumnName = '_locale',\n parentColumnName = '_parentID',\n parentID,\n pathColumnName = '_path',\n rows,\n tableName,\n}: Args): Promise<void> => {\n const localizedPathsToDelete = new Map<string, Set<string>>()\n const pathsToDelete = new Set<string>()\n const table = adapter.tables[tableName]\n\n rows.forEach((row) => {\n const path = row[pathColumnName]\n const localeData = row[localeColumnName]\n if (typeof path === 'string') {\n if (typeof localeData === 'string') {\n const localePaths = localizedPathsToDelete.get(localeData) ?? new Set<string>()\n\n localePaths.add(path)\n localizedPathsToDelete.set(localeData, localePaths)\n } else {\n pathsToDelete.add(path)\n }\n }\n })\n\n if (localizedPathsToDelete.size > 0) {\n for (const [locale, localePaths] of localizedPathsToDelete) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(localePaths)))\n }\n\n whereConstraints.push(eq(table[localeColumnName], locale))\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n }\n\n if (pathsToDelete.size > 0) {\n const whereConstraints = [eq(table[parentColumnName], parentID)]\n\n if (pathColumnName) {\n whereConstraints.push(inArray(table[pathColumnName], Array.from(pathsToDelete)))\n }\n\n await adapter.deleteWhere({\n db,\n tableName,\n where: and(...whereConstraints),\n })\n }\n}\n"],"names":["and","eq","inArray","deleteExistingRowsByPath","adapter","db","localeColumnName","parentColumnName","parentID","pathColumnName","rows","tableName","localizedPathsToDelete","Map","pathsToDelete","Set","table","tables","forEach","row","path","localeData","localePaths","get","add","set","size","locale","whereConstraints","push","Array","from","deleteWhere","where"],"mappings":"AAAA,SAASA,GAAG,EAAEC,EAAE,EAAEC,OAAO,QAAQ,cAAa;AAe9C,OAAO,MAAMC,2BAA2B,OAAO,EAC7CC,OAAO,EACPC,EAAE,EACFC,mBAAmB,SAAS,EAC5BC,mBAAmB,WAAW,EAC9BC,QAAQ,EACRC,iBAAiB,OAAO,EACxBC,IAAI,EACJC,SAAS,EACJ;IACL,MAAMC,yBAAyB,IAAIC;IACnC,MAAMC,gBAAgB,IAAIC;IAC1B,MAAMC,QAAQZ,QAAQa,MAAM,CAACN,UAAU;IAEvCD,KAAKQ,OAAO,CAAC,CAACC;QACZ,MAAMC,OAAOD,GAAG,CAACV,eAAe;QAChC,MAAMY,aAAaF,GAAG,CAACb,iBAAiB;QACxC,IAAI,OAAOc,SAAS,UAAU;YAC5B,IAAI,OAAOC,eAAe,UAAU;gBAClC,MAAMC,cAAcV,uBAAuBW,GAAG,CAACF,eAAe,IAAIN;gBAElEO,YAAYE,GAAG,CAACJ;gBAChBR,uBAAuBa,GAAG,CAACJ,YAAYC;YACzC,OAAO;gBACLR,cAAcU,GAAG,CAACJ;YACpB;QACF;IACF;IAEA,IAAIR,uBAAuBc,IAAI,GAAG,GAAG;QACnC,KAAK,MAAM,CAACC,QAAQL,YAAY,IAAIV,uBAAwB;YAC1D,MAAMgB,mBAAmB;gBAAC3B,GAAGe,KAAK,CAACT,iBAAiB,EAAEC;aAAU;YAEhE,IAAIC,gBAAgB;gBAClBmB,iBAAiBC,IAAI,CAAC3B,QAAQc,KAAK,CAACP,eAAe,EAAEqB,MAAMC,IAAI,CAACT;YAClE;YAEAM,iBAAiBC,IAAI,CAAC5B,GAAGe,KAAK,CAACV,iBAAiB,EAAEqB;YAElD,MAAMvB,QAAQ4B,WAAW,CAAC;gBACxB3B;gBACAM;gBACAsB,OAAOjC,OAAO4B;YAChB;QACF;IACF;IAEA,IAAId,cAAcY,IAAI,GAAG,GAAG;QAC1B,MAAME,mBAAmB;YAAC3B,GAAGe,KAAK,CAACT,iBAAiB,EAAEC;SAAU;QAEhE,IAAIC,gBAAgB;YAClBmB,iBAAiBC,IAAI,CAAC3B,QAAQc,KAAK,CAACP,eAAe,EAAEqB,MAAMC,IAAI,CAACjB;QAClE;QAEA,MAAMV,QAAQ4B,WAAW,CAAC;YACxB3B;YACAM;YACAsB,OAAOjC,OAAO4B;QAChB;IACF;AACF,EAAC"}
@@ -7,5 +7,5 @@ import type { Args } from './types.js';
7
7
  *
8
8
  * adapter function replaces the entire row and does not support partial updates.
9
9
  */
10
- export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, customID, joinQuery: _joinQuery, operation, path, req, select, tableName, upsertTarget, where, }: Args) => Promise<T>;
10
+ export declare const upsertRow: <T extends Record<string, unknown> | TypeWithID>({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, customID, joinQuery: _joinQuery, operation, path, req, select, tableName, upsertConflictData, upsertTarget, where, }: Args) => Promise<null | T>;
11
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAKzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAqBtC;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,+KAqB3E,IAAI,KAAG,OAAO,CAAC,CAAC,CAotBlB,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/upsertRow/index.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,SAAS,CAAA;AAMzC,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,YAAY,CAAA;AAqBtC;;;;;;GAMG;AACH,eAAO,MAAM,SAAS,GAAU,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG,UAAU,mMAsB3E,IAAI,KAAG,OAAO,CAAC,IAAI,GAAG,CAAC,CA01BzB,CAAA"}
@@ -1,4 +1,5 @@
1
1
  import { and, desc, eq, isNull, or } from 'drizzle-orm';
2
+ import { branchField, MAIN_BRANCH } from 'payload';
2
3
  import { buildFindManyArgs } from '../find/buildFindManyArgs.js';
3
4
  import { transform } from '../transform/read/index.js';
4
5
  import { transformForWrite } from '../transform/write/index.js';
@@ -17,7 +18,7 @@ import { shouldUseOptimizedUpsertRow } from './shouldUseOptimizedUpsertRow.js';
17
18
  */ export const upsertRow = async ({ id, adapter, collectionSlug, data, db, fields, globalSlug, ignoreResult, // TODO:
18
19
  // When we support joins for write operations (create/update) - pass collectionSlug to the buildFindManyArgs
19
20
  // Make a new argument in upsertRow.ts and pass the slug from every operation.
20
- customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, upsertTarget, where })=>{
21
+ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, upsertConflictData, upsertTarget, where })=>{
21
22
  if (operation === 'create' && !data.createdAt) {
22
23
  data.createdAt = new Date().toISOString();
23
24
  }
@@ -40,6 +41,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
40
41
  const { row } = transformedForWrite;
41
42
  const { arraysToPush } = transformedForWrite;
42
43
  const drizzle = db;
44
+ const updateWhere = where ? and(eq(adapter.tables[tableName].id, id), where) : eq(adapter.tables[tableName].id, id);
43
45
  // First, handle $push arrays
44
46
  if (arraysToPush && Object.keys(arraysToPush)?.length) {
45
47
  await insertArrays({
@@ -65,7 +67,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
65
67
  if (hasDataToUpdate) {
66
68
  // Only update row if there is something to update.
67
69
  // Example: if the data only consists of a single $push, calling insertArrays is enough - we don't need to update the row.
68
- await drizzle.update(adapter.tables[tableName]).set(row).where(eq(adapter.tables[tableName].id, id));
70
+ await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere);
69
71
  }
70
72
  return ignoreResult === 'idOnly' ? {
71
73
  id
@@ -83,8 +85,11 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
83
85
  const hasOnlyColumns = Object.keys(findManyArgs.columns || {}).length > 0;
84
86
  if (!hasDataToUpdate) {
85
87
  // Nothing to update => just fetch current row and return
86
- findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id);
88
+ findManyArgs.where = updateWhere;
87
89
  const doc = await db.query[tableName].findFirst(findManyArgs);
90
+ if (!doc) {
91
+ return null;
92
+ }
88
93
  return transform({
89
94
  adapter,
90
95
  config: adapter.payload.config,
@@ -105,7 +110,10 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
105
110
  }
106
111
  }
107
112
  }
108
- const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(eq(adapter.tables[tableName].id, id)).returning(Object.keys(selectedFields).length ? selectedFields : undefined);
113
+ const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere).returning(Object.keys(selectedFields).length ? selectedFields : undefined);
114
+ if (!docs[0]) {
115
+ return null;
116
+ }
109
117
  return transform({
110
118
  adapter,
111
119
  config: adapter.payload.config,
@@ -116,7 +124,13 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
116
124
  });
117
125
  }
118
126
  // DB Update that needs the result, potentially with joins => need to update first, then find. returning() does not work with joins.
119
- await drizzle.update(adapter.tables[tableName]).set(row).where(eq(adapter.tables[tableName].id, id));
127
+ const docs = await drizzle.update(adapter.tables[tableName]).set(row).where(updateWhere).returning({
128
+ id: adapter.tables[tableName].id
129
+ });
130
+ if (!docs[0]) {
131
+ return null;
132
+ }
133
+ insertedRow = docs[0];
120
134
  findManyArgs.where = eq(adapter.tables[tableName].id, insertedRow.id);
121
135
  const doc = await db.query[tableName].findFirst(findManyArgs);
122
136
  return transform({
@@ -149,6 +163,16 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
149
163
  path,
150
164
  tableName
151
165
  });
166
+ const conflictWrite = upsertConflictData ? transformForWrite({
167
+ adapter,
168
+ data: upsertConflictData,
169
+ enableAtomicWrites: false,
170
+ fields,
171
+ path,
172
+ tableName
173
+ }) : rowToInsert;
174
+ let nestedWrite = rowToInsert;
175
+ let isUpdatingAfterInsertConflict = false;
152
176
  if (customID) {
153
177
  rowToInsert.row.id = customID;
154
178
  }
@@ -168,19 +192,59 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
168
192
  rowToInsert.row.id = id;
169
193
  [insertedRow] = await adapter.insert({
170
194
  db,
171
- onConflictDoUpdate: {
172
- set: rowToInsert.row,
195
+ onConflictDoUpdate: where ? {
196
+ set: conflictWrite.row,
197
+ target,
198
+ where
199
+ } : {
200
+ set: conflictWrite.row,
173
201
  target
174
202
  },
175
203
  tableName,
176
204
  values: rowToInsert.row
177
205
  });
206
+ } else if (upsertConflictData && upsertTarget) {
207
+ // The parent conflict is atomic. Child-table replacements are serialized only
208
+ // when the caller's transaction spans this complete upsert.
209
+ const targetKey = Object.entries(adapter.tables[tableName]).find(([, column])=>column === target)?.[0];
210
+ const targetValue = targetKey ? rowToInsert.row[targetKey] : undefined;
211
+ if (!targetKey || typeof targetValue === 'undefined') {
212
+ throw new Error(`Could not resolve the upsert target value for table "${tableName}".`);
213
+ }
214
+ for(let attempt = 0; attempt < 10; attempt++){
215
+ ;
216
+ [insertedRow] = await adapter.insert({
217
+ db,
218
+ onConflictDoNothing: {
219
+ target
220
+ },
221
+ tableName,
222
+ values: rowToInsert.row
223
+ });
224
+ if (insertedRow) {
225
+ break;
226
+ }
227
+ const updateWhere = where ? and(eq(target, targetValue), where) : eq(target, targetValue);
228
+ const updatedRows = await db.update(adapter.tables[tableName]).set(conflictWrite.row).where(updateWhere).returning();
229
+ if (updatedRows[0]) {
230
+ insertedRow = updatedRows[0];
231
+ nestedWrite = conflictWrite;
232
+ isUpdatingAfterInsertConflict = true;
233
+ break;
234
+ }
235
+ if (where) {
236
+ return null;
237
+ }
238
+ }
239
+ if (!insertedRow) {
240
+ throw new Error(`Could not upsert a stable row in table "${tableName}".`);
241
+ }
178
242
  } else {
179
243
  ;
180
244
  [insertedRow] = await adapter.insert({
181
245
  db,
182
246
  onConflictDoUpdate: {
183
- set: rowToInsert.row,
247
+ set: conflictWrite.row,
184
248
  target,
185
249
  where
186
250
  },
@@ -188,6 +252,9 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
188
252
  values: rowToInsert.row
189
253
  });
190
254
  }
255
+ if (!insertedRow) {
256
+ return null;
257
+ }
191
258
  } else {
192
259
  // No main row data to update, just use the existing ID
193
260
  insertedRow = {
@@ -211,39 +278,44 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
211
278
  const numbersToInsert = [];
212
279
  const blocksToInsert = {};
213
280
  const selectsToInsert = {};
281
+ const localeTableName = `${tableName}${adapter.localesSuffix}`;
282
+ const shouldStampLocaleBranch = Boolean(adapter.rawTables[localeTableName]?.columns[branchField]);
214
283
  // If there are locale rows with data, add the parent and locale to each
215
- if (Object.keys(rowToInsert.locales).length > 0) {
216
- Object.entries(rowToInsert.locales).forEach(([locale, localeRow])=>{
284
+ if (Object.keys(nestedWrite.locales).length > 0) {
285
+ Object.entries(nestedWrite.locales).forEach(([locale, localeRow])=>{
217
286
  localeRow._parentID = insertedRow.id;
218
287
  localeRow._locale = locale;
288
+ if (shouldStampLocaleBranch) {
289
+ localeRow[branchField] = insertedRow[branchField] ?? data[branchField] ?? MAIN_BRANCH;
290
+ }
219
291
  localesToInsert.push(localeRow);
220
292
  });
221
293
  }
222
294
  // If there are relationships, add parent to each
223
- if (rowToInsert.relationships.length > 0) {
224
- rowToInsert.relationships.forEach((relation)=>{
295
+ if (nestedWrite.relationships.length > 0) {
296
+ nestedWrite.relationships.forEach((relation)=>{
225
297
  relation.parent = insertedRow.id;
226
298
  relationsToInsert.push(relation);
227
299
  });
228
300
  }
229
301
  // If there are texts, add parent to each
230
- if (rowToInsert.texts.length > 0) {
231
- rowToInsert.texts.forEach((textRow)=>{
302
+ if (nestedWrite.texts.length > 0) {
303
+ nestedWrite.texts.forEach((textRow)=>{
232
304
  textRow.parent = insertedRow.id;
233
305
  textsToInsert.push(textRow);
234
306
  });
235
307
  }
236
308
  // If there are numbers, add parent to each
237
- if (rowToInsert.numbers.length > 0) {
238
- rowToInsert.numbers.forEach((numberRow)=>{
309
+ if (nestedWrite.numbers.length > 0) {
310
+ nestedWrite.numbers.forEach((numberRow)=>{
239
311
  numberRow.parent = insertedRow.id;
240
312
  numbersToInsert.push(numberRow);
241
313
  });
242
314
  }
243
315
  // If there are selects, add parent to each, and then
244
316
  // store by table name and rows
245
- if (Object.keys(rowToInsert.selects).length > 0) {
246
- Object.entries(rowToInsert.selects).forEach(([selectTableName, selectRows])=>{
317
+ if (Object.keys(nestedWrite.selects).length > 0) {
318
+ Object.entries(nestedWrite.selects).forEach(([selectTableName, selectRows])=>{
247
319
  selectsToInsert[selectTableName] = [];
248
320
  selectRows.forEach((row)=>{
249
321
  if (typeof row.parent === 'undefined') {
@@ -255,8 +327,8 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
255
327
  }
256
328
  // If there are blocks, add parent to each, and then
257
329
  // store by table name and rows
258
- Object.keys(rowToInsert.blocks).forEach((tableName)=>{
259
- rowToInsert.blocks[tableName].forEach((blockRow)=>{
330
+ Object.keys(nestedWrite.blocks).forEach((tableName)=>{
331
+ nestedWrite.blocks[tableName].forEach((blockRow)=>{
260
332
  blockRow.row._parentID = insertedRow.id;
261
333
  if (!blocksToInsert[tableName]) {
262
334
  blocksToInsert[tableName] = [];
@@ -271,20 +343,36 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
271
343
  // INSERT LOCALES
272
344
  // //////////////////////////////////
273
345
  if (localesToInsert.length > 0) {
274
- const localeTableName = `${tableName}${adapter.localesSuffix}`;
275
346
  const localeTable = adapter.tables[`${tableName}${adapter.localesSuffix}`];
276
- if (operation === 'update') {
277
- await adapter.deleteWhere({
347
+ if (operation === 'update' && isUpdatingAfterInsertConflict) {
348
+ for (const localeRow of localesToInsert){
349
+ await adapter.insert({
350
+ db,
351
+ onConflictDoUpdate: {
352
+ set: localeRow,
353
+ target: [
354
+ localeTable._locale,
355
+ localeTable._parentID
356
+ ]
357
+ },
358
+ tableName: localeTableName,
359
+ values: localeRow
360
+ });
361
+ }
362
+ } else {
363
+ if (operation === 'update') {
364
+ await adapter.deleteWhere({
365
+ db,
366
+ tableName: localeTableName,
367
+ where: eq(localeTable._parentID, insertedRow.id)
368
+ });
369
+ }
370
+ await adapter.insert({
278
371
  db,
279
372
  tableName: localeTableName,
280
- where: eq(localeTable._parentID, insertedRow.id)
373
+ values: localesToInsert
281
374
  });
282
375
  }
283
- await adapter.insert({
284
- db,
285
- tableName: localeTableName,
286
- values: localesToInsert
287
- });
288
376
  }
289
377
  // //////////////////////////////////
290
378
  // INSERT RELATIONSHIPS
@@ -292,7 +380,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
292
380
  const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
293
381
  if (operation === 'update') {
294
382
  // Filter out specific item deletions (those with itemToRemove) from general path deletions
295
- const generalRelationshipDeletes = rowToInsert.relationshipsToDelete.filter((rel)=>!('itemToRemove' in rel));
383
+ const generalRelationshipDeletes = nestedWrite.relationshipsToDelete.filter((rel)=>!('itemToRemove' in rel));
296
384
  await deleteExistingRowsByPath({
297
385
  adapter,
298
386
  db,
@@ -317,9 +405,9 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
317
405
  // //////////////////////////////////
318
406
  // HANDLE RELATIONSHIP $push OPERATIONS
319
407
  // //////////////////////////////////
320
- if (rowToInsert.relationshipsToAppend.length > 0) {
408
+ if (nestedWrite.relationshipsToAppend.length > 0) {
321
409
  // Prepare all relationships for batch insert (order will be set after max query)
322
- const relationshipsToInsert = rowToInsert.relationshipsToAppend.map((rel)=>{
410
+ const relationshipsToInsert = nestedWrite.relationshipsToAppend.map((rel)=>{
323
411
  const parentId = id || insertedRow.id;
324
412
  const row = {
325
413
  parent: parentId,
@@ -417,10 +505,10 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
417
505
  // //////////////////////////////////
418
506
  // HANDLE RELATIONSHIP $remove OPERATIONS
419
507
  // //////////////////////////////////
420
- if (rowToInsert.relationshipsToDelete.some((rel)=>'itemToRemove' in rel)) {
508
+ if (nestedWrite.relationshipsToDelete.some((rel)=>'itemToRemove' in rel)) {
421
509
  const relationshipTable = adapter.tables[relationshipsTableName];
422
510
  if (relationshipTable) {
423
- for (const relToDelete of rowToInsert.relationshipsToDelete){
511
+ for (const relToDelete of nestedWrite.relationshipsToDelete){
424
512
  if ('itemToRemove' in relToDelete && relToDelete.itemToRemove) {
425
513
  const item = relToDelete.itemToRemove;
426
514
  const parentId = id || insertedRow.id;
@@ -474,7 +562,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
474
562
  pathColumnName: 'path',
475
563
  rows: [
476
564
  ...textsToInsert,
477
- ...rowToInsert.textsToDelete
565
+ ...nestedWrite.textsToDelete
478
566
  ],
479
567
  tableName: textsTableName
480
568
  });
@@ -500,7 +588,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
500
588
  pathColumnName: 'path',
501
589
  rows: [
502
590
  ...numbersToInsert,
503
- ...rowToInsert.numbersToDelete
591
+ ...nestedWrite.numbersToDelete
504
592
  ],
505
593
  tableName: numbersTableName
506
594
  });
@@ -517,7 +605,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
517
605
  // //////////////////////////////////
518
606
  const insertedBlockRows = {};
519
607
  if (operation === 'update') {
520
- for (const tableName of rowToInsert.blocksToDelete){
608
+ for (const tableName of nestedWrite.blocksToDelete){
521
609
  const blockTable = adapter.tables[tableName];
522
610
  await adapter.deleteWhere({
523
611
  db,
@@ -573,7 +661,7 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
573
661
  // INSERT ARRAYS RECURSIVELY
574
662
  // //////////////////////////////////
575
663
  if (operation === 'update') {
576
- for (const arrayTableName of Object.keys(rowToInsert.arrays)){
664
+ for (const arrayTableName of Object.keys(nestedWrite.arrays)){
577
665
  await deleteExistingArrayRows({
578
666
  adapter,
579
667
  db,
@@ -585,8 +673,8 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
585
673
  await insertArrays({
586
674
  adapter,
587
675
  arrays: [
588
- rowToInsert.arrays,
589
- rowToInsert.arraysToPush
676
+ nestedWrite.arrays,
677
+ nestedWrite.arraysToPush
590
678
  ],
591
679
  db,
592
680
  parentRows: [
@@ -598,14 +686,36 @@ customID, joinQuery: _joinQuery, operation, path = '', req, select, tableName, u
598
686
  // //////////////////////////////////
599
687
  // INSERT hasMany SELECTS
600
688
  // //////////////////////////////////
601
- for (const [selectTableName, tableRows] of Object.entries(selectsToInsert)){
689
+ const selectTableNames = new Set([
690
+ ...Object.keys(nestedWrite.selectsToDelete),
691
+ ...Object.keys(selectsToInsert)
692
+ ]);
693
+ for (const selectTableName of selectTableNames){
694
+ const tableRows = selectsToInsert[selectTableName] ?? [];
602
695
  const selectTable = adapter.tables[selectTableName];
603
696
  if (operation === 'update') {
604
- await adapter.deleteWhere({
605
- db,
606
- tableName: selectTableName,
607
- where: eq(selectTable.parent, insertedRow.id)
608
- });
697
+ for (const selectToDelete of nestedWrite.selectsToDelete[selectTableName] ?? []){
698
+ let selectParent = selectToDelete.parent ?? insertedRow.id;
699
+ if ((typeof selectParent === 'number' || typeof selectParent === 'string') && selectParent in arraysBlocksUUIDMap) {
700
+ selectParent = arraysBlocksUUIDMap[selectParent];
701
+ }
702
+ const deleteConstraints = [
703
+ eq(selectTable.parent, selectParent)
704
+ ];
705
+ if (typeof selectToDelete.locale === 'string') {
706
+ if (!selectTable.locale) {
707
+ throw new Error(`Could not limit the select replacement for table "${selectTableName}" to locale "${selectToDelete.locale}".`);
708
+ }
709
+ deleteConstraints.push(eq(selectTable.locale, selectToDelete.locale));
710
+ } else if (selectTable.locale) {
711
+ deleteConstraints.push(isNull(selectTable.locale));
712
+ }
713
+ await adapter.deleteWhere({
714
+ db,
715
+ tableName: selectTableName,
716
+ where: and(...deleteConstraints)
717
+ });
718
+ }
609
719
  }
610
720
  if (Object.keys(arraysBlocksUUIDMap).length > 0) {
611
721
  tableRows.forEach((row)=>{