2.0.4 • Published 6 years ago

simple-relations v2.0.4

Weekly downloads
6
License
MIT
Repository
github
Last release
6 years ago

simple-relations

Build Status

  • 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

npm install --save simple-relations

Usage example

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 } }), // default options for generated cursors
  });

  outgoingTransactions: HasManyRelation<Transaction, *> = 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<Account, *> = this.belongsTo('sourceAccount', {
    collection: () => Accounts,
  });

  targetAccount: BelongsToRelation<Account, *> = 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();
2.0.4

6 years ago

1.8.2

6 years ago

1.8.0

6 years ago

1.7.0

6 years ago

1.6.0

6 years ago

1.5.0

6 years ago

1.4.0

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago