{"id":14976136,"url":"https://github.com/taylorgoolsby/graphql-to-sql","last_synced_at":"2025-10-27T18:30:40.958Z","repository":{"id":33100563,"uuid":"151802703","full_name":"taylorgoolsby/graphql-to-sql","owner":"taylorgoolsby","description":"Unify your SQL schema and your GraphQL Schema. Use GraphQL SDL as the lingua franca to define your data requirements.","archived":false,"fork":false,"pushed_at":"2024-03-19T05:17:05.000Z","size":284,"stargazers_count":15,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-09-28T21:41:17.736Z","etag":null,"topics":["code-generation","database","directive","generator","graphql","graphql-schema","graphql-schema-language","mysql","schema","sdl","sql","sql-schema"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/taylorgoolsby.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":"2018-10-06T03:51:22.000Z","updated_at":"2024-09-27T06:21:16.000Z","dependencies_parsed_at":"2024-09-23T19:41:00.529Z","dependency_job_id":null,"html_url":"https://github.com/taylorgoolsby/graphql-to-sql","commit_stats":{"total_commits":62,"total_committers":3,"mean_commits":"20.666666666666668","dds":0.564516129032258,"last_synced_commit":"fce16f0abf62978b1174e60f6d815da9568e7f4f"},"previous_names":["taylrun/graphql-to-sql"],"tags_count":40,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-to-sql","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-to-sql/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-to-sql/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/taylorgoolsby%2Fgraphql-to-sql/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/taylorgoolsby","download_url":"https://codeload.github.com/taylorgoolsby/graphql-to-sql/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219873294,"owners_count":16554954,"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":["code-generation","database","directive","generator","graphql","graphql-schema","graphql-schema-language","mysql","schema","sdl","sql","sql-schema"],"created_at":"2024-09-24T13:53:22.506Z","updated_at":"2025-10-27T18:30:35.650Z","avatar_url":"https://github.com/taylorgoolsby.png","language":"JavaScript","readme":"# graphql-to-sql\n\nUnify your SQL schema and your GraphQL schema. Manage schema from a single source of truth.\n\n## Example\n\n`node generate-sql.js`\n```js\n// generate-sql.js\nimport sqlDirective from 'graphql-to-sql'\nimport gql from 'graphql-tag'\n\nconst {\n  sqlDirectiveTypeDefs,\n  generateSql\n} = sqlDirective('sql')\n\nconst typeDefs = gql`\n  directive @sql (\n    unicode: Boolean\n    auto: Boolean\n    default: String\n    index: Boolean\n    nullable: Boolean\n    primary: Boolean\n    type: String\n    unique: Boolean\n    generated: String\n    constraints: String\n  ) on OBJECT | FIELD_DEFINITION\n\n  # See graphql-directive-private\n  directive @private on OBJECT | FIELD_DEFINITION\n\n  type User @sql(unicode: true) {\n    userId: String @sql(type: \"BINARY(16)\", primary: true)\n    uniqueColumn: Int @sql(unique: true)\n    databaseOnlyField: Int @sql @private\n    \n    graphqlOnlyField: String\n    posts: [Post]\n  }\n\n  type Post {\n    postId: Int @sql(primary: true, auto: true)\n    userId: String @sql(type: \"BINARY(16)\", index: true)\n    content: String @sql(type: \"VARCHAR(300)\", unicode: true, nullable: true)\n    likes: Int @sql\n    dateCreated: String @sql(type: \"TIMESTAMP\", default: \"CURRENT_TIMESTAMP\")\n  }\n  \n  type UserPair @sql(constraints: \"UNIQUE(parentUserId, childUserId),\\\\n  FOREIGN KEY (parentUserId) REFERENCES User(userId)\") {\n    userPairId: String @sql(type: \"BINARY(16)\", primary: true)\n    parentUserId: String @sql(type: \"BINARY(16)\", index: true)\n    childUserId: String @sql(type: \"BINARY(16)\", index: true)\n  }\n`\n\nconst sql = generateSql({typeDefs: [typeDefs, sqlDirectiveTypeDefs]}, {\n  databaseName: 'public', // for postgres, keeping public is recommended.\n  tablePrefix: 'test', // or test_\n  dbType: 'mysql' // or postgres\n})\nconsole.log('sql', sql)\n```\n\nThe script above will produce this string:\n\n```sql\nCREATE TABLE IF NOT EXISTS `public`.`test_User` (\n  `userId` BINARY(16) NOT NULL,\n  `uniqueColumn` INT NOT NULL UNIQUE,\n  `databaseOnlyField` INT NOT NULL,\n  PRIMARY KEY (`userId`)\n) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n\nCREATE TABLE IF NOT EXISTS `public`.`test_UserPair` (\n  `userPairId` BINARY(16) NOT NULL,\n  `parentUserId` BINARY(16) NOT NULL,\n  `childUserId` BINARY(16) NOT NULL,\n  PRIMARY KEY (`userPairId`),\n  INDEX `PARENTUSERIDINDEX` (`parentUserId` ASC),\n  INDEX `CHILDUSERIDINDEX` (`childUserId` ASC),\n  UNIQUE(parentUserId, childUserId),\n  FOREIGN KEY (parentUserId) REFERENCES User(userId)\n);\n\nCREATE TABLE IF NOT EXISTS `public`.`test_Post` (\n  `postId` INT NOT NULL AUTO_INCREMENT,\n  `userId` BINARY(16) NOT NULL,\n  `content` VARCHAR(300) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL,\n  `likes` INT NOT NULL,\n  `dateCreated` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,\n  PRIMARY KEY (`postId`),\n  INDEX `USERIDINDEX` (`userId` ASC)\n) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;\n```\n\nAlso see [main-test.ts](__tests__/main-test.ts) for a working example.\n\n## Arguments for `@sql()`:\nON OBJECT:\n* unicode\n  * Adds `CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci` as *table_option*.\n* constraints\n  * Any text written in here will be appended \"as is\" to the *create_definition*.  \n\nON FIELD_DEFINITION:\n* auto\n  * Marks a column as being AUTO_INCREMENT.\n  * Column with \"auto\" must have INT or SERIAL type.\n  * \"default\" is not allowed with \"auto\".\n  * \"unicode\" is not allowed with \"auto\".\n* default\n  * Sets the DEFAULT clause.\n* index\n  * Creates an index for the column.\n* nullable\n  * Marks the column with NULL. By default, all columns are NOT NULL.\n* primary\n  * Creates a PRIMARY KEY clause for the column.\n  * At least one column needs to be marked as \"primary\".\n  * \"primary\" is not allowed with \"nullable\".\n* type\n  * Specify the column type.\n  * If type is not specified, the MySQL type will be inferred from the GraphQL type.\n  * GraphQLString types must be explicitly defined using \"type\".\n* unicode\n  * If any column is marked with \"unicode\", then the table will have `CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`.\n* unique\n  * Marks a column with the UNIQUE keyword.\n* generated\n  * Marks a generated column.\n  * Example: `@sql(generated: \"(data-\u003e\u003e'$.test')\")`. See [main-test.ts](__tests__/main-test.ts#L137) for more examples.\n\n## SQL Features Supported:\n- [x] Auto Increment\n- [x] Default\n- [x] Index\n- [x] Not Null\n- [x] Primary Key\n- [x] Unicode\n- [x] Unique\n- [ ] Check\n- [X] Constraints\n- [X] Foreign Key (via @constraints)\n- [x] Generated Columns\n\nMySQL and PostgreSQL are supported.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaylorgoolsby%2Fgraphql-to-sql","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftaylorgoolsby%2Fgraphql-to-sql","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftaylorgoolsby%2Fgraphql-to-sql/lists"}