{"id":13632608,"url":"https://github.com/cktang88/mongoose-api-generator","last_synced_at":"2025-04-18T05:32:42.142Z","repository":{"id":53236943,"uuid":"273581751","full_name":"cktang88/mongoose-api-generator","owner":"cktang88","description":"Autogenerate a REST API from your Mongoose models","archived":false,"fork":false,"pushed_at":"2021-04-01T02:46:44.000Z","size":214,"stargazers_count":19,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-08-01T22:53:59.671Z","etag":null,"topics":["api","boilerplate","db","expressjs","generator","mongodb","mongoose","nodejs","rest-api"],"latest_commit_sha":null,"homepage":"https://cktang88.github.io/mongoose-api-generator","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/cktang88.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":"2020-06-19T20:34:54.000Z","updated_at":"2021-06-25T16:56:55.000Z","dependencies_parsed_at":"2022-08-19T20:20:17.656Z","dependency_job_id":null,"html_url":"https://github.com/cktang88/mongoose-api-generator","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/cktang88%2Fmongoose-api-generator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cktang88%2Fmongoose-api-generator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cktang88%2Fmongoose-api-generator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/cktang88%2Fmongoose-api-generator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/cktang88","download_url":"https://codeload.github.com/cktang88/mongoose-api-generator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223775242,"owners_count":17200480,"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":["api","boilerplate","db","expressjs","generator","mongodb","mongoose","nodejs","rest-api"],"created_at":"2024-08-01T22:03:08.629Z","updated_at":"2024-11-09T01:30:24.465Z","avatar_url":"https://github.com/cktang88.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"## Mongoose REST API Autogenerator\n\nAutomatically generate a REST API from Mongoose models.\n\nCreates a hot-reloading server that auto-updates whenever models are updated or created.\n\n:warning: This project is still in an early stage and may undergo breaking changes.\n\n## Dev\n\nCreate a `.env` file with\n\n```bash\n\n# url of mongo database\nMONGODB_URL='\u003curl_of_mongo_database\u003e'\n\n# jwt signing secret for auth\nJWT_SECRET = '\u003csigning_secret\u003e'\n\n# directory models are discovered from (optional)\n# ./models by default\nMODELS_DIR = 'models'\n\n# the path of the autogenerated resources enum file for the client (optional)\n# ./client/src by default\nRESOURCES_FILE_DIR = 'client/src'\n\n```\n\nThen:\n\n```bash\nyarn\nyarn start\n```\n\n- API calls can be made using `curl`, [Httpie](https://httpie.org/), or a full-fledged API client like [Insomnia](https://insomnia.rest/) or [Postman](https://www.postman.com/).\n\n## Client library\n\nThis repo includes a sample frontend (React + TypeScript) at `./client`. This is a bare React app made with [create-snowpack-app](https://github.com/pikapkg/snowpack) using the `@snowpack/app-template-react-typescript` template.\n\nThere are two custom files included to make API requests easier.\n\n- `client/src/apiLib.ts` - exports helpful functions to easily interact with the API.\n- `client/src/apiResources.ts` - contains an exported TypeScript enum made to work with `apiLib.ts` that is autogenerated from your models, and is hot-reloaded as well.\n\nSample usage:\n\n```js\n// Resource is an enum exported by './apiResources.ts'\nimport { signup, login, api, Resource } from \"./apiLib\";\n\n// signup and login :)\nawait signup(\"bob\", email, password);\nawait login(email, password);\n\n// create a new box\nlet box = await api.CREATE(Resource.box, { height: 4 });\n// get a new box\nbox = await api.GET(Resource.box, box._id);\n// list all boxes\nbox = await api.LIST(Resource.box);\n\n// api.UPDATE and api.DELETE is also available.\n```\n\n## Authentication\n\n- Sign up:\n  - `POST /auth/signup`\n  - Sample request body:\n    ```\n    {\"username\": \"bob\", \"email\": \"bob@gmail.com\", \"password\": \"badpw\"}\n    ```\n- Login:\n\n  - `POST /auth/login`\n  - Sample request body:\n    ```\n    {\"email\": \"bob@gmail.com\", \"password\": \"badpw\"}\n    ```\n    - returns a JWT token, eg. `Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjVlZWQ3OWM1N2MxZmEzNzExODZlZjljOSIsInVzZXJuYW1lIjoiYWIiLCJpYXQiOjE1OTI2MjI1MDAsImV4cCI6MTU5MjY1ODUwMH0.-eFJ1FcotHsjtmgUaE3f-6fFz_7y8c2dCNqhH8E5S6A`\n  - Pass the token in the `Authorization` HTTP header for each subsequent API request.\n\n- View user profile via `GET /auth/profile`\n\n## Adding a model\n\nSimply create a new file that exports a mongoose model to `./models`.\n\nThis will result in two actions:\n\n1. autogenerated URL endpoints will be updated\n2. An autogenerated file will be created/overriden at `${RESOURCES_FILE_DIR}/apiResources.ts`.\n\n**NOTE: these endpoints are not accessible unless you are signed in.**\nThe autogenerated URL endpoints will be:\n\nCreate:\n\n- `POST /api/{fileName}`\n  - Inputs are automatically validated using the Mongoose Schema. Errors are returned to the client with a `400` HTTP Error code.\n\nList all\n\n- `GET /api/{fileName}`\n\nGet one\n\n- `GET /api/{fileName}/:id`\n\nUpdate one\n\n- `PATCH /api/{fileName}/:id`\n\nDelete one\n\n- `DELETE /api/{fileName}/:id`\n\n## Implementing permissions\n\nPermissions enable you to implement granular restrictions on who can perform an action on a resource.\n\n- need to have an `owner_id: String` field in the Mongoose Schema. This field is automatically populated whenever a new object is created via the API endpoint.\n- Export a `permissions` object that may override `list/get/update/remove` fields (by default all of these are set to `PUBLIC`)\n\n  - possible values (exported from `\"../framework/auth/permissions\"`):\n    - `PUBLIC`: anyone can perform this action on this resource\n    - `OWNER`: only the creator of the resource can perform this action\n    - `NONE`: this action is disabled for this object\n\n- Example:\n\n```js\nconst { Schema } = require(\"mongoose\");\nconst { PUBLIC, OWNER, NONE } = require(\"../framework/auth/permissions\");\nconst schema = new Schema(\n  {\n    width: Number,\n    height: Number,\n    created: { type: Date, default: Date.now },\n    name: String,\n    owner_id: String,\n  },\n  { strict: \"throw\" }\n);\n\nconst permissions = {\n  list: PUBLIC,\n  get: PUBLIC,\n  update: OWNER,\n  remove: NONE,\n};\n\nmodule.exports = { schema, permissions };\n```\n\n## Tech used\n\n- Mongoose, Express, Passport\n- `Nodemon` is used for hot-reloading instead of `node-dev` because the files are dynamically required, so file-watching is needed to identify new files being added but not specifically required in `./models`.\n\n## TODOs\n\n- enable extensibility for login object? (eg. phone num, descript, other meta fields)\n- sanitize all inputs in express middleware...\n- support listing with filtering?\n- auto-add `owner_id: String` and `{ strict: \"throw\", toObject: { versionKey: false } }` using Mongoose discriminators for schema inheritance?\n- enable disabling all auth for all endpoints via config var\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcktang88%2Fmongoose-api-generator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcktang88%2Fmongoose-api-generator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcktang88%2Fmongoose-api-generator/lists"}