Meteor package for easily creating classes for transformation of collections
meteor add maximum:document-classes
create a class, that extends DocumentClass.Base
Posts = new Mongo.Collection 'posts'
class Post extends DocumentClass.Base
# define your functions here
# test: -> return true
# Class.isTransformOf(Collection) transforms documents
# of the given collection to an instance of your class.
Post.isTransformOf Posts
mostly you'll have one class for one collection. In this case, you can use the 'collection' mixin.
class Post extends DocumentClass.Base
@mixin 'collection'
@collection ->
# with the collection function, you can define the
# instantiation of the collection for this class.
# You can also pass parameters or make use of
# collection-instances.
return new Mongo.Collection 'posts'
# we can get our collection with Class.Collection()
# documents of this collection will automatically transformed to our class
Posts = Post.Collection()
It's also possible to use javascript. Use extend for extending classes
var Posts = new Mongo.Collection('posts');
var Post = DocumentClass.Base.extend(function(){
this.isTransformOf(Posts);
this.staticFunction = function() {
return 'static';
};
this.prototype.instanceFunction = function() {
return 'instance';
};
return this;
});
document classes are extensible with mixins. You can add mixins, with Class.mixins()
class Post extends DocumentClass.Base
# the mixins function takes one or multiple mixin-names
@mixin 'collection', 'schema'
# you can also pass an array of mixins, instead of multiple parameters
the following mixins are available by default
Notice: the transform mixin is included by default and is only listed here to have a complete list
provides the isTransformOf
function to transform a collection to this class.
provides the ability to define a collection for a class.
example:
class Post extends DocumentClass.Base
@mixin 'collection'
@collection ->
# with the collection function, you can define the
# instantiation of the collection for this class.
# You can also pass parameters or make use of
# collection-instances.
return new Mongo.Collection 'posts'
# we can get our collection with Class.Collection()
# documents of this collection will automatically transformed to our class
Posts = Post.Collection()
add a schema to your class.
example:
class Post extends DocumentClass.Base
@mixin 'schema'
@schema new SimpleSchema
name:
type: String
provides the remove()
function, to delete the current document.
provides the update(modifier)
function, to update the current document.
you can add your own mixin with calling DocumentClass.Mixins.add(name, mixin)
.
Pass a class that extends DocumentClass.Mixins.Base
as the second parameter.
example:
DocumentClass.Mixins.add 'example', class ExampleMixin extends DocumentClass.Mixins.Base
@static:
# static functions
@instance:
# instance functions