Skip to content

Commit

Permalink
Track attached schema on LocalCollection object, too
Browse files Browse the repository at this point in the history
  • Loading branch information
aldeed committed Aug 27, 2015
1 parent 37c99f3 commit 6a78ab4
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
"booksSubscription": true,
"partOne": true,
"partTwo": true,
"partThree": true
"partThree": true,
"LocalCollection": false
}
}
15 changes: 11 additions & 4 deletions collection2.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
// Track the schema in the collection
self._c2._simpleSchema = ss;

if (self._collection instanceof LocalCollection) {
self._collection._c2 = self._collection._c2 || {};
self._collection._c2._simpleSchema = ss;
}

function ensureIndex(c, index, indexName, unique, sparse) {
Meteor.startup(function () {
c._collection._ensureIndex(index, {
Expand Down Expand Up @@ -141,10 +146,12 @@ Mongo.Collection.prototype.attachSchema = function c2AttachSchema(ss, options) {
keepInsecure(self);
};

Mongo.Collection.prototype.simpleSchema = function c2SS() {
var self = this;
return self._c2 ? self._c2._simpleSchema : null;
};
_.each([Mongo.Collection, LocalCollection], function (obj) {
obj.prototype.simpleSchema = function () {
var self = this;
return self._c2 ? self._c2._simpleSchema : null;
};
});

// Wrap DB write operation methods
_.each(['insert', 'update'], function(methodName) {
Expand Down
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Package.onUse(function(api) {
api.use('[email protected]');
api.use('[email protected]');
api.imply('mongo');
api.use('[email protected]');
api.use('[email protected]');

// Allow us to detect 'insecure'.
Expand Down
31 changes: 30 additions & 1 deletion tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,36 @@ Tinytest.addAsync('Collection2 - Reset', function (test, next) {
Meteor.call("removeAll", next);
});

Tinytest.add('Collection2 - Mongo.Collection - simpleSchema', function (test) {
var mc = new Mongo.Collection('mc');

mc.attachSchema(new SimpleSchema({
foo: {type: String}
}));

test.instanceOf(mc.simpleSchema(), SimpleSchema);

// It should work on the LocalCollection instance, too
if (Meteor.isClient) {
test.instanceOf(mc._collection, LocalCollection);
test.instanceOf(mc._collection.simpleSchema(), SimpleSchema);
}
});

Tinytest.add('Collection2 - LocalCollection - simpleSchema', function (test) {
var lc = new Mongo.Collection(null);

lc.attachSchema(new SimpleSchema({
foo: {type: String}
}));

test.instanceOf(lc.simpleSchema(), SimpleSchema);

// It should work on the LocalCollection instance, too
test.instanceOf(lc._collection, LocalCollection);
test.instanceOf(lc._collection.simpleSchema(), SimpleSchema);
});

// Attach more than one schema
Tinytest.add('Collection2 - Attach Multiple Schemas', function (test) {
var c = new Mongo.Collection("multiSchema");
Expand Down Expand Up @@ -729,7 +759,6 @@ if (Meteor.isServer) {
av: 'abc'
}
}, function (error, result) {
console.log(error, result, times);
test.equal(times, 1, 'AutoValue functions should run only once for an upsert');
next();
});
Expand Down

0 comments on commit 6a78ab4

Please sign in to comment.