Permalink
Browse files

Added comments array to products' schema

Collection instances don't reopen the db is still connected
  • Loading branch information...
1 parent 911c33a commit 1f43895056e662f7dc62bbfcd8fb029df7b5b9df @flesler committed Feb 1, 2012
Showing with 41 additions and 8 deletions.
  1. +20 −7 lib/db/collection.js
  2. +21 −1 lib/db/schemas.js
View
@@ -4,22 +4,34 @@ var ObjectID = mongo.ObjectID;
function Collection(name, schema) {
this.name = name;
this.schema = schema;
- this.binaryId = schema.idField == null;
+ this.hasBinaryId = schema.idField == null;
}
Collection.prototype = {
+ constructor: Collection,
+
_open:function(callback) {
- // TODO: Maybe this only needs to be done once
- // and the collection object can be cached
- var name = this.name;
+ if (this.collection) {
+ return callback(null, this.collection);
+ }
+ // No need to open over and over, at least locally it works
+ if (this.db.state === 'connected') {
+ return this._createCollection(callback);
+ }
+
+ var self = this;
this.db.open(function (err, db) {
if (err) return callback(err);
- var collection = new mongo.Collection(db, name);
- callback(null, collection);
+ self._createCollection(callback);
});
},
+ _createCollection:function(callback) {
+ this.collection = new mongo.Collection(this.db, this.name);
+ callback(null, this.collection);
+ },
+
_run:function(data, callback, partial, action) {
if (action === undefined) {
action = partial;
@@ -55,6 +67,7 @@ Collection.prototype = {
remove:function(query, callback) {
this._open(query, function(err, collection) {
+ if (err) return callback(err);
collection.remove(query, {safe:true}, callback);
});
},
@@ -90,7 +103,7 @@ Collection.prototype = {
},
byId:function(id, callback) {
- if (this.binaryId) {
+ if (this.hasBinaryId) {
id = ObjectID.createFromHexString(id);
}
View
@@ -13,10 +13,12 @@ function Schema(name, fields) {
Schema.prototype = {
+ constructor: Schema,
+
initFields:function(fields){
var fs = this.fields;
for (var key in fs) {
- if (typeof fs[key] !== 'object') {
+ if (!fs[key] || fs[key].constructor !== Object) {
fs[key] = {type:fs[key]};
}
@@ -172,6 +174,12 @@ var ObjectOf = function(type) {
};
};
+var Optional = function(type) {
+ return function(obj) {
+ return obj != null ? parse(obj, type) : null;
+ };
+};
+
//- Defaults
var now = function(){
@@ -185,6 +193,17 @@ var Category = new Schema('Category', {
filters:ObjectOf(ArrayOf(RealString))
});
+var Post = new Schema('Post', {
+ text:RealString,
+ owner:RealString,
+ created:{type:DateType, default:now}
+});
+
+var QA = new Schema('QA', {
+ question: Post,
+ answer: Optional(Post)
+});
+
//- Exposed Schemas
exports.user = new Schema('User', {
@@ -201,5 +220,6 @@ exports.product = new Schema('Product', {
category:RealString, // TODO: Check is a category
owner:RealString,
props:ObjectOf(RealString), // TODO: Should validate is a filter id
+ questions: {type:ArrayOf(QA), default:[]},
created:{type:DateType, default:now}
});

0 comments on commit 1f43895

Please sign in to comment.