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 } }),
allowedIds: () => ['a', 'b']
});
}
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) });
['a', 'b'].forEach(_id => Accounts.insert({ _id });
Transactions.insert({ sourceAccountId: 'a', targetAccountId: 'b' });
Transactions.insert({ sourceAccountId: 'b', targetAccountId: 'a' });
const transactions = Accounts.findOne('a').ingoingTransactions.find().fetch();
const account = transactions[0].sourceAccount.findOne();
const TransactionSchema = Transaction.generateSimpleSchema();