{"id":24208596,"url":"https://github.com/aasmal97/typescript-to-mongo-schema","last_synced_at":"2026-05-11T10:35:17.700Z","repository":{"id":40115489,"uuid":"490598442","full_name":"aasmal97/Typescript-To-Mongo-Schema","owner":"aasmal97","description":"This program uses the Compiler Api, to allow a user to convert Typescript defined interfaces or type alias declarations, to MongoDB bson type schemas. ","archived":false,"fork":false,"pushed_at":"2022-05-20T01:54:34.000Z","size":55,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-01-14T01:17:28.429Z","etag":null,"topics":["bson","compiler-api","mongodb","schema","typescript"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/ts-to-mongo-schema","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/aasmal97.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}},"created_at":"2022-05-10T07:59:58.000Z","updated_at":"2024-02-20T10:38:41.000Z","dependencies_parsed_at":"2022-09-26T20:53:46.875Z","dependency_job_id":null,"html_url":"https://github.com/aasmal97/Typescript-To-Mongo-Schema","commit_stats":null,"previous_names":["aasmal97/typescripttomongoschema"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmal97%2FTypescript-To-Mongo-Schema","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmal97%2FTypescript-To-Mongo-Schema/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmal97%2FTypescript-To-Mongo-Schema/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/aasmal97%2FTypescript-To-Mongo-Schema/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/aasmal97","download_url":"https://codeload.github.com/aasmal97/Typescript-To-Mongo-Schema/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":241686375,"owners_count":20003104,"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":["bson","compiler-api","mongodb","schema","typescript"],"created_at":"2025-01-14T01:17:35.104Z","updated_at":"2026-05-11T10:35:17.663Z","avatar_url":"https://github.com/aasmal97.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## Introduction\nThis program allows the user to convert Typescript defined interfaces or type alias declarations, to MongoDB bson type schemas. This is achieved through the steps below \n\n## Program Logic\n\u003cdetails\u003e\n\u003csummary\u003eClick To Expand!\u003c/summary\u003e\n\u003cbr\u003e\n  \nThe program traverses all nodes that the interface or type depends on, according to the steps below.\n1. Generate a file's AST through the [Typescript Compiler API](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API)\n2. Recursively parse through each AST node to either:\n    - Extract the property types\n    - Search another file for an imported module\n4. Map the extracted AST property types to supported MongoDB bson types\n5. Combine the property types into one large MongoDB bson schema\n\n\u003c/details\u003e\n\n## Setup \n\u003cdetails\u003e\n\u003csummary\u003eClick To Expand!\u003c/summary\u003e\n \u003cbr\u003e\n\n### Installation\n  ```\n  npm i ts-to-mongo-schema\n  ```\n### Function Parameters \n```typescript\ntype GenerateSchema = {\n  //Path to the tsconfig.json of the target project\n  configPath: string;\n  \n  //Exact string name of type or interface declaration\n  identifier: string;\n  \n  //Path to the file the identifier is located\n  filePath: string;\n  \n  //typescript file extension\n  extension: \".tsx\" | \".ts\";\n  \n  /* \n    Provide custom map that contains the name \n    of the custom generic as a string, and a \n    function that returns the bson schema value \n    of that generic\n  */\n  resolveCustomGenerics?: { [key: string]: (params: any) =\u003e any };\n};\n```\n### Example: \n```typescript\nimport { generateSchema, ResolveCustomParams } from \"ts-to-mongo-schema\";\nimport * as fs from \"fs\";\n\n//paths\nconst projectPath = \"../../testProject\";\nconst configPath = projectPath + \"/tsconfig.json\";\nconst filePath = projectPath + \"/src/types/testFile.tsx\";\n\nconst bsonSchema = generateSchema({\n  configPath: configPath,\n  identifier: \"Person\",\n  filePath: filePath,\n  extension: '.tsx',\n});\n```\n\u003c/details\u003e\n\n## Error Management\n\u003cdetails\u003e\n\u003csummary\u003eClick To Expand!\u003c/summary\u003e\n\u003cbr\u003e\n  \nIf the program cannot parse the property type, an empty object will be returned in the property type's place. The user can then modify this manually, in the generated schema.\nExample Typescript: \n```typescript\ntype ArrayOneOrMore\u003cT\u003e = {\n  0: T;\n} \u0026 Array\u003cT\u003e;\n\ninterface Person{\n  name: string; \n  interests: ArrayOneOrMore\u003cstring\u003e\n}\n```\nBSON Schema Result: \n```typescript \n{\n  bsonType: 'object'\n  properties: {\n    name: {bsonType: string}\n    //empty object\n    interests: {}\n  }\n  required: [\n    name, \n    interests\n  ]\n}\n```\n\n#### Note:  \nThis will usually occur if the interface or type depends on a custom generic, or an imported type from a third-party library. If this is the case, please use    the resolveCustomGenerics function to provide a custom value. This outlined below.\n\n\u003c/details\u003e\n\n## Providing Custom Values for Custom Generics\n\u003cdetails\u003e\n\u003csummary\u003eClick To Expand!\u003c/summary\u003e\n\u003cbr\u003e\n\nWhen the program returns too many empty objects for property values, there could be an unsupported custom generic that does not allow for the extraction of properties. However, that does not mean all hope is lost. \n\nFind the offending generics and pass in a map to help the program identify them, and parse them according to custom logic\n```typescript\ntype ResolveCustomParams = {\n  propertiesPerArg?: any[];\n  combinedProperties?: { [key: string]: any };\n};\nconst bsonSchema = generateSchema({\n  configPath: configPath,\n  identifier: \"Person\",\n  filePath: filePath,\n  extension: '.tsx',\n  resolveCustomGenerics: {\n    //the test file contains a custom generic declared as ArrayOneOrMore.\n    //Therefore, the key is the name of the generic, and attached function \n    //returns the custom value for that generic\n    ArrayOneOrMore: (props: ResolveCustomParams) =\u003e {\n      return {\n        bsonType: \"array\",\n        items: props.combinedProperties,\n        minItems: 1,\n      };\n    },\n  },\n});\n```\n  \u003c/details\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasmal97%2Ftypescript-to-mongo-schema","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Faasmal97%2Ftypescript-to-mongo-schema","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Faasmal97%2Ftypescript-to-mongo-schema/lists"}