@@ -2,6 +2,7 @@ import type { DMMF } from '@prisma/generator-helper';
22import {
33 PluginOptions ,
44 createProject ,
5+ generateModelMeta ,
56 getDataModels ,
67 getPrismaClientImportSpec ,
78 getPrismaVersion ,
@@ -16,9 +17,10 @@ import path from 'path';
1617import semver from 'semver' ;
1718import { FunctionDeclaration , OptionalKind , ParameterDeclarationStructure , Project , SourceFile } from 'ts-morph' ;
1819import { upperCaseFirst } from 'upper-case-first' ;
20+ import { name } from '.' ;
1921
2022export async function generate ( model : Model , options : PluginOptions , dmmf : DMMF . Document ) {
21- let outDir = requireOption < string > ( options , 'output' ) ;
23+ let outDir = requireOption < string > ( options , 'output' , name ) ;
2224 outDir = resolvePath ( outDir , options ) ;
2325
2426 const project = createProject ( ) ;
@@ -32,6 +34,8 @@ export async function generate(model: Model, options: PluginOptions, dmmf: DMMF.
3234
3335 const models = getDataModels ( model ) ;
3436
37+ await generateModelMeta ( project , models , path . join ( outDir , '__model_meta.ts' ) , false , true ) ;
38+
3539 generateIndex ( project , outDir , models ) ;
3640
3741 models . forEach ( ( dataModel ) => {
@@ -60,24 +64,20 @@ function generateModelHooks(project: Project, outDir: string, model: DataModel,
6064 moduleSpecifier : prismaImport ,
6165 } ) ;
6266 sf . addStatements ( [
63- `import { useContext } from 'react ';` ,
64- `import { RequestHandlerContext, type GetNextArgs, type RequestOptions, type InfiniteRequestOptions, type PickEnumerable, type CheckSelect } from '@zenstackhq/swr/runtime ';` ,
67+ `import { RequestHandlerContext, type GetNextArgs, type RequestOptions, type InfiniteRequestOptions, type PickEnumerable, type CheckSelect, useHooksContext } from '@zenstackhq/swr/runtime ';` ,
68+ `import metadata from './__model_meta ';` ,
6569 `import * as request from '@zenstackhq/swr/runtime';` ,
6670 ] ) ;
6771
6872 const modelNameCap = upperCaseFirst ( model . name ) ;
6973 const prismaVersion = getPrismaVersion ( ) ;
7074
71- const prefixesToMutate = [ 'find' , 'aggregate' , 'count' , 'groupBy' ] ;
7275 const useMutation = sf . addFunction ( {
7376 name : `useMutate${ model . name } ` ,
7477 isExported : true ,
7578 statements : [
76- 'const { endpoint, fetch } = useContext(RequestHandlerContext);' ,
77- `const prefixesToMutate = [${ prefixesToMutate
78- . map ( ( prefix ) => '`${endpoint}/' + lowerCaseFirst ( model . name ) + '/' + prefix + '`' )
79- . join ( ', ' ) } ];`,
80- 'const mutate = request.getMutate(prefixesToMutate);' ,
79+ 'const { endpoint, fetch, logging } = useHooksContext();' ,
80+ `const mutate = request.useMutate('${ model . name } ', metadata, logging);` ,
8181 ] ,
8282 } ) ;
8383 const mutationFuncs : string [ ] = [ ] ;
@@ -297,8 +297,6 @@ function generateQueryHook(
297297 typeParameters ?: string [ ] ,
298298 infinite = false
299299) {
300- const modelRouteName = lowerCaseFirst ( model . name ) ;
301-
302300 const typeParams = typeParameters ? [ ...typeParameters ] : [ `T extends ${ argsType } ` ] ;
303301 if ( infinite ) {
304302 typeParams . push ( `R extends ${ returnType } ` ) ;
@@ -329,10 +327,10 @@ function generateQueryHook(
329327 } )
330328 . addBody ( )
331329 . addStatements ( [
332- 'const { endpoint, fetch } = useContext(RequestHandlerContext );' ,
330+ 'const { endpoint, fetch } = useHooksContext( );' ,
333331 ! infinite
334- ? `return request.get <${ returnType } >(\`\${endpoint}/ ${ modelRouteName } / ${ operation } \` , args, options, fetch);`
335- : `return request.infiniteGet <${ inputType } | undefined, ${ returnType } >(\`\${endpoint}/ ${ modelRouteName } / ${ operation } \` , getNextArgs, options, fetch);` ,
332+ ? `return request.useGet <${ returnType } >(' ${ model . name } ', ' ${ operation } ', endpoint , args, options, fetch);`
333+ : `return request.useInfiniteGet <${ inputType } | undefined, ${ returnType } >(' ${ model . name } ', ' ${ operation } ', endpoint , getNextArgs, options, fetch);` ,
336334 ] ) ;
337335}
338336
0 commit comments