{"id":15110055,"url":"https://github.com/stantanasi/mongoose-jsonapi","last_synced_at":"2026-03-02T05:01:34.625Z","repository":{"id":257488185,"uuid":"857565423","full_name":"stantanasi/mongoose-jsonapi","owner":"stantanasi","description":"A Mongoose plugin to support JSON:API specification","archived":false,"fork":false,"pushed_at":"2025-05-02T10:00:11.000Z","size":124,"stargazers_count":0,"open_issues_count":2,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-26T22:59:51.101Z","etag":null,"topics":["api","json-api","jsonapi","mongodb","mongoose","mongoose-plugin","plugin"],"latest_commit_sha":null,"homepage":"https://npmjs.com/package/@stantanasi/mongoose-jsonapi","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/stantanasi.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,"zenodo":null}},"created_at":"2024-09-15T01:48:35.000Z","updated_at":"2025-05-02T10:00:16.000Z","dependencies_parsed_at":"2024-09-17T02:58:00.606Z","dependency_job_id":"98feb890-05d8-40de-b006-b95fe2b5262c","html_url":"https://github.com/stantanasi/mongoose-jsonapi","commit_stats":null,"previous_names":["stantanasi/mongoose-jsonapi"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/stantanasi/mongoose-jsonapi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stantanasi%2Fmongoose-jsonapi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stantanasi%2Fmongoose-jsonapi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stantanasi%2Fmongoose-jsonapi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stantanasi%2Fmongoose-jsonapi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/stantanasi","download_url":"https://codeload.github.com/stantanasi/mongoose-jsonapi/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/stantanasi%2Fmongoose-jsonapi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29993026,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-02T01:47:34.672Z","status":"online","status_checked_at":"2026-03-02T02:00:07.342Z","response_time":60,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","json-api","jsonapi","mongodb","mongoose","mongoose-plugin","plugin"],"created_at":"2024-09-25T23:40:30.176Z","updated_at":"2026-03-02T05:01:34.608Z","avatar_url":"https://github.com/stantanasi.png","language":"TypeScript","funding_links":["https://www.buymeacoffee.com/stantanasi"],"categories":[],"sub_categories":[],"readme":"# Mongoose JSON:API\n\nA Mongoose plugin to support [JSON:API](https://jsonapi.org) specification\n\n[![\"Buy Me A Coffee\"](https://www.buymeacoffee.com/assets/img/custom_images/orange_img.png)](https://www.buymeacoffee.com/stantanasi)\n\n### Features\n\n- JSON:API v1.0 specification\n- **Inclusion of Related Resources**\n- **Sparse Fieldsets**\n- **Sorting**\n- **Pagination**\n- **Filtering**\n- **Error handling**\n- Serialize Mongoose Model to JSON:API resource\n- Deserialize JSON:API resource to Mongoose Model\n\n## Installation\n\n```bash\nnpm install @stantanasi/mongoose-jsonapi\n```\n\n## Usage\n\n### Add plugin to a schema\n\n#### JavaScript\n\n```javascript\nimport MongooseJsonApi from '@stantanasi/mongoose-jsonapi'\nimport mongoose, { Schema } from 'mongoose'\n\nconst ArticleSchema = new Schema({\n  title: {\n    type: String,\n    required: true,\n  },\n})\n\nArticleSchema.plugin(MongooseJsonApi, {\n  type: 'articles',\n})\n\nconst Article = mongoose.model('Article', ArticleSchema)\n```\n\n#### TypeScript\n\n```typescript\nimport MongooseJsonApi, { JsonApiInstanceMethods, JsonApiModel, JsonApiQueryHelper } from '@stantanasi/mongoose-jsonapi'\nimport mongoose, { HydratedDocument, Model, Schema, Types } from 'mongoose'\n\ninterface IArticle {\n  _id: Types.ObjectId\n  title: string\n}\n\ntype ArticleInstanceMethods = JsonApiInstanceMethods\n\ntype ArticleQueryHelper = JsonApiQueryHelper\n\ntype ArticleModel = Model\u003cIArticle, ArticleQueryHelper, ArticleInstanceMethods\u003e \u0026 JsonApiModel\u003cIArticle\u003e\n\nconst ArticleSchema = new Schema\u003cIArticle, ArticleModel, ArticleInstanceMethods, ArticleQueryHelper\u003e({\n  title: {\n    type: String,\n    required: true,\n  },\n})\n\nArticleSchema.plugin(MongooseJsonApi, {\n  type: 'articles',\n})\n\n\ntype TArticle = HydratedDocument\u003cIArticle, ArticleInstanceMethods, ArticleQueryHelper\u003e\n\nconst Article = mongoose.model\u003cIArticle, ArticleModel\u003e('Article', ArticleSchema)\n```\n\n### Use methods\n\n```typescript\nconst body = {\n  data: {\n    type: 'articles',\n    id: '66e81ea7763111ccc64a104b',\n    attributes: {\n      title: 'JSON:API paints my bikeshed!'\n    }\n  }\n}\nconst article = Article.fromJsonApi(body)\nawait article.save()\n\narticle._id // '66e81ea7763111ccc64a104b'\narticle.title // 'JSON:API paints my bikeshed!'\n\n\nconst response1 = article.toJsonApi({\n  baseUrl: 'http://localhost:5000'\n})\n\nresponse1.jsonapi.version // '1.0'\nresponse1.data.type // 'articles'\nresponse1.data.id // '66e81ea7763111ccc64a104b'\nresponse1.data.links.self // 'http://localhost:5000/articles/66e81ea7763111ccc64a104b'\nresponse1.data.attributes.title // 'JSON:API paints my bikeshed!'\n\n\nconst response2 = await Article.findById('66e81ea7763111ccc64a104b')\n  .toJsonApi({\n    baseUrl: 'http://localhost:5000'\n  })\n\nresponse2.jsonapi.version // '1.0'\nresponse2.data.type // 'articles'\nresponse2.data.id // '66e81ea7763111ccc64a104b'\nresponse2.data.links.self // 'http://localhost:5000/articles/66e81ea7763111ccc64a104b'\nresponse2.data.attributes.title // 'JSON:API paints my bikeshed!'\n\n\nconst response3 = await Article.find()\n  .toJsonApi({\n    baseUrl: 'http://localhost:5000'\n  })\n  .paginate({\n    url: 'http://localhost:5000/articles',\n    query: {},\n  })\n\nresponse3.jsonapi.version // '1.0'\nresponse3.data[0].type // 'articles'\nresponse3.data[0].id // '66e81ea7763111ccc64a104b'\nresponse3.data[0].links.self // 'http://localhost:5000/articles/66e81ea7763111ccc64a104b'\nresponse3.data[0].attributes.title // 'JSON:API paints my bikeshed!'\nresponse3.links.first // 'http://localhost:5000/articles?page[limit]=10\u0026page[offset]=0'\n```\n\nPlease refer to the [example](./example/README.md) folder to see how to use it in an Express app\n\n## API\n\n\u003c!-- no toc --\u003e\n- [MongooseJsonApi()](#mongoosejsonapi)\n- [Model.fromJsonApi()](#modelfromjsonapi)\n- [Document.prototype.toJsonApi()](#documentprototypetojsonapi)\n- [Document.prototype.merge()](#documentprototypemerge)\n- [Query.prototype.getRelationship()](#queryprototypegetrelationship)\n- [Query.prototype.withJsonApi()](#queryprototypewithjsonapi)\n- [Query.prototype.toJsonApi()](#queryprototypetojsonapi)\n- [Query.prototype.paginate()](#queryprototypepaginate)\n\n### MongooseJsonApi()\n\n**Parameters:**\n\n- `type`: *string* - The JSON:API resource type for the model\n- `[filter]`: *object* - The JSON:API custom filtering\n\n#### Example\n\n```typescript\nArticleSchema.plugin(MongooseJsonApi, {\n  type: 'articles',\n})\n\n// GET /articles?filter[search]=...\nArticleSchema.plugin(MongooseJsonApi, {\n  type: \"articles\",\n  filter: {\n    search: (query: string) =\u003e {\n      return {\n        body : {\n          $regex : query,\n        },\n      }\n    },\n  },\n})\n```\n\n### Model.fromJsonApi()\n\n**Parameters:**\n\n- `body`: *JsonApiBody* - The JSON:API request body\n\n**Returns:**\n\n- *Document* - The Mongoose Document\n\n#### Example\n\n```typescript\nconst body = {\n  data: {\n    type: 'articles',\n    attributes: {\n      title: 'JSON:API paints my bikeshed!'\n    }\n  }\n}\n\nconst article = Article.fromJsonApi(body)\narticle.title // 'JSON:API paints my bikeshed!'\n```\n\n### Document.prototype.toJsonApi()\n\n**Parameters:**\n\n- `opts`: *object* - Options\n  - `opts.baseUrl`: *string* - The base URL used in JSON:API links object\n  - `[opts.meta]`: *object* - The meta information to include in the JSON:API response body\n\n**Returns:**\n\n- *JsonApiBody* - The JSON:API response body\n\n#### Example\n\n```typescript\nconst article = new Article({\n  title: 'JSON:API paints my bikeshed!'\n})\n\nconst body = article.toJsonApi({\n  baseUrl: 'http://localhost:5000'\n})\n\nbody.data.attributes.title // 'JSON:API paints my bikeshed!'\n```\n\n### Document.prototype.merge()\n\n**Parameters:**\n\n- `sources`: *object | Document* - One or more source objects or documents containing the properties to be applied\n\n**Returns:**\n\n- *Document* - The Mongoose Document\n\n#### Example\n\n```typescript\nconst people = new People({\n  firstName: 'John',\n  lastName: 'Gebhardt',\n})\n\nconst body = {\n  data: {\n    type: 'people',\n    attributes: {\n      lastName: 'Doe',\n    }\n  }\n}\n\npeople.merge(People.fromJsonApi(body))\n\npeople.firstName // 'John'\npeople.lastName // 'Doe'\n```\n\n### Query.prototype.getRelationship()\n\n**Parameters:**\n\n- `relationship`: *string* - The name of the relationship to retrieve\n\n**Returns:**\n\n- *Document | Document[]* - The related Mongoose document(s)\n\n#### Example\n\n```typescript\nconst people = new People({\n  firstName: 'John',\n  lastName: 'Doe',\n})\nawait people.save()\n\nconst article = new Article({\n  title: 'JSON:API paints my bikeshed!',\n  author: people,\n})\nawait article.save()\n\nconst author = await Article.findById(article._id).getRelationship('author').exec()\nauthor.firstName // 'John'\nauthor.lastName // 'Doe'\n```\n\n### Query.prototype.withJsonApi()\n\n**Parameters:**\n\n- `query`: *JsonApiQueryParams* - The JSON:API Query Parameters\n\n**Returns:**\n\n- *Query* - The Mongoose Query\n\n#### Example\n\n```typescript\n// ?include=author,comments\u0026fields[articles]=title\u0026filter[title]=JSON:API paints my bikeshed!\u0026sort=-updatedAt\u0026page[limit]=1\nconst query = {\n  include: 'author,comments',\n  fields: { articles: 'title' },\n  filter: { title: 'JSON:API paints my bikeshed!' },\n  sort: '-updatedAt',\n  page: { limit: 1 }\n}\n\nconst articles = await Article.find()\n  .withJsonApi(query)\n\narticles[0].title // 'JSON:API paints my bikeshed!'\n```\n\n### Query.prototype.toJsonApi()\n\n**Parameters:**\n\n- `opts`: *object* - Options\n  - `opts.baseUrl`: *string* - The base URL used in JSON:API links object\n  - `[opts.meta]`: *object* - The meta information to include in the JSON:API response body\n\n**Returns:**\n\n- *Query* - The Mongoose Query\n\n#### Example\n\n```typescript\nconst article = new Article({\n  title: 'Rails is Omakase',\n})\nawait article.save()\n\nconst articles = await Article.find()\n  .toJsonApi({\n    baseUrl: 'http://localhost:5000'\n  })\n\narticles.data[0].attributes.title // 'Rails is Omakase'\n```\n\n### Query.prototype.paginate()\n\n**Parameters:**\n\n- `opts`: *object* - Options\n  - `opts.url`: *string* - The current URL without query parameters\n  - `opts.query`: *JsonApiQueryParams* - The current JSON:API Query Parameters\n\n**Returns:**\n\n- *Query* - The Mongoose Query\n\n#### Example\n\n```typescript\nconst articles = await Article.find()\n  .toJsonApi({\n    baseUrl: 'http://localhost:5000'\n  })\n  .paginate({\n    url: 'http://localhost:5000/articles',\n    query: {},\n  })\n\narticles.links.first // 'http://localhost:5000/articles?page[limit]=10\u0026page[offset]=0'\n```\n\n## Contributing\n\nContributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\n1. Fork the project\n2. Create your feature branch (`git checkout -b feature/amazing-feature`)\n3. Commit your changes (`git commit -m 'feat: add some amazing feature'`)\n4. Push to the branch (`git push origin feature/amazing-feature`)\n5. Open a pull request\n\n## Author\n\n- [Lory-Stan TANASI](https://github.com/stantanasi)\n\n## License\n\nThis project is licensed under the `Apache-2.0` License - see the [LICENSE](LICENSE) file for details\n\n\u003cp align=\"center\"\u003e\n  \u003cbr /\u003e\n  © 2024 Lory-Stan TANASI. All rights reserved\n\u003c/p\u003e","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstantanasi%2Fmongoose-jsonapi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstantanasi%2Fmongoose-jsonapi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstantanasi%2Fmongoose-jsonapi/lists"}