activer
Base class for your JavaScript models that adds useful hasOne
, hasMany
, belongsTo
, hasAndBelongsToMany
, attributes
and delegate
static methods (as well as save
, update
and destroy
instance methods and a few callbacks).
Usage
in your user.js
:
; { console; } Userattributes'name' 'description';User;User;
in your comment.js
:
;; { console; } Commentattributes'name' 'description';Comment;
in your post.js
:
;;; { console; } Postattributes'name' 'description';Post;Post;
in your main.js
:
;; var user = User;var post = user;var comment1 = post;var comment2 = post; user; // 'Post'console; // 2console; // 2console; // "Comment 1 name"user;console; // 0console; // undefined
See tests for details.
hasMany { through: 'SomeClass' }
To be able to use the hasMany through association the join collection model should be implemented. See example below.
in your post.js
:
;;; {} Postattributes'name' 'description';Post;Post;
in your category.js
:
;;; {} Categoryattributes'name';Category;Category;
in your category_post.js
(please note class names in the join collection are sorted alphabetically, and they should be):
;;; {} CategoryPost;CategoryPost;
HABTM
To be able to use the hasAndBelongsToMany association the join collection model should be implemented. See example below.
in your post.js
:
;;; {} Postattributes'name' 'description';Post;
in your tag.js
:
;;; {} Tagattributes'name';Tag;
in your post_tag.js
(please note class names in the join collection are sorted alphabetically, and they should be):
;;; {} PostTag;PostTag;
Store
Activer uses in-memory storage by default but you can specify your own data access object to use whatewer you want using the static collection
method. DAO should implement some methods:
dataAccessObject = { /**/ } { /**/ } { /**/ } { /**/ } { /**/ } { /**/ } {}User
See default implementation in src/dao.coffee
.
Changelog
0.10.0: Model static methods all
and where
now return Collection instance. Collection instance method where
now returns new Collection instance. One can do User.all().where({ something: 'something' }).where({ anotherThing: 'Another thing' }).deleteAll()
now.
0.11.0: hasAndBelongsToMany
association implemented. hasMany { through }
association implemented.