# simple-mongo-relations

> Define relations between MongoDB documents and get simple-to-use helpers.

Latest version **1.0.0** (published 2018-02-03) · MIT license · 0 weekly downloads

## Install

```sh
npm install simple-mongo-relations
pnpm add simple-mongo-relations
yarn add simple-mongo-relations
bun add simple-mongo-relations
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.0.0 |
| Published | 2018-02-03 |
| First published | 2018-02-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 3 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Sebastian Felix Zappe |
| Maintainers | hypo808 |

## Links

- npm: https://www.npmjs.com/package/simple-mongo-relations
- Repository: https://github.com/opyh/simple-relations
- Homepage: https://github.com/opyh/simple-relations#readme
- Issues: https://github.com/opyh/simple-relations/issues
- npm.io page: https://npm.io/package/simple-mongo-relations

## Dependencies (3)

- [inflection](https://npm.io/package/inflection.md) ^1.12.0
- [lodash.memoize](https://npm.io/package/lodash.memoize.md) ^4.1.2
- [underscore.string](https://npm.io/package/underscore.string.md) ^3.3.4

## Recent versions

- 1.0.0 (latest) — 2018-02-03

## README

# simple-mongo-relations

- Adds relational data support to Meteor collections, inspired by Ruby on Rails’ `ActiveRecord`
- Supports has-many, belongs-to and has-many-through relations
- Creates `SimpleSchema` definitions to validate data
- Uses FlowType

## Installation

```bash
npm install --save simple-relations
```

## Usage example

```javascript
import { Meteor } from 'meteor/meteor';
import { Document, Model } from 'simple-relations';
import type { HasManyRelation, BelongsToRelation } from 'simple-relations';

let Accounts;
let Transactions;

class Account extends Document {
  ingoingTransactions: HasManyRelation<Transaction, *> = this.hasMany('ingoingTransactions', {
    collection() { return Transactions; },
    foreignKey: () => 'targetAccountId',
    options: () => ({ sort: { insertedAt: -1 } }),
  });

  outgoingTransactions: HasManyRelation<Transaction, *> = this.hasMany('outgoingTransactions', {
    collection() { return Transactions; },
    foreignKey: () => 'sourceAccountId',
    options: () => ({ sort: { insertedAt: -1 } }),
  });
}

export default class Transaction extends Document {
  sourceAccount: BelongsToRelation<Account, *> = this.belongsTo('sourceAccount', {
    collection: () => Accounts,
  });

  targetAccount: BelongsToRelation<Account, *> = this.belongsTo('targetAccount', {
    collection: () => Accounts,
  });
}

Accounts = new Meteor.Collection('Accounts', { transform: d => new Account(d) });
Transactions = new Meteor.Collection('Transactions', { transform: d => new Transaction(d) });

['a', 'b'].forEach(_id => Accounts.insert({ _id });
Transactions.insert({ sourceAccountId: 'a', targetAccountId: 'b' });

const transactions = Accounts.findOne('a').ingoingTransactions.find().fetch();
const account = transactions[0].sourceAccount.findOne();

const AccountRelationSchema = Account.generateSimpleSchema()
```

---
_Source: https://npm.io/package/simple-mongo-relations · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
