Skip to content

Commit 170535a

Browse files
authored
feat(schema): add option for generating lite schema (#357)
* feat(schema): add option for generating lite schema * fix --lite-only option and add CLI test
1 parent e919bb0 commit 170535a

File tree

22 files changed

+379
-107
lines changed

22 files changed

+379
-107
lines changed

packages/cli/src/actions/generate.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type Options = {
1313
schema?: string;
1414
output?: string;
1515
silent: boolean;
16+
lite: boolean;
17+
liteOnly: boolean;
1618
};
1719

1820
/**
@@ -88,10 +90,15 @@ async function runPlugins(schemaFile: string, model: Model, outputPath: string,
8890
}
8991
}
9092

91-
const defaultPlugins = [corePlugins['typescript']].reverse();
92-
defaultPlugins.forEach((d) => {
93-
if (!processedPlugins.some((p) => p.cliPlugin === d)) {
94-
processedPlugins.push({ cliPlugin: d, pluginOptions: {} });
93+
const defaultPlugins = [
94+
{
95+
plugin: corePlugins['typescript'],
96+
options: { lite: options.lite, liteOnly: options.liteOnly },
97+
},
98+
];
99+
defaultPlugins.forEach(({ plugin, options }) => {
100+
if (!processedPlugins.some((p) => p.cliPlugin === plugin)) {
101+
processedPlugins.push({ cliPlugin: plugin, pluginOptions: options });
95102
}
96103
});
97104

packages/cli/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ function createProgram() {
5959
.addOption(schemaOption)
6060
.addOption(noVersionCheckOption)
6161
.addOption(new Option('-o, --output <path>', 'default output directory for code generation'))
62+
.addOption(new Option('--lite', 'also generate a lite version of schema without attributes').default(false))
63+
.addOption(new Option('--lite-only', 'only generate lite version of schema without attributes').default(false))
6264
.addOption(new Option('--silent', 'suppress all output except errors').default(false))
6365
.action(generateAction);
6466

packages/cli/src/plugins/typescript.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,22 @@ const plugin: CliPlugin = {
77
name: 'TypeScript Schema Generator',
88
statusText: 'Generating TypeScript schema',
99
async generate({ model, defaultOutputPath, pluginOptions }) {
10+
// output path
1011
let outDir = defaultOutputPath;
1112
if (typeof pluginOptions['output'] === 'string') {
1213
outDir = path.resolve(defaultOutputPath, pluginOptions['output']);
1314
if (!fs.existsSync(outDir)) {
1415
fs.mkdirSync(outDir, { recursive: true });
1516
}
1617
}
17-
await new TsSchemaGenerator().generate(model, outDir);
18+
19+
// lite mode
20+
const lite = pluginOptions['lite'] === true;
21+
22+
// liteOnly mode
23+
const liteOnly = pluginOptions['liteOnly'] === true;
24+
25+
await new TsSchemaGenerator().generate(model, { outDir, lite, liteOnly });
1826
},
1927
};
2028

packages/cli/test/generate.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,18 @@ describe('CLI generate command test', () => {
4444
runCli('generate', workDir);
4545
expect(fs.existsSync(path.join(workDir, 'bar/schema.ts'))).toBe(true);
4646
});
47+
48+
it('should respect lite option', () => {
49+
const workDir = createProject(model);
50+
runCli('generate --lite', workDir);
51+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(true);
52+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
53+
});
54+
55+
it('should respect liteOnly option', () => {
56+
const workDir = createProject(model);
57+
runCli('generate --lite-only', workDir);
58+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema.ts'))).toBe(false);
59+
expect(fs.existsSync(path.join(workDir, 'zenstack/schema-lite.ts'))).toBe(true);
60+
});
4761
});

packages/cli/test/ts-schema-gen.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,4 +420,26 @@ model User {
420420
},
421421
});
422422
});
423+
424+
it('supports lite schema generation', async () => {
425+
const { schemaLite } = await generateTsSchema(
426+
`
427+
model User {
428+
id String @id @default(uuid())
429+
name String
430+
email String @unique
431+
432+
@@map('users')
433+
}
434+
`,
435+
undefined,
436+
undefined,
437+
undefined,
438+
true,
439+
);
440+
441+
expect(schemaLite!.models.User.attributes).toBeUndefined();
442+
expect(schemaLite!.models.User.fields.id.attributes).toBeUndefined();
443+
expect(schemaLite!.models.User.fields.email.attributes).toBeUndefined();
444+
});
423445
});

packages/clients/tanstack-query/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
"type": "module",
77
"private": true,
88
"scripts": {
9-
"build": "tsc --noEmit && tsup-node",
9+
"build": "tsc --noEmit && tsup-node && pnpm test:generate && pnpm test:typecheck",
1010
"watch": "tsup-node --watch",
1111
"lint": "eslint src --ext ts",
1212
"test": "vitest run",
1313
"pack": "pnpm pack",
14-
"test:generate": "tsx scripts/generate.ts"
14+
"test:generate": "tsx scripts/generate.ts",
15+
"test:typecheck": "tsc --noEmit --project tsconfig.test.json"
1516
},
1617
"keywords": [
1718
"tanstack-query",

packages/clients/tanstack-query/scripts/generate.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ async function main() {
1616

1717
async function generate(schemaPath: string) {
1818
const generator = new TsSchemaGenerator();
19-
const outputDir = path.dirname(schemaPath);
19+
const outDir = path.dirname(schemaPath);
2020
const result = await loadDocument(schemaPath);
2121
if (!result.success) {
2222
throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`);
2323
}
24-
await generator.generate(result.model, outputDir);
24+
await generator.generate(result.model, { outDir, liteOnly: true });
2525
}
2626

2727
main();

0 commit comments

Comments
 (0)