akyma:astronomy

v3.0.1Published 4 months ago

Astronomy for Meteor

Gitter

The Astronomy package introduces the Model Layer into Meteor applications. It can also be named the Object Document Mapping system (ODM) or for people coming from relational database environments the Object-Relational Mapping system (ORM). Astronomy extends MongoDB documents with functionalities defined in a schema.

Documentation

Astronomy documentation can be found here.

Tutorials

You can learn more about Astronomy by watching video tutorials that I'm creating. I'm trying to add a new one every week. You can access them here https://goo.gl/9gnrav

Installation

$ meteor add akyma:astronomy

Support Astronomy development

I've decided to start Patreon page. If you enjoy using Astronomy and want to support development of future versions, then any donation will be welcome :).

Introduction

When fetching documents from Mongo collections, you get plain JavaScript objects without any logic. You have to validate values of objects' properties, check what fields have changed, save only modified fields, transform values coming from forms, in every place you are playing with a document; a lot of things to do. Wouldn't it be great if you could define some simple rules and leave everything else to framework? It's actually possible thanks to Astronomy. But first let's take a look at how your code would look like without using Astronomy.

1var post = Posts.findOne(id);
2// Assign values manually instead doing it automatically.
3post.createdAt = new Date();
4post.userId = Meteor.userId();
5// Manually convert values coming from the form.
6post.title = tmpl.find('input[name=title]').value;
7post.publishedAt = new Date(tmpl.find('input[name=publishedAt]').value);
8// Every time implement custom validation logic.
9if (post.title.length < 3) {
10  // Implement an error messages system.
11  throw new Error('The "title" field has to be at least 3 characters long');
12} else {
13  // Detect what fields have changed and update only those.
14  // Access collection directly.
15  Posts.update({
16    _id: post._id
17  }, {
18    $set: {
19      title: post.title,
20      publishedAt: post.publishedAt,
21      createdAt: post.updateAt
22    }
23  });
24}

With Astronomy and defined schema your code would look like follows:

1// Notice that we call the "findOne" method
2// from the "Post" class not from the "Posts" collection.
3var post = Post.findOne(id);
4// Auto convert a string input value to a number.
5post.title = tmpl.find('input[name=title]').value;
6post.publishedAt = new Date(tmpl.find('input[name=publishedAt]').value);
7// Check if all fields are valid and update document
8// with only the fields that have changed.
9post.save();

What approach is simpler? I think the choice is obvious.

For clarity, here is a sample schema that allows that. May seem to be a lot of code but have in mind that you write it only once.

1import { Class } from 'meteor/akyma:astronomy';
2
3const Posts = new Mongo.Collection('posts');
4const Post = Class.create({
5  name: 'Post',
6  collection: Posts,
7  fields: {
8    title: {
9      type: String,
10      validators: [{
11        type: 'minLength',
12        param: 3
13      }]
14    },
15    userId: String,
16    publishedAt: Date
17  },
18  behaviors: {
19    timestamp: {}
20  }
21});

Supporters

Contribution

Bigs thanks for all the contributions in form of commits and bug reports. Without you it would not be possible to improve Astronomy. Special thanks to:

  • Faberle - for help with Meteor methods feature
  • Ben305 - for several PRs
  • peterchoo - for several PRs
  • talha-asad - for updating History of changes
  • all other commiters that I forgot to mention

If you have any suggestions or want to write new features or behaviors please contact me, or just create an issue or a pull request. If you found any error please create a reproduction repository and create an issue. Thanks to that it will be easier for me to tell what is wrong. Please, don't use CoffeeScript for creating a reproduction.

License

Astronomy is released under the MIT License.