following excellent meteor guide have tried play extending collection. ultimate aim create objects manage insertion , update values updated. i've tried order
class:
order = class order { constructor(customerid, orderdate){ this.document = {}; this.document.customerid = customerid; this.document.orderdate = orderdate; this.document._id = orders.insert(this.document); } // class methods go here. }
my orders
uniquely defined customer id , date. cannot make 2 orders on same day. therefore i've extended mongo.collection
class own ordercollection
class. sort of abstracts data layer ensure data therein remains remains unique it's key:
class ordercollection extends mongo.collection{ constructor(collectionname){ super(collectionname); this.attachschema( new simpleschema({ customerid: { type: string }, orderdate: { type: date } }) ); } insert(list, callback){ // check if actual order exists if (list._id){ return _id; } // won't insert new order if matching 1 exists. var order = orders.findone({ customerid: list.customerid, orderdate: list.orderdate }); if (order){return order._id;} // create order , return it. return super.insert(list, callback); } remove(selector, callback){ return super.remove(selector, callback); } } orders = new ordercollection("orders");
this seems work. when create new order it's either creating new one, or returns existing 1 correct _id. example when run meteor reset
, enter new order retrieve same _id
when stop meteor, restart , try same insert again:
{ document: { customerid: 'qb6reejirgxs3oj6r', orderdate: fri jan 29 2016 00:00:00 gmt+0000 (gmt), _id: 'awmygyspnxr86ks6d' } }
this , behaving expected. cannot seem retrieve objects through subscriptions or calls orders.find({}).fetch()
either on client.
even msavin:mongol tells me orders collection empty , if add new items through monogl interface, don't appear. doing wrong?
update
i'm using simpleschema , collections2 , wonder if somehow may not support way use this.
this beginner's issue. above worked, issue subscription on client, instead of instance.autorun(function...)
had written instance.autorun = function...
Comments
Post a Comment