{"id":16188282,"url":"https://github.com/konsumer/mongoose-type-relation","last_synced_at":"2025-04-02T02:31:51.486Z","repository":{"id":57302318,"uuid":"29914188","full_name":"konsumer/mongoose-type-relation","owner":"konsumer","description":"A field-type for Mongoose schemas that allows easy relationships between models","archived":false,"fork":false,"pushed_at":"2016-03-15T02:13:24.000Z","size":7,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-16T07:04:39.030Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/konsumer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-01-27T13:00:03.000Z","updated_at":"2017-09-21T21:23:11.000Z","dependencies_parsed_at":"2022-09-20T16:41:20.580Z","dependency_job_id":null,"html_url":"https://github.com/konsumer/mongoose-type-relation","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/konsumer%2Fmongoose-type-relation","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konsumer%2Fmongoose-type-relation/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konsumer%2Fmongoose-type-relation/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/konsumer%2Fmongoose-type-relation/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/konsumer","download_url":"https://codeload.github.com/konsumer/mongoose-type-relation/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246741119,"owners_count":20826066,"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":[],"created_at":"2024-10-10T07:25:48.328Z","updated_at":"2025-04-02T02:31:51.213Z","avatar_url":"https://github.com/konsumer.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-type-relation\n\nA field-type for Mongoose schemas that allows easy relationships between models.\n\nThis field-type will populate external model references. If you update either side of the the relationship, it will update it on both.\n\n[![npm](https://nodei.co/npm/mongoose-type-relation.png)](https://www.npmjs.com/package/mongoose-type-relation)\n[![Build Status](https://travis-ci.org/konsumer/mongoose-type-relation.svg?branch=master)](https://travis-ci.org/konsumer/mongoose-type-relation)\n[![Code Climate](https://codeclimate.com/github/konsumer/mongoose-type-relation/badges/gpa.svg)](https://codeclimate.com/github/konsumer/mongoose-type-relation)\n\n## usage\n\n```javascript\nvar mongoose = require('mongoose');\nvar Relation = require('mongoose-type-relation');\n```\n\nThe module exports it's field-type, but also adds it to `mongoose.SchemaTypes` as `mongoose.SchemaTypes.Relation`, like other fieldtypes.\n\n### example\n\n```javascript\nvar UserSchema = new mongoose.Schema({\n    authored: [{type:Relation, ref:'Post', fieldref: 'author'}],\n    edited: [{type:Relation, ref:'Post', fieldref:'editors'}]\n});\nvar User = mongoose.model('User', UserSchema);\n\nvar PostSchema = new mongoose.Schema({\n    author: {type:Relation, ref:'User'},\n    editors: [{type:Relation, ref:'User'}]\n});\nvar Post = mongoose.model('Post', PostSchema);\n```\n\nIn this example, if you add Posts to `User.edited` or `User.authored`, they will be added to the Post's corresponding fields and vice-versa.  You still need to save all the records involved.\n\nYou should always add relationships with the whole object:\n\n```javascript\nvar post = new Post();\nvar author = new User();\nvar editor1 = new User();\nvar editor2 = new User();\n\npost.author = author;\npost.editors = [editor1, editor2];\n```\n\nUsing plain `_id` triggers \"manual mode\", meaning that you are manually managing the relationship (like standard mongoose `ObjectId`s.)\n\nHave a look at the tests for more examples.\n\n### populate\n\nSince ths uses standard mongoose references over `ObjectId`s, you can fill record's children with [`populate()`](http://mongoosejs.com/docs/populate.html).","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonsumer%2Fmongoose-type-relation","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkonsumer%2Fmongoose-type-relation","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkonsumer%2Fmongoose-type-relation/lists"}