Collection API
Easily perform CRUD operations on Meteor Collections over HTTP/HTTPS from outside of the Meteor client or server environment.
Current version: 0.1.15 (Requires Meteor v0.6.5+)
Installation
With Metorite
$ mrt add collection-api
It's that easy! Be sure to check out other cool packages over at Atmosphere.
Quick Usage
1Players = new Meteor.Collection("players"); 2 3if (Meteor.isServer) { 4 Meteor.startup(function () { 5 6 // All values listed below are default 7 collectionApi = new CollectionAPI({ 8 authToken: undefined, // Require this string to be passed in on each request 9 apiPath: 'collectionapi', // API path prefix 10 standAlone: false, // Run as a stand-alone HTTP(S) server 11 sslEnabled: false, // Disable/Enable SSL (stand-alone only) 12 listenPort: 3005, // Port to listen to (stand-alone only) 13 listenHost: undefined, // Host to bind to (stand-alone only) 14 privateKeyFile: 'privatekey.pem', // SSL private key file (only used if SSL is enabled) 15 certificateFile: 'certificate.pem' // SSL certificate key file (only used if SSL is enabled) 16 }); 17 18 // Add the collection Players to the API "/players" path 19 collectionApi.addCollection(Players, 'players', { 20 // All values listed below are default 21 authToken: undefined, // Require this string to be passed in on each request 22 methods: ['POST','GET','PUT','DELETE'], // Allow creating, reading, updating, and deleting 23 before: { // This methods, if defined, will be called before the POST/GET/PUT/DELETE actions are performed on the collection. If the function returns false the action will be canceled, if you return true the action will take place. 24 POST: undefined, // function(obj) {return true/false;}, 25 GET: undefined, // function(collectionID, objs) {return true/false;}, 26 PUT: undefined, //function(collectionID, obj, newValues) {return true/false;}, 27 DELETE: undefined, //function(collectionID, obj) {return true/false;} 28 } 29 }); 30 31 // Starts the API server 32 collectionApi.start(); 33 }); 34}
Using the API
If you specify an authToken
it must be passed in either the X-Auth-Token
request header or as an auth-token
param in the query string.
API Usage Example
1Players = new Meteor.Collection("players"); 2 3if (Meteor.isServer) { 4 Meteor.startup(function () { 5 collectionApi = new CollectionAPI({ authToken: '97f0ad9e24ca5e0408a269748d7fe0a0' }); 6 collectionApi.addCollection(Players, 'players'); 7 collectionApi.start(); 8 }); 9}
Get all of the player records:
$ curl -H "X-Auth-Token: 97f0ad9e24ca5e0408a269748d7fe0a0" http://localhost:3000/collectionapi/players
Get an individual record:
$ curl -H "X-Auth-Token: 97f0ad9e24ca5e0408a269748d7fe0a0" http://localhost:3000/collectionapi/players/c4acddd1-a504-4212-9534-adca17af4885
Create a record:
$ curl -H "X-Auth-Token: 97f0ad9e24ca5e0408a269748d7fe0a0" -d "{\"name\": \"John Smith\"}" http://localhost:3000/collectionapi/players
Update a record:
$ curl -H "X-Auth-Token: 97f0ad9e24ca5e0408a269748d7fe0a0" -X PUT -d "{\"\$set\":{\"gender\":\"male\"}}" http://localhost:3000/collectionapi/players/c4acddd1-a504-4212-9534-adca17af4885
Delete a record:
$ curl -H "X-Auth-Token: 97f0ad9e24ca5e0408a269748d7fe0a0" -X DELETE http://localhost:3000/collectionapi/players/c4acddd1-a504-4212-9534-adca17af4885