https://github.com/opyh/simple-relations
Adds relational data accessors to documents in Meteor, inspired by Ruby on Rails’ ActiveRecord.
https://github.com/opyh/simple-relations
Last synced: 11 months ago
JSON representation
Adds relational data accessors to documents in Meteor, inspired by Ruby on Rails’ ActiveRecord.
- Host: GitHub
- URL: https://github.com/opyh/simple-relations
- Owner: opyh
- License: mit
- Created: 2018-01-04T23:35:38.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2018-12-23T23:43:47.000Z (over 7 years ago)
- Last Synced: 2025-06-28T19:22:18.413Z (12 months ago)
- Language: JavaScript
- Homepage:
- Size: 727 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-relations
[](https://travis-ci.org/opyh/simple-relations)
- Inspired by Ruby on Rails’ `ActiveRecord`
- Provides a `Document` base class that encapsules a plain MongoDB document
- Adds convenience data accessors like `thing.relatedOtherThings.findOne()`
- Supports belongs-to, has-many and has-many-through relations
- Supports `SimpleSchema`, creates schema definitions to validate relation ID attributes
- Typed using FlowType
- Comes with tests
## 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 = this.hasMany('ingoingTransactions', {
collection() { return Transactions; },
foreignKey: () => 'targetAccountId',
options: () => ({ sort: { insertedAt: -1 } }), // default options for generated cursors
});
outgoingTransactions: HasManyRelation = this.hasMany('outgoingTransactions', {
collection() { return Transactions; },
foreignKey: () => 'sourceAccountId',
options: () => ({ sort: { insertedAt: -1 } }), // default options for generated cursors
allowedIds: () => ['a', 'b'] // Limits assignable IDs in generated SimpleSchema
});
}
export default class Transaction extends Document {
sourceAccount: BelongsToRelation = this.belongsTo('sourceAccount', {
collection: () => Accounts,
});
targetAccount: BelongsToRelation = this.belongsTo('targetAccount', {
collection: () => Accounts,
});
}
const Accounts = new Meteor.Collection('Accounts', { transform: d => new Account(d) });
const Transactions = new Meteor.Collection('Transactions', { transform: d => new Transaction(d) });
// Generate some transactions and insert them into the database
['a', 'b'].forEach(_id => Accounts.insert({ _id });
Transactions.insert({ sourceAccountId: 'a', targetAccountId: 'b' });
Transactions.insert({ sourceAccountId: 'b', targetAccountId: 'a' });
// Use accessors to fetch related data from the database
const transactions = Accounts.findOne('a').ingoingTransactions.find().fetch();
const account = transactions[0].sourceAccount.findOne();
// Creates a SimpleSchema definition object
const TransactionSchema = Transaction.generateSimpleSchema();
```