Compatible with (optional):
- aldeed:collection2-core + simpl-schema
- dburles:mongo-collection-instances
- dburles:burles:collection-helpers
Changelog
0.1.9
- dropped dependency to
dburles:mongo-collection-instances
, only deps left areecmascript
andmongo
- isomorphic codebase (no server/client related execution branching)
- hooks are thus not restricted to server environment anymore
- HookNames are exported
- Renamed hooks (may cause breaks with previous config, please refactor)
- insertAfter ==> afterInsert
- updateAfter ==> afterUpdate
- removeAfter ==> afterRemove
0.1.8
- toggle hooks (on/off)
- removed try/catch from insert/update/remove as this should be responsibility of the surrounding execution context
0.1.7
- Remove dependencies to check npm versions
- Remove SimpleSchema indirect dependency (it is totally optional now)
- Remove hard dep to dburles:burles:collection-helpers (still working optional)
- Update test code and test project to Meteor 1.8
0.1.4
- Removed internal dependency to SimpleSchemaFactory
- Included tmeasday:check-npm-versions
0.1.2
- Fixed compatibility issues with hwillson:stub-collections
API
createCollection
1CollectionFactory.createCollection(definitionsObject);
Creates a new Mongo.Collection instance by a given definitions object. Minimum required attribute is name.
name:String
- The name of the collection
Most basic example
1const Todos = CollectionFactory.createCollection({ 2 name : "MY_COLLECTION_NAME", 3});
Optional Attributes
The following attributes for the parameter are accepted:
options:Object
- Mongo.Collection options
schema:Object
or SimpleSchema instance - attaches the schema to the collection. If it is an object, creates a new SimpleSchema from object. See:
explicit:Boolean
- if true it inserts and immediately removes a temp doc to enforce explicit creation
publicFields:Object
- A set of public fields as used by publications.
helpersObj:Object
- To be used to attach helpers. See:
Hooks
There are hooks for insert, update, remove, as well as afterInsert, afterUpdate, afterRemove available on server.
insert: function(doc, callback, cb) {}
update: function(query, modifier, options, callback) {}
remove: function(selector, callback) {}
afterInsert: function(doc, callback, cb, insertResult) {}
afterUpdate: function(query, modifier, options, callback, updateResult) {}
afterRemove: function(selector, callback, removeResult) {}
The after hooks are also called, if an error has occured. In this case the result parameter will be the error.
Note, that the this
context is bound to the collection itself, so use this to make use of the collection.
More extended example
1const Todos = CollectionFactory.createCollection({ 2 name : "todos", 3 schema: { 4 title: String, 5 createdAt: Number, 6 createdBy: String, 7 checked: {type: Boolean, defaultValue: false, }, 8 description: String, 9 }, 10 explicit: true, 11 publicFields: { 12 title: 1, 13 description: 1, 14 checked: 1, 15 }, 16 insert:function(doc, callback, cb) { 17 doc.createdAt = new Date().getTime(); 18 doc.createdBy = Meteor.userId(); 19 }, 20 afterUpdate: function(query, modifier, options, callback, updateResult){ 21 if (updateResult) { 22 // you may log collection updates 23 Logger.log("info", "doc updated", Meteor.userId()) 24 25 // or execute actions immediately 26 const updatedDoc = this.find(query); 27 if (updatedDoc.checked){ 28 //...notify creator of the todo... 29 } 30 } 31 }, 32});
hasCollection
1CollectionFactory.hasCollection('name')
Uses Mongo.Collection instances to check for existence. See https://github.com/dburles/mongo-collection-instances
getCollection
1CollectionFactory.getCollection('name')
Has the same effect as Mongo.Collection.get('name'). See https://github.com/dburles/mongo-collection-instances
dropCollection
1CollectionFactory.dropCollection('name')
Drops a collection by name.