Skip to content

Commit

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

Revert "feat: Typesafe generated models graph"
  • Loading branch information
dangchinh25 authored Nov 5, 2023
2 parents cf313da + c9f56a0 commit 9ce88a0
Show file tree
Hide file tree
Showing 23 changed files with 165 additions and 432 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@
}
],
"space-before-function-paren": "off",
"@typescript-eslint/no-var-requires": "warn",
"@typescript-eslint/space-before-function-paren": [
"error",
"always"
Expand Down
63 changes: 5 additions & 58 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 modelsGraph {
generator jsonSchema {
provider = "prisma-models-graph"
}
```

Additional options

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

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

```prisma
userId Int @map("user_id) /// [[User.id]]
```
- Format: `/// [[<Relation Model>.<Relation Model Attribute>]]`
<img src='./assets/annotation.png'>

- With the above annotation, the generated models graph will be like this:

```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'
```
<img src='./assets/output.png'>
Binary file added assets/annotation.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 9 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,31 @@
"prepack": "yarn build",
"test": "jest",
"lint": "eslint --fix .",
"prepare": "husky install",
"usage": "ts-node usage/index.ts"
"prepare": "husky install"
},
"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",
"ts-node": "^10.9.1"
"prettier": "3.0.3"
},
"devDependencies": {
"@semantic-release/changelog": "^6.0.1",
"@semantic-release/git": "^10.0.1",
"@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",
"@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",
"jest": "^29.6.1",
"semantic-release": "^18.0.1",
"ts-jest": "^29.1.1",
"ts-morph": "20.0.0",
"typescript": "^5.1.6"
"husky": "7.0.4"
},
"homepage": "https://github.com/dangchinh25/prisma-models-graph",
"repository": {
Expand Down
Loading

0 comments on commit 9ce88a0

Please sign in to comment.