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!
Simply add this package to your meteor packages
$ meteor add leaonline:collection-factory
Import the createCollectionFactory
method and create the factory function from it:
import { createCollectionFactory } from 'meteor/leaonline:collection-factory'
const createCollection = createCollectionFactory() // no params = use defaults
const FancyCollection = createCollection({ name: 'fancy' }) // minimal required
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:
import { createCollectionFactory } from 'meteor/leaonline:collection-factory'
import SimpleSchema from 'simpl-schema'
const schemaFactory = definitions => new SimpleSchema(definitions)
const createCollection = createCollectionFactory({ schemaFactory })
const FancyCollection = createCollection({
name: 'fancy',
schema: {
title: String,
points: {
type: Number,
min: 0,
max: 100
}
}
})
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.
import { createCollectionFactory } from 'meteor/leaonline:collection-factory'
import { Mongo } from 'meteor/mongo'
class CustomCollection extends Mongo.Collection {
insert(doc, callback) {
// there are better ways to do this
// but this is a good way to show it
doc.createdAt = new Date()
return super.insert(doc, callback)
}
}
const createCollection = createCollectionFactory({ custom: CustomCollection })
const FancyCollection = createCollection({ name: 'fancy' })
const insertId = FancyCollection.insert({ title: 'some fancy'})
FancyCollection.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.
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:
import { createCollectionFactory } from 'meteor/leaonline:collection-factory'
import SimpleSchema from 'simpl-schema'
const schemaFactory = definitions => new SimpleSchema(definitions)
const extendCollection = createCollectionFactory({ schemaFactory })
extendCollection({
collection: Meteor.users,
schema: {
username: String,
// ... etc.
}
})
We use standard
as code style and for linting.
$ npm install --global standard snazzy
$ standard | snazzy
$ meteor npm install --global standard snazzy
$ standard | snazzy
- 1.0.3
- accept
null
values as name to create local collections
- accept
We use meteortesting:mocha
to run our tests on the package.
$ TEST_WATCH=1 TEST_CLIENT=0 meteor test-packages ./ --driver-package meteortesting:mocha