- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 126
feat(hooks): add "portable" generation mode #1850
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
    
    
  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
    
  
  
    
              
  
    
      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,153 @@ | ||
| /// <reference types="@types/jest" /> | ||
|  | ||
| import { loadSchema, normalizePath } from '@zenstackhq/testtools'; | ||
| import path from 'path'; | ||
| import tmp from 'tmp'; | ||
|  | ||
| describe('Tanstack Query Plugin Portable Tests', () => { | ||
| it('supports portable for standard prisma client', async () => { | ||
| await loadSchema( | ||
| ` | ||
| plugin tanstack { | ||
| provider = '${normalizePath(path.resolve(__dirname, '../dist'))}' | ||
| output = '$projectRoot/hooks' | ||
| target = 'react' | ||
| portable = true | ||
| } | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| email String | ||
| posts Post[] | ||
| } | ||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| title String | ||
| author User @relation(fields: [authorId], references: [id]) | ||
| authorId Int | ||
| } | ||
| `, | ||
| { | ||
| provider: 'postgresql', | ||
| pushDb: false, | ||
| extraDependencies: ['react@18.2.0', '@types/react@18.2.0', '@tanstack/react-query@5.56.x'], | ||
| copyDependencies: [path.resolve(__dirname, '../dist')], | ||
| compile: true, | ||
| extraSourceFiles: [ | ||
| { | ||
| name: 'main.ts', | ||
| content: ` | ||
| import { useFindUniqueUser } from './hooks'; | ||
| const { data } = useFindUniqueUser({ where: { id: 1 }, include: { posts: true } }); | ||
| console.log(data?.email); | ||
| console.log(data?.posts[0].title); | ||
| `, | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
|         
                  ymc9 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| it('supports portable for custom prisma client output', async () => { | ||
| const t = tmp.dirSync({ unsafeCleanup: true }); | ||
| const projectDir = t.name; | ||
|  | ||
| await loadSchema( | ||
| ` | ||
| datasource db { | ||
| provider = 'postgresql' | ||
| url = env('DATABASE_URL') | ||
| } | ||
| generator client { | ||
| provider = 'prisma-client-js' | ||
| output = '$projectRoot/myprisma' | ||
| } | ||
| plugin tanstack { | ||
| provider = '${normalizePath(path.resolve(__dirname, '../dist'))}' | ||
| output = '$projectRoot/hooks' | ||
| target = 'react' | ||
| portable = true | ||
| } | ||
| model User { | ||
| id Int @id @default(autoincrement()) | ||
| email String | ||
| posts Post[] | ||
| } | ||
| model Post { | ||
| id Int @id @default(autoincrement()) | ||
| title String | ||
| author User @relation(fields: [authorId], references: [id]) | ||
| authorId Int | ||
| } | ||
| `, | ||
| { | ||
| provider: 'postgresql', | ||
| pushDb: false, | ||
| extraDependencies: ['react@18.2.0', '@types/react@18.2.0', '@tanstack/react-query@5.56.x'], | ||
| copyDependencies: [path.resolve(__dirname, '../dist')], | ||
| compile: true, | ||
| addPrelude: false, | ||
| projectDir, | ||
| prismaLoadPath: `${projectDir}/myprisma`, | ||
| extraSourceFiles: [ | ||
| { | ||
| name: 'main.ts', | ||
| content: ` | ||
| import { useFindUniqueUser } from './hooks'; | ||
| const { data } = useFindUniqueUser({ where: { id: 1 }, include: { posts: true } }); | ||
| console.log(data?.email); | ||
| console.log(data?.posts[0].title); | ||
| `, | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
|         
                  ymc9 marked this conversation as resolved.
              Show resolved
            Hide resolved | ||
|  | ||
| it('supports portable for logical client', async () => { | ||
| await loadSchema( | ||
| ` | ||
| plugin tanstack { | ||
| provider = '${normalizePath(path.resolve(__dirname, '../dist'))}' | ||
| output = '$projectRoot/hooks' | ||
| target = 'react' | ||
| portable = true | ||
| } | ||
| model Base { | ||
| id Int @id @default(autoincrement()) | ||
| createdAt DateTime @default(now()) | ||
| type String | ||
| @@delegate(type) | ||
| } | ||
| model User extends Base { | ||
| email String | ||
| } | ||
| `, | ||
| { | ||
| provider: 'postgresql', | ||
| pushDb: false, | ||
| extraDependencies: ['react@18.2.0', '@types/react@18.2.0', '@tanstack/react-query@5.56.x'], | ||
| copyDependencies: [path.resolve(__dirname, '../dist')], | ||
| compile: true, | ||
| extraSourceFiles: [ | ||
| { | ||
| name: 'main.ts', | ||
| content: ` | ||
| import { useFindUniqueUser } from './hooks'; | ||
| const { data } = useFindUniqueUser({ where: { id: 1 } }); | ||
| console.log(data?.email); | ||
| console.log(data?.createdAt); | ||
| `, | ||
| }, | ||
| ], | ||
| } | ||
| ); | ||
| }); | ||
| }); | ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ensure 'portable' option validation handles all non-boolean values
The current validation
if (options.portable && typeof options.portable !== 'boolean')may not catch falsey non-boolean values like0or''. To accurately validate theportableoption, consider checking if it is defined and not a boolean.Apply this diff to fix the condition:
📝 Committable suggestion