Commit 36b159afdf90627db752ff062f7b01b7a8c2ad5a

Authored by Ryan Glover
1 parent 463581e297
Exists in master

Add server/methods directory.

Add /insert, /remove, /update, and /utility with examples to /server/methods.
Closes #28.
server/methods/insert/example.js
... ... @@ -0,0 +1,20 @@
  1 +/*
  2 +* Methods: Insert - Example
  3 +* Example of a method used for inserting into the database.
  4 +*/
  5 +
  6 +Meteor.methods({
  7 + exampleInsertMethod: function(argument){
  8 + // Check the argument. Assuming an Object type here.
  9 + check(argument, Object);
  10 +
  11 + // Perform the insert.
  12 + try {
  13 + var exampleId = Example.insert(argument);
  14 + return exampleId;
  15 + } catch(exception) {
  16 + // If an error occurs, return it to the client.
  17 + return exception;
  18 + }
  19 + }
  20 +});
... ...
server/methods/read/example.js
... ... @@ -0,0 +1,22 @@
  1 +/*
  2 +* Methods: Read - Example
  3 +* Example of a method used for reading from the database.
  4 +*/
  5 +
  6 +Meteor.methods({
  7 + exampleReadMethod: function(argument){
  8 + // Check the argument. Assuming a String type here.
  9 + check(argument, String);
  10 +
  11 + // Perform the read.
  12 + var exampleItem = Example.findOne(argument);
  13 +
  14 + // If the read fails (no documents found), throw an error.
  15 + if (!exampleItem) {
  16 + throw new Meteor.Error(500, 'Error 500: Not Found', 'No documents found.');
  17 + }
  18 +
  19 + // Return either the result or the error.
  20 + return exampleItem;
  21 + }
  22 +});
... ...
server/methods/remove/example.js
... ... @@ -0,0 +1,20 @@
  1 +/*
  2 +* Methods: Remove - Example
  3 +* Example of a method used for removing a document from the database.
  4 +*/
  5 +
  6 +Meteor.methods({
  7 + exampleRemoveMethod: function(argument){
  8 + // Check the argument. Assuming a String type here.
  9 + check(argument, String);
  10 +
  11 + // Perform the remove.
  12 + try {
  13 + var exampleId = Example.remove(argument);
  14 + return exampleId;
  15 + } catch(exception) {
  16 + // If an error occurs, return it to the client.
  17 + return exception;
  18 + }
  19 + }
  20 +});
... ...
server/methods/update/example.js
... ... @@ -0,0 +1,24 @@
  1 +/*
  2 +* Methods: Update - Example
  3 +* Example of a method used for updating a document in the database.
  4 +*/
  5 +
  6 +Meteor.methods({
  7 + exampleUpdateMethod: function(argument){
  8 + // Check the argument. Assuming an Object type here.
  9 + check(argument, Object);
  10 +
  11 + // Perform the update.
  12 + try {
  13 + var exampleId = Example.update(argument._id, {
  14 + $set: {
  15 + "someKey": argument.someKey
  16 + }
  17 + });
  18 + return exampleId;
  19 + } catch(exception) {
  20 + // If an error occurs, return it to the client.
  21 + return exception;
  22 + }
  23 + }
  24 +});
... ...
server/methods/utility/example.js
... ... @@ -0,0 +1,28 @@
  1 +/*
  2 +* Methods: Utility - Example
  3 +* Example of a method used for performing a function on the server.
  4 +*/
  5 +
  6 +// Example third-party API stub to call.
  7 +// This should be deleted and is only here as an example.
  8 +var chipotle = {
  9 + getBurrito: function(burrito){
  10 + return burrito;
  11 + }
  12 +}
  13 +
  14 +Meteor.methods({
  15 + exampleUtilityMethod: function(argument){
  16 + // Check the argument. Assuming an Object type here.
  17 + check(argument, Object);
  18 +
  19 + // Perform the function.
  20 + try {
  21 + var apiCall = chipotle.getBurrito("Barbacoa");
  22 + return apiCall;
  23 + } catch(exception) {
  24 + // If an error occurs, return it to the client.
  25 + return exception;
  26 + }
  27 + }
  28 +});
... ...