From 39353cf15fc1f4893b713436a8f51a825d15f769 Mon Sep 17 00:00:00 2001 From: Erik Dakoda Date: Mon, 5 Oct 2026 17:46:11 -0400 Subject: [PATCH] fix(clients): keep the ORM runtime out of client bundles The type-only bindings in client-helpers were imported as values, so the build kept a bare `import "@zenstackhq/orm"` that bundlers cannot drop. tanstack-query imported `CoreReadOperations` from the ORM root. - import the client-helpers bindings with `import type` - add a dependency-free `@zenstackhq/orm/constants` entry for the operation-name lists, moved to `client/constants.ts` and re-exported from `operations/base.ts` - import `CoreReadOperations` in tanstack-query from the new entry Fixes #2763 Co-Authored-By: Claude Opus 5.5 --- packages/clients/client-helpers/src/types.ts | 2 +- .../tanstack-query/src/common/transaction.ts | 2 +- packages/orm/package.json | 10 ++ packages/orm/src/client/constants.ts | 125 ++++++++++++++++ .../orm/src/client/crud/operations/base.ts | 137 ++---------------- packages/orm/src/constants.ts | 12 ++ packages/orm/test/constants.test.ts | 22 +++ packages/orm/tsdown.config.ts | 1 + 8 files changed, 184 insertions(+), 127 deletions(-) create mode 100644 packages/orm/src/constants.ts create mode 100644 packages/orm/test/constants.test.ts diff --git a/packages/clients/client-helpers/src/types.ts b/packages/clients/client-helpers/src/types.ts index a4f4f68cc..247b8344a 100644 --- a/packages/clients/client-helpers/src/types.ts +++ b/packages/clients/client-helpers/src/types.ts @@ -1,4 +1,4 @@ -import { ExtQueryArgsMarker, ExtResultMarker, type QueryOptions } from '@zenstackhq/orm'; +import type { ExtQueryArgsMarker, ExtResultMarker, QueryOptions } from '@zenstackhq/orm'; import type { SchemaDef } from '@zenstackhq/schema'; /** diff --git a/packages/clients/tanstack-query/src/common/transaction.ts b/packages/clients/tanstack-query/src/common/transaction.ts index 396c5b3d4..31d09fb40 100644 --- a/packages/clients/tanstack-query/src/common/transaction.ts +++ b/packages/clients/tanstack-query/src/common/transaction.ts @@ -2,7 +2,7 @@ import type { Logger } from '@zenstackhq/client-helpers'; import { createInvalidator, TRANSACTION_ROUTE_PREFIX, type InvalidateFunc } from '@zenstackhq/client-helpers'; import { fetcher, marshal, type FetchFn } from '@zenstackhq/client-helpers/fetch'; import type { TransactionOperation } from '@zenstackhq/client-helpers'; -import { CoreReadOperations } from '@zenstackhq/orm'; +import { CoreReadOperations } from '@zenstackhq/orm/constants'; import type { SchemaDef } from '@zenstackhq/schema'; /** diff --git a/packages/orm/package.json b/packages/orm/package.json index 6f4021380..71e437f5c 100644 --- a/packages/orm/package.json +++ b/packages/orm/package.json @@ -106,6 +106,16 @@ "default": "./dist/common-types.cjs" } }, + "./constants": { + "import": { + "types": "./dist/constants.d.mts", + "default": "./dist/constants.mjs" + }, + "require": { + "types": "./dist/constants.d.cts", + "default": "./dist/constants.cjs" + } + }, "./package.json": { "import": "./package.json", "require": "./package.json" diff --git a/packages/orm/src/client/constants.ts b/packages/orm/src/client/constants.ts index eeb7e65b6..582e7df72 100644 --- a/packages/orm/src/client/constants.ts +++ b/packages/orm/src/client/constants.ts @@ -85,3 +85,128 @@ export const FILTER_PROPERTY_TO_KIND = { * Mapping of filter operators to their corresponding filter kind categories. */ export type FilterPropertyToKind = typeof FILTER_PROPERTY_TO_KIND; + +/** + * List of core CRUD operations. It excludes the 'orThrow' variants. + */ +export const CoreCrudOperations = [ + 'findMany', + 'findUnique', + 'findFirst', + 'create', + 'createMany', + 'createManyAndReturn', + 'update', + 'updateMany', + 'updateManyAndReturn', + 'upsert', + 'delete', + 'deleteMany', + 'count', + 'aggregate', + 'groupBy', + 'exists', +] as const; + +/** + * List of core CRUD operations. It excludes the 'orThrow' variants. + */ +export type CoreCrudOperations = (typeof CoreCrudOperations)[number]; + +/** + * List of core read operations. It excludes the 'orThrow' variants. + */ +export const CoreReadOperations = [ + 'findMany', + 'findUnique', + 'findFirst', + 'count', + 'aggregate', + 'groupBy', + 'exists', +] as const; + +/** + * List of core read operations. It excludes the 'orThrow' variants. + */ +export type CoreReadOperations = (typeof CoreReadOperations)[number]; + +/** + * List of core write operations. + */ +export const CoreWriteOperations = [ + 'create', + 'createMany', + 'createManyAndReturn', + 'update', + 'updateMany', + 'updateManyAndReturn', + 'upsert', + 'delete', + 'deleteMany', +] as const; + +/** + * List of core write operations. + */ +export type CoreWriteOperations = (typeof CoreWriteOperations)[number]; + +/** + * List of core create operations. + */ +export const CoreCreateOperations = ['create', 'createMany', 'createManyAndReturn', 'upsert'] as const; + +/** + * List of core create operations. + */ +export type CoreCreateOperations = (typeof CoreCreateOperations)[number]; + +/** + * List of core update operations. + */ +export const CoreUpdateOperations = ['update', 'updateMany', 'updateManyAndReturn', 'upsert'] as const; + +/** + * List of core update operations. + */ +export type CoreUpdateOperations = (typeof CoreUpdateOperations)[number]; + +/** + * List of core delete operations. + */ +export const CoreDeleteOperations = ['delete', 'deleteMany'] as const; + +/** + * List of core delete operations. + */ +export type CoreDeleteOperations = (typeof CoreDeleteOperations)[number]; + +/** + * List of all CRUD operations, including 'orThrow' variants. + */ +export const AllCrudOperations = [...CoreCrudOperations, 'findUniqueOrThrow', 'findFirstOrThrow'] as const; + +/** + * List of all CRUD operations, including 'orThrow' variants. + */ +export type AllCrudOperations = (typeof AllCrudOperations)[number]; + +/** + * List of all read operations, including 'orThrow' variants. + */ +export const AllReadOperations = [...CoreReadOperations, 'findUniqueOrThrow', 'findFirstOrThrow'] as const; + +/** + * List of all read operations, including 'orThrow' variants. + */ +export type AllReadOperations = (typeof AllReadOperations)[number]; + +/** + * List of all write operations - simply an alias of CoreWriteOperations. + */ +export const AllWriteOperations = CoreWriteOperations; + +/** + * List of all write operations - simply an alias of CoreWriteOperations. + */ +export type AllWriteOperations = CoreWriteOperations; diff --git a/packages/orm/src/client/crud/operations/base.ts b/packages/orm/src/client/crud/operations/base.ts index a9b9e572e..ce8b651e7 100644 --- a/packages/orm/src/client/crud/operations/base.ts +++ b/packages/orm/src/client/crud/operations/base.ts @@ -19,7 +19,7 @@ import { ulid } from 'ulid'; import * as uuid from 'uuid'; import type { AnyKysely } from '../../../utils/kysely-utils'; import { extractFields, fieldsToSelectObject, isEmptyObject } from '../../../utils/object-utils'; -import { NUMERIC_FIELD_TYPES } from '../../constants'; +import { NUMERIC_FIELD_TYPES, type CoreCrudOperations } from '../../constants'; import { TransactionIsolationLevel, type ClientContract, type CRUD } from '../../contract'; import type { FindArgs, SelectIncludeOmit, WhereInput } from '../../crud-types'; import { @@ -54,130 +54,17 @@ import { getCrudDialect } from '../dialects'; import type { BaseCrudDialect } from '../dialects/base-dialect'; import { InputValidator } from '../validator'; -/** - * List of core CRUD operations. It excludes the 'orThrow' variants. - */ -export const CoreCrudOperations = [ - 'findMany', - 'findUnique', - 'findFirst', - 'create', - 'createMany', - 'createManyAndReturn', - 'update', - 'updateMany', - 'updateManyAndReturn', - 'upsert', - 'delete', - 'deleteMany', - 'count', - 'aggregate', - 'groupBy', - 'exists', -] as const; - -/** - * List of core CRUD operations. It excludes the 'orThrow' variants. - */ -export type CoreCrudOperations = (typeof CoreCrudOperations)[number]; - -/** - * List of core read operations. It excludes the 'orThrow' variants. - */ -export const CoreReadOperations = [ - 'findMany', - 'findUnique', - 'findFirst', - 'count', - 'aggregate', - 'groupBy', - 'exists', -] as const; - -/** - * List of core read operations. It excludes the 'orThrow' variants. - */ -export type CoreReadOperations = (typeof CoreReadOperations)[number]; - -/** - * List of core write operations. - */ -export const CoreWriteOperations = [ - 'create', - 'createMany', - 'createManyAndReturn', - 'update', - 'updateMany', - 'updateManyAndReturn', - 'upsert', - 'delete', - 'deleteMany', -] as const; - -/** - * List of core write operations. - */ -export type CoreWriteOperations = (typeof CoreWriteOperations)[number]; - -/** - * List of core create operations. - */ -export const CoreCreateOperations = ['create', 'createMany', 'createManyAndReturn', 'upsert'] as const; - -/** - * List of core create operations. - */ -export type CoreCreateOperations = (typeof CoreCreateOperations)[number]; - -/** - * List of core update operations. - */ -export const CoreUpdateOperations = ['update', 'updateMany', 'updateManyAndReturn', 'upsert'] as const; - -/** - * List of core update operations. - */ -export type CoreUpdateOperations = (typeof CoreUpdateOperations)[number]; - -/** - * List of core delete operations. - */ -export const CoreDeleteOperations = ['delete', 'deleteMany'] as const; - -/** - * List of core delete operations. - */ -export type CoreDeleteOperations = (typeof CoreDeleteOperations)[number]; - -/** - * List of all CRUD operations, including 'orThrow' variants. - */ -export const AllCrudOperations = [...CoreCrudOperations, 'findUniqueOrThrow', 'findFirstOrThrow'] as const; - -/** - * List of all CRUD operations, including 'orThrow' variants. - */ -export type AllCrudOperations = (typeof AllCrudOperations)[number]; - -/** - * List of all read operations, including 'orThrow' variants. - */ -export const AllReadOperations = [...CoreReadOperations, 'findUniqueOrThrow', 'findFirstOrThrow'] as const; - -/** - * List of all read operations, including 'orThrow' variants. - */ -export type AllReadOperations = (typeof AllReadOperations)[number]; - -/** - * List of all write operations - simply an alias of CoreWriteOperations. - */ -export const AllWriteOperations = CoreWriteOperations; - -/** - * List of all write operations - simply an alias of CoreWriteOperations. - */ -export type AllWriteOperations = CoreWriteOperations; +export { + AllCrudOperations, + AllReadOperations, + AllWriteOperations, + CoreCreateOperations, + CoreCrudOperations, + CoreDeleteOperations, + CoreReadOperations, + CoreUpdateOperations, + CoreWriteOperations, +} from '../../constants'; // context for nested relation operations export type FromRelationContext = { diff --git a/packages/orm/src/constants.ts b/packages/orm/src/constants.ts new file mode 100644 index 000000000..8164f7a0a --- /dev/null +++ b/packages/orm/src/constants.ts @@ -0,0 +1,12 @@ +// Dependency-free constants, importable without pulling in the ORM runtime (e.g. from browser clients). +export { + AllCrudOperations, + AllReadOperations, + AllWriteOperations, + CoreCreateOperations, + CoreCrudOperations, + CoreDeleteOperations, + CoreReadOperations, + CoreUpdateOperations, + CoreWriteOperations, +} from './client/constants'; diff --git a/packages/orm/test/constants.test.ts b/packages/orm/test/constants.test.ts new file mode 100644 index 000000000..0270c23d4 --- /dev/null +++ b/packages/orm/test/constants.test.ts @@ -0,0 +1,22 @@ +import { describe, expect, it } from 'vitest'; +import * as constants from '../src/constants'; +import * as orm from '../src/index'; + +describe('constants entry', () => { + it('exports the operation lists of the main entry', () => { + expect(Object.keys(constants).sort()).toEqual([ + 'AllCrudOperations', + 'AllReadOperations', + 'AllWriteOperations', + 'CoreCreateOperations', + 'CoreCrudOperations', + 'CoreDeleteOperations', + 'CoreReadOperations', + 'CoreUpdateOperations', + 'CoreWriteOperations', + ]); + for (const [name, value] of Object.entries(constants)) { + expect(orm[name as keyof typeof orm]).toBe(value); + } + }); +}); diff --git a/packages/orm/tsdown.config.ts b/packages/orm/tsdown.config.ts index 39118a802..af397a1cf 100644 --- a/packages/orm/tsdown.config.ts +++ b/packages/orm/tsdown.config.ts @@ -6,6 +6,7 @@ export default createConfig({ schema: 'src/schema.ts', helpers: 'src/helpers.ts', 'common-types': 'src/common-types.ts', + constants: 'src/constants.ts', 'dialects/sqlite': 'src/dialects/sqlite.ts', 'dialects/postgres': 'src/dialects/postgres.ts', 'dialects/mysql': 'src/dialects/mysql.ts',