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 .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:

strategy:
matrix:
node-version: [16.x]
node-version: [18.x]

steps:
- uses: actions/checkout@v3
Expand Down
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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "",
"scripts": {
"build": "pnpm -r build",
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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"displayName": "ZenStack modeling language compiler",
"description": "ZenStack modeling language compiler",
"homepage": "https://zenstack.dev",
Expand Down
2 changes: 1 addition & 1 deletion packages/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zenstackhq/next",
"version": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"displayName": "ZenStack Next.js integration",
"description": "ZenStack Next.js integration",
"homepage": "https://zenstack.dev",
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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "ZenStack plugin and runtime supporting OpenAPI",
"main": "index.js",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/react/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@zenstackhq/react",
"displayName": "ZenStack plugin and runtime for ReactJS",
"version": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "ZenStack plugin and runtime for ReactJS",
"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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "ZenStack plugin for generating tanstack-query hooks",
"main": "index.js",
"repository": {
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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "ZenStack plugin for tRPC",
"main": "index.js",
"repository": {
Expand Down
7 changes: 0 additions & 7 deletions packages/plugins/trpc/src/config.ts

This file was deleted.

51 changes: 43 additions & 8 deletions packages/plugins/trpc/src/generator.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DMMF } from '@prisma/generator-helper';
import {
CrudFailureReason,
PluginError,
PluginOptions,
RUNTIME_PACKAGE,
requireOption,
Expand All @@ -12,6 +13,7 @@ import { promises as fs } from 'fs';
import { lowerCaseFirst } from 'lower-case-first';
import path from 'path';
import { Project } from 'ts-morph';
import { name } from '.';
import {
generateHelperImport,
generateProcedure,
Expand All @@ -27,6 +29,29 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
let outDir = requireOption<string>(options, 'output');
outDir = resolvePath(outDir, options);

// resolve "generateModelActions" option
let generateModelActions: string[] | undefined = undefined;
if (options.generateModelActions) {
if (typeof options.generateModelActions === 'string') {
// comma separated string
generateModelActions = options.generateModelActions
.split(',')
.filter((i) => !!i)
.map((i) => i.trim());
} else if (
Array.isArray(options.generateModelActions) &&
options.generateModelActions.every((i) => typeof i === 'string')
) {
// string array
generateModelActions = options.generateModelActions as string[];
} else {
throw new PluginError(
name,
`Invalid "generateModelActions" option: must be a comma-separated string or an array of strings`
);
}
}

await fs.mkdir(outDir, { recursive: true });
await removeDir(outDir, true);

Expand All @@ -39,13 +64,18 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
const hiddenModels: string[] = [];
resolveModelsComments(models, hiddenModels);

createAppRouter(outDir, modelOperations, hiddenModels);
createAppRouter(outDir, modelOperations, hiddenModels, generateModelActions);
createHelper(outDir);

await saveProject(project);
}

function createAppRouter(outDir: string, modelOperations: DMMF.ModelMapping[], hiddenModels: string[]) {
function createAppRouter(
outDir: string,
modelOperations: DMMF.ModelMapping[],
hiddenModels: string[],
generateModelActions: string[] | undefined
) {
const appRouter = project.createSourceFile(path.resolve(outDir, 'routers', `index.ts`), undefined, {
overwrite: true,
});
Expand Down Expand Up @@ -109,7 +139,7 @@ function createAppRouter(outDir: string, modelOperations: DMMF.ModelMapping[], h
continue;
}

generateModelCreateRouter(project, model, operations, outDir);
generateModelCreateRouter(project, model, operations, outDir, generateModelActions);

appRouter.addImportDeclaration({
defaultImport: `create${model}Router`,
Expand All @@ -129,7 +159,8 @@ function generateModelCreateRouter(
project: Project,
model: string,
operations: Record<string, string | undefined | null>,
outputDir: string
outputDir: string,
generateModelActions: string[] | undefined
) {
const modelRouter = project.createSourceFile(path.resolve(outputDir, 'routers', `${model}.router.ts`), undefined, {
overwrite: true,
Expand Down Expand Up @@ -162,11 +193,15 @@ function generateModelCreateRouter(
writer.block(() => {
for (const [opType, opNameWithModel] of Object.entries(operations)) {
const baseOpType = opType.replace('OrThrow', '');

const inputType = getInputTypeByOpName(baseOpType, model);

if (opNameWithModel && inputType) {
generateProcedure(writer, opType.replace(/One$/, ''), inputType, model, baseOpType);
const generateOpName = opType.replace(/One$/, '');

if (
opNameWithModel &&
inputType &&
(!generateModelActions || generateModelActions.includes(generateOpName))
) {
generateProcedure(writer, generateOpName, inputType, model, baseOpType);
}
}
});
Expand Down
Empty file removed packages/plugins/trpc/src/types.ts
Empty file.
56 changes: 56 additions & 0 deletions packages/plugins/trpc/tests/trpc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,60 @@ model Post {
);
expect(fs.existsSync(path.join(projectDir, 'zenstack/trpc'))).toBe(true);
});

it('generateModelActions option string', async () => {
const { projectDir } = await loadSchema(
`
plugin trpc {
provider = '${process.cwd()}/dist'
output = './trpc'
generateModelActions = 'findMany,findUnique,update'
}

model Post {
id String @id
title String
}
`,
true,
false,
[`${origDir}/dist`, '@trpc/client', '@trpc/server'],
true,
'zenstack/schema.zmodel'
);
const content = fs.readFileSync(path.join(projectDir, 'zenstack/trpc/routers/Post.router.ts'), 'utf-8');
expect(content).toContain('findMany:');
expect(content).toContain('findUnique:');
expect(content).toContain('update:');
expect(content).not.toContain('create:');
expect(content).not.toContain('aggregate:');
});

it('generateModelActions option array', async () => {
const { projectDir } = await loadSchema(
`
plugin trpc {
provider = '${process.cwd()}/dist'
output = './trpc'
generateModelActions = ['findMany', 'findUnique', 'update']
}

model Post {
id String @id
title String
}
`,
true,
false,
[`${origDir}/dist`, '@trpc/client', '@trpc/server'],
true,
'zenstack/schema.zmodel'
);
const content = fs.readFileSync(path.join(projectDir, 'zenstack/trpc/routers/Post.router.ts'), 'utf-8');
expect(content).toContain('findMany:');
expect(content).toContain('findUnique:');
expect(content).toContain('update:');
expect(content).not.toContain('create:');
expect(content).not.toContain('aggregate:');
});
});
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": "1.0.0-alpha.126",
"version": "1.0.0-beta.1",
"description": "Runtime of ZenStack for both client-side and server-side environments.",
"repository": {
"type": "git",
Expand Down
14 changes: 12 additions & 2 deletions packages/runtime/src/enhancements/omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ import { DefaultPrismaProxyHandler, makeProxy } from './proxy';
import { ModelMeta } from './types';
import { enumerate, getModelFields } from './utils';

/**
* Options for @see withOmit
*/
export type WithOmitOptions = {
/**
* Model metatadata
*/
modelMeta?: ModelMeta;
};

/**
* Gets an enhanced Prisma client that supports @omit attribute.
*/
export function withOmit<DbClient extends object>(prisma: DbClient, modelMeta?: ModelMeta): DbClient {
const _modelMeta = modelMeta ?? getDefaultModelMeta();
export function withOmit<DbClient extends object>(prisma: DbClient, options?: WithOmitOptions): DbClient {
const _modelMeta = options?.modelMeta ?? getDefaultModelMeta();
return makeProxy(
prisma,
_modelMeta,
Expand Down
14 changes: 12 additions & 2 deletions packages/runtime/src/enhancements/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,21 @@ import { NestedWriteVisitor } from './nested-write-vistor';
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy';
import { ModelMeta } from './types';

/**
* Options for @see withPassword
*/
export type WithPasswordOptions = {
/**
* Model metatadata
*/
modelMeta?: ModelMeta;
};

/**
* Gets an enhanced Prisma client that supports @password attribute.
*/
export function withPassword<DbClient extends object = any>(prisma: DbClient, modelMeta?: ModelMeta): DbClient {
const _modelMeta = modelMeta ?? getDefaultModelMeta();
export function withPassword<DbClient extends object = any>(prisma: DbClient, options?: WithPasswordOptions): DbClient {
const _modelMeta = options?.modelMeta ?? getDefaultModelMeta();
return makeProxy(
prisma,
_modelMeta,
Expand Down
Loading