tuxbear:collection-class 3.0.1

Meteor Collection Class returns ES6 class instances from Meteor Collections.

Platform: Meteor

Language: JavaScript

License: MIT

Repository: https://github.com/tuxbear/meteor-collection-class

View on registry: https://atmospherejs.com/tuxbear/collection-class

Install: meteor add tuxbear:collection-class@=3.0.1


Meteor Collection Class

Based on https://github.com/dburles/meteor-collection-helpers

Meteor Collection Class returns ES6 class instances from Meteor Collections. This allows you to create domain models with business logic in ES6 classes and tie them to collections. This acts like a lightweight document-to-object mapper

Supports property getters and setters and functions. Class instances can be saved directly.

Installation

$ meteor add tuxbear:collection-class

Usage

Keep your domain classes somewhere seen by both client and server.

class Book {

    constructor() {
        // constructor is invoked with no arguments
        // NOTE: fields or functions in the class will be overwritten if names conflict with the collection document
        this.title = 'this will be overwritten'
    }

    author() {
        return Authors.findOne(this.authorId);
    }

    collectionClassOnInit() {
        // all fields from the monogo document is on this. Use for init setup of object state if needed
        this.someState = 'hello';
    }
}

class Author {
    fullName() {
        return this.firstName + ' ' + this.lastName;
    }

    books() {
        return Books.find({authorId: this._id});
    }
}

// in collection modules:

Books = new Mongo.Collection('books');
Books.setClass(Book);

Authors = new Mongo.Collection('authors');
Authors.setClass(Author);

This will then allow you to do:

Books.findOne().author().firstName; // Charles
Books.findOne().someState; // hello
Books.findOne().author().fullName(); // Charles Darwin
Authors.findOne().books()

Polimorphism is supported via discriminator field values, by supplying a definition object instead of the constructor function to setClass:

class Animal {

    static get TYPE() { return "Animal"; }

    constructor() {
        this.type = Animal.TYPE;
        // constructor is invoked with no arguments
        // NOTE: fields or functions in the class will be overwritten if names conflict with the collection document
        this.name = 'this will be overwritten'
    }

    speak() {
        return "Animal speak";
    }

    collectionClassOnInit() {
        // all fields from the monogo document is on this. Use for init setup of object state if needed
    }
}

class Dog extends Animal {
    static get TYPE() { return "DOG"; }

    constructor() {
        this.type = Dog.TYPE;
        // constructor is invoked with no arguments
        // NOTE: fields or functions in the class will be overwritten if names conflict with the collection document
        this.name = 'this will be overwritten'
    }

    speak() {
        return "Dog speak";
    }

    collectionClassOnInit() {
        // all fields from the monogo document is on this. Use for init setup of object state if needed
    }


}

// in collection modules:

Animals = new Mongo.Collection('animals');
Animals.setClass({
    discriminatorField: 'type',
    types: {
        [Animal.TYPE]: Animal,
        [Dog.TYPE]: Dog
    }
});

License

MIT

Releases

  • 3.0.1 - December 21, 2016 15:46
  • 2.0.1 - September 25, 2016 11:30
  • 1.0.1 - July 18, 2016 22:53

Project Statistics

SourceRank 9
Dependent repositories 0
Total releases 3
Latest release
First release
Stars 0
Forks 0
Watchers 1
Contributors 1
Repo Size: 6.84 KB

Top Contributors See all

Ole - André Johansen

Something wrong with this page? Make a suggestion

Export .ABOUT file for this library

Last synced: 2016-12-22 08:38:03 UTC

Login to resync this project