-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix: add regression project and optimize ts schema generation #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { loadDocument } from '@zenstackhq/language'; | ||
| import { TsSchemaGenerator } from '@zenstackhq/sdk'; | ||
| import { glob } from 'glob'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
|
|
||
| const dir = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| async function main() { | ||
| const zmodelFiles = glob.sync(path.resolve(dir, './test/**/*.zmodel')); | ||
| for (const file of zmodelFiles) { | ||
| console.log(`Generating TS schema for: ${file}`); | ||
| await generate(file); | ||
| } | ||
| } | ||
|
|
||
| async function generate(schemaPath: string) { | ||
| const generator = new TsSchemaGenerator(); | ||
| const outputDir = path.dirname(schemaPath); | ||
| const tsPath = path.join(outputDir, 'schema.ts'); | ||
| const pluginModelFiles = glob.sync(path.resolve(dir, '../../packages/runtime/dist/**/plugin.zmodel')); | ||
| const result = await loadDocument(schemaPath, pluginModelFiles); | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (!result.success) { | ||
| throw new Error(`Failed to load schema from ${schemaPath}: ${result.errors}`); | ||
| } | ||
| await generator.generate(result.model, outputDir); | ||
| } | ||
|
|
||
| main(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| { | ||
| "name": "regression", | ||
| "version": "3.0.0-beta.3", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "generate": "tsx generate.ts", | ||
| "test": "pnpm generate && tsc && vitest run" | ||
| }, | ||
ymc9 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| "dependencies": { | ||
| "@zenstackhq/testtools": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@zenstackhq/cli": "workspace:*", | ||
| "@zenstackhq/sdk": "workspace:*", | ||
| "@zenstackhq/language": "workspace:*", | ||
| "@zenstackhq/runtime": "workspace:*", | ||
| "@zenstackhq/typescript-config": "workspace:*", | ||
| "@zenstackhq/vitest-config": "workspace:*" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { type SchemaType as $Schema } from "./schema"; | ||
| import type { FindManyArgs as $FindManyArgs, FindUniqueArgs as $FindUniqueArgs, FindFirstArgs as $FindFirstArgs, CreateArgs as $CreateArgs, CreateManyArgs as $CreateManyArgs, CreateManyAndReturnArgs as $CreateManyAndReturnArgs, UpdateArgs as $UpdateArgs, UpdateManyArgs as $UpdateManyArgs, UpdateManyAndReturnArgs as $UpdateManyAndReturnArgs, UpsertArgs as $UpsertArgs, DeleteArgs as $DeleteArgs, DeleteManyArgs as $DeleteManyArgs, CountArgs as $CountArgs, AggregateArgs as $AggregateArgs, GroupByArgs as $GroupByArgs, WhereInput as $WhereInput, SelectInput as $SelectInput, IncludeInput as $IncludeInput, OmitInput as $OmitInput } from "@zenstackhq/runtime"; | ||
| import type { SimplifiedModelResult as $SimplifiedModelResult, SelectIncludeOmit as $SelectIncludeOmit } from "@zenstackhq/runtime"; | ||
| export type FooFindManyArgs = $FindManyArgs<$Schema, "Foo">; | ||
| export type FooFindUniqueArgs = $FindUniqueArgs<$Schema, "Foo">; | ||
| export type FooFindFirstArgs = $FindFirstArgs<$Schema, "Foo">; | ||
| export type FooCreateArgs = $CreateArgs<$Schema, "Foo">; | ||
| export type FooCreateManyArgs = $CreateManyArgs<$Schema, "Foo">; | ||
| export type FooCreateManyAndReturnArgs = $CreateManyAndReturnArgs<$Schema, "Foo">; | ||
| export type FooUpdateArgs = $UpdateArgs<$Schema, "Foo">; | ||
| export type FooUpdateManyArgs = $UpdateManyArgs<$Schema, "Foo">; | ||
| export type FooUpdateManyAndReturnArgs = $UpdateManyAndReturnArgs<$Schema, "Foo">; | ||
| export type FooUpsertArgs = $UpsertArgs<$Schema, "Foo">; | ||
| export type FooDeleteArgs = $DeleteArgs<$Schema, "Foo">; | ||
| export type FooDeleteManyArgs = $DeleteManyArgs<$Schema, "Foo">; | ||
| export type FooCountArgs = $CountArgs<$Schema, "Foo">; | ||
| export type FooAggregateArgs = $AggregateArgs<$Schema, "Foo">; | ||
| export type FooGroupByArgs = $GroupByArgs<$Schema, "Foo">; | ||
| export type FooWhereInput = $WhereInput<$Schema, "Foo">; | ||
| export type FooSelect = $SelectInput<$Schema, "Foo">; | ||
| export type FooInclude = $IncludeInput<$Schema, "Foo">; | ||
| export type FooOmit = $OmitInput<$Schema, "Foo">; | ||
| export type FooGetPayload<Args extends $SelectIncludeOmit<$Schema, "Foo", true>> = $SimplifiedModelResult<$Schema, "Foo", Args>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
| // DO NOT MODIFY THIS FILE // | ||
| // This file is automatically generated by ZenStack CLI and should not be manually updated. // | ||
| ////////////////////////////////////////////////////////////////////////////////////////////// | ||
|
|
||
| /* eslint-disable */ | ||
|
|
||
| import { schema as $schema, type SchemaType as $Schema } from "./schema"; | ||
| import { type ModelResult as $ModelResult, type TypeDefResult as $TypeDefResult } from "@zenstackhq/runtime"; | ||
| export type Foo = $ModelResult<$Schema, "Foo">; | ||
| export type Configuration = $TypeDefResult<$Schema, "Configuration">; | ||
| export const ShirtColor = $schema.enums.ShirtColor; | ||
| export type ShirtColor = (typeof ShirtColor)[keyof typeof ShirtColor]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| import { describe, it } from 'vitest'; | ||
| import { type Configuration, ShirtColor } from './models'; | ||
|
|
||
| describe('Issue 204 regression tests', () => { | ||
| it('tests issue 204', () => { | ||
| const config: Configuration = { teamColors: [ShirtColor.Black, ShirtColor.Blue] }; | ||
| console.log(config.teamColors?.[0]); | ||
| const config1: Configuration = {}; | ||
| console.log(config1); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| datasource db { | ||
| provider = "sqlite" | ||
| url = "file:./dev.db" | ||
| } | ||
|
|
||
| enum ShirtColor { | ||
| Black | ||
| White | ||
| Red | ||
| Green | ||
| Blue | ||
| } | ||
|
|
||
| type Configuration { | ||
| teamColors ShirtColor[]? // This should be an optional array | ||
| } | ||
|
|
||
| model Foo { | ||
| id Int @id | ||
| config Configuration @json | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.