quave:collections
quave:collections
is a Meteor package that allows you to create your collections in a standard way.
Features
- Schemas
- Types
- Helpers
- Hooks
- Composers
Why
Every application that connects to databases usually need the following features:
- A way to access object instances when they come from the database: helpers
- Provide new methods to collections: collection
- Add a few hooks to react to changes in different collections: hooks
- Map some types to avoid manual conversion all the time: types
- Valid the data before persisting: schemas
- Centralize behaviors: composers
Meteor has packages for almost all these use cases but it's not easy to find the best in each case and also how to use them together, that is why we have created this package.
We offer here a standard way for you to create your collections by configuring all these features in a function call createCollection
using a bunch of options in a declarative way and without using Javascript classes.
We also allow you to extend your Meteor.users
collection in the same way as any other collection.
We believe we are not reinventing the wheel in this package but what we are doing is like putting together the wheels in the vehicle :).
Installation
meteor add quave:collections
Optional installations
To use Type or Hooks options you need to install meteor-collection-hooks
meteor add matb33:collection-hooks
To use Schema options you need to install meteor-collection2
meteor add aldeed:collection2 meteor npm install simpl-schema
To use Helpers options you need to install meteor-collection-helpers
meteor add dburles:collection-helpers
Check the documentation of each package to learn how to use them.
Usage
Methods
Example applying collection
property:
1import { createCollection } from 'meteor/quave:collections'; 2 3export const AddressesCollection = createCollection({ 4 name: 'addresses', 5 collection: { 6 save(addressParam) { 7 const address = { ...addressParam }; 8 9 if (address._id) { 10 this.update(address._id, { $set: { ...address } }); 11 return address._id; 12 } 13 delete address._id; 14 return this.insert({ ...address }); 15 }, 16 }, 17});
Schema
Example applying SimpleSchema
:
1import { createCollection } from 'meteor/quave:collections'; 2 3import SimpleSchema from 'simpl-schema'; 4 5const PlayerSchema = new SimpleSchema({ 6 name: { 7 type: String, 8 }, 9 age: { 10 type: SimpleSchema.Integer, 11 }, 12}); 13 14export const PlayersCollection = createCollection({ 15 name: 'players', 16 schema: PlayerSchema, 17});
Composers
Example creating a way to paginate the fetch of data using composers
1import { createCollection } from 'meteor/quave:collections'; 2 3const LIMIT = 7; 4export const paginable = collection => 5 Object.assign({}, collection, { 6 getPaginated({ selector, options = {}, paginationAction }) { 7 const { skip, limit } = paginationAction || { skip: 0, limit: LIMIT }; 8 const items = this.find(selector, { 9 ...options, 10 skip, 11 limit, 12 }).fetch(); 13 const total = this.find(selector).count(); 14 const nextSkip = skip + limit; 15 const previousSkip = skip - limit; 16 17 return { 18 items, 19 pagination: { 20 total, 21 totalPages: parseInt(total / limit, 10) + (total % limit > 0 ? 1 : 0), 22 currentPage: 23 parseInt(skip / limit, 10) + (skip % limit > 0 ? 1 : 0) + 1, 24 ...(nextSkip < total ? { next: { skip: nextSkip, limit } } : {}), 25 ...(previousSkip >= 0 26 ? { previous: { skip: previousSkip, limit } } 27 : {}), 28 }, 29 }; 30 }, 31 }); 32 33export const StoresCollection = createCollection({ 34 name: 'stores', 35 composers: [paginable], 36}); 37 38// This probably will come from the client, using Methods, REST, or GraphQL 39// const paginationAction = {skip: XXX, limit: YYY}; 40 41const { items, pagination } = StoresCollection.getPaginated({ 42 selector: { 43 ...(search ? { name: { $regex: search, $options: 'i' } } : {}), 44 }, 45 options: { sort: { updatedAt: -1 } }, 46 paginationAction, 47});
Options
Second argument for the default collections constructor. Example defining a transform function.
1const transform = doc => ({ 2 ...doc, 3 get user() { 4 return Meteor.users.findOne(this.userId); 5 }, 6}); 7 8export const PlayersCollection = createCollection({ 9 name: 'players', 10 schema, 11 options: { 12 transform, 13 }, 14});
Meteor.users
Extending Meteor.users, also using collection
, helpers
, composers
, apply
.
You can use all these options also with name
instead of instance
.
1import { createCollection } from 'meteor/quave:collections'; 2 3export const UsersCollection = createCollection({ 4 instance: Meteor.users, 5 schema: UserSchema, 6 collection: { 7 isAdmin(userId) { 8 const user = userId && this.findOne(userId, { fields: { profiles: 1 } }); 9 return ( 10 user && user.profiles && user.profiles.includes(UserProfile.ADMIN.name) 11 ); 12 }, 13 }, 14 helpers: { 15 toPaymentGatewayJson() { 16 return { 17 country: 'us', 18 external_id: this._id, 19 name: this.name, 20 type: 'individual', 21 email: this.email, 22 }; 23 }, 24 }, 25 composers: [paginable], 26 apply(coll) { 27 coll.after.insert(userAfterInsert(coll), { fetchPrevious: false }); 28 coll.after.update(userAfterUpdate); 29 }, 30});
Limitations
-
You can't apply
type
andtypeFields
when you inform an instance of a MongoDB collection, usually you only use an instance forMeteor.users
. In this case I would recommend you to don't add fields with custom types to the users documents. -
If you want to use your objects from the database also in the client but you don't use your whole collection in client (you are not using Mini Mongo) you need to instantiate your type also in the client, you can do this importing your type and calling
register
. This is important to register it as an EJSON type.
License
MIT