2016-09-14 16:04:08 +03:00
|
|
|
Welcome to Grapher
|
|
|
|
==================
|
|
|
|
|
2016-09-15 10:37:10 +03:00
|
|
|
[](https://api.travis-ci.org/cult-of-coders/grapher)
|
2016-09-16 19:22:12 +03:00
|
|
|
[](https://raw.githubusercontent.com/hyperium/hyper/master/LICENSE)
|
2016-09-14 16:04:08 +03:00
|
|
|
|
|
|
|
General
|
|
|
|
-------
|
2016-09-15 09:14:15 +03:00
|
|
|
*Grapher* is a Meteor package that will enhance the way you are used to fetch data from your collections.
|
2016-09-14 16:04:08 +03:00
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
Updates
|
|
|
|
-------
|
|
|
|
Check-out the [CHANGELOG](CHANGELOG.md) for latest updates.
|
|
|
|
|
2016-09-15 11:17:23 +03:00
|
|
|
Installation
|
|
|
|
------------
|
|
|
|
```
|
|
|
|
meteor add cultofcoders:grapher
|
|
|
|
```
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
|
2016-09-14 16:04:08 +03:00
|
|
|
Documentation
|
|
|
|
-------------
|
|
|
|
|
2016-09-15 10:37:10 +03:00
|
|
|
Please read the documentation:
|
2016-09-15 09:14:15 +03:00
|
|
|
|
|
|
|
- [Collection Links](docs/links.md)
|
|
|
|
- [Exposing Collections](docs/exposure.md)
|
|
|
|
- [Query](docs/query.md)
|
2016-09-15 10:37:10 +03:00
|
|
|
|
|
|
|
|
|
|
|
Reference API
|
|
|
|
=============
|
|
|
|
|
|
|
|
Collection Links
|
|
|
|
-------------------
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
#### Adding Links
|
2016-09-15 10:37:10 +03:00
|
|
|
```
|
2016-09-16 19:22:12 +03:00
|
|
|
PoliceMen.addLinks({
|
|
|
|
donuts: {
|
2016-09-15 10:37:10 +03:00
|
|
|
type: 'one'|'many',
|
|
|
|
collection: RelatedCollection,
|
|
|
|
field: 'relatedId' // optional, it generates you custom one
|
2016-09-16 19:22:12 +03:00
|
|
|
metadata: {} // use it for meta relationships
|
|
|
|
index: false // if set to true will create indexes for the links
|
|
|
|
autoremove: false // if set to true it will remove the link
|
2016-09-15 10:37:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
Donuts.addLinks({
|
|
|
|
policemen: {
|
|
|
|
collection: PoliceMen,
|
|
|
|
inversedBy: 'donuts'
|
2016-09-15 10:37:10 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
#### Fetching Links
|
2016-09-15 10:37:10 +03:00
|
|
|
```
|
|
|
|
const link = Collection.getLink(docId, 'linkName');
|
|
|
|
|
2016-09-15 11:14:46 +03:00
|
|
|
link.find([filters], [options]); // returns Mongo.Cursor
|
|
|
|
link.fetch([filters], [options]); // returns object or array of objects depending on relationship type.
|
|
|
|
link.find([filters], [options]).fetch(); // always returns array of objects
|
2016-09-16 19:22:12 +03:00
|
|
|
```
|
2016-09-15 11:14:46 +03:00
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
#### Managing Links (One Relationship)
|
|
|
|
```
|
2016-09-15 10:37:10 +03:00
|
|
|
// for one relationships
|
|
|
|
link.set(relatedId);
|
|
|
|
link.unset();
|
2016-09-16 19:22:12 +03:00
|
|
|
```
|
2016-09-15 10:37:10 +03:00
|
|
|
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
#### Managing Links (One Meta Relationship)
|
|
|
|
```
|
|
|
|
link.set(relatedId, {isSomething: true);
|
|
|
|
link.metadata(); // returns the metadata: // {_id: relatedId, isSomething: true}
|
|
|
|
link.metadata({isSomething: false}); // sets the metadata by extending the current metadata.
|
2016-09-15 10:37:10 +03:00
|
|
|
link.metadata({otherStuff: true}); // will update the metadata
|
2016-09-16 19:22:12 +03:00
|
|
|
link.metadata(); // returns the metadata: {_id: relatedId, someConditions: true, otherStuff: true}
|
|
|
|
link.unset(); // removes the link along with metadata.
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Managing Links (Many Relationship)
|
|
|
|
```
|
|
|
|
link.add(relatedId) // accepts as arguments: [relatedId1, relatedId2], relatedObject, [relatedObject1, relatedObject2]
|
|
|
|
link.add(objectWithoutIdProperty) // it will create the object and save it in the db and also link it
|
|
|
|
link.add([objectWithoutIdProperty, _id]) // it will create the object objectWithoutIdProperty, and just link it with _id string.
|
2016-09-15 10:37:10 +03:00
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
link.remove(relatedId) // accepts as arguments: [relatedId1, relatedId2], relatedObject, [relatedObject1, relatedObject2]
|
|
|
|
```
|
|
|
|
|
|
|
|
#### Managing Links (Many Meta Relationship)
|
|
|
|
```
|
|
|
|
link.add(relatedId, {someConditions: true}); // it also works with [relatedId1, relatedId2, relatedObj1, relatedObj2]
|
2016-09-15 10:37:10 +03:00
|
|
|
link.metadata(relatedId) // {_id: relatedId, someConditions: true}
|
2016-09-16 19:22:12 +03:00
|
|
|
link.metadata() // [{_id: relatedId, someConditions: true}, {_id: relatedI2, someThingElse: false}, ...]
|
2016-09-15 10:37:10 +03:00
|
|
|
link.metadata(relatedId, {otherStuff: true}); // will update the metadata
|
|
|
|
link.metadata(relatedId) // {_id: relatedId, someConditions: true, otherStuff: true}
|
2016-09-16 19:22:12 +03:00
|
|
|
link.remove(relatedId); // will remove the link with the relatedId along with the metadata
|
2016-09-15 10:37:10 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
Exposing Collections
|
|
|
|
--------------------
|
|
|
|
```
|
|
|
|
Collection.expose((filters, options, userId) => {
|
|
|
|
if (!isAdmin(userId)) {
|
|
|
|
filters.userId = userId;
|
|
|
|
}
|
2016-09-16 19:22:12 +03:00
|
|
|
// here you can do stuff like restrict options.fields or override certain filters
|
2016-09-15 10:37:10 +03:00
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
|
2016-09-15 10:37:10 +03:00
|
|
|
Query
|
|
|
|
-----
|
2016-09-16 19:22:12 +03:00
|
|
|
|
|
|
|
If you crate your collection like:
|
2016-09-15 10:37:10 +03:00
|
|
|
```
|
2016-09-16 19:22:12 +03:00
|
|
|
const Posts = new Mongo.Collection('posts')
|
|
|
|
```
|
|
|
|
|
|
|
|
Then "posts" is the name of your collection.
|
|
|
|
|
|
|
|
You have two ways of creating the query:
|
|
|
|
```
|
|
|
|
import { createQuery } from 'meteor/cultofcoders:grapher';
|
|
|
|
|
|
|
|
createQuery({
|
|
|
|
posts: {
|
|
|
|
title: 1
|
|
|
|
}
|
|
|
|
}, [parameters])
|
|
|
|
```
|
|
|
|
|
|
|
|
OR from the collection directly:
|
|
|
|
```
|
|
|
|
const query = Posts.createQuery({
|
|
|
|
// $all: 1, // use this only when you want all fields without specifying them (NOT RECOMMENDED)
|
2016-09-15 10:37:10 +03:00
|
|
|
$filter({filters, options, params}) {
|
|
|
|
filters.isApproved = true;
|
|
|
|
options.limit = params.limit;
|
|
|
|
},
|
|
|
|
$options: {
|
|
|
|
sort: {createdAt: -1}
|
|
|
|
},
|
|
|
|
createdAt: 1,
|
2016-09-16 19:22:12 +03:00
|
|
|
title: 1,
|
|
|
|
comments: {
|
2016-09-15 10:37:10 +03:00
|
|
|
$filter({filters, options, params}) {
|
2016-09-16 19:22:12 +03:00
|
|
|
if (params.categories) {
|
|
|
|
filters.categoryId = {$in: categories}
|
2016-09-15 10:37:10 +03:00
|
|
|
}
|
|
|
|
},
|
2016-09-16 19:22:12 +03:00
|
|
|
author: {
|
2016-09-15 10:37:10 +03:00
|
|
|
$filters: {
|
2016-09-16 19:22:12 +03:00
|
|
|
isApproved: true // if the author is not approved, comment.author will be empty
|
|
|
|
},
|
2016-09-15 10:37:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}, {
|
2016-09-16 19:22:12 +03:00
|
|
|
categories: ['id1', 'id2'],
|
2016-09-15 10:37:10 +03:00
|
|
|
limit: 100;
|
|
|
|
});
|
2016-09-16 19:22:12 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
#### Query - Playing with Dynamic Queries
|
2016-09-15 10:37:10 +03:00
|
|
|
|
2016-09-16 19:22:12 +03:00
|
|
|
```
|
2016-09-15 10:37:10 +03:00
|
|
|
query.setParams({limit: 200});
|
2016-09-16 19:22:12 +03:00
|
|
|
```
|
|
|
|
|
|
|
|
#### Query - Fetching Data
|
|
|
|
|
|
|
|
```
|
|
|
|
// server side
|
|
|
|
const data = query.fetch();
|
2016-09-15 10:37:10 +03:00
|
|
|
|
|
|
|
// client side
|
|
|
|
query.fetch((error, response) => {...});
|
|
|
|
|
|
|
|
// reactive
|
|
|
|
query.subscribe();
|
2016-09-16 19:22:12 +03:00
|
|
|
query.fetch(); // will fetch the data from client-side collections. and store the resulted links in the documents.
|
|
|
|
query.unsubscribe();
|
2016-09-15 10:37:10 +03:00
|
|
|
```
|