{"id":18400898,"url":"https://github.com/magicmark/prisma-introspect-renamer-saver","last_synced_at":"2025-04-12T16:59:25.368Z","repository":{"id":69482261,"uuid":"284347719","full_name":"magicmark/prisma-introspect-renamer-saver","owner":"magicmark","description":"PIRS: prisma-introspect-renamer-saver","archived":false,"fork":false,"pushed_at":"2020-08-02T00:16:18.000Z","size":63,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-16T03:24:48.072Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/magicmark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-08-01T22:16:14.000Z","updated_at":"2020-08-02T00:16:20.000Z","dependencies_parsed_at":"2023-04-03T12:22:10.357Z","dependency_job_id":null,"html_url":"https://github.com/magicmark/prisma-introspect-renamer-saver","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprisma-introspect-renamer-saver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprisma-introspect-renamer-saver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprisma-introspect-renamer-saver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/magicmark%2Fprisma-introspect-renamer-saver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/magicmark","download_url":"https://codeload.github.com/magicmark/prisma-introspect-renamer-saver/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248602229,"owners_count":21131613,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-06T02:37:12.605Z","updated_at":"2025-04-12T16:59:25.337Z","avatar_url":"https://github.com/magicmark.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# PIRS: prisma-introspect-renamer-saver\n\n![](https://i.fluffy.cc/q6wwsSBCTXKGkgcxnCBq666RWjMQPWn1.png)\n\nPIRS (prisma-introspect-renamer-saver) transforms a `.prisma` file to apply a set\nof diffs on the generated data model. This automates the mapping that may have to\nbe done after each time `$ prisma introspect` is run to make it fit the\n[naming conventions][conventions].\n\n## Install\n\n```bash\n$ yarn add --dev pirs\n```\n\n## Usage\n\n```bash\n$ yarn pirs -p schema.prisma -t transforms.yaml --write\n```\n\n(See `yarn pirs --help` for more options.)\n\n## Motivation\n\nThe generated Prisma data model (the `.prisma` file) that may not confirm to the\n[Prisma data model conventions][conventions], so it is encouraged to edit the\nfile accordingly.\n\n[conventions]: https://www.prisma.io/docs/reference/tools-and-interfaces/introspection#rules-and-conventions\n\nHowver according to the [Prisma's docs](https://www.prisma.io/docs/reference/tools-and-interfaces/prisma-client/configuring-the-prisma-client-api#renaming-relation-fields):\n\n\u003e Warning:\n\u003e\n\u003e Prisma-level relation fields that were renamed in the Prisma schema will be reset when you run prisma introspect again.\n\u003e You therefore might want to back up your Prisma schema with these attributes in order to not having to annotate everything from scratch again after a re-introspection.\n\nIt seems as though any artisanally hand crafted changes you make to the generated\n`.prisma` file will get blown away each time you run `$ prisma introspect` after\nan SQL Schema change.\n\n`pirs` addresses this by allowing you to declare the diffs you want to apply in a\nYAML file. After each time `$ prisma introspect` is run, we can now run\n`$ yarn pirs` to magically rename the fields, without having to do it by hand\nall over again. (Think of this as a fancy patch file).\n\n## Example\n\nConsider the following sample `schema.prisma` file:\n\n\u003cdetails\u003e\u003csummary\u003e\u003cb\u003eschema.prisma\u003c/b\u003e\u003c/summary\u003e\n\u003cp\u003e\n\n```prisma\ngenerator client {\n  provider = \"prisma-client-js\"\n}\n\ndatasource db {\n  provider = \"postgresql\"\n  url      = env(\"DATABASE_URL\")\n}\n\nmodel User {\n  id           String        @id\n  created      DateTime      @default(now())\n  name         String?\n  email        String        @unique\n  passwordHash String\n  UserSession  UserSession[]\n  Photo        Photo[]\n}\n\nmodel UserSession {\n  token   String   @id\n  created DateTime @default(now())\n  userId  String\n  User    User     @relation(fields: [userId], references: [id])\n}\n\nmodel Photo {\n  id             String  @id\n  title          String?\n  url            String\n  userUploadedId String\n  User           User    @relation(fields: [userUploadedId], references: [id])\n}\n```\n\n\u003c/p\u003e\n\u003c/details\u003e\n\nIn accordance with the [naming conventions][conventions], we'd probably want to transform:\n\n-   `User.UserSession` -\u003e `User.sessions`\n-   `User.Photo` -\u003e `User.uploadedPhotos`\n-   `UserSession.User` -\u003e `UserSession.user`\n-   `Photo.User` -\u003e `Photo.uploadedBy`\n\n[conventions]: https://www.prisma.io/docs/reference/tools-and-interfaces/introspection#rules-and-conventions\n\nApplying the following `transforms.yaml` file lets us do just that:\n\n```yaml\nmodels:\n    - name: User\n      fields:\n          - field: UserSession\n            attributes:\n                fieldName: sessions\n          - field: Photo\n            attributes:\n                fieldName: uploadedPhotos\n\n    - name: UserSession\n      fields:\n          - field: User\n            attributes:\n                fieldName: user\n\n    - name: Photo\n      fields:\n          - field: User\n            attributes:\n                fieldName: uploadedBy\n```\n\nSee [the tests](./tests/test.js) to see this in action.\n\n## API\n\nCurrently, we only support rewriting the field names on models.\n\nThe `--transformsFile` (or `-t`) arg must point to a yaml file in the following\nformat:\n\n```typescript\ninterface Transforms {\n    models: Array\u003c{\n        name: string;\n        fields: Array\u003c{\n            field: string;\n            attributes: {\n                fieldName?: string;\n            };\n        }\u003e;\n    }\u003e;\n}\n```\n\n## FAQs\n\n#### Why not use \\\u003cinsert some other tool here\u003e?\n\nI googled and didn't find any other lightweight solutions that that did this. But maybe I missed something! I figure if it does exist, the best way to find out is to publish this and have people let me know about it :)\n\n#### I want this to work with \\\u003cinsert feature here\u003e?\n\nIf there's a feature missing that you think should be added - open up an issue! Even better, PRs are super welcome too :)\n\n#### Why is this code so janky?\n\nI'd love to do a fancy AST transformation, but I didn't fancy writing my own parser :p\n\n#### Is this over-engineered? This seems over-engineered.\n\nProbably. Let me know what you think in a GitHub issue / on Twitter, and what you'd suggest instead!\n\n## Contact\n\n- [@mark_larah - https://twitter.com/mark_larah](https://twitter.com/mark_larah)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Fprisma-introspect-renamer-saver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmagicmark%2Fprisma-introspect-renamer-saver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmagicmark%2Fprisma-introspect-renamer-saver/lists"}