Skip to content

Commit

Permalink
Merge pull request #13 from dangchinh25/revert-11-revert-10-cle/feat/…
Browse files Browse the repository at this point in the history
…typesafe-models-graph

Revert "Revert "feat: Typesafe generated models graph""
  • Loading branch information
dangchinh25 authored Nov 5, 2023
2 parents 9ce88a0 + 0f2250a commit 07f8ab4
Show file tree
Hide file tree
Showing 23 changed files with 432 additions and 165 deletions.
1 change: 1 addition & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
}
],
"space-before-function-paren": "off",
"@typescript-eslint/no-var-requires": "warn",
"@typescript-eslint/space-before-function-paren": [
"error",
"always"
Expand Down
63 changes: 58 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ npm install prisma-models-graph
**2. Add the generator to the schema**

```prisma
generator jsonSchema {
generator modelsGraph {
provider = "prisma-models-graph"
}
```

Additional options

```prisma
generator jsonSchema {
generator modelsGraph {
provider = "prisma-models-graph"
output = "./customOutputs"
fileName = "custom.json"
Expand All @@ -34,8 +34,61 @@ generator jsonSchema {
## Usage

- Add custom relation annotation next to the field you want annotate relationship.
- Format: `/// [[<Relation Model>.<Relation Model Attribute>]]`
<img src='./assets/annotation.png'>
- Format: `/// [[<Relation Model>.<Relation Model Attribute>]]` (<b>The triple slash is important.</b>)

```prisma
userId Int @map("user_id) /// [[User.id]]
```

- With the above annotation, the generated models graph will be like this:
<img src='./assets/output.png'>

```js
{
"users": {
"attributes": [
"id",
"email",
"name",
"user_type_id"
],
"relations": [
{
"modelName": "posts",
"condition": "users.id = posts.user_id"
}
]
},
"posts": {
"attributes": [
"id",
"subject",
"body",
"user_id"
],
"relations": [
{
"modelName": "users",
"condition": "posts.user_id = users.id"
}
]
}
}
```

- To access the generated models graph, import it from `@generated/models-graph`. The generated models graph will be a _typesafe_ object matching the models declared in `schema.prisma`

```js
import { ModelsGraph } from '@generated/models-graph'
```

- There are a few helper types available:

```js
import {
ModelsGraph, // Generated models graph object
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
} from '@generated/models-graph'
```
Binary file removed assets/annotation.png
Binary file not shown.
Binary file removed assets/output.png
Binary file not shown.
21 changes: 12 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,34 @@
"prepack": "yarn build",
"test": "jest",
"lint": "eslint --fix .",
"prepare": "husky install"
"prepare": "husky install",
"usage": "ts-node usage/index.ts"
},
"dependencies": {
"@prisma/client": "^5.2.0",
"@prisma/generator-helper": "^5.2.0",
"@prisma/internals": "^5.2.0",
"prettier": "3.0.3",
"prisma": "^5.2.0",
"prettier": "3.0.3"
"ts-node": "^10.9.1"
},
"devDependencies": {
"@types/jest": "^29.5.2",
"@types/node": "^20.4.0",
"jest": "^29.6.1",
"ts-jest": "^29.1.1",
"typescript": "^5.1.6",
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"semantic-release": "^18.0.1",
"@types/jest": "^29.5.2",
"@types/node": "^20.4.0",
"@typescript-eslint/eslint-plugin": "^5.61.0",
"@typescript-eslint/parser": "^5.61.0",
"@typescript-eslint/typescript-estree": "^5.61.0",
"eslint": "^8.44.0",
"eslint-plugin-jest": "^27.2.2",
"eslint-plugin-jest-formatting": "^3.1.0",
"husky": "7.0.4"
"husky": "7.0.4",
"jest": "^29.6.1",
"semantic-release": "^18.0.1",
"ts-jest": "^29.1.1",
"ts-morph": "20.0.0",
"typescript": "^5.1.6"
},
"homepage": "https://github.com/dangchinh25/prisma-models-graph",
"repository": {
Expand Down
Loading

0 comments on commit 07f8ab4

Please sign in to comment.