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