@payloadcms/db-postgres 3.0.0-beta.4 → 3.0.0-beta.40

Sign up to get free protection for your applications and to get access to all the features.
Files changed (198) hide show
  1. package/dist/connect.d.ts.map +1 -1
  2. package/dist/connect.js +12 -7
  3. package/dist/connect.js.map +1 -1
  4. package/dist/count.d.ts +3 -0
  5. package/dist/count.d.ts.map +1 -0
  6. package/dist/count.js +41 -0
  7. package/dist/count.js.map +1 -0
  8. package/dist/create.d.ts.map +1 -1
  9. package/dist/create.js +3 -5
  10. package/dist/create.js.map +1 -1
  11. package/dist/createGlobal.d.ts.map +1 -1
  12. package/dist/createGlobal.js +3 -5
  13. package/dist/createGlobal.js.map +1 -1
  14. package/dist/createGlobalVersion.d.ts +1 -1
  15. package/dist/createGlobalVersion.d.ts.map +1 -1
  16. package/dist/createGlobalVersion.js +2 -6
  17. package/dist/createGlobalVersion.js.map +1 -1
  18. package/dist/createMigration.d.ts.map +1 -1
  19. package/dist/createMigration.js +58 -37
  20. package/dist/createMigration.js.map +1 -1
  21. package/dist/createVersion.d.ts.map +1 -1
  22. package/dist/createVersion.js +10 -18
  23. package/dist/createVersion.js.map +1 -1
  24. package/dist/deleteMany.d.ts.map +1 -1
  25. package/dist/deleteMany.js +2 -5
  26. package/dist/deleteMany.js.map +1 -1
  27. package/dist/deleteOne.d.ts.map +1 -1
  28. package/dist/deleteOne.js +4 -7
  29. package/dist/deleteOne.js.map +1 -1
  30. package/dist/deleteVersions.d.ts.map +1 -1
  31. package/dist/deleteVersions.js +2 -6
  32. package/dist/deleteVersions.js.map +1 -1
  33. package/dist/destroy.d.ts.map +1 -1
  34. package/dist/destroy.js +11 -7
  35. package/dist/destroy.js.map +1 -1
  36. package/dist/exports/migration-utils.d.ts +2 -0
  37. package/dist/exports/migration-utils.d.ts.map +1 -0
  38. package/dist/exports/migration-utils.js +3 -0
  39. package/dist/exports/migration-utils.js.map +1 -0
  40. package/dist/find/buildFindManyArgs.d.ts +5 -1
  41. package/dist/find/buildFindManyArgs.d.ts.map +1 -1
  42. package/dist/find/buildFindManyArgs.js +4 -3
  43. package/dist/find/buildFindManyArgs.js.map +1 -1
  44. package/dist/find/findMany.d.ts.map +1 -1
  45. package/dist/find/findMany.js +5 -16
  46. package/dist/find/findMany.js.map +1 -1
  47. package/dist/find/traverseFields.d.ts +3 -2
  48. package/dist/find/traverseFields.d.ts.map +1 -1
  49. package/dist/find/traverseFields.js +26 -24
  50. package/dist/find/traverseFields.js.map +1 -1
  51. package/dist/find.d.ts.map +1 -1
  52. package/dist/find.js +2 -5
  53. package/dist/find.js.map +1 -1
  54. package/dist/findGlobal.d.ts.map +1 -1
  55. package/dist/findGlobal.js +2 -5
  56. package/dist/findGlobal.js.map +1 -1
  57. package/dist/findGlobalVersions.d.ts.map +1 -1
  58. package/dist/findGlobalVersions.js +2 -6
  59. package/dist/findGlobalVersions.js.map +1 -1
  60. package/dist/findOne.d.ts.map +1 -1
  61. package/dist/findOne.js +2 -5
  62. package/dist/findOne.js.map +1 -1
  63. package/dist/findVersions.d.ts.map +1 -1
  64. package/dist/findVersions.js +2 -6
  65. package/dist/findVersions.js.map +1 -1
  66. package/dist/index.d.ts.map +1 -1
  67. package/dist/index.js +17 -8
  68. package/dist/index.js.map +1 -1
  69. package/dist/init.d.ts.map +1 -1
  70. package/dist/init.js +19 -22
  71. package/dist/init.js.map +1 -1
  72. package/dist/migrate.js.map +1 -1
  73. package/dist/migrateDown.js.map +1 -1
  74. package/dist/migrateFresh.js.map +1 -1
  75. package/dist/migrateRefresh.js.map +1 -1
  76. package/dist/migrateReset.js.map +1 -1
  77. package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
  78. package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
  79. package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
  80. package/dist/predefinedMigrations/relationships-v2-v3.mjs +11 -0
  81. package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts +20 -0
  82. package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -0
  83. package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js +170 -0
  84. package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -0
  85. package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts +11 -0
  86. package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts.map +1 -0
  87. package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js +191 -0
  88. package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js.map +1 -0
  89. package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts +3 -0
  90. package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts.map +1 -0
  91. package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js +56 -0
  92. package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js.map +1 -0
  93. package/dist/predefinedMigrations/v2-v3/index.d.ts +24 -0
  94. package/dist/predefinedMigrations/v2-v3/index.d.ts.map +1 -0
  95. package/dist/predefinedMigrations/v2-v3/index.js +211 -0
  96. package/dist/predefinedMigrations/v2-v3/index.js.map +1 -0
  97. package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts +19 -0
  98. package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts.map +1 -0
  99. package/dist/predefinedMigrations/v2-v3/migrateRelationships.js +57 -0
  100. package/dist/predefinedMigrations/v2-v3/migrateRelationships.js.map +1 -0
  101. package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts +23 -0
  102. package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts.map +1 -0
  103. package/dist/predefinedMigrations/v2-v3/traverseFields.js +85 -0
  104. package/dist/predefinedMigrations/v2-v3/traverseFields.js.map +1 -0
  105. package/dist/predefinedMigrations/v2-v3/types.d.ts +9 -0
  106. package/dist/predefinedMigrations/v2-v3/types.d.ts.map +1 -0
  107. package/dist/predefinedMigrations/v2-v3/types.js +6 -0
  108. package/dist/predefinedMigrations/v2-v3/types.js.map +1 -0
  109. package/dist/queries/buildAndOrConditions.d.ts +3 -4
  110. package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
  111. package/dist/queries/buildAndOrConditions.js +1 -2
  112. package/dist/queries/buildAndOrConditions.js.map +1 -1
  113. package/dist/queries/buildQuery.d.ts +1 -2
  114. package/dist/queries/buildQuery.d.ts.map +1 -1
  115. package/dist/queries/buildQuery.js +1 -5
  116. package/dist/queries/buildQuery.js.map +1 -1
  117. package/dist/queries/getTableColumnFromPath.d.ts +3 -4
  118. package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
  119. package/dist/queries/getTableColumnFromPath.js +248 -143
  120. package/dist/queries/getTableColumnFromPath.js.map +1 -1
  121. package/dist/queries/parseParams.d.ts +3 -4
  122. package/dist/queries/parseParams.d.ts.map +1 -1
  123. package/dist/queries/parseParams.js +7 -6
  124. package/dist/queries/parseParams.js.map +1 -1
  125. package/dist/queries/selectDistinct.d.ts +3 -4
  126. package/dist/queries/selectDistinct.d.ts.map +1 -1
  127. package/dist/queries/selectDistinct.js +3 -14
  128. package/dist/queries/selectDistinct.js.map +1 -1
  129. package/dist/queryDrafts.d.ts.map +1 -1
  130. package/dist/queryDrafts.js +2 -6
  131. package/dist/queryDrafts.js.map +1 -1
  132. package/dist/schema/build.d.ts +11 -6
  133. package/dist/schema/build.d.ts.map +1 -1
  134. package/dist/schema/build.js +208 -124
  135. package/dist/schema/build.js.map +1 -1
  136. package/dist/schema/{getTableName.d.ts → createTableName.d.ts} +5 -7
  137. package/dist/schema/createTableName.d.ts.map +1 -0
  138. package/dist/schema/createTableName.js +31 -0
  139. package/dist/schema/createTableName.js.map +1 -0
  140. package/dist/schema/traverseFields.d.ts +4 -6
  141. package/dist/schema/traverseFields.d.ts.map +1 -1
  142. package/dist/schema/traverseFields.js +145 -61
  143. package/dist/schema/traverseFields.js.map +1 -1
  144. package/dist/schema/validateExistingBlockIsIdentical.d.ts +2 -1
  145. package/dist/schema/validateExistingBlockIsIdentical.d.ts.map +1 -1
  146. package/dist/schema/validateExistingBlockIsIdentical.js +13 -7
  147. package/dist/schema/validateExistingBlockIsIdentical.js.map +1 -1
  148. package/dist/transactions/beginTransaction.d.ts.map +1 -1
  149. package/dist/transactions/beginTransaction.js +4 -0
  150. package/dist/transactions/beginTransaction.js.map +1 -1
  151. package/dist/transform/read/index.d.ts +3 -1
  152. package/dist/transform/read/index.d.ts.map +1 -1
  153. package/dist/transform/read/index.js +2 -1
  154. package/dist/transform/read/index.js.map +1 -1
  155. package/dist/transform/read/relationship.d.ts.map +1 -1
  156. package/dist/transform/read/relationship.js +0 -4
  157. package/dist/transform/read/relationship.js.map +1 -1
  158. package/dist/transform/read/traverseFields.d.ts +6 -1
  159. package/dist/transform/read/traverseFields.d.ts.map +1 -1
  160. package/dist/transform/read/traverseFields.js +69 -55
  161. package/dist/transform/read/traverseFields.js.map +1 -1
  162. package/dist/transform/write/array.d.ts.map +1 -1
  163. package/dist/transform/write/array.js.map +1 -1
  164. package/dist/transform/write/blocks.js +1 -1
  165. package/dist/transform/write/blocks.js.map +1 -1
  166. package/dist/transform/write/traverseFields.d.ts.map +1 -1
  167. package/dist/transform/write/traverseFields.js +22 -5
  168. package/dist/transform/write/traverseFields.js.map +1 -1
  169. package/dist/types.d.ts +7 -7
  170. package/dist/types.d.ts.map +1 -1
  171. package/dist/types.js.map +1 -1
  172. package/dist/update.d.ts.map +1 -1
  173. package/dist/update.js +3 -7
  174. package/dist/update.js.map +1 -1
  175. package/dist/updateGlobal.d.ts.map +1 -1
  176. package/dist/updateGlobal.js +2 -5
  177. package/dist/updateGlobal.js.map +1 -1
  178. package/dist/updateGlobalVersion.d.ts.map +1 -1
  179. package/dist/updateGlobalVersion.js +2 -6
  180. package/dist/updateGlobalVersion.js.map +1 -1
  181. package/dist/updateVersion.d.ts.map +1 -1
  182. package/dist/updateVersion.js +2 -6
  183. package/dist/updateVersion.js.map +1 -1
  184. package/dist/upsertRow/index.d.ts +1 -1
  185. package/dist/upsertRow/index.d.ts.map +1 -1
  186. package/dist/upsertRow/index.js +9 -6
  187. package/dist/upsertRow/index.js.map +1 -1
  188. package/dist/upsertRow/insertArrays.d.ts.map +1 -1
  189. package/dist/upsertRow/insertArrays.js +3 -3
  190. package/dist/upsertRow/insertArrays.js.map +1 -1
  191. package/dist/upsertRow/types.d.ts +7 -2
  192. package/dist/upsertRow/types.d.ts.map +1 -1
  193. package/dist/upsertRow/types.js.map +1 -1
  194. package/package.json +29 -27
  195. package/dist/schema/getTableName.d.ts.map +0 -1
  196. package/dist/schema/getTableName.js +0 -31
  197. package/dist/schema/getTableName.js.map +0 -1
  198. package/src/index.ts +0 -156
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/schema/createTableName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload/database'\n\nimport { APIError } from 'payload/errors'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { PostgresAdapter } from '../types.js'\n\ntype Args = {\n adapter: PostgresAdapter\n /** The collection, global or field config **/\n config: {\n dbName?: DBIdentifierName\n enumName?: DBIdentifierName\n name?: string\n slug?: string\n }\n /** For nested tables passed for the user custom dbName functions to handle their own iterations */\n parentTableName?: string\n /** For sub tables (array for example) this needs to include the parentTableName */\n prefix?: string\n /** For tables based on fields that could have both enumName and dbName (ie: select with hasMany), default: 'dbName' */\n target?: 'dbName' | 'enumName'\n throwValidationError?: boolean\n /** Adds the versions suffix to the default table name - should only be used on the base collection to avoid duplicate suffixing */\n versions?: boolean\n /** Adds the versions suffix to custom dbName only - this is used while creating blocks / selects / arrays / etc */\n versionsCustomName?: boolean\n}\n\n/**\n * Used to name database enums and tables\n * Returns the table or enum name for a given entity\n */\nexport const createTableName = ({\n adapter,\n config: { name, slug },\n config,\n parentTableName,\n prefix = '',\n target = 'dbName',\n throwValidationError = false,\n versions = false,\n versionsCustomName = false,\n}: Args): string => {\n let customNameDefinition = config[target]\n\n let defaultTableName = `${prefix}${toSnakeCase(name ?? slug)}`\n\n if (versions) defaultTableName = `_${defaultTableName}${adapter.versionsSuffix}`\n\n let customTableNameResult: string\n\n if (!customNameDefinition && target === 'enumName') {\n customNameDefinition = config['dbName']\n }\n\n if (customNameDefinition) {\n customTableNameResult =\n typeof customNameDefinition === 'function'\n ? customNameDefinition({ tableName: parentTableName })\n : customNameDefinition\n\n if (versionsCustomName)\n customTableNameResult = `_${customTableNameResult}${adapter.versionsSuffix}`\n }\n\n const result = customTableNameResult || defaultTableName\n\n adapter.tableNameMap.set(defaultTableName, result)\n\n if (!throwValidationError) {\n return result\n }\n\n if (result.length > 63) {\n throw new APIError(\n `Exceeded max identifier length for table or enum name of 63 characters. Invalid name: ${result}`,\n )\n }\n\n return result\n}\n"],"names":["APIError","toSnakeCase","createTableName","adapter","config","name","slug","parentTableName","prefix","target","throwValidationError","versions","versionsCustomName","customNameDefinition","defaultTableName","versionsSuffix","customTableNameResult","tableName","result","tableNameMap","set","length"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,QAAQ,QAAQ,iBAAgB;AACzC,OAAOC,iBAAiB,gBAAe;AA0BvC;;;CAGC,GACD,OAAO,MAAMC,kBAAkB,CAAC,EAC9BC,OAAO,EACPC,QAAQ,EAAEC,IAAI,EAAEC,IAAI,EAAE,EACtBF,MAAM,EACNG,eAAe,EACfC,SAAS,EAAE,EACXC,SAAS,QAAQ,EACjBC,uBAAuB,KAAK,EAC5BC,WAAW,KAAK,EAChBC,qBAAqB,KAAK,EACrB;IACL,IAAIC,uBAAuBT,MAAM,CAACK,OAAO;IAEzC,IAAIK,mBAAmB,CAAC,EAAEN,OAAO,EAAEP,YAAYI,QAAQC,MAAM,CAAC;IAE9D,IAAIK,UAAUG,mBAAmB,CAAC,CAAC,EAAEA,iBAAiB,EAAEX,QAAQY,cAAc,CAAC,CAAC;IAEhF,IAAIC;IAEJ,IAAI,CAACH,wBAAwBJ,WAAW,YAAY;QAClDI,uBAAuBT,MAAM,CAAC,SAAS;IACzC;IAEA,IAAIS,sBAAsB;QACxBG,wBACE,OAAOH,yBAAyB,aAC5BA,qBAAqB;YAAEI,WAAWV;QAAgB,KAClDM;QAEN,IAAID,oBACFI,wBAAwB,CAAC,CAAC,EAAEA,sBAAsB,EAAEb,QAAQY,cAAc,CAAC,CAAC;IAChF;IAEA,MAAMG,SAASF,yBAAyBF;IAExCX,QAAQgB,YAAY,CAACC,GAAG,CAACN,kBAAkBI;IAE3C,IAAI,CAACR,sBAAsB;QACzB,OAAOQ;IACT;IAEA,IAAIA,OAAOG,MAAM,GAAG,IAAI;QACtB,MAAM,IAAIrB,SACR,CAAC,sFAAsF,EAAEkB,OAAO,CAAC;IAErG;IAEA,OAAOA;AACT,EAAC"}
@@ -1,11 +1,9 @@
1
1
  import type { IndexBuilder, PgColumnBuilder } from 'drizzle-orm/pg-core';
2
2
  import type { Field, TabAsField } from 'payload/types';
3
3
  import type { GenericColumns, PostgresAdapter } from '../types.js';
4
+ import type { RelationMap } from './build.js';
4
5
  type Args = {
5
6
  adapter: PostgresAdapter;
6
- buildNumbers: boolean;
7
- buildRelationships: boolean;
8
- buildTexts: boolean;
9
7
  columnPrefix?: string;
10
8
  columns: Record<string, PgColumnBuilder>;
11
9
  disableNotNull: boolean;
@@ -18,9 +16,9 @@ type Args = {
18
16
  localesIndexes: Record<string, (cols: GenericColumns) => IndexBuilder>;
19
17
  newTableName: string;
20
18
  parentTableName: string;
21
- relationsToBuild: Map<string, string>;
19
+ relationsToBuild: RelationMap;
22
20
  relationships: Set<string>;
23
- rootRelationsToBuild?: Map<string, string>;
21
+ rootRelationsToBuild?: RelationMap;
24
22
  rootTableIDColType: string;
25
23
  rootTableName: string;
26
24
  versions: boolean;
@@ -33,6 +31,6 @@ type Result = {
33
31
  hasManyNumberField: 'index' | boolean;
34
32
  hasManyTextField: 'index' | boolean;
35
33
  };
36
- export declare const traverseFields: ({ adapter, buildNumbers, buildRelationships, buildTexts, columnPrefix, columns, disableNotNull, disableUnique, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationsToBuild, relationships, rootRelationsToBuild, rootTableIDColType, rootTableName, versions, }: Args) => Result;
34
+ export declare const traverseFields: ({ adapter, columnPrefix, columns, disableNotNull, disableUnique, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationsToBuild, relationships, rootRelationsToBuild, rootTableIDColType, rootTableName, versions, }: Args) => Result;
37
35
  export {};
38
36
  //# sourceMappingURL=traverseFields.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../../src/schema/traverseFields.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAA2B,MAAM,qBAAqB,CAAA;AACjG,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAqBtD,OAAO,KAAK,EAAE,cAAc,EAAU,eAAe,EAAE,MAAM,aAAa,CAAA;AAU1E,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,YAAY,EAAE,OAAO,CAAA;IACrB,kBAAkB,EAAE,OAAO,CAAA;IAC3B,UAAU,EAAE,OAAO,CAAA;IACnB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IACxC,cAAc,EAAE,OAAO,CAAA;IACvB,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,MAAM,EAAE,CAAC,KAAK,GAAG,UAAU,CAAC,EAAE,CAAA;IAC9B,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,YAAY,CAAC,CAAA;IAC/D,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC/C,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,YAAY,CAAC,CAAA;IACtE,YAAY,EAAE,MAAM,CAAA;IACpB,eAAe,EAAE,MAAM,CAAA;IACvB,gBAAgB,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACrC,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC1B,oBAAoB,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC1C,kBAAkB,EAAE,MAAM,CAAA;IAC1B,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,iBAAiB,EAAE,OAAO,CAAA;IAC1B,2BAA2B,EAAE,OAAO,CAAA;IACpC,yBAAyB,EAAE,OAAO,CAAA;IAClC,6BAA6B,EAAE,OAAO,CAAA;IACtC,kBAAkB,EAAE,OAAO,GAAG,OAAO,CAAA;IACrC,gBAAgB,EAAE,OAAO,GAAG,OAAO,CAAA;CACpC,CAAA;AAED,eAAO,MAAM,cAAc,sUAuBxB,IAAI,KAAG,MAknBT,CAAA"}
1
+ {"version":3,"file":"traverseFields.d.ts","sourceRoot":"","sources":["../../src/schema/traverseFields.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AACxE,OAAO,KAAK,EAAE,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAA;AAsBtD,OAAO,KAAK,EAAE,cAAc,EAAU,eAAe,EAAE,MAAM,aAAa,CAAA;AAC1E,OAAO,KAAK,EAAmB,WAAW,EAAE,MAAM,YAAY,CAAA;AAU9D,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,eAAe,CAAA;IACxB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IACxC,cAAc,EAAE,OAAO,CAAA;IACvB,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,WAAW,CAAC,EAAE,MAAM,CAAA;IACpB,MAAM,EAAE,CAAC,KAAK,GAAG,UAAU,CAAC,EAAE,CAAA;IAC9B,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,YAAY,CAAC,CAAA;IAC/D,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAA;IAC/C,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,IAAI,EAAE,cAAc,KAAK,YAAY,CAAC,CAAA;IACtE,YAAY,EAAE,MAAM,CAAA;IACpB,eAAe,EAAE,MAAM,CAAA;IACvB,gBAAgB,EAAE,WAAW,CAAA;IAC7B,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC1B,oBAAoB,CAAC,EAAE,WAAW,CAAA;IAClC,kBAAkB,EAAE,MAAM,CAAA;IAC1B,aAAa,EAAE,MAAM,CAAA;IACrB,QAAQ,EAAE,OAAO,CAAA;CAClB,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,iBAAiB,EAAE,OAAO,CAAA;IAC1B,2BAA2B,EAAE,OAAO,CAAA;IACpC,yBAAyB,EAAE,OAAO,CAAA;IAClC,6BAA6B,EAAE,OAAO,CAAA;IACtC,kBAAkB,EAAE,OAAO,GAAG,OAAO,CAAA;IACrC,gBAAgB,EAAE,OAAO,GAAG,OAAO,CAAA;CACpC,CAAA;AAED,eAAO,MAAM,cAAc,wRAoBxB,IAAI,KAAG,MA4rBT,CAAA"}
@@ -1,16 +1,16 @@
1
1
  /* eslint-disable no-param-reassign */ import { relations } from 'drizzle-orm';
2
- import { PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, boolean, index, integer, jsonb, numeric, pgEnum, text, timestamp, varchar } from 'drizzle-orm/pg-core';
2
+ import { PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, boolean, foreignKey, index, integer, jsonb, numeric, pgEnum, text, timestamp, varchar } from 'drizzle-orm/pg-core';
3
3
  import { InvalidConfiguration } from 'payload/errors';
4
4
  import { fieldAffectsData, optionIsObject } from 'payload/types';
5
5
  import toSnakeCase from 'to-snake-case';
6
6
  import { hasLocalesTable } from '../utilities/hasLocalesTable.js';
7
7
  import { buildTable } from './build.js';
8
8
  import { createIndex } from './createIndex.js';
9
- import { getTableName } from './getTableName.js';
9
+ import { createTableName } from './createTableName.js';
10
10
  import { idToUUID } from './idToUUID.js';
11
11
  import { parentIDColumnMap } from './parentIDColumnMap.js';
12
12
  import { validateExistingBlockIsIdentical } from './validateExistingBlockIsIdentical.js';
13
- export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buildTexts, columnPrefix, columns, disableNotNull, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationsToBuild, relationships, rootRelationsToBuild, rootTableIDColType, rootTableName, versions })=>{
13
+ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationsToBuild, relationships, rootRelationsToBuild, rootTableIDColType, rootTableName, versions })=>{
14
14
  const throwValidationError = true;
15
15
  let hasLocalizedField = false;
16
16
  let hasLocalizedRelationshipField = false;
@@ -33,7 +33,7 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
33
33
  fieldName = `${fieldPrefix?.replace('.', '_') || ''}${field.name}`;
34
34
  // If field is localized,
35
35
  // add the column to the locale table instead of main table
36
- if (adapter.payload.config.localization && (field.localized || forceLocalized)) {
36
+ if (adapter.payload.config.localization && (field.localized || forceLocalized) && field.type !== 'array' && field.type !== 'blocks' && ('hasMany' in field && field.hasMany !== true || !('hasMany' in field))) {
37
37
  hasLocalizedField = true;
38
38
  targetTable = localesColumns;
39
39
  targetIndexes = localesIndexes;
@@ -130,14 +130,13 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
130
130
  case 'radio':
131
131
  case 'select':
132
132
  {
133
- const enumName = getTableName({
133
+ const enumName = createTableName({
134
134
  adapter,
135
135
  config: field,
136
136
  parentTableName: newTableName,
137
137
  prefix: `enum_${newTableName}_`,
138
138
  target: 'enumName',
139
- throwValidationError,
140
- versions
139
+ throwValidationError
141
140
  });
142
141
  adapter.enums[enumName] = pgEnum(enumName, field.options.map((option)=>{
143
142
  if (optionIsObject(option)) {
@@ -146,23 +145,30 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
146
145
  return option;
147
146
  }));
148
147
  if (field.type === 'select' && field.hasMany) {
149
- const selectTableName = getTableName({
148
+ const selectTableName = createTableName({
150
149
  adapter,
151
150
  config: field,
152
151
  parentTableName: newTableName,
153
152
  prefix: `${newTableName}_`,
154
153
  throwValidationError,
155
- versions
154
+ versionsCustomName: versions
156
155
  });
157
156
  const baseColumns = {
158
157
  order: integer('order').notNull(),
159
- parent: parentIDColumnMap[parentIDColType]('parent_id').references(()=>adapter.tables[parentTableName].id, {
160
- onDelete: 'cascade'
161
- }).notNull(),
158
+ parent: parentIDColumnMap[parentIDColType]('parent_id').notNull(),
162
159
  value: adapter.enums[enumName]('value')
163
160
  };
164
161
  const baseExtraConfig = {
165
162
  orderIdx: (cols)=>index(`${selectTableName}_order_idx`).on(cols.order),
163
+ parentFk: (cols)=>foreignKey({
164
+ name: `${selectTableName}_parent_fk`,
165
+ columns: [
166
+ cols.parent
167
+ ],
168
+ foreignColumns: [
169
+ adapter.tables[parentTableName].id
170
+ ]
171
+ }).onDelete('cascade'),
166
172
  parentIdx: (cols)=>index(`${selectTableName}_parent_idx`).on(cols.parent)
167
173
  };
168
174
  if (field.localized) {
@@ -179,24 +185,27 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
179
185
  disableNotNull,
180
186
  disableUnique,
181
187
  fields: [],
188
+ rootTableName,
182
189
  tableName: selectTableName,
183
190
  versions
184
191
  });
185
- relationsToBuild.set(fieldName, selectTableName);
186
- const selectTableRelations = relations(adapter.tables[selectTableName], ({ one })=>{
187
- const result = {
192
+ relationsToBuild.set(fieldName, {
193
+ type: 'many',
194
+ // selects have their own localized table, independent of the base table.
195
+ localized: false,
196
+ target: selectTableName
197
+ });
198
+ adapter.relations[`relations_${selectTableName}`] = relations(adapter.tables[selectTableName], ({ one })=>({
188
199
  parent: one(adapter.tables[parentTableName], {
189
200
  fields: [
190
201
  adapter.tables[selectTableName].parent
191
202
  ],
192
203
  references: [
193
204
  adapter.tables[parentTableName].id
194
- ]
205
+ ],
206
+ relationName: fieldName
195
207
  })
196
- };
197
- return result;
198
- });
199
- adapter.relations[`relation_${selectTableName}`] = selectTableRelations;
208
+ }));
200
209
  } else {
201
210
  targetTable[fieldName] = adapter.enums[enumName](fieldName);
202
211
  }
@@ -210,21 +219,29 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
210
219
  case 'array':
211
220
  {
212
221
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
213
- const arrayTableName = getTableName({
222
+ const arrayTableName = createTableName({
214
223
  adapter,
215
224
  config: field,
216
225
  parentTableName: newTableName,
217
226
  prefix: `${newTableName}_`,
218
- throwValidationError
227
+ throwValidationError,
228
+ versionsCustomName: versions
219
229
  });
220
230
  const baseColumns = {
221
231
  _order: integer('_order').notNull(),
222
- _parentID: parentIDColumnMap[parentIDColType]('_parent_id').references(()=>adapter.tables[parentTableName].id, {
223
- onDelete: 'cascade'
224
- }).notNull()
232
+ _parentID: parentIDColumnMap[parentIDColType]('_parent_id').notNull()
225
233
  };
226
234
  const baseExtraConfig = {
227
235
  _orderIdx: (cols)=>index(`${arrayTableName}_order_idx`).on(cols._order),
236
+ _parentIDFk: (cols)=>foreignKey({
237
+ name: `${arrayTableName}_parent_id_fk`,
238
+ columns: [
239
+ cols['_parentID']
240
+ ],
241
+ foreignColumns: [
242
+ adapter.tables[parentTableName].id
243
+ ]
244
+ }).onDelete('cascade'),
228
245
  _parentIDIdx: (cols)=>index(`${arrayTableName}_parent_id_idx`).on(cols._parentID)
229
246
  };
230
247
  if (field.localized && adapter.payload.config.localization) {
@@ -251,8 +268,13 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
251
268
  if (subHasManyNumberField) {
252
269
  if (!hasManyNumberField || subHasManyNumberField === 'index') hasManyNumberField = subHasManyNumberField;
253
270
  }
254
- relationsToBuild.set(fieldName, arrayTableName);
255
- const arrayTableRelations = relations(adapter.tables[arrayTableName], ({ many, one })=>{
271
+ relationsToBuild.set(fieldName, {
272
+ type: 'many',
273
+ // arrays have their own localized table, independent of the base table.
274
+ localized: false,
275
+ target: arrayTableName
276
+ });
277
+ adapter.relations[`relations_${arrayTableName}`] = relations(adapter.tables[arrayTableName], ({ many, one })=>{
256
278
  const result = {
257
279
  _parentID: one(adapter.tables[parentTableName], {
258
280
  fields: [
@@ -260,42 +282,68 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
260
282
  ],
261
283
  references: [
262
284
  adapter.tables[parentTableName].id
263
- ]
285
+ ],
286
+ relationName: fieldName
264
287
  })
265
288
  };
266
289
  if (hasLocalesTable(field.fields)) {
267
- result._locales = many(adapter.tables[`${arrayTableName}${adapter.localesSuffix}`]);
290
+ result._locales = many(adapter.tables[`${arrayTableName}${adapter.localesSuffix}`], {
291
+ relationName: '_locales'
292
+ });
268
293
  }
269
- subRelationsToBuild.forEach((val, key)=>{
270
- result[key] = many(adapter.tables[val]);
294
+ subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
295
+ if (type === 'one') {
296
+ const arrayWithLocalized = localized ? `${arrayTableName}${adapter.localesSuffix}` : arrayTableName;
297
+ result[key] = one(adapter.tables[target], {
298
+ fields: [
299
+ adapter.tables[arrayWithLocalized][key]
300
+ ],
301
+ references: [
302
+ adapter.tables[target].id
303
+ ],
304
+ relationName: key
305
+ });
306
+ }
307
+ if (type === 'many') {
308
+ result[key] = many(adapter.tables[target], {
309
+ relationName: key
310
+ });
311
+ }
271
312
  });
272
313
  return result;
273
314
  });
274
- adapter.relations[`relations_${arrayTableName}`] = arrayTableRelations;
275
315
  break;
276
316
  }
277
317
  case 'blocks':
278
318
  {
279
319
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
280
320
  field.blocks.forEach((block)=>{
281
- const blockTableName = getTableName({
321
+ const blockTableName = createTableName({
282
322
  adapter,
283
323
  config: block,
284
324
  parentTableName: rootTableName,
285
325
  prefix: `${rootTableName}_blocks_`,
286
- throwValidationError
326
+ throwValidationError,
327
+ versionsCustomName: versions
287
328
  });
288
329
  if (!adapter.tables[blockTableName]) {
289
330
  const baseColumns = {
290
331
  _order: integer('_order').notNull(),
291
- _parentID: parentIDColumnMap[rootTableIDColType]('_parent_id').references(()=>adapter.tables[rootTableName].id, {
292
- onDelete: 'cascade'
293
- }).notNull(),
332
+ _parentID: parentIDColumnMap[rootTableIDColType]('_parent_id').notNull(),
294
333
  _path: text('_path').notNull()
295
334
  };
296
335
  const baseExtraConfig = {
297
336
  _orderIdx: (cols)=>index(`${blockTableName}_order_idx`).on(cols._order),
298
337
  _parentIDIdx: (cols)=>index(`${blockTableName}_parent_id_idx`).on(cols._parentID),
338
+ _parentIdFk: (cols)=>foreignKey({
339
+ name: `${blockTableName}_parent_id_fk`,
340
+ columns: [
341
+ cols._parentID
342
+ ],
343
+ foreignColumns: [
344
+ adapter.tables[rootTableName].id
345
+ ]
346
+ }).onDelete('cascade'),
299
347
  _pathIdx: (cols)=>index(`${blockTableName}_path_idx`).on(cols._path)
300
348
  };
301
349
  if (field.localized && adapter.payload.config.localization) {
@@ -322,7 +370,7 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
322
370
  if (subHasManyNumberField) {
323
371
  if (!hasManyNumberField || subHasManyNumberField === 'index') hasManyNumberField = subHasManyNumberField;
324
372
  }
325
- const blockTableRelations = relations(adapter.tables[blockTableName], ({ many, one })=>{
373
+ adapter.relations[`relations_${blockTableName}`] = relations(adapter.tables[blockTableName], ({ many, one })=>{
326
374
  const result = {
327
375
  _parentID: one(adapter.tables[rootTableName], {
328
376
  fields: [
@@ -330,28 +378,52 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
330
378
  ],
331
379
  references: [
332
380
  adapter.tables[rootTableName].id
333
- ]
381
+ ],
382
+ relationName: `_blocks_${block.slug}`
334
383
  })
335
384
  };
336
385
  if (hasLocalesTable(block.fields)) {
337
- result._locales = many(adapter.tables[`${blockTableName}${adapter.localesSuffix}`]);
386
+ result._locales = many(adapter.tables[`${blockTableName}${adapter.localesSuffix}`], {
387
+ relationName: '_locales'
388
+ });
338
389
  }
339
- subRelationsToBuild.forEach((val, key)=>{
340
- result[key] = many(adapter.tables[val]);
390
+ subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
391
+ if (type === 'one') {
392
+ const blockWithLocalized = localized ? `${blockTableName}${adapter.localesSuffix}` : blockTableName;
393
+ result[key] = one(adapter.tables[target], {
394
+ fields: [
395
+ adapter.tables[blockWithLocalized][key]
396
+ ],
397
+ references: [
398
+ adapter.tables[target].id
399
+ ],
400
+ relationName: key
401
+ });
402
+ }
403
+ if (type === 'many') {
404
+ result[key] = many(adapter.tables[target], {
405
+ relationName: key
406
+ });
407
+ }
341
408
  });
342
409
  return result;
343
410
  });
344
- adapter.relations[`relations_${blockTableName}`] = blockTableRelations;
345
411
  } else if (process.env.NODE_ENV !== 'production' && !versions) {
346
412
  validateExistingBlockIsIdentical({
347
413
  block,
348
414
  localized: field.localized,
349
415
  rootTableName,
350
- table: adapter.tables[blockTableName]
416
+ table: adapter.tables[blockTableName],
417
+ tableLocales: adapter.tables[`${blockTableName}${adapter.localesSuffix}`]
351
418
  });
352
419
  }
353
- adapter.blockTableNames[`${rootTableName}.${toSnakeCase(block.slug)}`] = blockTableName;
354
- rootRelationsToBuild.set(`_blocks_${block.slug}`, blockTableName);
420
+ // blocks relationships are defined from the collection or globals table down to the block, bypassing any subBlocks
421
+ rootRelationsToBuild.set(`_blocks_${block.slug}`, {
422
+ type: 'many',
423
+ // blocks are not localized on the parent table
424
+ localized: false,
425
+ target: blockTableName
426
+ });
355
427
  });
356
428
  break;
357
429
  }
@@ -361,9 +433,6 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
361
433
  if (!('name' in field)) {
362
434
  const { hasLocalizedField: groupHasLocalizedField, hasLocalizedManyNumberField: groupHasLocalizedManyNumberField, hasLocalizedManyTextField: groupHasLocalizedManyTextField, hasLocalizedRelationshipField: groupHasLocalizedRelationshipField, hasManyNumberField: groupHasManyNumberField, hasManyTextField: groupHasManyTextField } = traverseFields({
363
435
  adapter,
364
- buildNumbers,
365
- buildRelationships,
366
- buildTexts,
367
436
  columnPrefix,
368
437
  columns,
369
438
  disableNotNull,
@@ -394,9 +463,6 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
394
463
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
395
464
  const { hasLocalizedField: groupHasLocalizedField, hasLocalizedManyNumberField: groupHasLocalizedManyNumberField, hasLocalizedManyTextField: groupHasLocalizedManyTextField, hasLocalizedRelationshipField: groupHasLocalizedRelationshipField, hasManyNumberField: groupHasManyNumberField, hasManyTextField: groupHasManyTextField } = traverseFields({
396
465
  adapter,
397
- buildNumbers,
398
- buildRelationships,
399
- buildTexts,
400
466
  columnPrefix: `${columnName}_`,
401
467
  columns,
402
468
  disableNotNull: disableNotNullFromHere,
@@ -429,9 +495,6 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
429
495
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
430
496
  const { hasLocalizedField: tabHasLocalizedField, hasLocalizedManyNumberField: tabHasLocalizedManyNumberField, hasLocalizedManyTextField: tabHasLocalizedManyTextField, hasLocalizedRelationshipField: tabHasLocalizedRelationshipField, hasManyNumberField: tabHasManyNumberField, hasManyTextField: tabHasManyTextField } = traverseFields({
431
497
  adapter,
432
- buildNumbers,
433
- buildRelationships,
434
- buildTexts,
435
498
  columnPrefix,
436
499
  columns,
437
500
  disableNotNull: disableNotNullFromHere,
@@ -468,9 +531,6 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
468
531
  const disableNotNullFromHere = Boolean(field.admin?.condition) || disableNotNull;
469
532
  const { hasLocalizedField: rowHasLocalizedField, hasLocalizedManyNumberField: rowHasLocalizedManyNumberField, hasLocalizedManyTextField: rowHasLocalizedManyTextField, hasLocalizedRelationshipField: rowHasLocalizedRelationshipField, hasManyNumberField: rowHasManyNumberField, hasManyTextField: rowHasManyTextField } = traverseFields({
470
533
  adapter,
471
- buildNumbers,
472
- buildRelationships,
473
- buildTexts,
474
534
  columnPrefix,
475
535
  columns,
476
536
  disableNotNull: disableNotNullFromHere,
@@ -481,7 +541,7 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
481
541
  indexes,
482
542
  localesColumns,
483
543
  localesIndexes,
484
- newTableName: parentTableName,
544
+ newTableName,
485
545
  parentTableName,
486
546
  relationsToBuild,
487
547
  relationships,
@@ -502,10 +562,34 @@ export const traverseFields = ({ adapter, buildNumbers, buildRelationships, buil
502
562
  case 'upload':
503
563
  if (Array.isArray(field.relationTo)) {
504
564
  field.relationTo.forEach((relation)=>relationships.add(relation));
505
- } else {
565
+ } else if (field.type === 'relationship' && field.hasMany) {
506
566
  relationships.add(field.relationTo);
567
+ } else {
568
+ // simple relationships get a column on the targetTable with a foreign key to the relationTo table
569
+ const relationshipConfig = adapter.payload.collections[field.relationTo].config;
570
+ const tableName = adapter.tableNameMap.get(toSnakeCase(field.relationTo));
571
+ // get the id type of the related collection
572
+ let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer';
573
+ const relatedCollectionCustomID = relationshipConfig.fields.find((field)=>fieldAffectsData(field) && field.name === 'id');
574
+ if (relatedCollectionCustomID?.type === 'number') colType = 'numeric';
575
+ if (relatedCollectionCustomID?.type === 'text') colType = 'varchar';
576
+ // make the foreign key column for relationship using the correct id column type
577
+ targetTable[fieldName] = parentIDColumnMap[colType](`${columnName}_id`).references(()=>adapter.tables[tableName].id, {
578
+ onDelete: 'set null'
579
+ });
580
+ // add relationship to table
581
+ relationsToBuild.set(fieldName, {
582
+ type: 'one',
583
+ localized: adapter.payload.config.localization && field.localized,
584
+ target: tableName
585
+ });
586
+ // add notNull when not required
587
+ if (!disableNotNull && field.required && !field.admin?.condition) {
588
+ targetTable[fieldName].notNull();
589
+ }
590
+ break;
507
591
  }
508
- if (field.localized && adapter.payload.config.localization) {
592
+ if (adapter.payload.config.localization && field.localized) {
509
593
  hasLocalizedRelationshipField = true;
510
594
  }
511
595
  break;