@stryke/prisma-trpc-generator 0.4.7 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2631,6 +2631,12 @@ function normalizeString(path6, allowAboveRoot) {
2631
2631
  }
2632
2632
  __name(normalizeString, "normalizeString");
2633
2633
 
2634
+ // ../string-format/src/lower-case-first.ts
2635
+ init_cjs_shims();
2636
+ var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
2637
+ return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
2638
+ }, "lowerCaseFirst");
2639
+
2634
2640
  // src/prisma-generator.ts
2635
2641
  var import_node_path6 = __toESM(require("node:path"), 1);
2636
2642
  var import_pluralize = __toESM(require_pluralize(), 1);
@@ -6853,8 +6859,10 @@ var ModelAction = /* @__PURE__ */ function(ModelAction2) {
6853
6859
  ModelAction2["findMany"] = "findMany";
6854
6860
  ModelAction2["create"] = "create";
6855
6861
  ModelAction2["createMany"] = "createMany";
6862
+ ModelAction2["createManyAndReturn"] = "createManyAndReturn";
6856
6863
  ModelAction2["update"] = "update";
6857
6864
  ModelAction2["updateMany"] = "updateMany";
6865
+ ModelAction2["updateManyAndReturn"] = "updateManyAndReturn";
6858
6866
  ModelAction2["upsert"] = "upsert";
6859
6867
  ModelAction2["delete"] = "delete";
6860
6868
  ModelAction2["deleteMany"] = "deleteMany";
@@ -6884,8 +6892,9 @@ var configSchema = z.object({
6884
6892
  withShield: configShield.default("true"),
6885
6893
  withZod: configBoolean.default("true"),
6886
6894
  contextPath: z.string().default("../src/trpc/context"),
6887
- trpcOptionsPath: z.string().optional(),
6895
+ trpcOptions: z.boolean().or(z.string()).optional(),
6888
6896
  showModelNameInProcedure: configBoolean.default("true"),
6897
+ useTRPCNext: configBoolean.default("false"),
6889
6898
  generateModelActions: z.string().default(Object.values(ModelAction).join(",")).transform((arg) => {
6890
6899
  return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
6891
6900
  })
@@ -6894,12 +6903,6 @@ var configSchema = z.object({
6894
6903
  // src/helpers.ts
6895
6904
  init_cjs_shims();
6896
6905
 
6897
- // ../string-format/src/lower-case-first.ts
6898
- init_cjs_shims();
6899
- var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
6900
- return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
6901
- }, "lowerCaseFirst");
6902
-
6903
6906
  // src/utils/get-prisma-internals.ts
6904
6907
  init_cjs_shims();
6905
6908
 
@@ -7644,24 +7647,35 @@ var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural,
7644
7647
  async function generateBaseRouter(sourceFile, config, options) {
7645
7648
  const internals = await getPrismaInternals();
7646
7649
  const outputDir = internals.parseEnvValue(options.generator.output);
7650
+ const relativeContextPath = getRelativePath(outputDir, config.contextPath, true, options.schemaPath);
7647
7651
  sourceFile.addStatements(
7648
7652
  /* ts */
7649
7653
  `
7650
- import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
7654
+ import type { Context } from '${relativeContextPath}';
7651
7655
  `
7652
7656
  );
7653
- if (config.trpcOptionsPath) {
7657
+ if (config.trpcOptions) {
7654
7658
  sourceFile.addStatements(
7655
7659
  /* ts */
7656
7660
  `
7657
- import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
7661
+ import trpcOptions from '${getRelativePath(outputDir, typeof config.trpcOptions === "boolean" ? joinPaths(outputDir, "options") : config.trpcOptions, true, options.schemaPath)}';
7658
7662
  `
7659
7663
  );
7660
7664
  }
7665
+ if (config.useTRPCNext) {
7666
+ sourceFile.addStatements(
7667
+ /* ts */
7668
+ `
7669
+ import { createContext } from '${relativeContextPath}';
7670
+ import { initTRPC, TRPCError } from '@trpc/server';
7671
+ import { createTRPCServerActionHandler } from '@stryke/trpc-next/action-handler';
7672
+ `
7673
+ );
7674
+ }
7661
7675
  sourceFile.addStatements(
7662
7676
  /* ts */
7663
7677
  `
7664
- export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
7678
+ export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptions ? "trpcOptions" : ""});
7665
7679
  `
7666
7680
  );
7667
7681
  const middlewares = [];
@@ -7717,11 +7731,44 @@ async function generateBaseRouter(sourceFile, config, options) {
7717
7731
  )
7718
7732
  });
7719
7733
  }
7734
+ sourceFile.addStatements(
7735
+ /* ts */
7736
+ `
7737
+ /**
7738
+ * Create a server-side caller
7739
+ * @see https://trpc.io/docs/server/server-side-calls
7740
+ */
7741
+ export const createCallerFactory = t.createCallerFactory;`
7742
+ );
7720
7743
  sourceFile.addStatements(
7721
7744
  /* ts */
7722
7745
  `
7723
7746
  export const publicProcedure = t.procedure; `
7724
7747
  );
7748
+ if (config.useTRPCNext) {
7749
+ sourceFile.addStatements(
7750
+ /* ts */
7751
+ `
7752
+ export const protectedProcedure = publicProcedure.use((opts) => {
7753
+ const { session } = opts.ctx;
7754
+
7755
+ if (!session?.user) {
7756
+ throw new TRPCError({
7757
+ code: 'UNAUTHORIZED',
7758
+ });
7759
+ }
7760
+
7761
+ return opts.next({ ctx: { session } });
7762
+ });
7763
+ `
7764
+ );
7765
+ sourceFile.addStatements(
7766
+ /* ts */
7767
+ `
7768
+ export const createAction = createTRPCServerActionHandler(t, createContext);
7769
+ `
7770
+ );
7771
+ }
7725
7772
  if (middlewares.length > 0) {
7726
7773
  const procName = getProcedureName(config);
7727
7774
  middlewares.forEach((middleware, i) => {
@@ -7795,6 +7842,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7795
7842
  case "createMany":
7796
7843
  inputType = `${modelName}CreateManySchema`;
7797
7844
  break;
7845
+ case "createManyAndReturn":
7846
+ inputType = `${modelName}CreateManyAndReturnSchema`;
7847
+ break;
7798
7848
  case "deleteOne":
7799
7849
  inputType = `${modelName}DeleteOneSchema`;
7800
7850
  break;
@@ -7807,6 +7857,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7807
7857
  case "updateMany":
7808
7858
  inputType = `${modelName}UpdateManySchema`;
7809
7859
  break;
7860
+ case "updateManyAndReturn":
7861
+ inputType = `${modelName}UpdateManyAndReturnSchema`;
7862
+ break;
7810
7863
  case "upsertOne":
7811
7864
  inputType = `${modelName}UpsertSchema`;
7812
7865
  break;
@@ -7841,10 +7894,12 @@ var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
7841
7894
  break;
7842
7895
  case "createOne":
7843
7896
  case "createMany":
7897
+ case "createManyAndReturn":
7844
7898
  case "deleteOne":
7845
7899
  case "updateOne":
7846
7900
  case "deleteMany":
7847
7901
  case "updateMany":
7902
+ case "updateManyAndReturn":
7848
7903
  case "upsertOne":
7849
7904
  procType = "mutation";
7850
7905
  break;
@@ -7935,7 +7990,9 @@ var constructShield = /* @__PURE__ */ __name(async ({ queries, mutations, subscr
7935
7990
  })},`;
7936
7991
  rootItems += subscriptionLinesWrapped;
7937
7992
  }
7938
- if (rootItems.length === 0) return "";
7993
+ if (rootItems.length === 0) {
7994
+ return "";
7995
+ }
7939
7996
  let shieldText = getImports("trpc-shield");
7940
7997
  const internals = await getPrismaInternals();
7941
7998
  const outputDir = internals.parseEnvValue(options.generator.output);
@@ -11602,6 +11659,27 @@ async function generate(options) {
11602
11659
  consoleLog("Skipping tRPC Shield generation");
11603
11660
  }
11604
11661
  consoleLog(`Generating tRPC source code for ${models.length} models`);
11662
+ if (config.trpcOptions && typeof config.trpcOptions === "boolean") {
11663
+ const trpcOptionsOutputPath = joinPaths(outputDir, "options.ts");
11664
+ consoleLog("Generating tRPC options source file");
11665
+ await writeFileSafely(trpcOptionsOutputPath, `import { ZodError } from 'zod';${config.useTRPCNext ? '\nimport { transformer } from "@stryke/trpc-next/shared";' : ""}
11666
+
11667
+ export default {${config.useTRPCNext ? "\n transformer," : ""}
11668
+ errorFormatter({ shape, error }) {
11669
+ return {
11670
+ ...shape,
11671
+ data: {
11672
+ ...shape.data,
11673
+ zodError:
11674
+ error.code === 'BAD_REQUEST' && error.cause instanceof ZodError
11675
+ ? error.cause.flatten()
11676
+ : null,
11677
+ },
11678
+ };
11679
+ },
11680
+ };
11681
+ `);
11682
+ }
11605
11683
  resolveModelsComments(models, hiddenModels);
11606
11684
  const createRouter = project.createSourceFile(import_node_path6.default.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
11607
11685
  overwrite: true
@@ -11630,12 +11708,19 @@ async function generate(options) {
11630
11708
  consoleLog(`Skipping model ${model} as it is hidden`);
11631
11709
  continue;
11632
11710
  }
11633
- const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
11711
+ if (!model) {
11712
+ consoleLog(`Skipping model ${model} as it is not defined`);
11713
+ continue;
11714
+ }
11715
+ const modelActions = Object.keys(operations).filter((opType) => (
11716
+ // eslint-disable-next-line unicorn/prefer-includes
11717
+ config.generateModelActions.some((generateModelAction) => generateModelAction === opType.replace("One", ""))
11718
+ ));
11634
11719
  if (!modelActions.length) {
11635
11720
  consoleLog(`Skipping model ${model} as it has no actions to generate`);
11636
11721
  continue;
11637
11722
  }
11638
- const plural = (0, import_pluralize.default)(model.toLowerCase());
11723
+ const plural = (0, import_pluralize.default)(lowerCaseFirst(model));
11639
11724
  consoleLog(`Generating tRPC router for model ${model}`);
11640
11725
  generateRouterImport(appRouter, plural, model);
11641
11726
  const modelRouter = project.createSourceFile(import_node_path6.default.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
package/dist/generator.js CHANGED
@@ -2636,6 +2636,12 @@ function normalizeString(path6, allowAboveRoot) {
2636
2636
  }
2637
2637
  __name(normalizeString, "normalizeString");
2638
2638
 
2639
+ // ../string-format/src/lower-case-first.ts
2640
+ init_esm_shims();
2641
+ var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
2642
+ return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
2643
+ }, "lowerCaseFirst");
2644
+
2639
2645
  // src/prisma-generator.ts
2640
2646
  var import_pluralize = __toESM(require_pluralize(), 1);
2641
2647
  import path5 from "node:path";
@@ -6858,8 +6864,10 @@ var ModelAction = /* @__PURE__ */ function(ModelAction2) {
6858
6864
  ModelAction2["findMany"] = "findMany";
6859
6865
  ModelAction2["create"] = "create";
6860
6866
  ModelAction2["createMany"] = "createMany";
6867
+ ModelAction2["createManyAndReturn"] = "createManyAndReturn";
6861
6868
  ModelAction2["update"] = "update";
6862
6869
  ModelAction2["updateMany"] = "updateMany";
6870
+ ModelAction2["updateManyAndReturn"] = "updateManyAndReturn";
6863
6871
  ModelAction2["upsert"] = "upsert";
6864
6872
  ModelAction2["delete"] = "delete";
6865
6873
  ModelAction2["deleteMany"] = "deleteMany";
@@ -6889,8 +6897,9 @@ var configSchema = z.object({
6889
6897
  withShield: configShield.default("true"),
6890
6898
  withZod: configBoolean.default("true"),
6891
6899
  contextPath: z.string().default("../src/trpc/context"),
6892
- trpcOptionsPath: z.string().optional(),
6900
+ trpcOptions: z.boolean().or(z.string()).optional(),
6893
6901
  showModelNameInProcedure: configBoolean.default("true"),
6902
+ useTRPCNext: configBoolean.default("false"),
6894
6903
  generateModelActions: z.string().default(Object.values(ModelAction).join(",")).transform((arg) => {
6895
6904
  return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
6896
6905
  })
@@ -6899,12 +6908,6 @@ var configSchema = z.object({
6899
6908
  // src/helpers.ts
6900
6909
  init_esm_shims();
6901
6910
 
6902
- // ../string-format/src/lower-case-first.ts
6903
- init_esm_shims();
6904
- var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
6905
- return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
6906
- }, "lowerCaseFirst");
6907
-
6908
6911
  // src/utils/get-prisma-internals.ts
6909
6912
  init_esm_shims();
6910
6913
 
@@ -7649,24 +7652,35 @@ var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural,
7649
7652
  async function generateBaseRouter(sourceFile, config, options) {
7650
7653
  const internals = await getPrismaInternals();
7651
7654
  const outputDir = internals.parseEnvValue(options.generator.output);
7655
+ const relativeContextPath = getRelativePath(outputDir, config.contextPath, true, options.schemaPath);
7652
7656
  sourceFile.addStatements(
7653
7657
  /* ts */
7654
7658
  `
7655
- import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
7659
+ import type { Context } from '${relativeContextPath}';
7656
7660
  `
7657
7661
  );
7658
- if (config.trpcOptionsPath) {
7662
+ if (config.trpcOptions) {
7659
7663
  sourceFile.addStatements(
7660
7664
  /* ts */
7661
7665
  `
7662
- import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
7666
+ import trpcOptions from '${getRelativePath(outputDir, typeof config.trpcOptions === "boolean" ? joinPaths(outputDir, "options") : config.trpcOptions, true, options.schemaPath)}';
7663
7667
  `
7664
7668
  );
7665
7669
  }
7670
+ if (config.useTRPCNext) {
7671
+ sourceFile.addStatements(
7672
+ /* ts */
7673
+ `
7674
+ import { createContext } from '${relativeContextPath}';
7675
+ import { initTRPC, TRPCError } from '@trpc/server';
7676
+ import { createTRPCServerActionHandler } from '@stryke/trpc-next/action-handler';
7677
+ `
7678
+ );
7679
+ }
7666
7680
  sourceFile.addStatements(
7667
7681
  /* ts */
7668
7682
  `
7669
- export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
7683
+ export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptions ? "trpcOptions" : ""});
7670
7684
  `
7671
7685
  );
7672
7686
  const middlewares = [];
@@ -7722,11 +7736,44 @@ async function generateBaseRouter(sourceFile, config, options) {
7722
7736
  )
7723
7737
  });
7724
7738
  }
7739
+ sourceFile.addStatements(
7740
+ /* ts */
7741
+ `
7742
+ /**
7743
+ * Create a server-side caller
7744
+ * @see https://trpc.io/docs/server/server-side-calls
7745
+ */
7746
+ export const createCallerFactory = t.createCallerFactory;`
7747
+ );
7725
7748
  sourceFile.addStatements(
7726
7749
  /* ts */
7727
7750
  `
7728
7751
  export const publicProcedure = t.procedure; `
7729
7752
  );
7753
+ if (config.useTRPCNext) {
7754
+ sourceFile.addStatements(
7755
+ /* ts */
7756
+ `
7757
+ export const protectedProcedure = publicProcedure.use((opts) => {
7758
+ const { session } = opts.ctx;
7759
+
7760
+ if (!session?.user) {
7761
+ throw new TRPCError({
7762
+ code: 'UNAUTHORIZED',
7763
+ });
7764
+ }
7765
+
7766
+ return opts.next({ ctx: { session } });
7767
+ });
7768
+ `
7769
+ );
7770
+ sourceFile.addStatements(
7771
+ /* ts */
7772
+ `
7773
+ export const createAction = createTRPCServerActionHandler(t, createContext);
7774
+ `
7775
+ );
7776
+ }
7730
7777
  if (middlewares.length > 0) {
7731
7778
  const procName = getProcedureName(config);
7732
7779
  middlewares.forEach((middleware, i) => {
@@ -7800,6 +7847,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7800
7847
  case "createMany":
7801
7848
  inputType = `${modelName}CreateManySchema`;
7802
7849
  break;
7850
+ case "createManyAndReturn":
7851
+ inputType = `${modelName}CreateManyAndReturnSchema`;
7852
+ break;
7803
7853
  case "deleteOne":
7804
7854
  inputType = `${modelName}DeleteOneSchema`;
7805
7855
  break;
@@ -7812,6 +7862,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7812
7862
  case "updateMany":
7813
7863
  inputType = `${modelName}UpdateManySchema`;
7814
7864
  break;
7865
+ case "updateManyAndReturn":
7866
+ inputType = `${modelName}UpdateManyAndReturnSchema`;
7867
+ break;
7815
7868
  case "upsertOne":
7816
7869
  inputType = `${modelName}UpsertSchema`;
7817
7870
  break;
@@ -7846,10 +7899,12 @@ var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
7846
7899
  break;
7847
7900
  case "createOne":
7848
7901
  case "createMany":
7902
+ case "createManyAndReturn":
7849
7903
  case "deleteOne":
7850
7904
  case "updateOne":
7851
7905
  case "deleteMany":
7852
7906
  case "updateMany":
7907
+ case "updateManyAndReturn":
7853
7908
  case "upsertOne":
7854
7909
  procType = "mutation";
7855
7910
  break;
@@ -7940,7 +7995,9 @@ var constructShield = /* @__PURE__ */ __name(async ({ queries, mutations, subscr
7940
7995
  })},`;
7941
7996
  rootItems += subscriptionLinesWrapped;
7942
7997
  }
7943
- if (rootItems.length === 0) return "";
7998
+ if (rootItems.length === 0) {
7999
+ return "";
8000
+ }
7944
8001
  let shieldText = getImports("trpc-shield");
7945
8002
  const internals = await getPrismaInternals();
7946
8003
  const outputDir = internals.parseEnvValue(options.generator.output);
@@ -11607,6 +11664,27 @@ async function generate(options) {
11607
11664
  consoleLog("Skipping tRPC Shield generation");
11608
11665
  }
11609
11666
  consoleLog(`Generating tRPC source code for ${models.length} models`);
11667
+ if (config.trpcOptions && typeof config.trpcOptions === "boolean") {
11668
+ const trpcOptionsOutputPath = joinPaths(outputDir, "options.ts");
11669
+ consoleLog("Generating tRPC options source file");
11670
+ await writeFileSafely(trpcOptionsOutputPath, `import { ZodError } from 'zod';${config.useTRPCNext ? '\nimport { transformer } from "@stryke/trpc-next/shared";' : ""}
11671
+
11672
+ export default {${config.useTRPCNext ? "\n transformer," : ""}
11673
+ errorFormatter({ shape, error }) {
11674
+ return {
11675
+ ...shape,
11676
+ data: {
11677
+ ...shape.data,
11678
+ zodError:
11679
+ error.code === 'BAD_REQUEST' && error.cause instanceof ZodError
11680
+ ? error.cause.flatten()
11681
+ : null,
11682
+ },
11683
+ };
11684
+ },
11685
+ };
11686
+ `);
11687
+ }
11610
11688
  resolveModelsComments(models, hiddenModels);
11611
11689
  const createRouter = project.createSourceFile(path5.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
11612
11690
  overwrite: true
@@ -11635,12 +11713,19 @@ async function generate(options) {
11635
11713
  consoleLog(`Skipping model ${model} as it is hidden`);
11636
11714
  continue;
11637
11715
  }
11638
- const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
11716
+ if (!model) {
11717
+ consoleLog(`Skipping model ${model} as it is not defined`);
11718
+ continue;
11719
+ }
11720
+ const modelActions = Object.keys(operations).filter((opType) => (
11721
+ // eslint-disable-next-line unicorn/prefer-includes
11722
+ config.generateModelActions.some((generateModelAction) => generateModelAction === opType.replace("One", ""))
11723
+ ));
11639
11724
  if (!modelActions.length) {
11640
11725
  consoleLog(`Skipping model ${model} as it has no actions to generate`);
11641
11726
  continue;
11642
11727
  }
11643
- const plural = (0, import_pluralize.default)(model.toLowerCase());
11728
+ const plural = (0, import_pluralize.default)(lowerCaseFirst(model));
11644
11729
  consoleLog(`Generating tRPC router for model ${model}`);
11645
11730
  generateRouterImport(appRouter, plural, model);
11646
11731
  const modelRouter = project.createSourceFile(path5.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
package/dist/index.cjs CHANGED
@@ -2627,6 +2627,12 @@ function normalizeString(path6, allowAboveRoot) {
2627
2627
  }
2628
2628
  __name(normalizeString, "normalizeString");
2629
2629
 
2630
+ // ../string-format/src/lower-case-first.ts
2631
+ init_cjs_shims();
2632
+ var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
2633
+ return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
2634
+ }, "lowerCaseFirst");
2635
+
2630
2636
  // src/prisma-generator.ts
2631
2637
  var import_node_path6 = __toESM(require("node:path"), 1);
2632
2638
  var import_pluralize = __toESM(require_pluralize(), 1);
@@ -6849,8 +6855,10 @@ var ModelAction = /* @__PURE__ */ function(ModelAction2) {
6849
6855
  ModelAction2["findMany"] = "findMany";
6850
6856
  ModelAction2["create"] = "create";
6851
6857
  ModelAction2["createMany"] = "createMany";
6858
+ ModelAction2["createManyAndReturn"] = "createManyAndReturn";
6852
6859
  ModelAction2["update"] = "update";
6853
6860
  ModelAction2["updateMany"] = "updateMany";
6861
+ ModelAction2["updateManyAndReturn"] = "updateManyAndReturn";
6854
6862
  ModelAction2["upsert"] = "upsert";
6855
6863
  ModelAction2["delete"] = "delete";
6856
6864
  ModelAction2["deleteMany"] = "deleteMany";
@@ -6880,8 +6888,9 @@ var configSchema = z.object({
6880
6888
  withShield: configShield.default("true"),
6881
6889
  withZod: configBoolean.default("true"),
6882
6890
  contextPath: z.string().default("../src/trpc/context"),
6883
- trpcOptionsPath: z.string().optional(),
6891
+ trpcOptions: z.boolean().or(z.string()).optional(),
6884
6892
  showModelNameInProcedure: configBoolean.default("true"),
6893
+ useTRPCNext: configBoolean.default("false"),
6885
6894
  generateModelActions: z.string().default(Object.values(ModelAction).join(",")).transform((arg) => {
6886
6895
  return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
6887
6896
  })
@@ -6890,12 +6899,6 @@ var configSchema = z.object({
6890
6899
  // src/helpers.ts
6891
6900
  init_cjs_shims();
6892
6901
 
6893
- // ../string-format/src/lower-case-first.ts
6894
- init_cjs_shims();
6895
- var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
6896
- return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
6897
- }, "lowerCaseFirst");
6898
-
6899
6902
  // src/utils/get-prisma-internals.ts
6900
6903
  init_cjs_shims();
6901
6904
 
@@ -7640,24 +7643,35 @@ var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural,
7640
7643
  async function generateBaseRouter(sourceFile, config, options) {
7641
7644
  const internals = await getPrismaInternals();
7642
7645
  const outputDir = internals.parseEnvValue(options.generator.output);
7646
+ const relativeContextPath = getRelativePath(outputDir, config.contextPath, true, options.schemaPath);
7643
7647
  sourceFile.addStatements(
7644
7648
  /* ts */
7645
7649
  `
7646
- import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
7650
+ import type { Context } from '${relativeContextPath}';
7647
7651
  `
7648
7652
  );
7649
- if (config.trpcOptionsPath) {
7653
+ if (config.trpcOptions) {
7650
7654
  sourceFile.addStatements(
7651
7655
  /* ts */
7652
7656
  `
7653
- import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
7657
+ import trpcOptions from '${getRelativePath(outputDir, typeof config.trpcOptions === "boolean" ? joinPaths(outputDir, "options") : config.trpcOptions, true, options.schemaPath)}';
7654
7658
  `
7655
7659
  );
7656
7660
  }
7661
+ if (config.useTRPCNext) {
7662
+ sourceFile.addStatements(
7663
+ /* ts */
7664
+ `
7665
+ import { createContext } from '${relativeContextPath}';
7666
+ import { initTRPC, TRPCError } from '@trpc/server';
7667
+ import { createTRPCServerActionHandler } from '@stryke/trpc-next/action-handler';
7668
+ `
7669
+ );
7670
+ }
7657
7671
  sourceFile.addStatements(
7658
7672
  /* ts */
7659
7673
  `
7660
- export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
7674
+ export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptions ? "trpcOptions" : ""});
7661
7675
  `
7662
7676
  );
7663
7677
  const middlewares = [];
@@ -7713,11 +7727,44 @@ async function generateBaseRouter(sourceFile, config, options) {
7713
7727
  )
7714
7728
  });
7715
7729
  }
7730
+ sourceFile.addStatements(
7731
+ /* ts */
7732
+ `
7733
+ /**
7734
+ * Create a server-side caller
7735
+ * @see https://trpc.io/docs/server/server-side-calls
7736
+ */
7737
+ export const createCallerFactory = t.createCallerFactory;`
7738
+ );
7716
7739
  sourceFile.addStatements(
7717
7740
  /* ts */
7718
7741
  `
7719
7742
  export const publicProcedure = t.procedure; `
7720
7743
  );
7744
+ if (config.useTRPCNext) {
7745
+ sourceFile.addStatements(
7746
+ /* ts */
7747
+ `
7748
+ export const protectedProcedure = publicProcedure.use((opts) => {
7749
+ const { session } = opts.ctx;
7750
+
7751
+ if (!session?.user) {
7752
+ throw new TRPCError({
7753
+ code: 'UNAUTHORIZED',
7754
+ });
7755
+ }
7756
+
7757
+ return opts.next({ ctx: { session } });
7758
+ });
7759
+ `
7760
+ );
7761
+ sourceFile.addStatements(
7762
+ /* ts */
7763
+ `
7764
+ export const createAction = createTRPCServerActionHandler(t, createContext);
7765
+ `
7766
+ );
7767
+ }
7721
7768
  if (middlewares.length > 0) {
7722
7769
  const procName = getProcedureName(config);
7723
7770
  middlewares.forEach((middleware, i) => {
@@ -7791,6 +7838,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7791
7838
  case "createMany":
7792
7839
  inputType = `${modelName}CreateManySchema`;
7793
7840
  break;
7841
+ case "createManyAndReturn":
7842
+ inputType = `${modelName}CreateManyAndReturnSchema`;
7843
+ break;
7794
7844
  case "deleteOne":
7795
7845
  inputType = `${modelName}DeleteOneSchema`;
7796
7846
  break;
@@ -7803,6 +7853,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7803
7853
  case "updateMany":
7804
7854
  inputType = `${modelName}UpdateManySchema`;
7805
7855
  break;
7856
+ case "updateManyAndReturn":
7857
+ inputType = `${modelName}UpdateManyAndReturnSchema`;
7858
+ break;
7806
7859
  case "upsertOne":
7807
7860
  inputType = `${modelName}UpsertSchema`;
7808
7861
  break;
@@ -7837,10 +7890,12 @@ var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
7837
7890
  break;
7838
7891
  case "createOne":
7839
7892
  case "createMany":
7893
+ case "createManyAndReturn":
7840
7894
  case "deleteOne":
7841
7895
  case "updateOne":
7842
7896
  case "deleteMany":
7843
7897
  case "updateMany":
7898
+ case "updateManyAndReturn":
7844
7899
  case "upsertOne":
7845
7900
  procType = "mutation";
7846
7901
  break;
@@ -7931,7 +7986,9 @@ var constructShield = /* @__PURE__ */ __name(async ({ queries, mutations, subscr
7931
7986
  })},`;
7932
7987
  rootItems += subscriptionLinesWrapped;
7933
7988
  }
7934
- if (rootItems.length === 0) return "";
7989
+ if (rootItems.length === 0) {
7990
+ return "";
7991
+ }
7935
7992
  let shieldText = getImports("trpc-shield");
7936
7993
  const internals = await getPrismaInternals();
7937
7994
  const outputDir = internals.parseEnvValue(options.generator.output);
@@ -11598,6 +11655,27 @@ async function generate(options) {
11598
11655
  consoleLog("Skipping tRPC Shield generation");
11599
11656
  }
11600
11657
  consoleLog(`Generating tRPC source code for ${models.length} models`);
11658
+ if (config.trpcOptions && typeof config.trpcOptions === "boolean") {
11659
+ const trpcOptionsOutputPath = joinPaths(outputDir, "options.ts");
11660
+ consoleLog("Generating tRPC options source file");
11661
+ await writeFileSafely(trpcOptionsOutputPath, `import { ZodError } from 'zod';${config.useTRPCNext ? '\nimport { transformer } from "@stryke/trpc-next/shared";' : ""}
11662
+
11663
+ export default {${config.useTRPCNext ? "\n transformer," : ""}
11664
+ errorFormatter({ shape, error }) {
11665
+ return {
11666
+ ...shape,
11667
+ data: {
11668
+ ...shape.data,
11669
+ zodError:
11670
+ error.code === 'BAD_REQUEST' && error.cause instanceof ZodError
11671
+ ? error.cause.flatten()
11672
+ : null,
11673
+ },
11674
+ };
11675
+ },
11676
+ };
11677
+ `);
11678
+ }
11601
11679
  resolveModelsComments(models, hiddenModels);
11602
11680
  const createRouter = project.createSourceFile(import_node_path6.default.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
11603
11681
  overwrite: true
@@ -11626,12 +11704,19 @@ async function generate(options) {
11626
11704
  consoleLog(`Skipping model ${model} as it is hidden`);
11627
11705
  continue;
11628
11706
  }
11629
- const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
11707
+ if (!model) {
11708
+ consoleLog(`Skipping model ${model} as it is not defined`);
11709
+ continue;
11710
+ }
11711
+ const modelActions = Object.keys(operations).filter((opType) => (
11712
+ // eslint-disable-next-line unicorn/prefer-includes
11713
+ config.generateModelActions.some((generateModelAction) => generateModelAction === opType.replace("One", ""))
11714
+ ));
11630
11715
  if (!modelActions.length) {
11631
11716
  consoleLog(`Skipping model ${model} as it has no actions to generate`);
11632
11717
  continue;
11633
11718
  }
11634
- const plural = (0, import_pluralize.default)(model.toLowerCase());
11719
+ const plural = (0, import_pluralize.default)(lowerCaseFirst(model));
11635
11720
  consoleLog(`Generating tRPC router for model ${model}`);
11636
11721
  generateRouterImport(appRouter, plural, model);
11637
11722
  const modelRouter = project.createSourceFile(import_node_path6.default.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
package/dist/index.js CHANGED
@@ -2632,6 +2632,12 @@ function normalizeString(path6, allowAboveRoot) {
2632
2632
  }
2633
2633
  __name(normalizeString, "normalizeString");
2634
2634
 
2635
+ // ../string-format/src/lower-case-first.ts
2636
+ init_esm_shims();
2637
+ var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
2638
+ return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
2639
+ }, "lowerCaseFirst");
2640
+
2635
2641
  // src/prisma-generator.ts
2636
2642
  var import_pluralize = __toESM(require_pluralize(), 1);
2637
2643
  import path5 from "node:path";
@@ -6854,8 +6860,10 @@ var ModelAction = /* @__PURE__ */ function(ModelAction2) {
6854
6860
  ModelAction2["findMany"] = "findMany";
6855
6861
  ModelAction2["create"] = "create";
6856
6862
  ModelAction2["createMany"] = "createMany";
6863
+ ModelAction2["createManyAndReturn"] = "createManyAndReturn";
6857
6864
  ModelAction2["update"] = "update";
6858
6865
  ModelAction2["updateMany"] = "updateMany";
6866
+ ModelAction2["updateManyAndReturn"] = "updateManyAndReturn";
6859
6867
  ModelAction2["upsert"] = "upsert";
6860
6868
  ModelAction2["delete"] = "delete";
6861
6869
  ModelAction2["deleteMany"] = "deleteMany";
@@ -6885,8 +6893,9 @@ var configSchema = z.object({
6885
6893
  withShield: configShield.default("true"),
6886
6894
  withZod: configBoolean.default("true"),
6887
6895
  contextPath: z.string().default("../src/trpc/context"),
6888
- trpcOptionsPath: z.string().optional(),
6896
+ trpcOptions: z.boolean().or(z.string()).optional(),
6889
6897
  showModelNameInProcedure: configBoolean.default("true"),
6898
+ useTRPCNext: configBoolean.default("false"),
6890
6899
  generateModelActions: z.string().default(Object.values(ModelAction).join(",")).transform((arg) => {
6891
6900
  return arg.split(",").map((action) => modelActionEnum.parse(action.trim()));
6892
6901
  })
@@ -6895,12 +6904,6 @@ var configSchema = z.object({
6895
6904
  // src/helpers.ts
6896
6905
  init_esm_shims();
6897
6906
 
6898
- // ../string-format/src/lower-case-first.ts
6899
- init_esm_shims();
6900
- var lowerCaseFirst = /* @__PURE__ */ __name((input) => {
6901
- return input ? input.charAt(0).toLowerCase() + input.slice(1) : input;
6902
- }, "lowerCaseFirst");
6903
-
6904
6907
  // src/utils/get-prisma-internals.ts
6905
6908
  init_esm_shims();
6906
6909
 
@@ -7645,24 +7648,35 @@ var generateRouterImport = /* @__PURE__ */ __name((sourceFile, modelNamePlural,
7645
7648
  async function generateBaseRouter(sourceFile, config, options) {
7646
7649
  const internals = await getPrismaInternals();
7647
7650
  const outputDir = internals.parseEnvValue(options.generator.output);
7651
+ const relativeContextPath = getRelativePath(outputDir, config.contextPath, true, options.schemaPath);
7648
7652
  sourceFile.addStatements(
7649
7653
  /* ts */
7650
7654
  `
7651
- import type { Context } from '${getRelativePath(outputDir, config.contextPath, true, options.schemaPath)}';
7655
+ import type { Context } from '${relativeContextPath}';
7652
7656
  `
7653
7657
  );
7654
- if (config.trpcOptionsPath) {
7658
+ if (config.trpcOptions) {
7655
7659
  sourceFile.addStatements(
7656
7660
  /* ts */
7657
7661
  `
7658
- import trpcOptions from '${getRelativePath(outputDir, config.trpcOptionsPath, true, options.schemaPath)}';
7662
+ import trpcOptions from '${getRelativePath(outputDir, typeof config.trpcOptions === "boolean" ? joinPaths(outputDir, "options") : config.trpcOptions, true, options.schemaPath)}';
7659
7663
  `
7660
7664
  );
7661
7665
  }
7666
+ if (config.useTRPCNext) {
7667
+ sourceFile.addStatements(
7668
+ /* ts */
7669
+ `
7670
+ import { createContext } from '${relativeContextPath}';
7671
+ import { initTRPC, TRPCError } from '@trpc/server';
7672
+ import { createTRPCServerActionHandler } from '@stryke/trpc-next/action-handler';
7673
+ `
7674
+ );
7675
+ }
7662
7676
  sourceFile.addStatements(
7663
7677
  /* ts */
7664
7678
  `
7665
- export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptionsPath ? "trpcOptions" : ""});
7679
+ export const t = trpc.initTRPC.context<Context>().create(${config.trpcOptions ? "trpcOptions" : ""});
7666
7680
  `
7667
7681
  );
7668
7682
  const middlewares = [];
@@ -7718,11 +7732,44 @@ async function generateBaseRouter(sourceFile, config, options) {
7718
7732
  )
7719
7733
  });
7720
7734
  }
7735
+ sourceFile.addStatements(
7736
+ /* ts */
7737
+ `
7738
+ /**
7739
+ * Create a server-side caller
7740
+ * @see https://trpc.io/docs/server/server-side-calls
7741
+ */
7742
+ export const createCallerFactory = t.createCallerFactory;`
7743
+ );
7721
7744
  sourceFile.addStatements(
7722
7745
  /* ts */
7723
7746
  `
7724
7747
  export const publicProcedure = t.procedure; `
7725
7748
  );
7749
+ if (config.useTRPCNext) {
7750
+ sourceFile.addStatements(
7751
+ /* ts */
7752
+ `
7753
+ export const protectedProcedure = publicProcedure.use((opts) => {
7754
+ const { session } = opts.ctx;
7755
+
7756
+ if (!session?.user) {
7757
+ throw new TRPCError({
7758
+ code: 'UNAUTHORIZED',
7759
+ });
7760
+ }
7761
+
7762
+ return opts.next({ ctx: { session } });
7763
+ });
7764
+ `
7765
+ );
7766
+ sourceFile.addStatements(
7767
+ /* ts */
7768
+ `
7769
+ export const createAction = createTRPCServerActionHandler(t, createContext);
7770
+ `
7771
+ );
7772
+ }
7726
7773
  if (middlewares.length > 0) {
7727
7774
  const procName = getProcedureName(config);
7728
7775
  middlewares.forEach((middleware, i) => {
@@ -7796,6 +7843,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7796
7843
  case "createMany":
7797
7844
  inputType = `${modelName}CreateManySchema`;
7798
7845
  break;
7846
+ case "createManyAndReturn":
7847
+ inputType = `${modelName}CreateManyAndReturnSchema`;
7848
+ break;
7799
7849
  case "deleteOne":
7800
7850
  inputType = `${modelName}DeleteOneSchema`;
7801
7851
  break;
@@ -7808,6 +7858,9 @@ var getInputTypeByOpName = /* @__PURE__ */ __name((opName, modelName) => {
7808
7858
  case "updateMany":
7809
7859
  inputType = `${modelName}UpdateManySchema`;
7810
7860
  break;
7861
+ case "updateManyAndReturn":
7862
+ inputType = `${modelName}UpdateManyAndReturnSchema`;
7863
+ break;
7811
7864
  case "upsertOne":
7812
7865
  inputType = `${modelName}UpsertSchema`;
7813
7866
  break;
@@ -7842,10 +7895,12 @@ var getProcedureTypeByOpName = /* @__PURE__ */ __name((opName) => {
7842
7895
  break;
7843
7896
  case "createOne":
7844
7897
  case "createMany":
7898
+ case "createManyAndReturn":
7845
7899
  case "deleteOne":
7846
7900
  case "updateOne":
7847
7901
  case "deleteMany":
7848
7902
  case "updateMany":
7903
+ case "updateManyAndReturn":
7849
7904
  case "upsertOne":
7850
7905
  procType = "mutation";
7851
7906
  break;
@@ -7936,7 +7991,9 @@ var constructShield = /* @__PURE__ */ __name(async ({ queries, mutations, subscr
7936
7991
  })},`;
7937
7992
  rootItems += subscriptionLinesWrapped;
7938
7993
  }
7939
- if (rootItems.length === 0) return "";
7994
+ if (rootItems.length === 0) {
7995
+ return "";
7996
+ }
7940
7997
  let shieldText = getImports("trpc-shield");
7941
7998
  const internals = await getPrismaInternals();
7942
7999
  const outputDir = internals.parseEnvValue(options.generator.output);
@@ -11603,6 +11660,27 @@ async function generate(options) {
11603
11660
  consoleLog("Skipping tRPC Shield generation");
11604
11661
  }
11605
11662
  consoleLog(`Generating tRPC source code for ${models.length} models`);
11663
+ if (config.trpcOptions && typeof config.trpcOptions === "boolean") {
11664
+ const trpcOptionsOutputPath = joinPaths(outputDir, "options.ts");
11665
+ consoleLog("Generating tRPC options source file");
11666
+ await writeFileSafely(trpcOptionsOutputPath, `import { ZodError } from 'zod';${config.useTRPCNext ? '\nimport { transformer } from "@stryke/trpc-next/shared";' : ""}
11667
+
11668
+ export default {${config.useTRPCNext ? "\n transformer," : ""}
11669
+ errorFormatter({ shape, error }) {
11670
+ return {
11671
+ ...shape,
11672
+ data: {
11673
+ ...shape.data,
11674
+ zodError:
11675
+ error.code === 'BAD_REQUEST' && error.cause instanceof ZodError
11676
+ ? error.cause.flatten()
11677
+ : null,
11678
+ },
11679
+ };
11680
+ },
11681
+ };
11682
+ `);
11683
+ }
11606
11684
  resolveModelsComments(models, hiddenModels);
11607
11685
  const createRouter = project.createSourceFile(path5.resolve(outputDir, "routers", "helpers", "createRouter.ts"), void 0, {
11608
11686
  overwrite: true
@@ -11631,12 +11709,19 @@ async function generate(options) {
11631
11709
  consoleLog(`Skipping model ${model} as it is hidden`);
11632
11710
  continue;
11633
11711
  }
11634
- const modelActions = Object.keys(operations).filter((opType) => config.generateModelActions.includes(opType.replace("One", "")));
11712
+ if (!model) {
11713
+ consoleLog(`Skipping model ${model} as it is not defined`);
11714
+ continue;
11715
+ }
11716
+ const modelActions = Object.keys(operations).filter((opType) => (
11717
+ // eslint-disable-next-line unicorn/prefer-includes
11718
+ config.generateModelActions.some((generateModelAction) => generateModelAction === opType.replace("One", ""))
11719
+ ));
11635
11720
  if (!modelActions.length) {
11636
11721
  consoleLog(`Skipping model ${model} as it has no actions to generate`);
11637
11722
  continue;
11638
11723
  }
11639
- const plural = (0, import_pluralize.default)(model.toLowerCase());
11724
+ const plural = (0, import_pluralize.default)(lowerCaseFirst(model));
11640
11725
  consoleLog(`Generating tRPC router for model ${model}`);
11641
11726
  generateRouterImport(appRouter, plural, model);
11642
11727
  const modelRouter = project.createSourceFile(path5.resolve(outputDir, "routers", `${model}.router.ts`), void 0, {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@stryke/prisma-trpc-generator",
3
- "version": "0.4.7",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "A fork of the prisma-trpc-generator code to work in ESM with Prisma v6.",
6
6
  "repository": {