Meteor Mongo Collection Factory
Create Mongo collections. Isomorphic. Lightweight. Simple.
With this package you can define factory functions to create a variety of Mongo.Collection instances. Decouples definition from instantiation (also for the schema) and allows different configurations for different types of collections.
Minified size < 2KB!
Installation
Simply add this package to your meteor packages
$ meteor add leaonline:collection-factory
Usage
Import the createCollectionFactory
method and create the factory function from it:
1import { createCollectionFactory } from 'meteor/leaonline:collection-factory' 2 3const createCollection = createCollectionFactory() // no params = use defaults 4const FancyCollection = createCollection({ name: 'fancy' }) // minimal required
With schema
We support aldeed:collection2
which itself requires a schema
to be attached.
To decouple schema definition from instantiation, we introduced a shemaFactory
, which
is basically a function that creates your schema for this collection. This also ensures, that
collections don't share the same schema instances:
1import { createCollectionFactory } from 'meteor/leaonline:collection-factory' 2import SimpleSchema from 'simpl-schema' 3 4const schemaFactory = definitions => new SimpleSchema(definitions) 5 6const createCollection = createCollectionFactory({ schemaFactory }) 7const FancyCollection = createCollection({ 8 name: 'fancy', 9 schema: { 10 title: String, 11 points: { 12 type: Number, 13 min: 0, 14 max: 100 15 } 16 } 17})
With custom Mongo.Collection
You can extend the Mongo.Collection
and pass it to the factory as well.
Note, that you need to inherit from Mongo.Collection
, solely custom classes are not
supported.
1import { createCollectionFactory } from 'meteor/leaonline:collection-factory' 2import { Mongo } from 'meteor/mongo' 3 4class CustomCollection extends Mongo.Collection { 5 insert(doc, callback) { 6 // there are better ways to do this 7 // but this is a good way to show it 8 doc.createdAt = new Date() 9 10 return super.insert(doc, callback) 11 } 12} 13 14const createCollection = createCollectionFactory({ custom: CustomCollection }) 15const FancyCollection = createCollection({ name: 'fancy' }) 16const insertId = FancyCollection.insert({ title: 'some fancy'}) 17FancyCollection.findOne(insertId) // { title: 'some fancy', createdAt: ISODate("2020-04-20T10:19:54.552Z") }
You can combine both examples but note, that if you require a schema, then the above example would require an additional
createdAt
definition in the schema.
With existing collection
There are use cases where an existing collection is already defined and you want to attach the schema but also keep your creational pipeline going.
In this case you just pass in the existing collection with a schema:
1import { createCollectionFactory } from 'meteor/leaonline:collection-factory' 2import SimpleSchema from 'simpl-schema' 3 4const schemaFactory = definitions => new SimpleSchema(definitions) 5 6const extendCollection = createCollectionFactory({ schemaFactory }) 7extendCollection({ 8 collection: Meteor.users, 9 schema: { 10 username: String, 11 // ... etc. 12 } 13})
Codestyle
We use standard
as code style and for linting.
via npm
$ npm install --global standard snazzy $ standard | snazzy
via Meteor npm
$ meteor npm install --global standard snazzy $ standard | snazzy
History
- 1.0.3
- accept
null
values as name to create local collections
- accept
Test
We use meteortesting:mocha
to run our tests on the package.
Watch mode
$ TEST_WATCH=1 TEST_CLIENT=0 meteor test-packages ./ --driver-package meteortesting:mocha