From 62a038466c2f1c0022a1d94c8ded9a8c66e0fcf2 Mon Sep 17 00:00:00 2001 From: dangchinh25 Date: Mon, 6 Nov 2023 10:30:04 -0600 Subject: [PATCH 1/2] feat: include attribute type in parsed model --- prisma/modelsGraph/ModelsGraph.json | 120 ++++++++++++++++++++++++++++ src/generator/config.ts | 2 + src/generator/generateCode.ts | 10 ++- src/generator/helpers/parser.ts | 5 +- src/generator/types/models.ts | 7 +- 5 files changed, 140 insertions(+), 4 deletions(-) create mode 100644 prisma/modelsGraph/ModelsGraph.json diff --git a/prisma/modelsGraph/ModelsGraph.json b/prisma/modelsGraph/ModelsGraph.json new file mode 100644 index 0000000..4f6c559 --- /dev/null +++ b/prisma/modelsGraph/ModelsGraph.json @@ -0,0 +1,120 @@ +{ + "users": { + "attributes": [ + { + "name": "id", + "type": "Int" + }, + { + "name": "email", + "type": "String" + }, + { + "name": "name", + "type": "String" + }, + { + "name": "user_type_id", + "type": "Int" + } + ], + "relations": [ + { + "modelName": "user_types", + "condition": "users.user_type_id = user_types.id" + }, + { + "modelName": "posts", + "condition": "users.id = posts.user_id" + }, + { + "modelName": "users_user_groups", + "condition": "users.id = users_user_groups.user_id" + } + ] + }, + "posts": { + "attributes": [ + { + "name": "id", + "type": "Int" + }, + { + "name": "subject", + "type": "String" + }, + { + "name": "body", + "type": "String" + }, + { + "name": "user_id", + "type": "Int" + } + ], + "relations": [ + { + "modelName": "users", + "condition": "posts.user_id = users.id" + } + ] + }, + "user_types": { + "attributes": [ + { + "name": "id", + "type": "Int" + }, + { + "name": "name", + "type": "String" + } + ], + "relations": [ + { + "modelName": "users", + "condition": "user_types.id = users.user_type_id" + } + ] + }, + "user_groups": { + "attributes": [ + { + "name": "id", + "type": "Int" + }, + { + "name": "name", + "type": "String" + } + ], + "relations": [ + { + "modelName": "users_user_groups", + "condition": "user_groups.id = users_user_groups.user_group_id" + } + ] + }, + "users_user_groups": { + "attributes": [ + { + "name": "user_id", + "type": "Int" + }, + { + "name": "user_group_id", + "type": "Int" + } + ], + "relations": [ + { + "modelName": "users", + "condition": "users_user_groups.user_id = users.id" + }, + { + "modelName": "user_groups", + "condition": "users_user_groups.user_group_id = user_groups.id" + } + ] + } +} diff --git a/src/generator/config.ts b/src/generator/config.ts index 1f4802e..55ea244 100644 --- a/src/generator/config.ts +++ b/src/generator/config.ts @@ -12,6 +12,8 @@ export const PARSED_MODEL_TYPE_NAME = 'ParsedModel'; export const PARSED_MODEL_RELATION_TYPE_NAME = 'ParsedModelRelation'; +export const PARSED_MODEL_ATTRIBUTE_TYPE_NAME = 'ParsedModelAttribute'; + export const MODEL_NAMES_VARIABLE_NAME = 'modelNames'; export const MODEL_NAMES_TYPE_NAME = 'ModelNames'; \ No newline at end of file diff --git a/src/generator/generateCode.ts b/src/generator/generateCode.ts index bc123a2..f5f852b 100644 --- a/src/generator/generateCode.ts +++ b/src/generator/generateCode.ts @@ -17,7 +17,8 @@ import { PARSED_MODELS_TYPE_NAME, PARSED_MODEL_RELATION_TYPE_NAME, PARSED_MODEL_TYPE_NAME, - TYPE_DIRECTORY_NAME + TYPE_DIRECTORY_NAME, + PARSED_MODEL_ATTRIBUTE_TYPE_NAME } from './config'; import { ParsedModels } from './types'; @@ -72,9 +73,14 @@ const generateTypeDirectory = async ( modelName: string; condition: string; }; + + export type ${ PARSED_MODEL_ATTRIBUTE_TYPE_NAME } = { + name: string; + type: string; + }; export type ${ PARSED_MODEL_TYPE_NAME } = { - attributes: string[]; + attributes: ${ PARSED_MODEL_ATTRIBUTE_TYPE_NAME }[]; relations: ${ PARSED_MODEL_RELATION_TYPE_NAME }[]; }; diff --git a/src/generator/helpers/parser.ts b/src/generator/helpers/parser.ts index 4e1bb45..3da9db2 100644 --- a/src/generator/helpers/parser.ts +++ b/src/generator/helpers/parser.ts @@ -21,7 +21,10 @@ export const parseDMMFModels = ( for ( const field of model.fields ) { const attribute = field.dbName || field.name; - parsedModel.attributes.push( attribute ); + parsedModel.attributes.push( { + name: attribute, + type: field.type + } ); attributesDbNameMap.set( `${ model.name }.${ field.name }`, attribute ); } diff --git a/src/generator/types/models.ts b/src/generator/types/models.ts index 4f9f743..98b4923 100644 --- a/src/generator/types/models.ts +++ b/src/generator/types/models.ts @@ -3,8 +3,13 @@ export type ParsedModelRelation = { condition: string; }; +export type ParsedModelAttribute = { + name: string; + type: string; +}; + export type ParsedModel = { - attributes: string[]; + attributes: ParsedModelAttribute[]; relations: ParsedModelRelation[]; }; From 3222d308ea679c1493294516e24050fbd2d7b0eb Mon Sep 17 00:00:00 2001 From: dangchinh25 Date: Mon, 6 Nov 2023 10:31:39 -0600 Subject: [PATCH 2/2] feat: update readme --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index e0a6a25..148c439 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ import { ParsedModel, // Type definition of a singular generated model graph ParsedModels, // Type definition of all generated models graph ParsedModelRelation, // Type definition of the relation between two models - ModelNames, // List of all generated models name + ModelNames, // List of all generated models name, + ParsedModelAttribute, // Type definition of generated models attribute } from '@generated/models-graph' ```