Skip to content

Commit

Permalink
Meteor 0.9.1+ compatibility take 1
Browse files Browse the repository at this point in the history
  • Loading branch information
wheeler committed Sep 23, 2014
1 parent ced16fe commit 30c0ea5
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ $ mrt add graviton

# API Docs

## Meteor.Collection.prototype
## Mongo.Collection.prototype
The following are added to all your meteor collections:
* `all()` is an alias for `find().fetch()`
* `build()` returns a new local `Gravition.Model` based on your collection definition. Does not save to db. The instance can be saved using `Model.save()`.
* `create()` calls build() to generate a Model instance then inserts it into the db.

## Graviton
* `Graviton.define(collectionName, options)` Use to define your collections. Returns a Meteor.Collection instantiated with a transform function based on the options passed.
* `Graviton.define(collectionName, options)` Use to define your collections. Returns a Mongo.Collection instantiated with a transform function based on the options passed.

*Options*
* `persist` Passing false will define a local collection. defaults to `true`
Expand Down Expand Up @@ -119,7 +119,7 @@ car.windows.at(2); // only builds one model

Graviton transforms collection objects into Models for you. This allows them to carry useful metadata and functions with the data. The vanilla Graviton.Model allows for basic functionality. Defining an extension of the Graviton.Model allows you to specify details of the collection's relationships or other custom functionality.

* `Graviton.Model.extend({Options}, {ExtensionPrototype});` Use to define your collections. Returns a Meteor.Collection instantiated with a transform function based on the options passed.
* `Graviton.Model.extend({Options}, {ExtensionPrototype});` Use to define your collections. Returns a Mongo.Collection instantiated with a transform function based on the options passed.

*Options*
* `defaults`: an object containing default key:value pairs for the collection. These key:values will be added to all model instances where there is not already a stored value with the same key. Functions should not be placed here as stored records cannot have functions as values.
Expand Down
12 changes: 7 additions & 5 deletions graviton.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ Meteor.startup(function() {

/**
*
* Meteor.Collection.prototype
* Mongo.Collection.prototype
*
*/

CollectionClass = (typeof Mongo !== 'undefined') ? Mongo.Collection : Meteor.Collection;

// all() convenience method == find().fetch()
Meteor.Collection.prototype.all = ManyRelation.prototype.all;
CollectionClass.prototype.all = ManyRelation.prototype.all;

// build an instance of this collections model type but do not save it to the db
// returns the built model.
Meteor.Collection.prototype.build = function(obj) {
CollectionClass.prototype.build = function(obj) {
if (!_.isObject(obj)) obj = {};
var mdl = this._graviton.model(obj);
mdl._id = obj._id;
return mdl;
};

// does an insert but builds a model first, returns the model instead of an id
Meteor.Collection.prototype.create = function(obj, callback) {
CollectionClass.prototype.create = function(obj, callback) {
var model = this.build(obj);
var id;
if (callback) {
Expand Down Expand Up @@ -155,7 +157,7 @@ Graviton.define = function(collectionName, options) {

var colName = (options.persist) ? collectionName : null;

var collection = new Meteor.Collection(colName, {
var collection = new CollectionClass(colName, {
transform: options.model
});

Expand Down
4 changes: 2 additions & 2 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
// model instances have a reference to the collection they came from
// relations are also defined with collections and added to models when they are instantiated
Model = function(collection, obj) {
if (!(collection instanceof Meteor.Collection))
throw new Error("Models must be instantiated with a Meteor.Collection");
if (!(collection instanceof CollectionClass))
throw new Error("Models must be instantiated with a Mongo.Collection");

if (!_.isObject(obj) || _.isFunction(obj))
obj = new Object;
Expand Down
2 changes: 1 addition & 1 deletion lib/relations.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// return a defined collection based on relation configuration
var getCollection = function(config) {
var collection;
if (config.collection && (config.collection instanceof Meteor.Collection)) {
if (config.collection && (config.collection instanceof CollectionClass)) {
collection = config.collection;
} else {
var name = config.collectionName || config.collection || config.klass;
Expand Down
6 changes: 6 additions & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ Package.describe({
});

Package.on_use(function (api, where) {

if (api.versionsFrom) { // 0.9.0+ litmus test
api.versionsFrom("0.9.1");
api.use('mongo', ['client', 'server']);
}

api.use(['underscore', 'minimongo', 'async'], ['client', 'server']);
api.add_files(['lib/relations.js', 'lib/model.js', 'graviton.js'], ['client', 'server']);

Expand Down
2 changes: 1 addition & 1 deletion test/legacy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ Driver = Graviton.define("drivers", {
init(Driver);

Plate = Graviton.define("plates", {
persist: false // sends null as mongo collection name to Meteor.Collection
persist: false // sends null as mongo collection name to Mongo.Collection
});

Window = Graviton.define("windows", {
Expand Down
2 changes: 1 addition & 1 deletion test/relation-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ var Driver = Graviton.define("rt-drivers", {
init(Driver);

var Plate = Graviton.define("rt-plates", {
persist: false // sends null as mongo collection name to Meteor.Collection
persist: false // sends null as mongo collection name to Mongo.Collection
});

var Window = Graviton.define("rt-windows", {
Expand Down
46 changes: 43 additions & 3 deletions versions.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,39 @@
{
"dependencies": [
[
"application-configuration",
"1.0.2"
],
[
"async",
"0.0.0"
],
[
"base64",
"1.0.0"
],
[
"binary-heap",
"1.0.0"
],
[
"callback-hook",
"1.0.0"
],
[
"check",
"1.0.0"
],
[
"ddp",
"1.0.8"
],
[
"ejson",
"1.0.2"
],
[
"follower-livedata",
"1.0.1"
],
[
Expand All @@ -21,12 +49,20 @@
"1.0.0"
],
[
"meteor",
"logging",
"1.0.3"
],
[
"meteor",
"1.1.0"
],
[
"minimongo",
"1.0.2"
"1.0.3"
],
[
"mongo",
"1.0.5"
],
[
"ordered-dict",
Expand All @@ -36,6 +72,10 @@
"random",
"1.0.0"
],
[
"retry",
"1.0.0"
],
[
"tracker",
"1.0.2"
Expand All @@ -46,6 +86,6 @@
]
],
"pluginDependencies": [],
"toolVersion": "meteor-tool@1.0.28",
"toolVersion": "meteor-tool@1.0.31",
"format": "1.0"
}

0 comments on commit 30c0ea5

Please sign in to comment.