{"id":23054252,"url":"https://github.com/mrleebo/prisma-ast","last_synced_at":"2025-05-15T13:08:36.081Z","repository":{"id":37754644,"uuid":"360029247","full_name":"MrLeebo/prisma-ast","owner":"MrLeebo","description":"Abstract Syntax Tree for parsing schema.prisma files","archived":false,"fork":false,"pushed_at":"2024-12-16T15:11:19.000Z","size":421,"stargazers_count":150,"open_issues_count":0,"forks_count":19,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-05-12T21:19:21.899Z","etag":null,"topics":["abstract-syntax-tree","ast","javascript","parse","parser","printer","prisma","schema","sdk","typescript"],"latest_commit_sha":null,"homepage":"","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/MrLeebo.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":"2021-04-21T04:13:32.000Z","updated_at":"2025-05-02T15:10:31.000Z","dependencies_parsed_at":"2023-01-31T13:15:45.208Z","dependency_job_id":"83f65e4e-2269-43aa-b84c-3555fea24c8d","html_url":"https://github.com/MrLeebo/prisma-ast","commit_stats":{"total_commits":64,"total_committers":7,"mean_commits":9.142857142857142,"dds":0.203125,"last_synced_commit":"1da6d2a5713542a876c8e362244d8e8a5b8dbb17"},"previous_names":[],"tags_count":30,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrLeebo%2Fprisma-ast","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrLeebo%2Fprisma-ast/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrLeebo%2Fprisma-ast/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MrLeebo%2Fprisma-ast/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MrLeebo","download_url":"https://codeload.github.com/MrLeebo/prisma-ast/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254346624,"owners_count":22055808,"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":["abstract-syntax-tree","ast","javascript","parse","parser","printer","prisma","schema","sdk","typescript"],"created_at":"2024-12-16T00:37:48.727Z","updated_at":"2025-05-15T13:08:31.070Z","avatar_url":"https://github.com/MrLeebo.png","language":"TypeScript","readme":"\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/prisma-ast\" target=\"_blank\" rel=\"noopener\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/dw/@mrleebo/prisma-ast.svg\" alt=\"Total Downloads\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/@mrleebo/prisma-ast\" target=\"_blank\" rel=\"noopener\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/@mrleebo/prisma-ast.svg\" alt=\"npm package\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/mrleebo/prisma-ast/blob/main/LICENSE\" target=\"_blank\" rel=\"noopener\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/l/@mrleebo/prisma-ast.svg\" alt=\"License\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.prisma.io/\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/Prisma-3982CE?style=for-the-badge\u0026logo=Prisma\u0026logoColor=white\" height=\"28\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.buymeacoffee.com/mrleebo\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://cdn.buymeacoffee.com/buttons/v2/default-violet.png\" alt=\"Buy Me A Coffee\" height=\"28\" \u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n# @mrleebo/prisma-ast\n\nThis library uses an abstract syntax tree to parse schema.prisma files into an object in JavaScript. It also allows you to update your Prisma schema files using a Builder object pattern that is fully implemented in TypeScript.\n\nIt is similar to [@prisma/sdk](https://github.com/prisma/prisma/tree/master/src/packages/sdk) except that it preserves comments and model attributes. It also doesn't attempt to validate the correctness of the schema at all; the focus is instead on the ability to parse the schema into an object, manipulate it using JavaScript, and re-print the schema back to a file without losing information that isn't captured by other parsers.\n\n\u003e It is probable that a future version of @prisma/sdk will render this library obsolete.\n\n## Install\n\n```bash\nnpm install @mrleebo/prisma-ast\n```\n\n## Examples\n\n### Produce a modified schema by building upon an existing schema\n\n```ts\nproduceSchema(source: string, (builder: PrismaSchemaBuilder) =\u003e void, printOptions?: PrintOptions): string\n```\n\nproduceSchema is the simplest way to interact with prisma-ast; you input your schema source and a producer function to produce modifications to it, and it will output the schema source with your modifications applied.\n\n```ts\nimport { produceSchema } from '@mrleebo/prisma-ast';\n\nconst source = `\nmodel User {\n  id   Int    @id @default(autoincrement())\n  name String @unique\n}\n`;\n\nconst output = produceSchema(source, (builder) =\u003e {\n  builder\n    .model('AppSetting')\n    .field('key', 'String', [{ name: 'id' }])\n    .field('value', 'Json');\n});\n```\n\n```prisma\nmodel User {\n  id   Int    @id @default(autoincrement())\n  name String @unique\n}\n\nmodel AppSetting {\n  key   String @id\n  value Json\n}\n```\n\nFor more information about what the builder can do, check out the [PrismaSchemaBuilder](#prismaschemabuilder) class.\n\n### PrismaSchemaBuilder\n\nThe `produceSchema()` utility will construct a builder for you, but you can also create your own instance, which may be useful for more interactive use-cases.\n\n```ts\nimport { createPrismaSchemaBuilder } from '@mrleebo/prisma-ast';\n\nconst builder = createPrismaSchemaBuilder();\n\nbuilder\n  .model('User')\n  .field('id', 'Int')\n  .attribute('id')\n  .attribute('default', [{ name: 'autoincrement' }])\n  .field('name', 'String')\n  .attribute('unique')\n  .break()\n  .comment('this is a comment')\n  .blockAttribute('index', ['name']);\n\nconst output = builder.print();\n```\n\n```prisma\nmodel User {\n  id   Int @id @default(autoincrement())\n  name String @unique\n\n  // this is a comment\n  @@index([name])\n}\n```\n\n### Query the prisma schema for specific objects\n\nThe builder can also help you find matching objects in the schema based on name (by string or RegExp) or parent context. You can use this to write tests against your schema, or find fields that don't match a naming convention, for example.\n\n```ts\nconst source = `\n  model Product {\n    id     String  @id @default(auto()) @map(\"_id\") @db.ObjectId\n    name   String\n    photos Photo[]\n  }\n`\n\nconst builder = createPrismaSchemaBuilder(source);\n\nconst product = builder.findByType('model', { name: 'Product' });\nexpect(product).toHaveProperty('name', 'Product');\n\nconst id = builder.findByType('field', {\n  name: 'id',\n  within: product?.properties,\n});\nexpect(id).toHaveProperty('name', 'id');\n\nconst map = builder.findByType('attribute', {\n  name: 'map',\n  within: id?.attributes,\n});\nexpect(map).toHaveProperty('name', 'map');\n```\n\n### Re-sort the schema\n\nprisma-ast can sort the schema for you. The default sort order is `['generator', 'datasource', 'model', 'enum']` and will sort objects of the same type alphabetically.\n\n```ts\nprint(options?: {\n  sort: boolean,\n  locales?: string | string[],\n  sortOrder?: Array\u003c'generator' | 'datasource' | 'model' | 'enum'\u003e\n})\n```\n\nYou can optionally set your own sort order, or change the locale used by the sort.\n\n```ts\n// sort with default parameters\nbuilder.print({ sort: true });\n\n// sort with options\nbuilder.print({\n  sort: true,\n  locales: 'en-US',\n  sortOrder: ['datasource', 'generator', 'model', 'enum'],\n});\n```\n\n### Need More SchemaBuilder Code snippets?\n\nThere is a lot that you can do with the schema builder. There are [additional sample references available](./EXAMPLES.md) for you to explore.\n\n## Configuration Options\n\nprisma-ast uses [lilconfig](https://github.com/antonk52/lilconfig) to read configuration options which\ncan be located in any of the following files, and in several other variations (see [the complete list of search paths](https://www.npmjs.com/package/cosmiconfig)):\n\n- `\"prisma-ast\"` in `package.json`\n- `.prisma-astrc`\n- `.prisma-astrc.json`\n- `.prisma-astrc.js`\n- `.config/.prisma-astrc`\n\nConfiguration options are:\n\n| Option                        | Description                                                                                                                                                                                     | Default Value |\n| ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------- |\n| `parser.nodeTrackingLocation` | Include the token locations of CST Nodes in the output schema.\u003cbr\u003eDisabled by default because it can impact parsing performance.\u003cbr\u003ePossible values are `\"none\"`, `\"onlyOffset\"`, and `\"full\"`. | `\"none\"`      |\n\n### Example Custom Configuration\n\nHere is an example of how you can customize your configuration options in `package.json`.\n\n```json\n{\n  \"prisma-ast\": {\n    \"parser\": {\n      \"nodeTrackingLocation\": \"full\"\n    }\n  }\n}\n```\n\n## Underlying utility functions\n\nThe `produceSchema` and `createPrismaSchemaBuilder` functions are intended to be your interface for interacting with the prisma schema, but you can also get direct access to the AST representation if you need to edit the schema for more advanced usages that aren't covered by the methods above.\n\n### Parse a schema.prisma file into an AST object\n\nThe shape of the AST is not fully documented, and it is more likely to change than the builder API.\n\n```ts\nimport { getSchema } from '@mrleebo/prisma-ast';\n\nconst source = `\nmodel User {\n  id   Int    @id @default(autoincrement())\n  name String @unique\n}\n`;\n\nconst schema = getSchema(source);\n```\n\n### Print a schema AST back out as a string\n\nThis is what `builder.print()` calls internally, and is what you'd use to print if you called `getSchema()`.\n\n```ts\nimport { printSchema } from '@mrleebo/prisma-ast';\n\nconst source = printSchema(schema);\n```\n\nYou can optionally re-sort the schema. The default sort order is `['generator', 'datasource', 'model', 'enum']`, and objects with the same type are sorted alphabetically, but the sort order can be overridden.\n\n```ts\nconst source = printSchema(schema, {\n  sort: true,\n  locales: 'en-US',\n  sortOrder: ['datasource', 'generator', 'model', 'enum'],\n});\n```\n","funding_links":["https://www.buymeacoffee.com/mrleebo"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrleebo%2Fprisma-ast","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmrleebo%2Fprisma-ast","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmrleebo%2Fprisma-ast/lists"}