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-monorepo",
"version": "2.19.1",
"version": "2.19.2",
"description": "",
"scripts": {
"build": "pnpm -r --filter=\"!./packages/ide/*\" build",
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ plugins {
}

group = "dev.zenstack"
version = "2.19.1"
version = "2.19.2"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion packages/ide/jetbrains/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jetbrains",
"version": "2.19.1",
"version": "2.19.2",
"displayName": "ZenStack JetBrains IDE Plugin",
"description": "ZenStack JetBrains IDE plugin",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/language/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/language",
"version": "2.19.1",
"version": "2.19.2",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/misc/redwood/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/redwood",
"displayName": "ZenStack RedwoodJS Integration",
"version": "2.19.1",
"version": "2.19.2",
"description": "CLI and runtime for integrating ZenStack with RedwoodJS projects.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/openapi/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/openapi",
"displayName": "ZenStack Plugin and Runtime for OpenAPI",
"version": "2.19.1",
"version": "2.19.2",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/swr/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/swr",
"displayName": "ZenStack plugin for generating SWR hooks",
"version": "2.19.1",
"version": "2.19.2",
"description": "ZenStack plugin for generating SWR hooks",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/tanstack-query/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/tanstack-query",
"displayName": "ZenStack plugin for generating tanstack-query hooks",
"version": "2.19.1",
"version": "2.19.2",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"exports": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/trpc/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/trpc",
"displayName": "ZenStack plugin for tRPC",
"version": "2.19.1",
"version": "2.19.2",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/runtime",
"displayName": "ZenStack Runtime Library",
"version": "2.19.1",
"version": "2.19.2",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/cross/model-meta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type RuntimeAttribute = {
/**
* Attribute arguments
*/
args: Array<{ name?: string; value: unknown }>;
args: Array<{ name?: string; value?: unknown }>;
};

/**
Expand Down
3 changes: 3 additions & 0 deletions packages/runtime/src/enhancements/node/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
data[field] = {};
}
await this.injectSelectIncludeHierarchy(fieldInfo.type, data[field]);
if (data[field].where) {
this.injectWhereHierarchy(fieldInfo.type, data[field].where);
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/schema/build/bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ const watch = process.argv.includes('--watch');
const minify = process.argv.includes('--minify');
const success = watch ? 'Watch build succeeded' : 'Build succeeded';
const fs = require('fs');
const path = require('path');
require('dotenv').config({ path: './.env.local' });
require('dotenv').config({ path: './.env' });

// Replace telemetry token in generated bundle files after building
function replaceTelemetryTokenInBundle() {
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "FullStack enhancement for Prisma ORM: seamless integration from database to UI",
"version": "2.19.1",
"version": "2.19.2",
"author": {
"name": "ZenStack Team"
},
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": "2.19.1",
"version": "2.19.2",
"description": "ZenStack plugin development SDK",
"main": "index.js",
"scripts": {
Expand Down
11 changes: 9 additions & 2 deletions packages/sdk/src/model-meta-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,20 @@ export function generate(
) {
const sf = project.createSourceFile(options.output, undefined, { overwrite: true });

// generate: import type { ModelMeta } from '@zenstackhq/runtime';
sf.addImportDeclaration({
isTypeOnly: true,
namedImports: ['ModelMeta'],
moduleSpecifier: '@zenstackhq/runtime',
});

const writer = new FastWriter();
const extraFunctions: OptionalKind<FunctionDeclarationStructure>[] = [];
generateModelMetadata(models, typeDefs, writer, options, extraFunctions);

sf.addVariableStatement({
declarationKind: VariableDeclarationKind.Const,
declarations: [{ name: 'metadata', initializer: writer.result }],
declarations: [{ name: 'metadata', type: 'ModelMeta', initializer: writer.result }],
});

if (extraFunctions.length > 0) {
Expand Down Expand Up @@ -364,7 +371,7 @@ function writeFields(
function getAttributes(target: DataModelField | DataModel | TypeDefField): RuntimeAttribute[] {
return target.attributes
.map((attr) => {
const args: Array<{ name?: string; value: unknown }> = [];
const args: Array<{ name?: string; value?: unknown }> = [];
for (const arg of attr.args) {
const argName = arg.$resolvedParam?.name ?? arg.name;
const argValue = exprToValue(arg.value);
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": "2.19.1",
"version": "2.19.2",
"displayName": "ZenStack Server-side Adapters",
"description": "ZenStack server-side adapters",
"homepage": "https://zenstack.dev",
Expand Down
101 changes: 93 additions & 8 deletions packages/server/src/api/rest/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class RequestHandler extends APIHandlerBase {
status: 400,
title: 'Invalid value for type',
},
duplicatedFieldsParameter: {
status: 400,
title: 'Fields Parameter Duplicated',
},
forbidden: {
status: 403,
title: 'Operation is forbidden',
Expand All @@ -185,6 +189,7 @@ class RequestHandler extends APIHandlerBase {
status: 422,
title: 'Operation is unprocessable due to validation errors',
},

unknownError: {
status: 400,
title: 'Unknown error',
Expand Down Expand Up @@ -511,7 +516,7 @@ class RequestHandler extends APIHandlerBase {
// handle "include" query parameter
let include: string[] | undefined;
if (query?.include) {
const { select, error, allIncludes } = this.buildRelationSelect(type, query.include);
const { select, error, allIncludes } = this.buildRelationSelect(type, query.include, query);
if (error) {
return error;
}
Expand All @@ -521,6 +526,20 @@ class RequestHandler extends APIHandlerBase {
include = allIncludes;
}

// handle partial results for requested type
const { select, error } = this.buildPartialSelect(type, query);
if (error) return error;
if (select) {
args.select = { ...select, ...args.select };
if (args.include) {
args.select = {
...args.select,
...args.include,
};
args.include = undefined;
}
}

const entity = await prisma[type].findUnique(args);

if (entity) {
Expand Down Expand Up @@ -555,7 +574,7 @@ class RequestHandler extends APIHandlerBase {
// handle "include" query parameter
let include: string[] | undefined;
if (query?.include) {
const { select: relationSelect, error, allIncludes } = this.buildRelationSelect(type, query.include);
const { select: relationSelect, error, allIncludes } = this.buildRelationSelect(type, query.include, query);
if (error) {
return error;
}
Expand All @@ -566,7 +585,14 @@ class RequestHandler extends APIHandlerBase {
select = relationSelect;
}

select = select ?? { [relationship]: true };
// handle partial results for requested type
if (!select) {
const { select: partialFields, error } = this.buildPartialSelect(lowerCaseFirst(relationInfo.type), query);
if (error) return error;

select = partialFields ? { [relationship]: { select: { ...partialFields } } } : { [relationship]: true };
}

const args: any = {
where: this.makePrismaIdFilter(typeInfo.idFields, resourceId),
select,
Expand Down Expand Up @@ -710,7 +736,7 @@ class RequestHandler extends APIHandlerBase {
// handle "include" query parameter
let include: string[] | undefined;
if (query?.include) {
const { select, error, allIncludes } = this.buildRelationSelect(type, query.include);
const { select, error, allIncludes } = this.buildRelationSelect(type, query.include, query);
if (error) {
return error;
}
Expand All @@ -720,6 +746,20 @@ class RequestHandler extends APIHandlerBase {
include = allIncludes;
}

// handle partial results for requested type
const { select, error } = this.buildPartialSelect(type, query);
if (error) return error;
if (select) {
args.select = { ...select, ...args.select };
if (args.include) {
args.select = {
...args.select,
...args.include,
};
args.include = undefined;
}
}

const { offset, limit } = this.getPagination(query);
if (offset > 0) {
args.skip = offset;
Expand All @@ -738,6 +778,7 @@ class RequestHandler extends APIHandlerBase {
};
} else {
args.take = limit;

const [entities, count] = await Promise.all([
prisma[type].findMany(args),
prisma[type].count({ where: args.where ?? {} }),
Expand All @@ -762,6 +803,33 @@ class RequestHandler extends APIHandlerBase {
}
}

private buildPartialSelect(type: string, query: Record<string, string | string[]> | undefined) {
const selectFieldsQuery = query?.[`fields[${type}]`];
if (!selectFieldsQuery) {
return { select: undefined, error: undefined };
}

if (Array.isArray(selectFieldsQuery)) {
return {
select: undefined,
error: this.makeError('duplicatedFieldsParameter', `duplicated fields query for type ${type}`),
};
}

const typeInfo = this.typeMap[lowerCaseFirst(type)];
if (!typeInfo) {
return { select: undefined, error: this.makeUnsupportedModelError(type) };
}

const selectFieldNames = selectFieldsQuery.split(',').filter((i) => i);

const fields = selectFieldNames.reduce((acc, curr) => ({ ...acc, [curr]: true }), {});

return {
select: { ...this.makeIdSelect(typeInfo.idFields), ...fields },
};
}

private addTotalCountToMeta(meta: any, total: any) {
return meta ? Object.assign(meta, { total }) : Object.assign({}, { total });
}
Expand Down Expand Up @@ -1790,7 +1858,11 @@ class RequestHandler extends APIHandlerBase {
return { sort: result, error: undefined };
}

private buildRelationSelect(type: string, include: string | string[]) {
private buildRelationSelect(
type: string,
include: string | string[],
query: Record<string, string | string[]> | undefined
) {
const typeInfo = this.typeMap[lowerCaseFirst(type)];
if (!typeInfo) {
return { select: undefined, error: this.makeUnsupportedModelError(type) };
Expand Down Expand Up @@ -1820,11 +1892,24 @@ class RequestHandler extends APIHandlerBase {
return { select: undefined, error: this.makeUnsupportedModelError(relationInfo.type) };
}

// handle partial results for requested type
const { select, error } = this.buildPartialSelect(lowerCaseFirst(relationInfo.type), query);
if (error) return { select: undefined, error };

if (i !== parts.length - 1) {
currPayload[relation] = { include: { ...currPayload[relation]?.include } };
currPayload = currPayload[relation].include;
if (select) {
currPayload[relation] = { select: { ...select } };
currPayload = currPayload[relation].select;
} else {
currPayload[relation] = { include: { ...currPayload[relation]?.include } };
currPayload = currPayload[relation].include;
}
} else {
currPayload[relation] = true;
currPayload[relation] = select
? {
select: { ...select },
}
: true;
}
}
}
Expand Down
Loading
Loading