@payloadcms/drizzle 4.0.0-internal.688c4d0 → 4.0.0-internal.77191e5
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/LICENSE.md +1 -1
- package/dist/createTableName.d.ts.map +1 -1
- package/dist/createTableName.js +2 -7
- package/dist/createTableName.js.map +1 -1
- 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/find/buildFindManyArgs.d.ts.map +1 -1
- package/dist/find/buildPolymorphicJoinQuery.d.ts +38 -0
- package/dist/find/buildPolymorphicJoinQuery.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinQuery.js +135 -0
- package/dist/find/buildPolymorphicJoinQuery.js.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts +20 -0
- package/dist/find/buildPolymorphicJoinWhere.d.ts.map +1 -0
- package/dist/find/buildPolymorphicJoinWhere.js +301 -0
- package/dist/find/buildPolymorphicJoinWhere.js.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts +37 -0
- package/dist/find/createPolymorphicJoinWherePlan.d.ts.map +1 -0
- package/dist/find/createPolymorphicJoinWherePlan.js +191 -0
- package/dist/find/createPolymorphicJoinWherePlan.js.map +1 -0
- package/dist/find/findMany.d.ts.map +1 -1
- package/dist/find/traverseFields.d.ts.map +1 -1
- package/dist/find/traverseFields.js +19 -191
- package/dist/find/traverseFields.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/migrate.d.ts.map +1 -1
- package/dist/migrate.js +35 -13
- package/dist/migrate.js.map +1 -1
- package/dist/migrateDown.d.ts +2 -1
- package/dist/migrateDown.d.ts.map +1 -1
- package/dist/migrateDown.js +12 -2
- package/dist/migrateDown.js.map +1 -1
- package/dist/migrateFresh.d.ts +4 -2
- package/dist/migrateFresh.d.ts.map +1 -1
- package/dist/migrateFresh.js +21 -7
- package/dist/migrateFresh.js.map +1 -1
- package/dist/migrateRefresh.d.ts +2 -1
- package/dist/migrateRefresh.d.ts.map +1 -1
- package/dist/migrateRefresh.js +14 -3
- package/dist/migrateRefresh.js.map +1 -1
- package/dist/migrateReset.d.ts +2 -1
- package/dist/migrateReset.d.ts.map +1 -1
- package/dist/migrateReset.js +12 -2
- package/dist/migrateReset.js.map +1 -1
- package/dist/migrateStatus.d.ts +2 -1
- package/dist/migrateStatus.d.ts.map +1 -1
- package/dist/migrateStatus.js +11 -6
- package/dist/migrateStatus.js.map +1 -1
- package/dist/postgres/createDatabase.d.ts.map +1 -1
- package/dist/postgres/createExtensions.d.ts.map +1 -1
- package/dist/postgres/createJSONQuery/index.d.ts.map +1 -1
- package/dist/postgres/createJSONQuery/index.js +3 -3
- package/dist/postgres/createJSONQuery/index.js.map +1 -1
- package/dist/postgres/init.d.ts.map +1 -1
- package/dist/postgres/init.js +6 -0
- package/dist/postgres/init.js.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/index.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/fetchAndResave/traverseFields.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/groupUpSQLStatements.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/index.js.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/migrateRelationships.d.ts.map +1 -1
- package/dist/postgres/predefinedMigrations/v2-v3/traverseFields.d.ts.map +1 -1
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts +15 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.d.ts.map +1 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js +25 -0
- package/dist/postgres/query/assertOperatorHandlerExtensionsInstalled.js.map +1 -0
- package/dist/postgres/query/postgresUnaccent.d.ts +13 -0
- package/dist/postgres/query/postgresUnaccent.d.ts.map +1 -0
- package/dist/postgres/query/postgresUnaccent.js +40 -0
- package/dist/postgres/query/postgresUnaccent.js.map +1 -0
- package/dist/postgres/schema/buildDrizzleTable.d.ts.map +1 -1
- package/dist/postgres/schema/geometryColumn.d.ts +2 -2
- package/dist/postgres/schema/geometryColumn.d.ts.map +1 -1
- package/dist/postgres/types.d.ts +13 -2
- package/dist/postgres/types.d.ts.map +1 -1
- package/dist/postgres/types.js.map +1 -1
- package/dist/queries/addJoinTable.d.ts +1 -1
- package/dist/queries/addJoinTable.d.ts.map +1 -1
- package/dist/queries/buildOperatorConstraint.d.ts +22 -0
- package/dist/queries/buildOperatorConstraint.d.ts.map +1 -0
- package/dist/queries/buildOperatorConstraint.js +63 -0
- package/dist/queries/buildOperatorConstraint.js.map +1 -0
- package/dist/queries/buildOrderBy.d.ts +1 -1
- package/dist/queries/buildOrderBy.d.ts.map +1 -1
- package/dist/queries/buildQuery.d.ts.map +1 -1
- package/dist/queries/getTableAlias.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.d.ts +12 -3
- package/dist/queries/getTableColumnFromPath.d.ts.map +1 -1
- package/dist/queries/getTableColumnFromPath.js +59 -27
- package/dist/queries/getTableColumnFromPath.js.map +1 -1
- package/dist/queries/operatorMap.d.ts +7 -3
- package/dist/queries/operatorMap.d.ts.map +1 -1
- package/dist/queries/operatorMap.js.map +1 -1
- package/dist/queries/parseParams.d.ts.map +1 -1
- package/dist/queries/parseParams.js +262 -10
- package/dist/queries/parseParams.js.map +1 -1
- package/dist/queries/resolveRelationshipPath.d.ts +36 -0
- package/dist/queries/resolveRelationshipPath.d.ts.map +1 -0
- package/dist/queries/resolveRelationshipPath.js +66 -0
- package/dist/queries/resolveRelationshipPath.js.map +1 -0
- package/dist/queries/sanitizeQueryValue.d.ts.map +1 -1
- package/dist/queries/selectDistinct.d.ts.map +1 -1
- package/dist/queries/validateOperatorHandlers.d.ts +8 -0
- package/dist/queries/validateOperatorHandlers.d.ts.map +1 -0
- package/dist/queries/validateOperatorHandlers.js +30 -0
- package/dist/queries/validateOperatorHandlers.js.map +1 -0
- package/dist/schema/build.d.ts.map +1 -1
- package/dist/schema/buildDrizzleRelations.d.ts.map +1 -1
- package/dist/schema/buildRawSchema.d.ts.map +1 -1
- package/dist/schema/idToUUID.d.ts.map +1 -1
- package/dist/schema/traverseFields.d.ts.map +1 -1
- package/dist/schema/traverseFields.js +6 -5
- package/dist/schema/traverseFields.js.map +1 -1
- package/dist/schema/withDefault.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js +3 -2
- package/dist/sqlite/createJSONQuery/convertPathToJSONTraversal.js.map +1 -1
- package/dist/sqlite/createJSONQuery/index.d.ts.map +1 -1
- package/dist/sqlite/createJSONQuery/index.js +6 -5
- package/dist/sqlite/createJSONQuery/index.js.map +1 -1
- package/dist/transform/read/hasManyNumber.d.ts.map +1 -1
- package/dist/transform/read/hasManyText.d.ts.map +1 -1
- package/dist/transform/read/index.d.ts.map +1 -1
- package/dist/transform/read/relationship.d.ts.map +1 -1
- package/dist/transform/read/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/array.d.ts.map +1 -1
- package/dist/transform/write/blocks.d.ts.map +1 -1
- package/dist/transform/write/index.d.ts.map +1 -1
- package/dist/transform/write/numbers.d.ts.map +1 -1
- package/dist/transform/write/relationships.d.ts.map +1 -1
- package/dist/transform/write/selects.d.ts.map +1 -1
- package/dist/transform/write/texts.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.d.ts.map +1 -1
- package/dist/transform/write/traverseFields.js +8 -14
- package/dist/transform/write/traverseFields.js.map +1 -1
- package/dist/types.d.ts +50 -6
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -3
- package/dist/types.js.map +1 -1
- package/dist/updateJobs.d.ts.map +1 -1
- package/dist/updateJobs.js +37 -0
- package/dist/updateJobs.js.map +1 -1
- package/dist/updateOne.d.ts.map +1 -1
- package/dist/updateOne.js +41 -1
- package/dist/updateOne.js.map +1 -1
- package/dist/upsertRow/deleteExistingArrayRows.d.ts.map +1 -1
- package/dist/upsertRow/deleteExistingRowsByPath.d.ts.map +1 -1
- package/dist/upsertRow/handleUpsertError.d.ts.map +1 -1
- package/dist/upsertRow/handleUpsertError.js +2 -1
- package/dist/upsertRow/handleUpsertError.js.map +1 -1
- package/dist/upsertRow/index.d.ts +1 -1
- package/dist/upsertRow/index.d.ts.map +1 -1
- package/dist/upsertRow/index.js +25 -5
- package/dist/upsertRow/index.js.map +1 -1
- package/dist/upsertRow/insertArrays.d.ts.map +1 -1
- package/dist/upsertRow/insertArrays.js +2 -1
- package/dist/upsertRow/insertArrays.js.map +1 -1
- package/dist/upsertRow/shouldUseOptimizedUpsertRow.d.ts.map +1 -1
- package/dist/utilities/appendPrefixToKeys.d.ts.map +1 -1
- package/dist/utilities/blocksToJsonMigrator.d.ts.map +1 -1
- package/dist/utilities/buildCreateMigration.d.ts.map +1 -1
- package/dist/utilities/buildCreateMigration.js +12 -8
- package/dist/utilities/buildCreateMigration.js.map +1 -1
- package/dist/utilities/buildForeignKeyName.d.ts.map +1 -1
- package/dist/utilities/buildForeignKeyName.js +3 -2
- package/dist/utilities/buildForeignKeyName.js.map +1 -1
- package/dist/utilities/buildIndexName.d.ts.map +1 -1
- package/dist/utilities/buildIndexName.js +3 -2
- package/dist/utilities/buildIndexName.js.map +1 -1
- package/dist/utilities/checkTruncatedIdentifiers.d.ts +13 -0
- package/dist/utilities/checkTruncatedIdentifiers.d.ts.map +1 -0
- package/dist/utilities/checkTruncatedIdentifiers.js +56 -0
- package/dist/utilities/checkTruncatedIdentifiers.js.map +1 -0
- package/dist/utilities/createBlocksMap.d.ts.map +1 -1
- package/dist/utilities/createRelationshipMap.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.d.ts.map +1 -1
- package/dist/utilities/createSchemaGenerator.js +3 -0
- package/dist/utilities/createSchemaGenerator.js.map +1 -1
- package/dist/utilities/escapeSQLValue.d.ts.map +1 -1
- package/dist/utilities/executeSchemaHooks.d.ts.map +1 -1
- package/dist/utilities/extendDrizzleTable.d.ts.map +1 -1
- package/dist/utilities/getArrayRelationName.d.ts.map +1 -1
- package/dist/utilities/getCollectionIdType.d.ts.map +1 -1
- package/dist/utilities/getMigrationTemplate.d.ts.map +1 -1
- package/dist/utilities/getNameFromDrizzleTable.d.ts.map +1 -1
- package/dist/utilities/getPrimaryDb.d.ts +1 -1
- package/dist/utilities/getPrimaryDb.d.ts.map +1 -1
- package/dist/utilities/getTransaction.d.ts +1 -1
- package/dist/utilities/getTransaction.d.ts.map +1 -1
- package/dist/utilities/hasLocalesTable.d.ts.map +1 -1
- package/dist/utilities/isPolymorphicRelationship.d.ts.map +1 -1
- package/dist/utilities/isUUIDType.d.ts.map +1 -1
- package/dist/utilities/json.d.ts.map +1 -1
- package/dist/utilities/migrationTableExists.d.ts.map +1 -1
- package/dist/utilities/parseError.d.ts.map +1 -1
- package/dist/utilities/parseJSONPathSegment.d.ts +9 -0
- package/dist/utilities/parseJSONPathSegment.d.ts.map +1 -0
- package/dist/utilities/parseJSONPathSegment.js +17 -0
- package/dist/utilities/parseJSONPathSegment.js.map +1 -0
- package/dist/utilities/pushDevSchema.d.ts.map +1 -1
- package/dist/utilities/rawConstraint.d.ts.map +1 -1
- package/dist/utilities/sanitizePathSegment.d.ts.map +1 -1
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts +1 -5
- package/dist/utilities/validateExistingBlockIsIdentical.d.ts.map +1 -1
- package/dist/utilities/validateExistingBlockIsIdentical.js.map +1 -1
- package/dist/utilities/validateIdentifierLength.d.ts +12 -0
- package/dist/utilities/validateIdentifierLength.d.ts.map +1 -0
- package/dist/utilities/validateIdentifierLength.js +18 -0
- package/dist/utilities/validateIdentifierLength.js.map +1 -0
- package/package.json +3 -3
- package/dist/find/chainMethods.d.ts +0 -20
- package/dist/find/chainMethods.d.ts.map +0 -1
- package/dist/find/chainMethods.js +0 -16
- package/dist/find/chainMethods.js.map +0 -1
- package/dist/utilities/isValidStringID.spec.js +0 -15
- package/dist/utilities/isValidStringID.spec.js.map +0 -1
package/LICENSE.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2018-
|
|
3
|
+
Copyright (c) 2018-2026 Payload CMS, LLC <info@payloadcms.com>
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
6
|
a copy of this software and associated documentation files (the
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createTableName.d.ts","sourceRoot":"","sources":["../src/createTableName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;
|
|
1
|
+
{"version":3,"file":"createTableName.d.ts","sourceRoot":"","sources":["../src/createTableName.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,SAAS,CAAA;AAI/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,YAAY,CAAA;AAIhD,KAAK,IAAI,GAAG;IACV,OAAO,EAAE,IAAI,CAAC,cAAc,EAAE,cAAc,GAAG,gBAAgB,CAAC,CAAA;IAChE,8CAA8C;IAC9C,MAAM,EAAE;QACN,MAAM,CAAC,EAAE,gBAAgB,CAAA;QACzB,QAAQ,CAAC,EAAE,gBAAgB,CAAA;QAC3B,IAAI,CAAC,EAAE,MAAM,CAAA;QACb,IAAI,CAAC,EAAE,MAAM,CAAA;KACd,CAAA;IACD,mGAAmG;IACnG,eAAe,CAAC,EAAE,MAAM,CAAA;IACxB,mFAAmF;IACnF,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,uHAAuH;IACvH,MAAM,CAAC,EAAE,QAAQ,GAAG,UAAU,CAAA;IAC9B,uFAAuF;IACvF,oBAAoB,CAAC,EAAE,OAAO,CAAA;IAC9B,mIAAmI;IACnI,QAAQ,CAAC,EAAE,OAAO,CAAA;IAClB,mHAAmH;IACnH,kBAAkB,CAAC,EAAE,OAAO,CAAA;CAC7B,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,eAAe,sIAUzB,IAAI,KAAG,MAmCT,CAAA"}
|
package/dist/createTableName.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { APIError } from 'payload';
|
|
2
1
|
import toSnakeCase from 'to-snake-case';
|
|
2
|
+
import { validateIdentifierLength } from './utilities/validateIdentifierLength.js';
|
|
3
3
|
/**
|
|
4
4
|
* Used to name database enums and tables
|
|
5
5
|
* Returns the table or enum name for a given entity
|
|
@@ -26,12 +26,7 @@ import toSnakeCase from 'to-snake-case';
|
|
|
26
26
|
if (!throwValidationError) {
|
|
27
27
|
return result;
|
|
28
28
|
}
|
|
29
|
-
|
|
30
|
-
throw new APIError(`Exceeded max identifier length for table or enum name of 63 characters. Invalid name: ${result}.
|
|
31
|
-
Tip: You can use the dbName property to reduce the table name length.
|
|
32
|
-
`);
|
|
33
|
-
}
|
|
34
|
-
return result;
|
|
29
|
+
return validateIdentifierLength(result);
|
|
35
30
|
};
|
|
36
31
|
|
|
37
32
|
//# sourceMappingURL=createTableName.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/createTableName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload'\n\nimport
|
|
1
|
+
{"version":3,"sources":["../src/createTableName.ts"],"sourcesContent":["import type { DBIdentifierName } from 'payload'\n\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter } from './types.js'\n\nimport { validateIdentifierLength } from './utilities/validateIdentifierLength.js'\n\ntype Args = {\n adapter: Pick<DrizzleAdapter, 'tableNameMap' | 'versionsSuffix'>\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 /** Throws error if true for postgres when table and enum names exceed 63 characters */\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) {\n defaultTableName = `_${defaultTableName}${adapter.versionsSuffix}`\n }\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\n const result = customTableNameResult || defaultTableName\n\n adapter.tableNameMap.set(defaultTableName, result)\n\n if (!throwValidationError) {\n return result\n }\n\n return validateIdentifierLength(result)\n}\n"],"names":["toSnakeCase","validateIdentifierLength","createTableName","adapter","config","name","slug","parentTableName","prefix","target","throwValidationError","versions","versionsCustomName","customNameDefinition","defaultTableName","versionsSuffix","customTableNameResult","tableName","result","tableNameMap","set"],"mappings":"AAEA,OAAOA,iBAAiB,gBAAe;AAIvC,SAASC,wBAAwB,QAAQ,0CAAyC;AAyBlF;;;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,GAAGN,SAASR,YAAYK,QAAQC,OAAO;IAE9D,IAAIK,UAAU;QACZG,mBAAmB,CAAC,CAAC,EAAEA,mBAAmBX,QAAQY,cAAc,EAAE;IACpE;IAEA,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,oBAAoB;YACtBI,wBAAwB,CAAC,CAAC,EAAEA,wBAAwBb,QAAQY,cAAc,EAAE;QAC9E;IACF;IAEA,MAAMG,SAASF,yBAAyBF;IAExCX,QAAQgB,YAAY,CAACC,GAAG,CAACN,kBAAkBI;IAE3C,IAAI,CAACR,sBAAsB;QACzB,OAAOQ;IACT;IAEA,OAAOjB,yBAAyBiB;AAClC,EAAC"}
|
|
@@ -11,6 +11,8 @@ export { init } from '../postgres/init.js';
|
|
|
11
11
|
export { insert } from '../postgres/insert.js';
|
|
12
12
|
export { migratePostgresLocalizeStatus } from '../postgres/predefinedMigrations/localize-status/index.js';
|
|
13
13
|
export { migratePostgresV2toV3 } from '../postgres/predefinedMigrations/v2-v3/index.js';
|
|
14
|
+
export { assertOperatorHandlerExtensionsInstalled } from '../postgres/query/assertOperatorHandlerExtensionsInstalled.js';
|
|
15
|
+
export { postgresUnaccent } from '../postgres/query/postgresUnaccent.js';
|
|
14
16
|
export { requireDrizzleKit } from '../postgres/requireDrizzleKit.js';
|
|
15
17
|
export { geometryColumn } from '../postgres/schema/geometryColumn.js';
|
|
16
18
|
export * from '../postgres/types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/exports/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAA;AAC5E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAA;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAA;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,6BAA6B,EAAE,MAAM,2DAA2D,CAAA;AACzG,OAAO,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAA;AACvF,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAA;AACrE,cAAc,sBAAsB,CAAA"}
|
|
1
|
+
{"version":3,"file":"postgres.d.ts","sourceRoot":"","sources":["../../src/exports/postgres.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,sCAAsC,CAAA;AAC5E,OAAO,EAAE,aAAa,EAAE,MAAM,8BAA8B,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAA;AAC9D,OAAO,EAAE,gBAAgB,EAAE,MAAM,iCAAiC,CAAA;AAClE,OAAO,EAAE,eAAe,EAAE,MAAM,sCAAsC,CAAA;AACtE,OAAO,EAAE,sBAAsB,EAAE,MAAM,gCAAgC,CAAA;AACvE,OAAO,EAAE,WAAW,EAAE,MAAM,4BAA4B,CAAA;AACxD,OAAO,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAA;AAC1D,OAAO,EAAE,OAAO,EAAE,MAAM,wBAAwB,CAAA;AAChD,OAAO,EAAE,IAAI,EAAE,MAAM,qBAAqB,CAAA;AAC1C,OAAO,EAAE,MAAM,EAAE,MAAM,uBAAuB,CAAA;AAC9C,OAAO,EAAE,6BAA6B,EAAE,MAAM,2DAA2D,CAAA;AACzG,OAAO,EAAE,qBAAqB,EAAE,MAAM,iDAAiD,CAAA;AACvF,OAAO,EAAE,wCAAwC,EAAE,MAAM,+DAA+D,CAAA;AACxH,OAAO,EAAE,gBAAgB,EAAE,MAAM,uCAAuC,CAAA;AACxE,OAAO,EAAE,iBAAiB,EAAE,MAAM,kCAAkC,CAAA;AACpE,OAAO,EAAE,cAAc,EAAE,MAAM,sCAAsC,CAAA;AACrE,cAAc,sBAAsB,CAAA"}
|
package/dist/exports/postgres.js
CHANGED
|
@@ -11,6 +11,8 @@ export { init } from '../postgres/init.js';
|
|
|
11
11
|
export { insert } from '../postgres/insert.js';
|
|
12
12
|
export { migratePostgresLocalizeStatus } from '../postgres/predefinedMigrations/localize-status/index.js';
|
|
13
13
|
export { migratePostgresV2toV3 } from '../postgres/predefinedMigrations/v2-v3/index.js';
|
|
14
|
+
export { assertOperatorHandlerExtensionsInstalled } from '../postgres/query/assertOperatorHandlerExtensionsInstalled.js';
|
|
15
|
+
export { postgresUnaccent } from '../postgres/query/postgresUnaccent.js';
|
|
14
16
|
export { requireDrizzleKit } from '../postgres/requireDrizzleKit.js';
|
|
15
17
|
export { geometryColumn } from '../postgres/schema/geometryColumn.js';
|
|
16
18
|
export * from '../postgres/types.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/exports/postgres.ts"],"sourcesContent":["export { columnToCodeConverter } from '../postgres/columnToCodeConverter.js'\nexport { countDistinct } from '../postgres/countDistinct.js'\nexport { createDatabase } from '../postgres/createDatabase.js'\nexport { createExtensions } from '../postgres/createExtensions.js'\nexport { createJSONQuery } from '../postgres/createJSONQuery/index.js'\nexport { defaultDrizzleSnapshot } from '../postgres/defaultSnapshot.js'\nexport { deleteWhere } from '../postgres/deleteWhere.js'\nexport { dropDatabase } from '../postgres/dropDatabase.js'\nexport { execute } from '../postgres/execute.js'\nexport { init } from '../postgres/init.js'\nexport { insert } from '../postgres/insert.js'\nexport { migratePostgresLocalizeStatus } from '../postgres/predefinedMigrations/localize-status/index.js'\nexport { migratePostgresV2toV3 } from '../postgres/predefinedMigrations/v2-v3/index.js'\nexport { requireDrizzleKit } from '../postgres/requireDrizzleKit.js'\nexport { geometryColumn } from '../postgres/schema/geometryColumn.js'\nexport * from '../postgres/types.js'\n"],"names":["columnToCodeConverter","countDistinct","createDatabase","createExtensions","createJSONQuery","defaultDrizzleSnapshot","deleteWhere","dropDatabase","execute","init","insert","migratePostgresLocalizeStatus","migratePostgresV2toV3","requireDrizzleKit","geometryColumn"],"mappings":"AAAA,SAASA,qBAAqB,QAAQ,uCAAsC;AAC5E,SAASC,aAAa,QAAQ,+BAA8B;AAC5D,SAASC,cAAc,QAAQ,gCAA+B;AAC9D,SAASC,gBAAgB,QAAQ,kCAAiC;AAClE,SAASC,eAAe,QAAQ,uCAAsC;AACtE,SAASC,sBAAsB,QAAQ,iCAAgC;AACvE,SAASC,WAAW,QAAQ,6BAA4B;AACxD,SAASC,YAAY,QAAQ,8BAA6B;AAC1D,SAASC,OAAO,QAAQ,yBAAwB;AAChD,SAASC,IAAI,QAAQ,sBAAqB;AAC1C,SAASC,MAAM,QAAQ,wBAAuB;AAC9C,SAASC,6BAA6B,QAAQ,4DAA2D;AACzG,SAASC,qBAAqB,QAAQ,kDAAiD;AACvF,SAASC,iBAAiB,QAAQ,mCAAkC;AACpE,SAASC,cAAc,QAAQ,uCAAsC;AACrE,cAAc,uBAAsB"}
|
|
1
|
+
{"version":3,"sources":["../../src/exports/postgres.ts"],"sourcesContent":["export { columnToCodeConverter } from '../postgres/columnToCodeConverter.js'\nexport { countDistinct } from '../postgres/countDistinct.js'\nexport { createDatabase } from '../postgres/createDatabase.js'\nexport { createExtensions } from '../postgres/createExtensions.js'\nexport { createJSONQuery } from '../postgres/createJSONQuery/index.js'\nexport { defaultDrizzleSnapshot } from '../postgres/defaultSnapshot.js'\nexport { deleteWhere } from '../postgres/deleteWhere.js'\nexport { dropDatabase } from '../postgres/dropDatabase.js'\nexport { execute } from '../postgres/execute.js'\nexport { init } from '../postgres/init.js'\nexport { insert } from '../postgres/insert.js'\nexport { migratePostgresLocalizeStatus } from '../postgres/predefinedMigrations/localize-status/index.js'\nexport { migratePostgresV2toV3 } from '../postgres/predefinedMigrations/v2-v3/index.js'\nexport { assertOperatorHandlerExtensionsInstalled } from '../postgres/query/assertOperatorHandlerExtensionsInstalled.js'\nexport { postgresUnaccent } from '../postgres/query/postgresUnaccent.js'\nexport { requireDrizzleKit } from '../postgres/requireDrizzleKit.js'\nexport { geometryColumn } from '../postgres/schema/geometryColumn.js'\nexport * from '../postgres/types.js'\n"],"names":["columnToCodeConverter","countDistinct","createDatabase","createExtensions","createJSONQuery","defaultDrizzleSnapshot","deleteWhere","dropDatabase","execute","init","insert","migratePostgresLocalizeStatus","migratePostgresV2toV3","assertOperatorHandlerExtensionsInstalled","postgresUnaccent","requireDrizzleKit","geometryColumn"],"mappings":"AAAA,SAASA,qBAAqB,QAAQ,uCAAsC;AAC5E,SAASC,aAAa,QAAQ,+BAA8B;AAC5D,SAASC,cAAc,QAAQ,gCAA+B;AAC9D,SAASC,gBAAgB,QAAQ,kCAAiC;AAClE,SAASC,eAAe,QAAQ,uCAAsC;AACtE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AC,CAAA;CAC1C,GAAG,aAAa,CAAC,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,GAAG,CAAC,CAAA;AAIzC,eAAO,MAAM,iBAAiB,sHAY3B,kBAAkB,KAAG,MAkGvB,CAAA"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import type { SQL } from 'drizzle-orm';
|
|
2
|
+
import { type FlattenedJoinField, type Where } from 'payload';
|
|
3
|
+
import type { DrizzleAdapter } from '../types.js';
|
|
4
|
+
type BuildPolymorphicJoinQueryArgs = {
|
|
5
|
+
adapter: DrizzleAdapter;
|
|
6
|
+
currentTableName: string;
|
|
7
|
+
field: FlattenedJoinField;
|
|
8
|
+
limit: number;
|
|
9
|
+
locale?: string;
|
|
10
|
+
page?: number;
|
|
11
|
+
path: string;
|
|
12
|
+
shouldCount: boolean;
|
|
13
|
+
sort?: string | string[];
|
|
14
|
+
where?: Where;
|
|
15
|
+
};
|
|
16
|
+
type PolymorphicJoinQuery = {
|
|
17
|
+
columnName: string;
|
|
18
|
+
count?: SQL.Aliased;
|
|
19
|
+
documents: SQL.Aliased;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Builds the correlated document and optional count projections for a join that targets multiple
|
|
23
|
+
* collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort
|
|
24
|
+
* columns before the branches are combined. A missing sort field resolves to SQL NULL so targets
|
|
25
|
+
* with different schemas can still share one ordered union.
|
|
26
|
+
*
|
|
27
|
+
* `field.collection` must contain at least one configured collection. Each mapped table must have
|
|
28
|
+
* an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can
|
|
29
|
+
* include an extra look-ahead row when they calculate pagination.
|
|
30
|
+
*
|
|
31
|
+
* @returns The stable projection name, the document projection, and the count projection when
|
|
32
|
+
* `shouldCount` is true.
|
|
33
|
+
* @throws If the join definition, target collection, mapped table, required columns, or sort shape
|
|
34
|
+
* cannot be represented by the polymorphic query.
|
|
35
|
+
*/
|
|
36
|
+
export declare const buildPolymorphicJoinQuery: ({ adapter, currentTableName, field, limit, locale, page, path, shouldCount, sort, where, }: BuildPolymorphicJoinQueryArgs) => PolymorphicJoinQuery;
|
|
37
|
+
export {};
|
|
38
|
+
//# sourceMappingURL=buildPolymorphicJoinQuery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildPolymorphicJoinQuery.d.ts","sourceRoot":"","sources":["../../src/find/buildPolymorphicJoinQuery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAE,MAAM,aAAa,CAAA;AAKtC,OAAO,EAAE,KAAK,kBAAkB,EAAE,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAG7D,OAAO,KAAK,EAAE,cAAc,EAAgB,MAAM,aAAa,CAAA;AAO/D,KAAK,6BAA6B,GAAG;IACnC,OAAO,EAAE,cAAc,CAAA;IACvB,gBAAgB,EAAE,MAAM,CAAA;IACxB,KAAK,EAAE,kBAAkB,CAAA;IACzB,KAAK,EAAE,MAAM,CAAA;IACb,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,OAAO,CAAA;IACpB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;IACxB,KAAK,CAAC,EAAE,KAAK,CAAA;CACd,CAAA;AAED,KAAK,oBAAoB,GAAG;IAC1B,UAAU,EAAE,MAAM,CAAA;IAClB,KAAK,CAAC,EAAE,GAAG,CAAC,OAAO,CAAA;IACnB,SAAS,EAAE,GAAG,CAAC,OAAO,CAAA;CACvB,CAAA;AAED;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,yBAAyB,+FAWnC,6BAA6B,KAAG,oBA6HlC,CAAA"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { asc, count, desc, eq, sql } from 'drizzle-orm';
|
|
2
|
+
import toSnakeCase from 'to-snake-case';
|
|
3
|
+
import { jsonAggBuildObject } from '../utilities/json.js';
|
|
4
|
+
import { buildPolymorphicJoinWhere } from './buildPolymorphicJoinWhere.js';
|
|
5
|
+
import { createPolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js';
|
|
6
|
+
/**
|
|
7
|
+
* Builds the correlated document and optional count projections for a join that targets multiple
|
|
8
|
+
* collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort
|
|
9
|
+
* columns before the branches are combined. A missing sort field resolves to SQL NULL so targets
|
|
10
|
+
* with different schemas can still share one ordered union.
|
|
11
|
+
*
|
|
12
|
+
* `field.collection` must contain at least one configured collection. Each mapped table must have
|
|
13
|
+
* an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can
|
|
14
|
+
* include an extra look-ahead row when they calculate pagination.
|
|
15
|
+
*
|
|
16
|
+
* @returns The stable projection name, the document projection, and the count projection when
|
|
17
|
+
* `shouldCount` is true.
|
|
18
|
+
* @throws If the join definition, target collection, mapped table, required columns, or sort shape
|
|
19
|
+
* cannot be represented by the polymorphic query.
|
|
20
|
+
*/ export const buildPolymorphicJoinQuery = ({ adapter, currentTableName, field, limit, locale, page, path, shouldCount, sort, where })=>{
|
|
21
|
+
if (!Array.isArray(field.collection)) {
|
|
22
|
+
throw new Error('A polymorphic join requires a collection array');
|
|
23
|
+
}
|
|
24
|
+
if (field.collection.length === 0) {
|
|
25
|
+
throw new Error('A polymorphic join requires at least one collection');
|
|
26
|
+
}
|
|
27
|
+
if (Array.isArray(sort)) {
|
|
28
|
+
throw new Error('Polymorphic joins do not support multiple sort fields');
|
|
29
|
+
}
|
|
30
|
+
const collections = field.collection;
|
|
31
|
+
const columnName = `${path.replaceAll('.', '_')}${field.name}`;
|
|
32
|
+
const db = adapter.drizzle;
|
|
33
|
+
const onPath = field.on.split('.').join('_');
|
|
34
|
+
const collectionTables = collections.map((collection)=>{
|
|
35
|
+
if (!adapter.payload.collections[collection]) {
|
|
36
|
+
throw new Error(`Unknown polymorphic join collection "${collection}"`);
|
|
37
|
+
}
|
|
38
|
+
const tableName = adapter.tableNameMap.get(toSnakeCase(collection));
|
|
39
|
+
const table = tableName ? adapter.tables[tableName] : undefined;
|
|
40
|
+
if (!table) {
|
|
41
|
+
throw new Error(`Polymorphic join collection "${collection}" has no database table`);
|
|
42
|
+
}
|
|
43
|
+
if (!table['id']) {
|
|
44
|
+
throw new Error(`Polymorphic join collection "${collection}" has no "id" column`);
|
|
45
|
+
}
|
|
46
|
+
if (!table[onPath]) {
|
|
47
|
+
throw new Error(`Polymorphic join collection "${collection}" has no "${onPath}" column`);
|
|
48
|
+
}
|
|
49
|
+
return {
|
|
50
|
+
collection,
|
|
51
|
+
table
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
const sortField = getSortField({
|
|
55
|
+
adapter,
|
|
56
|
+
collections,
|
|
57
|
+
sort
|
|
58
|
+
});
|
|
59
|
+
const sortOrder = sortField.startsWith('-') ? desc : asc;
|
|
60
|
+
const sortPath = sortField.replace('-', '').split('.').join('_');
|
|
61
|
+
const wherePlan = where ? createPolymorphicJoinWherePlan({
|
|
62
|
+
adapter,
|
|
63
|
+
collections,
|
|
64
|
+
where
|
|
65
|
+
}) : new Map();
|
|
66
|
+
let unionQuery = null;
|
|
67
|
+
for (const { collection, table } of collectionTables){
|
|
68
|
+
const sortColumn = table[sortPath];
|
|
69
|
+
const selectFields = {
|
|
70
|
+
id: table['id'],
|
|
71
|
+
parent: sql`${table[onPath]}`.as(onPath),
|
|
72
|
+
relationTo: sql`${collection}`.as('relationTo'),
|
|
73
|
+
sortPath: sql`${sortColumn ?? null}`.as('sortPath')
|
|
74
|
+
};
|
|
75
|
+
const collectionWhere = where && Object.keys(where).length > 0 ? buildPolymorphicJoinWhere({
|
|
76
|
+
adapter,
|
|
77
|
+
collection,
|
|
78
|
+
locale,
|
|
79
|
+
table,
|
|
80
|
+
where,
|
|
81
|
+
wherePlan
|
|
82
|
+
}) : undefined;
|
|
83
|
+
let collectionQuery = db.select(selectFields).from(table).$dynamic();
|
|
84
|
+
if (collectionWhere) {
|
|
85
|
+
collectionQuery = collectionQuery.where(collectionWhere);
|
|
86
|
+
}
|
|
87
|
+
if (unionQuery === null) {
|
|
88
|
+
unionQuery = collectionQuery;
|
|
89
|
+
} else {
|
|
90
|
+
unionQuery = unionQuery.unionAll(collectionQuery);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const subQueryAlias = `${columnName}_subquery`;
|
|
94
|
+
const parentConstraint = eq(sql.raw(`"${currentTableName}"."id"`), sql.raw(`"${subQueryAlias}"."${onPath}"`));
|
|
95
|
+
const countExpression = shouldCount ? sql`${db.select({
|
|
96
|
+
count: count()
|
|
97
|
+
}).from(sql`${unionQuery.as(subQueryAlias)}`).where(parentConstraint)}`.as(`${columnName}_count`) : undefined;
|
|
98
|
+
unionQuery = unionQuery.orderBy(sortOrder(sql`"sortPath"`));
|
|
99
|
+
const sortedUnionAlias = `${columnName}_sorted`;
|
|
100
|
+
let limitOffsetSQL = sql.empty();
|
|
101
|
+
if (limit) {
|
|
102
|
+
limitOffsetSQL = sql` LIMIT ${limit}`;
|
|
103
|
+
}
|
|
104
|
+
if (page && limit !== 0) {
|
|
105
|
+
const offset = (page - 1) * limit;
|
|
106
|
+
if (offset > 0) {
|
|
107
|
+
limitOffsetSQL = sql`${limitOffsetSQL} OFFSET ${offset}`;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
const parentWhere = sql.raw(`"${sortedUnionAlias}"."${onPath}" = "${currentTableName}"."id"`);
|
|
111
|
+
const documents = sql`(
|
|
112
|
+
SELECT ${jsonAggBuildObject(adapter, {
|
|
113
|
+
id: sql.raw(`"${subQueryAlias}"."id"`),
|
|
114
|
+
relationTo: sql.raw(`"${subQueryAlias}"."relationTo"`)
|
|
115
|
+
})}
|
|
116
|
+
FROM (
|
|
117
|
+
SELECT * FROM ${sql`${unionQuery.as(sortedUnionAlias)}`}
|
|
118
|
+
WHERE ${parentWhere}${limitOffsetSQL}
|
|
119
|
+
) AS ${sql.raw(`"${subQueryAlias}"`)}
|
|
120
|
+
)`.as(columnName);
|
|
121
|
+
return {
|
|
122
|
+
columnName,
|
|
123
|
+
count: countExpression,
|
|
124
|
+
documents
|
|
125
|
+
};
|
|
126
|
+
};
|
|
127
|
+
const getSortField = ({ adapter, collections, sort })=>{
|
|
128
|
+
if (sort) {
|
|
129
|
+
return sort;
|
|
130
|
+
}
|
|
131
|
+
const hasCreatedAt = collections.some((collection)=>adapter.payload.collections[collection].config.fields.some((field)=>field.type === 'date' && field.name === 'createdAt'));
|
|
132
|
+
return hasCreatedAt ? '-createdAt' : 'id';
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
//# sourceMappingURL=buildPolymorphicJoinQuery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/find/buildPolymorphicJoinQuery.ts"],"sourcesContent":["import type { SQL } from 'drizzle-orm'\nimport type { LibSQLDatabase } from 'drizzle-orm/libsql'\nimport type { SQLiteSelect } from 'drizzle-orm/sqlite-core'\n\nimport { asc, count, desc, eq, sql } from 'drizzle-orm'\nimport { type FlattenedJoinField, type Where } from 'payload'\nimport toSnakeCase from 'to-snake-case'\n\nimport type { DrizzleAdapter, GenericTable } from '../types.js'\nimport type { PolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js'\n\nimport { jsonAggBuildObject } from '../utilities/json.js'\nimport { buildPolymorphicJoinWhere } from './buildPolymorphicJoinWhere.js'\nimport { createPolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js'\n\ntype BuildPolymorphicJoinQueryArgs = {\n adapter: DrizzleAdapter\n currentTableName: string\n field: FlattenedJoinField\n limit: number\n locale?: string\n page?: number\n path: string\n shouldCount: boolean\n sort?: string | string[]\n where?: Where\n}\n\ntype PolymorphicJoinQuery = {\n columnName: string\n count?: SQL.Aliased\n documents: SQL.Aliased\n}\n\n/**\n * Builds the correlated document and optional count projections for a join that targets multiple\n * collections. Every collection branch exposes the same `id`, parent, `relationTo`, and sort\n * columns before the branches are combined. A missing sort field resolves to SQL NULL so targets\n * with different schemas can still share one ordered union.\n *\n * `field.collection` must contain at least one configured collection. Each mapped table must have\n * an `id` column and the column named by `field.on`. The supplied `limit` is used as-is; callers can\n * include an extra look-ahead row when they calculate pagination.\n *\n * @returns The stable projection name, the document projection, and the count projection when\n * `shouldCount` is true.\n * @throws If the join definition, target collection, mapped table, required columns, or sort shape\n * cannot be represented by the polymorphic query.\n */\nexport const buildPolymorphicJoinQuery = ({\n adapter,\n currentTableName,\n field,\n limit,\n locale,\n page,\n path,\n shouldCount,\n sort,\n where,\n}: BuildPolymorphicJoinQueryArgs): PolymorphicJoinQuery => {\n if (!Array.isArray(field.collection)) {\n throw new Error('A polymorphic join requires a collection array')\n }\n\n if (field.collection.length === 0) {\n throw new Error('A polymorphic join requires at least one collection')\n }\n\n if (Array.isArray(sort)) {\n throw new Error('Polymorphic joins do not support multiple sort fields')\n }\n\n const collections = field.collection\n const columnName = `${path.replaceAll('.', '_')}${field.name}`\n const db = adapter.drizzle as LibSQLDatabase\n const onPath = field.on.split('.').join('_')\n const collectionTables = collections.map((collection) => {\n if (!adapter.payload.collections[collection]) {\n throw new Error(`Unknown polymorphic join collection \"${collection}\"`)\n }\n\n const tableName = adapter.tableNameMap.get(toSnakeCase(collection))\n const table = tableName ? (adapter.tables[tableName] as GenericTable | undefined) : undefined\n\n if (!table) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no database table`)\n }\n\n if (!table['id']) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no \"id\" column`)\n }\n\n if (!table[onPath]) {\n throw new Error(`Polymorphic join collection \"${collection}\" has no \"${onPath}\" column`)\n }\n\n return { collection, table }\n })\n const sortField = getSortField({ adapter, collections, sort })\n const sortOrder = sortField.startsWith('-') ? desc : asc\n const sortPath = sortField.replace('-', '').split('.').join('_')\n const wherePlan: PolymorphicJoinWherePlan = where\n ? createPolymorphicJoinWherePlan({ adapter, collections, where })\n : new Map()\n let unionQuery: null | SQLiteSelect = null\n\n for (const { collection, table } of collectionTables) {\n const sortColumn = table[sortPath]\n const selectFields = {\n id: table['id'],\n parent: sql`${table[onPath]}`.as(onPath),\n relationTo: sql`${collection}`.as('relationTo'),\n sortPath: sql`${sortColumn ?? null}`.as('sortPath'),\n }\n const collectionWhere =\n where && Object.keys(where).length > 0\n ? buildPolymorphicJoinWhere({\n adapter,\n collection,\n locale,\n table,\n where,\n wherePlan,\n })\n : undefined\n let collectionQuery = db.select(selectFields).from(table).$dynamic()\n\n if (collectionWhere) {\n collectionQuery = collectionQuery.where(collectionWhere)\n }\n\n if (unionQuery === null) {\n unionQuery = collectionQuery as unknown as SQLiteSelect\n } else {\n unionQuery = unionQuery.unionAll(collectionQuery)\n }\n }\n\n const subQueryAlias = `${columnName}_subquery`\n const parentConstraint = eq(\n sql.raw(`\"${currentTableName}\".\"id\"`),\n sql.raw(`\"${subQueryAlias}\".\"${onPath}\"`),\n )\n const countExpression = shouldCount\n ? sql`${db\n .select({ count: count() })\n .from(sql`${unionQuery.as(subQueryAlias)}`)\n .where(parentConstraint)}`.as(`${columnName}_count`)\n : undefined\n\n unionQuery = unionQuery.orderBy(sortOrder(sql`\"sortPath\"`))\n\n const sortedUnionAlias = `${columnName}_sorted`\n let limitOffsetSQL = sql.empty()\n\n if (limit) {\n limitOffsetSQL = sql` LIMIT ${limit}`\n }\n\n if (page && limit !== 0) {\n const offset = (page - 1) * limit\n\n if (offset > 0) {\n limitOffsetSQL = sql`${limitOffsetSQL} OFFSET ${offset}`\n }\n }\n\n const parentWhere = sql.raw(`\"${sortedUnionAlias}\".\"${onPath}\" = \"${currentTableName}\".\"id\"`)\n const documents = sql`(\n SELECT ${jsonAggBuildObject(adapter, {\n id: sql.raw(`\"${subQueryAlias}\".\"id\"`),\n relationTo: sql.raw(`\"${subQueryAlias}\".\"relationTo\"`),\n })}\n FROM (\n SELECT * FROM ${sql`${unionQuery.as(sortedUnionAlias)}`}\n WHERE ${parentWhere}${limitOffsetSQL}\n ) AS ${sql.raw(`\"${subQueryAlias}\"`)}\n )`.as(columnName)\n\n return {\n columnName,\n count: countExpression,\n documents,\n }\n}\n\nconst getSortField = ({\n adapter,\n collections,\n sort,\n}: {\n adapter: DrizzleAdapter\n collections: string[]\n sort?: string\n}): string => {\n if (sort) {\n return sort\n }\n\n const hasCreatedAt = collections.some((collection) =>\n adapter.payload.collections[collection].config.fields.some(\n (field) => field.type === 'date' && field.name === 'createdAt',\n ),\n )\n\n return hasCreatedAt ? '-createdAt' : 'id'\n}\n"],"names":["asc","count","desc","eq","sql","toSnakeCase","jsonAggBuildObject","buildPolymorphicJoinWhere","createPolymorphicJoinWherePlan","buildPolymorphicJoinQuery","adapter","currentTableName","field","limit","locale","page","path","shouldCount","sort","where","Array","isArray","collection","Error","length","collections","columnName","replaceAll","name","db","drizzle","onPath","on","split","join","collectionTables","map","payload","tableName","tableNameMap","get","table","tables","undefined","sortField","getSortField","sortOrder","startsWith","sortPath","replace","wherePlan","Map","unionQuery","sortColumn","selectFields","id","parent","as","relationTo","collectionWhere","Object","keys","collectionQuery","select","from","$dynamic","unionAll","subQueryAlias","parentConstraint","raw","countExpression","orderBy","sortedUnionAlias","limitOffsetSQL","empty","offset","parentWhere","documents","hasCreatedAt","some","config","fields","type"],"mappings":"AAIA,SAASA,GAAG,EAAEC,KAAK,EAAEC,IAAI,EAAEC,EAAE,EAAEC,GAAG,QAAQ,cAAa;AAEvD,OAAOC,iBAAiB,gBAAe;AAKvC,SAASC,kBAAkB,QAAQ,uBAAsB;AACzD,SAASC,yBAAyB,QAAQ,iCAAgC;AAC1E,SAASC,8BAA8B,QAAQ,sCAAqC;AAqBpF;;;;;;;;;;;;;;CAcC,GACD,OAAO,MAAMC,4BAA4B,CAAC,EACxCC,OAAO,EACPC,gBAAgB,EAChBC,KAAK,EACLC,KAAK,EACLC,MAAM,EACNC,IAAI,EACJC,IAAI,EACJC,WAAW,EACXC,IAAI,EACJC,KAAK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AAC,CAAC,EAAE;GACtC,CAAC,CAACV,EAAE,CAAC/B;IAEN,OAAO;QACLA;QACAzB,OAAOqE;QACPO;IACF;AACF,EAAC;AAED,MAAMhC,eAAe,CAAC,EACpBnC,OAAO,EACPe,WAAW,EACXP,IAAI,EAKL;IACC,IAAIA,MAAM;QACR,OAAOA;IACT;IAEA,MAAM4D,eAAerD,YAAYsD,IAAI,CAAC,CAACzD,aACrCZ,QAAQ2B,OAAO,CAACZ,WAAW,CAACH,WAAW,CAAC0D,MAAM,CAACC,MAAM,CAACF,IAAI,CACxD,CAACnE,QAAUA,MAAMsE,IAAI,KAAK,UAAUtE,MAAMgB,IAAI,KAAK;IAIvD,OAAOkD,eAAe,eAAe;AACvC"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { SQL } from 'drizzle-orm';
|
|
2
|
+
import { type Where } from 'payload';
|
|
3
|
+
import type { DrizzleAdapter, GenericTable } from '../types.js';
|
|
4
|
+
import type { PolymorphicJoinWherePlan } from './createPolymorphicJoinWherePlan.js';
|
|
5
|
+
type BuildPolymorphicJoinWhereArgs = {
|
|
6
|
+
adapter: DrizzleAdapter;
|
|
7
|
+
collection: string;
|
|
8
|
+
locale?: string;
|
|
9
|
+
table: GenericTable;
|
|
10
|
+
where: Where;
|
|
11
|
+
wherePlan: PolymorphicJoinWherePlan;
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* Builds a where clause for one collection before the polymorphic branches are combined. A field
|
|
15
|
+
* that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current
|
|
16
|
+
* collection slug.
|
|
17
|
+
*/
|
|
18
|
+
export declare const buildPolymorphicJoinWhere: ({ adapter, collection, locale, table, where, wherePlan, }: BuildPolymorphicJoinWhereArgs) => SQL | undefined;
|
|
19
|
+
export {};
|
|
20
|
+
//# sourceMappingURL=buildPolymorphicJoinWhere.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"buildPolymorphicJoinWhere.d.ts","sourceRoot":"","sources":["../../src/find/buildPolymorphicJoinWhere.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAU,GAAG,EAAE,MAAM,aAAa,CAAA;AAG9C,OAAO,EAAkD,KAAK,KAAK,EAAE,MAAM,SAAS,CAAA;AAIpF,OAAO,KAAK,EAAE,cAAc,EAAE,YAAY,EAAE,MAAM,aAAa,CAAA;AAC/D,OAAO,KAAK,EACV,wBAAwB,EAGzB,MAAM,qCAAqC,CAAA;AAO5C,KAAK,6BAA6B,GAAG;IACnC,OAAO,EAAE,cAAc,CAAA;IACvB,UAAU,EAAE,MAAM,CAAA;IAClB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,YAAY,CAAA;IACnB,KAAK,EAAE,KAAK,CAAA;IACZ,SAAS,EAAE,wBAAwB,CAAA;CACpC,CAAA;AAqBD;;;;GAIG;AACH,eAAO,MAAM,yBAAyB,8DAOnC,6BAA6B,KAAG,GAAG,GAAG,SAsMxC,CAAA"}
|
|
@@ -0,0 +1,301 @@
|
|
|
1
|
+
import { and, getTableName, isNotNull, isNull, or, sql } from 'drizzle-orm';
|
|
2
|
+
import { QueryError } from 'payload';
|
|
3
|
+
import toSnakeCase from 'to-snake-case';
|
|
4
|
+
import { buildOperatorConstraint } from '../queries/buildOperatorConstraint.js';
|
|
5
|
+
import { sanitizeQueryValue } from '../queries/sanitizeQueryValue.js';
|
|
6
|
+
const supportedJSONQueryOperators = new Set([
|
|
7
|
+
'contains',
|
|
8
|
+
'equals',
|
|
9
|
+
'exists',
|
|
10
|
+
'in',
|
|
11
|
+
'like'
|
|
12
|
+
]);
|
|
13
|
+
/**
|
|
14
|
+
* Builds a where clause for one collection before the polymorphic branches are combined. A field
|
|
15
|
+
* that is absent from this collection resolves to SQL NULL, and relationTo resolves to the current
|
|
16
|
+
* collection slug.
|
|
17
|
+
*/ export const buildPolymorphicJoinWhere = ({ adapter, collection, locale, table, where, wherePlan })=>{
|
|
18
|
+
const constraints = [];
|
|
19
|
+
for(const key in where){
|
|
20
|
+
if ([
|
|
21
|
+
'AND',
|
|
22
|
+
'OR'
|
|
23
|
+
].includes(key.toUpperCase())) {
|
|
24
|
+
if (!Array.isArray(where[key])) {
|
|
25
|
+
throw new QueryError([
|
|
26
|
+
{
|
|
27
|
+
path: key
|
|
28
|
+
}
|
|
29
|
+
]);
|
|
30
|
+
}
|
|
31
|
+
const combine = key.toUpperCase() === 'AND' ? and : or;
|
|
32
|
+
const nestedConstraints = where[key].map((nestedWhere)=>buildPolymorphicJoinWhere({
|
|
33
|
+
adapter,
|
|
34
|
+
collection,
|
|
35
|
+
locale,
|
|
36
|
+
table,
|
|
37
|
+
where: nestedWhere,
|
|
38
|
+
wherePlan
|
|
39
|
+
}));
|
|
40
|
+
const nestedCondition = combine(...nestedConstraints);
|
|
41
|
+
if (nestedCondition) {
|
|
42
|
+
constraints.push(nestedCondition);
|
|
43
|
+
}
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
const resolvedPath = resolveWherePath({
|
|
47
|
+
adapter,
|
|
48
|
+
collection,
|
|
49
|
+
schemaPath: key,
|
|
50
|
+
table,
|
|
51
|
+
wherePlan
|
|
52
|
+
});
|
|
53
|
+
for (let payloadOperator of Object.keys(where[key])){
|
|
54
|
+
const originalOperator = payloadOperator;
|
|
55
|
+
let value = where[key][payloadOperator];
|
|
56
|
+
if (resolvedPath.type === 'invalid') {
|
|
57
|
+
throw new QueryError([
|
|
58
|
+
{
|
|
59
|
+
path: `${key}.${payloadOperator}`
|
|
60
|
+
}
|
|
61
|
+
]);
|
|
62
|
+
}
|
|
63
|
+
if (resolvedPath.type === 'hasManySelect') {
|
|
64
|
+
if (!supportedJSONQueryOperators.has(payloadOperator)) {
|
|
65
|
+
throw new QueryError([
|
|
66
|
+
{
|
|
67
|
+
path: `${key}.${payloadOperator}`
|
|
68
|
+
}
|
|
69
|
+
]);
|
|
70
|
+
}
|
|
71
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
72
|
+
adapter,
|
|
73
|
+
field: resolvedPath.fieldContext,
|
|
74
|
+
isUUID: false,
|
|
75
|
+
operator: payloadOperator,
|
|
76
|
+
relationOrPath: key,
|
|
77
|
+
val: value
|
|
78
|
+
});
|
|
79
|
+
if (sanitizedQueryValue === null) {
|
|
80
|
+
throw new QueryError([
|
|
81
|
+
{
|
|
82
|
+
path: `${key}.${originalOperator}`
|
|
83
|
+
}
|
|
84
|
+
]);
|
|
85
|
+
}
|
|
86
|
+
if (sanitizedQueryValue.columns) {
|
|
87
|
+
throw new QueryError([
|
|
88
|
+
{
|
|
89
|
+
path: `${key}.${originalOperator}`
|
|
90
|
+
}
|
|
91
|
+
]);
|
|
92
|
+
}
|
|
93
|
+
value = sanitizedQueryValue.value;
|
|
94
|
+
if (payloadOperator === 'in' && Array.isArray(value) && value.length === 0) {
|
|
95
|
+
constraints.push(sql.raw('false'));
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
const jsonQueryOperator = payloadOperator === 'exists' ? 'exists' : sanitizedQueryValue.operator;
|
|
99
|
+
constraints.push(sql.raw(adapter.createJSONQuery({
|
|
100
|
+
column: resolvedPath.jsonExpression,
|
|
101
|
+
operator: jsonQueryOperator,
|
|
102
|
+
pathSegments: [
|
|
103
|
+
resolvedPath.pathPlan.columnPath
|
|
104
|
+
],
|
|
105
|
+
value
|
|
106
|
+
})));
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
if (payloadOperator === '$raw') {
|
|
110
|
+
if (typeof value !== 'string') {
|
|
111
|
+
throw new QueryError([
|
|
112
|
+
{
|
|
113
|
+
path: `${key}.${payloadOperator}`
|
|
114
|
+
}
|
|
115
|
+
]);
|
|
116
|
+
}
|
|
117
|
+
constraints.push(sql.raw(value));
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
const { column, fieldContext, queryValueContext } = resolvedPath;
|
|
121
|
+
if (payloadOperator === 'like' && (queryValueContext.field.type === 'number' || queryValueContext.field.type === 'relationship' || queryValueContext.field.type === 'upload' || queryValueContext.isUUID)) {
|
|
122
|
+
payloadOperator = 'equals';
|
|
123
|
+
}
|
|
124
|
+
const sanitizedQueryValue = sanitizeQueryValue({
|
|
125
|
+
adapter,
|
|
126
|
+
field: queryValueContext.field,
|
|
127
|
+
isUUID: queryValueContext.isUUID,
|
|
128
|
+
operator: payloadOperator,
|
|
129
|
+
relationOrPath: key,
|
|
130
|
+
val: value
|
|
131
|
+
});
|
|
132
|
+
if (sanitizedQueryValue === null) {
|
|
133
|
+
throw new QueryError([
|
|
134
|
+
{
|
|
135
|
+
path: `${key}.${originalOperator}`
|
|
136
|
+
}
|
|
137
|
+
]);
|
|
138
|
+
}
|
|
139
|
+
if (sanitizedQueryValue.columns) {
|
|
140
|
+
throw new QueryError([
|
|
141
|
+
{
|
|
142
|
+
path: `${key}.${originalOperator}`
|
|
143
|
+
}
|
|
144
|
+
]);
|
|
145
|
+
}
|
|
146
|
+
payloadOperator = sanitizedQueryValue.operator;
|
|
147
|
+
value = sanitizedQueryValue.value;
|
|
148
|
+
if (!(payloadOperator in adapter.operators)) {
|
|
149
|
+
throw new QueryError([
|
|
150
|
+
{
|
|
151
|
+
path: `${key}.${originalOperator}`
|
|
152
|
+
}
|
|
153
|
+
]);
|
|
154
|
+
}
|
|
155
|
+
const resolvedOperator = payloadOperator;
|
|
156
|
+
const operator = adapter.operators[resolvedOperator];
|
|
157
|
+
const buildColumnConstraint = (constraintValue)=>{
|
|
158
|
+
if (!fieldContext) {
|
|
159
|
+
return operator(column, constraintValue);
|
|
160
|
+
}
|
|
161
|
+
return buildOperatorConstraint({
|
|
162
|
+
adapter,
|
|
163
|
+
column,
|
|
164
|
+
field: fieldContext.field,
|
|
165
|
+
locale,
|
|
166
|
+
originalOperator,
|
|
167
|
+
path: key,
|
|
168
|
+
resolvedOperator,
|
|
169
|
+
value: constraintValue
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
if (originalOperator === 'equals' && value === null) {
|
|
173
|
+
constraints.push(isNull(column));
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (originalOperator === 'not_equals') {
|
|
177
|
+
if (value === null) {
|
|
178
|
+
constraints.push(isNotNull(column));
|
|
179
|
+
} else {
|
|
180
|
+
const notEqualsConstraint = or(isNull(column), buildColumnConstraint(value));
|
|
181
|
+
if (notEqualsConstraint) {
|
|
182
|
+
constraints.push(notEqualsConstraint);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
continue;
|
|
186
|
+
}
|
|
187
|
+
if (originalOperator === 'in' && Array.isArray(value) && value.includes(null)) {
|
|
188
|
+
const nonNullValues = value.filter((item)=>item !== null);
|
|
189
|
+
const inConstraint = buildColumnConstraint(nonNullValues);
|
|
190
|
+
const nullAwareInConstraint = or(isNull(column), inConstraint);
|
|
191
|
+
if (nullAwareInConstraint) {
|
|
192
|
+
constraints.push(nullAwareInConstraint);
|
|
193
|
+
}
|
|
194
|
+
continue;
|
|
195
|
+
}
|
|
196
|
+
if (payloadOperator === 'like' && typeof value === 'string') {
|
|
197
|
+
const wordConstraints = value.split(' ').map((word)=>buildColumnConstraint(`%${word}%`));
|
|
198
|
+
const wordCondition = and(...wordConstraints);
|
|
199
|
+
if (wordCondition) {
|
|
200
|
+
constraints.push(wordCondition);
|
|
201
|
+
}
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
constraints.push(buildColumnConstraint(value));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return and(...constraints);
|
|
208
|
+
};
|
|
209
|
+
const resolveWherePath = ({ adapter, collection, schemaPath, table, wherePlan })=>{
|
|
210
|
+
const pathPlan = wherePlan.get(schemaPath);
|
|
211
|
+
if (!pathPlan) {
|
|
212
|
+
throw new QueryError([
|
|
213
|
+
{
|
|
214
|
+
path: schemaPath
|
|
215
|
+
}
|
|
216
|
+
]);
|
|
217
|
+
}
|
|
218
|
+
if (pathPlan.type === 'invalid') {
|
|
219
|
+
return {
|
|
220
|
+
type: 'invalid',
|
|
221
|
+
pathPlan
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
if (schemaPath === 'relationTo') {
|
|
225
|
+
return {
|
|
226
|
+
type: 'scalar',
|
|
227
|
+
column: sql`${collection}`,
|
|
228
|
+
pathPlan,
|
|
229
|
+
queryValueContext: {
|
|
230
|
+
type: 'scalar',
|
|
231
|
+
field: {
|
|
232
|
+
name: 'relationTo',
|
|
233
|
+
type: 'text'
|
|
234
|
+
},
|
|
235
|
+
isUUID: false
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
if (pathPlan.type === 'hasManySelect') {
|
|
240
|
+
const fieldContext = [
|
|
241
|
+
...pathPlan.fieldsByCollection.values()
|
|
242
|
+
].find((field)=>field.type === 'hasManySelect')?.field;
|
|
243
|
+
if (!fieldContext) {
|
|
244
|
+
return {
|
|
245
|
+
type: 'invalid',
|
|
246
|
+
pathPlan
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
return {
|
|
250
|
+
type: 'hasManySelect',
|
|
251
|
+
fieldContext,
|
|
252
|
+
jsonExpression: getHasManySelectExpression({
|
|
253
|
+
adapter,
|
|
254
|
+
collection,
|
|
255
|
+
pathPlan,
|
|
256
|
+
table
|
|
257
|
+
}),
|
|
258
|
+
pathPlan
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
const collectionField = pathPlan.fieldsByCollection.get(collection);
|
|
262
|
+
const fieldContext = collectionField?.type === 'scalar' ? collectionField : undefined;
|
|
263
|
+
const queryValueContext = fieldContext ?? [
|
|
264
|
+
...pathPlan.fieldsByCollection.values()
|
|
265
|
+
].find((field)=>field.type === 'scalar');
|
|
266
|
+
if (!queryValueContext) {
|
|
267
|
+
return {
|
|
268
|
+
type: 'invalid',
|
|
269
|
+
pathPlan
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
return {
|
|
273
|
+
type: 'scalar',
|
|
274
|
+
column: fieldContext ? table[pathPlan.columnPath] : sql`null`,
|
|
275
|
+
fieldContext,
|
|
276
|
+
pathPlan,
|
|
277
|
+
queryValueContext
|
|
278
|
+
};
|
|
279
|
+
};
|
|
280
|
+
const getHasManySelectExpression = ({ adapter, collection, pathPlan, table })=>{
|
|
281
|
+
if (pathPlan.fieldsByCollection.get(collection)?.type !== 'hasManySelect') {
|
|
282
|
+
return adapter.name === 'postgres' ? `'[]'::jsonb` : `'[]'`;
|
|
283
|
+
}
|
|
284
|
+
const selectTablePath = pathPlan.schemaPath.split('.').map((pathSegment)=>toSnakeCase(pathSegment)).join('_');
|
|
285
|
+
const valuesTableName = adapter.tableNameMap.get(`${toSnakeCase(collection)}_${selectTablePath}`);
|
|
286
|
+
if (!valuesTableName) {
|
|
287
|
+
throw new Error(`Polymorphic join collection "${collection}" has no value table for "${pathPlan.schemaPath}"`);
|
|
288
|
+
}
|
|
289
|
+
let qualifiedValuesTableName = valuesTableName;
|
|
290
|
+
let parentTableName = getTableName(table);
|
|
291
|
+
if (adapter.schemaName) {
|
|
292
|
+
qualifiedValuesTableName = `"${adapter.schemaName}"."${valuesTableName}"`;
|
|
293
|
+
parentTableName = `"${adapter.schemaName}"."${parentTableName}"`;
|
|
294
|
+
}
|
|
295
|
+
if (adapter.name === 'postgres') {
|
|
296
|
+
return `(select coalesce(jsonb_agg(${qualifiedValuesTableName}.value), '[]'::jsonb) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
297
|
+
}
|
|
298
|
+
return `(select json_group_array(${qualifiedValuesTableName}.value) from ${qualifiedValuesTableName} where ${qualifiedValuesTableName}.parent_id = ${parentTableName}.id)`;
|
|
299
|
+
};
|
|
300
|
+
|
|
301
|
+
//# sourceMappingURL=buildPolymorphicJoinWhere.js.map
|