@payloadcms/drizzle 3.7.0 → 3.7.1-canary.2ce70a3

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 (90) hide show
  1. package/dist/exports/postgres.d.ts +2 -0
  2. package/dist/exports/postgres.d.ts.map +1 -1
  3. package/dist/exports/postgres.js +2 -0
  4. package/dist/exports/postgres.js.map +1 -1
  5. package/dist/index.d.ts +3 -0
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +3 -0
  8. package/dist/index.js.map +1 -1
  9. package/dist/postgres/columnToCodeConverter.d.ts +3 -0
  10. package/dist/postgres/columnToCodeConverter.d.ts.map +1 -0
  11. package/dist/postgres/columnToCodeConverter.js +64 -0
  12. package/dist/postgres/columnToCodeConverter.js.map +1 -0
  13. package/dist/postgres/init.d.ts.map +1 -1
  14. package/dist/postgres/init.js +16 -85
  15. package/dist/postgres/init.js.map +1 -1
  16. package/dist/postgres/schema/buildDrizzleTable.d.ts +7 -0
  17. package/dist/postgres/schema/buildDrizzleTable.d.ts.map +1 -0
  18. package/dist/postgres/schema/buildDrizzleTable.js +114 -0
  19. package/dist/postgres/schema/buildDrizzleTable.js.map +1 -0
  20. package/dist/postgres/schema/setColumnID.d.ts +2 -10
  21. package/dist/postgres/schema/setColumnID.d.ts.map +1 -1
  22. package/dist/postgres/schema/setColumnID.js +21 -5
  23. package/dist/postgres/schema/setColumnID.js.map +1 -1
  24. package/dist/{postgres/schema → schema}/build.d.ts +12 -7
  25. package/dist/schema/build.d.ts.map +1 -0
  26. package/dist/schema/build.js +603 -0
  27. package/dist/schema/build.js.map +1 -0
  28. package/dist/schema/buildDrizzleRelations.d.ts +5 -0
  29. package/dist/schema/buildDrizzleRelations.d.ts.map +1 -0
  30. package/dist/schema/buildDrizzleRelations.js +26 -0
  31. package/dist/schema/buildDrizzleRelations.js.map +1 -0
  32. package/dist/schema/buildRawSchema.d.ts +9 -0
  33. package/dist/schema/buildRawSchema.d.ts.map +1 -0
  34. package/dist/schema/buildRawSchema.js +97 -0
  35. package/dist/schema/buildRawSchema.js.map +1 -0
  36. package/dist/schema/idToUUID.d.ts.map +1 -0
  37. package/dist/schema/idToUUID.js.map +1 -0
  38. package/dist/{postgres/schema → schema}/traverseFields.d.ts +9 -9
  39. package/dist/schema/traverseFields.d.ts.map +1 -0
  40. package/dist/{postgres/schema → schema}/traverseFields.js +343 -174
  41. package/dist/schema/traverseFields.js.map +1 -0
  42. package/dist/schema/withDefault.d.ts +4 -0
  43. package/dist/schema/withDefault.d.ts.map +1 -0
  44. package/dist/{postgres/schema → schema}/withDefault.js +8 -5
  45. package/dist/schema/withDefault.js.map +1 -0
  46. package/dist/transform/read/traverseFields.d.ts.map +1 -1
  47. package/dist/transform/read/traverseFields.js +7 -0
  48. package/dist/transform/read/traverseFields.js.map +1 -1
  49. package/dist/types.d.ts +124 -2
  50. package/dist/types.d.ts.map +1 -1
  51. package/dist/types.js.map +1 -1
  52. package/dist/utilities/createSchemaGenerator.d.ts +11 -0
  53. package/dist/utilities/createSchemaGenerator.d.ts.map +1 -0
  54. package/dist/utilities/createSchemaGenerator.js +216 -0
  55. package/dist/utilities/createSchemaGenerator.js.map +1 -0
  56. package/dist/utilities/executeSchemaHooks.js +1 -1
  57. package/dist/utilities/executeSchemaHooks.js.map +1 -1
  58. package/dist/utilities/validateExistingBlockIsIdentical.d.ts +3 -2
  59. package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
  60. package/dist/utilities/validateExistingBlockIsIdentical.js +2 -2
  61. package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
  62. package/package.json +3 -3
  63. package/dist/postgres/createMigration.d.ts +0 -3
  64. package/dist/postgres/createMigration.d.ts.map +0 -1
  65. package/dist/postgres/createMigration.js +0 -91
  66. package/dist/postgres/createMigration.js.map +0 -1
  67. package/dist/postgres/getMigrationTemplate.d.ts +0 -4
  68. package/dist/postgres/getMigrationTemplate.d.ts.map +0 -1
  69. package/dist/postgres/getMigrationTemplate.js +0 -13
  70. package/dist/postgres/getMigrationTemplate.js.map +0 -1
  71. package/dist/postgres/schema/build.d.ts.map +0 -1
  72. package/dist/postgres/schema/build.js +0 -404
  73. package/dist/postgres/schema/build.js.map +0 -1
  74. package/dist/postgres/schema/createIndex.d.ts +0 -11
  75. package/dist/postgres/schema/createIndex.d.ts.map +0 -1
  76. package/dist/postgres/schema/createIndex.js +0 -20
  77. package/dist/postgres/schema/createIndex.js.map +0 -1
  78. package/dist/postgres/schema/idToUUID.d.ts.map +0 -1
  79. package/dist/postgres/schema/idToUUID.js.map +0 -1
  80. package/dist/postgres/schema/parentIDColumnMap.d.ts +0 -4
  81. package/dist/postgres/schema/parentIDColumnMap.d.ts.map +0 -1
  82. package/dist/postgres/schema/parentIDColumnMap.js +0 -9
  83. package/dist/postgres/schema/parentIDColumnMap.js.map +0 -1
  84. package/dist/postgres/schema/traverseFields.d.ts.map +0 -1
  85. package/dist/postgres/schema/traverseFields.js.map +0 -1
  86. package/dist/postgres/schema/withDefault.d.ts +0 -4
  87. package/dist/postgres/schema/withDefault.d.ts.map +0 -1
  88. package/dist/postgres/schema/withDefault.js.map +0 -1
  89. /package/dist/{postgres/schema → schema}/idToUUID.d.ts +0 -0
  90. /package/dist/{postgres/schema → schema}/idToUUID.js +0 -0
@@ -1,14 +1,18 @@
1
- import type { PgColumnBuilder } from 'drizzle-orm/pg-core';
2
1
  import type { FlattenedField } from 'payload';
3
- import type { BaseExtraConfig, BasePostgresAdapter, RelationMap } from '../types.js';
2
+ import type { DrizzleAdapter, IDType, RawColumn, RawForeignKey, RawIndex, RelationMap, SetColumnID } from '../types.js';
4
3
  type Args = {
5
- adapter: BasePostgresAdapter;
6
- baseColumns?: Record<string, PgColumnBuilder>;
4
+ adapter: DrizzleAdapter;
5
+ baseColumns?: Record<string, RawColumn>;
7
6
  /**
8
7
  * After table is created, run these functions to add extra config to the table
9
8
  * ie. indexes, multiple columns, etc
10
9
  */
11
- baseExtraConfig?: BaseExtraConfig;
10
+ baseForeignKeys?: Record<string, RawForeignKey>;
11
+ /**
12
+ * After table is created, run these functions to add extra config to the table
13
+ * ie. indexes, multiple columns, etc
14
+ */
15
+ baseIndexes?: Record<string, RawIndex>;
12
16
  buildNumbers?: boolean;
13
17
  buildRelationships?: boolean;
14
18
  disableNotNull: boolean;
@@ -17,9 +21,10 @@ type Args = {
17
21
  fields: FlattenedField[];
18
22
  rootRelationships?: Set<string>;
19
23
  rootRelationsToBuild?: RelationMap;
20
- rootTableIDColType?: string;
24
+ rootTableIDColType?: IDType;
21
25
  rootTableName?: string;
22
26
  rootUniqueRelationships?: Set<string>;
27
+ setColumnID: SetColumnID;
23
28
  tableName: string;
24
29
  timestamps?: boolean;
25
30
  versions: boolean;
@@ -37,6 +42,6 @@ type Result = {
37
42
  hasManyTextField: 'index' | boolean;
38
43
  relationsToBuild: RelationMap;
39
44
  };
40
- export declare const buildTable: ({ adapter, baseColumns, baseExtraConfig, disableNotNull, disableRelsTableUnique, disableUnique, fields, rootRelationships, rootRelationsToBuild, rootTableIDColType, rootTableName: incomingRootTableName, rootUniqueRelationships, tableName, timestamps, versions, withinLocalizedArrayOrBlock, }: Args) => Result;
45
+ export declare const buildTable: ({ adapter, baseColumns, baseForeignKeys, baseIndexes, disableNotNull, disableRelsTableUnique, disableUnique, fields, rootRelationships, rootRelationsToBuild, rootTableIDColType, rootTableName: incomingRootTableName, rootUniqueRelationships, setColumnID, tableName, timestamps, versions, withinLocalizedArrayOrBlock, }: Args) => Result;
41
46
  export {};
42
47
  //# sourceMappingURL=build.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build.d.ts","sourceRoot":"","sources":["../../src/schema/build.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,SAAS,CAAA;AAI7C,OAAO,KAAK,EACV,cAAc,EACd,MAAM,EACN,SAAS,EACT,aAAa,EACb,QAAQ,EAGR,WAAW,EACX,WAAW,EACZ,MAAM,aAAa,CAAA;AAMpB,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,cAAc,CAAA;IACvB,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,SAAS,CAAC,CAAA;IACvC;;;OAGG;IACH,eAAe,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,aAAa,CAAC,CAAA;IAC/C;;;OAGG;IACH,WAAW,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAA;IACtC,YAAY,CAAC,EAAE,OAAO,CAAA;IACtB,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B,cAAc,EAAE,OAAO,CAAA;IACvB,sBAAsB,CAAC,EAAE,OAAO,CAAA;IAChC,aAAa,EAAE,OAAO,CAAA;IACtB,MAAM,EAAE,cAAc,EAAE,CAAA;IACxB,iBAAiB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IAC/B,oBAAoB,CAAC,EAAE,WAAW,CAAA;IAClC,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,aAAa,CAAC,EAAE,MAAM,CAAA;IACtB,uBAAuB,CAAC,EAAE,GAAG,CAAC,MAAM,CAAC,CAAA;IACrC,WAAW,EAAE,WAAW,CAAA;IACxB,SAAS,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,OAAO,CAAA;IACpB,QAAQ,EAAE,OAAO,CAAA;IACjB;;;OAGG;IACH,2BAA2B,CAAC,EAAE,OAAO,CAAA;CACtC,CAAA;AAED,KAAK,MAAM,GAAG;IACZ,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;IACnC,gBAAgB,EAAE,WAAW,CAAA;CAC9B,CAAA;AAED,eAAO,MAAM,UAAU,kUAmBpB,IAAI,KAAG,MA6mBT,CAAA"}
@@ -0,0 +1,603 @@
1
+ import toSnakeCase from 'to-snake-case';
2
+ import { createTableName } from '../createTableName.js';
3
+ import { buildIndexName } from '../utilities/buildIndexName.js';
4
+ import { traverseFields } from './traverseFields.js';
5
+ export const buildTable = ({ adapter, baseColumns = {}, baseForeignKeys = {}, baseIndexes = {}, disableNotNull, disableRelsTableUnique = false, disableUnique = false, fields, rootRelationships, rootRelationsToBuild, rootTableIDColType, rootTableName: incomingRootTableName, rootUniqueRelationships, setColumnID, tableName, timestamps, versions, withinLocalizedArrayOrBlock })=>{
6
+ const isRoot = !incomingRootTableName;
7
+ const rootTableName = incomingRootTableName || tableName;
8
+ const columns = baseColumns;
9
+ const indexes = baseIndexes;
10
+ const localesColumns = {};
11
+ const localesIndexes = {};
12
+ let localesTable;
13
+ let textsTable;
14
+ let numbersTable;
15
+ // Relationships to the base collection
16
+ const relationships = rootRelationships || new Set();
17
+ // Unique relationships to the base collection
18
+ const uniqueRelationships = rootUniqueRelationships || new Set();
19
+ let relationshipsTable;
20
+ // Drizzle relations
21
+ const relationsToBuild = new Map();
22
+ const idColType = setColumnID({
23
+ adapter,
24
+ columns,
25
+ fields
26
+ });
27
+ const { hasLocalizedField, hasLocalizedManyNumberField, hasLocalizedManyTextField, hasLocalizedRelationshipField, hasManyNumberField, hasManyTextField } = traverseFields({
28
+ adapter,
29
+ columns,
30
+ disableNotNull,
31
+ disableRelsTableUnique,
32
+ disableUnique,
33
+ fields,
34
+ indexes,
35
+ localesColumns,
36
+ localesIndexes,
37
+ newTableName: tableName,
38
+ parentTableName: tableName,
39
+ relationships,
40
+ relationsToBuild,
41
+ rootRelationsToBuild: rootRelationsToBuild || relationsToBuild,
42
+ rootTableIDColType: rootTableIDColType || idColType,
43
+ rootTableName,
44
+ setColumnID,
45
+ uniqueRelationships,
46
+ versions,
47
+ withinLocalizedArrayOrBlock
48
+ });
49
+ // split the relationsToBuild by localized and non-localized
50
+ const localizedRelations = new Map();
51
+ const nonLocalizedRelations = new Map();
52
+ relationsToBuild.forEach(({ type, localized, relationName, target }, key)=>{
53
+ const map = localized ? localizedRelations : nonLocalizedRelations;
54
+ map.set(key, {
55
+ type,
56
+ relationName,
57
+ target
58
+ });
59
+ });
60
+ if (timestamps) {
61
+ columns.createdAt = {
62
+ name: 'created_at',
63
+ type: 'timestamp',
64
+ defaultNow: true,
65
+ mode: 'string',
66
+ notNull: true,
67
+ precision: 3,
68
+ withTimezone: true
69
+ };
70
+ columns.updatedAt = {
71
+ name: 'updated_at',
72
+ type: 'timestamp',
73
+ defaultNow: true,
74
+ mode: 'string',
75
+ notNull: true,
76
+ precision: 3,
77
+ withTimezone: true
78
+ };
79
+ }
80
+ const table = {
81
+ name: tableName,
82
+ columns,
83
+ foreignKeys: baseForeignKeys,
84
+ indexes
85
+ };
86
+ adapter.rawTables[tableName] = table;
87
+ if (hasLocalizedField || localizedRelations.size) {
88
+ const localeTableName = `${tableName}${adapter.localesSuffix}`;
89
+ localesColumns.id = {
90
+ name: 'id',
91
+ type: 'serial',
92
+ primaryKey: true
93
+ };
94
+ localesColumns._locale = {
95
+ name: '_locale',
96
+ type: 'enum',
97
+ locale: true,
98
+ notNull: true
99
+ };
100
+ localesColumns._parentID = {
101
+ name: '_parent_id',
102
+ type: idColType,
103
+ notNull: true
104
+ };
105
+ localesIndexes._localeParent = {
106
+ name: `${localeTableName}_locale_parent_id_unique`,
107
+ on: [
108
+ '_locale',
109
+ '_parentID'
110
+ ],
111
+ unique: true
112
+ };
113
+ localesTable = {
114
+ name: localeTableName,
115
+ columns: localesColumns,
116
+ foreignKeys: {
117
+ _parentIdFk: {
118
+ name: `${localeTableName}_parent_id_fk`,
119
+ columns: [
120
+ '_parentID'
121
+ ],
122
+ foreignColumns: [
123
+ {
124
+ name: 'id',
125
+ table: tableName
126
+ }
127
+ ],
128
+ onDelete: 'cascade'
129
+ }
130
+ },
131
+ indexes: localesIndexes
132
+ };
133
+ adapter.rawTables[localeTableName] = localesTable;
134
+ const localeRelations = {
135
+ _parentID: {
136
+ type: 'one',
137
+ fields: [
138
+ {
139
+ name: '_parentID',
140
+ table: localeTableName
141
+ }
142
+ ],
143
+ references: [
144
+ 'id'
145
+ ],
146
+ relationName: '_locales',
147
+ to: tableName
148
+ }
149
+ };
150
+ localizedRelations.forEach(({ type, target }, key)=>{
151
+ if (type === 'one') {
152
+ localeRelations[key] = {
153
+ type: 'one',
154
+ fields: [
155
+ {
156
+ name: key,
157
+ table: localeTableName
158
+ }
159
+ ],
160
+ references: [
161
+ 'id'
162
+ ],
163
+ relationName: key,
164
+ to: target
165
+ };
166
+ }
167
+ if (type === 'many') {
168
+ localeRelations[key] = {
169
+ type: 'many',
170
+ relationName: key,
171
+ to: target
172
+ };
173
+ }
174
+ });
175
+ adapter.rawRelations[localeTableName] = localeRelations;
176
+ }
177
+ if (isRoot) {
178
+ if (hasManyTextField) {
179
+ const textsTableName = `${rootTableName}_texts`;
180
+ const columns = {
181
+ id: {
182
+ name: 'id',
183
+ type: 'serial',
184
+ primaryKey: true
185
+ },
186
+ order: {
187
+ name: 'order',
188
+ type: 'integer',
189
+ notNull: true
190
+ },
191
+ parent: {
192
+ name: 'parent_id',
193
+ type: idColType,
194
+ notNull: true
195
+ },
196
+ path: {
197
+ name: 'path',
198
+ type: 'varchar',
199
+ notNull: true
200
+ },
201
+ text: {
202
+ name: 'text',
203
+ type: 'varchar'
204
+ }
205
+ };
206
+ if (hasLocalizedManyTextField) {
207
+ columns.locale = {
208
+ name: 'locale',
209
+ type: 'enum',
210
+ locale: true
211
+ };
212
+ }
213
+ const textsTableIndexes = {
214
+ orderParentIdx: {
215
+ name: `${textsTableName}_order_parent_idx`,
216
+ on: [
217
+ 'order',
218
+ 'parent'
219
+ ]
220
+ }
221
+ };
222
+ if (hasManyTextField === 'index') {
223
+ textsTableIndexes.text_idx = {
224
+ name: `${textsTableName}_text_idx`,
225
+ on: 'text'
226
+ };
227
+ }
228
+ if (hasLocalizedManyTextField) {
229
+ textsTableIndexes.localeParent = {
230
+ name: `${textsTableName}_locale_parent`,
231
+ on: [
232
+ 'locale',
233
+ 'parent'
234
+ ]
235
+ };
236
+ }
237
+ textsTable = {
238
+ name: textsTableName,
239
+ columns,
240
+ foreignKeys: {
241
+ parentFk: {
242
+ name: `${textsTableName}_parent_fk`,
243
+ columns: [
244
+ 'parent'
245
+ ],
246
+ foreignColumns: [
247
+ {
248
+ name: 'id',
249
+ table: tableName
250
+ }
251
+ ],
252
+ onDelete: 'cascade'
253
+ }
254
+ },
255
+ indexes: textsTableIndexes
256
+ };
257
+ adapter.rawTables[textsTableName] = textsTable;
258
+ adapter.rawRelations[textsTableName] = {
259
+ parent: {
260
+ type: 'one',
261
+ fields: [
262
+ {
263
+ name: 'parent',
264
+ table: textsTableName
265
+ }
266
+ ],
267
+ references: [
268
+ 'id'
269
+ ],
270
+ relationName: '_texts',
271
+ to: tableName
272
+ }
273
+ };
274
+ }
275
+ if (hasManyNumberField) {
276
+ const numbersTableName = `${rootTableName}_numbers`;
277
+ const columns = {
278
+ id: {
279
+ name: 'id',
280
+ type: 'serial',
281
+ primaryKey: true
282
+ },
283
+ number: {
284
+ name: 'number',
285
+ type: 'numeric'
286
+ },
287
+ order: {
288
+ name: 'order',
289
+ type: 'integer',
290
+ notNull: true
291
+ },
292
+ parent: {
293
+ name: 'parent_id',
294
+ type: idColType,
295
+ notNull: true
296
+ },
297
+ path: {
298
+ name: 'path',
299
+ type: 'varchar',
300
+ notNull: true
301
+ }
302
+ };
303
+ if (hasLocalizedManyNumberField) {
304
+ columns.locale = {
305
+ name: 'locale',
306
+ type: 'enum',
307
+ locale: true
308
+ };
309
+ }
310
+ const numbersTableIndexes = {
311
+ orderParentIdx: {
312
+ name: `${numbersTableName}_order_parent_idx`,
313
+ on: [
314
+ 'order',
315
+ 'parent'
316
+ ]
317
+ }
318
+ };
319
+ if (hasManyNumberField === 'index') {
320
+ numbersTableIndexes.numberIdx = {
321
+ name: `${numbersTableName}_number_idx`,
322
+ on: 'number'
323
+ };
324
+ }
325
+ if (hasLocalizedManyNumberField) {
326
+ numbersTableIndexes.localeParent = {
327
+ name: `${numbersTableName}_locale_parent`,
328
+ on: [
329
+ 'locale',
330
+ 'parent'
331
+ ]
332
+ };
333
+ }
334
+ numbersTable = {
335
+ name: numbersTableName,
336
+ columns,
337
+ foreignKeys: {
338
+ parentFk: {
339
+ name: `${numbersTableName}_parent_fk`,
340
+ columns: [
341
+ 'parent'
342
+ ],
343
+ foreignColumns: [
344
+ {
345
+ name: 'id',
346
+ table: tableName
347
+ }
348
+ ],
349
+ onDelete: 'cascade'
350
+ }
351
+ },
352
+ indexes: numbersTableIndexes
353
+ };
354
+ adapter.rawTables[numbersTableName] = numbersTable;
355
+ adapter.rawRelations[numbersTableName] = {
356
+ parent: {
357
+ type: 'one',
358
+ fields: [
359
+ {
360
+ name: 'parent',
361
+ table: numbersTableName
362
+ }
363
+ ],
364
+ references: [
365
+ 'id'
366
+ ],
367
+ relationName: '_numbers',
368
+ to: tableName
369
+ }
370
+ };
371
+ }
372
+ if (relationships.size) {
373
+ const relationshipColumns = {
374
+ id: {
375
+ name: 'id',
376
+ type: 'serial',
377
+ primaryKey: true
378
+ },
379
+ order: {
380
+ name: 'order',
381
+ type: 'integer'
382
+ },
383
+ parent: {
384
+ name: 'parent_id',
385
+ type: idColType,
386
+ notNull: true
387
+ },
388
+ path: {
389
+ name: 'path',
390
+ type: 'varchar',
391
+ notNull: true
392
+ }
393
+ };
394
+ if (hasLocalizedRelationshipField) {
395
+ relationshipColumns.locale = {
396
+ name: 'locale',
397
+ type: 'enum',
398
+ locale: true
399
+ };
400
+ }
401
+ const relationshipsTableName = `${tableName}${adapter.relationshipsSuffix}`;
402
+ const relationshipIndexes = {
403
+ order: {
404
+ name: `${relationshipsTableName}_order_idx`,
405
+ on: 'order'
406
+ },
407
+ parentIdx: {
408
+ name: `${relationshipsTableName}_parent_idx`,
409
+ on: 'parent'
410
+ },
411
+ pathIdx: {
412
+ name: `${relationshipsTableName}_path_idx`,
413
+ on: 'path'
414
+ }
415
+ };
416
+ if (hasLocalizedRelationshipField) {
417
+ relationshipIndexes.localeIdx = {
418
+ name: `${relationshipsTableName}_locale_idx`,
419
+ on: 'locale'
420
+ };
421
+ }
422
+ const relationshipForeignKeys = {
423
+ parentFk: {
424
+ name: `${relationshipsTableName}_parent_fk`,
425
+ columns: [
426
+ 'parent'
427
+ ],
428
+ foreignColumns: [
429
+ {
430
+ name: 'id',
431
+ table: tableName
432
+ }
433
+ ],
434
+ onDelete: 'cascade'
435
+ }
436
+ };
437
+ relationships.forEach((relationTo)=>{
438
+ const relationshipConfig = adapter.payload.collections[relationTo].config;
439
+ const formattedRelationTo = createTableName({
440
+ adapter,
441
+ config: relationshipConfig,
442
+ throwValidationError: true
443
+ });
444
+ let colType = adapter.idType === 'uuid' ? 'uuid' : 'integer';
445
+ const relatedCollectionCustomIDType = adapter.payload.collections[relationshipConfig.slug]?.customIDType;
446
+ if (relatedCollectionCustomIDType === 'number') {
447
+ colType = 'numeric';
448
+ }
449
+ if (relatedCollectionCustomIDType === 'text') {
450
+ colType = 'varchar';
451
+ }
452
+ const colName = `${relationTo}ID`;
453
+ relationshipColumns[colName] = {
454
+ name: `${formattedRelationTo}_id`,
455
+ type: colType
456
+ };
457
+ relationshipForeignKeys[`${relationTo}IdFk`] = {
458
+ name: `${relationshipsTableName}_${toSnakeCase(relationTo)}_fk`,
459
+ columns: [
460
+ colName
461
+ ],
462
+ foreignColumns: [
463
+ {
464
+ name: 'id',
465
+ table: formattedRelationTo
466
+ }
467
+ ],
468
+ onDelete: 'cascade'
469
+ };
470
+ const indexColumns = [
471
+ colName
472
+ ];
473
+ const unique = !disableUnique && uniqueRelationships.has(relationTo);
474
+ if (unique) {
475
+ indexColumns.push('path');
476
+ }
477
+ if (hasLocalizedRelationshipField) {
478
+ indexColumns.push('locale');
479
+ }
480
+ const indexName = buildIndexName({
481
+ name: `${relationshipsTableName}_${formattedRelationTo}_id`,
482
+ adapter
483
+ });
484
+ relationshipIndexes[indexName] = {
485
+ name: indexName,
486
+ on: indexColumns,
487
+ unique
488
+ };
489
+ });
490
+ relationshipsTable = {
491
+ name: relationshipsTableName,
492
+ columns: relationshipColumns,
493
+ foreignKeys: relationshipForeignKeys,
494
+ indexes: relationshipIndexes
495
+ };
496
+ adapter.rawTables[relationshipsTableName] = relationshipsTable;
497
+ const relationshipsTableRelations = {
498
+ parent: {
499
+ type: 'one',
500
+ fields: [
501
+ {
502
+ name: 'parent',
503
+ table: relationshipsTableName
504
+ }
505
+ ],
506
+ references: [
507
+ 'id'
508
+ ],
509
+ relationName: '_rels',
510
+ to: tableName
511
+ }
512
+ };
513
+ relationships.forEach((relationTo)=>{
514
+ const relatedTableName = createTableName({
515
+ adapter,
516
+ config: adapter.payload.collections[relationTo].config,
517
+ throwValidationError: true
518
+ });
519
+ const idColumnName = `${relationTo}ID`;
520
+ relationshipsTableRelations[idColumnName] = {
521
+ type: 'one',
522
+ fields: [
523
+ {
524
+ name: idColumnName,
525
+ table: relationshipsTableName
526
+ }
527
+ ],
528
+ references: [
529
+ 'id'
530
+ ],
531
+ relationName: relationTo,
532
+ to: relatedTableName
533
+ };
534
+ });
535
+ adapter.rawRelations[relationshipsTableName] = relationshipsTableRelations;
536
+ }
537
+ }
538
+ const tableRelations = {};
539
+ nonLocalizedRelations.forEach(({ type, relationName, target }, key)=>{
540
+ if (type === 'one') {
541
+ tableRelations[key] = {
542
+ type: 'one',
543
+ fields: [
544
+ {
545
+ name: key,
546
+ table: tableName
547
+ }
548
+ ],
549
+ references: [
550
+ 'id'
551
+ ],
552
+ relationName: key,
553
+ to: target
554
+ };
555
+ }
556
+ if (type === 'many') {
557
+ tableRelations[key] = {
558
+ type: 'many',
559
+ relationName: relationName || key,
560
+ to: target
561
+ };
562
+ }
563
+ });
564
+ if (hasLocalizedField) {
565
+ tableRelations._locales = {
566
+ type: 'many',
567
+ relationName: '_locales',
568
+ to: localesTable.name
569
+ };
570
+ }
571
+ if (isRoot && textsTable) {
572
+ tableRelations._texts = {
573
+ type: 'many',
574
+ relationName: '_texts',
575
+ to: textsTable.name
576
+ };
577
+ }
578
+ if (isRoot && numbersTable) {
579
+ tableRelations._numbers = {
580
+ type: 'many',
581
+ relationName: '_numbers',
582
+ to: numbersTable.name
583
+ };
584
+ }
585
+ if (relationships.size && relationshipsTable) {
586
+ tableRelations._rels = {
587
+ type: 'many',
588
+ relationName: '_rels',
589
+ to: relationshipsTable.name
590
+ };
591
+ }
592
+ adapter.rawRelations[tableName] = tableRelations;
593
+ return {
594
+ hasLocalizedManyNumberField,
595
+ hasLocalizedManyTextField,
596
+ hasLocalizedRelationshipField,
597
+ hasManyNumberField,
598
+ hasManyTextField,
599
+ relationsToBuild
600
+ };
601
+ };
602
+
603
+ //# sourceMappingURL=build.js.map