{"id":14070176,"url":"https://github.com/L-Mario564/drizzle-dbml-generator","last_synced_at":"2025-07-30T07:30:46.361Z","repository":{"id":192118326,"uuid":"686222420","full_name":"L-Mario564/drizzle-dbml-generator","owner":"L-Mario564","description":"Generate DBML markup from your schema defined with Drizzle ORM.","archived":false,"fork":false,"pushed_at":"2024-08-22T03:27:21.000Z","size":135,"stargazers_count":164,"open_issues_count":7,"forks_count":8,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-11-30T15:08:44.245Z","etag":null,"topics":["database","dbml","drizzle-orm","generator","schema-viewer"],"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/L-Mario564.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":"2023-09-02T04:40:16.000Z","updated_at":"2024-11-25T11:22:13.000Z","dependencies_parsed_at":"2023-11-28T22:28:18.687Z","dependency_job_id":"726afa3a-ebf6-482c-838e-4d271c446edf","html_url":"https://github.com/L-Mario564/drizzle-dbml-generator","commit_stats":null,"previous_names":["l-mario564/drizzle-dbml-generator"],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/L-Mario564%2Fdrizzle-dbml-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/L-Mario564%2Fdrizzle-dbml-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/L-Mario564%2Fdrizzle-dbml-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/L-Mario564%2Fdrizzle-dbml-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/L-Mario564","download_url":"https://codeload.github.com/L-Mario564/drizzle-dbml-generator/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":228102575,"owners_count":17869882,"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":["database","dbml","drizzle-orm","generator","schema-viewer"],"created_at":"2024-08-13T07:07:31.912Z","updated_at":"2024-12-04T11:31:25.829Z","avatar_url":"https://github.com/L-Mario564.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"# Drizzle DBML Generator\n\nGenerate DBML markup from your schema defined with Drizzle ORM. Works with any dialect.\n\n## Generate DBML\n\nTo generate the DBML markup from your schema, all you have to do is run a script that executes the generate function of the dialect you're working with. To do that, you must install `tsx` to run Typescript files, `drizzle-orm` if it's not already installed and `drizzle-dbml-generator` of course.\n\n```bash\n# npm\nnpm i drizzle-orm\nnpm i -D drizzle-dbml-generator tsx\n# yarn\nyarn add drizzle-orm\nyarn add -D drizzle-dbml-generator tsx\n# pnpm\npnpm add drizzle-orm\npnpm add -D drizzle-dbml-generator tsx\n```\n\n### Example\n\n**schema.ts**\n\n```ts\nimport {\n  boolean,\n  integer,\n  pgTable,\n  primaryKey,\n  serial,\n  text,\n  timestamp,\n  varchar\n} from 'drizzle-orm/pg-core';\nimport { relations } from 'drizzle-orm';\n\nexport const users = pgTable('users', {\n  id: serial('id').primaryKey(),\n  registeredAt: timestamp('registered_at').notNull().defaultNow(),\n  username: varchar('username', { length: 16 }).notNull().unique('uq_users_username'),\n  bio: text('bio'),\n  hasBlue: boolean('has_blue').notNull().default(false)\n});\n\nexport const usersRelations = relations(users, ({ many }) =\u003e ({\n  followers: many(followers, { relationName: 'user_followers' }),\n  following: many(followers, { relationName: 'user_follows' }),\n  tweets: many(tweets),\n  likes: many(likes)\n}));\n\nexport const followers = pgTable(\n  'followers',\n  {\n    userId: integer('user_id')\n      .notNull()\n      .references(() =\u003e users.id),\n    followsUserId: integer('follows_user_id')\n      .notNull()\n      .references(() =\u003e users.id)\n  },\n  (followers) =\u003e ({\n    pk: primaryKey(followers.userId, followers.followsUserId)\n  })\n);\n\nexport const followersRelations = relations(followers, ({ one }) =\u003e ({\n  user: one(users, {\n    fields: [followers.userId],\n    references: [users.id],\n    relationName: 'user_followers'\n  }),\n  followsUser: one(users, {\n    fields: [followers.followsUserId],\n    references: [users.id],\n    relationName: 'user_follows'\n  })\n}));\n\nexport const tweets = pgTable('tweets', {\n  id: serial('id').primaryKey(),\n  postedAt: timestamp('posted_at').notNull().defaultNow(),\n  content: text('content').notNull(),\n  postedById: integer('posted_by_id')\n    .notNull()\n    .references(() =\u003e users.id)\n});\n\nexport const tweetsRelations = relations(tweets, ({ one }) =\u003e ({\n  postedBy: one(users, {\n    fields: [tweets.postedById],\n    references: [users.id]\n  })\n}));\n\nexport const likes = pgTable(\n  'likes',\n  {\n    likedTweetId: integer('liked_tweet_id')\n      .notNull()\n      .references(() =\u003e tweets.id),\n    likedById: integer('liked_by_id')\n      .notNull()\n      .references(() =\u003e users.id)\n  },\n  (likes) =\u003e ({\n    pk: primaryKey(likes.likedById, likes.likedTweetId)\n  })\n);\n\nexport const likesRelations = relations(likes, ({ one }) =\u003e ({\n  likedTweet: one(tweets, {\n    fields: [likes.likedTweetId],\n    references: [tweets.id]\n  }),\n  likedBy: one(users, {\n    fields: [likes.likedById],\n    references: [users.id]\n  })\n}));\n```\n\n**dbml.ts**\n\n```ts\nimport * as schema from './schema';\nimport { pgGenerate } from 'drizzle-dbml-generator'; // Using Postgres for this example\n\nconst out = './schema.dbml';\nconst relational = true;\n\npgGenerate({ schema, out, relational });\n```\n\nRunning the following command will generate the file with the DBML:\n\n```bash\n# You can add this as a package.json script\ntsx dbml.ts\n```\n\n**schema.dbml**\n\n```dbml\ntable users {\n  id serial [pk, not null, increment]\n  registered_at timestamp [not null, default: `now()`]\n  username varchar(16) [not null, unique]\n  bio text\n  has_blue boolean [not null, default: false]\n}\n\ntable followers {\n  user_id integer [not null]\n  follows_user_id integer [not null]\n\n  indexes {\n    (user_id, follows_user_id) [pk]\n  }\n}\n\ntable tweets {\n  id serial [pk, not null, increment]\n  posted_at timestamp [not null, default: `now()`]\n  content text [not null]\n  posted_by_id integer [not null]\n}\n\ntable likes {\n  liked_tweet_id integer [not null]\n  liked_by_id integer [not null]\n\n  indexes {\n    (liked_by_id, liked_tweet_id) [pk]\n  }\n}\n\nref followers_user_id_users_id_fk: followers.user_id \u003e users.id [delete: no action, update: no action]\n\nref followers_follows_user_id_users_id_fk: followers.follows_user_id \u003e users.id [delete: no action, update: no action]\n\nref tweets_posted_by_id_users_id_fk: tweets.posted_by_id \u003e users.id [delete: no action, update: no action]\n\nref likes_liked_tweet_id_tweets_id_fk: likes.liked_tweet_id \u003e tweets.id [delete: no action, update: no action]\n\nref likes_liked_by_id_users_id_fk: likes.liked_by_id \u003e users.id [delete: no action, update: no action]\n```\n\n## Options\n\n**pgGenerate**\n\n| Option     | Type     | Description                                                                                                                                                                                               |\n| ---------- | -------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| schema     | PgSchema | An object containing Postgres dialect tables, enums and relations                                                                                                                                         |\n| out?       | string   | The output directory and file name. Uses the current working directory as the root. If not set, it will not write the DBML file.                                                                          |\n| relational | boolean? | If set to true, it will create references based on the relations generated with the `relations` function instead of foreign keys. Useful for databases that don't support foreign keys. Default: `false`. |\n\n**mysqlGenerate**\n\n| Option     | Type        | Description                                                                                                                                                                                               |\n| ---------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| schema     | MySqlSchema | An object containing MySQL dialect tables and relations                                                                                                                                                   |\n| out?       | string      | The output directory and file name. Uses the current working directory as the root. If not set, it will not write the DBML file.                                                                          |\n| relational | boolean?    | If set to true, it will create references based on the relations generated with the `relations` function instead of foreign keys. Useful for databases that don't support foreign keys. Default: `false`. |\n\n**sqliteGenerate**\n\n| Option     | Type         | Description                                                                                                                                                                                               |\n| ---------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| schema     | SQLiteSchema | An object containing SQLite dialect tables and relations                                                                                                                                                  |\n| out?       | string       | The output directory and file name. Uses the current working directory as the root. If not set, it will not write the DBML file.                                                                          |\n| relational | boolean?     | If set to true, it will create references based on the relations generated with the `relations` function instead of foreign keys. Useful for databases that don't support foreign keys. Default: `false`. |\n\nAll generate functions return the DBML as a string regardless if a file is written or not, in case you want to do something with the generated DBML.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FL-Mario564%2Fdrizzle-dbml-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FL-Mario564%2Fdrizzle-dbml-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FL-Mario564%2Fdrizzle-dbml-generator/lists"}