From 6a78ab4020c88828051c7800dee2b1da002d5322 Mon Sep 17 00:00:00 2001 From: Eric Dobbertin Date: Thu, 27 Aug 2015 01:50:26 -0500 Subject: [PATCH] Track attached schema on LocalCollection object, too --- .jshintrc | 3 ++- collection2.js | 15 +++++++++++---- package.js | 1 + tests/tests.js | 31 ++++++++++++++++++++++++++++++- 4 files changed, 44 insertions(+), 6 deletions(-) diff --git a/.jshintrc b/.jshintrc index dec69cc..d89aa16 100644 --- a/.jshintrc +++ b/.jshintrc @@ -160,6 +160,7 @@ "booksSubscription": true, "partOne": true, "partTwo": true, - "partThree": true + "partThree": true, + "LocalCollection": false } } diff --git a/collection2.js b/collection2.js index 3f1bae8..86ddc76 100644 --- a/collection2.js +++ b/collection2.js @@ -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, { @@ -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) { diff --git a/package.js b/package.js index 0ea3beb..e36a6c2 100644 --- a/package.js +++ b/package.js @@ -16,6 +16,7 @@ Package.onUse(function(api) { api.use('check@1.0.0'); api.use('mongo@1.0.4'); api.imply('mongo'); + api.use('minimongo@1.0.0'); api.use('ejson@1.0.0'); // Allow us to detect 'insecure'. diff --git a/tests/tests.js b/tests/tests.js index 1ffc4a1..06cc4d4 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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"); @@ -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(); });