Leaonline Factories
Attention
This package has been split and is not maintained anymore. Please use the single factory packages:
- collection factory
- method factory
- publication factory
- ratelimit factory
- http routes factory
- GridFS FilesCollection factory
Provides an easy way to create Meteor Collections, Methods, Publications using factory methods.
History
1.2.0
- reorganized package structure
- code splitting for server and client
- updated
ostrio:files
to 1.14.1 - remove dependency to
leaonline:errors
How to use this package
First install this package
$ meteor add leaonline:factories
Then you need to initialize each new factory with respective content to inject. The following section describes the respective steps for each factory.
Create Mongo.Collection
There are two types of collections: Mongo collections and Files collections. To create a Mongo Collection factory you need to inject a schema resolver like so:
1import SimpleSchema from 'simpl-schema' 2 3const schemaResolver = (schema, options) => new SimpleSchema(schema, options) 4const createCollection = getCreateCollection(schemaResolver, { attachSchema: true }) 5const MyCollection = createCollection({ 6 name: 'myCollection', 7 schema: { 8 foo: String, 9 bar: Number 10 } 11})
If you have aldeed:collection2
installed, it will attach the schema, unless you pass the option
{ attachSchema: false }
.
Create FilesCollection
Files collections are currently implemented using ostrio:files
The factory functions require different parameters on server and client. However, since the parameters are wrapped
in an object you may pass in whatever you like.
Note that the current factory function implementation uses GridFSBucket
in order to store
the file data in GridFS. In order to create a bucket, please follow the new GridFS guide.
Server
1import { FilesCollection } from 'meteor/ostrio:files' 2 3const createFilesCollection = getCreateFilesCollection(FilesColletion, { i18n, fs, bucket, createObjectId }) 4const MyFilesCollection = createFilesCollection({ 5 name, 6 collection, 7 ddp, 8 allowedOrigins, 9 roles, 10 group, 11 debug, 12 maxSize, 13 extensions, 14 onBeforeUpload, 15 onAfterUpload 16})
The FilesCollection
needs to be injected in order to avoid hard coupling of the package with ostrio:files
.
Client
1import { FilesCollection } from 'meteor/ostrio:files' 2 3const createFilesCollection = getCreateFilesCollection(FilesCollection, { i18n }) 4const MyFilesCollection = createFilesCollection({ name, collection, ddp, roles, group, debug, maxSize, extensions, onBeforeUpload })
Create ValidatedMethod
Validated Meteor Methods can be created as with mdg:validated-method
but with extended options (implemented via mixins):
1import { ValidatedMethod } from 'meteor/mdg:validated-method' 2import SimpleSchema from 'simpl-schema' 3 4const validate = true 5const useRoles = true 6const schemaResolver = (schema, options) => new SimpleSchema(schema, options) 7const createMethod = getCreateMethod(ValidatedMethod, schemaResolver, validate, useRoles) 8const validatedMethod = createMethod({ 9 name: 'updatePost', 10 schema: { 11 postId: String, 12 text: String 13 }, 14 roles: ['edit-posts'], 15 group: 'editors', 16 isPublic: false, 17 run: function({ postId, text }) { 18 return Posts.update(postId, { $set: { text }}) 19 } 20})
We need to inject ValidatedMethod
in order to avoid hard coupling between the mdg:validated-method
package and this
package.
Method parameters
The following parameters are defined:
name
Name of the method as when using Meteor.call
schema
A definitions object, that is validated using your schemaResolver.
The example uses SimpleSchema
but you could also use a schema resolver, that is based
on other validations (for examples Meteor's builtin check
or 3rd party npm packages).
roles
If the method is restricted to certain roles you can pass in an array of roles.
See alanning:roles
for further documentation.
group
This is the group field for alanning:roles
isPublic
If this is true, there won't be any check if the current method is executed by a valid registered account.
Otherwise the check will throw on any this.userId
value that does not match in Meteor.users
.
run
This is basically the part that executes the method's core business logic. Runs only after all checks have been passed!
Create ValidatedPublication
Validated publications are a custom approach to create publications the same way as validated methods.
1import SimpleSchema from 'simpl-schema' 2 3const schemaResolver = (schema, options) => new SimpleSchema(schema, options) 4const createPublication = getCreatePublication(schemaResolver) 5createPublication({ 6 name: 'allPosts', 7 schema: {}, 8 roles: ['read-posts'], 9 group: 'editors', 10 isPublic: false, 11 run: function() { 12 return Posts.find() 13 } 14})
ValidatedPublication
is an internal class and there is no need to inject this for now.
Create HTTP Route (via WebApp connect handlers)
While there are frameworks like express
to create HTTP / REST routes, we have
created a factory, that builts upon Meteor's internal webapp
(connect) api.
1 2const createRoute = getCreateRoute({ schemaResolver, allowedOrigins, debug, xAuthToken }) 3createRoute({ 4 path: '/posts', 5 schema: { 6 postId: String 7 }, 8 method: 'get', 9 hasNext: false, 10 tokenRequired: false, 11 run: function({ postId }) { 12 return Posts.findOne(postId) 13 } 14})
Sidenode on the x-auth-token
: This is an inofficial header we use to fast-authenticate requests.
The token should be a secret that only this application server and your consumer (be it a client or external server)
possess. Note the following discussion on get requests: https://stackoverflow.com/questions/2629222/are-querystring-parameters-secure-in-https-http-ssl
Rate limiting
The functionality for rate limiting is separated from the factories. This gives you the freedom to decide what, when and how to rate limit the factory products. The following methods are provided by rate limiting:
runRateLimiter
Call this after all products have been added for rate limiting using the methods below.
rateLimitMethod
Rate limit a single method.
rateLimitMethods
Rate limit a list of methods
rateLimitPublications
Rate limit a single publication
rateLimitAccounts
Rate limit builtin Meteor and accounts functions
Contributing
This package is under active development and any contribution is very welcomed!
License
MIT, see LICENSE