Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zenstack-v3",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack",
"packageManager": "pnpm@10.23.0",
"type": "module",
Expand Down
2 changes: 1 addition & 1 deletion packages/auth-adapters/better-auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/better-auth",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Better Auth Adapter. This adapter is modified from better-auth's Prisma adapter.",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack CLI",
"description": "FullStack database toolkit with built-in access control and automatic API generation.",
"version": "3.4.5",
"version": "3.4.6",
"type": "module",
"author": {
"name": "ZenStack Team"
Expand Down
2 changes: 1 addition & 1 deletion packages/clients/client-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/client-helpers",
"version": "3.4.5",
"version": "3.4.6",
"description": "Helpers for implementing clients that consume ZenStack's CRUD service",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/clients/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/tanstack-query",
"version": "3.4.5",
"version": "3.4.6",
"description": "TanStack Query Client for consuming ZenStack v3's CRUD service",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/common-helpers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/common-helpers",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Common Helpers",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/config/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/eslint-config",
"version": "3.4.5",
"version": "3.4.6",
"type": "module",
"private": true,
"license": "MIT"
Expand Down
2 changes: 1 addition & 1 deletion packages/config/typescript-config/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/typescript-config",
"version": "3.4.5",
"version": "3.4.6",
"private": true,
"license": "MIT"
}
2 changes: 1 addition & 1 deletion packages/config/vitest-config/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/vitest-config",
"type": "module",
"version": "3.4.5",
"version": "3.4.6",
"private": true,
"license": "MIT",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/create-zenstack/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "create-zenstack",
"version": "3.4.5",
"version": "3.4.6",
"description": "Create a new ZenStack project",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/vscode/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "zenstack-v3",
"publisher": "zenstack",
"version": "3.4.5",
"version": "3.4.6",
"displayName": "ZenStack V3 Language Tools",
"description": "VSCode extension for ZenStack (v3) ZModel language",
"private": true,
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/language",
"description": "ZenStack ZModel language specification",
"version": "3.4.5",
"version": "3.4.6",
"license": "MIT",
"author": "ZenStack Team",
"files": [
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/orm",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack ORM",
"type": "module",
"scripts": {
Expand Down
18 changes: 14 additions & 4 deletions packages/orm/src/client/crud/dialects/base-dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -757,11 +757,21 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
}

protected buildJsonEqualityFilter(lhs: Expression<any>, rhs: unknown) {
return this.buildLiteralFilter(lhs, 'Json', rhs);
return this.buildValueFilter(lhs, 'Json', rhs);
}

private buildLiteralFilter(lhs: Expression<any>, type: BuiltinType, rhs: unknown) {
return this.eb(lhs, '=', rhs !== null && rhs !== undefined ? this.transformInput(rhs, type, false) : rhs);
private buildValueFilter(lhs: Expression<any>, type: BuiltinType, rhs: unknown) {
if (rhs === undefined) {
// undefined filter is no-op, always true
return this.true();
}

if (rhs === null) {
// null comparison
return this.eb(lhs, 'is', null);
}

return this.eb(lhs, '=', this.transformInput(rhs, type, false));
}
Comment thread
ymc9 marked this conversation as resolved.

private buildStandardFilter(
Expand All @@ -776,7 +786,7 @@ export abstract class BaseCrudDialect<Schema extends SchemaDef> {
) {
if (payload === null || !isPlainObject(payload)) {
return {
conditions: [this.buildLiteralFilter(lhs, type, payload)],
conditions: [this.buildValueFilter(lhs, type, payload)],
consumedKeys: [],
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/orm/src/client/crud/dialects/postgresql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export class PostgresCrudDialect<Schema extends SchemaDef> extends LateralJoinDi
// override node-pg's default type parser to resolve the timezone handling issue
// with "TIMESTAMP WITHOUT TIME ZONE" fields
// https://github.com/brianc/node-postgres/issues/429
import('pg')
import(/* webpackIgnore: true */ 'pg') // suppress bundler analysis warnings
.then((pg) => {
pg.types.setTypeParser(pg.types.builtins.TIMESTAMP, (value) => {
if (typeof value !== 'string') {
Expand Down
1 change: 1 addition & 0 deletions packages/orm/src/client/crud/operations/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1105,6 +1105,7 @@ export abstract class BaseOperationHandler<Schema extends SchemaDef> {
return this.formatGeneratedValue(generated, defaultValue.args?.[1]);
})
.with('ulid', () => this.formatGeneratedValue(ulid(), defaultValue.args?.[0]))
.with('now', () => new Date())
.otherwise(() => undefined);
} else if (
ExpressionUtils.isMember(defaultValue) &&
Expand Down
14 changes: 13 additions & 1 deletion packages/orm/src/client/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,19 @@ export const isEmpty: ZModelFunction<any> = (eb, args, { dialect }: ZModelFuncti
return eb(dialect.buildArrayLength(field), '=', sql.lit(0));
};

export const now: ZModelFunction<any> = () => sql.raw('CURRENT_TIMESTAMP');
export const now: ZModelFunction<any> = (_eb, _args, context) =>
match(context.dialect.provider)
// SQLite stores DateTime as ISO 8601 text ('YYYY-MM-DDTHH:MM:SS.sssZ'), but
// CURRENT_TIMESTAMP returns 'YYYY-MM-DD HH:MM:SS'. Use strftime for ISO format.
.with('sqlite', () => sql.raw("strftime('%Y-%m-%dT%H:%M:%fZ')"))
// MySQL stores DateTime as ISO 8601 text ('YYYY-MM-DDTHH:MM:SS.sss+00:00').
// Use CONCAT + SUBSTRING to produce matching 3-digit millisecond ISO format.
.with('mysql', () =>
sql.raw("CONCAT(SUBSTRING(DATE_FORMAT(UTC_TIMESTAMP(3), '%Y-%m-%dT%H:%i:%s.%f'), 1, 23), '+00:00')"),
)
// PostgreSQL has native timestamp type that compares correctly.
.with('postgresql', () => sql.raw('CURRENT_TIMESTAMP'))
.exhaustive();

export const currentModel: ZModelFunction<any> = (_eb, args, { model }: ZModelFunctionContext<any>) => {
let result = model;
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/policy/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/plugin-policy",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Policy Plugin",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/schema",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Runtime Schema",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/sdk",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack SDK",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/server/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/server",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack automatic CRUD API handlers and server adapters",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/testtools/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/testtools",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Test Tools",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion packages/zod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/zod",
"version": "3.4.5",
"version": "3.4.6",
"description": "ZenStack Zod integration",
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion samples/orm/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sample-orm",
"version": "3.4.5",
"version": "3.4.6",
"description": "",
"main": "index.js",
"private": true,
Expand Down
24 changes: 24 additions & 0 deletions tests/e2e/orm/client-api/filter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,18 @@ describe('Client filter tests ', () => {
where: { email: { not: { not: { contains: 'test' } } } },
}),
).toResolveTruthy();

// not null (issue #2472)
await expect(
client.user.findMany({
where: { name: { not: null } },
}),
).toResolveWithLength(1);
await expect(
client.user.findFirst({
where: { name: { not: null } },
}),
).resolves.toMatchObject({ id: user1.id });
});

it('supports numeric filters', async () => {
Expand Down Expand Up @@ -490,6 +502,18 @@ describe('Client filter tests ', () => {
where: { age: { not: { not: { equals: null } } } },
}),
).toResolveTruthy();

// not null shorthand (issue #2472)
await expect(
client.profile.findMany({
where: { age: { not: null } },
}),
).toResolveWithLength(1);
await expect(
client.profile.findFirst({
where: { age: { not: null } },
}),
).resolves.toMatchObject({ id: '1' });
});

it('supports boolean filters', async () => {
Expand Down
125 changes: 125 additions & 0 deletions tests/e2e/orm/policy/now-function.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { createPolicyTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

describe('now() function in policy tests', () => {
it('allows create when createdAt default now() is used in comparison', async () => {
const db = await createPolicyTestClient(
`
model Post {
id Int @id @default(autoincrement())
title String
createdAt DateTime? @default(now())
@@allow('create', createdAt != null)
@@allow('read', true)
}
`,
);

// createdAt should be auto-filled with now(), satisfying the <= now() check
await expect(db.post.create({ data: { title: 'hello' } })).resolves.toMatchObject({ title: 'hello' });
const post = await db.post.findFirst();
expect(post.createdAt).toBeInstanceOf(Date);
});

it('uses now() in update policy to compare against DateTime field', async () => {
const db = await createPolicyTestClient(
`
model Event {
id Int @id @default(autoincrement())
name String
scheduledAt DateTime
@@allow('create,read', true)
@@allow('update', scheduledAt > now())
}
`,
);

// create an event in the future - should be updatable
const futureDate = new Date(Date.now() + 60 * 60 * 1000);
await db.event.create({ data: { name: 'future', scheduledAt: futureDate } });
await expect(db.event.update({ where: { id: 1 }, data: { name: 'updated' } })).resolves.toMatchObject({
name: 'updated',
});

// create an event in the past - should NOT be updatable
const pastDate = new Date(Date.now() - 60 * 60 * 1000);
await db.event.create({ data: { name: 'past', scheduledAt: pastDate } });
await expect(db.event.update({ where: { id: 2 }, data: { name: 'updated' } })).toBeRejectedNotFound();
});

it('uses now() in read policy to filter DateTime field', async () => {
const db = await createPolicyTestClient(
`
model Article {
id Int @id @default(autoincrement())
title String
publishedAt DateTime
@@allow('create', true)
@@allow('read', publishedAt <= now())
}
`,
);

const rawDb = db.$unuseAll();
const pastDate = new Date(Date.now() - 60 * 60 * 1000);
const futureDate = new Date(Date.now() + 60 * 60 * 1000);

await rawDb.article.create({ data: { title: 'published', publishedAt: pastDate } });
await rawDb.article.create({ data: { title: 'scheduled', publishedAt: futureDate } });

// only the past article should be readable
const articles = await db.article.findMany();
expect(articles).toHaveLength(1);
expect(articles[0].title).toBe('published');
});

it('uses now() in delete policy', async () => {
const db = await createPolicyTestClient(
`
model Task {
id Int @id @default(autoincrement())
name String
expiresAt DateTime
@@allow('create,read', true)
@@allow('delete', expiresAt < now())
}
`,
);

// create an expired task - should be deletable
const pastDate = new Date(Date.now() - 60 * 60 * 1000);
await db.task.create({ data: { name: 'expired', expiresAt: pastDate } });
await expect(db.task.delete({ where: { id: 1 } })).resolves.toMatchObject({ name: 'expired' });

// create a non-expired task - should NOT be deletable
const futureDate = new Date(Date.now() + 60 * 60 * 1000);
await db.task.create({ data: { name: 'active', expiresAt: futureDate } });
await expect(db.task.delete({ where: { id: 2 } })).toBeRejectedNotFound();
});

it('combines now() default with auth in create policy', async () => {
const db = await createPolicyTestClient(
`
type Auth {
id Int
@@auth
}

model Log {
id Int @id @default(autoincrement())
message String
createdAt DateTime @default(now())
@@allow('create', createdAt <= now() && auth() != null)
@@allow('read', true)
}
`,
);

// anonymous user - rejected
await expect(db.log.create({ data: { message: 'test' } })).toBeRejectedByPolicy();
// authenticated user with auto-filled createdAt - allowed
await expect(db.$setAuth({ id: 1 }).log.create({ data: { message: 'test' } })).resolves.toMatchObject({
message: 'test',
});
});
});
Loading
Loading