Skip to content

Commit

Permalink
Merge pull request #15 from dangchinh25/cle/feat/attributes-with-type
Browse files Browse the repository at this point in the history
Cle/feat/attributes with type
  • Loading branch information
dangchinh25 authored Nov 6, 2023
2 parents 96d3244 + 3222d30 commit d92de67
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```
120 changes: 120 additions & 0 deletions prisma/modelsGraph/ModelsGraph.json
Original file line number Diff line number Diff line change
@@ -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"
}
]
}
}
2 changes: 2 additions & 0 deletions src/generator/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
10 changes: 8 additions & 2 deletions src/generator/generateCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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 }[];
};
Expand Down
5 changes: 4 additions & 1 deletion src/generator/helpers/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

Expand Down
7 changes: 6 additions & 1 deletion src/generator/types/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
};

Expand Down

0 comments on commit d92de67

Please sign in to comment.