{"id":28739882,"url":"https://github.com/opyh/simple-relations","last_synced_at":"2025-07-05T03:10:17.523Z","repository":{"id":57360465,"uuid":"116318195","full_name":"opyh/simple-relations","owner":"opyh","description":"Adds relational data accessors to documents in Meteor, inspired by Ruby on Rails’ ActiveRecord.","archived":false,"fork":false,"pushed_at":"2018-12-23T23:43:47.000Z","size":744,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-28T19:22:18.413Z","etag":null,"topics":[],"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/opyh.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":"2018-01-04T23:35:38.000Z","updated_at":"2018-12-23T23:43:46.000Z","dependencies_parsed_at":"2022-09-06T22:30:52.233Z","dependency_job_id":null,"html_url":"https://github.com/opyh/simple-relations","commit_stats":null,"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"purl":"pkg:github/opyh/simple-relations","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyh%2Fsimple-relations","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyh%2Fsimple-relations/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyh%2Fsimple-relations/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyh%2Fsimple-relations/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/opyh","download_url":"https://codeload.github.com/opyh/simple-relations/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/opyh%2Fsimple-relations/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263674489,"owners_count":23494584,"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":"2025-06-16T06:11:11.888Z","updated_at":"2025-07-05T03:10:17.505Z","avatar_url":"https://github.com/opyh.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# simple-relations\n\n[![Build Status](https://travis-ci.org/opyh/simple-relations.svg?branch=master)](https://travis-ci.org/opyh/simple-relations)\n\n- Inspired by Ruby on Rails’ `ActiveRecord`\n- Provides a `Document` base class that encapsules a plain MongoDB document\n- Adds convenience data accessors like `thing.relatedOtherThings.findOne()`\n- Supports belongs-to, has-many and has-many-through relations\n- Supports `SimpleSchema`, creates schema definitions to validate relation ID attributes\n- Typed using FlowType\n- Comes with tests\n\n## Installation\n\n```bash\nnpm install --save simple-relations\n```\n\n## Usage example\n\n```javascript\nimport { Meteor } from 'meteor/meteor';\nimport { Document, Model } from 'simple-relations';\nimport type { HasManyRelation, BelongsToRelation } from 'simple-relations';\n\nlet Accounts;\nlet Transactions;\n\n\nclass Account extends Document {\n  ingoingTransactions: HasManyRelation\u003cTransaction, *\u003e = this.hasMany('ingoingTransactions', {\n    collection() { return Transactions; },\n    foreignKey: () =\u003e 'targetAccountId',\n    options: () =\u003e ({ sort: { insertedAt: -1 } }), // default options for generated cursors\n  });\n\n  outgoingTransactions: HasManyRelation\u003cTransaction, *\u003e = this.hasMany('outgoingTransactions', {\n    collection() { return Transactions; },\n    foreignKey: () =\u003e 'sourceAccountId',\n    options: () =\u003e ({ sort: { insertedAt: -1 } }),  // default options for generated cursors\n    allowedIds: () =\u003e ['a', 'b'] // Limits assignable IDs in generated SimpleSchema\n  });\n}\n\n\nexport default class Transaction extends Document {\n  sourceAccount: BelongsToRelation\u003cAccount, *\u003e = this.belongsTo('sourceAccount', {\n    collection: () =\u003e Accounts,\n  });\n\n  targetAccount: BelongsToRelation\u003cAccount, *\u003e = this.belongsTo('targetAccount', {\n    collection: () =\u003e Accounts,\n  });\n}\n\nconst Accounts = new Meteor.Collection('Accounts', { transform: d =\u003e new Account(d) });\nconst Transactions = new Meteor.Collection('Transactions', { transform: d =\u003e new Transaction(d) });\n\n// Generate some transactions and insert them into the database\n['a', 'b'].forEach(_id =\u003e Accounts.insert({ _id });\nTransactions.insert({ sourceAccountId: 'a', targetAccountId: 'b' });\nTransactions.insert({ sourceAccountId: 'b', targetAccountId: 'a' });\n\n// Use accessors to fetch related data from the database\nconst transactions = Accounts.findOne('a').ingoingTransactions.find().fetch();\nconst account = transactions[0].sourceAccount.findOne();\n\n// Creates a SimpleSchema definition object\nconst TransactionSchema = Transaction.generateSimpleSchema();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopyh%2Fsimple-relations","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopyh%2Fsimple-relations","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopyh%2Fsimple-relations/lists"}