@payloadcms/db-postgres 3.0.0-beta.5 → 3.0.0-beta.50
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +1 -1
- package/dist/connect.d.ts +1 -1
- package/dist/connect.d.ts.map +1 -1
- package/dist/connect.js +12 -7
- package/dist/connect.js.map +1 -1
- package/dist/count.d.ts +3 -0
- package/dist/count.d.ts.map +1 -0
- package/dist/count.js +41 -0
- package/dist/count.js.map +1 -0
- package/dist/create.d.ts +1 -1
- package/dist/create.d.ts.map +1 -1
- package/dist/create.js +3 -5
- package/dist/create.js.map +1 -1
- package/dist/createGlobal.d.ts +1 -2
- package/dist/createGlobal.d.ts.map +1 -1
- package/dist/createGlobal.js +3 -5
- package/dist/createGlobal.js.map +1 -1
- package/dist/createGlobalVersion.d.ts +3 -4
- package/dist/createGlobalVersion.d.ts.map +1 -1
- package/dist/createGlobalVersion.js +3 -7
- package/dist/createGlobalVersion.js.map +1 -1
- package/dist/createMigration.d.ts +1 -1
- package/dist/createMigration.d.ts.map +1 -1
- package/dist/createMigration.js +56 -35
- package/dist/createMigration.js.map +1 -1
- package/dist/createVersion.d.ts +1 -2
- package/dist/createVersion.d.ts.map +1 -1
- package/dist/createVersion.js +11 -19
- package/dist/createVersion.js.map +1 -1
- package/dist/deleteMany.d.ts +1 -1
- package/dist/deleteMany.d.ts.map +1 -1
- package/dist/deleteMany.js +2 -5
- package/dist/deleteMany.js.map +1 -1
- package/dist/deleteOne.d.ts +1 -1
- package/dist/deleteOne.d.ts.map +1 -1
- package/dist/deleteOne.js +4 -7
- package/dist/deleteOne.js.map +1 -1
- package/dist/deleteVersions.d.ts +1 -1
- package/dist/deleteVersions.d.ts.map +1 -1
- package/dist/deleteVersions.js +3 -7
- package/dist/deleteVersions.js.map +1 -1
- package/dist/destroy.d.ts +1 -1
- package/dist/destroy.d.ts.map +1 -1
- package/dist/destroy.js +11 -7
- package/dist/destroy.js.map +1 -1
- package/dist/exports/migration-utils.d.ts +2 -0
- package/dist/exports/migration-utils.d.ts.map +1 -0
- package/dist/exports/migration-utils.js +3 -0
- package/dist/exports/migration-utils.js.map +1 -0
- package/dist/find/buildFindManyArgs.d.ts +6 -2
- package/dist/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildFindManyArgs.js +4 -3
- package/dist/find/buildFindManyArgs.js.map +1 -1
- package/dist/find/findMany.d.ts +1 -2
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/findMany.js +5 -16
- package/dist/find/findMany.js.map +1 -1
- package/dist/find/traverseFields.d.ts +4 -3
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +27 -25
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/find.d.ts +1 -1
- package/dist/find.d.ts.map +1 -1
- package/dist/find.js +2 -5
- package/dist/find.js.map +1 -1
- package/dist/findGlobal.d.ts +1 -1
- package/dist/findGlobal.d.ts.map +1 -1
- package/dist/findGlobal.js +2 -5
- package/dist/findGlobal.js.map +1 -1
- package/dist/findGlobalVersions.d.ts +1 -1
- package/dist/findGlobalVersions.d.ts.map +1 -1
- package/dist/findGlobalVersions.js +3 -7
- package/dist/findGlobalVersions.js.map +1 -1
- package/dist/findOne.d.ts +1 -2
- package/dist/findOne.d.ts.map +1 -1
- package/dist/findOne.js +2 -5
- package/dist/findOne.js.map +1 -1
- package/dist/findVersions.d.ts +1 -1
- package/dist/findVersions.d.ts.map +1 -1
- package/dist/findVersions.js +3 -7
- package/dist/findVersions.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +18 -9
- package/dist/index.js.map +1 -1
- package/dist/init.d.ts +1 -1
- package/dist/init.d.ts.map +1 -1
- package/dist/init.js +20 -23
- package/dist/init.js.map +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +1 -1
- package/dist/migrate.js.map +1 -1
- package/dist/migrateDown.js +1 -1
- package/dist/migrateDown.js.map +1 -1
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +1 -1
- package/dist/migrateFresh.js.map +1 -1
- package/dist/migrateRefresh.js +1 -1
- package/dist/migrateRefresh.js.map +1 -1
- package/dist/migrateReset.js +1 -1
- package/dist/migrateReset.js.map +1 -1
- package/dist/migrateStatus.js +1 -1
- package/dist/migrateStatus.js.map +1 -1
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts +4 -0
- package/dist/predefinedMigrations/relationships-v2-v3.d.ts.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.js.map +1 -0
- package/dist/predefinedMigrations/relationships-v2-v3.mjs +11 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts +19 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js +170 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/index.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts +11 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js +191 -0
- package/dist/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts +3 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js +56 -0
- package/dist/predefinedMigrations/v2-v3/groupUpSQLStatements.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/index.d.ts +23 -0
- package/dist/predefinedMigrations/v2-v3/index.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/index.js +211 -0
- package/dist/predefinedMigrations/v2-v3/index.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts +19 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.js +57 -0
- package/dist/predefinedMigrations/v2-v3/migrateRelationships.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts +22 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.js +85 -0
- package/dist/predefinedMigrations/v2-v3/traverseFields.js.map +1 -0
- package/dist/predefinedMigrations/v2-v3/types.d.ts +9 -0
- package/dist/predefinedMigrations/v2-v3/types.d.ts.map +1 -0
- package/dist/predefinedMigrations/v2-v3/types.js +6 -0
- package/dist/predefinedMigrations/v2-v3/types.js.map +1 -0
- package/dist/queries/buildAndOrConditions.d.ts +4 -5
- package/dist/queries/buildAndOrConditions.d.ts.map +1 -1
- package/dist/queries/buildAndOrConditions.js +1 -2
- package/dist/queries/buildAndOrConditions.js.map +1 -1
- package/dist/queries/buildQuery.d.ts +2 -3
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/buildQuery.js +1 -5
- package/dist/queries/buildQuery.js.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +4 -5
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +250 -146
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/parseParams.d.ts +4 -5
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +9 -8
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts +1 -1
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/sanitizeQueryValue.js +2 -3
- package/dist/queries/sanitizeQueryValue.js.map +1 -1
- package/dist/queries/selectDistinct.d.ts +3 -4
- package/dist/queries/selectDistinct.d.ts.map +1 -1
- package/dist/queries/selectDistinct.js +3 -14
- package/dist/queries/selectDistinct.js.map +1 -1
- package/dist/queryDrafts.d.ts +1 -1
- package/dist/queryDrafts.d.ts.map +1 -1
- package/dist/queryDrafts.js +3 -8
- package/dist/queryDrafts.js.map +1 -1
- package/dist/schema/build.d.ts +12 -7
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/build.js +208 -124
- package/dist/schema/build.js.map +1 -1
- package/dist/schema/{getTableName.d.ts → createTableName.d.ts} +6 -8
- package/dist/schema/createTableName.d.ts.map +1 -0
- package/dist/schema/createTableName.js +31 -0
- package/dist/schema/createTableName.js.map +1 -0
- package/dist/schema/idToUUID.d.ts +1 -1
- package/dist/schema/idToUUID.d.ts.map +1 -1
- package/dist/schema/idToUUID.js.map +1 -1
- package/dist/schema/setColumnID.d.ts +1 -1
- package/dist/schema/setColumnID.d.ts.map +1 -1
- package/dist/schema/setColumnID.js +2 -2
- package/dist/schema/setColumnID.js.map +1 -1
- package/dist/schema/traverseFields.d.ts +5 -7
- package/dist/schema/traverseFields.d.ts.map +1 -1
- package/dist/schema/traverseFields.js +147 -63
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.d.ts +3 -2
- package/dist/schema/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/schema/validateExistingBlockIsIdentical.js +15 -9
- package/dist/schema/validateExistingBlockIsIdentical.js.map +1 -1
- package/dist/transactions/beginTransaction.d.ts +1 -1
- package/dist/transactions/beginTransaction.d.ts.map +1 -1
- package/dist/transactions/beginTransaction.js +4 -0
- package/dist/transactions/beginTransaction.js.map +1 -1
- package/dist/transactions/commitTransaction.d.ts +1 -1
- package/dist/transactions/commitTransaction.d.ts.map +1 -1
- package/dist/transactions/commitTransaction.js.map +1 -1
- package/dist/transactions/rollbackTransaction.d.ts +1 -1
- package/dist/transactions/rollbackTransaction.d.ts.map +1 -1
- package/dist/transactions/rollbackTransaction.js.map +1 -1
- package/dist/transform/read/hasManyNumber.d.ts +1 -1
- package/dist/transform/read/hasManyNumber.d.ts.map +1 -1
- package/dist/transform/read/hasManyNumber.js.map +1 -1
- package/dist/transform/read/hasManyText.d.ts +1 -1
- package/dist/transform/read/hasManyText.d.ts.map +1 -1
- package/dist/transform/read/hasManyText.js.map +1 -1
- package/dist/transform/read/index.d.ts +5 -3
- package/dist/transform/read/index.d.ts.map +1 -1
- package/dist/transform/read/index.js +2 -1
- package/dist/transform/read/index.js.map +1 -1
- package/dist/transform/read/relationship.d.ts +1 -1
- package/dist/transform/read/relationship.d.ts.map +1 -1
- package/dist/transform/read/relationship.js +0 -4
- package/dist/transform/read/relationship.js.map +1 -1
- package/dist/transform/read/traverseFields.d.ts +8 -3
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.js +70 -56
- package/dist/transform/read/traverseFields.js.map +1 -1
- package/dist/transform/write/array.d.ts +1 -1
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/array.js.map +1 -1
- package/dist/transform/write/blocks.d.ts +1 -1
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/blocks.js +1 -1
- package/dist/transform/write/blocks.js.map +1 -1
- package/dist/transform/write/index.d.ts +1 -1
- package/dist/transform/write/index.d.ts.map +1 -1
- package/dist/transform/write/index.js.map +1 -1
- package/dist/transform/write/relationships.d.ts +1 -1
- package/dist/transform/write/relationships.d.ts.map +1 -1
- package/dist/transform/write/relationships.js +1 -1
- package/dist/transform/write/relationships.js.map +1 -1
- package/dist/transform/write/traverseFields.d.ts +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +23 -6
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/types.d.ts +7 -9
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/dist/update.d.ts +1 -1
- package/dist/update.d.ts.map +1 -1
- package/dist/update.js +3 -7
- package/dist/update.js.map +1 -1
- package/dist/updateGlobal.d.ts +1 -2
- package/dist/updateGlobal.d.ts.map +1 -1
- package/dist/updateGlobal.js +2 -5
- package/dist/updateGlobal.js.map +1 -1
- package/dist/updateGlobalVersion.d.ts +1 -2
- package/dist/updateGlobalVersion.d.ts.map +1 -1
- package/dist/updateGlobalVersion.js +3 -7
- package/dist/updateGlobalVersion.js.map +1 -1
- package/dist/updateVersion.d.ts +1 -2
- package/dist/updateVersion.d.ts.map +1 -1
- package/dist/updateVersion.js +3 -7
- package/dist/updateVersion.js.map +1 -1
- package/dist/upsertRow/index.d.ts +2 -2
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +10 -7
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js +3 -3
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/types.d.ts +7 -2
- package/dist/upsertRow/types.d.ts.map +1 -1
- package/dist/upsertRow/types.js.map +1 -1
- package/dist/utilities/hasLocalesTable.d.ts +1 -1
- package/dist/utilities/hasLocalesTable.d.ts.map +1 -1
- package/dist/utilities/hasLocalesTable.js +1 -1
- package/dist/utilities/hasLocalesTable.js.map +1 -1
- package/dist/utilities/pushDevSchema.d.ts.map +1 -1
- package/dist/utilities/pushDevSchema.js +1 -1
- package/dist/utilities/pushDevSchema.js.map +1 -1
- package/package.json +33 -28
- package/dist/schema/getTableName.d.ts.map +0 -1
- package/dist/schema/getTableName.js +0 -31
- package/dist/schema/getTableName.js.map +0 -1
- package/src/index.ts +0 -156
@@ -0,0 +1,31 @@
|
|
1
|
+
import { APIError } from 'payload';
|
2
|
+
import toSnakeCase from 'to-snake-case';
|
3
|
+
/**
|
4
|
+
* Used to name database enums and tables
|
5
|
+
* Returns the table or enum name for a given entity
|
6
|
+
*/ export const createTableName = ({ adapter, config: { name, slug }, config, parentTableName, prefix = '', target = 'dbName', throwValidationError = false, versions = false, versionsCustomName = false })=>{
|
7
|
+
let customNameDefinition = config[target];
|
8
|
+
let defaultTableName = `${prefix}${toSnakeCase(name ?? slug)}`;
|
9
|
+
if (versions) defaultTableName = `_${defaultTableName}${adapter.versionsSuffix}`;
|
10
|
+
let customTableNameResult;
|
11
|
+
if (!customNameDefinition && target === 'enumName') {
|
12
|
+
customNameDefinition = config['dbName'];
|
13
|
+
}
|
14
|
+
if (customNameDefinition) {
|
15
|
+
customTableNameResult = typeof customNameDefinition === 'function' ? customNameDefinition({
|
16
|
+
tableName: parentTableName
|
17
|
+
}) : customNameDefinition;
|
18
|
+
if (versionsCustomName) customTableNameResult = `_${customTableNameResult}${adapter.versionsSuffix}`;
|
19
|
+
}
|
20
|
+
const result = customTableNameResult || defaultTableName;
|
21
|
+
adapter.tableNameMap.set(defaultTableName, result);
|
22
|
+
if (!throwValidationError) {
|
23
|
+
return result;
|
24
|
+
}
|
25
|
+
if (result.length > 63) {
|
26
|
+
throw new APIError(`Exceeded max identifier length for table or enum name of 63 characters. Invalid name: ${result}`);
|
27
|
+
}
|
28
|
+
return result;
|
29
|
+
};
|
30
|
+
|
31
|
+
//# sourceMappingURL=createTableName.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"sources":["../../src/schema/createTableName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload'\n\nimport { APIError } from 'payload'\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,UAAS;AAClC,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 +1 @@
|
|
1
|
-
{"version":3,"file":"idToUUID.d.ts","sourceRoot":"","sources":["../../src/schema/idToUUID.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,
|
1
|
+
{"version":3,"file":"idToUUID.d.ts","sourceRoot":"","sources":["../../src/schema/idToUUID.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,SAAS,CAAA;AAEpC,eAAO,MAAM,QAAQ,WAAY,KAAK,EAAE,KAAG,KAAK,EAU5C,CAAA"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/schema/idToUUID.ts"],"sourcesContent":["import type { Field } from 'payload
|
1
|
+
{"version":3,"sources":["../../src/schema/idToUUID.ts"],"sourcesContent":["import type { Field } from 'payload'\n\nexport const idToUUID = (fields: Field[]): Field[] =>\n fields.map((field) => {\n if ('name' in field && field.name === 'id') {\n return {\n ...field,\n name: '_uuid',\n }\n }\n\n return field\n })\n"],"names":["idToUUID","fields","map","field","name"],"rangeMappings":";;;;;;;;","mappings":"AAEA,OAAO,MAAMA,WAAW,CAACC,SACvBA,OAAOC,GAAG,CAAC,CAACC;QACV,IAAI,UAAUA,SAASA,MAAMC,IAAI,KAAK,MAAM;YAC1C,OAAO;gBACL,GAAGD,KAAK;gBACRC,MAAM;YACR;QACF;QAEA,OAAOD;IACT,GAAE"}
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"setColumnID.d.ts","sourceRoot":"","sources":["../../src/schema/setColumnID.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAG1D,OAAO,EAAE,KAAK,KAAK,
|
1
|
+
{"version":3,"file":"setColumnID.d.ts","sourceRoot":"","sources":["../../src/schema/setColumnID.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AAG1D,OAAO,EAAE,KAAK,KAAK,EAAyB,MAAM,SAAS,CAAA;AAG3D,OAAO,KAAK,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE1D,KAAK,IAAI,GAAG;IAAE,OAAO,EAAE,eAAe,CAAC;IAAC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAAC,CAAC;IAAC,MAAM,EAAE,KAAK,EAAE,CAAA;CAAE,CAAA;AACnG,eAAO,MAAM,WAAW,iCAAkC,IAAI,KAAG,MAuBhE,CAAA"}
|
@@ -1,6 +1,6 @@
|
|
1
1
|
import { numeric, serial, uuid, varchar } from 'drizzle-orm/pg-core';
|
2
|
-
import {
|
3
|
-
import {
|
2
|
+
import { flattenTopLevelFields } from 'payload';
|
3
|
+
import { fieldAffectsData } from 'payload/shared';
|
4
4
|
export const setColumnID = ({ adapter, columns, fields })=>{
|
5
5
|
const idField = flattenTopLevelFields(fields).find((field)=>fieldAffectsData(field) && field.name === 'id');
|
6
6
|
if (idField) {
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"sources":["../../src/schema/setColumnID.ts"],"sourcesContent":["import type { PgColumnBuilder } from 'drizzle-orm/pg-core'\n\nimport { numeric, serial, uuid, varchar } from 'drizzle-orm/pg-core'\nimport { type Field,
|
1
|
+
{"version":3,"sources":["../../src/schema/setColumnID.ts"],"sourcesContent":["import type { PgColumnBuilder } from 'drizzle-orm/pg-core'\n\nimport { numeric, serial, uuid, varchar } from 'drizzle-orm/pg-core'\nimport { type Field, flattenTopLevelFields } from 'payload'\nimport { fieldAffectsData } from 'payload/shared'\n\nimport type { IDType, PostgresAdapter } from '../types.js'\n\ntype Args = { adapter: PostgresAdapter; columns: Record<string, PgColumnBuilder>; fields: Field[] }\nexport const setColumnID = ({ adapter, columns, fields }: Args): IDType => {\n const idField = flattenTopLevelFields(fields).find(\n (field) => fieldAffectsData(field) && field.name === 'id',\n )\n if (idField) {\n if (idField.type === 'number') {\n columns.id = numeric('id').primaryKey()\n return 'numeric'\n }\n\n if (idField.type === 'text') {\n columns.id = varchar('id').primaryKey()\n return 'varchar'\n }\n }\n\n if (adapter.idType === 'uuid') {\n columns.id = uuid('id').defaultRandom().primaryKey()\n return 'uuid'\n }\n\n columns.id = serial('id').primaryKey()\n return 'integer'\n}\n"],"names":["numeric","serial","uuid","varchar","flattenTopLevelFields","fieldAffectsData","setColumnID","adapter","columns","fields","idField","find","field","name","type","id","primaryKey","idType","defaultRandom"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;","mappings":"AAEA,SAASA,OAAO,EAAEC,MAAM,EAAEC,IAAI,EAAEC,OAAO,QAAQ,sBAAqB;AACpE,SAAqBC,qBAAqB,QAAQ,UAAS;AAC3D,SAASC,gBAAgB,QAAQ,iBAAgB;AAKjD,OAAO,MAAMC,cAAc,CAAC,EAAEC,OAAO,EAAEC,OAAO,EAAEC,MAAM,EAAQ;IAC5D,MAAMC,UAAUN,sBAAsBK,QAAQE,IAAI,CAChD,CAACC,QAAUP,iBAAiBO,UAAUA,MAAMC,IAAI,KAAK;IAEvD,IAAIH,SAAS;QACX,IAAIA,QAAQI,IAAI,KAAK,UAAU;YAC7BN,QAAQO,EAAE,GAAGf,QAAQ,MAAMgB,UAAU;YACrC,OAAO;QACT;QAEA,IAAIN,QAAQI,IAAI,KAAK,QAAQ;YAC3BN,QAAQO,EAAE,GAAGZ,QAAQ,MAAMa,UAAU;YACrC,OAAO;QACT;IACF;IAEA,IAAIT,QAAQU,MAAM,KAAK,QAAQ;QAC7BT,QAAQO,EAAE,GAAGb,KAAK,MAAMgB,aAAa,GAAGF,UAAU;QAClD,OAAO;IACT;IAEAR,QAAQO,EAAE,GAAGd,OAAO,MAAMe,UAAU;IACpC,OAAO;AACT,EAAC"}
|
@@ -1,11 +1,9 @@
|
|
1
1
|
import type { IndexBuilder, PgColumnBuilder } from 'drizzle-orm/pg-core';
|
2
|
-
import type { Field, TabAsField } from 'payload
|
2
|
+
import type { Field, TabAsField } from 'payload';
|
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:
|
19
|
+
relationsToBuild: RelationMap;
|
22
20
|
relationships: Set<string>;
|
23
|
-
rootRelationsToBuild?:
|
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,
|
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,
|
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,SAAS,CAAA;AAsBhD,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';
|
3
|
-
import { InvalidConfiguration } from 'payload
|
4
|
-
import { fieldAffectsData, optionIsObject } from 'payload/
|
2
|
+
import { PgNumericBuilder, PgUUIDBuilder, PgVarcharBuilder, boolean, foreignKey, index, integer, jsonb, numeric, pgEnum, text, timestamp, varchar } from 'drizzle-orm/pg-core';
|
3
|
+
import { InvalidConfiguration } from 'payload';
|
4
|
+
import { fieldAffectsData, optionIsObject } from 'payload/shared';
|
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 {
|
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,
|
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 =
|
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 =
|
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').
|
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,
|
186
|
-
|
187
|
-
|
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 =
|
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').
|
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,
|
255
|
-
|
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((
|
270
|
-
|
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 =
|
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').
|
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
|
-
|
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((
|
340
|
-
|
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
|
-
|
354
|
-
rootRelationsToBuild.set(`_blocks_${block.slug}`,
|
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
|
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 (
|
592
|
+
if (adapter.payload.config.localization && field.localized) {
|
509
593
|
hasLocalizedRelationshipField = true;
|
510
594
|
}
|
511
595
|
break;
|