Skip to content

The open alternative to Salesforce, designed for AI.

ORM v2: TypeORM parity for joins / andSelect / distinctOn (#24225)

Charles Bochetc90bf57216a1
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-hydration.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-hydration.spec.ts
new file mode 100644
index 00000000000..4608bae3307
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-hydration.spec.ts
@@ -0,0 +1,172 @@
+import { TwentyOrmV2Exception } from 'src/engine/twenty-orm-v2/exceptions/twenty-orm-v2.exception';
+import {
+ SCHEMA_NAME,
+ buildQueryBuilder,
+} from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+
+describe('WorkspaceSelectQueryBuilderV2 joined hydration', () => {
+ it('should project every joined column for a leftJoinAndSelect', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoinAndSelect('person.company', 'company');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain(
+ '"company"."id" AS "company_id", "company"."name" AS "company_name", ' +
+ '"company"."deletedAt" AS "company_deletedAt"',
+ );
+ expect(sql).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."company" AS "company" ` +
+ 'ON ("person"."companyId" = "company"."id")',
+ );
+ });
+
+ it('should render an inner join for an innerJoinAndSelect', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.innerJoinAndSelect('person.company', 'company');
+
+ expect(queryBuilder.getQuery()).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."company" AS "company"`,
+ );
+ });
+
+ it('should hydrate joined columns under the relation property', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [
+ {
+ person_id: 'person-1',
+ company_id: 'company-1',
+ company_name: 'Acme',
+ company_deletedAt: null,
+ },
+ ],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoinAndSelect('person.company', 'company');
+
+ await expect(queryBuilder.getMany()).resolves.toEqual([
+ {
+ id: 'person-1',
+ company: { id: 'company-1', name: 'Acme', deletedAt: null },
+ },
+ ]);
+ });
+
+ it('should hydrate an unmatched left-joined relation as null', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [
+ {
+ person_id: 'person-1',
+ company_id: null,
+ company_name: null,
+ company_deletedAt: null,
+ },
+ ],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoinAndSelect('person.company', 'company');
+
+ await expect(queryBuilder.getMany()).resolves.toEqual([
+ { id: 'person-1', company: null },
+ ]);
+ });
+
+ it('should hydrate a partial relation column selected without an output alias', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [{ person_id: 'person-1', company_name: 'Acme' }],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.addSelect('company.name');
+ queryBuilder.leftJoin('person.company', 'company');
+
+ expect(queryBuilder.getQuery()).toContain(
+ '"company"."name" AS "company_name"',
+ );
+ await expect(queryBuilder.getMany()).resolves.toEqual([
+ { id: 'person-1', company: { name: 'Acme' } },
+ ]);
+ });
+
+ it('should keep a partial relation object whose only selected column is null', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [{ person_id: 'person-1', company_name: null }],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.addSelect('company.name');
+ queryBuilder.leftJoin('person.company', 'company');
+
+ await expect(queryBuilder.getMany()).resolves.toEqual([
+ { id: 'person-1', company: { name: null } },
+ ]);
+ });
+
+ it('should keep an aliased relation column selection as raw output', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [{ person_id: 'person-1', companyName: 'Acme' }],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addSelect('company.name', 'companyName');
+
+ expect(queryBuilder.getQuery()).toContain(
+ '"company"."name" AS "companyName"',
+ );
+ await expect(queryBuilder.getMany()).resolves.toEqual([{ id: 'person-1' }]);
+ });
+
+ it('should reject a partial selection on an unknown alias or column', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+
+ queryBuilder.addSelect('unknown.name');
+ expect(() => queryBuilder.getQuery()).toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should keep the v1 permission surface for joinAndSelect projections', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoinAndSelect('person.company', 'company');
+
+ const columnNamesByAlias = queryBuilder.getReferencedColumnNamesByAlias();
+
+ expect(columnNamesByAlias['person']).toEqual(['id']);
+ expect(columnNamesByAlias['company']).toBeUndefined();
+ });
+
+ it('should still reject a to-many joinAndSelect for an entity-hydrating read', async () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoinAndSelect('person.people', 'people');
+
+ await expect(queryBuilder.getMany()).rejects.toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should render an added join condition on a selected inner join', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.innerJoinAndSelect('person.company', 'company');
+ queryBuilder.addJoinCondition('company', '"company"."name" = :rlsName');
+ queryBuilder.setParameters({ rlsName: 'Acme' });
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('("company"."name" = :rlsName)');
+ expect(sql.indexOf('"company"."name" = :rlsName')).toBeLessThan(
+ sql.indexOf('WHERE'),
+ );
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-joins.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-joins.spec.ts
new file mode 100644
index 00000000000..6249d411a8b
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-joins.spec.ts
@@ -0,0 +1,279 @@
+import { TwentyOrmV2Exception } from 'src/engine/twenty-orm-v2/exceptions/twenty-orm-v2.exception';
+import {
+ SCHEMA_NAME,
+ buildQueryBuilder,
+} from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+
+describe('WorkspaceSelectQueryBuilderV2 joins', () => {
+ it('should join a to-one relation on its foreign key', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+
+ expect(queryBuilder.getQuery()).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."company" AS "company" ` +
+ 'ON ("person"."companyId" = "company"."id")',
+ );
+ });
+
+ it('should render an inner join for a to-one relation', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.innerJoin('person.company', 'company');
+
+ expect(queryBuilder.getQuery()).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."company" AS "company" ` +
+ 'ON ("person"."companyId" = "company"."id")',
+ );
+ });
+
+ it('should resolve a join path rooted on a joined alias', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.leftJoin('company.person', 'companyPerson');
+
+ expect(queryBuilder.getQuery()).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."company" AS "companyPerson" ` +
+ 'ON ("company"."personId" = "companyPerson"."id")',
+ );
+ });
+
+ it('should reject a join path rooted on an unknown alias', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ expect(() => queryBuilder.leftJoin('unknown.company', 'company')).toThrow(
+ TwentyOrmV2Exception,
+ );
+ });
+
+ it('should reject a join alias that collides with the main alias', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ expect(() => queryBuilder.leftJoin('person.company', 'person')).toThrow(
+ TwentyOrmV2Exception,
+ );
+ });
+
+ it('should report a to-many join to the shared guard rather than rendering it', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.people', 'people');
+
+ expect(queryBuilder.expressionMap.joinAttributes).toEqual([
+ {
+ alias: { name: 'people' },
+ relation: { isOneToMany: true, isManyToMany: false },
+ },
+ ]);
+ expect(() => queryBuilder.getQuery()).toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should render a deduped to-many join on the inverse foreign key when the caller opts in', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.people', 'people', undefined, {
+ allowToManyJoin: true,
+ });
+
+ expect(queryBuilder.getQuery()).toContain(
+ `LEFT JOIN (SELECT DISTINCT ON ("personId") * ` +
+ `FROM "${SCHEMA_NAME}"."company" ` +
+ `WHERE "deletedAt" IS NULL ` +
+ `ORDER BY "personId", "id") AS "people" ` +
+ 'ON ("people"."personId" = "person"."id")',
+ );
+ });
+
+ it('should render a plain to-many join for a raw read', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('person.id', 'id');
+ queryBuilder.innerJoin('person.people', 'people');
+
+ await queryBuilder.getRawMany();
+
+ expect(executedStatements[0].text).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."company" AS "people" ` +
+ 'ON ("people"."personId" = "person"."id") AND ("people"."deletedAt" IS NULL)',
+ );
+ });
+
+ it('should render a plain to-many left join for a raw read', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('person.id', 'id');
+ queryBuilder.leftJoin('person.people', 'people');
+
+ await queryBuilder.getRawMany();
+
+ expect(executedStatements[0].text).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."company" AS "people" ` +
+ 'ON ("people"."personId" = "person"."id")',
+ );
+ });
+
+ it('should still reject a plain to-many join for an entity-hydrating read', async () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.people', 'people');
+
+ await expect(queryBuilder.getMany()).rejects.toThrow(TwentyOrmV2Exception);
+ await expect(queryBuilder.getOne()).rejects.toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should reject a to-many join with an explicit condition for an entity-hydrating read', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin(
+ 'person.people',
+ 'people',
+ '"people"."personId" = "person"."id"',
+ );
+
+ await expect(queryBuilder.getMany()).rejects.toThrow(TwentyOrmV2Exception);
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('person.id', 'id');
+ await queryBuilder.getRawMany();
+
+ expect(executedStatements[0].text).toContain(
+ 'ON ("people"."personId" = "person"."id")',
+ );
+ });
+
+ it('should count distinct main records when a to-many join multiplies rows', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder({
+ rows: [{ count: '3' }],
+ });
+
+ queryBuilder.innerJoin('person.people', 'people');
+ queryBuilder.where('people.name = :name', { name: 'Acme' });
+ queryBuilder.groupBy('person.id');
+
+ const count = await queryBuilder.getCount();
+
+ expect(count).toBe(3);
+ expect(executedStatements[0].text).toContain(
+ 'SELECT COUNT(DISTINCT "person"."id") AS "count"',
+ );
+ expect(executedStatements[0].text).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."company" AS "people"`,
+ );
+ expect(executedStatements[0].text).not.toContain('GROUP BY');
+ });
+
+ it('should build a COUNT statement that ignores projection, order and pagination', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder({
+ rows: [{ count: '7' }],
+ });
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .orderBy('"person"."id"', 'ASC')
+ .take(10);
+
+ const count = await queryBuilder.getCount();
+
+ expect(count).toBe(7);
+ expect(executedStatements[0].text).toBe(
+ 'SELECT COUNT(1) AS "count" ' +
+ `FROM "${SCHEMA_NAME}"."person" AS "person" ` +
+ 'WHERE "person"."deletedAt" IS NULL',
+ );
+ });
+
+ it('should render an added join condition on a plain to-many join', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('person.id', 'id');
+ queryBuilder.innerJoin('person.people', 'people');
+ queryBuilder.addJoinCondition('people', '"people"."name" = :ownerName');
+ queryBuilder.setParameters({ ownerName: 'Acme' });
+
+ await queryBuilder.getRawMany();
+
+ expect(executedStatements[0].text).toContain(
+ '("people"."personId" = "person"."id") AND ("people"."name" = $1)',
+ );
+ });
+
+ it('should filter soft-deleted joined rows in the ON clause', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('("company"."deletedAt" IS NULL)');
+ expect(sql.indexOf('"company"."deletedAt"')).toBeLessThan(
+ sql.indexOf('WHERE'),
+ );
+ });
+
+ it('should add a joined predicate to ON rather than WHERE', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addJoinCondition('company', '"company"."name" = :owner');
+ queryBuilder.setParameters({ owner: 'Acme' });
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('("company"."name" = :owner)');
+ expect(sql.indexOf('"company"."name"')).toBeLessThan(sql.indexOf('WHERE'));
+ });
+
+ it('should not widen the projection when a relation is joined', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'SELECT "person"."id" AS "person_id" FROM',
+ );
+ });
+
+ it('should carry applied row-level markers onto a clone', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
+ expect(queryBuilder.clone().markRowLevelPermissionApplied('person')).toBe(
+ false,
+ );
+ });
+
+ it('should clear applied row-level markers when where() replaces the WHERE', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.markRowLevelPermissionApplied('person');
+ queryBuilder.where('"person"."id" = :id', { id: 1 });
+
+ expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
+ });
+
+ it('should keep joined row-level markers when where() replaces the WHERE', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.markRowLevelPermissionApplied('person');
+ queryBuilder.markRowLevelPermissionApplied('company');
+ queryBuilder.where('"person"."id" = :id', { id: 1 });
+
+ expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
+ expect(queryBuilder.markRowLevelPermissionApplied('company')).toBe(false);
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-order-group.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-order-group.spec.ts
new file mode 100644
index 00000000000..d8b42a29d26
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-order-group.spec.ts
@@ -0,0 +1,163 @@
+import { buildQueryBuilder } from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+
+describe('WorkspaceSelectQueryBuilderV2 order, group and distinct', () => {
+ it('should render order by with direction and nulls handling', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.orderBy({
+ 'person.nameFirstName': { order: 'DESC', nulls: 'NULLS LAST' },
+ 'company.name': { order: 'ASC' },
+ });
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'ORDER BY "person"."nameFirstName" DESC NULLS LAST, "company"."name" ASC',
+ );
+ });
+
+ it('should render a DISTINCT ON clause with normalised columns', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.distinctOn(['company.name', 'nameFirstName']);
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'SELECT DISTINCT ON ("company"."name", "person"."nameFirstName") ',
+ );
+ });
+
+ it('should order by an added-select alias without qualifying it', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('MAX("person"."nameFirstName")', 'max_first_name');
+ queryBuilder.groupBy('person.companyId');
+ queryBuilder.orderBy('max_first_name', 'DESC');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('ORDER BY "max_first_name" DESC');
+ expect(sql).not.toContain('"person"."max_first_name"');
+ });
+
+ it('should not report an added-select order-by alias as a selected column', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('MAX("person"."nameFirstName")', 'max_first_name');
+ queryBuilder.orderBy('max_first_name', 'DESC');
+
+ expect(queryBuilder.getReferencedColumnNamesByAlias()['person']).toEqual([
+ 'nameFirstName',
+ ]);
+ });
+
+ it('should report distinct-on columns as referenced for permissions', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.distinctOn(['company.name']);
+
+ expect(queryBuilder.getReferencedColumnNamesByAlias()['company']).toEqual([
+ 'name',
+ ]);
+ });
+
+ it('should quote bare alias.column references inside select and order expressions', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addSelect('MAX(company.name)', 'max_name');
+ queryBuilder.groupBy('person.id');
+ queryBuilder.addOrderBy('MIN(company.name)', 'ASC');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('MAX("company"."name") AS "max_name"');
+ expect(sql).toContain('ORDER BY MIN("company"."name") ASC');
+ });
+
+ it('should attribute aggregate references over a joined alias to that alias', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addSelect('MAX(company.name)', 'max_name');
+
+ expect(queryBuilder.getReferencedColumnNamesByAlias()['company']).toEqual([
+ 'name',
+ ]);
+ });
+
+ it('should report joined-alias columns referenced only in order by', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.orderBy({ 'company.name': { order: 'ASC' } });
+
+ const columnNamesByAlias = queryBuilder.getReferencedColumnNamesByAlias();
+
+ expect(columnNamesByAlias['person']).toEqual(['id']);
+ expect(columnNamesByAlias['company']).toEqual(['name']);
+ });
+
+ it('should report joined-alias columns referenced only in an added select', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addSelect('"company"."name"', 'company_name');
+
+ expect(queryBuilder.getReferencedColumnNamesByAlias()['company']).toEqual([
+ 'name',
+ ]);
+ });
+
+ it('should not attribute a joined column to the main alias', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.addSelect('"company"."name"', 'company_name');
+
+ const columnNamesByAlias = queryBuilder.getReferencedColumnNamesByAlias();
+
+ expect(columnNamesByAlias['person']).toEqual(['id']);
+ expect(columnNamesByAlias['company']).toEqual(['name']);
+ });
+
+ it('should emit a GROUP BY with aggregate and grouped columns', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .select([])
+ .addSelect('COUNT(*)', 'totalCount')
+ .addSelect('"person"."companyId"', 'companyId')
+ .groupBy('"person"."companyId"');
+
+ expect(queryBuilder.getQuery()).toBe(
+ 'SELECT COUNT(*) AS "totalCount", "person"."companyId" AS "companyId" ' +
+ `FROM "workspace_1wgvd1injqtife6y4rvfbu3h5"."person" AS "person" ` +
+ 'WHERE "person"."deletedAt" IS NULL ' +
+ 'GROUP BY "person"."companyId"',
+ );
+ });
+
+ it('should append additional GROUP BY expressions with addGroupBy', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .select([])
+ .addSelect('COUNT(*)', 'totalCount')
+ .groupBy('"person"."companyId"')
+ .addGroupBy('"person"."nameFirstName"');
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'GROUP BY "person"."companyId", "person"."nameFirstName"',
+ );
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-selection.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-selection.spec.ts
new file mode 100644
index 00000000000..daf40f9b5ec
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-selection.spec.ts
@@ -0,0 +1,286 @@
+import { WorkspaceSelectQueryBuilderV2 } from 'src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2';
+import {
+ SCHEMA_NAME,
+ buildColumn,
+ buildQueryBuilder,
+ companyTableShape,
+ personTableShape,
+} from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+import { buildColumnResultAlias } from 'src/engine/twenty-orm-v2/sql/utils/build-column-result-alias.util';
+import { TwentyOrmV2Exception } from 'src/engine/twenty-orm-v2/exceptions/twenty-orm-v2.exception';
+
+describe('WorkspaceSelectQueryBuilderV2 selection', () => {
+ it('should select the requested columns with alias-prefixed output names', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({
+ select: { id: true, nameFirstName: true },
+ });
+
+ expect(queryBuilder.getQuery()).toBe(
+ 'SELECT "person"."id" AS "person_id", "person"."nameFirstName" AS "person_nameFirstName" ' +
+ `FROM "${SCHEMA_NAME}"."person" AS "person" ` +
+ 'WHERE "person"."deletedAt" IS NULL',
+ );
+ });
+
+ it('should compile named parameters coming from the shared filter parser', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .andWhere('"person"."nameFirstName" ILIKE :name7f3a', {
+ name7f3a: '%ac%',
+ });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('"person"."nameFirstName" ILIKE $1');
+ expect(values).toEqual(['%ac%']);
+ });
+
+ it('should add the soft-delete predicate unless withDeleted was called', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ expect(queryBuilder.getQuery()).toContain('"person"."deletedAt" IS NULL');
+
+ queryBuilder.withDeleted();
+ expect(queryBuilder.getQuery()).not.toContain(
+ '"person"."deletedAt" IS NULL',
+ );
+ });
+
+ it('should reject a column that does not exist on the object', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { nonExistent: true } });
+
+ expect(() => queryBuilder.getQuery()).toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should select only aggregate expressions after select([])', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true, nameFirstName: true } });
+ queryBuilder.select([]);
+ queryBuilder.addSelect('COUNT("person"."id")', 'totalCount');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('SELECT COUNT("person"."id") AS "totalCount" FROM');
+ expect(sql).not.toContain('"person"."nameFirstName"');
+ });
+
+ it('should count columns referenced only inside aggregates as selected', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select([]);
+ queryBuilder.addSelect('MAX("person"."nameFirstName")', 'maxFirstName');
+
+ expect(queryBuilder.getSelectedColumnNames()).toContain('nameFirstName');
+ });
+
+ it('should keep an explicit empty selection on a clone', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.select([]);
+ queryBuilder.addSelect('COUNT("person"."id")', 'totalCount');
+
+ expect(queryBuilder.clone().getQuery()).not.toContain('"person"."id" AS');
+ });
+
+ it('should treat selecting the main alias as selecting every main column', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.select('person');
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('"person"."id" AS "person_id"');
+ expect(sql).toContain('"person"."nameFirstName" AS "person_nameFirstName"');
+ });
+
+ it('should strip the alias prefix when mapping rows back to records', async () => {
+ const { queryBuilder } = buildQueryBuilder({
+ rows: [
+ {
+ person_id: 'person-1',
+ person_nameFirstName: 'Ada',
+ company_name: 'Acme',
+ },
+ ],
+ });
+
+ queryBuilder.setFindOptions({ select: { id: true, nameFirstName: true } });
+
+ const records = await queryBuilder.getMany();
+
+ expect(records).toEqual([{ id: 'person-1', nameFirstName: 'Ada' }]);
+ });
+
+ it('should keep result aliases within the postgres identifier limit', async () => {
+ const longColumnName = `veryLongSubfield${'X'.repeat(60)}`;
+ const otherLongColumnName = `veryLongSubfield${'Y'.repeat(60)}`;
+ const resultAlias = buildColumnResultAlias('person', longColumnName);
+ const otherResultAlias = buildColumnResultAlias(
+ 'person',
+ otherLongColumnName,
+ );
+
+ const { queryBuilder } = buildQueryBuilder({
+ tableShape: {
+ ...personTableShape,
+ columnShapeByColumnName: {
+ ...personTableShape.columnShapeByColumnName,
+ [longColumnName]: buildColumn(longColumnName),
+ [otherLongColumnName]: buildColumn(otherLongColumnName),
+ },
+ },
+ rows: [{ [resultAlias]: 'left', [otherResultAlias]: 'right' }],
+ });
+
+ queryBuilder.setFindOptions({
+ select: { [longColumnName]: true, [otherLongColumnName]: true },
+ });
+
+ expect(resultAlias.length).toBeLessThanOrEqual(63);
+ expect(resultAlias).not.toBe(otherResultAlias);
+ expect(queryBuilder.getQuery()).toContain(
+ `"person"."${longColumnName}" AS "${resultAlias}"`,
+ );
+ await expect(queryBuilder.getMany()).resolves.toEqual([
+ { [longColumnName]: 'left', [otherLongColumnName]: 'right' },
+ ]);
+ });
+
+ it('should report the columns it selected so permissions do not have to parse SQL', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true, companyId: true } });
+
+ expect(queryBuilder.getSelectedColumnNames()).toEqual(['id', 'companyId']);
+ });
+
+ it('should keep a clone independent of the builder it came from', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+
+ const clone = queryBuilder.clone();
+
+ clone.andWhere('"person"."id" = :id', { id: 1 });
+
+ expect(queryBuilder.getQuery()).not.toContain('"person"."id" = :id');
+ expect(clone.getQuery()).toContain('"person"."id" = :id');
+ });
+
+ it('should send one parameterised statement to the executor', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .andWhere('"person"."nameFirstName" = :name', { name: 'Ada' });
+
+ await queryBuilder.getMany();
+
+ expect(executedStatements).toHaveLength(1);
+ expect(executedStatements[0].text).toContain('$1');
+ expect(executedStatements[0].values).toEqual(['Ada']);
+ });
+
+ it('should map take and skip to LIMIT and OFFSET parameters', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .take(31)
+ .skip(60);
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('LIMIT $1');
+ expect(text).toContain('OFFSET $2');
+ expect(values).toEqual([31, 60]);
+ });
+
+ it('should reuse one statement shape across page sizes and offsets', () => {
+ const buildPaginatedQuery = (limit: number, offset: number) => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.limit(limit);
+ queryBuilder.offset(offset);
+
+ return queryBuilder.getQueryAndParameters();
+ };
+
+ const [firstText, firstValues] = buildPaginatedQuery(30, 0);
+ const [secondText, secondValues] = buildPaginatedQuery(60, 120);
+
+ expect(firstText).toBe(secondText);
+ expect(firstValues).toEqual([30, 0]);
+ expect(secondValues).toEqual([60, 120]);
+ });
+
+ it('should emit a plain parameterised LIMIT, with no distinct sub-select', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.limit(31);
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('LIMIT $');
+ expect(text).not.toContain('DISTINCT');
+ expect(values).toContain(31);
+ });
+
+ it('should restore the previous limit when getOne fails', async () => {
+ const failingBuilder = new WorkspaceSelectQueryBuilderV2('person', {
+ tableShape: personTableShape,
+ executor: {
+ execute: async () => {
+ throw new Error('connection lost');
+ },
+ },
+ objectRecordsPermissions: {},
+ tableShapeByObjectMetadataId: () => companyTableShape,
+ onBeforeExecute: () => undefined,
+ formatResult: (records) => records as never,
+ });
+
+ failingBuilder.setFindOptions({ select: { id: true } });
+ failingBuilder.limit(25);
+
+ await expect(failingBuilder.getOne()).rejects.toThrow('connection lost');
+ expect(failingBuilder.getQueryAndParameters()[1]).toEqual([25]);
+ });
+
+ it('should return every raw row from getRawMany', async () => {
+ const { queryBuilder, executedStatements } = buildQueryBuilder({
+ rows: [
+ { companyId: 'c1', totalCount: '3' },
+ { companyId: 'c2', totalCount: '1' },
+ ],
+ });
+
+ queryBuilder
+ .select([])
+ .addSelect('COUNT(*)', 'totalCount')
+ .addSelect('"person"."companyId"', 'companyId')
+ .groupBy('"person"."companyId"');
+
+ const rows = await queryBuilder.getRawMany();
+
+ expect(rows).toEqual([
+ { companyId: 'c1', totalCount: '3' },
+ { companyId: 'c2', totalCount: '1' },
+ ]);
+ expect(executedStatements[0].text).toContain(
+ 'GROUP BY "person"."companyId"',
+ );
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util.ts
new file mode 100644
index 00000000000..136df479ab7
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util.ts
@@ -0,0 +1,111 @@
+import { FieldMetadataType } from 'twenty-shared/types';
+
+import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
+import { WorkspaceSelectQueryBuilderV2 } from 'src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2';
+import { type CompiledStatement } from 'src/engine/twenty-orm-v2/sql/utils/compile-named-parameters.util';
+import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
+
+export const SCHEMA_NAME = 'workspace_1wgvd1injqtife6y4rvfbu3h5';
+
+export const buildColumn = (
+ columnName: string,
+ compositeParentFieldName?: string,
+) => ({
+ columnName,
+ fieldMetadataId: `field-${columnName}`,
+ fieldName: compositeParentFieldName ?? columnName,
+ fieldMetadataType: FieldMetadataType.TEXT,
+ ...(compositeParentFieldName !== undefined
+ ? { compositeParentFieldName }
+ : {}),
+});
+
+export const companyTableShape: WorkspaceTableShape = {
+ objectMetadataId: 'company-object-id',
+ nameSingular: 'company',
+ schemaName: SCHEMA_NAME,
+ tableName: 'company',
+ columnShapeByColumnName: {
+ id: buildColumn('id'),
+ name: buildColumn('name'),
+ deletedAt: buildColumn('deletedAt'),
+ },
+ columnNames: ['id', 'name', 'deletedAt'],
+ relationShapeByFieldName: {
+ person: {
+ fieldName: 'person',
+ fieldMetadataId: 'field-company',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'person-object-id',
+ targetFieldMetadataId: 'field-people',
+ joinColumnName: 'personId',
+ },
+ },
+ hasDeletedAtColumn: true,
+};
+
+export const personTableShape: WorkspaceTableShape = {
+ objectMetadataId: 'person-object-id',
+ nameSingular: 'person',
+ schemaName: SCHEMA_NAME,
+ tableName: 'person',
+ columnShapeByColumnName: {
+ id: buildColumn('id'),
+ nameFirstName: buildColumn('nameFirstName', 'name'),
+ nameLastName: buildColumn('nameLastName', 'name'),
+ companyId: buildColumn('companyId'),
+ deletedAt: buildColumn('deletedAt'),
+ },
+ columnNames: [
+ 'id',
+ 'nameFirstName',
+ 'nameLastName',
+ 'companyId',
+ 'deletedAt',
+ ],
+ relationShapeByFieldName: {
+ company: {
+ fieldName: 'company',
+ fieldMetadataId: 'field-company',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'company-object-id',
+ targetFieldMetadataId: 'field-people',
+ joinColumnName: 'companyId',
+ },
+ people: {
+ fieldName: 'people',
+ fieldMetadataId: 'field-people',
+ relationType: RelationType.ONE_TO_MANY,
+ targetObjectMetadataId: 'company-object-id',
+ targetFieldMetadataId: 'field-company',
+ },
+ },
+ hasDeletedAtColumn: true,
+};
+
+export const buildQueryBuilder = ({
+ rows = [],
+ tableShape = personTableShape,
+}: {
+ rows?: Record<string, unknown>[];
+ tableShape?: WorkspaceTableShape;
+} = {}) => {
+ const executedStatements: CompiledStatement[] = [];
+
+ const queryBuilder = new WorkspaceSelectQueryBuilderV2('person', {
+ tableShape,
+ executor: {
+ execute: async (statement) => {
+ executedStatements.push(statement);
+
+ return rows;
+ },
+ },
+ objectRecordsPermissions: {},
+ tableShapeByObjectMetadataId: () => companyTableShape,
+ onBeforeExecute: () => undefined,
+ formatResult: (records) => records as never,
+ });
+
+ return { queryBuilder, executedStatements };
+};
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-timeline-messaging.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-timeline-messaging.spec.ts
new file mode 100644
index 00000000000..08597cbfb6d
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-timeline-messaging.spec.ts
@@ -0,0 +1,371 @@
+import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
+import {
+ SCHEMA_NAME,
+ buildColumn,
+} from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+import { WorkspaceSelectQueryBuilderV2 } from 'src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2';
+import { type CompiledStatement } from 'src/engine/twenty-orm-v2/sql/utils/compile-named-parameters.util';
+import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
+
+describe('WorkspaceSelectQueryBuilderV2 timeline messaging parity', () => {
+ const buildShape = (
+ objectMetadataId: string,
+ nameSingular: string,
+ columnNames: string[],
+ relationShapeByFieldName: WorkspaceTableShape['relationShapeByFieldName'],
+ ): WorkspaceTableShape => ({
+ objectMetadataId,
+ nameSingular,
+ schemaName: SCHEMA_NAME,
+ tableName: nameSingular,
+ columnShapeByColumnName: Object.fromEntries(
+ columnNames.map((columnName) => [columnName, buildColumn(columnName)]),
+ ),
+ columnNames,
+ relationShapeByFieldName,
+ hasDeletedAtColumn: true,
+ });
+
+ const messageThreadShape = buildShape(
+ 'thread-id',
+ 'messageThread',
+ ['id', 'deletedAt'],
+ {
+ messages: {
+ fieldName: 'messages',
+ fieldMetadataId: 'field-thread-messages',
+ relationType: RelationType.ONE_TO_MANY,
+ targetObjectMetadataId: 'message-id',
+ targetFieldMetadataId: 'field-message-thread',
+ },
+ },
+ );
+
+ const messageShape = buildShape(
+ 'message-id',
+ 'message',
+ ['id', 'messageThreadId', 'receivedAt', 'deletedAt'],
+ {
+ messageThread: {
+ fieldName: 'messageThread',
+ fieldMetadataId: 'field-message-thread',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'thread-id',
+ targetFieldMetadataId: 'field-thread-messages',
+ joinColumnName: 'messageThreadId',
+ },
+ messageParticipants: {
+ fieldName: 'messageParticipants',
+ fieldMetadataId: 'field-message-participants',
+ relationType: RelationType.ONE_TO_MANY,
+ targetObjectMetadataId: 'participant-id',
+ targetFieldMetadataId: 'field-participant-message',
+ },
+ messageChannelMessageAssociations: {
+ fieldName: 'messageChannelMessageAssociations',
+ fieldMetadataId: 'field-message-associations',
+ relationType: RelationType.ONE_TO_MANY,
+ targetObjectMetadataId: 'association-id',
+ targetFieldMetadataId: 'field-association-message',
+ },
+ },
+ );
+
+ const messageParticipantShape = buildShape(
+ 'participant-id',
+ 'messageParticipant',
+ [
+ 'id',
+ 'messageId',
+ 'personId',
+ 'workspaceMemberId',
+ 'role',
+ 'handle',
+ 'deletedAt',
+ ],
+ {
+ message: {
+ fieldName: 'message',
+ fieldMetadataId: 'field-participant-message',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'message-id',
+ targetFieldMetadataId: 'field-message-participants',
+ joinColumnName: 'messageId',
+ },
+ person: {
+ fieldName: 'person',
+ fieldMetadataId: 'field-participant-person',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'timeline-person-id',
+ targetFieldMetadataId: null,
+ joinColumnName: 'personId',
+ },
+ workspaceMember: {
+ fieldName: 'workspaceMember',
+ fieldMetadataId: 'field-participant-member',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'member-id',
+ targetFieldMetadataId: null,
+ joinColumnName: 'workspaceMemberId',
+ },
+ },
+ );
+
+ const timelinePersonShape = buildShape(
+ 'timeline-person-id',
+ 'person',
+ ['id', 'nameFirstName', 'avatarUrl', 'deletedAt'],
+ {},
+ );
+
+ const workspaceMemberShape = buildShape(
+ 'member-id',
+ 'workspaceMember',
+ ['id', 'nameFirstName', 'avatarUrl', 'deletedAt'],
+ {},
+ );
+
+ const associationShape = buildShape(
+ 'association-id',
+ 'messageChannelMessageAssociation',
+ ['id', 'messageId', 'messageChannelId', 'deletedAt'],
+ {
+ message: {
+ fieldName: 'message',
+ fieldMetadataId: 'field-association-message',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'message-id',
+ targetFieldMetadataId: 'field-message-associations',
+ joinColumnName: 'messageId',
+ },
+ },
+ );
+
+ const shapeByObjectMetadataId: Record<string, WorkspaceTableShape> = {
+ 'thread-id': messageThreadShape,
+ 'message-id': messageShape,
+ 'participant-id': messageParticipantShape,
+ 'timeline-person-id': timelinePersonShape,
+ 'member-id': workspaceMemberShape,
+ 'association-id': associationShape,
+ };
+
+ const buildTimelineQueryBuilder = (
+ alias: string,
+ tableShape: WorkspaceTableShape,
+ rows: Record<string, unknown>[] = [],
+ ) => {
+ const executedStatements: CompiledStatement[] = [];
+
+ const queryBuilder = new WorkspaceSelectQueryBuilderV2(alias, {
+ tableShape,
+ executor: {
+ execute: async (statement) => {
+ executedStatements.push(statement);
+
+ return rows;
+ },
+ },
+ objectRecordsPermissions: {},
+ tableShapeByObjectMetadataId: (objectMetadataId) =>
+ shapeByObjectMetadataId[objectMetadataId],
+ onBeforeExecute: () => undefined,
+ formatResult: (records) => records as never,
+ });
+
+ return { queryBuilder, executedStatements };
+ };
+
+ it('should count threads across two to-many inner joins', async () => {
+ const { queryBuilder, executedStatements } = buildTimelineQueryBuilder(
+ 'messageThread',
+ messageThreadShape,
+ [{ count: '2' }],
+ );
+
+ const count = await queryBuilder
+ .innerJoin('messageThread.messages', 'messages')
+ .innerJoin('messages.messageParticipants', 'messageParticipants')
+ .where('messageParticipants.personId IN(:...personIds)', {
+ personIds: ['person-1'],
+ })
+ .groupBy('messageThread.id')
+ .getCount();
+
+ expect(count).toBe(2);
+
+ const sql = executedStatements[0].text;
+
+ expect(sql).toContain('SELECT COUNT(DISTINCT "messageThread"."id")');
+ expect(sql).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."message" AS "messages" ` +
+ 'ON ("messages"."messageThreadId" = "messageThread"."id")',
+ );
+ expect(sql).toContain(
+ `INNER JOIN "${SCHEMA_NAME}"."messageParticipant" AS "messageParticipants" ` +
+ 'ON ("messageParticipants"."messageId" = "messages"."id")',
+ );
+ expect(sql).toContain('"messageParticipants"."personId" IN($1)');
+ });
+
+ it('should page thread ids ordered by an aggregate select alias', async () => {
+ const { queryBuilder, executedStatements } = buildTimelineQueryBuilder(
+ 'messageThread',
+ messageThreadShape,
+ [{ id: 'thread-1', max_received_at: new Date('2026-01-01') }],
+ );
+
+ const rows = await queryBuilder
+ .select('messageThread.id', 'id')
+ .addSelect('MAX(messages.receivedAt)', 'max_received_at')
+ .innerJoin('messageThread.messages', 'messages')
+ .innerJoin('messages.messageParticipants', 'messageParticipants')
+ .where('messageParticipants.personId IN (:...personIds)', {
+ personIds: ['person-1'],
+ })
+ .groupBy('messageThread.id')
+ .orderBy('max_received_at', 'DESC')
+ .offset(3)
+ .limit(6)
+ .getRawMany();
+
+ expect(rows[0]).toEqual({
+ id: 'thread-1',
+ max_received_at: new Date('2026-01-01'),
+ });
+
+ const sql = executedStatements[0].text;
+
+ expect(sql).toContain(
+ 'SELECT "messageThread"."id" AS "id", ' +
+ 'MAX("messages"."receivedAt") AS "max_received_at"',
+ );
+ expect(sql).toContain('GROUP BY "messageThread"."id"');
+ expect(sql).toContain('ORDER BY "max_received_at" DESC');
+ expect(sql).toContain('LIMIT $');
+ expect(sql).toContain('OFFSET $');
+ });
+
+ it('should load distinct thread participants with hydrated relations', async () => {
+ const receivedAt = new Date('2026-02-02');
+ const { queryBuilder, executedStatements } = buildTimelineQueryBuilder(
+ 'messageParticipant',
+ messageParticipantShape,
+ [
+ {
+ messageParticipant_id: 'participant-1',
+ messageParticipant_messageId: 'message-1',
+ messageParticipant_personId: 'person-1',
+ messageParticipant_workspaceMemberId: null,
+ messageParticipant_role: 'from',
+ messageParticipant_handle: '[email protected]',
+ messageParticipant_deletedAt: null,
+ person_id: 'person-1',
+ person_nameFirstName: 'Ada',
+ person_avatarUrl: '',
+ person_deletedAt: null,
+ workspaceMember_id: null,
+ workspaceMember_nameFirstName: null,
+ workspaceMember_avatarUrl: null,
+ workspaceMember_deletedAt: null,
+ message_messageThreadId: 'thread-1',
+ message_receivedAt: receivedAt,
+ },
+ ],
+ );
+
+ const participants = await queryBuilder
+ .select('messageParticipant')
+ .addSelect('message.messageThreadId')
+ .addSelect('message.receivedAt')
+ .leftJoinAndSelect('messageParticipant.person', 'person')
+ .leftJoinAndSelect(
+ 'messageParticipant.workspaceMember',
+ 'workspaceMember',
+ )
+ .leftJoin('messageParticipant.message', 'message')
+ .where('message.messageThreadId = ANY(:messageThreadIds)', {
+ messageThreadIds: ['thread-1'],
+ })
+ .andWhere('messageParticipant.role = :role', { role: 'from' })
+ .orderBy('message.messageThreadId')
+ .distinctOn(['message.messageThreadId', 'messageParticipant.handle'])
+ .getMany();
+
+ expect(participants).toEqual([
+ {
+ id: 'participant-1',
+ messageId: 'message-1',
+ personId: 'person-1',
+ workspaceMemberId: null,
+ role: 'from',
+ handle: '[email protected]',
+ deletedAt: null,
+ person: {
+ id: 'person-1',
+ nameFirstName: 'Ada',
+ avatarUrl: '',
+ deletedAt: null,
+ },
+ workspaceMember: null,
+ message: { messageThreadId: 'thread-1', receivedAt },
+ },
+ ]);
+
+ const sql = executedStatements[0].text;
+
+ expect(sql).toContain(
+ 'SELECT DISTINCT ON ("message"."messageThreadId", "messageParticipant"."handle")',
+ );
+ expect(sql).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."person" AS "person" ` +
+ 'ON ("messageParticipant"."personId" = "person"."id")',
+ );
+ expect(sql).toContain('"message"."messageThreadId" = ANY($1)');
+ expect(sql).toContain('("messageParticipant"."role" = $2)');
+ expect(sql).toContain('ORDER BY "message"."messageThreadId" ASC');
+ });
+
+ it('should map thread visibility rows through raw to-many left joins', async () => {
+ const { queryBuilder, executedStatements } = buildTimelineQueryBuilder(
+ 'messageThread',
+ messageThreadShape,
+ [{ id: 'thread-1', messageChannelId: 'channel-1' }],
+ );
+
+ const rows = await queryBuilder
+ .select('messageThread.id', 'id')
+ .addSelect(
+ 'messageChannelMessageAssociation.messageChannelId',
+ 'messageChannelId',
+ )
+ .leftJoin('messageThread.messages', 'message')
+ .leftJoin(
+ 'message.messageChannelMessageAssociations',
+ 'messageChannelMessageAssociation',
+ )
+ .where('messageThread.id = ANY(:messageThreadIds)', {
+ messageThreadIds: ['thread-1'],
+ })
+ .getRawMany();
+
+ expect(rows).toEqual([{ id: 'thread-1', messageChannelId: 'channel-1' }]);
+
+ const sql = executedStatements[0].text;
+
+ expect(sql).toContain(
+ 'SELECT "messageThread"."id" AS "id", ' +
+ '"messageChannelMessageAssociation"."messageChannelId" AS "messageChannelId"',
+ );
+ expect(sql).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."message" AS "message" ` +
+ 'ON ("message"."messageThreadId" = "messageThread"."id")',
+ );
+ expect(sql).toContain(
+ `LEFT JOIN "${SCHEMA_NAME}"."messageChannelMessageAssociation" ` +
+ 'AS "messageChannelMessageAssociation" ' +
+ 'ON ("messageChannelMessageAssociation"."messageId" = "message"."id")',
+ );
+ expect(sql).toContain('"messageThread"."id" = ANY($1)');
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-where.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-where.spec.ts
new file mode 100644
index 00000000000..892673256c1
--- /dev/null
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-where.spec.ts
@@ -0,0 +1,207 @@
+import { And, Equal, In, LessThan, Not } from 'typeorm';
+
+import { TwentyOrmV2Exception } from 'src/engine/twenty-orm-v2/exceptions/twenty-orm-v2.exception';
+import { buildQueryBuilder } from 'src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2-test-shapes.util';
+
+describe('WorkspaceSelectQueryBuilderV2 where', () => {
+ it('should nest a bracketed condition built by the shared parsers', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.andWhere({
+ whereFactory: (nested) => {
+ nested.where('"person"."id" = :a', { a: 1 });
+ nested.orWhere('"person"."id" = :b', { b: 2 });
+ },
+ });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('(("person"."id" = $1) OR ("person"."id" = $2))');
+ expect(values).toEqual([1, 2]);
+ });
+
+ it('should negate a NotBrackets group instead of rendering it as a plain group', () => {
+ const { queryBuilder } = buildQueryBuilder();
+ const notBrackets = {
+ '@instanceof': Symbol.for('NotBrackets'),
+ whereFactory: (nested: { where: (sql: string) => unknown }) => {
+ nested.where('"person"."nameFirstName" = \'Ada\'');
+ },
+ };
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.andWhere(notBrackets);
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'NOT (("person"."nameFirstName" = \'Ada\'))',
+ );
+ });
+
+ it('should render a plain Brackets group without negation', () => {
+ const { queryBuilder } = buildQueryBuilder();
+ const brackets = {
+ '@instanceof': Symbol.for('Brackets'),
+ whereFactory: (nested: { where: (sql: string) => unknown }) => {
+ nested.where('"person"."nameFirstName" = \'Ada\'');
+ },
+ };
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.andWhere(brackets);
+
+ expect(queryBuilder.getQuery()).not.toContain('NOT (');
+ });
+
+ it('should keep the soft-delete predicate outside an OR chain', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.where('"person"."id" = :a', { a: 1 });
+ queryBuilder.orWhere('"person"."id" = :b', { b: 2 });
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'WHERE (("person"."id" = :a) OR ("person"."id" = :b)) AND "person"."deletedAt" IS NULL',
+ );
+ });
+
+ it('should quote bare alias.column references in raw where SQL', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.where('company.name = :name', { name: 'Acme' });
+ queryBuilder.andWhere('person.id IN (:...ids)', { ids: ['a'] });
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain('("company"."name" = :name)');
+ expect(sql).toContain('("person"."id" IN (:...ids))');
+ });
+
+ it('should not quote alias.column text inside string literals', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.leftJoin('person.company', 'company');
+ queryBuilder.where('"person"."nameFirstName" = \'company.name\'');
+
+ expect(queryBuilder.getQuery()).toContain(
+ '"person"."nameFirstName" = \'company.name\'',
+ );
+ });
+
+ it('should refuse a caller parameter that collides with a reserved name', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ expect(() =>
+ queryBuilder.andWhere('"person"."id" = :ormV2Limit', { ormV2Limit: 1 }),
+ ).toThrow(TwentyOrmV2Exception);
+ });
+
+ it('should render an object-literal where with In as a bound IN clause', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .where({ id: In(['a', 'b']) });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('"person"."id" IN ($1, $2)');
+ expect(values).toEqual(['a', 'b']);
+ });
+
+ it('should allocate unique parameter names across object-literal where clauses', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .where({ id: In(['a']) })
+ .andWhere({ companyId: In(['b']) });
+
+ const [, values] = queryBuilder.getQueryAndParameters();
+
+ expect(values).toEqual(['a', 'b']);
+ });
+
+ it('should reject an object-literal where on an unknown column', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ expect(() => queryBuilder.where({ missing: In(['x']) })).toThrow(
+ TwentyOrmV2Exception,
+ );
+ });
+
+ it('should render comparison operators in an object-literal where', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.where({ nameFirstName: LessThan('x') });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('"person"."nameFirstName" < $1');
+ expect(values).toContain('x');
+ });
+
+ it('should combine operators with And', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.where({ nameFirstName: And(LessThan('z'), Not(Equal('a'))) });
+
+ const [text] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain(
+ '("person"."nameFirstName" < $1 AND NOT ("person"."nameFirstName" = $2))',
+ );
+ });
+
+ it('should negate a nested operator with Not', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder.setFindOptions({ select: { id: true } });
+ queryBuilder.where({ id: Not(In(['a', 'b'])) });
+
+ const [text] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('NOT ("person"."id" IN ($1, $2))');
+ });
+
+ it('should treat a plain value in an object-literal where as equality', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .where({ companyId: 'company-1' });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('("person"."companyId" = $1)');
+ expect(values).toEqual(['company-1']);
+ });
+
+ it('should treat the Equal operator in an object-literal where as equality', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .where({ companyId: Equal('company-1') });
+
+ const [text, values] = queryBuilder.getQueryAndParameters();
+
+ expect(text).toContain('("person"."companyId" = $1)');
+ expect(values).toEqual(['company-1']);
+ });
+
+ it('should treat a null value in an object-literal where as IS NULL', () => {
+ const { queryBuilder } = buildQueryBuilder();
+
+ queryBuilder
+ .setFindOptions({ select: { id: true } })
+ .where({ companyId: null });
+
+ expect(queryBuilder.getQuery()).toContain('("person"."companyId" IS NULL)');
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2.spec.ts
deleted file mode 100644
index 9d9411efe73..00000000000
--- a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/__tests__/workspace-select-query-builder-v2.spec.ts
+++ /dev/null
@@ -1,770 +0,0 @@
-import { FieldMetadataType } from 'twenty-shared/types';
-import { And, Equal, In, LessThan, Not } from 'typeorm';
-
-import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
-import { type CompiledStatement } from 'src/engine/twenty-orm-v2/sql/utils/compile-named-parameters.util';
-import { TwentyOrmV2Exception } from 'src/engine/twenty-orm-v2/exceptions/twenty-orm-v2.exception';
-import { WorkspaceSelectQueryBuilderV2 } from 'src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2';
-import { buildColumnResultAlias } from 'src/engine/twenty-orm-v2/sql/utils/build-column-result-alias.util';
-import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
-
-const SCHEMA_NAME = 'workspace_1wgvd1injqtife6y4rvfbu3h5';
-
-const buildColumn = (
- columnName: string,
- compositeParentFieldName?: string,
-) => ({
- columnName,
- fieldMetadataId: `field-${columnName}`,
- fieldName: compositeParentFieldName ?? columnName,
- fieldMetadataType: FieldMetadataType.TEXT,
- ...(compositeParentFieldName !== undefined
- ? { compositeParentFieldName }
- : {}),
-});
-
-const companyTableShape: WorkspaceTableShape = {
- objectMetadataId: 'company-object-id',
- nameSingular: 'company',
- schemaName: SCHEMA_NAME,
- tableName: 'company',
- columnShapeByColumnName: {
- id: buildColumn('id'),
- name: buildColumn('name'),
- deletedAt: buildColumn('deletedAt'),
- },
- columnNames: ['id', 'name', 'deletedAt'],
- relationShapeByFieldName: {
- person: {
- fieldName: 'person',
- fieldMetadataId: 'field-company',
- relationType: RelationType.MANY_TO_ONE,
- targetObjectMetadataId: 'person-object-id',
- targetFieldMetadataId: 'field-people',
- joinColumnName: 'personId',
- },
- },
- hasDeletedAtColumn: true,
-};
-
-const personTableShape: WorkspaceTableShape = {
- objectMetadataId: 'person-object-id',
- nameSingular: 'person',
- schemaName: SCHEMA_NAME,
- tableName: 'person',
- columnShapeByColumnName: {
- id: buildColumn('id'),
- nameFirstName: buildColumn('nameFirstName', 'name'),
- nameLastName: buildColumn('nameLastName', 'name'),
- companyId: buildColumn('companyId'),
- deletedAt: buildColumn('deletedAt'),
- },
- columnNames: [
- 'id',
- 'nameFirstName',
- 'nameLastName',
- 'companyId',
- 'deletedAt',
- ],
- relationShapeByFieldName: {
- company: {
- fieldName: 'company',
- fieldMetadataId: 'field-company',
- relationType: RelationType.MANY_TO_ONE,
- targetObjectMetadataId: 'company-object-id',
- targetFieldMetadataId: 'field-people',
- joinColumnName: 'companyId',
- },
- people: {
- fieldName: 'people',
- fieldMetadataId: 'field-people',
- relationType: RelationType.ONE_TO_MANY,
- targetObjectMetadataId: 'company-object-id',
- targetFieldMetadataId: 'field-company',
- },
- },
- hasDeletedAtColumn: true,
-};
-
-const buildQueryBuilder = ({
- rows = [],
- tableShape = personTableShape,
-}: {
- rows?: Record<string, unknown>[];
- tableShape?: WorkspaceTableShape;
-} = {}) => {
- const executedStatements: CompiledStatement[] = [];
-
- const queryBuilder = new WorkspaceSelectQueryBuilderV2('person', {
- tableShape,
- executor: {
- execute: async (statement) => {
- executedStatements.push(statement);
-
- return rows;
- },
- },
- objectRecordsPermissions: {},
- tableShapeByObjectMetadataId: () => companyTableShape,
- onBeforeExecute: () => undefined,
- formatResult: (records) => records as never,
- });
-
- return { queryBuilder, executedStatements };
-};
-
-describe('WorkspaceSelectQueryBuilderV2', () => {
- it('should select the requested columns with alias-prefixed output names', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({
- select: { id: true, nameFirstName: true },
- });
-
- expect(queryBuilder.getQuery()).toBe(
- 'SELECT "person"."id" AS "person_id", "person"."nameFirstName" AS "person_nameFirstName" ' +
- `FROM "${SCHEMA_NAME}"."person" AS "person" ` +
- 'WHERE "person"."deletedAt" IS NULL',
- );
- });
-
- it('should compile named parameters coming from the shared filter parser', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .andWhere('"person"."nameFirstName" ILIKE :name7f3a', {
- name7f3a: '%ac%',
- });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('"person"."nameFirstName" ILIKE $1');
- expect(values).toEqual(['%ac%']);
- });
-
- it('should add the soft-delete predicate unless withDeleted was called', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- expect(queryBuilder.getQuery()).toContain('"person"."deletedAt" IS NULL');
-
- queryBuilder.withDeleted();
- expect(queryBuilder.getQuery()).not.toContain(
- '"person"."deletedAt" IS NULL',
- );
- });
-
- it('should join a to-one relation on its foreign key', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
-
- expect(queryBuilder.getQuery()).toContain(
- `LEFT JOIN "${SCHEMA_NAME}"."company" AS "company" ` +
- 'ON ("person"."companyId" = "company"."id")',
- );
- });
-
- it('should report a to-many join to the shared guard rather than rendering it', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.people', 'people');
-
- expect(queryBuilder.expressionMap.joinAttributes).toEqual([
- {
- alias: { name: 'people' },
- relation: { isOneToMany: true, isManyToMany: false },
- },
- ]);
- expect(() => queryBuilder.getQuery()).toThrow(TwentyOrmV2Exception);
- });
-
- it('should render a deduped to-many join on the inverse foreign key when the caller opts in', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.people', 'people', undefined, {
- allowToManyJoin: true,
- });
-
- expect(queryBuilder.getQuery()).toContain(
- `LEFT JOIN (SELECT DISTINCT ON ("personId") * ` +
- `FROM "${SCHEMA_NAME}"."company" ` +
- `WHERE "deletedAt" IS NULL ` +
- `ORDER BY "personId", "id") AS "people" ` +
- 'ON ("people"."personId" = "person"."id")',
- );
- });
-
- it('should emit a plain parameterised LIMIT, with no distinct sub-select', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.limit(31);
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('LIMIT $');
- expect(text).not.toContain('DISTINCT');
- expect(values).toContain(31);
- });
-
- it('should reuse one statement shape across page sizes and offsets', () => {
- const buildPaginatedQuery = (limit: number, offset: number) => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.limit(limit);
- queryBuilder.offset(offset);
-
- return queryBuilder.getQueryAndParameters();
- };
-
- const [firstText, firstValues] = buildPaginatedQuery(30, 0);
- const [secondText, secondValues] = buildPaginatedQuery(60, 120);
-
- expect(firstText).toBe(secondText);
- expect(firstValues).toEqual([30, 0]);
- expect(secondValues).toEqual([60, 120]);
- });
-
- it('should render order by with direction and nulls handling', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.orderBy({
- 'person.nameFirstName': { order: 'DESC', nulls: 'NULLS LAST' },
- 'company.name': { order: 'ASC' },
- });
-
- expect(queryBuilder.getQuery()).toContain(
- 'ORDER BY "person"."nameFirstName" DESC NULLS LAST, "company"."name" ASC',
- );
- });
-
- it('should reject a column that does not exist on the object', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { nonExistent: true } });
-
- expect(() => queryBuilder.getQuery()).toThrow(TwentyOrmV2Exception);
- });
-
- it('should nest a bracketed condition built by the shared parsers', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.andWhere({
- whereFactory: (nested) => {
- nested.where('"person"."id" = :a', { a: 1 });
- nested.orWhere('"person"."id" = :b', { b: 2 });
- },
- });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('(("person"."id" = $1) OR ("person"."id" = $2))');
- expect(values).toEqual([1, 2]);
- });
-
- it('should negate a NotBrackets group instead of rendering it as a plain group', () => {
- const { queryBuilder } = buildQueryBuilder();
- const notBrackets = {
- '@instanceof': Symbol.for('NotBrackets'),
- whereFactory: (nested: { where: (sql: string) => unknown }) => {
- nested.where('"person"."nameFirstName" = \'Ada\'');
- },
- };
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.andWhere(notBrackets);
-
- expect(queryBuilder.getQuery()).toContain(
- 'NOT (("person"."nameFirstName" = \'Ada\'))',
- );
- });
-
- it('should render a plain Brackets group without negation', () => {
- const { queryBuilder } = buildQueryBuilder();
- const brackets = {
- '@instanceof': Symbol.for('Brackets'),
- whereFactory: (nested: { where: (sql: string) => unknown }) => {
- nested.where('"person"."nameFirstName" = \'Ada\'');
- },
- };
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.andWhere(brackets);
-
- expect(queryBuilder.getQuery()).not.toContain('NOT (');
- });
-
- it('should select only aggregate expressions after select([])', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true, nameFirstName: true } });
- queryBuilder.select([]);
- queryBuilder.addSelect('COUNT("person"."id")', 'totalCount');
-
- const sql = queryBuilder.getQuery();
-
- expect(sql).toContain('SELECT COUNT("person"."id") AS "totalCount" FROM');
- expect(sql).not.toContain('"person"."nameFirstName"');
- });
-
- it('should count columns referenced only inside aggregates as selected', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.select([]);
- queryBuilder.addSelect('MAX("person"."nameFirstName")', 'maxFirstName');
-
- expect(queryBuilder.getSelectedColumnNames()).toContain('nameFirstName');
- });
-
- it('should keep an explicit empty selection on a clone', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.select([]);
- queryBuilder.addSelect('COUNT("person"."id")', 'totalCount');
-
- expect(queryBuilder.clone().getQuery()).not.toContain('"person"."id" AS');
- });
-
- it('should carry applied row-level markers onto a clone', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
- expect(queryBuilder.clone().markRowLevelPermissionApplied('person')).toBe(
- false,
- );
- });
-
- it('should clear applied row-level markers when where() replaces the WHERE', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.markRowLevelPermissionApplied('person');
- queryBuilder.where('"person"."id" = :id', { id: 1 });
-
- expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
- });
-
- it('should report joined-alias columns referenced only in order by', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.orderBy({ 'company.name': { order: 'ASC' } });
-
- const columnNamesByAlias = queryBuilder.getReferencedColumnNamesByAlias();
-
- expect(columnNamesByAlias['person']).toEqual(['id']);
- expect(columnNamesByAlias['company']).toEqual(['name']);
- });
-
- it('should report joined-alias columns referenced only in an added select', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.addSelect('"company"."name"', 'company_name');
-
- expect(queryBuilder.getReferencedColumnNamesByAlias()['company']).toEqual([
- 'name',
- ]);
- });
-
- it('should keep the soft-delete predicate outside an OR chain', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.where('"person"."id" = :a', { a: 1 });
- queryBuilder.orWhere('"person"."id" = :b', { b: 2 });
-
- expect(queryBuilder.getQuery()).toContain(
- 'WHERE (("person"."id" = :a) OR ("person"."id" = :b)) AND "person"."deletedAt" IS NULL',
- );
- });
-
- it('should filter soft-deleted joined rows in the ON clause', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
-
- const sql = queryBuilder.getQuery();
-
- expect(sql).toContain('("company"."deletedAt" IS NULL)');
- expect(sql.indexOf('"company"."deletedAt"')).toBeLessThan(
- sql.indexOf('WHERE'),
- );
- });
-
- it('should add a joined predicate to ON rather than WHERE', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.addJoinCondition('company', '"company"."name" = :owner');
- queryBuilder.setParameters({ owner: 'Acme' });
-
- const sql = queryBuilder.getQuery();
-
- expect(sql).toContain('("company"."name" = :owner)');
- expect(sql.indexOf('"company"."name"')).toBeLessThan(sql.indexOf('WHERE'));
- });
-
- it('should restore the previous limit when getOne fails', async () => {
- const failingBuilder = new WorkspaceSelectQueryBuilderV2('person', {
- tableShape: personTableShape,
- executor: {
- execute: async () => {
- throw new Error('connection lost');
- },
- },
- objectRecordsPermissions: {},
- tableShapeByObjectMetadataId: () => companyTableShape,
- onBeforeExecute: () => undefined,
- formatResult: (records) => records as never,
- });
-
- failingBuilder.setFindOptions({ select: { id: true } });
- failingBuilder.limit(25);
-
- await expect(failingBuilder.getOne()).rejects.toThrow('connection lost');
- expect(failingBuilder.getQueryAndParameters()[1]).toEqual([25]);
- });
-
- it('should not attribute a joined column to the main alias', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.addSelect('"company"."name"', 'company_name');
-
- const columnNamesByAlias = queryBuilder.getReferencedColumnNamesByAlias();
-
- expect(columnNamesByAlias['person']).toEqual(['id']);
- expect(columnNamesByAlias['company']).toEqual(['name']);
- });
-
- it('should keep joined row-level markers when where() replaces the WHERE', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.leftJoin('person.company', 'company');
- queryBuilder.markRowLevelPermissionApplied('person');
- queryBuilder.markRowLevelPermissionApplied('company');
- queryBuilder.where('"person"."id" = :id', { id: 1 });
-
- expect(queryBuilder.markRowLevelPermissionApplied('person')).toBe(true);
- expect(queryBuilder.markRowLevelPermissionApplied('company')).toBe(false);
- });
-
- it('should strip the alias prefix when mapping rows back to records', async () => {
- const { queryBuilder } = buildQueryBuilder({
- rows: [
- {
- person_id: 'person-1',
- person_nameFirstName: 'Ada',
- company_name: 'Acme',
- },
- ],
- });
-
- queryBuilder.setFindOptions({ select: { id: true, nameFirstName: true } });
-
- const records = await queryBuilder.getMany();
-
- expect(records).toEqual([{ id: 'person-1', nameFirstName: 'Ada' }]);
- });
-
- it('should not widen the projection when a relation is joined', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.leftJoin('person.company', 'company');
-
- expect(queryBuilder.getQuery()).toContain(
- 'SELECT "person"."id" AS "person_id" FROM',
- );
- });
-
- it('should keep result aliases within the postgres identifier limit', async () => {
- const longColumnName = `veryLongSubfield${'X'.repeat(60)}`;
- const otherLongColumnName = `veryLongSubfield${'Y'.repeat(60)}`;
- const resultAlias = buildColumnResultAlias('person', longColumnName);
- const otherResultAlias = buildColumnResultAlias(
- 'person',
- otherLongColumnName,
- );
-
- const { queryBuilder } = buildQueryBuilder({
- tableShape: {
- ...personTableShape,
- columnShapeByColumnName: {
- ...personTableShape.columnShapeByColumnName,
- [longColumnName]: buildColumn(longColumnName),
- [otherLongColumnName]: buildColumn(otherLongColumnName),
- },
- },
- rows: [{ [resultAlias]: 'left', [otherResultAlias]: 'right' }],
- });
-
- queryBuilder.setFindOptions({
- select: { [longColumnName]: true, [otherLongColumnName]: true },
- });
-
- expect(resultAlias.length).toBeLessThanOrEqual(63);
- expect(resultAlias).not.toBe(otherResultAlias);
- expect(queryBuilder.getQuery()).toContain(
- `"person"."${longColumnName}" AS "${resultAlias}"`,
- );
- await expect(queryBuilder.getMany()).resolves.toEqual([
- { [longColumnName]: 'left', [otherLongColumnName]: 'right' },
- ]);
- });
-
- it('should refuse a caller parameter that collides with a reserved name', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- expect(() =>
- queryBuilder.andWhere('"person"."id" = :ormV2Limit', { ormV2Limit: 1 }),
- ).toThrow(TwentyOrmV2Exception);
- });
-
- it('should report the columns it selected so permissions do not have to parse SQL', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true, companyId: true } });
-
- expect(queryBuilder.getSelectedColumnNames()).toEqual(['id', 'companyId']);
- });
-
- it('should keep a clone independent of the builder it came from', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
-
- const clone = queryBuilder.clone();
-
- clone.andWhere('"person"."id" = :id', { id: 1 });
-
- expect(queryBuilder.getQuery()).not.toContain('"person"."id" = :id');
- expect(clone.getQuery()).toContain('"person"."id" = :id');
- });
-
- it('should send one parameterised statement to the executor', async () => {
- const { queryBuilder, executedStatements } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .andWhere('"person"."nameFirstName" = :name', { name: 'Ada' });
-
- await queryBuilder.getMany();
-
- expect(executedStatements).toHaveLength(1);
- expect(executedStatements[0].text).toContain('$1');
- expect(executedStatements[0].values).toEqual(['Ada']);
- });
-
- it('should map take and skip to LIMIT and OFFSET parameters', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .take(31)
- .skip(60);
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('LIMIT $1');
- expect(text).toContain('OFFSET $2');
- expect(values).toEqual([31, 60]);
- });
-
- it('should build a COUNT statement that ignores projection, order and pagination', async () => {
- const { queryBuilder, executedStatements } = buildQueryBuilder({
- rows: [{ count: '7' }],
- });
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .orderBy('"person"."id"', 'ASC')
- .take(10);
-
- const count = await queryBuilder.getCount();
-
- expect(count).toBe(7);
- expect(executedStatements[0].text).toBe(
- 'SELECT COUNT(1) AS "count" ' +
- `FROM "${SCHEMA_NAME}"."person" AS "person" ` +
- 'WHERE "person"."deletedAt" IS NULL',
- );
- });
-
- it('should render an object-literal where with In as a bound IN clause', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .where({ id: In(['a', 'b']) });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('"person"."id" IN ($1, $2)');
- expect(values).toEqual(['a', 'b']);
- });
-
- it('should allocate unique parameter names across object-literal where clauses', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .where({ id: In(['a']) })
- .andWhere({ companyId: In(['b']) });
-
- const [, values] = queryBuilder.getQueryAndParameters();
-
- expect(values).toEqual(['a', 'b']);
- });
-
- it('should reject an object-literal where on an unknown column', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- expect(() => queryBuilder.where({ missing: In(['x']) })).toThrow(
- TwentyOrmV2Exception,
- );
- });
-
- it('should render comparison operators in an object-literal where', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.where({ nameFirstName: LessThan('x') });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('"person"."nameFirstName" < $1');
- expect(values).toContain('x');
- });
-
- it('should combine operators with And', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.where({ nameFirstName: And(LessThan('z'), Not(Equal('a'))) });
-
- const [text] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain(
- '("person"."nameFirstName" < $1 AND NOT ("person"."nameFirstName" = $2))',
- );
- });
-
- it('should negate a nested operator with Not', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder.setFindOptions({ select: { id: true } });
- queryBuilder.where({ id: Not(In(['a', 'b'])) });
-
- const [text] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('NOT ("person"."id" IN ($1, $2))');
- });
-
- it('should treat a plain value in an object-literal where as equality', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .where({ companyId: 'company-1' });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('("person"."companyId" = $1)');
- expect(values).toEqual(['company-1']);
- });
-
- it('should treat the Equal operator in an object-literal where as equality', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .where({ companyId: Equal('company-1') });
-
- const [text, values] = queryBuilder.getQueryAndParameters();
-
- expect(text).toContain('("person"."companyId" = $1)');
- expect(values).toEqual(['company-1']);
- });
-
- it('should treat a null value in an object-literal where as IS NULL', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .setFindOptions({ select: { id: true } })
- .where({ companyId: null });
-
- expect(queryBuilder.getQuery()).toContain('("person"."companyId" IS NULL)');
- });
-
- it('should emit a GROUP BY with aggregate and grouped columns', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .select([])
- .addSelect('COUNT(*)', 'totalCount')
- .addSelect('"person"."companyId"', 'companyId')
- .groupBy('"person"."companyId"');
-
- expect(queryBuilder.getQuery()).toBe(
- 'SELECT COUNT(*) AS "totalCount", "person"."companyId" AS "companyId" ' +
- `FROM "${SCHEMA_NAME}"."person" AS "person" ` +
- 'WHERE "person"."deletedAt" IS NULL ' +
- 'GROUP BY "person"."companyId"',
- );
- });
-
- it('should append additional GROUP BY expressions with addGroupBy', () => {
- const { queryBuilder } = buildQueryBuilder();
-
- queryBuilder
- .select([])
- .addSelect('COUNT(*)', 'totalCount')
- .groupBy('"person"."companyId"')
- .addGroupBy('"person"."nameFirstName"');
-
- expect(queryBuilder.getQuery()).toContain(
- 'GROUP BY "person"."companyId", "person"."nameFirstName"',
- );
- });
-
- it('should return every raw row from getRawMany', async () => {
- const { queryBuilder, executedStatements } = buildQueryBuilder({
- rows: [
- { companyId: 'c1', totalCount: '3' },
- { companyId: 'c2', totalCount: '1' },
- ],
- });
-
- queryBuilder
- .select([])
- .addSelect('COUNT(*)', 'totalCount')
- .addSelect('"person"."companyId"', 'companyId')
- .groupBy('"person"."companyId"');
-
- const rows = await queryBuilder.getRawMany();
-
- expect(rows).toEqual([
- { companyId: 'c1', totalCount: '3' },
- { companyId: 'c2', totalCount: '1' },
- ]);
- expect(executedStatements[0].text).toContain(
- 'GROUP BY "person"."companyId"',
- );
- });
-});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/__tests__/apply-find-options.util.spec.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/__tests__/apply-find-options.util.spec.ts
index 26425a0454d..a506f254cae 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/__tests__/apply-find-options.util.spec.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/__tests__/apply-find-options.util.spec.ts
@@ -1,7 +1,13 @@
import { FieldMetadataType } from 'twenty-shared/types';
import { In } from 'typeorm';
-import { applyFindOptionsToQueryBuilder } from 'src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util';
+import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
+
+import {
+ applyFindOptionsToQueryBuilder,
+ normalizeFindOptionsRelations,
+ splitFindOptionsOrder,
+} from 'src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util';
import { WorkspaceSelectQueryBuilderV2 } from 'src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2';
import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
@@ -142,3 +148,128 @@ describe('applyFindOptionsToQueryBuilder', () => {
expect(queryBuilder.getQuery()).toContain('"person"."id" =');
});
});
+
+const messageShape: WorkspaceTableShape = {
+ objectMetadataId: 'message-object-id',
+ nameSingular: 'message',
+ schemaName: SCHEMA_NAME,
+ tableName: 'message',
+ columnShapeByColumnName: {
+ id: buildColumn('id'),
+ threadId: buildColumn('threadId'),
+ receivedAt: buildColumn('receivedAt'),
+ deletedAt: buildColumn('deletedAt'),
+ },
+ columnNames: ['id', 'threadId', 'receivedAt', 'deletedAt'],
+ relationShapeByFieldName: {
+ thread: {
+ fieldName: 'thread',
+ fieldMetadataId: 'field-message-thread',
+ relationType: RelationType.MANY_TO_ONE,
+ targetObjectMetadataId: 'thread-object-id',
+ targetFieldMetadataId: 'field-thread-messages',
+ joinColumnName: 'threadId',
+ },
+ },
+ hasDeletedAtColumn: true,
+};
+
+const threadShape: WorkspaceTableShape = {
+ objectMetadataId: 'thread-object-id',
+ nameSingular: 'thread',
+ schemaName: SCHEMA_NAME,
+ tableName: 'thread',
+ columnShapeByColumnName: {
+ id: buildColumn('id'),
+ subject: buildColumn('subject'),
+ deletedAt: buildColumn('deletedAt'),
+ },
+ columnNames: ['id', 'subject', 'deletedAt'],
+ relationShapeByFieldName: {
+ messages: {
+ fieldName: 'messages',
+ fieldMetadataId: 'field-thread-messages',
+ relationType: RelationType.ONE_TO_MANY,
+ targetObjectMetadataId: 'message-object-id',
+ targetFieldMetadataId: 'field-message-thread',
+ },
+ },
+ hasDeletedAtColumn: true,
+};
+
+const buildThreadQueryBuilder = () =>
+ new WorkspaceSelectQueryBuilderV2('thread', {
+ tableShape: threadShape,
+ executor: { execute: async () => [] },
+ objectRecordsPermissions: {},
+ tableShapeByObjectMetadataId: () => messageShape,
+ onBeforeExecute: () => undefined,
+ formatResult: (records) => records as never,
+ });
+
+describe('applyFindOptionsToQueryBuilder relation order', () => {
+ it('orders parents through a deduped to-many join whose representative follows the order', () => {
+ const queryBuilder = applyFindOptionsToQueryBuilder(
+ buildThreadQueryBuilder(),
+ { order: { messages: { receivedAt: 'DESC' } } },
+ );
+
+ const sql = queryBuilder.getQuery();
+
+ expect(sql).toContain(
+ `LEFT JOIN (SELECT DISTINCT ON ("threadId") * ` +
+ `FROM "${SCHEMA_NAME}"."message" ` +
+ `WHERE "deletedAt" IS NULL ` +
+ `ORDER BY "threadId", "receivedAt" DESC, "id") AS "messages" ` +
+ 'ON ("messages"."threadId" = "thread"."id")',
+ );
+ expect(sql).toContain('ORDER BY "messages"."receivedAt" DESC');
+ });
+
+ it('keeps mixed column and relation order entries in their original sequence', () => {
+ const queryBuilder = applyFindOptionsToQueryBuilder(
+ buildThreadQueryBuilder(),
+ {
+ order: {
+ messages: { receivedAt: { order: 'DESC', nulls: 'NULLS LAST' } },
+ subject: 'ASC',
+ },
+ },
+ );
+
+ expect(queryBuilder.getQuery()).toContain(
+ 'ORDER BY "messages"."receivedAt" DESC NULLS LAST, "thread"."subject" ASC',
+ );
+ });
+});
+
+describe('splitFindOptionsOrder', () => {
+ it('splits relation order entries from column order entries', () => {
+ const { columnOrder, orderByRelationFieldName } = splitFindOptionsOrder(
+ threadShape,
+ { subject: 'ASC', messages: { receivedAt: 'DESC' } },
+ );
+
+ expect(columnOrder).toEqual({ subject: 'ASC' });
+ expect(orderByRelationFieldName).toEqual({
+ messages: { receivedAt: 'DESC' },
+ });
+ });
+});
+
+describe('normalizeFindOptionsRelations', () => {
+ it('keeps an object form untouched', () => {
+ expect(normalizeFindOptionsRelations({ messages: true })).toEqual({
+ messages: true,
+ });
+ });
+
+ it('normalises an array of dotted relation paths into nested objects', () => {
+ expect(
+ normalizeFindOptionsRelations([
+ 'messages',
+ 'messages.messageParticipants',
+ ]),
+ ).toEqual({ messages: { messageParticipants: {} } });
+ });
+});
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util.ts
index 79a59037728..b18997d4a08 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util.ts
@@ -5,7 +5,10 @@ import {
type FindOptionsSelectLike,
type ObjectWhereLike,
type OrderByConditionLike,
+ type OrderByValueLike,
} from 'src/engine/twenty-orm-v2/query-builder/types/query-builder-v2.type';
+import { type ToManyDedupOrder } from 'src/engine/twenty-orm-v2/sql/utils/build-select-statement.util';
+import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
export type FindOptionsRelationsV2 = {
[relationFieldName: string]: boolean | FindOptionsRelationsV2;
@@ -13,14 +16,42 @@ export type FindOptionsRelationsV2 = {
export type FindOptionsSelectV2 = FindOptionsSelectLike | string[];
+export type FindOptionsOrderV2 = {
+ [key: string]: OrderByValueLike | OrderByConditionLike;
+};
+
export type FindOptionsV2 = {
where?: ObjectWhereLike | ObjectWhereLike[];
select?: FindOptionsSelectV2;
- order?: OrderByConditionLike;
+ order?: FindOptionsOrderV2;
take?: number;
skip?: number;
withDeleted?: boolean;
- relations?: FindOptionsRelationsV2;
+ relations?: FindOptionsRelationsV2 | string[];
+};
+
+export const normalizeFindOptionsRelations = (
+ relations: FindOptionsRelationsV2 | string[],
+): FindOptionsRelationsV2 => {
+ if (!Array.isArray(relations)) {
+ return relations;
+ }
+
+ const normalized: FindOptionsRelationsV2 = {};
+
+ for (const relationPath of relations) {
+ let currentLevel = normalized;
+
+ for (const fieldName of relationPath.split('.')) {
+ const existing = currentLevel[fieldName];
+ const nested = typeof existing === 'object' ? existing : {};
+
+ currentLevel[fieldName] = nested;
+ currentLevel = nested;
+ }
+ }
+
+ return normalized;
};
const normalizeSelect = (select: FindOptionsSelectV2): FindOptionsSelectLike =>
@@ -57,6 +88,102 @@ const applyWhere = (
});
};
+const isRelationOrderValue = (value: unknown): value is OrderByConditionLike =>
+ isDefined(value) &&
+ typeof value === 'object' &&
+ !('order' in value) &&
+ !('nulls' in value);
+
+const isRelationOrderEntry = (
+ tableShape: WorkspaceTableShape,
+ key: string,
+ value: OrderByValueLike | OrderByConditionLike,
+): value is OrderByConditionLike =>
+ isDefined(tableShape.relationShapeByFieldName[key]) &&
+ isRelationOrderValue(value);
+
+export const splitFindOptionsOrder = (
+ tableShape: WorkspaceTableShape,
+ order?: FindOptionsOrderV2,
+): {
+ columnOrder: OrderByConditionLike;
+ orderByRelationFieldName: Record<string, OrderByConditionLike>;
+} => {
+ const columnOrder: OrderByConditionLike = {};
+ const orderByRelationFieldName: Record<string, OrderByConditionLike> = {};
+
+ for (const [key, value] of Object.entries(order ?? {})) {
+ if (isRelationOrderEntry(tableShape, key, value)) {
+ orderByRelationFieldName[key] = value;
+ continue;
+ }
+
+ columnOrder[key] = value as OrderByValueLike;
+ }
+
+ return { columnOrder, orderByRelationFieldName };
+};
+
+const toDedupOrder = (order: OrderByConditionLike): ToManyDedupOrder[] =>
+ Object.entries(order).map(([columnName, value]) => ({
+ columnName,
+ direction: typeof value === 'string' ? value : (value.order ?? 'ASC'),
+ nulls: typeof value === 'string' ? undefined : value.nulls,
+ }));
+
+const applyRelationOrderEntry = (
+ queryBuilder: WorkspaceSelectQueryBuilderV2,
+ relationFieldName: string,
+ relationOrder: OrderByConditionLike,
+): void => {
+ queryBuilder.leftJoin(
+ `${queryBuilder.alias}.${relationFieldName}`,
+ relationFieldName,
+ undefined,
+ {
+ allowToManyJoin: true,
+ toManyDedupOrder: toDedupOrder(relationOrder),
+ },
+ );
+
+ for (const [columnName, value] of Object.entries(relationOrder)) {
+ if (typeof value === 'string') {
+ queryBuilder.addOrderBy(`${relationFieldName}.${columnName}`, value);
+ continue;
+ }
+
+ queryBuilder.addOrderBy(
+ `${relationFieldName}.${columnName}`,
+ value.order ?? 'ASC',
+ value.nulls,
+ );
+ }
+};
+
+const applyOrder = (
+ queryBuilder: WorkspaceSelectQueryBuilderV2,
+ order: FindOptionsOrderV2,
+): void => {
+ for (const [key, value] of Object.entries(order)) {
+ if (isRelationOrderEntry(queryBuilder.tableShape, key, value)) {
+ applyRelationOrderEntry(queryBuilder, key, value);
+ continue;
+ }
+
+ queryBuilder.addOrderBy(
+ ...toOrderByArguments(key, value as OrderByValueLike),
+ );
+ }
+};
+
+const toOrderByArguments = (
+ key: string,
+ value: OrderByValueLike,
+): [string, 'ASC' | 'DESC', ('NULLS FIRST' | 'NULLS LAST')?] =>
+ typeof value === 'string'
+ ? [key, value, undefined]
+ : [key, value.order ?? 'ASC', value.nulls];
+
export const applyFindOptionsToQueryBuilder = (
queryBuilder: WorkspaceSelectQueryBuilderV2,
options?: FindOptionsV2,
@@ -78,7 +205,7 @@ export const applyFindOptionsToQueryBuilder = (
}
if (isDefined(options.order)) {
- queryBuilder.orderBy(options.order);
+ applyOrder(queryBuilder, options.order);
}
if (isDefined(options.take)) {
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2.ts b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2.ts
index 8f707c6e29a..3250d9c52d6 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/query-builder/workspace-select-query-builder-v2.ts
@@ -27,15 +27,19 @@ import { collectReferencedColumnNames } from 'src/engine/twenty-orm-v2/sql/utils
import { compileNamedParameters } from 'src/engine/twenty-orm-v2/sql/utils/compile-named-parameters.util';
import {
RESERVED_PARAMETER_NAMES,
- buildColumnNameByResultAlias,
buildCountStatement,
+ buildHydrationPathByResultAlias,
buildPaginationParameters,
buildProjection,
buildSelectStatement,
buildWhereExpression,
+ collectStatementAliases,
mapRowToEntity,
normaliseColumnExpression,
quoteColumn,
+ quoteQualifiedAliasReferences,
+ type ColumnSelection,
+ type ToManyDedupOrder,
type JoinClause,
type OrderByClause,
type SelectClause,
@@ -43,6 +47,7 @@ import {
type WhereClause,
} from 'src/engine/twenty-orm-v2/sql/utils/build-select-statement.util';
import { type WorkspaceTableShape } from 'src/engine/twenty-orm-v2/table-shape/types/workspace-table-shape.type';
+import { escapeIdentifier } from 'src/engine/workspace-manager/workspace-migration/utils/remove-sql-injection.util';
let objectWhereParameterSequence = 0;
@@ -66,8 +71,10 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
private readonly whereClauses: WhereClause[] = [];
private readonly joinClauses: JoinClause[] = [];
private readonly extraSelectClauses: SelectClause[] = [];
+ private readonly pendingColumnSelections: string[] = [];
private orderByClauses: OrderByClause[] = [];
private groupByExpressions: string[] = [];
+ private distinctOnExpressions: string[] = [];
private parameters: Record<string, unknown> = {};
private findOptions: FindOptionsLike = {};
private limitValue?: number;
@@ -107,8 +114,10 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
})),
);
cloned.extraSelectClauses.push(...this.extraSelectClauses);
+ cloned.pendingColumnSelections.push(...this.pendingColumnSelections);
cloned.orderByClauses = [...this.orderByClauses];
cloned.groupByExpressions = [...this.groupByExpressions];
+ cloned.distinctOnExpressions = [...this.distinctOnExpressions];
cloned.parameters = { ...this.parameters };
cloned.findOptions = { ...this.findOptions };
cloned.limitValue = this.limitValue;
@@ -192,6 +201,7 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
select(selection?: string | string[], alias?: string): this {
this.extraSelectClauses.length = 0;
+ this.pendingColumnSelections.length = 0;
if (selection === undefined) {
this.explicitSelection = undefined;
@@ -205,6 +215,12 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
return this;
}
+ if (selection === this.alias && alias === undefined) {
+ this.explicitSelection = [...this.tableShape.columnNames];
+
+ return this;
+ }
+
this.explicitSelection = [];
this.extraSelectClauses.push({
expression: this.normaliseColumnExpression(selection),
@@ -214,8 +230,20 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
return this;
}
- addSelect(expression: string, alias: string): this {
- this.extraSelectClauses.push({ expression, alias });
+ addSelect(expression: string, alias?: string): this {
+ if (isDefined(alias)) {
+ this.extraSelectClauses.push({ expression, alias });
+
+ return this;
+ }
+
+ if (/^\w+\.\w+$/.test(expression)) {
+ this.pendingColumnSelections.push(expression);
+
+ return this;
+ }
+
+ this.extraSelectClauses.push({ expression, alias: expression });
return this;
}
@@ -238,6 +266,14 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
return this.appendOrderBy(orderByOrExpression, direction, nulls);
}
+ distinctOn(columns: string[]): this {
+ this.distinctOnExpressions = columns.map((column) =>
+ this.normaliseColumnExpression(column),
+ );
+
+ return this;
+ }
+
groupBy(expression: string): this {
this.groupByExpressions = [this.normaliseColumnExpression(expression)];
@@ -254,7 +290,56 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
relationPath: string,
alias: string,
condition?: string,
- options?: { allowToManyJoin?: boolean },
+ options?: {
+ allowToManyJoin?: boolean;
+ toManyDedupOrder?: ToManyDedupOrder[];
+ },
+ ): this {
+ return this.addJoin('LEFT', relationPath, alias, condition, options);
+ }
+
+ innerJoin(
+ relationPath: string,
+ alias: string,
+ condition?: string,
+ options?: {
+ allowToManyJoin?: boolean;
+ toManyDedupOrder?: ToManyDedupOrder[];
+ },
+ ): this {
+ return this.addJoin('INNER', relationPath, alias, condition, options);
+ }
+
+ leftJoinAndSelect(
+ relationPath: string,
+ alias: string,
+ condition?: string,
+ ): this {
+ return this.addJoin('LEFT', relationPath, alias, condition, {
+ select: true,
+ });
+ }
+
+ innerJoinAndSelect(
+ relationPath: string,
+ alias: string,
+ condition?: string,
+ ): this {
+ return this.addJoin('INNER', relationPath, alias, condition, {
+ select: true,
+ });
+ }
+
+ private addJoin(
+ joinType: 'INNER' | 'LEFT',
+ relationPath: string,
+ alias: string,
+ condition?: string,
+ options?: {
+ allowToManyJoin?: boolean;
+ toManyDedupOrder?: ToManyDedupOrder[];
+ select?: boolean;
+ },
): this {
const [parentAlias, relationFieldName] = relationPath.split('.');
@@ -265,16 +350,32 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
);
}
+ if (alias === this.alias) {
+ throw new TwentyOrmV2Exception(
+ `Join alias "${alias}" collides with the main query alias`,
+ TwentyOrmV2ExceptionCode.INVALID_PARAMETER,
+ );
+ }
+
if (this.joinClauses.some((joinClause) => joinClause.alias === alias)) {
return this;
}
+ const parentTableShape = this.getTableShapeForAlias(parentAlias);
+
+ if (!isDefined(parentTableShape)) {
+ throw new TwentyOrmV2Exception(
+ `Join path "${relationPath}" references "${parentAlias}", which is neither the main alias nor a joined alias`,
+ TwentyOrmV2ExceptionCode.UNKNOWN_RELATION,
+ );
+ }
+
const relationShape =
- this.tableShape.relationShapeByFieldName[relationFieldName];
+ parentTableShape.relationShapeByFieldName[relationFieldName];
if (!isDefined(relationShape)) {
throw new TwentyOrmV2Exception(
- `Relation "${relationFieldName}" does not exist on "${this.tableShape.nameSingular}"`,
+ `Relation "${relationFieldName}" does not exist on "${parentTableShape.nameSingular}"`,
TwentyOrmV2ExceptionCode.UNKNOWN_RELATION,
);
}
@@ -285,36 +386,78 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
const joinColumnName = relationShape.joinColumnName;
- // A to-one relation carries its join column on the current table, so the condition
- // is always renderable. A to-many relation carries the foreign key on the target
- // table; it is only built when the caller opts in (group-by "with records" ordering)
- // and renders as a DISTINCT ON derived table so it stays one row per parent.
- // Otherwise the condition is left undefined and the shared to-one guard rejects it.
- const toManyJoin =
- options?.allowToManyJoin === true
- ? this.buildToManyJoin({
- parentAlias,
- alias,
- targetTableShape,
- targetFieldMetadataId: relationShape.targetFieldMetadataId,
- })
- : undefined;
+ const toManyJoin = isDefined(joinColumnName)
+ ? undefined
+ : this.buildToManyJoin({
+ parentAlias,
+ alias,
+ targetTableShape,
+ targetFieldMetadataId: relationShape.targetFieldMetadataId,
+ });
+
+ const shouldJoinDedupedToMany = options?.allowToManyJoin === true;
this.joinClauses.push({
alias,
+ parentAlias,
+ relationFieldName,
targetTableShape,
relationType: relationShape.relationType,
+ joinType,
+ isSelected: options?.select === true,
condition: isDefined(joinColumnName)
? (condition ??
`${this.quoteColumn(parentAlias, joinColumnName)} = ${this.quoteColumn(alias, 'id')}`)
- : (condition ?? toManyJoin?.condition),
- toManyForeignKeyColumnName: toManyJoin?.foreignKeyColumnName,
+ : shouldJoinDedupedToMany
+ ? (condition ?? toManyJoin?.condition)
+ : undefined,
+ toManyForeignKeyColumnName: shouldJoinDedupedToMany
+ ? toManyJoin?.foreignKeyColumnName
+ : undefined,
+ toManyPlainCondition: condition ?? toManyJoin?.condition,
+ toManyDedupOrder: shouldJoinDedupedToMany
+ ? options?.toManyDedupOrder
+ : undefined,
additionalOnConditions: [],
});
return this;
}
+ private getTableShapeForAlias(
+ alias: string,
+ ): WorkspaceTableShape | undefined {
+ if (alias === this.alias) {
+ return this.tableShape;
+ }
+
+ return this.joinClauses.find((joinClause) => joinClause.alias === alias)
+ ?.targetTableShape;
+ }
+
+ private resolveColumnSelections(): ColumnSelection[] {
+ return this.pendingColumnSelections.map((expression) => {
+ const [alias, columnName] = expression.split('.');
+ const tableShape = this.getTableShapeForAlias(alias);
+
+ if (!isDefined(tableShape)) {
+ throw new TwentyOrmV2Exception(
+ `Selection "${expression}" references "${alias}", which is neither the main alias nor a joined alias`,
+ TwentyOrmV2ExceptionCode.UNKNOWN_RELATION,
+ );
+ }
+
+ if (!isDefined(tableShape.columnShapeByColumnName[columnName])) {
+ throw new TwentyOrmV2Exception(
+ `Column "${columnName}" does not exist on "${tableShape.nameSingular}"`,
+ TwentyOrmV2ExceptionCode.UNKNOWN_COLUMN,
+ );
+ }
+
+ return { alias, columnName };
+ });
+ }
+
private buildToManyJoin({
parentAlias,
alias,
@@ -439,7 +582,7 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
this.limitValue = this.limitValue ?? 1;
try {
- const rows = await this.executeSelect();
+ const rows = await this.executeSelect({ allowPlainToManyJoins: true });
return rows[0] as T | undefined;
} finally {
@@ -448,7 +591,7 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
}
async getRawMany<T extends Record<string, unknown>>(): Promise<T[]> {
- const rows = await this.executeSelect();
+ const rows = await this.executeSelect({ allowPlainToManyJoins: true });
return rows as T[];
}
@@ -462,7 +605,9 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
async getCount(): Promise<number> {
this.context.onBeforeExecute(this);
- const sql = buildCountStatement(this.toSelectStatementState());
+ const sql = buildCountStatement(
+ this.toSelectStatementState({ allowPlainToManyJoins: true }),
+ );
const compiled = compileNamedParameters(sql, this.parameters);
const rows = await this.context.executor.execute(compiled);
@@ -497,11 +642,36 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
}
getReferencedColumnNamesByAlias(): Record<string, string[]> {
+ const state = this.toSelectStatementState();
+ const aliases = collectStatementAliases(state);
+
return collectReferencedColumnNames({
mainAlias: this.alias,
mainAliasColumnNames: this.buildProjection().mainAliasColumnNames,
- extraSelectClauses: this.extraSelectClauses,
- orderByClauses: this.orderByClauses,
+ extraSelectClauses: this.extraSelectClauses.map((selectClause) => ({
+ ...selectClause,
+ expression: quoteQualifiedAliasReferences(
+ selectClause.expression,
+ aliases,
+ ),
+ })),
+ orderByClauses: this.orderByClauses
+ .filter(
+ (orderByClause) =>
+ !this.extraSelectClauses.some(
+ (selectClause) =>
+ escapeIdentifier(selectClause.alias) ===
+ orderByClause.expression,
+ ),
+ )
+ .map((orderByClause) => ({
+ ...orderByClause,
+ expression: quoteQualifiedAliasReferences(
+ orderByClause.expression,
+ aliases,
+ ),
+ })),
+ distinctOnExpressions: this.distinctOnExpressions,
});
}
@@ -786,27 +956,44 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
direction,
nulls,
normaliseColumnExpression: (expression) =>
- this.normaliseColumnExpression(expression),
+ this.normaliseOrderByExpression(expression),
}),
);
return this;
}
- private async executeSelect(): Promise<Record<string, unknown>[]> {
+ private normaliseOrderByExpression(expression: string): string {
+ const isBareIdentifier = /^\w+$/.test(expression);
+
+ if (
+ isBareIdentifier &&
+ this.extraSelectClauses.some(
+ (selectClause) => selectClause.alias === expression,
+ )
+ ) {
+ return escapeIdentifier(expression);
+ }
+
+ return this.normaliseColumnExpression(expression);
+ }
+
+ private async executeSelect(options?: {
+ allowPlainToManyJoins?: boolean;
+ }): Promise<Record<string, unknown>[]> {
this.context.onBeforeExecute(this);
- const { sql, parameters } = this.buildSelectStatement();
+ const { sql, parameters } = this.buildSelectStatement(options);
const compiled = compileNamedParameters(sql, parameters);
return this.context.executor.execute(compiled);
}
- private buildSelectStatement(): {
+ private buildSelectStatement(options?: { allowPlainToManyJoins?: boolean }): {
sql: string;
parameters: Record<string, unknown>;
} {
- const state = this.toSelectStatementState();
+ const state = this.toSelectStatementState(options);
return {
sql: buildSelectStatement(state),
@@ -822,10 +1009,7 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
}
private buildColumnNameByResultAlias(): Record<string, string> {
- return buildColumnNameByResultAlias(
- this.alias,
- this.buildProjection().mainAliasColumnNames,
- );
+ return buildHydrationPathByResultAlias(this.toSelectStatementState());
}
private buildWhereExpression(options?: {
@@ -842,18 +1026,23 @@ export class WorkspaceSelectQueryBuilderV2 implements WhereExpressionLike {
return quoteColumn(alias, columnName);
}
- private toSelectStatementState(): SelectStatementState {
+ private toSelectStatementState(options?: {
+ allowPlainToManyJoins?: boolean;
+ }): SelectStatementState {
return {
alias: this.alias,
tableShape: this.tableShape,
findOptions: this.findOptions,
explicitSelection: this.explicitSelection,
extraSelectClauses: this.extraSelectClauses,
+ columnSelections: this.resolveColumnSelections(),
joinClauses: this.joinClauses,
whereClauses: this.whereClauses,
groupByExpressions: this.groupByExpressions,
orderByClauses: this.orderByClauses,
+ distinctOnExpressions: this.distinctOnExpressions,
includeDeleted: this.includeDeleted,
+ allowPlainToManyJoins: options?.allowPlainToManyJoins ?? false,
limitValue: this.limitValue,
offsetValue: this.offsetValue,
};
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/repository/workspace-repository-v2.ts b/packages/twenty-server/src/engine/twenty-orm-v2/repository/workspace-repository-v2.ts
index 170336c9ca2..b148374f9a4 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/repository/workspace-repository-v2.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/repository/workspace-repository-v2.ts
@@ -33,6 +33,8 @@ import { type MutationKind } from 'src/engine/twenty-orm-v2/sql/utils/build-muta
import { RelationType } from 'src/engine/metadata-modules/field-metadata/interfaces/relation-type.interface';
import {
applyFindOptionsToQueryBuilder,
+ normalizeFindOptionsRelations,
+ splitFindOptionsOrder,
type FindOptionsRelationsV2,
type FindOptionsV2,
} from 'src/engine/twenty-orm-v2/query-builder/utils/apply-find-options.util';
@@ -40,7 +42,10 @@ import {
applyMutationCriteriaToQueryBuilder,
type MutationCriteria,
} from 'src/engine/twenty-orm-v2/query-builder/utils/apply-mutation-criteria.util';
-import { type ObjectWhereLike } from 'src/engine/twenty-orm-v2/query-builder/types/query-builder-v2.type';
+import {
+ type ObjectWhereLike,
+ type OrderByConditionLike,
+} from 'src/engine/twenty-orm-v2/query-builder/types/query-builder-v2.type';
import {
attachToManyRelationToRecords,
attachToOneRelationToRecords,
@@ -172,8 +177,10 @@ export class WorkspaceRepositoryV2 {
if (isDefined(options?.relations)) {
await this.loadRelations(
records,
- options.relations,
+ normalizeFindOptionsRelations(options.relations),
options.withDeleted ?? false,
+ splitFindOptionsOrder(this.options.tableShape, options.order)
+ .orderByRelationFieldName,
);
}
@@ -202,8 +209,10 @@ export class WorkspaceRepositoryV2 {
if (isDefined(record) && isDefined(options?.relations)) {
await this.loadRelations(
[record],
- options.relations,
+ normalizeFindOptionsRelations(options.relations),
options.withDeleted ?? false,
+ splitFindOptionsOrder(this.options.tableShape, options.order)
+ .orderByRelationFieldName,
);
}
@@ -330,6 +339,7 @@ export class WorkspaceRepositoryV2 {
records: ObjectRecord[],
relations: FindOptionsRelationsV2,
withDeleted: boolean,
+ orderByRelationFieldName: Record<string, OrderByConditionLike> = {},
): Promise<void> {
if (records.length === 0) {
return;
@@ -371,6 +381,7 @@ export class WorkspaceRepositoryV2 {
targetRepository,
nestedRelations,
withDeleted,
+ order: orderByRelationFieldName[fieldName],
});
} else {
throw new TwentyOrmV2Exception(
@@ -427,6 +438,7 @@ export class WorkspaceRepositoryV2 {
targetRepository,
nestedRelations,
withDeleted,
+ order,
}: {
records: ObjectRecord[];
fieldName: string;
@@ -434,6 +446,7 @@ export class WorkspaceRepositoryV2 {
targetRepository: WorkspaceRepositoryV2;
nestedRelations?: FindOptionsRelationsV2;
withDeleted: boolean;
+ order?: OrderByConditionLike;
}): Promise<void> {
const inverseForeignKeyColumnName =
this.resolveInverseForeignKeyColumnName(relationShape);
@@ -446,6 +459,7 @@ export class WorkspaceRepositoryV2 {
where: { [inverseForeignKeyColumnName]: In(parentIds) },
relations: nestedRelations,
withDeleted,
+ order,
})
: [];
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/build-select-statement.util.ts b/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/build-select-statement.util.ts
index 86ff880a922..0c6f967f28d 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/build-select-statement.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/build-select-statement.util.ts
@@ -18,13 +18,30 @@ export type WhereClause = {
export type JoinClause = {
alias: string;
+ parentAlias: string;
+ relationFieldName: string;
targetTableShape: WorkspaceTableShape;
relationType: RelationType;
+ joinType: 'INNER' | 'LEFT';
+ isSelected?: boolean;
condition?: string;
toManyForeignKeyColumnName?: string;
+ toManyPlainCondition?: string;
+ toManyDedupOrder?: ToManyDedupOrder[];
additionalOnConditions: string[];
};
+export type ToManyDedupOrder = {
+ columnName: string;
+ direction: 'ASC' | 'DESC';
+ nulls?: 'NULLS FIRST' | 'NULLS LAST';
+};
+
+export type ColumnSelection = {
+ alias: string;
+ columnName: string;
+};
+
export type SelectClause = {
expression: string;
alias: string;
@@ -42,11 +59,14 @@ export type SelectStatementState = {
findOptions: FindOptionsLike;
explicitSelection?: string[];
extraSelectClauses: SelectClause[];
+ columnSelections: ColumnSelection[];
joinClauses: JoinClause[];
whereClauses: WhereClause[];
groupByExpressions: string[];
orderByClauses: OrderByClause[];
+ distinctOnExpressions: string[];
includeDeleted: boolean;
+ allowPlainToManyJoins: boolean;
limitValue?: number;
offsetValue?: number;
};
@@ -71,6 +91,120 @@ export const normaliseColumnExpression = (
return quoteColumn(defaultAlias, expression);
};
+const SQL_TEXT_SEGMENTS = /('(?:[^']|'')*'|"[^"]*")/;
+
+const quoteQualifiedAliasReferencesInSegment = (
+ segment: string,
+ aliases: string[],
+): string =>
+ aliases.reduce(
+ (quotedSegment, alias) =>
+ quotedSegment.replace(
+ new RegExp(
+ `(?<![\\w".:])${alias.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')}\\.(\\w+)`,
+ 'g',
+ ),
+ (_, columnName) => quoteColumn(alias, columnName),
+ ),
+ segment,
+ );
+
+export const quoteQualifiedAliasReferences = (
+ expression: string,
+ aliases: string[],
+): string =>
+ expression
+ .split(SQL_TEXT_SEGMENTS)
+ .map((segment, segmentIndex) =>
+ segmentIndex % 2 === 1
+ ? segment
+ : quoteQualifiedAliasReferencesInSegment(segment, aliases),
+ )
+ .join('');
+
+export const collectStatementAliases = (
+ state: SelectStatementState,
+): string[] => [
+ state.alias,
+ ...state.joinClauses.map((joinClause) => joinClause.alias),
+];
+
+const buildJoinPropertyPath = (
+ state: SelectStatementState,
+ joinAlias: string,
+): string => {
+ const propertySegments: string[] = [];
+ let currentAlias = joinAlias;
+
+ while (currentAlias !== state.alias) {
+ const joinClause = state.joinClauses.find(
+ (candidate) => candidate.alias === currentAlias,
+ );
+
+ if (!isDefined(joinClause)) {
+ throw new TwentyOrmV2Exception(
+ `Alias "${currentAlias}" does not belong to this statement`,
+ TwentyOrmV2ExceptionCode.UNKNOWN_RELATION,
+ );
+ }
+
+ propertySegments.unshift(joinClause.relationFieldName);
+ currentAlias = joinClause.parentAlias;
+ }
+
+ return propertySegments.join('.');
+};
+
+export type JoinedColumnProjection = {
+ joinAlias: string;
+ columnName: string;
+ resultAlias: string;
+ propertyPath: string;
+};
+
+export const collectJoinedColumnProjections = (
+ state: SelectStatementState,
+): JoinedColumnProjection[] => {
+ const projections: JoinedColumnProjection[] = [];
+ const seenResultAliases = new Set<string>();
+
+ const addProjection = (joinAlias: string, columnName: string) => {
+ const resultAlias = buildColumnResultAlias(joinAlias, columnName);
+
+ if (seenResultAliases.has(resultAlias)) {
+ return;
+ }
+
+ seenResultAliases.add(resultAlias);
+ projections.push({
+ joinAlias,
+ columnName,
+ resultAlias,
+ propertyPath: buildJoinPropertyPath(state, joinAlias),
+ });
+ };
+
+ for (const joinClause of state.joinClauses) {
+ if (joinClause.isSelected !== true) {
+ continue;
+ }
+
+ for (const columnName of joinClause.targetTableShape.columnNames) {
+ addProjection(joinClause.alias, columnName);
+ }
+ }
+
+ for (const columnSelection of state.columnSelections) {
+ if (columnSelection.alias === state.alias) {
+ continue;
+ }
+
+ addProjection(columnSelection.alias, columnSelection.columnName);
+ }
+
+ return projections;
+};
+
export const buildProjection = (
state: SelectStatementState,
): { expressions: string[]; mainAliasColumnNames: string[] } => {
@@ -100,9 +234,41 @@ export const buildProjection = (
)}`,
);
+ const projectedMainColumnNames = new Set(mainAliasColumnNames);
+
+ for (const columnSelection of state.columnSelections) {
+ if (
+ columnSelection.alias !== state.alias ||
+ projectedMainColumnNames.has(columnSelection.columnName)
+ ) {
+ continue;
+ }
+
+ projectedMainColumnNames.add(columnSelection.columnName);
+ expressions.push(
+ `${quoteColumn(state.alias, columnSelection.columnName)} AS ${escapeIdentifier(
+ buildColumnResultAlias(state.alias, columnSelection.columnName),
+ )}`,
+ );
+ }
+
+ for (const joinedProjection of collectJoinedColumnProjections(state)) {
+ expressions.push(
+ `${quoteColumn(
+ joinedProjection.joinAlias,
+ joinedProjection.columnName,
+ )} AS ${escapeIdentifier(joinedProjection.resultAlias)}`,
+ );
+ }
+
+ const aliases = collectStatementAliases(state);
+
for (const extraSelect of state.extraSelectClauses) {
expressions.push(
- `${extraSelect.expression} AS ${escapeIdentifier(extraSelect.alias)}`,
+ `${quoteQualifiedAliasReferences(
+ extraSelect.expression,
+ aliases,
+ )} AS ${escapeIdentifier(extraSelect.alias)}`,
);
}
@@ -126,7 +292,10 @@ export const buildWhereExpression = (
includeSoftDeletePredicate = true,
}: { includeSoftDeletePredicate?: boolean } = {},
): string => {
- const userExpression = renderUserWhereExpression(state.whereClauses);
+ const userExpression = quoteQualifiedAliasReferences(
+ renderUserWhereExpression(state.whereClauses),
+ collectStatementAliases(state),
+ );
const shouldAddSoftDeletePredicate =
includeSoftDeletePredicate &&
@@ -155,23 +324,42 @@ const buildToManyDedupedJoinSource = ({
tableExpression,
foreignKeyColumnName,
includeSoftDeleteFilter,
+ dedupOrder = [],
}: {
tableExpression: string;
foreignKeyColumnName: string;
includeSoftDeleteFilter: boolean;
+ dedupOrder?: ToManyDedupOrder[];
}): string => {
const foreignKey = escapeIdentifier(foreignKeyColumnName);
const whereClause = includeSoftDeleteFilter
? ` WHERE ${escapeIdentifier('deletedAt')} IS NULL`
: '';
- return `(SELECT DISTINCT ON (${foreignKey}) * FROM ${tableExpression}${whereClause} ORDER BY ${foreignKey}, ${escapeIdentifier('id')})`;
+ const orderExpressions = [
+ foreignKey,
+ ...dedupOrder.map(
+ (order) =>
+ `${escapeIdentifier(order.columnName)} ${order.direction}${
+ isDefined(order.nulls) ? ` ${order.nulls}` : ''
+ }`,
+ ),
+ escapeIdentifier('id'),
+ ];
+
+ return `(SELECT DISTINCT ON (${foreignKey}) * FROM ${tableExpression}${whereClause} ORDER BY ${orderExpressions.join(', ')})`;
};
export const buildJoinClause = (state: SelectStatementState): string =>
state.joinClauses
.map((joinClause) => {
- if (!isDefined(joinClause.condition)) {
+ const condition =
+ joinClause.condition ??
+ (state.allowPlainToManyJoins
+ ? joinClause.toManyPlainCondition
+ : undefined);
+
+ if (!isDefined(condition)) {
throw new TwentyOrmV2Exception(
`Only to-one relations can be joined; "${joinClause.alias}" is to-many and must be loaded as a separate query`,
TwentyOrmV2ExceptionCode.UNSUPPORTED_OPERATION,
@@ -186,14 +374,8 @@ export const buildJoinClause = (state: SelectStatementState): string =>
? joinClause.toManyForeignKeyColumnName
: undefined;
- const onConditions = [
- joinClause.condition,
- ...joinClause.additionalOnConditions,
- ];
+ const onConditions = [condition, ...joinClause.additionalOnConditions];
- // A to-one join filters soft-deleted rows in its ON clause. The deduped
- // to-many join picks one representative row per parent, so its soft-delete
- // filter runs inside the derived table before that pick.
if (
softDeletePredicateApplies &&
!isDefined(toManyForeignKeyColumnName)
@@ -212,10 +394,11 @@ export const buildJoinClause = (state: SelectStatementState): string =>
tableExpression,
foreignKeyColumnName: toManyForeignKeyColumnName,
includeSoftDeleteFilter: softDeletePredicateApplies,
+ dedupOrder: joinClause.toManyDedupOrder,
})
: tableExpression;
- return `LEFT JOIN ${joinSource} AS ${escapeIdentifier(
+ return `${joinClause.joinType} JOIN ${joinSource} AS ${escapeIdentifier(
joinClause.alias,
)} ON ${onConditions.map((condition) => `(${condition})`).join(' AND ')}`;
})
@@ -234,18 +417,18 @@ export const buildOrderByClause = (state: SelectStatementState): string => {
return '';
}
+ const aliases = collectStatementAliases(state);
+
return `ORDER BY ${state.orderByClauses
.map(
(orderByClause) =>
- `${orderByClause.expression} ${orderByClause.direction}${
- isDefined(orderByClause.nulls) ? ` ${orderByClause.nulls}` : ''
- }`,
+ `${quoteQualifiedAliasReferences(orderByClause.expression, aliases)} ${
+ orderByClause.direction
+ }${isDefined(orderByClause.nulls) ? ` ${orderByClause.nulls}` : ''}`,
)
.join(', ')}`;
};
-// Inlining page size and offset would mint a new prepared statement shape per page, so
-// they bind as parameters under reserved names the shared parsers cannot produce.
export const LIMIT_PARAMETER_NAME = 'ormV2Limit';
export const OFFSET_PARAMETER_NAME = 'ormV2Offset';
@@ -265,11 +448,23 @@ export const buildPaginationParameters = (
: {}),
});
+const buildDistinctOnClause = (state: SelectStatementState): string => {
+ if (state.distinctOnExpressions.length === 0) {
+ return '';
+ }
+
+ const aliases = collectStatementAliases(state);
+
+ return `DISTINCT ON (${state.distinctOnExpressions
+ .map((expression) => quoteQualifiedAliasReferences(expression, aliases))
+ .join(', ')}) `;
+};
+
export const buildSelectStatement = (state: SelectStatementState): string => {
const whereExpression = buildWhereExpression(state);
return [
- `SELECT ${buildProjection(state).expressions.join(', ')}`,
+ `SELECT ${buildDistinctOnClause(state)}${buildProjection(state).expressions.join(', ')}`,
buildFromClause(state),
buildJoinClause(state),
whereExpression.length > 0 ? `WHERE ${whereExpression}` : '',
@@ -285,8 +480,13 @@ export const buildSelectStatement = (state: SelectStatementState): string => {
export const buildCountStatement = (state: SelectStatementState): string => {
const whereExpression = buildWhereExpression(state);
+ const countExpression =
+ state.joinClauses.length > 0
+ ? `COUNT(DISTINCT ${quoteColumn(state.alias, 'id')})`
+ : 'COUNT(1)';
+
return [
- 'SELECT COUNT(1) AS "count"',
+ `SELECT ${countExpression} AS "count"`,
buildFromClause(state),
buildJoinClause(state),
whereExpression.length > 0 ? `WHERE ${whereExpression}` : '',
@@ -295,20 +495,81 @@ export const buildCountStatement = (state: SelectStatementState): string => {
.join(' ');
};
-// Only the projected columns of the main alias become entity properties: anything else
-// in the row, such as a relation order-by column, is raw join output.
+type RelationColumnLeaf = {
+ propertySegments: string[];
+ value: unknown;
+};
+
+const buildRelationValue = (leaves: RelationColumnLeaf[]): unknown => {
+ const idLeaf = leaves.find(
+ (leaf) =>
+ leaf.propertySegments.length === 1 && leaf.propertySegments[0] === 'id',
+ );
+
+ if (isDefined(idLeaf) && idLeaf.value === null) {
+ return null;
+ }
+
+ const relationValue: Record<string, unknown> = {};
+ const nestedLeavesByProperty = new Map<string, RelationColumnLeaf[]>();
+
+ for (const leaf of leaves) {
+ const [propertyName, ...remainingSegments] = leaf.propertySegments;
+
+ if (remainingSegments.length === 0) {
+ relationValue[propertyName] = leaf.value;
+ continue;
+ }
+
+ const nestedLeaves = nestedLeavesByProperty.get(propertyName) ?? [];
+
+ nestedLeaves.push({
+ propertySegments: remainingSegments,
+ value: leaf.value,
+ });
+ nestedLeavesByProperty.set(propertyName, nestedLeaves);
+ }
+
+ for (const [propertyName, nestedLeaves] of nestedLeavesByProperty) {
+ relationValue[propertyName] = buildRelationValue(nestedLeaves);
+ }
+
+ return relationValue;
+};
+
export const mapRowToEntity = <T extends Record<string, unknown>>(
row: Record<string, unknown>,
columnNameByResultAlias: Record<string, string>,
): T => {
const entity: Record<string, unknown> = {};
+ const relationLeavesByProperty = new Map<string, RelationColumnLeaf[]>();
- for (const [resultAlias, columnName] of Object.entries(
+ for (const [resultAlias, propertyPath] of Object.entries(
columnNameByResultAlias,
)) {
- if (resultAlias in row) {
- entity[columnName] = row[resultAlias];
+ if (!(resultAlias in row)) {
+ continue;
+ }
+
+ const propertySegments = propertyPath.split('.');
+
+ if (propertySegments.length === 1) {
+ entity[propertyPath] = row[resultAlias];
+ continue;
}
+
+ const [relationProperty, ...remainingSegments] = propertySegments;
+ const relationLeaves = relationLeavesByProperty.get(relationProperty) ?? [];
+
+ relationLeaves.push({
+ propertySegments: remainingSegments,
+ value: row[resultAlias],
+ });
+ relationLeavesByProperty.set(relationProperty, relationLeaves);
+ }
+
+ for (const [relationProperty, relationLeaves] of relationLeavesByProperty) {
+ entity[relationProperty] = buildRelationValue(relationLeaves);
}
return entity as T;
@@ -324,3 +585,26 @@ export const buildColumnNameByResultAlias = (
columnName,
]),
);
+
+export const buildHydrationPathByResultAlias = (
+ state: SelectStatementState,
+): Record<string, string> => {
+ const { mainAliasColumnNames } = buildProjection(state);
+
+ const mainColumnNames = [
+ ...mainAliasColumnNames,
+ ...state.columnSelections
+ .filter((columnSelection) => columnSelection.alias === state.alias)
+ .map((columnSelection) => columnSelection.columnName),
+ ];
+
+ return {
+ ...buildColumnNameByResultAlias(state.alias, mainColumnNames),
+ ...Object.fromEntries(
+ collectJoinedColumnProjections(state).map((joinedProjection) => [
+ joinedProjection.resultAlias,
+ `${joinedProjection.propertyPath}.${joinedProjection.columnName}`,
+ ]),
+ ),
+ };
+};
diff --git a/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/collect-referenced-column-names.util.ts b/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/collect-referenced-column-names.util.ts
index 7d8e08bab57..51a86aefc2b 100644
--- a/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/collect-referenced-column-names.util.ts
+++ b/packages/twenty-server/src/engine/twenty-orm-v2/sql/utils/collect-referenced-column-names.util.ts
@@ -11,11 +11,13 @@ export const collectReferencedColumnNames = ({
mainAliasColumnNames,
extraSelectClauses,
orderByClauses,
+ distinctOnExpressions = [],
}: {
mainAlias: string;
mainAliasColumnNames: string[];
extraSelectClauses: SelectClause[];
orderByClauses: OrderByClause[];
+ distinctOnExpressions?: string[];
}): Record<string, string[]> => {
const columnNamesByAlias: Record<string, Set<string>> = {
[mainAlias]: new Set(mainAliasColumnNames),
@@ -30,6 +32,7 @@ export const collectReferencedColumnNames = ({
const expressions = [
...extraSelectClauses.map((extraSelect) => extraSelect.expression),
...orderByClauses.map((orderByClause) => orderByClause.expression),
+ ...distinctOnExpressions,
];
for (const expression of expressions) {