Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BREAKINGCHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
1. `update` and `delete` policy rejection throws `NotFoundError`
1. `check()` ORM api has been removed
1. non-optional to-one relation doesn't automatically filter parent read when evaluating access policies
1. `@omit` and `@password` attributes have been removed
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isPlainObject } from '@zenstackhq/common-helpers';
import { isPlainObject } from './is-plain-object';

/**
* Clones the given object. Only arrays and plain objects are cloned. Other values are returned as is.
Expand Down
2 changes: 2 additions & 0 deletions packages/common-helpers/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export * from './clone';
export * from './enumerable';
export * from './is-plain-object';
export * from './lower-case-first';
export * from './param-case';
Expand Down
12 changes: 6 additions & 6 deletions packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { invariant, isPlainObject } from '@zenstackhq/common-helpers';
import { enumerate, invariant, isPlainObject } from '@zenstackhq/common-helpers';
import type { Expression, ExpressionBuilder, ExpressionWrapper, SqlBool, ValueNode } from 'kysely';
import { expressionBuilder, sql, type SelectQueryBuilder } from 'kysely';
import { match, P } from 'ts-pattern';
import type { BuiltinType, DataSourceProviderType, FieldDef, GetModels, ModelDef, SchemaDef } from '../../../schema';
import { enumerate } from '../../../utils/enumerate';
import type { OrArray } from '../../../utils/type-utils';
import { AGGREGATE_OPERATORS, DELEGATE_JOINED_FIELD_PREFIX, LOGICAL_COMBINATORS } from '../../constants';
import type {
Expand Down Expand Up @@ -755,7 +754,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
: this.fieldRef(model, field, modelAlias);
};

enumerate(orderBy).forEach((orderBy) => {
enumerate(orderBy).forEach((orderBy, index) => {
for (const [field, value] of Object.entries<any>(orderBy)) {
if (!value) {
continue;
Expand Down Expand Up @@ -841,15 +840,16 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
}
} else {
// order by to-one relation
result = result.leftJoin(relationModel, (join) => {
const joinPairs = buildJoinPairs(this.schema, model, modelAlias, field, relationModel);
const joinAlias = `${modelAlias}$orderBy$${index}`;
result = result.leftJoin(`${relationModel} as ${joinAlias}`, (join) => {
const joinPairs = buildJoinPairs(this.schema, model, modelAlias, field, joinAlias);
return join.on((eb) =>
this.and(
...joinPairs.map(([left, right]) => eb(this.eb.ref(left), '=', this.eb.ref(right))),
),
);
});
result = this.buildOrderBy(result, fieldDef.type, relationModel, value, negated);
result = this.buildOrderBy(result, relationModel, joinAlias, value, negated);
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions packages/orm/src/client/crud/dialects/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends BaseCrudDiale
: value,
)
.with('Decimal', () => (value !== null ? value.toString() : value))
.with('Json', () => {
if (typeof value === 'string') {
// Postgres string JSON needs to be quoted
return `"${value}"`;
} else if (typeof value === 'number') {
// Postgres number JSON needs to be in string form
return value.toString();
} else {
return value;
}
})
.otherwise(() => value);
}
}
Expand Down
4 changes: 1 addition & 3 deletions packages/orm/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { createId } from '@paralleldrive/cuid2';
import { invariant, isPlainObject } from '@zenstackhq/common-helpers';
import { clone, enumerate, invariant, isPlainObject } from '@zenstackhq/common-helpers';
import {
DeleteResult,
expressionBuilder,
Expand All @@ -17,8 +17,6 @@ import * as uuid from 'uuid';
import type { ClientContract } from '../..';
import type { BuiltinType, Expression, FieldDef } from '../../../schema';
import { ExpressionUtils, type GetModels, type ModelDef, type SchemaDef } from '../../../schema';
import { clone } from '../../../utils/clone';
import { enumerate } from '../../../utils/enumerate';
import { extractFields, fieldsToSelectObject } from '../../../utils/object-utils';
import { NUMERIC_FIELD_TYPES } from '../../constants';
import { TransactionIsolationLevel, type CRUD } from '../../contract';
Expand Down
3 changes: 1 addition & 2 deletions packages/orm/src/client/crud/validator/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { invariant } from '@zenstackhq/common-helpers';
import { enumerate, invariant } from '@zenstackhq/common-helpers';
import Decimal from 'decimal.js';
import stableStringify from 'json-stable-stringify';
import { match, P } from 'ts-pattern';
Expand All @@ -12,7 +12,6 @@ import {
type ModelDef,
type SchemaDef,
} from '../../../schema';
import { enumerate } from '../../../utils/enumerate';
import { extractFields } from '../../../utils/object-utils';
import { formatError } from '../../../utils/zod-utils';
import { AGGREGATE_OPERATORS, LOGICAL_COMBINATORS, NUMERIC_FIELD_TYPES } from '../../constants';
Expand Down
13 changes: 9 additions & 4 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@
"@zenstackhq/orm": "workspace:*",
"decimal.js": "catalog:",
"superjson": "^2.2.3",
"ts-pattern": "catalog:"
"ts-japi": "^1.12.0",
"ts-pattern": "catalog:",
"url-pattern": "^1.0.3",
"zod-validation-error": "catalog:"
},
"devDependencies": {
"@types/body-parser": "^1.19.6",
Expand All @@ -66,13 +69,15 @@
"@zenstackhq/typescript-config": "workspace:*",
"@zenstackhq/vitest-config": "workspace:*",
"body-parser": "^2.2.0",
"supertest": "^7.1.4",
"express": "^5.0.0",
"next": "^15.0.0"
"next": "^15.0.0",
"supertest": "^7.1.4",
"zod": "~3.25.0"
},
"peerDependencies": {
"express": "^5.0.0",
"next": "^15.0.0"
"next": "^15.0.0",
"zod": "catalog:"
},
"peerDependenciesMeta": {
"express": {
Expand Down
3 changes: 2 additions & 1 deletion packages/server/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { RPCApiHandler } from './rpc';
export { RestApiHandler, type RestApiHandlerOptions } from './rest';
export { RPCApiHandler, type RPCApiHandlerOptions } from './rpc';
Loading