@cedarjs/internal 4.0.1-next.0 → 4.1.0-rc.70

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.
@@ -2,5 +2,5 @@ import type { BuildOptions } from 'esbuild';
2
2
  export declare const buildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
3
3
  export declare const rebuildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
4
4
  export declare const cleanApiBuild: () => Promise<void>;
5
- export declare const transpileApi: (files: string[]) => Promise<import("esbuild").BuildResult<BuildOptions>>;
5
+ export declare const buildApiWithVite: () => Promise<import("rollup").RollupOutput | import("rollup").RollupOutput[] | import("rollup").RollupWatcher>;
6
6
  //# sourceMappingURL=api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAatE,eAAO,MAAM,QAAQ,4DAOpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAQtB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AAiCD,eAAO,MAAM,YAAY,GAAU,OAAO,MAAM,EAAE,yDAEjD,CAAA"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/build/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAetE,eAAO,MAAM,QAAQ,4DAIpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAMtB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AAqED,eAAO,MAAM,gBAAgB,iHAiD5B,CAAA"}
package/dist/build/api.js CHANGED
@@ -1,5 +1,7 @@
1
1
  import fs from "node:fs";
2
+ import path from "node:path";
2
3
  import { build, context } from "esbuild";
4
+ import { build as viteBuild, normalizePath } from "vite";
3
5
  import {
4
6
  getApiSideBabelPlugins,
5
7
  transformWithBabel
@@ -45,6 +47,78 @@ const runCedarBabelTransformsPlugin = {
45
47
  });
46
48
  }
47
49
  };
50
+ function createCedarViteApiPlugin() {
51
+ const cedarConfig = getConfig();
52
+ const isEsm = projectSideIsEsm("api");
53
+ return {
54
+ name: "cedar-vite-api-babel-transform",
55
+ async transform(_code, id) {
56
+ if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
57
+ return null;
58
+ }
59
+ if (id.includes("node_modules")) {
60
+ return null;
61
+ }
62
+ const cedarPaths = getPaths();
63
+ if (!normalizePath(id).startsWith(normalizePath(cedarPaths.api.base))) {
64
+ return null;
65
+ }
66
+ const transformedCode = await transformWithBabel(
67
+ id,
68
+ getApiSideBabelPlugins({
69
+ openTelemetry: cedarConfig.experimental.opentelemetry.enabled && cedarConfig.experimental.opentelemetry.wrapApi,
70
+ projectIsEsm: isEsm
71
+ })
72
+ );
73
+ if (transformedCode?.code) {
74
+ return {
75
+ code: transformedCode.code,
76
+ map: transformedCode.map ?? null
77
+ };
78
+ }
79
+ throw new Error(`Could not transform file: ${id}`);
80
+ }
81
+ };
82
+ }
83
+ const buildApiWithVite = async () => {
84
+ const cedarPaths = getPaths();
85
+ const isEsm = projectSideIsEsm("api");
86
+ const format = isEsm ? "es" : "cjs";
87
+ const apiFiles = findApiFiles();
88
+ const input = {};
89
+ for (const f of apiFiles) {
90
+ const key = path.relative(cedarPaths.api.src, f).replace(/\.(ts|tsx|mts|js|jsx|mjs)$/, "");
91
+ input[key] = f;
92
+ }
93
+ return viteBuild({
94
+ root: cedarPaths.api.base,
95
+ logLevel: "warn",
96
+ build: {
97
+ ssr: true,
98
+ sourcemap: true,
99
+ outDir: cedarPaths.api.dist,
100
+ rollupOptions: {
101
+ input,
102
+ output: {
103
+ format,
104
+ preserveModules: true,
105
+ preserveModulesRoot: cedarPaths.api.src,
106
+ entryFileNames: "[name].js"
107
+ },
108
+ external: (id) => {
109
+ if (id.startsWith("node:")) {
110
+ return true;
111
+ }
112
+ if (!id.startsWith(".") && !path.isAbsolute(id)) {
113
+ return true;
114
+ }
115
+ return false;
116
+ }
117
+ }
118
+ },
119
+ plugins: [createCedarViteApiPlugin()]
120
+ });
121
+ };
48
122
  const transpileApi = async (files) => {
49
123
  return build(getEsbuildOptions(files));
50
124
  };
@@ -69,7 +143,7 @@ function getEsbuildOptions(files) {
69
143
  }
70
144
  export {
71
145
  buildApi,
146
+ buildApiWithVite,
72
147
  cleanApiBuild,
73
- rebuildApi,
74
- transpileApi
148
+ rebuildApi
75
149
  };
@@ -2,5 +2,5 @@ import type { BuildOptions } from 'esbuild';
2
2
  export declare const buildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
3
3
  export declare const rebuildApi: () => Promise<import("esbuild").BuildResult<BuildOptions>>;
4
4
  export declare const cleanApiBuild: () => Promise<void>;
5
- export declare const transpileApi: (files: string[]) => Promise<import("esbuild").BuildResult<BuildOptions>>;
5
+ export declare const buildApiWithVite: () => Promise<import("rollup").RollupOutput | import("rollup").RollupOutput[] | import("rollup").RollupWatcher>;
6
6
  //# sourceMappingURL=api.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/build/api.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAatE,eAAO,MAAM,QAAQ,4DAOpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAQtB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AAiCD,eAAO,MAAM,YAAY,GAAU,OAAO,MAAM,EAAE,yDAEjD,CAAA"}
1
+ {"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../../src/build/api.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAgB,YAAY,EAAe,MAAM,SAAS,CAAA;AAetE,eAAO,MAAM,QAAQ,4DAIpB,CAAA;AAED,eAAO,MAAM,UAAU,4DAMtB,CAAA;AAED,eAAO,MAAM,aAAa,qBAGzB,CAAA;AAqED,eAAO,MAAM,gBAAgB,iHAiD5B,CAAA"}
@@ -29,13 +29,15 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
29
29
  var api_exports = {};
30
30
  __export(api_exports, {
31
31
  buildApi: () => buildApi,
32
+ buildApiWithVite: () => buildApiWithVite,
32
33
  cleanApiBuild: () => cleanApiBuild,
33
- rebuildApi: () => rebuildApi,
34
- transpileApi: () => transpileApi
34
+ rebuildApi: () => rebuildApi
35
35
  });
36
36
  module.exports = __toCommonJS(api_exports);
37
37
  var import_node_fs = __toESM(require("node:fs"), 1);
38
+ var import_node_path = __toESM(require("node:path"), 1);
38
39
  var import_esbuild = require("esbuild");
40
+ var import_vite = require("vite");
39
41
  var import_babel_config = require("@cedarjs/babel-config");
40
42
  var import_project_config = require("@cedarjs/project-config");
41
43
  var import_files = require("../files.js");
@@ -78,6 +80,78 @@ const runCedarBabelTransformsPlugin = {
78
80
  });
79
81
  }
80
82
  };
83
+ function createCedarViteApiPlugin() {
84
+ const cedarConfig = (0, import_project_config.getConfig)();
85
+ const isEsm = (0, import_project_config.projectSideIsEsm)("api");
86
+ return {
87
+ name: "cedar-vite-api-babel-transform",
88
+ async transform(_code, id) {
89
+ if (!/\.(js|ts|tsx|jsx)$/.test(id)) {
90
+ return null;
91
+ }
92
+ if (id.includes("node_modules")) {
93
+ return null;
94
+ }
95
+ const cedarPaths = (0, import_project_config.getPaths)();
96
+ if (!(0, import_vite.normalizePath)(id).startsWith((0, import_vite.normalizePath)(cedarPaths.api.base))) {
97
+ return null;
98
+ }
99
+ const transformedCode = await (0, import_babel_config.transformWithBabel)(
100
+ id,
101
+ (0, import_babel_config.getApiSideBabelPlugins)({
102
+ openTelemetry: cedarConfig.experimental.opentelemetry.enabled && cedarConfig.experimental.opentelemetry.wrapApi,
103
+ projectIsEsm: isEsm
104
+ })
105
+ );
106
+ if (transformedCode?.code) {
107
+ return {
108
+ code: transformedCode.code,
109
+ map: transformedCode.map ?? null
110
+ };
111
+ }
112
+ throw new Error(`Could not transform file: ${id}`);
113
+ }
114
+ };
115
+ }
116
+ const buildApiWithVite = async () => {
117
+ const cedarPaths = (0, import_project_config.getPaths)();
118
+ const isEsm = (0, import_project_config.projectSideIsEsm)("api");
119
+ const format = isEsm ? "es" : "cjs";
120
+ const apiFiles = (0, import_files.findApiFiles)();
121
+ const input = {};
122
+ for (const f of apiFiles) {
123
+ const key = import_node_path.default.relative(cedarPaths.api.src, f).replace(/\.(ts|tsx|mts|js|jsx|mjs)$/, "");
124
+ input[key] = f;
125
+ }
126
+ return (0, import_vite.build)({
127
+ root: cedarPaths.api.base,
128
+ logLevel: "warn",
129
+ build: {
130
+ ssr: true,
131
+ sourcemap: true,
132
+ outDir: cedarPaths.api.dist,
133
+ rollupOptions: {
134
+ input,
135
+ output: {
136
+ format,
137
+ preserveModules: true,
138
+ preserveModulesRoot: cedarPaths.api.src,
139
+ entryFileNames: "[name].js"
140
+ },
141
+ external: (id) => {
142
+ if (id.startsWith("node:")) {
143
+ return true;
144
+ }
145
+ if (!id.startsWith(".") && !import_node_path.default.isAbsolute(id)) {
146
+ return true;
147
+ }
148
+ return false;
149
+ }
150
+ }
151
+ },
152
+ plugins: [createCedarViteApiPlugin()]
153
+ });
154
+ };
81
155
  const transpileApi = async (files) => {
82
156
  return (0, import_esbuild.build)(getEsbuildOptions(files));
83
157
  };
@@ -103,7 +177,7 @@ function getEsbuildOptions(files) {
103
177
  // Annotate the CommonJS export names for ESM import in node:
104
178
  0 && (module.exports = {
105
179
  buildApi,
180
+ buildApiWithVite,
106
181
  cleanApiBuild,
107
- rebuildApi,
108
- transpileApi
182
+ rebuildApi
109
183
  });
@@ -1 +1 @@
1
- {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../../src/generate/generate.ts"],"names":[],"mappings":";AAWA,eAAO,MAAM,QAAQ;;;;;;EA6CpB,CAAA;AAED,eAAO,MAAM,GAAG,qBAoCf,CAAA"}
1
+ {"version":3,"file":"generate.d.ts","sourceRoot":"","sources":["../../../src/generate/generate.ts"],"names":[],"mappings":";AAWA,eAAO,MAAM,QAAQ;;;;;;EA2CpB,CAAA;AAED,eAAO,MAAM,GAAG,qBAoCf,CAAA"}
@@ -36,7 +36,7 @@ const generate = async () => {
36
36
  const { typeDefFiles, errors: generateTypeDefsErrors } = await (0, import_typeDefinitions.generateTypeDefs)();
37
37
  const clientPresetFiles = [];
38
38
  const { possibleTypesFiles, errors: generatePossibleTypesErrors } = await (0, import_possibleTypes.generatePossibleTypes)();
39
- const { files: gqlormFiles, errors: gqlormErrors } = config.experimental?.gqlorm?.enabled ? await (0, import_gqlormSchema.generateGqlormArtifacts)() : { files: [], errors: [] };
39
+ const { files: gqlormFiles, errors: gqlormErrors } = await (0, import_gqlormSchema.generateGqlormArtifacts)();
40
40
  if (config.graphql.trustedDocuments) {
41
41
  const preset = await (0, import_clientPreset.generateClientPreset)();
42
42
  clientPresetFiles.push(...preset.clientPresetFiles);
@@ -1,5 +1,43 @@
1
1
  import type * as DMMF from '@prisma/dmmf';
2
2
  type ModelSchema = Record<string, string[]>;
3
+ interface FrontendFieldInfo {
4
+ name: string;
5
+ tsType: string;
6
+ }
7
+ interface FrontendModelInfo {
8
+ modelName: string;
9
+ camelName: string;
10
+ fields: FrontendFieldInfo[];
11
+ }
12
+ export interface BackendFieldInfo {
13
+ name: string;
14
+ graphqlType: string;
15
+ isRequired: boolean;
16
+ isId: boolean;
17
+ }
18
+ export interface BackendModelInfo {
19
+ modelName: string;
20
+ camelName: string;
21
+ pluralName: string;
22
+ fields: BackendFieldInfo[];
23
+ idField: BackendFieldInfo | undefined;
24
+ }
25
+ export interface GqlormBackendConfig {
26
+ membershipModel: string;
27
+ membershipModelCamel: string;
28
+ membershipUserField: string;
29
+ membershipOrganizationField: string;
30
+ membershipModelExists: boolean;
31
+ }
32
+ /**
33
+ * Map a DMMF field type to its GraphQL SDL equivalent.
34
+ *
35
+ * Enum fields (kind === 'enum') are mapped to String. Unknown scalar types
36
+ * fall back to String.
37
+ */
38
+ export declare function mapDmmfTypeToGraphql(type: string, kind: string): string;
39
+ export declare function buildFrontendModelInfo(dmmf: DMMF.Document): FrontendModelInfo[];
40
+ export declare function generateWebGqlormModelsContent(models: FrontendModelInfo[]): string;
3
41
  /**
4
42
  * Build a ModelSchema from a Prisma DMMF document.
5
43
  *
@@ -7,12 +45,57 @@ type ModelSchema = Record<string, string[]>;
7
45
  * with a mock DMMF object.
8
46
  */
9
47
  export declare function buildModelSchema(dmmf: DMMF.Document): ModelSchema;
48
+ /**
49
+ * Build enriched model information from the DMMF, applying the same visibility
50
+ * rules as `buildModelSchema()` but also collecting type, nullability, and
51
+ * @id flag per field.
52
+ *
53
+ * **Note:** This function silently excludes sensitive fields without emitting
54
+ * warnings. It is designed to run after `buildModelSchema()` (which emits the
55
+ * warnings). In `generateGqlormArtifacts()` the call order is guaranteed, but
56
+ * callers using this function standalone should be aware that no warnings will
57
+ * be printed for auto-hidden sensitive fields.
58
+ *
59
+ * This is a pure function — safe for testing.
60
+ */
61
+ export declare function buildBackendModelInfo(dmmf: DMMF.Document): BackendModelInfo[];
62
+ /**
63
+ * Scan all SDL files in the given directory and return the set of GraphQL type
64
+ * names that are already defined by user-authored SDLs.
65
+ *
66
+ * This prevents gqlorm from generating duplicate type definitions that would
67
+ * cause merge conflicts in `makeMergedSchema`.
68
+ */
69
+ export declare function getExistingSdlTypeNames(graphqlDir: string): Set<string>;
70
+ /**
71
+ * Generate the full TypeScript source for `.cedar/gqlorm/backend.ts`.
72
+ *
73
+ * The generated file exports:
74
+ * - `schema`: a gql DocumentNode with type defs and Query fields
75
+ * - `createGqlormResolvers(db: GqlormDb)`: a factory function that takes a
76
+ * Prisma client-like object and returns a resolvers object
77
+ *
78
+ * The file does NOT import `db` directly. Instead, the Babel inject plugin
79
+ * imports `db` from `src/lib/db` in `graphql.ts` (where that alias resolves
80
+ * correctly) and passes it to `createGqlormResolvers`.
81
+ *
82
+ * The generated `GqlormDb` interface is scoped to exactly the visible models
83
+ * and fields — no hidden/sensitive fields, no @gqlorm hide models, no
84
+ * dependency on the generated Prisma client path or @prisma/client.
85
+ */
86
+ export declare function generateGqlormBackendContent(models: BackendModelInfo[], config?: GqlormBackendConfig): string;
10
87
  /**
11
88
  * Generate gqlorm artifacts from the Prisma schema.
12
89
  *
13
90
  * Reads the project's Prisma schema via DMMF, applies visibility rules
14
- * (@gqlorm directives + sensitivity heuristics), and writes the resulting
15
- * ModelSchema to `.cedar/gqlorm-schema.json`.
91
+ * (@gqlorm directives + sensitivity heuristics), and writes:
92
+ *
93
+ * 1. `.cedar/gqlorm-schema.json` — the frontend ModelSchema (field names only)
94
+ * 2. `.cedar/gqlorm/backend.ts` — auto-generated GraphQL types and resolvers
95
+ * for models that don't already have manually-written SDL files
96
+ *
97
+ * Cedar targets Node.js 24, which strips TypeScript types natively without any
98
+ * flags, so backend.ts can be imported directly at runtime
16
99
  *
17
100
  * Returns the same `{ files, errors }` shape used by other generators so it
18
101
  * can be integrated into `generate.ts` without special handling.
@@ -1 +1 @@
1
- {"version":3,"file":"gqlormSchema.d.ts","sourceRoot":"","sources":["../../../src/generate/gqlormSchema.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,KAAK,IAAI,MAAM,cAAc,CAAA;AAgBzC,KAAK,WAAW,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAA;AAiC3C;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,IAAI,CAAC,QAAQ,GAAG,WAAW,CAkDjE;AAED;;;;;;;;;GASG;AACH,wBAAsB,uBAAuB,IAAI,OAAO,CAAC;IACvD,KAAK,EAAE,MAAM,EAAE,CAAA;IACf,MAAM,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,OAAO,CAAA;KAAE,EAAE,CAAA;CAC9C,CAAC,CA+BD"}
1
+ {"version":3,"file":"gqlormSchema.d.ts","sourceRoot":"","sources":["../../../src/generate/gqlormSch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