@payloadcms/drizzle 3.7.0 → 3.7.1-canary.6b01088
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/postgres/init.d.ts.map +1 -1
- package/dist/postgres/init.js +16 -85
- package/dist/postgres/init.js.map +1 -1
- package/dist/postgres/schema/buildDrizzleTable.d.ts +7 -0
- package/dist/postgres/schema/buildDrizzleTable.d.ts.map +1 -0
- package/dist/postgres/schema/buildDrizzleTable.js +114 -0
- package/dist/postgres/schema/buildDrizzleTable.js.map +1 -0
- package/dist/postgres/schema/setColumnID.d.ts +2 -10
- package/dist/postgres/schema/setColumnID.d.ts.map +1 -1
- package/dist/postgres/schema/setColumnID.js +21 -5
- package/dist/postgres/schema/setColumnID.js.map +1 -1
- package/dist/{postgres/schema → schema}/build.d.ts +12 -7
- package/dist/schema/build.d.ts.map +1 -0
- package/dist/schema/build.js +603 -0
- package/dist/schema/build.js.map +1 -0
- package/dist/schema/buildDrizzleRelations.d.ts +5 -0
- package/dist/schema/buildDrizzleRelations.d.ts.map +1 -0
- package/dist/schema/buildDrizzleRelations.js +26 -0
- package/dist/schema/buildDrizzleRelations.js.map +1 -0
- package/dist/schema/buildRawSchema.d.ts +9 -0
- package/dist/schema/buildRawSchema.d.ts.map +1 -0
- package/dist/schema/buildRawSchema.js +97 -0
- package/dist/schema/buildRawSchema.js.map +1 -0
- package/dist/schema/idToUUID.d.ts.map +1 -0
- package/dist/schema/idToUUID.js.map +1 -0
- package/dist/{postgres/schema → schema}/traverseFields.d.ts +9 -9
- package/dist/schema/traverseFields.d.ts.map +1 -0
- package/dist/{postgres/schema → schema}/traverseFields.js +343 -174
- package/dist/schema/traverseFields.js.map +1 -0
- package/dist/schema/withDefault.d.ts +4 -0
- package/dist/schema/withDefault.d.ts.map +1 -0
- package/dist/{postgres/schema → schema}/withDefault.js +8 -5
- package/dist/schema/withDefault.js.map +1 -0
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +7 -0
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/types.d.ts +117 -2
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/utilities/executeSchemaHooks.js +1 -1
- package/dist/utilities/executeSchemaHooks.js.map +1 -1
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts +3 -2
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/utilities/validateExistingBlockIsIdentical.js +2 -2
- package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
- package/package.json +4 -4
- package/dist/postgres/createMigration.d.ts +0 -3
- package/dist/postgres/createMigration.d.ts.map +0 -1
- package/dist/postgres/createMigration.js +0 -91
- package/dist/postgres/createMigration.js.map +0 -1
- package/dist/postgres/getMigrationTemplate.d.ts +0 -4
- package/dist/postgres/getMigrationTemplate.d.ts.map +0 -1
- package/dist/postgres/getMigrationTemplate.js +0 -13
- package/dist/postgres/getMigrationTemplate.js.map +0 -1
- package/dist/postgres/schema/build.d.ts.map +0 -1
- package/dist/postgres/schema/build.js +0 -404
- package/dist/postgres/schema/build.js.map +0 -1
- package/dist/postgres/schema/createIndex.d.ts +0 -11
- package/dist/postgres/schema/createIndex.d.ts.map +0 -1
- package/dist/postgres/schema/createIndex.js +0 -20
- package/dist/postgres/schema/createIndex.js.map +0 -1
- package/dist/postgres/schema/idToUUID.d.ts.map +0 -1
- package/dist/postgres/schema/idToUUID.js.map +0 -1
- package/dist/postgres/schema/parentIDColumnMap.d.ts +0 -4
- package/dist/postgres/schema/parentIDColumnMap.d.ts.map +0 -1
- package/dist/postgres/schema/parentIDColumnMap.js +0 -9
- package/dist/postgres/schema/parentIDColumnMap.js.map +0 -1
- package/dist/postgres/schema/traverseFields.d.ts.map +0 -1
- package/dist/postgres/schema/traverseFields.js.map +0 -1
- package/dist/postgres/schema/withDefault.d.ts +0 -4
- package/dist/postgres/schema/withDefault.d.ts.map +0 -1
- package/dist/postgres/schema/withDefault.js.map +0 -1
- /package/dist/{postgres/schema → schema}/idToUUID.d.ts +0 -0
- /package/dist/{postgres/schema → schema}/idToUUID.js +0 -0
|
@@ -1,19 +1,14 @@
|
|
|
1
|
-
import { relations } from 'drizzle-orm';
|
|
2
|
-
import { boolean, foreignKey, index, integer, jsonb, numeric, PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, text, timestamp, varchar } from 'drizzle-orm/pg-core';
|
|
3
1
|
import { InvalidConfiguration } from 'payload';
|
|
4
2
|
import { fieldAffectsData, fieldIsVirtual, optionIsObject } from 'payload/shared';
|
|
5
3
|
import toSnakeCase from 'to-snake-case';
|
|
6
|
-
import { createTableName } from '
|
|
7
|
-
import { buildIndexName } from '
|
|
8
|
-
import { hasLocalesTable } from '
|
|
9
|
-
import { validateExistingBlockIsIdentical } from '
|
|
4
|
+
import { createTableName } from '../createTableName.js';
|
|
5
|
+
import { buildIndexName } from '../utilities/buildIndexName.js';
|
|
6
|
+
import { hasLocalesTable } from '../utilities/hasLocalesTable.js';
|
|
7
|
+
import { validateExistingBlockIsIdentical } from '../utilities/validateExistingBlockIsIdentical.js';
|
|
10
8
|
import { buildTable } from './build.js';
|
|
11
|
-
import { createIndex } from './createIndex.js';
|
|
12
|
-
import { geometryColumn } from './geometryColumn.js';
|
|
13
9
|
import { idToUUID } from './idToUUID.js';
|
|
14
|
-
import { parentIDColumnMap } from './parentIDColumnMap.js';
|
|
15
10
|
import { withDefault } from './withDefault.js';
|
|
16
|
-
export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableRelsTableUnique, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationships, relationsToBuild, rootRelationsToBuild, rootTableIDColType, rootTableName, uniqueRelationships, versions, withinLocalizedArrayOrBlock })=>{
|
|
11
|
+
export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull, disableRelsTableUnique, disableUnique = false, fieldPrefix, fields, forceLocalized, indexes, localesColumns, localesIndexes, newTableName, parentTableName, relationships, relationsToBuild, rootRelationsToBuild, rootTableIDColType, rootTableName, setColumnID, uniqueRelationships, versions, withinLocalizedArrayOrBlock })=>{
|
|
17
12
|
const throwValidationError = true;
|
|
18
13
|
let hasLocalizedField = false;
|
|
19
14
|
let hasLocalizedRelationshipField = false;
|
|
@@ -22,14 +17,14 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
22
17
|
let hasManyNumberField = false;
|
|
23
18
|
let hasLocalizedManyNumberField = false;
|
|
24
19
|
let parentIDColType = 'integer';
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
parentIDColType =
|
|
20
|
+
const idColumn = columns.id;
|
|
21
|
+
if (idColumn && [
|
|
22
|
+
'numeric',
|
|
23
|
+
'text',
|
|
24
|
+
'uuid',
|
|
25
|
+
'varchar'
|
|
26
|
+
].includes(idColumn.type)) {
|
|
27
|
+
parentIDColType = idColumn.type;
|
|
33
28
|
}
|
|
34
29
|
fields.forEach((field)=>{
|
|
35
30
|
if ('name' in field && field.name === 'id') {
|
|
@@ -69,14 +64,14 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
69
64
|
name: `${newTableName}_${columnName}`,
|
|
70
65
|
adapter
|
|
71
66
|
});
|
|
72
|
-
targetIndexes[indexName] =
|
|
73
|
-
name:
|
|
67
|
+
targetIndexes[indexName] = {
|
|
68
|
+
name: indexName,
|
|
69
|
+
on: field.localized ? [
|
|
74
70
|
fieldName,
|
|
75
71
|
'_locale'
|
|
76
72
|
] : fieldName,
|
|
77
|
-
indexName,
|
|
78
73
|
unique
|
|
79
|
-
}
|
|
74
|
+
};
|
|
80
75
|
}
|
|
81
76
|
switch(field.type){
|
|
82
77
|
case 'array':
|
|
@@ -91,31 +86,62 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
91
86
|
versionsCustomName: versions
|
|
92
87
|
});
|
|
93
88
|
const baseColumns = {
|
|
94
|
-
_order:
|
|
95
|
-
|
|
89
|
+
_order: {
|
|
90
|
+
name: '_order',
|
|
91
|
+
type: 'integer',
|
|
92
|
+
notNull: true
|
|
93
|
+
},
|
|
94
|
+
_parentID: {
|
|
95
|
+
name: '_parent_id',
|
|
96
|
+
type: parentIDColType,
|
|
97
|
+
notNull: true
|
|
98
|
+
}
|
|
96
99
|
};
|
|
97
|
-
const
|
|
98
|
-
_orderIdx:
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
const baseIndexes = {
|
|
101
|
+
_orderIdx: {
|
|
102
|
+
name: `${arrayTableName}_order_idx`,
|
|
103
|
+
on: [
|
|
104
|
+
'_order'
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
_parentIDIdx: {
|
|
108
|
+
name: `${arrayTableName}_parent_id_idx`,
|
|
109
|
+
on: '_parentID'
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
const baseForeignKeys = {
|
|
113
|
+
_parentIDFk: {
|
|
114
|
+
name: `${arrayTableName}_parent_id_fk`,
|
|
115
|
+
columns: [
|
|
116
|
+
'_parentID'
|
|
117
|
+
],
|
|
118
|
+
foreignColumns: [
|
|
119
|
+
{
|
|
120
|
+
name: 'id',
|
|
121
|
+
table: parentTableName
|
|
122
|
+
}
|
|
123
|
+
],
|
|
124
|
+
onDelete: 'cascade'
|
|
125
|
+
}
|
|
109
126
|
};
|
|
110
127
|
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
111
128
|
if (isLocalized) {
|
|
112
|
-
baseColumns._locale =
|
|
113
|
-
|
|
129
|
+
baseColumns._locale = {
|
|
130
|
+
name: '_locale',
|
|
131
|
+
type: 'enum',
|
|
132
|
+
locale: true,
|
|
133
|
+
notNull: true
|
|
134
|
+
};
|
|
135
|
+
baseIndexes._localeIdx = {
|
|
136
|
+
name: `${arrayTableName}_locale_idx`,
|
|
137
|
+
on: '_locale'
|
|
138
|
+
};
|
|
114
139
|
}
|
|
115
140
|
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
116
141
|
adapter,
|
|
117
142
|
baseColumns,
|
|
118
|
-
|
|
143
|
+
baseForeignKeys,
|
|
144
|
+
baseIndexes,
|
|
119
145
|
disableNotNull: disableNotNullFromHere,
|
|
120
146
|
disableRelsTableUnique: true,
|
|
121
147
|
disableUnique,
|
|
@@ -125,6 +151,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
125
151
|
rootTableIDColType,
|
|
126
152
|
rootTableName,
|
|
127
153
|
rootUniqueRelationships: uniqueRelationships,
|
|
154
|
+
setColumnID,
|
|
128
155
|
tableName: arrayTableName,
|
|
129
156
|
versions,
|
|
130
157
|
withinLocalizedArrayOrBlock: isLocalized
|
|
@@ -154,44 +181,56 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
154
181
|
localized: false,
|
|
155
182
|
target: arrayTableName
|
|
156
183
|
});
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
184
|
+
const arrayRelations = {
|
|
185
|
+
_parentID: {
|
|
186
|
+
type: 'one',
|
|
187
|
+
fields: [
|
|
188
|
+
{
|
|
189
|
+
name: '_parentID',
|
|
190
|
+
table: arrayTableName
|
|
191
|
+
}
|
|
192
|
+
],
|
|
193
|
+
references: [
|
|
194
|
+
'id'
|
|
195
|
+
],
|
|
196
|
+
relationName: fieldName,
|
|
197
|
+
to: parentTableName
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
if (hasLocalesTable(field.fields)) {
|
|
201
|
+
arrayRelations._locales = {
|
|
202
|
+
type: 'many',
|
|
203
|
+
relationName: '_locales',
|
|
204
|
+
to: `${arrayTableName}${adapter.localesSuffix}`
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
208
|
+
if (type === 'one') {
|
|
209
|
+
const arrayWithLocalized = localized ? `${arrayTableName}${adapter.localesSuffix}` : arrayTableName;
|
|
210
|
+
arrayRelations[key] = {
|
|
211
|
+
type: 'one',
|
|
160
212
|
fields: [
|
|
161
|
-
|
|
213
|
+
{
|
|
214
|
+
name: key,
|
|
215
|
+
table: arrayWithLocalized
|
|
216
|
+
}
|
|
162
217
|
],
|
|
163
218
|
references: [
|
|
164
|
-
|
|
219
|
+
'id'
|
|
165
220
|
],
|
|
166
|
-
relationName:
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
221
|
+
relationName: key,
|
|
222
|
+
to: target
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
if (type === 'many') {
|
|
226
|
+
arrayRelations[key] = {
|
|
227
|
+
type: 'many',
|
|
228
|
+
relationName: key,
|
|
229
|
+
to: target
|
|
230
|
+
};
|
|
173
231
|
}
|
|
174
|
-
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
175
|
-
if (type === 'one') {
|
|
176
|
-
const arrayWithLocalized = localized ? `${arrayTableName}${adapter.localesSuffix}` : arrayTableName;
|
|
177
|
-
result[key] = one(adapter.tables[target], {
|
|
178
|
-
fields: [
|
|
179
|
-
adapter.tables[arrayWithLocalized][key]
|
|
180
|
-
],
|
|
181
|
-
references: [
|
|
182
|
-
adapter.tables[target].id
|
|
183
|
-
],
|
|
184
|
-
relationName: key
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
if (type === 'many') {
|
|
188
|
-
result[key] = many(adapter.tables[target], {
|
|
189
|
-
relationName: key
|
|
190
|
-
});
|
|
191
|
-
}
|
|
192
|
-
});
|
|
193
|
-
return result;
|
|
194
232
|
});
|
|
233
|
+
adapter.rawRelations[arrayTableName] = arrayRelations;
|
|
195
234
|
break;
|
|
196
235
|
}
|
|
197
236
|
case 'blocks':
|
|
@@ -206,35 +245,73 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
206
245
|
throwValidationError,
|
|
207
246
|
versionsCustomName: versions
|
|
208
247
|
});
|
|
209
|
-
if (!adapter.
|
|
248
|
+
if (!adapter.rawTables[blockTableName]) {
|
|
210
249
|
const baseColumns = {
|
|
211
|
-
_order:
|
|
212
|
-
|
|
213
|
-
|
|
250
|
+
_order: {
|
|
251
|
+
name: '_order',
|
|
252
|
+
type: 'integer',
|
|
253
|
+
notNull: true
|
|
254
|
+
},
|
|
255
|
+
_parentID: {
|
|
256
|
+
name: '_parent_id',
|
|
257
|
+
type: rootTableIDColType,
|
|
258
|
+
notNull: true
|
|
259
|
+
},
|
|
260
|
+
_path: {
|
|
261
|
+
name: '_path',
|
|
262
|
+
type: 'text',
|
|
263
|
+
notNull: true
|
|
264
|
+
}
|
|
214
265
|
};
|
|
215
|
-
const
|
|
216
|
-
_orderIdx:
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
266
|
+
const baseIndexes = {
|
|
267
|
+
_orderIdx: {
|
|
268
|
+
name: `${blockTableName}_order_idx`,
|
|
269
|
+
on: '_order'
|
|
270
|
+
},
|
|
271
|
+
_parentIDIdx: {
|
|
272
|
+
name: `${blockTableName}_parent_id_idx`,
|
|
273
|
+
on: [
|
|
274
|
+
'_parentID'
|
|
275
|
+
]
|
|
276
|
+
},
|
|
277
|
+
_pathIdx: {
|
|
278
|
+
name: `${blockTableName}_path_idx`,
|
|
279
|
+
on: '_path'
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
const baseForeignKeys = {
|
|
283
|
+
_parentIdFk: {
|
|
284
|
+
name: `${blockTableName}_parent_id_fk`,
|
|
285
|
+
columns: [
|
|
286
|
+
'_parentID'
|
|
287
|
+
],
|
|
288
|
+
foreignColumns: [
|
|
289
|
+
{
|
|
290
|
+
name: 'id',
|
|
291
|
+
table: rootTableName
|
|
292
|
+
}
|
|
293
|
+
],
|
|
294
|
+
onDelete: 'cascade'
|
|
295
|
+
}
|
|
228
296
|
};
|
|
229
297
|
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
230
298
|
if (isLocalized) {
|
|
231
|
-
baseColumns._locale =
|
|
232
|
-
|
|
299
|
+
baseColumns._locale = {
|
|
300
|
+
name: '_locale',
|
|
301
|
+
type: 'enum',
|
|
302
|
+
locale: true,
|
|
303
|
+
notNull: true
|
|
304
|
+
};
|
|
305
|
+
baseIndexes._localeIdx = {
|
|
306
|
+
name: `${blockTableName}_locale_idx`,
|
|
307
|
+
on: '_locale'
|
|
308
|
+
};
|
|
233
309
|
}
|
|
234
310
|
const { hasLocalizedManyNumberField: subHasLocalizedManyNumberField, hasLocalizedManyTextField: subHasLocalizedManyTextField, hasLocalizedRelationshipField: subHasLocalizedRelationshipField, hasManyNumberField: subHasManyNumberField, hasManyTextField: subHasManyTextField, relationsToBuild: subRelationsToBuild } = buildTable({
|
|
235
311
|
adapter,
|
|
236
312
|
baseColumns,
|
|
237
|
-
|
|
313
|
+
baseForeignKeys,
|
|
314
|
+
baseIndexes,
|
|
238
315
|
disableNotNull: disableNotNullFromHere,
|
|
239
316
|
disableRelsTableUnique: true,
|
|
240
317
|
disableUnique,
|
|
@@ -244,6 +321,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
244
321
|
rootTableIDColType,
|
|
245
322
|
rootTableName,
|
|
246
323
|
rootUniqueRelationships: uniqueRelationships,
|
|
324
|
+
setColumnID,
|
|
247
325
|
tableName: blockTableName,
|
|
248
326
|
versions,
|
|
249
327
|
withinLocalizedArrayOrBlock: isLocalized
|
|
@@ -267,51 +345,63 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
267
345
|
hasManyNumberField = subHasManyNumberField;
|
|
268
346
|
}
|
|
269
347
|
}
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
348
|
+
const blockRelations = {
|
|
349
|
+
_parentID: {
|
|
350
|
+
type: 'one',
|
|
351
|
+
fields: [
|
|
352
|
+
{
|
|
353
|
+
name: '_parentID',
|
|
354
|
+
table: blockTableName
|
|
355
|
+
}
|
|
356
|
+
],
|
|
357
|
+
references: [
|
|
358
|
+
'id'
|
|
359
|
+
],
|
|
360
|
+
relationName: `_blocks_${block.slug}`,
|
|
361
|
+
to: rootTableName
|
|
362
|
+
}
|
|
363
|
+
};
|
|
364
|
+
if (hasLocalesTable(block.fields)) {
|
|
365
|
+
blockRelations._locales = {
|
|
366
|
+
type: 'many',
|
|
367
|
+
relationName: '_locales',
|
|
368
|
+
to: `${blockTableName}${adapter.localesSuffix}`
|
|
369
|
+
};
|
|
370
|
+
}
|
|
371
|
+
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
372
|
+
if (type === 'one') {
|
|
373
|
+
const blockWithLocalized = localized ? `${blockTableName}${adapter.localesSuffix}` : blockTableName;
|
|
374
|
+
blockRelations[key] = {
|
|
375
|
+
type: 'one',
|
|
273
376
|
fields: [
|
|
274
|
-
|
|
377
|
+
{
|
|
378
|
+
name: key,
|
|
379
|
+
table: blockWithLocalized
|
|
380
|
+
}
|
|
275
381
|
],
|
|
276
382
|
references: [
|
|
277
|
-
|
|
383
|
+
'id'
|
|
278
384
|
],
|
|
279
|
-
relationName:
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
385
|
+
relationName: key,
|
|
386
|
+
to: target
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
if (type === 'many') {
|
|
390
|
+
blockRelations[key] = {
|
|
391
|
+
type: 'many',
|
|
392
|
+
relationName: key,
|
|
393
|
+
to: target
|
|
394
|
+
};
|
|
286
395
|
}
|
|
287
|
-
subRelationsToBuild.forEach(({ type, localized, target }, key)=>{
|
|
288
|
-
if (type === 'one') {
|
|
289
|
-
const blockWithLocalized = localized ? `${blockTableName}${adapter.localesSuffix}` : blockTableName;
|
|
290
|
-
result[key] = one(adapter.tables[target], {
|
|
291
|
-
fields: [
|
|
292
|
-
adapter.tables[blockWithLocalized][key]
|
|
293
|
-
],
|
|
294
|
-
references: [
|
|
295
|
-
adapter.tables[target].id
|
|
296
|
-
],
|
|
297
|
-
relationName: key
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
if (type === 'many') {
|
|
301
|
-
result[key] = many(adapter.tables[target], {
|
|
302
|
-
relationName: key
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
});
|
|
306
|
-
return result;
|
|
307
396
|
});
|
|
397
|
+
adapter.rawRelations[blockTableName] = blockRelations;
|
|
308
398
|
} else if (process.env.NODE_ENV !== 'production' && !versions) {
|
|
309
399
|
validateExistingBlockIsIdentical({
|
|
310
400
|
block,
|
|
311
401
|
localized: field.localized,
|
|
312
402
|
rootTableName,
|
|
313
|
-
table: adapter.
|
|
314
|
-
tableLocales: adapter.
|
|
403
|
+
table: adapter.rawTables[blockTableName],
|
|
404
|
+
tableLocales: adapter.rawTables[`${blockTableName}${adapter.localesSuffix}`]
|
|
315
405
|
});
|
|
316
406
|
}
|
|
317
407
|
// blocks relationships are defined from the collection or globals table down to the block, bypassing any subBlocks
|
|
@@ -326,23 +416,31 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
326
416
|
}
|
|
327
417
|
case 'checkbox':
|
|
328
418
|
{
|
|
329
|
-
targetTable[fieldName] = withDefault(
|
|
419
|
+
targetTable[fieldName] = withDefault({
|
|
420
|
+
name: columnName,
|
|
421
|
+
type: 'boolean'
|
|
422
|
+
}, field);
|
|
330
423
|
break;
|
|
331
424
|
}
|
|
332
425
|
case 'code':
|
|
333
426
|
case 'email':
|
|
334
427
|
case 'textarea':
|
|
335
428
|
{
|
|
336
|
-
targetTable[fieldName] = withDefault(
|
|
429
|
+
targetTable[fieldName] = withDefault({
|
|
430
|
+
name: columnName,
|
|
431
|
+
type: 'varchar'
|
|
432
|
+
}, field);
|
|
337
433
|
break;
|
|
338
434
|
}
|
|
339
435
|
case 'date':
|
|
340
436
|
{
|
|
341
|
-
targetTable[fieldName] = withDefault(
|
|
437
|
+
targetTable[fieldName] = withDefault({
|
|
438
|
+
name: columnName,
|
|
439
|
+
type: 'timestamp',
|
|
342
440
|
mode: 'string',
|
|
343
441
|
precision: 3,
|
|
344
442
|
withTimezone: true
|
|
345
|
-
}
|
|
443
|
+
}, field);
|
|
346
444
|
break;
|
|
347
445
|
}
|
|
348
446
|
case 'group':
|
|
@@ -368,6 +466,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
368
466
|
rootRelationsToBuild,
|
|
369
467
|
rootTableIDColType,
|
|
370
468
|
rootTableName,
|
|
469
|
+
setColumnID,
|
|
371
470
|
uniqueRelationships,
|
|
372
471
|
versions,
|
|
373
472
|
withinLocalizedArrayOrBlock: withinLocalizedArrayOrBlock || field.localized
|
|
@@ -395,7 +494,10 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
395
494
|
case 'json':
|
|
396
495
|
case 'richText':
|
|
397
496
|
{
|
|
398
|
-
targetTable[fieldName] = withDefault(
|
|
497
|
+
targetTable[fieldName] = withDefault({
|
|
498
|
+
name: columnName,
|
|
499
|
+
type: 'jsonb'
|
|
500
|
+
}, field);
|
|
399
501
|
break;
|
|
400
502
|
}
|
|
401
503
|
case 'number':
|
|
@@ -414,16 +516,19 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
414
516
|
throw new InvalidConfiguration('Unique is not supported in Postgres for hasMany number fields.');
|
|
415
517
|
}
|
|
416
518
|
} else {
|
|
417
|
-
targetTable[fieldName] = withDefault(
|
|
519
|
+
targetTable[fieldName] = withDefault({
|
|
520
|
+
name: columnName,
|
|
521
|
+
type: 'numeric'
|
|
522
|
+
}, field);
|
|
418
523
|
}
|
|
419
524
|
break;
|
|
420
525
|
}
|
|
421
526
|
case 'point':
|
|
422
527
|
{
|
|
423
|
-
targetTable[fieldName] = withDefault(
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
}
|
|
528
|
+
targetTable[fieldName] = withDefault({
|
|
529
|
+
name: columnName,
|
|
530
|
+
type: 'geometry'
|
|
531
|
+
}, field);
|
|
427
532
|
break;
|
|
428
533
|
}
|
|
429
534
|
case 'radio':
|
|
@@ -437,12 +542,12 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
437
542
|
target: 'enumName',
|
|
438
543
|
throwValidationError
|
|
439
544
|
});
|
|
440
|
-
|
|
545
|
+
const options = field.options.map((option)=>{
|
|
441
546
|
if (optionIsObject(option)) {
|
|
442
547
|
return option.value;
|
|
443
548
|
}
|
|
444
549
|
return option;
|
|
445
|
-
})
|
|
550
|
+
});
|
|
446
551
|
if (field.type === 'select' && field.hasMany) {
|
|
447
552
|
const selectTableName = createTableName({
|
|
448
553
|
adapter,
|
|
@@ -453,39 +558,84 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
453
558
|
versionsCustomName: versions
|
|
454
559
|
});
|
|
455
560
|
const baseColumns = {
|
|
456
|
-
order:
|
|
457
|
-
|
|
458
|
-
|
|
561
|
+
order: {
|
|
562
|
+
name: 'order',
|
|
563
|
+
type: 'integer',
|
|
564
|
+
notNull: true
|
|
565
|
+
},
|
|
566
|
+
parent: {
|
|
567
|
+
name: 'parent_id',
|
|
568
|
+
type: parentIDColType,
|
|
569
|
+
notNull: true
|
|
570
|
+
},
|
|
571
|
+
value: {
|
|
572
|
+
name: 'value',
|
|
573
|
+
type: 'enum',
|
|
574
|
+
enumName: createTableName({
|
|
575
|
+
adapter,
|
|
576
|
+
config: field,
|
|
577
|
+
parentTableName: newTableName,
|
|
578
|
+
prefix: `enum_${newTableName}_`,
|
|
579
|
+
target: 'enumName',
|
|
580
|
+
throwValidationError
|
|
581
|
+
}),
|
|
582
|
+
options
|
|
583
|
+
}
|
|
459
584
|
};
|
|
460
|
-
const
|
|
461
|
-
orderIdx:
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
585
|
+
const baseIndexes = {
|
|
586
|
+
orderIdx: {
|
|
587
|
+
name: `${selectTableName}_order_idx`,
|
|
588
|
+
on: 'order'
|
|
589
|
+
},
|
|
590
|
+
parentIdx: {
|
|
591
|
+
name: `${selectTableName}_parent_idx`,
|
|
592
|
+
on: 'parent'
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
const baseForeignKeys = {
|
|
596
|
+
parentFk: {
|
|
597
|
+
name: `${selectTableName}_parent_fk`,
|
|
598
|
+
columns: [
|
|
599
|
+
'parent'
|
|
600
|
+
],
|
|
601
|
+
foreignColumns: [
|
|
602
|
+
{
|
|
603
|
+
name: 'id',
|
|
604
|
+
table: parentTableName
|
|
605
|
+
}
|
|
606
|
+
],
|
|
607
|
+
onDelete: 'cascade'
|
|
608
|
+
}
|
|
472
609
|
};
|
|
473
610
|
const isLocalized = Boolean(field.localized && adapter.payload.config.localization) || withinLocalizedArrayOrBlock || forceLocalized;
|
|
474
611
|
if (isLocalized) {
|
|
475
|
-
baseColumns.locale =
|
|
476
|
-
|
|
612
|
+
baseColumns.locale = {
|
|
613
|
+
name: 'locale',
|
|
614
|
+
type: 'enum',
|
|
615
|
+
locale: true,
|
|
616
|
+
notNull: true
|
|
617
|
+
};
|
|
618
|
+
baseIndexes.localeIdx = {
|
|
619
|
+
name: `${selectTableName}_locale_idx`,
|
|
620
|
+
on: 'locale'
|
|
621
|
+
};
|
|
477
622
|
}
|
|
478
623
|
if (field.index) {
|
|
479
|
-
|
|
624
|
+
baseIndexes.value = {
|
|
625
|
+
name: `${selectTableName}_value_idx`,
|
|
626
|
+
on: 'value'
|
|
627
|
+
};
|
|
480
628
|
}
|
|
481
629
|
buildTable({
|
|
482
630
|
adapter,
|
|
483
631
|
baseColumns,
|
|
484
|
-
|
|
632
|
+
baseForeignKeys,
|
|
633
|
+
baseIndexes,
|
|
485
634
|
disableNotNull,
|
|
486
635
|
disableUnique,
|
|
487
636
|
fields: [],
|
|
488
637
|
rootTableName,
|
|
638
|
+
setColumnID,
|
|
489
639
|
tableName: selectTableName,
|
|
490
640
|
versions
|
|
491
641
|
});
|
|
@@ -495,19 +645,29 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
495
645
|
localized: false,
|
|
496
646
|
target: selectTableName
|
|
497
647
|
});
|
|
498
|
-
adapter.
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
648
|
+
adapter.rawRelations[selectTableName] = {
|
|
649
|
+
parent: {
|
|
650
|
+
type: 'one',
|
|
651
|
+
fields: [
|
|
652
|
+
{
|
|
653
|
+
name: 'parent',
|
|
654
|
+
table: selectTableName
|
|
655
|
+
}
|
|
656
|
+
],
|
|
657
|
+
references: [
|
|
658
|
+
'id'
|
|
659
|
+
],
|
|
660
|
+
relationName: fieldName,
|
|
661
|
+
to: parentTableName
|
|
662
|
+
}
|
|
663
|
+
};
|
|
509
664
|
} else {
|
|
510
|
-
targetTable[fieldName] = withDefault(
|
|
665
|
+
targetTable[fieldName] = withDefault({
|
|
666
|
+
name: columnName,
|
|
667
|
+
type: 'enum',
|
|
668
|
+
enumName,
|
|
669
|
+
options
|
|
670
|
+
}, field);
|
|
511
671
|
}
|
|
512
672
|
break;
|
|
513
673
|
}
|
|
@@ -539,9 +699,15 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
539
699
|
colType = 'varchar';
|
|
540
700
|
}
|
|
541
701
|
// make the foreign key column for relationship using the correct id column type
|
|
542
|
-
targetTable[fieldName] =
|
|
543
|
-
|
|
544
|
-
|
|
702
|
+
targetTable[fieldName] = {
|
|
703
|
+
name: `${columnName}_id`,
|
|
704
|
+
type: colType,
|
|
705
|
+
reference: {
|
|
706
|
+
name: 'id',
|
|
707
|
+
onDelete: 'set null',
|
|
708
|
+
table: tableName
|
|
709
|
+
}
|
|
710
|
+
};
|
|
545
711
|
// add relationship to table
|
|
546
712
|
relationsToBuild.set(fieldName, {
|
|
547
713
|
type: 'one',
|
|
@@ -550,7 +716,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
550
716
|
});
|
|
551
717
|
// add notNull when not required
|
|
552
718
|
if (!disableNotNull && field.required && !field.admin?.condition) {
|
|
553
|
-
targetTable[fieldName].notNull
|
|
719
|
+
targetTable[fieldName].notNull = true;
|
|
554
720
|
}
|
|
555
721
|
break;
|
|
556
722
|
}
|
|
@@ -574,7 +740,10 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
574
740
|
throw new InvalidConfiguration('Unique is not supported in Postgres for hasMany text fields.');
|
|
575
741
|
}
|
|
576
742
|
} else {
|
|
577
|
-
targetTable[fieldName] = withDefault(
|
|
743
|
+
targetTable[fieldName] = withDefault({
|
|
744
|
+
name: columnName,
|
|
745
|
+
type: 'varchar'
|
|
746
|
+
}, field);
|
|
578
747
|
}
|
|
579
748
|
break;
|
|
580
749
|
}
|
|
@@ -583,7 +752,7 @@ export const traverseFields = ({ adapter, columnPrefix, columns, disableNotNull,
|
|
|
583
752
|
}
|
|
584
753
|
const condition = field.admin && field.admin.condition;
|
|
585
754
|
if (!disableNotNull && targetTable[fieldName] && 'required' in field && field.required && !condition) {
|
|
586
|
-
targetTable[fieldName].notNull
|
|
755
|
+
targetTable[fieldName].notNull = true;
|
|
587
756
|
}
|
|
588
757
|
});
|
|
589
758
|
return {
|